diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-10-06 20:20:36 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-10-06 20:20:36 +0000 |
commit | 09db562f2fca5c49fa1a92ba0b6dc60db479ae08 (patch) | |
tree | 6e3b607ceaf02558423b8f7f3beb108b3d6b2d53 | |
parent | 4a43cb1c6e5ba304d70df7066c61c52718a5f249 (diff) | |
download | mana-09db562f2fca5c49fa1a92ba0b6dc60db479ae08.tar.gz mana-09db562f2fca5c49fa1a92ba0b6dc60db479ae08.tar.bz2 mana-09db562f2fca5c49fa1a92ba0b6dc60db479ae08.tar.xz mana-09db562f2fca5c49fa1a92ba0b6dc60db479ae08.zip |
Transition to SDL integer types. Some int vs. long issues fixed. Return NULL when requesting an invalid inventory item.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | src/being.cpp | 145 | ||||
-rw-r--r-- | src/being.h | 76 | ||||
-rw-r--r-- | src/game.cpp | 468 | ||||
-rw-r--r-- | src/inventory.cpp | 5 |
5 files changed, 352 insertions, 349 deletions
@@ -1,3 +1,10 @@ +2005-10-06 Björn Steinbrink <B.Steinbrink@gmx.de> + + * src/being.cpp, src/being.h, src/game.cpp: Use integer types from SDL + and along the way fixed some long vs. int issues. + * src/inventory.cpp: Return NULL when no valid item index is given to + getItem() to bail out early. + 2005-10-04 Yohann Ferreira <bertram@cegetel.net> * src/game.cpp, src/gui/menuwindow.cpp, src/gui/inventorywindow.cpp, diff --git a/src/being.cpp b/src/being.cpp index 59284787..4301b0a9 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -49,12 +49,12 @@ Being *player_node = NULL; std::list<Being*> beings; -PATH_NODE::PATH_NODE(unsigned short x, unsigned short y): +PATH_NODE::PATH_NODE(Uint16 x, Uint16 y): x(x), y(y) { } -Being* createBeing(unsigned int id, unsigned short job, Map *map) +Being* createBeing(Uint32 id, Uint16 job, Map *map) { Being *being = new Being; @@ -100,7 +100,7 @@ void remove_node(Being *being) beings.remove(being); } -Being *findNode(unsigned int id) +Being *findNode(Uint32 id) { std::list<Being*>::iterator i; for (i = beings.begin(); i != beings.end(); i++) { @@ -112,7 +112,7 @@ Being *findNode(unsigned int id) return NULL; } -Being *findNode(unsigned short x, unsigned short y) +Being *findNode(Uint16 x, Uint16 y) { std::list<Being*>::iterator i; for (i = beings.begin(); i != beings.end(); i++) { @@ -125,7 +125,7 @@ Being *findNode(unsigned short x, unsigned short y) return NULL; } -Being* findNode(unsigned short x, unsigned short y, Being::Type type) +Being* findNode(Uint16 x, Uint16 y, Being::Type type) { std::list<Being *>::iterator i; for (i = beings.begin(); i != beings.end(); i++) { @@ -176,7 +176,7 @@ Being::~Being() clearPath(); } -void Being::setDestination(int destX, int destY) +void Being::setDestination(Uint16 destX, Uint16 destY) { if (!map) return; @@ -200,7 +200,7 @@ void Being::setPath(std::list<PATH_NODE> path) } } -void Being::setHairColor(int color) +void Being::setHairColor(Uint16 color) { hairColor = color; if (hairColor < 1 || hairColor > NR_HAIR_COLORS + 1) @@ -209,7 +209,7 @@ void Being::setHairColor(int color) } } -void Being::setHairStyle(int style) +void Being::setHairStyle(Uint16 style) { hairStyle = style; if (hairStyle < 1 || hairStyle > NR_HAIR_STYLES) @@ -218,24 +218,24 @@ void Being::setHairStyle(int style) } } -unsigned short Being::getHairColor() +Uint16 Being::getHairColor() { return hairColor; } -unsigned short Being::getHairStyle() +Uint16 Being::getHairStyle() { return hairStyle; } -void Being::setSpeech(const std::string &text, int time) +void Being::setSpeech(const std::string &text, Uint32 time) { speech = text; speech_time = tick_time; showSpeech = true; } -void Being::setDamage(short amount, int time) +void Being::setDamage(Sint16 amount, Uint32 time) { if (!amount) { damage = "miss"; @@ -255,77 +255,76 @@ void Being::setMap(Map *map) void Being::nextStep() { - if (!path.empty()) - { - PATH_NODE node = path.front(); - path.pop_front(); - - int oldX = x; - int oldY = y; - int newX = node.x; - int newY = node.y; - - if (newX > oldX) { - if (newY > oldY) direction = SE; - else if (newY < oldY) direction = NE; - else direction = EAST; - } - else if (newX < oldX) { - if (newY > oldY) direction = SW; - else if (newY < oldY) direction = NW; - else direction = WEST; - } - else { - if (newY > oldY) direction = SOUTH; - else if (newY < oldY) direction = NORTH; - } + frame = 0; - x = newX; - y = newY; - action = WALK; - walk_time += speed / 10; - } else { + if (path.empty()) + { action = STAND; + return; } - frame = 0; -} -void Being::logic() -{ - if (getType() == PLAYER) - { - switch (action) { - case WALK: - frame = (get_elapsed_time(walk_time) * 4) / speed; - if (frame >= 4) { - nextStep(); - } - break; - case ATTACK: - frame = (get_elapsed_time(walk_time) * 4) / aspd; - if (frame >= 4) { - nextStep(); - if (autoTarget && this == player_node) { - attack(autoTarget); - } - } - break; - } + PATH_NODE node = path.front(); + path.pop_front(); - if (emotion != 0) { - emotion_time--; - if (emotion_time == 0) { - emotion = 0; - } - } + if (node.x > x) { + if (node.y > y) direction = SE; + else if (node.y < y) direction = NE; + else direction = EAST; + } + else if (node.x < x) { + if (node.y > y) direction = SW; + else if (node.y < y) direction = NW; + else direction = WEST; + } + else { + if (node.y > y) direction = SOUTH; + else if (node.y < y) direction = NORTH; } + x = node.x; + y = node.y; + action = WALK; + walk_time += speed / 10; +} + +void Being::logic() +{ if (get_elapsed_time(speech_time) > 5000) { showSpeech = false; } if (get_elapsed_time(damage_time) > 3000) { showDamage = false; } + + if (getType() != PLAYER) + { + return; + } + + switch (action) { + case WALK: + frame = (get_elapsed_time(walk_time) * 4) / speed; + if (frame >= 4) { + nextStep(); + } + break; + case ATTACK: + frame = (get_elapsed_time(walk_time) * 4) / aspd; + if (frame >= 4) { + nextStep(); + if (autoTarget && this == player_node) { + attack(autoTarget); + } + } + break; + } + + if (emotion != 0) { + emotion_time--; + if (emotion_time == 0) { + emotion = 0; + } + } } void Being::drawSpeech(Graphics *graphics) @@ -389,12 +388,12 @@ Being::Type Being::getType() } } -void Being::setWeapon(unsigned short weapon) +void Being::setWeapon(Uint16 weapon) { m_weapon = weapon; } -void Being::setWeaponById(unsigned short weapon) +void Being::setWeaponById(Uint16 weapon) { switch (weapon) { @@ -424,7 +423,7 @@ void Being::setWeaponById(unsigned short weapon) } } -void Being::setId(unsigned int id) +void Being::setId(Uint32 id) { m_id = id; } diff --git a/src/being.h b/src/being.h index 9c853c32..07b12232 100644 --- a/src/being.h +++ b/src/being.h @@ -26,6 +26,7 @@ #include <list> #include <string> +#include <SDL_types.h> #define NR_HAIR_STYLES 5 #define NR_HAIR_COLORS 10 @@ -75,19 +76,19 @@ class Being SE = 7, }; - unsigned short job; /**< Job (player job, npc, monster, ) */ - unsigned short x, y; /**< Tile coordinates */ - unsigned char direction; /**< Facing direction */ - unsigned char action; - unsigned char frame; - int speech_color; - unsigned short walk_time; - unsigned short speed; - unsigned char emotion; /**< Currently showing emotion */ - unsigned char emotion_time; /**< Time until emotion disappears */ - unsigned int text_x, text_y; // temp solution to fix speech position + Uint16 job; /**< Job (player job, npc, monster, ) */ + Uint16 x, y; /**< Tile coordinates */ + Uint8 direction; /**< Facing direction */ + Uint8 action; + Uint8 frame; + Sint32 speech_color; + Uint16 walk_time; + Uint16 speed; + Uint8 emotion; /**< Currently showing emotion */ + Uint8 emotion_time; /**< Time until emotion disappears */ + Uint32 text_x, text_y; // temp solution to fix speech position - unsigned short aspd; /**< Attack speed */ + Uint16 aspd; /**< Attack speed */ /** * Constructor. @@ -107,7 +108,7 @@ class Being /** * Sets a new destination for this being to walk to. */ - void setDestination(int destX, int destY); + void setDestination(Uint16 destX, Uint16 destY); /** * Puts a "speech balloon" above this being for the specified amount @@ -116,7 +117,7 @@ class Being * @param text The text that should appear. * @param time The amount of time the text should stay in milliseconds. */ - void setSpeech(const std::string &text, int time); + void setSpeech(const std::string &text, Uint32 time); /** * Puts a damage bubble above this being for the specified amount @@ -125,7 +126,7 @@ class Being * @param text The text that should appear. * @param time The amount of time the text should stay in milliseconds. */ - void setDamage(short amount, int time); + void setDamage(Sint16 amount, Uint32 time); /** * Returns the name of the being. @@ -144,22 +145,22 @@ class Being /** * Sets the hair color for this being. */ - void setHairColor(int color); + void setHairColor(Uint16 color); /** * Sets the hair style for this being. */ - void setHairStyle(int style); + void setHairStyle(Uint16 style); /** * Gets the hair color for this being. */ - unsigned short getHairColor(); + Uint16 getHairColor(); /** * Gets the hair style for this being. */ - unsigned short getHairStyle(); + Uint16 getHairStyle(); /** * Makes this being take the next step of his path. @@ -186,12 +187,12 @@ class Being /** * get the weapon picture id. */ - unsigned short getWeapon() {return m_weapon;} + Uint16 getWeapon() { return m_weapon; } /** * get the sprite id. */ - unsigned int getId() {return m_id;} + Uint32 getId() { return m_id; } // MODIFICATION METHODS @@ -200,19 +201,19 @@ class Being * * @param weapon : the picture id */ - void setWeapon(unsigned short weapon); + void setWeapon(Uint16 weapon); /** * set the weapon picture id with the weapon id. * * @param weapon : the weapon id */ - void setWeaponById(unsigned short weapon); + void setWeaponById(Uint16 weapon); /** * set the sprite id. */ - void setId(unsigned int id); + void setId(Uint32 id); /** * Set the map the being is on @@ -220,16 +221,16 @@ class Being void setMap(Map *map); private: - unsigned short m_weapon; - unsigned int m_id; /**< Unique id */ + Uint16 m_weapon; + Uint32 m_id; /**< Unique id */ Map *map; std::list<PATH_NODE> path; std::string speech; std::string damage; - unsigned short hairStyle, hairColor; - unsigned int speech_time; - unsigned int damage_time; + Uint16 hairStyle, hairColor; + Uint32 speech_time; + Uint32 damage_time; bool showSpeech, showDamage; std::string mName; /**< Name of character */ @@ -240,29 +241,20 @@ class Being }; /** Return a specific id Being */ -Being *findNode(unsigned int id); +Being *findNode(Uint32 id); /** Return a being at specific coordinates */ -Being *findNode(unsigned short x, unsigned short y); +Being *findNode(Uint16 x, Uint16 y); /** Return a being at specific coordinates with specific type*/ -Being *findNode(unsigned short x, unsigned short y, Being::Type type); +Being *findNode(Uint16 x, Uint16 y, Being::Type type); /** Create a being and add it to the list of beings */ -Being *createBeing(unsigned int id, unsigned short job, Map *map); +Being *createBeing(Uint32 id, Uint16 job, Map *map); /** Remove a Being */ void remove_node(Being *being); -/** Find a NPC id based on its coordinates */ -unsigned int findNpc(unsigned short x, unsigned short y); - -/** Find a PLAYER id based on its coordinates */ -unsigned int findPlayer(unsigned short x, unsigned short y); - -/** Find a MONSTER id based on its coordinates */ -unsigned int findMonster(unsigned short x, unsigned short y); - /** Sort beings in vertical order */ void sort(); diff --git a/src/game.cpp b/src/game.cpp index c1b9ceef..5c220155 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -27,6 +27,7 @@ #include <physfs.h> #include <sstream> #include <string> +#include <SDL_types.h> #include <guichan/sdl/sdlinput.hpp> @@ -86,7 +87,7 @@ volatile bool action_time = false; int server_tick; int fps = 0, frame = 0, current_npc = 0; bool displayPathToMouse = false; -unsigned short startX = 0, startY = 0; +Uint16 startX = 0, startY = 0; Being *autoTarget = NULL; Map *tiledMap = NULL; Engine *engine = NULL; @@ -406,7 +407,7 @@ void do_exit() delete tiledMap; destroyGuiWindows(); close_session(); - + delete inventory; delete player_node; @@ -537,39 +538,43 @@ void do_input() // Picking up items on the floor case SDLK_g: case SDLK_z: - if (!chatWindow->isFocused()) + if (chatWindow->isFocused()) { - unsigned short x = player_node->x; - unsigned short y = player_node->y; - int id = find_floor_item_by_cor(x, y); - - // If none below the player, try the tile in front of - // the player - if (!id) { - switch (player_node->direction) - { - case Being::NORTH: y--; break; - case Being::SOUTH: y++; break; - case Being::WEST: x--; break; - case Being::EAST: x++; break; - case Being::NW: x--; y--; break; - case Being::NE: x++; y--; break; - case Being::SW: x--; y++; break; - case Being::SE: x++; y++; break; - default: break; - } - id = find_floor_item_by_cor(x, y); - } + break; + } + + Uint32 id = find_floor_item_by_cor( + player_node->x, player_node->y); - if (id) + // If none below the player, try the tile in front of + // the player + if (!id) { + Uint16 x = player_node->x; + Uint16 y = player_node->y; + + switch (player_node->direction) { - // TODO: remove duplicated code, probably add a pick up command - MessageOut outMsg; - outMsg.writeShort(0x009f); - outMsg.writeLong(id); + case Being::NORTH: y--; break; + case Being::SOUTH: y++; break; + case Being::WEST: x--; break; + case Being::EAST: x++; break; + case Being::NW: x--; y--; break; + case Being::NE: x++; y--; break; + case Being::SW: x--; y++; break; + case Being::SE: x++; y++; break; + default: break; } - used = true; + id = find_floor_item_by_cor(x, y); } + + if (id) + { + // TODO: remove duplicated code, probably add a pick up command + MessageOut outMsg; + outMsg.writeShort(0x009f); + outMsg.writeLong(id); + } + used = true; break; // Quitting confirmation dialog @@ -590,32 +595,52 @@ void do_input() // Emotions and some internal gui windows if (event.key.keysym.mod & KMOD_ALT) { + gcn::Window *requestedWindow = 0; + switch (event.key.keysym.sym) { // Inventory window case SDLK_i: - inventoryWindow->setVisible( - !inventoryWindow->isVisible()); - if (inventoryWindow->isVisible()) inventoryWindow->requestMoveToTop(); - used = true; + requestedWindow = inventoryWindow; break; // Statistics window case SDLK_s: - statusWindow->setVisible(!statusWindow->isVisible()); - if (statusWindow->isVisible()) statusWindow->requestMoveToTop(); - used = true; + requestedWindow = statusWindow; + break; + + // Skill window + case SDLK_k: + requestedWindow = skillDialog; + break; + + // Equipment window + case SDLK_e: + requestedWindow = equipmentWindow; + break; + + /* + // Buddy window + case SDLK_b: + requestedWindow = buddyWindow; break; + */ /* // New skills window case SDLK_n: - newSkillWindow->setVisible(!newSkillWindow->isVisible()); - if (newSkillWindow->isVisible()) newSkillWindow->requestMoveToTop(); - used = true; + requestedWindow = newSkillWindow; break; */ - // screenshot (picture, hence the p) + + // Setup window + case SDLK_c: + setupWindow->setVisible(true); + setupWindow->requestMoveToTop(); + used = true; + break; + + // screenshot (picture, hence the p) case SDLK_p: { SDL_Surface *screenshot = graphics->getScreenshot(); @@ -627,45 +652,24 @@ void do_input() } break; - // Skill window - case SDLK_k: - skillDialog->setVisible(!skillDialog->isVisible()); - if (skillDialog->isVisible()) skillDialog->requestMoveToTop(); - used = true; - break; - - // Setup window - case SDLK_c: - setupWindow->setVisible(true); - setupWindow->requestMoveToTop(); - used = true; - break; - - // Equipment window - case SDLK_e: - equipmentWindow->setVisible( - !equipmentWindow->isVisible()); - if (equipmentWindow->isVisible()) equipmentWindow->requestMoveToTop(); - used = true; - break; - - /* - // Buddy window - case SDLK_b: - buddyWindow->setVisible(!buddyWindow->isVisible()); - if (buddyWindow->isVisible()) buddyWindow->requestMoveToTop(); - used = true; - break; - */ - default: break; } + if (requestedWindow) + { + requestedWindow->setVisible(requestedWindow->isVisible()); + if (requestedWindow->isVisible()) + { + requestedWindow->requestMoveToTop(); + } + used = true; + } + // Emotions if (action_time && !player_node->emotion) { - unsigned char emotion = 0; + Uint8 emotion = 0; switch (event.key.keysym.sym) { case SDLK_1: emotion = 1; break; @@ -712,7 +716,7 @@ void do_input() // Check for default actions for NPC/Monster/Players Being *target = findNode(mx, my); - unsigned int floorItemId = find_floor_item_by_cor(mx, my); + Uint32 floorItemId = find_floor_item_by_cor(mx, my); if (target) { @@ -840,10 +844,10 @@ void do_input() if ((player_node->action != Being::DEAD) && (current_npc == 0) && !chatWindow->isFocused()) { - int x = player_node->x; - int y = player_node->y; - int xDirection = 0; - int yDirection = 0; + Uint16 x = player_node->x; + Uint16 y = player_node->y; + Sint16 xDirection = 0; + Sint16 yDirection = 0; Being::Direction Direction = Being::DIR_NONE; // Translate pressed keys to movement and direction @@ -957,9 +961,9 @@ void do_input() if (joy[JOY_BTN1]) { - unsigned short x = player_node->x; - unsigned short y = player_node->y; - int id = find_floor_item_by_cor(x, y); + Uint16 x = player_node->x; + Uint16 y = player_node->y; + Uint32 id = find_floor_item_by_cor(x, y); if (id != 0) { @@ -981,10 +985,12 @@ void do_input() void do_parse() { - int n_items; Map *tiledMap = engine->getCurrentMap(); Equipment *equipment = Equipment::getInstance(); + int n_items; + Being *being; + // We need at least 2 bytes to identify a packet while (in_size >= 2) { @@ -995,57 +1001,58 @@ void do_parse() { case SMSG_LOGIN_SUCCESS: // Connected to game server succesfully, set spawn point - { - msg.readLong(); // server tick - msg.readCoordinates(player_node->x, - player_node->y, - player_node->direction); - msg.skip(2); // unknown - } + msg.readLong(); // server tick + msg.readCoordinates(player_node->x, player_node->y, + player_node->direction); + msg.skip(2); // unknown break; // Received speech from being case SMSG_BEING_CHAT: { - int chatMsgLength = msg.readShort() - 8; - Being *being = findNode(msg.readLong()); + Sint16 chatMsgLength = msg.readShort() - 8; + being = findNode(msg.readLong()); - if (being != NULL && chatMsgLength > 0) + if (!being || chatMsgLength <= 0) { - std::string chatMsg = msg.readString(chatMsgLength); + break; + } - chatWindow->chat_log(chatMsg, BY_OTHER); + std::string chatMsg = msg.readString(chatMsgLength); - chatMsg.erase(0, chatMsg.find(" : ", 0) + 3); - being->setSpeech(chatMsg, SPEECH_TIME); - } + chatWindow->chat_log(chatMsg, BY_OTHER); + + chatMsg.erase(0, chatMsg.find(" : ", 0) + 3); + being->setSpeech(chatMsg, SPEECH_TIME); } break; case SMSG_PLAYER_CHAT: case SMSG_GM_CHAT: { - int chatMsgLength = msg.readShort() - 4; + Sint16 chatMsgLength = msg.readShort() - 4; - if (chatMsgLength > 0) + if (chatMsgLength <= 0) { - std::string chatMsg = msg.readString(chatMsgLength); + break; + } - if (msg.getId() == SMSG_PLAYER_CHAT) - { - chatWindow->chat_log(chatMsg, BY_PLAYER); + std::string chatMsg = msg.readString(chatMsgLength); - std::string::size_type pos = chatMsg.find(" : ", 0); - if (pos != std::string::npos) - { - chatMsg.erase(0, pos + 3); - } - player_node->setSpeech(chatMsg, SPEECH_TIME); - } - else + if (msg.getId() == SMSG_PLAYER_CHAT) + { + chatWindow->chat_log(chatMsg, BY_PLAYER); + + std::string::size_type pos = chatMsg.find(" : ", 0); + if (pos != std::string::npos) { - chatWindow->chat_log(chatMsg, BY_GM); + chatMsg.erase(0, pos + 3); } + player_node->setSpeech(chatMsg, SPEECH_TIME); + } + else + { + chatWindow->chat_log(chatMsg, BY_GM); } } break; @@ -1061,14 +1068,14 @@ void do_parse() case SMSG_BEING_MOVE: // Information about a being in range { - int id = msg.readLong(); - unsigned short speed = msg.readShort(); + Uint32 id = msg.readLong(); + Uint16 speed = msg.readShort(); msg.readShort(); // unknown msg.readShort(); // unknown msg.readShort(); // option - unsigned short job = msg.readShort(); // class + Uint16 job = msg.readShort(); // class - Being *being = findNode(id); + being = findNode(id); if (being == NULL) { @@ -1119,7 +1126,7 @@ void do_parse() if (msg.getId() == SMSG_BEING_MOVE) { - unsigned short srcX, srcY, dstX, dstY; + Uint16 srcX, srcY, dstX, dstY; msg.readCoordinatePair(srcX, srcY, dstX, dstY); being->action = Being::STAND; being->x = srcX; @@ -1140,37 +1147,35 @@ void do_parse() case SMSG_BEING_REMOVE: // A being should be removed or has died - { - Being *being = findNode(msg.readLong()); + being = findNode(msg.readLong()); - if (being != NULL) + if (being != NULL) + { + if (msg.readByte() == 1) { - if (msg.readByte() == 1) - { - // Death - switch (being->getType()) - { - case Being::MONSTER: - being->action = Being::MONSTER_DEAD; - being->frame = 0; - being->walk_time = tick_time; - break; - - default: - being->action = Being::DEAD; - break; - } - } - else + // Death + switch (being->getType()) { - remove_node(being); - } + case Being::MONSTER: + being->action = Being::MONSTER_DEAD; + being->frame = 0; + being->walk_time = tick_time; + break; - if (being == autoTarget) - { - autoTarget = NULL; + default: + being->action = Being::DEAD; + break; } } + else + { + remove_node(being); + } + + if (being == autoTarget) + { + autoTarget = NULL; + } } break; @@ -1179,14 +1184,14 @@ void do_parse() case SMSG_PLAYER_MOVE: // An update about a player, potentially including movement. { - int id = msg.readLong(); - unsigned short speed = msg.readShort(); + Uint32 id = msg.readLong(); + Uint16 speed = msg.readShort(); msg.readShort(); // option 1 msg.readShort(); // option 2 msg.readShort(); // option - unsigned short job = msg.readShort(); + Uint16 job = msg.readShort(); - Being *being = findNode(id); + being = findNode(id); if (being == NULL) { @@ -1218,7 +1223,7 @@ void do_parse() if (msg.getId() == SMSG_PLAYER_MOVE) { - unsigned short srcX, srcY, dstX, dstY; + Uint16 srcX, srcY, dstX, dstY; msg.readCoordinatePair(srcX, srcY, dstX, dstY); being->x = srcX; being->y = srcY; @@ -1274,7 +1279,7 @@ void do_parse() // situation, and that the requesting player would get a // special message about the player being occupied. - if (tradeWindow->isVisible() == true || requestTradeDialogOpen) + if (tradeWindow->isVisible() || requestTradeDialogOpen) { MessageOut outMsg; outMsg.writeShort(CMSG_TRADE_RESPONSE); @@ -1324,8 +1329,8 @@ void do_parse() case SMSG_TRADE_ITEM_ADD: { - long amount = msg.readLong(); - short type = msg.readShort(); + Sint32 amount = msg.readLong(); + Sint16 type = msg.readShort(); msg.readByte(); // identified flag msg.readByte(); // attribute msg.readByte(); // refine @@ -1344,7 +1349,7 @@ void do_parse() // Trade: New Item add response (was 0x00ea, now 01b1) { Item *item = inventory->getItem(msg.readShort()); - short quantity = msg.readShort(); + Sint16 quantity = msg.readShort(); switch (msg.readByte()) { @@ -1395,15 +1400,15 @@ void do_parse() // to not load them twice on map change. inventory->resetItems(); msg.readShort(); // length - int number = (msg.getLength() - 4) / 18; + Sint32 number = (msg.getLength() - 4) / 18; for (int loop = 0; loop < number; loop++) { - short index = msg.readShort(); - short itemId = msg.readShort(); + Sint16 index = msg.readShort(); + Sint16 itemId = msg.readShort(); msg.readByte(); // type msg.readByte(); // identify flag - short amount = msg.readShort(); + Sint16 amount = msg.readShort(); msg.skip(2); // unknown msg.skip(8); // card (4 shorts) @@ -1421,16 +1426,16 @@ void do_parse() case SMSG_PLAYER_EQUIPMENT: { msg.readShort(); // length - int number = (msg.getLength() - 4) / 20; + Sint32 number = (msg.getLength() - 4) / 20; for (int loop = 0; loop < number; loop++) { - short index = msg.readShort(); - short itemId = msg.readShort(); + Sint16 index = msg.readShort(); + Sint16 itemId = msg.readShort(); msg.readByte(); // type msg.readByte(); // identify flag msg.readShort(); // equip type - short equipPoint = msg.readShort(); + Sint16 equipPoint = msg.readShort(); msg.readByte(); // attribute msg.readByte(); // refine msg.skip(8); // card @@ -1456,8 +1461,8 @@ void do_parse() case SMSG_ITEM_USE_RESPONSE: { - short index = msg.readShort(); - short amount = msg.readShort(); + Sint16 index = msg.readShort(); + Sint16 amount = msg.readShort(); if (msg.readByte() == 0) { chatWindow->chat_log("Failed to use item", BY_SERVER); @@ -1473,8 +1478,8 @@ void do_parse() map_path = "maps/" + msg.readString(16); map_path= map_path.substr(0, map_path.rfind(".")) + ".tmx.gz"; - int x = msg.readShort(); - int y = msg.readShort(); + Uint16 x = msg.readShort(); + Uint16 y = msg.readShort(); logger->log("Warping to %s (%d, %d)", map_path.c_str(), x, y); @@ -1531,8 +1536,8 @@ void do_parse() case SMSG_PLAYER_STAT_UPDATE_1: { - short type = msg.readShort(); - long value = msg.readLong(); + Sint16 type = msg.readShort(); + Sint32 value = msg.readLong(); switch (type) { @@ -1605,9 +1610,9 @@ void do_parse() msg.readLong(); // server tick msg.readLong(); // src speed msg.readLong(); // dst speed - short param1 = msg.readShort(); + Sint16 param1 = msg.readShort(); msg.readShort(); // param 2 - char type = msg.readByte(); + Sint8 type = msg.readByte(); msg.readShort(); // param 3 switch (type) @@ -1663,7 +1668,7 @@ void do_parse() break; case SMSG_BEING_LEVELUP: - if ((unsigned long)msg.readLong() == player_node->getId()) { + if ((Uint32)msg.readLong() == player_node->getId()) { logger->log("Level up"); sound.playSfx("sfx/levelup.ogg"); } else { @@ -1673,21 +1678,21 @@ void do_parse() break; case SMSG_BEING_EMOTION: + if (!(being = findNode(msg.readLong()))) { - Being *being = findNode(msg.readLong()); - if (being == NULL) break; - - being->emotion = msg.readByte(); - being->emotion_time = EMOTION_TIME; + break; } + + being->emotion = msg.readByte(); + being->emotion_time = EMOTION_TIME; break; case SMSG_PLAYER_STAT_UPDATE_3: { - long type = msg.readLong(); - long base = msg.readLong(); - long bonus = msg.readLong(); - long total = base + bonus; + Sint32 type = msg.readLong(); + Sint32 base = msg.readLong(); + Sint32 bonus = msg.readLong(); + Sint32 total = base + bonus; switch (type) { case 0x000d: player_info->STR = total; break; @@ -1718,10 +1723,10 @@ void do_parse() for (int k = 0; k < n_items; k++) { - long value = msg.readLong(); + Sint32 value = msg.readLong(); msg.readLong(); // DCvalue msg.readByte(); // type - short itemId = msg.readShort(); + Sint16 itemId = msg.readShort(); buyDialog->addItem(itemId, value); } break; @@ -1735,8 +1740,8 @@ void do_parse() for (int k = 0; k < n_items; k++) { - short index = msg.readShort(); - long value = msg.readLong(); + Sint16 index = msg.readShort(); + Sint32 value = msg.readLong(); msg.readLong(); // OCvalue Item *item = inventory->getItem(index); @@ -1769,16 +1774,16 @@ void do_parse() case SMSG_PLAYER_INVENTORY_ADD: { - short index = msg.readShort(); - short amount = msg.readShort(); - short itemId = msg.readShort(); + Sint16 index = msg.readShort(); + Sint16 amount = msg.readShort(); + Sint16 itemId = msg.readShort(); msg.readByte(); // identify flag msg.readByte(); // attribute msg.readByte(); // refine msg.skip(8); // card - short equipType = msg.readShort(); + Sint16 equipType = msg.readShort(); msg.readByte(); // type - char fail = msg.readByte(); + Sint8 fail = msg.readByte(); if (fail > 0) { chatWindow->chat_log("Unable to pick up item", @@ -1792,18 +1797,18 @@ void do_parse() case SMSG_PLAYER_INVENTORY_REMOVE: { - short index = msg.readShort(); - short amount = msg.readShort(); + Sint16 index = msg.readShort(); + Sint16 amount = msg.readShort(); inventory->getItem(index)->increaseQuantity(-amount); } break; case SMSG_PLAYER_INVENTORY_USE: { - short index = msg.readShort(); + Sint16 index = msg.readShort(); msg.readShort(); // item id msg.readLong(); // id - short amountLeft = msg.readShort(); + Sint16 amountLeft = msg.readShort(); msg.readByte(); // type inventory->getItem(index)->setQuantity(amountLeft); @@ -1817,14 +1822,14 @@ void do_parse() for (int k = 0; k < n_items; k++) { - short skillId = msg.readShort(); + Sint16 skillId = msg.readShort(); msg.readShort(); // target type msg.readShort(); // unknown - short level = msg.readShort(); - short sp = msg.readShort(); + Sint16 level = msg.readShort(); + Sint16 sp = msg.readShort(); msg.readShort(); // range std::string skillName = msg.readString(24); - char up = msg.readByte(); + Sint8 up = msg.readByte(); if (level != 0 || up != 0) { @@ -1847,11 +1852,11 @@ void do_parse() case SMSG_ITEM_VISIBLE: case SMSG_ITEM_DROPPED: { - long id = msg.readLong(); - short itemId = msg.readShort(); + Uint32 id = msg.readLong(); + Sint16 itemId = msg.readShort(); msg.readByte(); // identify flag - short x = msg.readShort(); - short y = msg.readShort(); + Uint16 x = msg.readShort(); + Uint16 y = msg.readShort(); msg.skip(4); // amount,subX,subY / subX,subY,amount add_floor_item(new FloorItem(id, itemId, x, y)); @@ -1870,34 +1875,32 @@ void do_parse() break; case SMSG_BEING_CHANGE_LOOKS: + if (!(being = findNode(msg.readLong()))) { - Being *being = findNode(msg.readLong()); + break; + } - if (being) - { - switch (msg.readByte()) { - case 1: - being->setHairStyle(msg.readByte()); - break; - case 2: - being->setWeapon(msg.readByte()); - break; - case 6: - being->setHairColor(msg.readByte()); - break; - default: - msg.readByte(); // unsupported - break; - } - } + switch (msg.readByte()) { + case 1: + being->setHairStyle(msg.readByte()); + break; + case 2: + being->setWeapon(msg.readByte()); + break; + case 6: + being->setHairColor(msg.readByte()); + break; + default: + msg.readByte(); // unsupported + break; } break; case SMSG_PLAYER_EQUIP: { - short index = msg.readShort(); - short equipPoint = msg.readShort(); - char type = msg.readByte(); + Sint16 index = msg.readShort(); + Sint16 equipPoint = msg.readShort(); + Sint8 type = msg.readByte(); logger->log("Equipping: %i %i %i", index, equipPoint, type); @@ -1931,9 +1934,9 @@ void do_parse() case 0x01d7: // Equipment related { - Being *being = findNode(msg.readLong()); + being = findNode(msg.readLong()); msg.readByte(); // equip point - short itemId1 = msg.readShort(); + Sint16 itemId1 = msg.readShort(); msg.readShort(); // item id 2 if (being != NULL) @@ -1945,9 +1948,9 @@ void do_parse() case SMSG_PLAYER_UNEQUIP: { - short index = msg.readShort(); - short equipPoint = msg.readShort(); - char type = msg.readByte(); + Sint16 index = msg.readShort(); + Sint16 equipPoint = msg.readShort(); + Sint8 type = msg.readByte(); if (type == 0) { chatWindow->chat_log("Unable to unequip.", BY_SERVER); @@ -1999,7 +2002,7 @@ void do_parse() case SMSG_PLAYER_ARROW_EQUIP: { - short id = msg.readShort(); + Sint16 id = msg.readShort(); if (id > 1) { Item *item = inventory->getItem(id); @@ -2014,7 +2017,7 @@ void do_parse() case SMSG_PLAYER_ARROW_MESSAGE: { - short type = msg.readShort(); + Sint16 type = msg.readShort(); switch (type) { case 0: @@ -2030,9 +2033,9 @@ void do_parse() case SMSG_PLAYER_STAT_UPDATE_4: { - short type = msg.readShort(); - char fail = msg.readByte(); - char value = msg.readByte(); + Sint16 type = msg.readShort(); + Sint8 fail = msg.readByte(); + Sint8 value = msg.readByte(); if (fail == 1) { @@ -2090,12 +2093,9 @@ void do_parse() break; case SMSG_BEING_NAME_RESPONSE: + if ((being = findNode(msg.readLong()))) { - Being *being = findNode(msg.readLong()); - - if (being) { - being->setName(msg.readString(24)); - } + being->setName(msg.readString(24)); } break; diff --git a/src/inventory.cpp b/src/inventory.cpp index 39860261..63fbab7a 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -44,6 +44,11 @@ Inventory::~Inventory() Item* Inventory::getItem(int index) { + if (index < 0 || index > INVENTORY_SIZE) + { + return 0; + } + return &items[index]; } |