diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 678 |
1 files changed, 335 insertions, 343 deletions
diff --git a/src/game.cpp b/src/game.cpp index 0562b75e..5e7ee1e8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -68,6 +68,7 @@ #include "gui/status.h" #include "gui/trade.h" #include "gui/viewport.h" + #include "net/protocol.h" #include "net/beinghandler.h" #include "net/buysellhandler.h" @@ -132,19 +133,19 @@ Particle* particleEngine = NULL; const int MAX_TIME = 10000; /** - * Listener used for exitting handling. + * Listener used for exiting handling. */ namespace { struct ExitListener : public gcn::ActionListener { - void action(const gcn::ActionEvent &event) - { - if (event.getId() == "yes" || event.getId() == "ok") { - done = true; + void action(const gcn::ActionEvent &event) + { + if (event.getId() == "yes" || event.getId() == "ok") { + done = true; + } + exitConfirm = NULL; + disconnectedDialog = NULL; } - exitConfirm = NULL; - disconnectedDialog = NULL; - } } exitListener; } @@ -171,11 +172,13 @@ Uint32 nextSecond(Uint32 interval, void *param) int get_elapsed_time(int start_time) { - if (start_time <= tick_time) { - return (tick_time - start_time) * 10; + if (start_time <= tick_time) + { + return (tick_time - start_time) * 10; } - else { - return (tick_time + (MAX_TIME - start_time)) * 10; + else + { + return (tick_time + (MAX_TIME - start_time)) * 10; } } @@ -218,8 +221,9 @@ void createGuiWindows(Network *network) chatWindow->setVisible((bool) config.getValue( chatWindow->getWindowName() + "Visible", true)); miniStatusWindow->setVisible((bool) config.getValue( - miniStatusWindow->getWindowName() + "Visible", - true)); + miniStatusWindow->getWindowName() + "Visible", true)); + buyDialog->setVisible(false); + sellDialog->setVisible(false); menuWindow->setVisible((bool) config.getValue( menuWindow->getWindowName() + "Visible", true)); itemShortcutWindow->setVisible((bool) config.getValue( @@ -227,7 +231,7 @@ void createGuiWindows(Network *network) if (config.getValue("logToChat", 0)) { - logger->setChatWindow(chatWindow); + logger->setChatWindow(chatWindow); } } @@ -369,15 +373,15 @@ bool saveScreenshot(SDL_Surface *screenshot) if (ImageWriter::writePNG(screenshot, filename.str())) { - std::stringstream chatlogentry; - chatlogentry << "Screenshot saved to " << filename.str().c_str(); - chatWindow->chatLog(chatlogentry.str(), BY_SERVER); - return true; + std::stringstream chatlogentry; + chatlogentry << "Screenshot saved to " << filename.str().c_str(); + chatWindow->chatLog(chatlogentry.str(), BY_SERVER); + return true; } else { - chatWindow->chatLog("Saving screenshot failed!", BY_SERVER); - return false; + chatWindow->chatLog("Saving screenshot failed!", BY_SERVER); + return false; } } @@ -420,18 +424,18 @@ void Game::logic() if (!mMinFrameTime || get_elapsed_time(mDrawTime / 10) > mMinFrameTime) { - frame++; - gui->draw(); - graphics->updateScreen(); - mDrawTime += mMinFrameTime; - - // Make sure to wrap mDrawTime, since tick_time will wrap. - if (mDrawTime > MAX_TIME * 10) - mDrawTime -= MAX_TIME * 10; + frame++; + gui->draw(); + graphics->updateScreen(); + mDrawTime += mMinFrameTime; + + // Make sure to wrap mDrawTime, since tick_time will wrap. + if (mDrawTime > MAX_TIME * 10) + mDrawTime -= MAX_TIME * 10; } else { - SDL_Delay(10); + SDL_Delay(10); } } else @@ -448,12 +452,11 @@ void Game::logic() { if (!disconnectedDialog) { - disconnectedDialog = new - OkDialog("Network Error", - "The connection to the server was lost, the " - "program will now quit"); - disconnectedDialog->addActionListener(&exitListener); - disconnectedDialog->requestMoveToTop(); + disconnectedDialog = new + OkDialog("Network Error", + "The connection to the server was lost, the program will now quit"); + disconnectedDialog->addActionListener(&exitListener); + disconnectedDialog->requestMoveToTop(); } } } @@ -463,273 +466,268 @@ void Game::handleInput() { if (joystick != NULL) { - joystick->update(); + joystick->update(); } // Events SDL_Event event; while (SDL_PollEvent(&event)) { - bool used = false; - - // Keyboard events (for discontinuous keys) - if (event.type == SDL_KEYDOWN) - { - gcn::Window *requestedWindow = NULL; + bool used = false; - if (setupWindow->isVisible() && - keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE) + // Keyboard events (for discontinuous keys) + if (event.type == SDL_KEYDOWN) { - keyboard.setNewKey((int) event.key.keysym.sym); - keyboard.callbackNewKey(); - keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE); - return; - } - // Keys pressed together with Alt/Meta - // Emotions and some internal gui windows - #ifndef __APPLE__ - if (event.key.keysym.mod & KMOD_ALT) - #else - if (event.key.keysym.mod & KMOD_LMETA) - #endif - { - switch (event.key.keysym.sym) - { - case SDLK_p: - // Screenshot (picture, hence the p) + gcn::Window *requestedWindow = NULL; + + if (setupWindow->isVisible() && + keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE) { - SDL_Surface *screenshot = - graphics->getScreenshot(); - if (!saveScreenshot(screenshot)) - { - logger-> - log("Error: could not save Screenshot."); - } - SDL_FreeSurface(screenshot); + keyboard.setNewKey((int) event.key.keysym.sym); + keyboard.callbackNewKey(); + keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE); + return; } - used = true; - break; - - default: - break; - - case SDLK_f: - // Find path to mouse (debug purpose) - viewport->toggleDebugPath(); - used = true; - break; - - case SDLK_t: - // Toggle accepting of incoming trade requests + // Keys pressed together with Alt/Meta + // Emotions and some internal gui windows + #ifndef __APPLE__ + if (event.key.keysym.mod & KMOD_ALT) + #else + if (event.key.keysym.mod & KMOD_LMETA) + #endif { - unsigned int deflt = player_relations.getDefault(); - if (deflt & PlayerRelation::TRADE) { - chatWindow-> - chatLog("Ignoring incoming trade requests", - BY_SERVER); - deflt &= ~PlayerRelation::TRADE; - } else { - chatWindow->chatLog("Accepting incoming trade " - "requests", BY_SERVER); - deflt |= PlayerRelation::TRADE; - } - - player_relations.setDefault(deflt); + switch (event.key.keysym.sym) + { + case SDLK_p: + // Screenshot (picture, hence the p) + { + SDL_Surface *screenshot = graphics->getScreenshot(); + if (!saveScreenshot(screenshot)) + { + logger->log("Error: could not save Screenshot."); + } + SDL_FreeSurface(screenshot); + } + used = true; + break; + + default: + break; + + case SDLK_f: + // Find path to mouse (debug purpose) + viewport->toggleDebugPath(); + used = true; + break; + + case SDLK_t: + // Toggle accepting of incoming trade requests + { + unsigned int deflt = player_relations.getDefault(); + if (deflt & PlayerRelation::TRADE) { + chatWindow->chatLog("Ignoring incoming trade requests", BY_SERVER); + deflt &= ~PlayerRelation::TRADE; + } else { + chatWindow->chatLog("Accepting incoming trade requests", BY_SERVER); + deflt |= PlayerRelation::TRADE; + } + + player_relations.setDefault(deflt); + } + used = true; + break; } - used = true; - break; - } - - // Emotions - Uint8 emotion; - switch (event.key.keysym.sym) - { - case SDLK_1: emotion = 1; break; - case SDLK_2: emotion = 2; break; - case SDLK_3: emotion = 3; break; - case SDLK_4: emotion = 4; break; - case SDLK_5: emotion = 5; break; - case SDLK_6: emotion = 6; break; - case SDLK_7: emotion = 7; break; - case SDLK_8: emotion = 8; break; - case SDLK_9: emotion = 9; break; - case SDLK_0: emotion = 10; break; - case SDLK_MINUS: emotion = 11; break; - case SDLK_EQUALS: emotion = 12; break; - default: emotion = 0; break; } - if (emotion) - { - player_node->emote(emotion); - used = true; - } - } - switch (event.key.keysym.sym) + // Smilie + if (keyboard.isKeyActive(keyboard.KEY_SMILIE)) { - case SDLK_PAGEUP: - if (chatWindow->isVisible()) + // Emotions + Uint8 emotion; + switch (event.key.keysym.sym) { - chatWindow->scroll(-DEFAULT_CHAT_WINDOW_SCROLL); - used = true; + case SDLK_1: emotion = 1; break; + case SDLK_2: emotion = 2; break; + case SDLK_3: emotion = 3; break; + case SDLK_4: emotion = 4; break; + case SDLK_5: emotion = 5; break; + case SDLK_6: emotion = 6; break; + case SDLK_7: emotion = 7; break; + case SDLK_8: emotion = 8; break; + case SDLK_9: emotion = 9; break; + case SDLK_0: emotion = 10; break; + case SDLK_MINUS: emotion = 11; break; + case SDLK_EQUALS: emotion = 12; break; + default: emotion = 0; break; } - break; - case SDLK_PAGEDOWN: - if (chatWindow->isVisible()) + if (emotion) { - chatWindow->scroll(DEFAULT_CHAT_WINDOW_SCROLL); - used = true; + player_node->emote(emotion); + used = true; } - break; + } + switch (event.key.keysym.sym) + { + case SDLK_PAGEUP: + if (chatWindow->isVisible()) + { + chatWindow->scroll(-DEFAULT_CHAT_WINDOW_SCROLL); + used = true; + } + break; - case SDLK_F1: - // In-game Help - if (helpWindow->isVisible()) - { - helpWindow->setVisible(false); - } - else - { - helpWindow->loadHelp("index"); - helpWindow->requestMoveToTop(); - } - used = true; - break; + case SDLK_PAGEDOWN: + if (chatWindow->isVisible()) + { + chatWindow->scroll(DEFAULT_CHAT_WINDOW_SCROLL); + used = true; + } + break; - case SDLK_F2: requestedWindow = statusWindow; break; - case SDLK_F3: requestedWindow = inventoryWindow; break; - case SDLK_F4: requestedWindow = equipmentWindow; break; - case SDLK_F5: requestedWindow = skillDialog; break; - case SDLK_F6: requestedWindow = minimap; break; - case SDLK_F7: requestedWindow = chatWindow; break; - case SDLK_F8: requestedWindow = itemShortcutWindow; break; - case SDLK_F9: requestedWindow = setupWindow; break; - case SDLK_F10: requestedWindow = debugWindow; break; - //case SDLK_F11: requestedWindow = newSkillWindow; break; - - case SDLK_RETURN: - // Input chat window - if (chatWindow->isInputFocused() || - deathNotice != NULL || - weightNotice != NULL) - { - break; - } + case SDLK_F1: + // In-game Help + if (helpWindow->isVisible()) + { + helpWindow->setVisible(false); + } + else + { + helpWindow->loadHelp("index"); + helpWindow->requestMoveToTop(); + } + used = true; + break; + + case SDLK_F2: requestedWindow = statusWindow; break; + case SDLK_F3: requestedWindow = inventoryWindow; break; + case SDLK_F4: requestedWindow = equipmentWindow; break; + case SDLK_F5: requestedWindow = skillDialog; break; + case SDLK_F6: requestedWindow = minimap; break; + case SDLK_F7: requestedWindow = chatWindow; break; + case SDLK_F8: requestedWindow = itemShortcutWindow; break; + case SDLK_F9: requestedWindow = setupWindow; break; + case SDLK_F10: requestedWindow = debugWindow; break; + //case SDLK_F11: requestedWindow = newSkillWindow; break; + + case SDLK_RETURN: + // Input chat window + if (chatWindow->isInputFocused() || + deathNotice != NULL || + weightNotice != NULL) + { + break; + } - // Quit by pressing Enter if the exit confirm is there - if (exitConfirm) - { - done = true; - } - // Close the Browser if opened - else if (helpWindow->isVisible()) - { - helpWindow->setVisible(false); - } - // Close the config window, cancelling changes if opened - else if (setupWindow->isVisible()) - { - setupWindow->action(gcn::ActionEvent(NULL, "cancel")); - } - // Else, open the chat edit box - else - { - chatWindow->requestChatFocus(); - used = true; - } - break; - // Quitting confirmation dialog - case SDLK_ESCAPE: - if (!exitConfirm) { - exitConfirm = new ConfirmDialog( - "Quit", "Are you sure you want to quit?"); - exitConfirm->addActionListener(&exitListener); - exitConfirm->requestMoveToTop(); - } - else - { - exitConfirm->action(gcn::ActionEvent(NULL, "no")); - } - break; + // Quit by pressing Enter if the exit confirm is there + if (exitConfirm) + { + done = true; + } + // Close the Browser if opened + else if (helpWindow->isVisible()) + { + helpWindow->setVisible(false); + } + // Close the config window, cancelling changes if opened + else if (setupWindow->isVisible()) + { + setupWindow->action(gcn::ActionEvent(NULL, "cancel")); + } + // Else, open the chat edit box + else + { + chatWindow->requestChatFocus(); + used = true; + } + break; + // Quitting confirmation dialog + case SDLK_ESCAPE: + if (!exitConfirm) { + exitConfirm = new ConfirmDialog( + "Quit", "Are you sure you want to quit?"); + exitConfirm->addActionListener(&exitListener); + exitConfirm->requestMoveToTop(); + } + else + { + exitConfirm->action(gcn::ActionEvent(NULL, "no")); + } + break; - default: - break; + default: + break; } if (keyboard.isEnabled() && !chatWindow->isInputFocused()) { - const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); - // Checks if any item shortcut is pressed. - for (int i = KeyboardConfig::KEY_SHORTCUT_0; - i <= KeyboardConfig::KEY_SHORTCUT_9; - i++) - { - if (tKey == i && !used) { - itemShortcut->useItem( - i - KeyboardConfig::KEY_SHORTCUT_0); - break; + const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); + // Checks if any item shortcut is pressed. + for (int i = KeyboardConfig::KEY_SHORTCUT_0; + i <= KeyboardConfig::KEY_SHORTCUT_9; + i++) + { + if (tKey == i && !used) { + itemShortcut->useItem(i - KeyboardConfig::KEY_SHORTCUT_0); + break; } } switch (tKey) { case KeyboardConfig::KEY_PICKUP: { - FloorItem *item = - floorItemManager->findByCoordinates( - player_node->mX, player_node->mY); + FloorItem *item = floorItemManager->findByCoordinates( + player_node->mX, player_node->mY); // If none below the player, try the tile in front // of the player if (!item) { - Uint16 x = player_node->mX; - Uint16 y = player_node->mY; - if (player_node->getDirection() & Being::UP) - y--; - if (player_node->getDirection() & Being::DOWN) - y++; - if (player_node->getDirection() & Being::LEFT) - x--; - if (player_node->getDirection() & Being::RIGHT) - x++; - - item = floorItemManager-> - findByCoordinates(x, y); + Uint16 x = player_node->mX; + Uint16 y = player_node->mY; + switch (player_node->getSpriteDirection()) + { + case DIRECTION_UP : --y; break; + case DIRECTION_DOWN : ++y; break; + case DIRECTION_LEFT : --x; break; + case DIRECTION_RIGHT: ++x; break; + default: break; + } + item = floorItemManager->findByCoordinates(x, y); } if (item) - player_node->pickUp(item); + player_node->pickUp(item); used = true; } break; case KeyboardConfig::KEY_SIT: - // Player sit action - player_node->toggleSit(); - used = true; - break; + // Player sit action + player_node->toggleSit(); + used = true; + break; case KeyboardConfig::KEY_HIDE_WINDOWS: - // Hide certain windows - if (!chatWindow->isInputFocused()) - { - statusWindow->setVisible(false); - inventoryWindow->setVisible(false); - skillDialog->setVisible(false); - setupWindow->setVisible(false); - equipmentWindow->setVisible(false); - helpWindow->setVisible(false); - debugWindow->setVisible(false); + // Hide certain windows + if (!chatWindow->isInputFocused()) + { + statusWindow->setVisible(false); + inventoryWindow->setVisible(false); + skillDialog->setVisible(false); + setupWindow->setVisible(false); + equipmentWindow->setVisible(false); + helpWindow->setVisible(false); + debugWindow->setVisible(false); + } + break; } - break; - } } if (requestedWindow) { - requestedWindow->setVisible(!requestedWindow->isVisible()); - if (requestedWindow->isVisible()) - { - requestedWindow->requestMoveToTop(); - } - used = true; + requestedWindow->setVisible(!requestedWindow->isVisible()); + if (requestedWindow->isVisible()) + { + requestedWindow->requestMoveToTop(); + } + used = true; } } @@ -745,12 +743,12 @@ void Game::handleInput() { try { - guiInput->pushInput(event); + guiInput->pushInput(event); } catch (gcn::Exception e) { - const char* err = e.getMessage().c_str(); - logger->log("Warning: guichan input exception: %s", err); + const char* err = e.getMessage().c_str(); + logger->log("Warning: guichan input exception: %s", err); } } @@ -762,112 +760,106 @@ void Game::handleInput() } // Moving player around if (player_node->mAction != Being::DEAD && - current_npc == 0 && - !chatWindow->isInputFocused()) + current_npc == 0 && + !chatWindow->isInputFocused()) { - // Get the state of the keyboard keys - keyboard.refreshActiveKeys(); + // Get the state of the keyboard keys + keyboard.refreshActiveKeys(); - const Uint16 x = player_node->mX; - const Uint16 y = player_node->mY; - unsigned char direction = 0; + const Uint16 x = player_node->mX; + const Uint16 y = player_node->mY; + unsigned char direction = 0; - // Translate pressed keys to movement and direction - if (keyboard.isKeyActive(keyboard.KEY_MOVE_UP) || - (joystick && joystick->isUp())) - { - direction |= Being::UP; - } - else if (keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN) || - (joystick && joystick->isDown())) - { - direction |= Being::DOWN; - } + // Translate pressed keys to movement and direction + if (keyboard.isKeyActive(keyboard.KEY_MOVE_UP) || + (joystick && joystick->isUp())) + { + direction |= Being::UP; + } + else if (keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN) || + (joystick && joystick->isDown())) + { + direction |= Being::DOWN; + } - if (keyboard.isKeyActive(keyboard.KEY_MOVE_LEFT) || - (joystick && joystick->isLeft())) - { - direction |= Being::LEFT; - } - else if (keyboard.isKeyActive(keyboard.KEY_MOVE_RIGHT) || - (joystick && joystick->isRight())) - { - direction |= Being::RIGHT; - } + if (keyboard.isKeyActive(keyboard.KEY_MOVE_LEFT) || + (joystick && joystick->isLeft())) + { + direction |= Being::LEFT; + } + else if (keyboard.isKeyActive(keyboard.KEY_MOVE_RIGHT) || + (joystick && joystick->isRight())) + { + direction |= Being::RIGHT; + } - player_node->setWalkingDir(direction); + player_node->setWalkingDir(direction); - // Attacking monsters - if (keyboard.isKeyActive(keyboard.KEY_ATTACK) || - (joystick && joystick->buttonPressed(0))) - { - Being *target = NULL; - bool newTarget = keyboard.isKeyActive(keyboard.KEY_TARGET); - // A set target has highest priority - if (newTarget || !player_node->getTarget()) + // Attacking monsters + if (keyboard.isKeyActive(keyboard.KEY_ATTACK) || + (joystick && joystick->buttonPressed(0))) { - Uint16 targetX = x, targetY = y; - - if (player_node->getDirection() & Being::UP) - targetY--; - if (player_node->getDirection() & Being::DOWN) - targetY++; - if (player_node->getDirection() & Being::LEFT) - targetX--; - if (player_node->getDirection() & Being::RIGHT) - targetX++; - - // Attack priorioty is: Monster, Player, auto target - target = beingManager->findBeing( - targetX, targetY, Being::MONSTER); - if (!target) - target = beingManager->findBeing( - targetX, targetY, Being::PLAYER); - } + Being *target = beingManager->findNearestLivingBeing(x, y, 20, Being::MONSTER); - player_node->attack(target, newTarget); - } + bool newTarget = keyboard.isKeyActive(keyboard.KEY_TARGET); + // A set target has highest priority + if (newTarget || !player_node->getTarget()) + { + Uint16 targetX = x, targetY = y; - // Target the nearest player if 'q' is pressed - if ( keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) ) - //if (keys[SDLK_q]) - { - Being *target = - beingManager-> - findNearestLivingBeing(player_node, 20, Being::PLAYER); + switch (player_node->getSpriteDirection()) + { + case DIRECTION_UP : --targetY; break; + case DIRECTION_DOWN : ++targetY; break; + case DIRECTION_LEFT : --targetX; break; + case DIRECTION_RIGHT: ++targetX; break; + default: break; + } - if (target) - { - player_node->setTarget(target); - } - } + // Attack priorioty is: Monster, Player, auto target + target = beingManager->findBeing(targetX, targetY, Being::MONSTER); + if (!target) + target = beingManager->findBeing(targetX, targetY, Being::PLAYER); + } - // Target the nearest monster if 'a' pressed - if ( keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) ) - //if (keys[SDLK_a]) - { - Being *target = - beingManager->findNearestLivingBeing(x, y, 20, Being::MONSTER); + player_node->attack(target, newTarget); + } - if (target) + // Target the nearest player if 'q' is pressed + if ( keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) ) { - player_node->setTarget(target); + Being *target = beingManager->findNearestLivingBeing(player_node, 20, Being::PLAYER); + + if (target) + { + player_node->setTarget(target); + } } - } - if (joystick) - { - if (joystick->buttonPressed(1)) + // Target the nearest monster if 'a' pressed + if ( keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) ) { - FloorItem *item = floorItemManager->findByCoordinates(x, y); + Being *target = beingManager->findNearestLivingBeing(x, y, 20, Being::MONSTER); - if (item) - player_node->pickUp(item); + if (target) + { + player_node->setTarget(target); + } } - else if (joystick->buttonPressed(2)) + + if (joystick) { - player_node->toggleSit(); + if (joystick->buttonPressed(1)) + { + FloorItem *item = floorItemManager->findByCoordinates(x, y); + + if (item) + player_node->pickUp(item); + } + else if (joystick->buttonPressed(2)) + { + player_node->toggleSit(); + } } } - } } |