diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | src/gamehandler.cpp | 31 | ||||
-rw-r--r-- | src/gamehandler.h | 7 | ||||
-rw-r--r-- | src/object.h | 48 | ||||
-rw-r--r-- | src/player.cpp | 2 | ||||
-rw-r--r-- | src/state.cpp | 45 | ||||
-rw-r--r-- | src/state.h | 6 |
7 files changed, 77 insertions, 70 deletions
@@ -1,3 +1,11 @@ +2006-09-03 Guillaume Melquiond <guillaume.melquiond@gmail.com> + + * src/player.cpp, src/object.h, src/gamehandler.h, src/state.cpp, + src/gamehandler.cpp, src/state.h: Removed mNeedUpdate and mNew from + Object class; added update flags instead. Reduced size of Object + fields. Moved sayAround from GameHandler to State class. Improved + handling of flags for move messages. + 2006-09-02 Bjørn Lindeijer <bjorn@lindeijer.nl> * src/defines.h: Corrected hair and hair color constants to match diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 26f300ec..a91fbbc6 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -160,7 +160,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_SAY: { std::string say = message.readString(); - sayAround(computer, say); + gameState->sayAround(computer.getCharacter().get(), say); } break; case PGMSG_PICKUP: @@ -222,35 +222,6 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) computer.send(result); } -void GameHandler::sayAround(GameClient &computer, std::string const &text) -{ - PlayerPtr beingPtr = computer.getCharacter(); - - MessageOut msg(GPMSG_SAY); - msg.writeShort(beingPtr->getPublicID()); - msg.writeString(text); - - unsigned speakerMapId = beingPtr->getMapId(); - Point speakerPosition = beingPtr->getPosition(); - - for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); - i != i_end; ++i) - { - // See if the other being is near enough, then send the message - Player const *listener = - static_cast<GameClient *>(*i)->getCharacter().get(); - - if (!listener || listener->getMapId() != speakerMapId) { - continue; - } - - if (speakerPosition.inRangeOf(listener->getPosition())) - { - (*i)->send(msg); - } - } -} - void GameHandler::sendTo(Player *beingPtr, MessageOut &msg) { GameClient *client = beingPtr->getClient(); diff --git a/src/gamehandler.h b/src/gamehandler.h index e0f39e65..41d5f780 100644 --- a/src/gamehandler.h +++ b/src/gamehandler.h @@ -53,13 +53,6 @@ class GameHandler: public ConnectionHandler void processMessage(NetComputer *computer, MessageIn &message); private: - /** - * Display a message to every player around one's player - * in the default channel. - * The tile area has been set to 10 for now. - */ - void sayAround(GameClient &computer, std::string const &text); - void removeOutdatedPending(); }; diff --git a/src/object.h b/src/object.h index bb800013..8c771aa2 100644 --- a/src/object.h +++ b/src/object.h @@ -30,6 +30,12 @@ #include "point.h" #include "utils/countedptr.h" +enum +{ + NEW_ON_MAP = 1, + NEW_DESTINATION = 2 +}; + /** * Generic in-game object definition. * Base class for in-game objects. @@ -42,8 +48,7 @@ class Object */ Object(int type) : mType(type), - mNew(true), - mNeedUpdate(false) + mUpdateFlags(0) {} /** @@ -82,39 +87,42 @@ class Object update() = 0; /** - * Get map name where being is + * Gets the map the object is located on. * - * @return Name of map being is located. + * @return ID of map. */ - unsigned int getMapId() const + int getMapId() const { return mMapId; } /** - * Set map being is located + * Sets the map the object is located on. */ - void setMapId(unsigned int mapId) + void setMapId(int mapId) { mMapId = mapId; } /** - * Tells if the object just appeared. + * Gets what changed in the object. */ - bool isNew() const - { return mNew; } + int getUpdateFlags() const + { return mUpdateFlags; } /** - * Sets the age of the object. + * Sets some changes in the object. */ - void setNew(bool n) - { mNew = n; } + void raiseUpdateFlags(int n) + { mUpdateFlags |= n; } + + /** + * Clears changes in the object. + */ + void clearUpdateFlags() + { mUpdateFlags = 0; } private: - int mType; /**< Object type */ + char mType; /**< Object type */ + char mUpdateFlags; /**< changes in object status */ + unsigned short mMapId; /**< id of the map being is on */ Point mPos; /**< coordinates */ - unsigned int mMapId; /**< id of the map being is on */ - bool mNew; /**< true if the object just appeared */ - - protected: - bool mNeedUpdate; /**< update() must be invoked if true */ }; /** @@ -141,7 +149,7 @@ class MovingObject: public Object * Sets the destination coordinates of the object. */ void setDestination(Point dst) - { mDst = dst; } + { mDst = dst; raiseUpdateFlags(NEW_DESTINATION); } /** * Gets the old coordinates of the object. diff --git a/src/player.cpp b/src/player.cpp index 25e8b2ea..70659efc 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -42,8 +42,6 @@ void Player::update() setStat(STAT_MAG, 10 + mRawStats.stats[STAT_INT]); setStat(STAT_ACC, 50 + mRawStats.stats[STAT_DEX]); setStat(STAT_SPD, mRawStats.stats[STAT_DEX]); - - mNeedUpdate = false; } void Player::setInventory(const std::vector<unsigned int> &inven) diff --git a/src/state.cpp b/src/state.cpp index e992fbf6..d959876a 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -101,7 +101,7 @@ State::update() * the last time and whether they will be around the next time. */ bool wereInRange = (*p)->getOldPosition().inRangeOf(os) && - !(*p)->isNew() && !(*o)->isNew(); + !(((*p)->getUpdateFlags() | (*o)->getUpdateFlags()) & NEW_ON_MAP); bool willBeInRange = (*p)->getPosition().inRangeOf(on); int flags = 0; @@ -159,6 +159,10 @@ State::update() if (on.x != od.x || on.y != od.y) { flags |= MOVING_POSITION; + if ((*o)->getUpdateFlags() & NEW_DESTINATION) + { + flags |= MOVING_DESTINATION; + } } else { @@ -166,14 +170,6 @@ State::update() flags |= MOVING_DESTINATION; } - // TODO: updates destination only on changes. - flags |= MOVING_DESTINATION; - - if (!(flags & (MOVING_POSITION | MOVING_DESTINATION))) - { - continue; - } - msg.writeShort((*o)->getPublicID()); msg.writeByte(flags); if (flags & MOVING_POSITION) @@ -195,7 +191,7 @@ State::update() for (Movings::iterator o = movings.begin(), o_end = movings.end(); o != o_end; ++o) { - (*o)->setNew(false); + (*o)->clearUpdateFlags(); } } } @@ -207,7 +203,7 @@ State::addObject(ObjectPtr objectPtr) MapComposite *map = loadMap(mapId); if (!map) return; map->objects.push_back(objectPtr); - objectPtr->setNew(true); + objectPtr->raiseUpdateFlags(NEW_ON_MAP); int type = objectPtr->getType(); if (type != OBJECT_MONSTER && type != OBJECT_PLAYER && type != OBJECT_NPC) return; map->allocate(static_cast< MovingObject * >(objectPtr.get())); @@ -291,3 +287,30 @@ MapComposite *State::loadMap(unsigned mapId) return tmp; } + +void State::sayAround(Object *obj, std::string text) +{ + unsigned short id = 65535; + int type = obj->getType(); + if (type == OBJECT_PLAYER || type == OBJECT_NPC || type == OBJECT_MONSTER) + { + id = static_cast< MovingObject * >(obj)->getPublicID(); + } + MessageOut msg(GPMSG_SAY); + msg.writeShort(id); + msg.writeString(text); + + std::map< unsigned, MapComposite * >::iterator m = maps.find(obj->getMapId()); + if (m == maps.end()) return; + MapComposite *map = m->second; + Point speakerPosition = obj->getPosition(); + + for (std::vector< Player * >::iterator i = map->players.begin(), + i_end = map->players.end(); i != i_end; ++i) + { + if (speakerPosition.inRangeOf((*i)->getPosition())) + { + gameHandler->sendTo(*i, msg); + } + } +} diff --git a/src/state.h b/src/state.h index 07d466a1..36bd87d0 100644 --- a/src/state.h +++ b/src/state.h @@ -25,6 +25,7 @@ #define _TMW_SERVER_STATE_ #include <map> +#include <string> #include "object.h" @@ -64,6 +65,11 @@ class State * Remove an object from the map. */ void removeObject(ObjectPtr objectPtr); + + /** + * Say around an object. + */ + void sayAround(Object *, std::string text); }; extern State *gameState; |