From 1c43b2cdee5a5508a6e771b477100517deae07e7 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Tue, 26 Jul 2005 20:07:20 +0000 Subject: Added Being::Type enumeration and type-aware findNode(). Fixed font not being reset after drawing speech. Target marker drawing moved into engine. --- ChangeLog | 9 +++ src/being.cpp | 99 +++++++++----------------- src/being.h | 24 +++---- src/engine.cpp | 194 +++++++++++++++++++++++++++----------------------- src/game.cpp | 119 ++++++++++++++++++------------- src/gui/minimap.cpp | 25 ++++--- src/gui/popupmenu.cpp | 45 +++++++----- src/net/protocol.cpp | 30 +++++--- 8 files changed, 285 insertions(+), 260 deletions(-) diff --git a/ChangeLog b/ChangeLog index f764a9cc..94239775 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-07-26 Björn Steinbrink + + * src/being.cpp, src/being.h, src/engine.cpp, src/game.cpp, + src/gui/minimap.cpp, src/gui/popupmenu.cpp, src/net/protocol.cpp: + Introduced a Being::Type enumeration. Added type-aware findNode() + function. + * src/being.cpp: Set font back to gui font after drawing speech. + Draw auto-target marker in engine instead of being. + 2005-07-26 Eugenio Favalli * The Mana World.dev: Updated release infos. diff --git a/src/being.cpp b/src/being.cpp index 5faa3974..315dbd7e 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -49,7 +49,7 @@ void add_node(Being *being) beings.push_back(being); // If the being is a player, request the name - if (being->isPlayer()) { + if (being->getType() == Being::PLAYER) { WFIFOW(0) = net_w_value(0x0094); WFIFOL(2) = net_l_value(RFIFOL(2)); WFIFOSET(6); @@ -78,10 +78,6 @@ void remove_node(unsigned int id) { if ((*i)->getId() == id) { - if (autoTarget == (*i)) - { - autoTarget = NULL; - } delete (*i); beings.erase(i); return; @@ -89,48 +85,6 @@ void remove_node(unsigned int id) } } -unsigned int findNpc(unsigned short x, unsigned short y) -{ - std::list::iterator i; - for (i = beings.begin(); i != beings.end(); i++) { - Being *being = (*i); - // Check if is a NPC (only low job ids) - if (being->isNpc() && being->x == x && being->y == y) - { - return being->getId(); - } - } - return 0; -} - -unsigned int findPlayer(unsigned short x, unsigned short y) -{ - std::list::iterator i; - for (i = beings.begin(); i != beings.end(); i++) { - Being *being = (*i); - // Check if is a player - if (being->isPlayer() && being->x == x && being->y == y) { - return being->getId(); - } - } - return 0; -} - -unsigned int findMonster(unsigned short x, unsigned short y) -{ - std::list::iterator i; - for (i = beings.begin(); i != beings.end(); i++) { - Being *being = (*i); - // Check if is a MONSTER that is alive - if (being->isMonster() && being->x == x && being->y == y && - being->action != MONSTER_DEAD) - { - return being->getId(); - } - } - return 0; -} - Being *findNode(unsigned int id) { std::list::iterator i; @@ -156,6 +110,21 @@ Being *findNode(unsigned short x, unsigned short y) return NULL; } +Being* findNode(unsigned short x, unsigned short y, Being::Type type) +{ + std::list::iterator i; + for (i = beings.begin(); i != beings.end(); i++) { + Being *being = (*i); + // Check if is a NPC (only low job ids) + if (being->x == x && being->y == y && + being->getType() == type && being->action != MONSTER_DEAD) + { + return being; + } + } + return NULL; +} + class BeingCompare { public: bool operator() (const Being *a, const Being *b) const { @@ -309,7 +278,7 @@ void Being::nextStep() void Being::logic() { - if (isPlayer()) + if (getType() == PLAYER) { switch (action) { case WALK: @@ -353,6 +322,9 @@ void Being::drawSpeech(Graphics *graphics) graphics->drawText(speech, text_x + 18, text_y - 60, gcn::Graphics::CENTER); + + // Backing to default font + graphics->setFont(gui->getFont()); } if (showDamage) { // Selecting the right color @@ -360,7 +332,7 @@ void Being::drawSpeech(Graphics *graphics) { graphics->setFont(hitYellowFont); } - else if (isMonster()) + else if (getType() == MONSTER) { graphics->setFont(hitBlueFont); } @@ -371,7 +343,7 @@ void Being::drawSpeech(Graphics *graphics) int textX = 0; int textY = 0; - if (this->isPlayer()) { + if (getType() == PLAYER) { textX = 16; textY = 70; } @@ -388,26 +360,19 @@ void Being::drawSpeech(Graphics *graphics) // Backing to default font graphics->setFont(gui->getFont()); } - if (this == autoTarget) { - graphics->drawText("[TARGET]", - text_x + 60, text_y, - gcn::Graphics::CENTER); - } } -bool Being::isPlayer() +Being::Type Being::getType() { - return job < 10; -} - -bool Being::isNpc() -{ - return job >= 100 && job < 200; -} - -bool Being::isMonster() -{ - return job >= 1000 && job < 1200; + if (job < 10) { + return PLAYER; + } else if (job >= 100 & job < 200) { + return NPC; + } else if (job >= 1000 && job < 1200) { + return MONSTER; + } else { + return UNKNOWN; + } } void Being::setWeapon(unsigned short weapon) diff --git a/src/being.h b/src/being.h index ec933d90..3a68e0ac 100644 --- a/src/being.h +++ b/src/being.h @@ -46,6 +46,13 @@ struct PATH_NODE { class Being { public: + enum Type { + UNKNOWN, + PLAYER, + NPC, + MONSTER + }; + unsigned short job; /**< Job (player job, npc, monster, ) */ unsigned short x, y; /**< Tile coordinates */ unsigned char direction; /**< Facing direction */ @@ -142,19 +149,9 @@ class Being void drawSpeech(Graphics *graphics); /** - * Checks if the being is a player. - */ - bool isPlayer(); - - /** - * Checks if the being is a npc. + * Returns the type of the being. */ - bool isNpc(); - - /** - * Checks if the being is a monster. - */ - bool isMonster(); + Type getType(); // ACCES METHODS @@ -222,6 +219,9 @@ Being *findNode(unsigned int id); /** Return a being at specific coordinates */ Being *findNode(unsigned short x, unsigned short y); +/** Return a being at specific coordinates with specific type*/ +Being *findNode(unsigned short x, unsigned short y, Being::Type type); + /** Remove a Being */ void remove_node(unsigned int id); diff --git a/src/engine.cpp b/src/engine.cpp index f2140f40..45a2c3e4 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -390,117 +390,122 @@ void Engine::draw() guiGraphics->setColor(gcn::Color(0, 0, 255)); guiGraphics->drawRectangle(gcn::Rectangle(sx * 32, sy * 32, 32, 32)); #endif - - // Draw a NPC - if (being->isNpc()) + int frame; + switch (being->getType()) { - guiGraphics->drawImage(npcset->spriteset[being->job - 100], - sx * 32 - 8 - offset_x, - sy * 32 - 52 - offset_y); - } + // Draw a player + case Being::PLAYER: + being->text_x = sx * 32 + get_x_offset(being) - offset_x; + being->text_y = sy * 32 + get_y_offset(being) - offset_y; - // Draw a player - else if (being->isPlayer()) - { - being->text_x = sx * 32 + get_x_offset(being) - offset_x; - being->text_y = sy * 32 + get_y_offset(being) - offset_y; - - if (being->action == SIT || being->action == DEAD) { - being->frame = 0; - } - - int pf = being->frame + being->action; - - if (being->action == ATTACK) { - if (being->getWeapon() > 0) - pf += 4 * (being->getWeapon() - 1); - } + if (being->action == SIT || being->action == DEAD) { + being->frame = 0; + } - guiGraphics->drawImage(playerset->spriteset[pf + 16 * dir], - being->text_x - 16, being->text_y - 32); + frame = being->frame + being->action; - //if (being->action == ATTACK) - //{ - // std::cout << being->name << " " << being->getWeapon() << std::endl; - //} - if (being->getWeapon() != 0 && being->action == ATTACK) { - Image *image = weaponset->spriteset[ - 16 * (being->getWeapon() - 1) + 4 * being->frame + dir]; + if (being->action == ATTACK) { + if (being->getWeapon() > 0) + frame += 4 * (being->getWeapon() - 1); + } - guiGraphics->drawImage(image, - being->text_x - 64, being->text_y - 80); - } + guiGraphics->drawImage(playerset->spriteset[frame + 16 * dir], + being->text_x - 16, being->text_y - 32); - if (being->getHairColor() <= NR_HAIR_COLORS) { - int hf = being->getHairColor() - 1 + 10 * (dir + 4 * - (being->getHairStyle() - 1)); + //if (being->action == ATTACK) + //{ + // std::cout << being->name << " " << being->getWeapon() << std::endl; + //} + if (being->getWeapon() != 0 && being->action == ATTACK) { + Image *image = weaponset->spriteset[ + 16 * (being->getWeapon() - 1) + 4 * being->frame + dir]; - guiGraphics->drawImage(hairset->spriteset[hf], - being->text_x - 2 + 2 * hairtable[pf][dir][0], - being->text_y - 50 + 2 * hairtable[pf][dir][1]); - } + guiGraphics->drawImage(image, + being->text_x - 64, being->text_y - 80); + } - if (being->emotion != 0) { - guiGraphics->drawImage( - emotionset->spriteset[being->emotion - 1], - sx * 32 + 3 + get_x_offset(being) - offset_x, - sy * 32 - 90 + get_y_offset(being) - offset_y); - } + if (being->getHairColor() <= NR_HAIR_COLORS) { + int hf = being->getHairColor() - 1 + 10 * (dir + 4 * + (being->getHairStyle() - 1)); - graphics->setFont(speechFont); - graphics->drawText(being->name, - being->text_x + 15, being->text_y + 30, - gcn::Graphics::CENTER); - graphics->setFont(gui->getFont()); - } + guiGraphics->drawImage(hairset->spriteset[hf], + being->text_x - 2 + 2 * hairtable[frame][dir][0], + being->text_y - 50 + 2 * hairtable[frame][dir][1]); + } - // Draw a warp - else if (being->job == 45) - { - } + if (being->emotion != 0) { + guiGraphics->drawImage( + emotionset->spriteset[being->emotion - 1], + sx * 32 + 3 + get_x_offset(being) - offset_x, + sy * 32 - 90 + get_y_offset(being) - offset_y); + } - // Draw a monster - else - { - if (being->frame >= 4) - { - being->frame = 3; - } + graphics->setFont(speechFont); + graphics->drawText(being->name, + being->text_x + 15, being->text_y + 30, + gcn::Graphics::CENTER); + graphics->setFont(gui->getFont()); + break; + + // Draw a NPC + case Being::NPC: + guiGraphics->drawImage(npcset->spriteset[being->job - 100], + sx * 32 - 8 - offset_x, + sy * 32 - 52 - offset_y); + break; + + // Draw a monster + case Being::MONSTER: + if (being->frame >= 4) + { + being->frame = 3; + } - being->text_x = sx * 32 - 42 + get_x_offset(being) - offset_x; - being->text_y = sy * 32 - 65 + get_y_offset(being) - offset_y; + being->text_x = sx * 32 - 42 + get_x_offset(being) - offset_x; + being->text_y = sy * 32 - 65 + get_y_offset(being) - offset_y; - int mf = being->frame + being->action; + frame = being->frame + being->action; - if (being->action == MONSTER_DEAD) { - guiGraphics->drawImage( - monsterset[being->job - 1002]->spriteset[dir + 4 * MONSTER_DEAD], - being->text_x + 30, being->text_y + 40); + if (being->action == MONSTER_DEAD) { + guiGraphics->drawImage( + monsterset[being->job - 1002]->spriteset[dir + 4 * MONSTER_DEAD], + being->text_x + 30, being->text_y + 40); - if (autoTarget == being) { - autoTarget = NULL; + if (autoTarget == being) { + autoTarget = NULL; + } } - } - else { - guiGraphics->drawImage( - monsterset[being->job-1002]->spriteset[dir + 4 * mf], - being->text_x + 30, being->text_y + 40); - - if (being->x == mouseTileX && being->y == mouseTileY) - { - guiGraphics->drawImage(attackTarget, - being->text_x + 30 + 16, being->text_y + 32); + else { + guiGraphics->drawImage( + monsterset[being->job-1002]->spriteset[dir + 4 * frame], + being->text_x + 30, being->text_y + 40); + + if (being->x == mouseTileX && being->y == mouseTileY) + { + guiGraphics->drawImage(attackTarget, + being->text_x + 30 + 16, being->text_y + 32); + } } - } - if (being->action != STAND) { - being->frame = - (get_elapsed_time(being->walk_time) * 4) / (being->speed); + if (being->action != STAND) { + being->frame = + (get_elapsed_time(being->walk_time) * 4) / (being->speed); - if (being->frame >= 4 && being->action != MONSTER_DEAD) { - being->nextStep(); + if (being->frame >= 4 && being->action != MONSTER_DEAD) { + being->nextStep(); + } } - } + break; + + /* + // Draw a warp (job == 45) + case Being::WARP: + break; + */ + + // No idea how to draw this ;) + default: + break; } beingIterator++; @@ -552,6 +557,13 @@ void Engine::draw() beingIterator++; } + if (autoTarget) { + graphics->drawText("[TARGET]", + autoTarget->text_x + 60, + autoTarget->text_y, + gcn::Graphics::CENTER); + } + if (statsWindow->isVisible()) { statsWindow->update(); } diff --git a/src/game.cpp b/src/game.cpp index 16addcd4..df5033be 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -536,39 +536,47 @@ void do_input() if (target) { - // NPC default: talk - if ((target->isNpc()) && (current_npc == 0)) + switch (target->getType()) { - WFIFOW(0) = net_w_value(0x0090); - WFIFOL(2) = net_l_value(target->getId()); - WFIFOB(6) = 0; - WFIFOSET(7); - current_npc = target->getId(); - } - // Monster default: attack - else if (target->isMonster()) - { - /** - * TODO: Move player to mouse click position before - * attack the monster (maybe using follow mode). - */ - if (target->action != MONSTER_DEAD && - player_node->action==STAND) - { - attack(target); - // Autotarget by default with mouse - //if (keys[SDLK_LSHIFT]) - //{ + // Player default: trade + case Being::PLAYER: + WFIFOW(0) = net_w_value(0x00e4); + WFIFOL(2) = net_l_value(target->getId()); + WFIFOSET(6); + break; + + // NPC default: talk + case Being::NPC: + if (!current_npc) + { + WFIFOW(0) = net_w_value(0x0090); + WFIFOL(2) = net_l_value(target->getId()); + WFIFOB(6) = 0; + WFIFOSET(7); + current_npc = target->getId(); + } + break; + + // Monster default: attack + case Being::MONSTER: + /** + * TODO: Move player to mouse click position before + * attack the monster (maybe using follow mode). + */ + if (target->action != MONSTER_DEAD && + player_node->action==STAND) + { + attack(target); + // Autotarget by default with mouse + //if (keys[SDLK_LSHIFT]) + //{ autoTarget = target; - //} - } - } - // Player default: trade - else if (target->isPlayer()) - { - WFIFOW(0) = net_w_value(0x00e4); - WFIFOL(2) = net_l_value(target->getId()); - WFIFOSET(6); + //} + } + break; + + default: + break; } } // Check for default action to items on the floor @@ -947,19 +955,26 @@ void do_parse() { if (RFIFOB(6) == 1) { // Death - if (being->isMonster()) - { - being->action = MONSTER_DEAD; - being->frame = 0; - being->walk_time = tick_time; - } - else + switch (being->getType()) { - being->action = DEAD; + case Being::MONSTER: + being->action = MONSTER_DEAD; + being->frame = 0; + being->walk_time = tick_time; + break; + + default: + being->action = DEAD; + break; } //remove_node(RFIFOL(2)); } - else remove_node(RFIFOL(2)); + else { + remove_node(RFIFOL(2)); + if (being == autoTarget) { + autoTarget = NULL; + } + } } break; @@ -1354,16 +1369,20 @@ void do_parse() if (RFIFOL(2) != player_node->getId()) { // buggy being = findNode(RFIFOL(2)); - if (being != NULL) { - if (being->isPlayer()) { - being->action = ATTACK; - being->frame = 0; - being->walk_time = tick_time; - } - else { - being->action = MONSTER_ATTACK; - being->frame = 0; - being->walk_time = tick_time; + if (being) { + switch (being->getType()) + { + case Being::PLAYER: + being->action = ATTACK; + being->frame = 0; + being->walk_time = tick_time; + break; + + default: + being->action = MONSTER_ATTACK; + being->frame = 0; + being->walk_time = tick_time; + break; } being->frame = 0; } diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index 98392adf..c733da42 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -91,18 +91,21 @@ void Minimap::draw(gcn::Graphics *graphics) being->x / 2 + getPadding() - 1, being->y / 2 + getTitleBarHeight() - 1, 3, 3)); } - else if (being->isPlayer()) + else { - // Other player dot - graphics->setColor(gcn::Color(61, 52, 209)); - graphics->fillRectangle(gcn::Rectangle( - being->x / 2 + getPadding(), - being->y / 2 + getTitleBarHeight(), 1, 1)); - } - else if (being->isMonster()) - { - // Enemy dot - graphics->setColor(gcn::Color(209, 52, 61)); + switch (being->getType()) { + case Being::PLAYER: + graphics->setColor(gcn::Color(61, 52, 209)); + break; + + case Being::MONSTER: + graphics->setColor(gcn::Color(209, 52, 61)); + break; + + default: + break; + } + graphics->fillRectangle(gcn::Rectangle( being->x / 2 + getPadding(), being->y / 2 + getTitleBarHeight(), 1, 1)); diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp index 77f95d2f..18f90af5 100644 --- a/src/gui/popupmenu.cpp +++ b/src/gui/popupmenu.cpp @@ -70,22 +70,31 @@ void PopupMenu::showPopup(int mx, int my) mY = my; browserBox->clearRows(); - if (being && being->isNpc()) - { - // NPCs can be talked to (single option, candidate for removal unless - // more options would be added) - browserBox->addRow("@@talk|Talk To NPC@@"); - } - else if (being && being->isPlayer()) - { - // Players can be traded with. Later also attack, follow and add as - // buddy will be options in this menu. - - std::string name = being->name; - //browserBox->addRow("@@attack|Attack " + name + "@@"); - browserBox->addRow("@@trade|Trade With " + name + "@@"); - //browserBox->addRow("@@follow|Follow " + name + "@@"); - //browserBox->addRow("@@buddy|Add " + name + " to Buddy List@@"); + if (being) { + std::string name; + switch (being->getType()) + { + case Being::PLAYER: + // Players can be traded with. Later also attack, follow and + // add as buddy will be options in this menu. + + name = being->name; + //browserBox->addRow("@@attack|Attack " + name + "@@"); + browserBox->addRow("@@trade|Trade With " + name + "@@"); + //browserBox->addRow("@@follow|Follow " + name + "@@"); + //browserBox->addRow("@@buddy|Add " + name + " to Buddy List@@"); + break; + + case Being::NPC: + // NPCs can be talked to (single option, candidate for removal + // unless more options would be added) + browserBox->addRow("@@talk|Talk To NPC@@"); + break; + + default: + /* Other beings aren't interesting... */ + break; + } } else if (floorItem) { @@ -118,7 +127,7 @@ void PopupMenu::showPopup(int mx, int my) void PopupMenu::handleLink(const std::string& link) { // Talk To action - if ((link == "talk") && being && being->isNpc() && + if ((link == "talk") && being && being->getType() == Being::NPC && (current_npc == 0)) { WFIFOW(0) = net_w_value(0x0090); @@ -129,7 +138,7 @@ void PopupMenu::handleLink(const std::string& link) } // Trade action - else if ((link == "trade") && being && being->isPlayer()) + else if ((link == "trade") && being && being->getType() == Being::PLAYER) { WFIFOW(0) = net_w_value(0x00e4); WFIFOL(2) = net_l_value(being->getId()); diff --git a/src/net/protocol.cpp b/src/net/protocol.cpp index 5a3fd487..8a8f0e6d 100644 --- a/src/net/protocol.cpp +++ b/src/net/protocol.cpp @@ -230,22 +230,30 @@ Being* attack(unsigned short x, unsigned short y, unsigned char direction) { Being *target = NULL; - if (direction == SOUTH) { - target = findNode(x, y + 1); - } else if(direction == WEST) { - target = findNode(x - 1, y); - } else if(direction == NORTH) { - target = findNode(x, y - 1); - } else if(direction == EAST) { - target = findNode(x + 1, y); + switch (direction) + { + case SOUTH: + target = findNode(x, y + 1, Being::MONSTER); + break; + + case WEST: + target = findNode(x - 1, y, Being::MONSTER); + break; + + case NORTH: + target = findNode(x, y - 1, Being::MONSTER); + break; + + case EAST: + target = findNode(x + 1, y, Being::MONSTER); + break; } - if (target && target->isMonster()) { + if (target) { attack(target); - return target; } - return NULL; + return target; } void attack(Being *target) -- cgit v1.2.3-70-g09d2