| 1 |
/* |
| 2 |
* The Mana World |
| 3 |
* Copyright (C) 2004 The Mana World Development Team |
| 4 |
* |
| 5 |
* This file is part of The Mana World. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "beingmanager.h" |
| 23 |
#include "channelmanager.h" |
| 24 |
#include "commandhandler.h" |
| 25 |
#include "configuration.h" |
| 26 |
#include "effectmanager.h" |
| 27 |
#include "emoteshortcut.h" |
| 28 |
#include "engine.h" |
| 29 |
#include "flooritemmanager.h" |
| 30 |
#include "game.h" |
| 31 |
#include "graphics.h" |
| 32 |
#include "itemshortcut.h" |
| 33 |
#include "joystick.h" |
| 34 |
#include "keyboardconfig.h" |
| 35 |
#include "localplayer.h" |
| 36 |
#include "log.h" |
| 37 |
#include "main.h" |
| 38 |
#include "map.h" |
| 39 |
#include "npc.h" |
| 40 |
#include "particle.h" |
| 41 |
#include "playerrelations.h" |
| 42 |
#include "sound.h" |
| 43 |
|
| 44 |
#include "gui/widgets/chattab.h" |
| 45 |
#include "gui/buy.h" |
| 46 |
#include "gui/buysell.h" |
| 47 |
#include "gui/chat.h" |
| 48 |
#include "gui/confirmdialog.h" |
| 49 |
#include "gui/debugwindow.h" |
| 50 |
#include "gui/emoteshortcutcontainer.h" |
| 51 |
#include "gui/equipmentwindow.h" |
| 52 |
#include "gui/gui.h" |
| 53 |
#include "gui/help.h" |
| 54 |
#include "gui/inventorywindow.h" |
| 55 |
#include "gui/shortcutwindow.h" |
| 56 |
#include "gui/itemshortcutcontainer.h" |
| 57 |
#include "gui/minimap.h" |
| 58 |
#include "gui/ministatus.h" |
| 59 |
#include "gui/npcdialog.h" |
| 60 |
#include "gui/okdialog.h" |
| 61 |
#include "gui/outfitwindow.h" |
| 62 |
#include "gui/sdlinput.h" |
| 63 |
#include "gui/sell.h" |
| 64 |
#include "gui/setup.h" |
| 65 |
#include "gui/skilldialog.h" |
| 66 |
#include "gui/statuswindow.h" |
| 67 |
#include "gui/trade.h" |
| 68 |
#include "gui/viewport.h" |
| 69 |
#include "gui/windowmenu.h" |
| 70 |
#include "gui/partywindow.h" |
| 71 |
#ifdef TMWSERV_SUPPORT |
| 72 |
#include "gui/buddywindow.h" |
| 73 |
#include "gui/guildwindow.h" |
| 74 |
#endif |
| 75 |
#include "gui/npcpostdialog.h" |
| 76 |
#include "gui/quitdialog.h" |
| 77 |
#include "gui/specialswindow.h" |
| 78 |
#include "gui/storagewindow.h" |
| 79 |
|
| 80 |
#include "net/gamehandler.h" |
| 81 |
#include "net/generalhandler.h" |
| 82 |
#include "net/net.h" |
| 83 |
|
| 84 |
#include "net/tmwserv/inventoryhandler.h" |
| 85 |
#include "net/ea/inventoryhandler.h" |
| 86 |
|
| 87 |
#include "resources/imagewriter.h" |
| 88 |
|
| 89 |
#include "utils/gettext.h" |
| 90 |
|
| 91 |
#include <guichan/exception.hpp> |
| 92 |
#include <guichan/focushandler.hpp> |
| 93 |
|
| 94 |
#include <fstream> |
| 95 |
#include <physfs.h> |
| 96 |
#include <sstream> |
| 97 |
#include <string> |
| 98 |
|
| 99 |
std::string map_path; |
| 100 |
|
| 101 |
bool done = false; |
| 102 |
volatile int tick_time; |
| 103 |
volatile int fps = 0, frame = 0; |
| 104 |
|
| 105 |
Engine *engine = NULL; |
| 106 |
Joystick *joystick = NULL; |
| 107 |
|
| 108 |
extern Window *weightNotice; |
| 109 |
extern Window *deathNotice; |
| 110 |
QuitDialog *quitDialog = NULL; |
| 111 |
OkDialog *disconnectedDialog = NULL; |
| 112 |
|
| 113 |
ChatWindow *chatWindow; |
| 114 |
StatusWindow *statusWindow; |
| 115 |
MiniStatusWindow *miniStatusWindow; |
| 116 |
BuyDialog *buyDialog; |
| 117 |
SellDialog *sellDialog; |
| 118 |
#ifdef EATHENA_SUPPORT |
| 119 |
BuySellDialog *buySellDialog; |
| 120 |
#endif |
| 121 |
InventoryWindow *inventoryWindow; |
| 122 |
SkillDialog *skillDialog; |
| 123 |
PartyWindow *partyWindow; |
| 124 |
#ifdef TMWSERV_SUPPORT |
| 125 |
BuddyWindow *buddyWindow; |
| 126 |
GuildWindow *guildWindow; |
| 127 |
#endif |
| 128 |
NpcDialog *npcDialog; |
| 129 |
NpcPostDialog *npcPostDialog; |
| 130 |
StorageWindow *storageWindow; |
| 131 |
Minimap *minimap; |
| 132 |
EquipmentWindow *equipmentWindow; |
| 133 |
TradeWindow *tradeWindow; |
| 134 |
HelpWindow *helpWindow; |
| 135 |
DebugWindow *debugWindow; |
| 136 |
ShortcutWindow *itemShortcutWindow; |
| 137 |
ShortcutWindow *emoteShortcutWindow; |
| 138 |
OutfitWindow *outfitWindow; |
| 139 |
SpecialsWindow *specialsWindow; |
| 140 |
|
| 141 |
BeingManager *beingManager = NULL; |
| 142 |
FloorItemManager *floorItemManager = NULL; |
| 143 |
ChannelManager *channelManager = NULL; |
| 144 |
CommandHandler *commandHandler = NULL; |
| 145 |
Particle *particleEngine = NULL; |
| 146 |
EffectManager *effectManager = NULL; |
| 147 |
|
| 148 |
ChatTab *localChatTab = NULL; |
| 149 |
|
| 150 |
/** |
| 151 |
* Tells the max tick value, |
| 152 |
* setting it back to zero (and start again). |
| 153 |
*/ |
| 154 |
const int MAX_TICK_VALUE = 10000; |
| 155 |
/** |
| 156 |
* Set the milliseconds value of a tick time. |
| 157 |
*/ |
| 158 |
const int MILLISECONDS_IN_A_TICK = 10; |
| 159 |
|
| 160 |
/** |
| 161 |
* Listener used for exiting handling. |
| 162 |
*/ |
| 163 |
namespace { |
| 164 |
class ExitListener : public gcn::ActionListener |
| 165 |
{ |
| 166 |
public: |
| 167 |
void action(const gcn::ActionEvent &event) |
| 168 |
{ |
| 169 |
if (event.getId() == "yes" || event.getId() == "ok") |
| 170 |
done = true; |
| 171 |
|
| 172 |
disconnectedDialog = NULL; |
| 173 |
} |
| 174 |
} exitListener; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Advances game logic counter. |
| 179 |
* Called every 10 milliseconds by SDL_AddTimer() |
| 180 |
* @see MILLISECONDS_IN_A_TICK value |
| 181 |
*/ |
| 182 |
Uint32 nextTick(Uint32 interval, void *param) |
| 183 |
{ |
| 184 |
tick_time++; |
| 185 |
if (tick_time == MAX_TICK_VALUE) tick_time = 0; |
| 186 |
return interval; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Updates fps. |
| 191 |
* Called every seconds by SDL_AddTimer() |
| 192 |
*/ |
| 193 |
Uint32 nextSecond(Uint32 interval, void *param) |
| 194 |
{ |
| 195 |
fps = frame; |
| 196 |
frame = 0; |
| 197 |
|
| 198 |
return interval; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @return the elapsed time in milliseconds |
| 203 |
* between two tick values. |
| 204 |
*/ |
| 205 |
int get_elapsed_time(int start_time) |
| 206 |
{ |
| 207 |
if (start_time <= tick_time) |
| 208 |
return (tick_time - start_time) * MILLISECONDS_IN_A_TICK; |
| 209 |
else |
| 210 |
return (tick_time + (MAX_TICK_VALUE - start_time)) |
| 211 |
* MILLISECONDS_IN_A_TICK; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Initialize every game sub-engines in the right order |
| 216 |
*/ |
| 217 |
static void initEngines() |
| 218 |
{ |
| 219 |
engine = new Engine; |
| 220 |
|
| 221 |
beingManager = new BeingManager; |
| 222 |
commandHandler = new CommandHandler; |
| 223 |
floorItemManager = new FloorItemManager; |
| 224 |
channelManager = new ChannelManager; |
| 225 |
effectManager = new EffectManager; |
| 226 |
|
| 227 |
particleEngine = new Particle(NULL); |
| 228 |
particleEngine->setupEngine(); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Create all the various globally accessible gui windows |
| 233 |
*/ |
| 234 |
static void createGuiWindows() |
| 235 |
{ |
| 236 |
setupWindow->clearWindowsForReset(); |
| 237 |
|
| 238 |
// Create dialogs |
| 239 |
chatWindow = new ChatWindow; |
| 240 |
buyDialog = new BuyDialog; |
| 241 |
sellDialog = new SellDialog; |
| 242 |
tradeWindow = new TradeWindow; |
| 243 |
partyWindow = new PartyWindow; |
| 244 |
#ifdef TMWSERV_SUPPORT |
| 245 |
buddyWindow = new BuddyWindow; |
| 246 |
guildWindow = new GuildWindow; |
| 247 |
#else |
| 248 |
buySellDialog = new BuySellDialog; |
| 249 |
#endif |
| 250 |
equipmentWindow = new EquipmentWindow(player_node->mEquipment.get()); |
| 251 |
npcDialog = new NpcDialog; |
| 252 |
npcPostDialog = new NpcPostDialog; |
| 253 |
storageWindow = new StorageWindow; |
| 254 |
statusWindow = new StatusWindow; |
| 255 |
miniStatusWindow = new MiniStatusWindow; |
| 256 |
inventoryWindow = new InventoryWindow; |
| 257 |
skillDialog = new SkillDialog; |
| 258 |
minimap = new Minimap; |
| 259 |
helpWindow = new HelpWindow; |
| 260 |
debugWindow = new DebugWindow; |
| 261 |
itemShortcutWindow = new ShortcutWindow("ItemShortcut", |
| 262 |
new ItemShortcutContainer); |
| 263 |
emoteShortcutWindow = new ShortcutWindow("EmoteShortcut", |
| 264 |
new EmoteShortcutContainer); |
| 265 |
outfitWindow = new OutfitWindow(); |
| 266 |
specialsWindow = new SpecialsWindow(); |
| 267 |
|
| 268 |
localChatTab = new ChatTab(_("General")); |
| 269 |
|
| 270 |
if (config.getValue("logToChat", 0)) |
| 271 |
{ |
| 272 |
logger->setChatWindow(chatWindow); |
| 273 |
} |
| 274 |
|
| 275 |
Net::getGeneralHandler()->guiWindowsLoaded(); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Destroy all the globally accessible gui windows |
| 280 |
*/ |
| 281 |
static void destroyGuiWindows() |
| 282 |
{ |
| 283 |
Net::getGeneralHandler()->guiWindowsUnloaded(); |
| 284 |
logger->setChatWindow(NULL); |
| 285 |
delete localChatTab; // Need to do this first, so it can remove itself |
| 286 |
delete chatWindow; |
| 287 |
delete statusWindow; |
| 288 |
delete miniStatusWindow; |
| 289 |
delete buyDialog; |
| 290 |
delete sellDialog; |
| 291 |
#ifdef EATHENA_SUPPORT |
| 292 |
delete buySellDialog; |
| 293 |
#endif |
| 294 |
delete inventoryWindow; |
| 295 |
delete partyWindow; |
| 296 |
delete npcDialog; |
| 297 |
delete npcPostDialog; |
| 298 |
#ifdef TMWSERV_SUPPORT |
| 299 |
delete buddyWindow; |
| 300 |
delete guildWindow; |
| 301 |
#endif |
| 302 |
delete skillDialog; |
| 303 |
delete minimap; |
| 304 |
delete equipmentWindow; |
| 305 |
delete tradeWindow; |
| 306 |
delete helpWindow; |
| 307 |
delete debugWindow; |
| 308 |
delete itemShortcutWindow; |
| 309 |
delete emoteShortcutWindow; |
| 310 |
delete storageWindow; |
| 311 |
delete outfitWindow; |
| 312 |
delete specialsWindow; |
| 313 |
} |
| 314 |
|
| 315 |
Game::Game(): |
| 316 |
mLastTarget(Being::UNKNOWN), |
| 317 |
mLogicCounterId(0), mSecondsCounterId(0) |
| 318 |
{ |
| 319 |
done = false; |
| 320 |
|
| 321 |
createGuiWindows(); |
| 322 |
|
| 323 |
mWindowMenu = new WindowMenu; |
| 324 |
windowContainer->add(mWindowMenu); |
| 325 |
|
| 326 |
initEngines(); |
| 327 |
|
| 328 |
// Initialize logic and seconds counters |
| 329 |
tick_time = 0; |
| 330 |
mLogicCounterId = SDL_AddTimer(MILLISECONDS_IN_A_TICK, nextTick, NULL); |
| 331 |
mSecondsCounterId = SDL_AddTimer(1000, nextSecond, NULL); |
| 332 |
|
| 333 |
// This part is eAthena specific |
| 334 |
// For TMWserv, the map is obtained |
| 335 |
// with the GPMSG_PLAYER_MAP_CHANGE flag. |
| 336 |
map_path = map_path.substr(0, map_path.rfind(".")); |
| 337 |
if (!map_path.empty()) |
| 338 |
engine->changeMap(map_path); |
| 339 |
|
| 340 |
// Initialize beings |
| 341 |
beingManager->setPlayer(player_node); |
| 342 |
|
| 343 |
/* |
| 344 |
* To prevent the server from sending data before the client |
| 345 |
* has initialized, I've modified it to wait for a "ping" |
| 346 |
* from the client to complete its initialization |
| 347 |
* |
| 348 |
* Note: This only affects the latest eAthena version. This |
| 349 |
* packet is handled by the older version, but its response |
| 350 |
* is ignored by the client |
| 351 |
*/ |
| 352 |
Net::getGameHandler()->ping(tick_time); |
| 353 |
|
| 354 |
// Initialize frame limiting |
| 355 |
config.addListener("fpslimit", this); |
| 356 |
optionChanged("fpslimit"); |
| 357 |
|
| 358 |
Joystick::init(); |
| 359 |
// TODO: The user should be able to choose which one to use |
| 360 |
// Open the first device |
| 361 |
if (Joystick::getNumberOfJoysticks() > 0) |
| 362 |
joystick = new Joystick(0); |
| 363 |
|
| 364 |
setupWindow->setInGame(true); |
| 365 |
} |
| 366 |
|
| 367 |
Game::~Game() |
| 368 |
{ |
| 369 |
delete mWindowMenu; |
| 370 |
|
| 371 |
destroyGuiWindows(); |
| 372 |
|
| 373 |
delete beingManager; |
| 374 |
delete player_node; |
| 375 |
delete floorItemManager; |
| 376 |
delete channelManager; |
| 377 |
delete commandHandler; |
| 378 |
delete joystick; |
| 379 |
delete particleEngine; |
| 380 |
delete engine; |
| 381 |
|
| 382 |
viewport->setMap(NULL); |
| 383 |
player_node = NULL; |
| 384 |
beingManager = NULL; |
| 385 |
floorItemManager = NULL; |
| 386 |
joystick = NULL; |
| 387 |
|
| 388 |
SDL_RemoveTimer(mLogicCounterId); |
| 389 |
SDL_RemoveTimer(mSecondsCounterId); |
| 390 |
} |
| 391 |
|
| 392 |
static bool saveScreenshot() |
| 393 |
{ |
| 394 |
static unsigned int screenshotCount = 0; |
| 395 |
|
| 396 |
SDL_Surface *screenshot = graphics->getScreenshot(); |
| 397 |
|
| 398 |
// Search for an unused screenshot name |
| 399 |
std::stringstream filenameSuffix; |
| 400 |
std::stringstream filename; |
| 401 |
std::fstream testExists; |
| 402 |
bool found = false; |
| 403 |
|
| 404 |
do { |
| 405 |
screenshotCount++; |
| 406 |
filename.str(""); |
| 407 |
filenameSuffix.str(""); |
| 408 |
filename << PHYSFS_getUserDir(); |
| 409 |
#if (defined __USE_UNIX98 || defined __FreeBSD__) |
| 410 |
filenameSuffix << ".tmw/"; |
| 411 |
#elif defined __APPLE__ |
| 412 |
filenameSuffix << "Desktop/"; |
| 413 |
#endif |
| 414 |
filenameSuffix << "TMW_Screenshot_" << screenshotCount << ".png"; |
| 415 |
filename << filenameSuffix.str(); |
| 416 |
testExists.open(filename.str().c_str(), std::ios::in); |
| 417 |
found = !testExists.is_open(); |
| 418 |
testExists.close(); |
| 419 |
} while (!found); |
| 420 |
|
| 421 |
const bool success = ImageWriter::writePNG(screenshot, filename.str()); |
| 422 |
|
| 423 |
if (success) |
| 424 |
{ |
| 425 |
std::stringstream chatlogentry; |
| 426 |
// TODO: Make it one complete gettext string below |
| 427 |
chatlogentry << _("Screenshot saved to ~/") << filenameSuffix.str(); |
| 428 |
localChatTab->chatLog(chatlogentry.str(), BY_SERVER); |
| 429 |
} |
| 430 |
else |
| 431 |
{ |
| 432 |
localChatTab->chatLog(_("Saving screenshot failed!"), BY_SERVER); |
| 433 |
logger->log("Error: could not save screenshot."); |
| 434 |
} |
| 435 |
|
| 436 |
SDL_FreeSurface(screenshot); |
| 437 |
|
| 438 |
return success; |
| 439 |
} |
| 440 |
|
| 441 |
void Game::optionChanged(const std::string &name) |
| 442 |
{ |
| 443 |
int fpsLimit = (int) config.getValue("fpslimit", 60); |
| 444 |
|
| 445 |
mMinFrameTime = fpsLimit ? 1000 / fpsLimit : 0; |
| 446 |
|
| 447 |
// Reset draw time to current time |
| 448 |
mDrawTime = tick_time * MILLISECONDS_IN_A_TICK; |
| 449 |
} |
| 450 |
|
| 451 |
void Game::logic() |
| 452 |
{ |
| 453 |
// mDrawTime has a higher granularity than gameTime in order to be able to |
| 454 |
// work with minimum frame durations in milliseconds. |
| 455 |
int gameTime = tick_time; |
| 456 |
mDrawTime = tick_time * MILLISECONDS_IN_A_TICK; |
| 457 |
|
| 458 |
while (!done) |
| 459 |
{ |
| 460 |
if (Map *map = engine->getCurrentMap()) |
| 461 |
map->update(get_elapsed_time(gameTime)); |
| 462 |
|
| 463 |
// Handle all necessary game logic |
| 464 |
while (get_elapsed_time(gameTime) > 0) |
| 465 |
{ |
| 466 |
handleInput(); |
| 467 |
engine->logic(); |
| 468 |
gameTime++; |
| 469 |
} |
| 470 |
|
| 471 |
// This is done because at some point tick_time will wrap. |
| 472 |
gameTime = tick_time; |
| 473 |
|
| 474 |
// Update the screen when application is active, delay otherwise. |
| 475 |
if (SDL_GetAppState() & SDL_APPACTIVE) |
| 476 |
{ |
| 477 |
// Draw a frame if either frames are not limited or enough time has |
| 478 |
// passed since the last frame. |
| 479 |
if (!mMinFrameTime || |
| 480 |
get_elapsed_time(mDrawTime / 10) > mMinFrameTime) |
| 481 |
{ |
| 482 |
frame++; |
| 483 |
gui->draw(); |
| 484 |
graphics->updateScreen(); |
| 485 |
mDrawTime += mMinFrameTime; |
| 486 |
|
| 487 |
// Make sure to wrap mDrawTime, since tick_time will wrap. |
| 488 |
if (mDrawTime > MAX_TICK_VALUE * MILLISECONDS_IN_A_TICK) |
| 489 |
mDrawTime -= MAX_TICK_VALUE * MILLISECONDS_IN_A_TICK; |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
SDL_Delay(MILLISECONDS_IN_A_TICK); |
| 494 |
} |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
SDL_Delay(MILLISECONDS_IN_A_TICK); |
| 499 |
mDrawTime = tick_time * MILLISECONDS_IN_A_TICK; |
| 500 |
} |
| 501 |
|
| 502 |
// Handle network stuff |
| 503 |
Net::getGeneralHandler()->flushNetwork(); |
| 504 |
if (!Net::getGameHandler()->isConnected()) |
| 505 |
{ |
| 506 |
if (state != STATE_ERROR) |
| 507 |
{ |
| 508 |
errorMessage = _("The connection to the server was lost, " |
| 509 |
"the program will now quit"); |
| 510 |
} |
| 511 |
|
| 512 |
if (!disconnectedDialog) |
| 513 |
{ |
| 514 |
disconnectedDialog = new OkDialog(_("Network Error"), errorMessage); |
| 515 |
disconnectedDialog->addActionListener(&exitListener); |
| 516 |
disconnectedDialog->requestMoveToTop(); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
void Game::handleInput() |
| 523 |
{ |
| 524 |
if (joystick) |
| 525 |
joystick->update(); |
| 526 |
|
| 527 |
// Events |
| 528 |
SDL_Event event; |
| 529 |
while (SDL_PollEvent(&event)) |
| 530 |
{ |
| 531 |
bool used = false; |
| 532 |
|
| 533 |
// Keyboard events (for discontinuous keys) |
| 534 |
if (event.type == SDL_KEYDOWN) |
| 535 |
{ |
| 536 |
gcn::Window *requestedWindow = NULL; |
| 537 |
|
| 538 |
if (setupWindow->isVisible() && |
| 539 |
keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE) |
| 540 |
{ |
| 541 |
keyboard.setNewKey((int) event.key.keysym.sym); |
| 542 |
keyboard.callbackNewKey(); |
| 543 |
keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE); |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
// send straight to gui for certain windows |
| 548 |
if (npcPostDialog->isVisible()) |
| 549 |
{ |
| 550 |
try |
| 551 |
{ |
| 552 |
guiInput->pushInput(event); |
| 553 |
} |
| 554 |
catch (gcn::Exception e) |
| 555 |
{ |
| 556 |
const char* err = e.getMessage().c_str(); |
| 557 |
logger->log("Warning: guichan input exception: %s", err); |
| 558 |
} |
| 559 |
return; |
| 560 |
} |
| 561 |
|
| 562 |
// Mode switch to emotes |
| 563 |
if (keyboard.isKeyActive(keyboard.KEY_EMOTE)) |
| 564 |
{ |
| 565 |
// Emotions |
| 566 |
int emotion = keyboard.getKeyEmoteOffset(event.key.keysym.sym); |
| 567 |
if (emotion) |
| 568 |
{ |
| 569 |
emoteShortcut->useEmote(emotion); |
| 570 |
used = true; |
| 571 |
return; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if (!chatWindow->isInputFocused() |
| 576 |
&& !gui->getFocusHandler()->getModalFocused()) |
| 577 |
{ |
| 578 |
if (keyboard.isKeyActive(keyboard.KEY_OK)) |
| 579 |
{ |
| 580 |
// Do not focus chat input when quit dialog is active |
| 581 |
if (quitDialog != NULL && quitDialog->isVisible()) |
| 582 |
continue; |
| 583 |
// Close the Browser if opened |
| 584 |
else if (helpWindow->isVisible() && |
| 585 |
keyboard.isKeyActive(keyboard.KEY_OK)) |
| 586 |
helpWindow->setVisible(false); |
| 587 |
// Close the config window, cancelling changes if opened |
| 588 |
else if (setupWindow->isVisible() && |
| 589 |
keyboard.isKeyActive(keyboard.KEY_OK)) |
| 590 |
setupWindow->action(gcn::ActionEvent(NULL, "cancel")); |
| 591 |
else if (npcDialog->isVisible() && |
| 592 |
keyboard.isKeyActive(keyboard.KEY_OK)) |
| 593 |
npcDialog->action(gcn::ActionEvent(NULL, "ok")); |
| 594 |
/* |
| 595 |
else if (guildWindow->isVisible()) |
| 596 |
{ |
| 597 |
// TODO: Check if a dialog is open and close it if so |
| 598 |
} |
| 599 |
*/ |
| 600 |
} |
| 601 |
if (keyboard.isKeyActive(keyboard.KEY_TOGGLE_CHAT)) |
| 602 |
{ |
| 603 |
if (chatWindow->requestChatFocus()) |
| 604 |
used = true; |
| 605 |
} |
| 606 |
if (npcDialog->isVisible()) |
| 607 |
{ |
| 608 |
if (keyboard.isKeyActive(keyboard.KEY_MOVE_UP)) |
| 609 |
npcDialog->move(1); |
| 610 |
else if (keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN)) |
| 611 |
npcDialog->move(-1); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
if (!chatWindow->isInputFocused() || (event.key.keysym.mod & KMOD_ALT)) |
| 617 |
{ |
| 618 |
if (keyboard.isKeyActive(keyboard.KEY_PREV_CHAT_TAB)) |
| 619 |
{ |
| 620 |
chatWindow->prevTab(); |
| 621 |
return; |
| 622 |
} |
| 623 |
else if (keyboard.isKeyActive(keyboard.KEY_NEXT_CHAT_TAB)) |
| 624 |
{ |
| 625 |
chatWindow->nextTab(); |
| 626 |
return; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
if ((event.key.keysym.mod & KMOD_RCTRL || event.key.keysym.mod & KMOD_LCTRL) |
| 631 |
&& !chatWindow->isInputFocused()) |
| 632 |
{ |
| 633 |
int outfitNum = -1; |
| 634 |
switch (event.key.keysym.sym) |
| 635 |
{ |
| 636 |
case SDLK_1: |
| 637 |
outfitNum = 0; |
| 638 |
break; |
| 639 |
|
| 640 |
case SDLK_2: |
| 641 |
outfitNum = 1; |
| 642 |
break; |
| 643 |
|
| 644 |
case SDLK_3: |
| 645 |
outfitNum = 2; |
| 646 |
break; |
| 647 |
|
| 648 |
case SDLK_4: |
| 649 |
outfitNum = 3; |
| 650 |
break; |
| 651 |
|
| 652 |
case SDLK_5: |
| 653 |
outfitNum = 4; |
| 654 |
break; |
| 655 |
|
| 656 |
case SDLK_6: |
| 657 |
outfitNum = 5; |
| 658 |
break; |
| 659 |
|
| 660 |
case SDLK_7: |
| 661 |
outfitNum = 6; |
| 662 |
break; |
| 663 |
|
| 664 |
case SDLK_8: |
| 665 |
outfitNum = 7; |
| 666 |
break; |
| 667 |
|
| 668 |
case SDLK_9: |
| 669 |
outfitNum = 8; |
| 670 |
break; |
| 671 |
|
| 672 |
case SDLK_0: |
| 673 |
outfitNum = 9; |
| 674 |
break; |
| 675 |
|
| 676 |
case SDLK_MINUS: |
| 677 |
outfitNum = 10; |
| 678 |
break; |
| 679 |
|
| 680 |
case SDLK_EQUALS: |
| 681 |
outfitNum = 11; |
| 682 |
break; |
| 683 |
|
| 684 |
case SDLK_BACKSPACE: |
| 685 |
outfitNum = 12; |
| 686 |
break; |
| 687 |
|
| 688 |
case SDLK_INSERT: |
| 689 |
outfitNum = 13; |
| 690 |
break; |
| 691 |
|
| 692 |
case SDLK_HOME: |
| 693 |
outfitNum = 14; |
| 694 |
break; |
| 695 |
|
| 696 |
default: |
| 697 |
break; |
| 698 |
} |
| 699 |
if (outfitNum >= 0) |
| 700 |
{ |
| 701 |
used = true; |
| 702 |
if (event.key.keysym.mod & KMOD_RCTRL) |
| 703 |
outfitWindow->wearOutfit(outfitNum); |
| 704 |
else if (event.key.keysym.mod & KMOD_LCTRL) |
| 705 |
outfitWindow->copyOutfit(outfitNum); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); |
| 710 |
switch (tKey) |
| 711 |
{ |
| 712 |
case KeyboardConfig::KEY_SCROLL_CHAT_UP: |
| 713 |
if (chatWindow->isVisible()) |
| 714 |
{ |
| 715 |
chatWindow->scroll(-DEFAULT_CHAT_WINDOW_SCROLL); |
| 716 |
used = true; |
| 717 |
} |
| 718 |
break; |
| 719 |
case KeyboardConfig::KEY_SCROLL_CHAT_DOWN: |
| 720 |
if (chatWindow->isVisible()) |
| 721 |
{ |
| 722 |
chatWindow->scroll(DEFAULT_CHAT_WINDOW_SCROLL); |
| 723 |
used = true; |
| 724 |
return; |
| 725 |
} |
| 726 |
break; |
| 727 |
case KeyboardConfig::KEY_WINDOW_HELP: |
| 728 |
// In-game Help |
| 729 |
if (helpWindow->isVisible()) |
| 730 |
helpWindow->setVisible(false); |
| 731 |
else |
| 732 |
{ |
| 733 |
helpWindow->loadHelp("index"); |
| 734 |
helpWindow->requestMoveToTop(); |
| 735 |
} |
| 736 |
used = true; |
| 737 |
break; |
| 738 |
// Quitting confirmation dialog |
| 739 |
case KeyboardConfig::KEY_QUIT: |
| 740 |
if (!quitDialog) |
| 741 |
{ |
| 742 |
quitDialog = new QuitDialog(&done, &quitDialog); |
| 743 |
quitDialog->requestMoveToTop(); |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
quitDialog->action(gcn::ActionEvent(NULL, "cancel")); |
| 748 |
} |
| 749 |
break; |
| 750 |
default: |
| 751 |
break; |
| 752 |
} |
| 753 |
if (keyboard.isEnabled() && |
| 754 |
!chatWindow->isInputFocused() && !npcDialog->isInputFocused()) |
| 755 |
{ |
| 756 |
const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); |
| 757 |
|
| 758 |
// Do not activate shortcuts if tradewindow is visible |
| 759 |
if (!tradeWindow->isVisible()) |
| 760 |
{ |
| 761 |
// Checks if any item shortcut is pressed. |
| 762 |
for (int i = KeyboardConfig::KEY_SHORTCUT_1; |
| 763 |
i <= KeyboardConfig::KEY_SHORTCUT_12; |
| 764 |
i++) |
| 765 |
{ |
| 766 |
if (tKey == i && !used) |
| 767 |
{ |
| 768 |
itemShortcut->useItem( |
| 769 |
i - KeyboardConfig::KEY_SHORTCUT_1); |
| 770 |
break; |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
switch (tKey) |
| 776 |
{ |
| 777 |
case KeyboardConfig::KEY_PICKUP: |
| 778 |
{ |
| 779 |
#ifdef TMWSERV_SUPPORT |
| 780 |
const Vector &pos = player_node->getPosition(); |
| 781 |
Uint16 x = (int) pos.x / 32; |
| 782 |
Uint16 y = (int) pos.y / 32; |
| 783 |
#else |
| 784 |
Uint16 x = player_node->getTileX(); |
| 785 |
Uint16 y = player_node->getTileY(); |
| 786 |
#endif |
| 787 |
FloorItem *item = |
| 788 |
floorItemManager->findByCoordinates(x, y); |
| 789 |
|
| 790 |
// If none below the player, try the tile in front |
| 791 |
// of the player |
| 792 |
if (!item) |
| 793 |
{ |
| 794 |
// Temporary until tile-based picking is |
| 795 |
// removed. |
| 796 |
switch (player_node->getSpriteDirection()) |
| 797 |
{ |
| 798 |
case DIRECTION_UP : --y; break; |
| 799 |
case DIRECTION_DOWN : ++y; break; |
| 800 |
case DIRECTION_LEFT : --x; break; |
| 801 |
case DIRECTION_RIGHT: ++x; break; |
| 802 |
default: break; |
| 803 |
} |
| 804 |
|
| 805 |
item = floorItemManager->findByCoordinates( |
| 806 |
x, y); |
| 807 |
} |
| 808 |
|
| 809 |
if (item) |
| 810 |
player_node->pickUp(item); |
| 811 |
|
| 812 |
used = true; |
| 813 |
} |
| 814 |
break; |
| 815 |
case KeyboardConfig::KEY_SIT: |
| 816 |
// Player sit action |
| 817 |
player_node->toggleSit(); |
| 818 |
used = true; |
| 819 |
break; |
| 820 |
case KeyboardConfig::KEY_HIDE_WINDOWS: |
| 821 |
// Hide certain windows |
| 822 |
if (!chatWindow->isInputFocused()) |
| 823 |
{ |
| 824 |
statusWindow->setVisible(false); |
| 825 |
inventoryWindow->setVisible(false); |
| 826 |
skillDialog->setVisible(false); |
| 827 |
setupWindow->setVisible(false); |
| 828 |
equipmentWindow->setVisible(false); |
| 829 |
helpWindow->setVisible(false); |
| 830 |
debugWindow->setVisible(false); |
| 831 |
#ifdef TMWSERV_SUPPORT |
| 832 |
guildWindow->setVisible(false); |
| 833 |
buddyWindow->setVisible(false); |
| 834 |
#endif |
| 835 |
} |
| 836 |
break; |
| 837 |
case KeyboardConfig::KEY_WINDOW_STATUS: |
| 838 |
requestedWindow = statusWindow; |
| 839 |
break; |
| 840 |
case KeyboardConfig::KEY_WINDOW_INVENTORY: |
| 841 |
requestedWindow = inventoryWindow; |
| 842 |
break; |
| 843 |
case KeyboardConfig::KEY_WINDOW_EQUIPMENT: |
| 844 |
requestedWindow = equipmentWindow; |
| 845 |
break; |
| 846 |
case KeyboardConfig::KEY_WINDOW_SKILL: |
| 847 |
requestedWindow = skillDialog; |
| 848 |
break; |
| 849 |
case KeyboardConfig::KEY_WINDOW_MINIMAP: |
| 850 |
minimap->toggle(); |
| 851 |
break; |
| 852 |
case KeyboardConfig::KEY_WINDOW_CHAT: |
| 853 |
requestedWindow = chatWindow; |
| 854 |
break; |
| 855 |
case KeyboardConfig::KEY_WINDOW_SHORTCUT: |
| 856 |
requestedWindow = itemShortcutWindow; |
| 857 |
break; |
| 858 |
case KeyboardConfig::KEY_WINDOW_SETUP: |
| 859 |
requestedWindow = setupWindow; |
| 860 |
break; |
| 861 |
case KeyboardConfig::KEY_WINDOW_DEBUG: |
| 862 |
requestedWindow = debugWindow; |
| 863 |
break; |
| 864 |
case KeyboardConfig::KEY_WINDOW_PARTY: |
| 865 |
requestedWindow = partyWindow; |
| 866 |
break; |
| 867 |
case KeyboardConfig::KEY_WINDOW_EMOTE_SHORTCUT: |
| 868 |
requestedWindow = emoteShortcutWindow; |
| 869 |
break; |
| 870 |
case KeyboardConfig::KEY_WINDOW_OUTFIT: |
| 871 |
requestedWindow = outfitWindow; |
| 872 |
break; |
| 873 |
case KeyboardConfig::KEY_SCREENSHOT: |
| 874 |
// Screenshot (picture, hence the p) |
| 875 |
saveScreenshot(); |
| 876 |
used = true; |
| 877 |
break; |
| 878 |
case KeyboardConfig::KEY_PATHFIND: |
| 879 |
// Find path to mouse (debug purpose) |
| 880 |
viewport->toggleDebugPath(); |
| 881 |
used = true; |
| 882 |
break; |
| 883 |
case KeyboardConfig::KEY_TRADE: |
| 884 |
// Toggle accepting of incoming trade requests |
| 885 |
unsigned int deflt = player_relations.getDefault(); |
| 886 |
if (deflt & PlayerRelation::TRADE) |
| 887 |
{ |
| 888 |
localChatTab->chatLog( |
| 889 |
_("Ignoring incoming trade requests"), |
| 890 |
BY_SERVER); |
| 891 |
deflt &= ~PlayerRelation::TRADE; |
| 892 |
} |
| 893 |
else |
| 894 |
{ |
| 895 |
localChatTab->chatLog( |
| 896 |
_("Accepting incoming trade requests"), |
| 897 |
BY_SERVER); |
| 898 |
deflt |= PlayerRelation::TRADE; |
| 899 |
} |
| 900 |
|
| 901 |
player_relations.setDefault(deflt); |
| 902 |
|
| 903 |
used = true; |
| 904 |
break; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
if (requestedWindow) |
| 909 |
{ |
| 910 |
requestedWindow->setVisible(!requestedWindow->isVisible()); |
| 911 |
if (requestedWindow->isVisible()) |
| 912 |
requestedWindow->requestMoveToTop(); |
| 913 |
used = true; |
| 914 |
} |
| 915 |
} |
| 916 |
// Quit event |
| 917 |
else if (event.type == SDL_QUIT) |
| 918 |
{ |
| 919 |
done = true; |
| 920 |
} |
| 921 |
|
| 922 |
// Push input to GUI when not used |
| 923 |
if (!used) |
| 924 |
{ |
| 925 |
try |
| 926 |
{ |
| 927 |
guiInput->pushInput(event); |
| 928 |
} |
| 929 |
catch (gcn::Exception e) |
| 930 |
{ |
| 931 |
const char* err = e.getMessage().c_str(); |
| 932 |
logger->log("Warning: guichan input exception: %s", err); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
} // End while |
| 937 |
|
| 938 |
// If the user is configuring the keys then don't respond. |
| 939 |
if (!keyboard.isEnabled()) |
| 940 |
return; |
| 941 |
|
| 942 |
// Moving player around |
| 943 |
if (player_node->mAction != Being::DEAD && current_npc == 0 && |
| 944 |
!chatWindow->isInputFocused()) |
| 945 |
{ |
| 946 |
// Get the state of the keyboard keys |
| 947 |
keyboard.refreshActiveKeys(); |
| 948 |
|
| 949 |
// Ignore input if either "ignore" key is pressed |
| 950 |
// Stops the character moving about if the user's window manager |
| 951 |
// uses "ignore+arrow key" to switch virtual desktops. |
| 952 |
if (keyboard.isKeyActive(keyboard.KEY_IGNORE_INPUT_1) || |
| 953 |
keyboard.isKeyActive(keyboard.KEY_IGNORE_INPUT_2)) |
| 954 |
{ |
| 955 |
return; |
| 956 |
} |
| 957 |
|
| 958 |
const Vector &pos = player_node->getPosition(); |
| 959 |
const Uint16 x = (int) pos.x / 32; |
| 960 |
const Uint16 y = (int) pos.y / 32; |
| 961 |
unsigned char direction = 0; |
| 962 |
|
| 963 |
// Translate pressed keys to movement and direction |
| 964 |
if (keyboard.isKeyActive(keyboard.KEY_MOVE_UP) || |
| 965 |
(joystick && joystick->isUp())) |
| 966 |
{ |
| 967 |
direction |= Being::UP; |
| 968 |
} |
| 969 |
else if (keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN) || |
| 970 |
(joystick && joystick->isDown())) |
| 971 |
{ |
| 972 |
direction |= Being::DOWN; |
| 973 |
} |
| 974 |
|
| 975 |
if (keyboard.isKeyActive(keyboard.KEY_MOVE_LEFT) || |
| 976 |
(joystick && joystick->isLeft())) |
| 977 |
{ |
| 978 |
direction |= Being::LEFT; |
| 979 |
} |
| 980 |
else if (keyboard.isKeyActive(keyboard.KEY_MOVE_RIGHT) || |
| 981 |
(joystick && joystick->isRight())) |
| 982 |
{ |
| 983 |
direction |= Being::RIGHT; |
| 984 |
} |
| 985 |
|
| 986 |
player_node->setWalkingDir(direction); |
| 987 |
|
| 988 |
// Attacking monsters |
| 989 |
if (keyboard.isKeyActive(keyboard.KEY_ATTACK) || |
| 990 |
(joystick && joystick->buttonPressed(0))) |
| 991 |
{ |
| 992 |
if (player_node->getTarget()) |
| 993 |
player_node->attack(player_node->getTarget(), true); |
| 994 |
} |
| 995 |
|
| 996 |
if (keyboard.isKeyActive(keyboard.KEY_TARGET_ATTACK)) |
| 997 |
{ |
| 998 |
Being *target = NULL; |
| 999 |
|
| 1000 |
bool newTarget = !keyboard.isKeyActive(keyboard.KEY_TARGET); |
| 1001 |
// A set target has highest priority |
| 1002 |
if (!player_node->getTarget()) |
| 1003 |
{ |
| 1004 |
#ifdef TMWSERV_SUPPORT |
| 1005 |
Uint16 targetX = x / 32, targetY = y / 32; |
| 1006 |
#else |
| 1007 |
Uint16 targetX = x, targetY = y; |
| 1008 |
#endif |
| 1009 |
// Only auto target Monsters |
| 1010 |
target = beingManager->findNearestLivingBeing(targetX, targetY, |
| 1011 |
20, Being::MONSTER); |
| 1012 |
} |
| 1013 |
player_node->attack(target, newTarget); |
| 1014 |
} |
| 1015 |
|
| 1016 |
// Target the nearest player/monster/npc |
| 1017 |
if ((keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) || |
| 1018 |
keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) || |
| 1019 |
keyboard.isKeyActive(keyboard.KEY_TARGET_NPC) || |
| 1020 |
(joystick && joystick->buttonPressed(3))) && |
| 1021 |
!keyboard.isKeyActive(keyboard.KEY_TARGET)) |
| 1022 |
{ |
| 1023 |
Being::Type currentTarget = Being::UNKNOWN; |
| 1024 |
if (keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) || |
| 1025 |
(joystick && joystick->buttonPressed(3))) |
| 1026 |
currentTarget = Being::MONSTER; |
| 1027 |
else if (keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER)) |
| 1028 |
currentTarget = Being::PLAYER; |
| 1029 |
else if (keyboard.isKeyActive(keyboard.KEY_TARGET_NPC)) |
| 1030 |
currentTarget = Being::NPC; |
| 1031 |
|
| 1032 |
Being *target = beingManager->findNearestLivingBeing(player_node, |
| 1033 |
20, currentTarget); |
| 1034 |
|
| 1035 |
if (target && (target != player_node->getTarget() || |
| 1036 |
currentTarget != mLastTarget)) |
| 1037 |
{ |
| 1038 |
player_node->setTarget(target); |
| 1039 |
mLastTarget = currentTarget; |
| 1040 |
} |
| 1041 |
} else mLastTarget = Being::UNKNOWN; // Reset last target |
| 1042 |
|
| 1043 |
// Talk to the nearest NPC if 't' pressed |
| 1044 |
if ( keyboard.isKeyActive(keyboard.KEY_TALK) ) |
| 1045 |
{ |
| 1046 |
if (!npcDialog->isVisible()) |
| 1047 |
{ |
| 1048 |
Being *target = player_node->getTarget(); |
| 1049 |
|
| 1050 |
if (target) |
| 1051 |
{ |
| 1052 |
if (target->getType() == Being::NPC) |
| 1053 |
dynamic_cast<NPC*>(target)->talk(); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
// Stop attacking if the right key is pressed |
| 1059 |
if (!keyboard.isKeyActive(keyboard.KEY_ATTACK) |
| 1060 |
&& keyboard.isKeyActive(keyboard.KEY_TARGET)) |
| 1061 |
{ |
| 1062 |
player_node->stopAttack(); |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (joystick) |
| 1066 |
{ |
| 1067 |
if (joystick->buttonPressed(1)) |
| 1068 |
{ |
| 1069 |
FloorItem *item = floorItemManager->findByCoordinates(x, y); |
| 1070 |
|
| 1071 |
if (item) |
| 1072 |
player_node->pickUp(item); |
| 1073 |
} |
| 1074 |
else if (joystick->buttonPressed(2)) |
| 1075 |
{ |
| 1076 |
player_node->toggleSit(); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |