diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/being.h | 52 | ||||
-rw-r--r-- | src/defines.h | 2 | ||||
-rw-r--r-- | src/gamehandler.cpp | 7 | ||||
-rw-r--r-- | src/state.cpp | 25 | ||||
-rw-r--r-- | src/state.h | 22 |
5 files changed, 74 insertions, 34 deletions
diff --git a/src/being.h b/src/being.h index 97c43d3e..6e2fbda6 100644 --- a/src/being.h +++ b/src/being.h @@ -32,28 +32,42 @@ #include "object.h" #include "utils/countedptr.h" -const unsigned int MAX_EQUIP_SLOTS = 5; /**< Maximum number of equipped slots */ +/** Maximum number of equipped slots */ +const unsigned int MAX_EQUIP_SLOTS = 5; /** * Raw statistics of a Player */ - -enum { STAT_STR = 0, STAT_AGI, STAT_VIT, STAT_INT, STAT_DEX, STAT_LUK, NB_RSTAT }; +enum { + STAT_STR = 0, + STAT_AGI, + STAT_VIT, + STAT_INT, + STAT_DEX, + STAT_LUK, + NB_RSTAT +}; /** * Structure types for the raw statistics of a Player */ - struct RawStatistics { unsigned short stats[NB_RSTAT]; }; -/* +/** * Computed statistics of a Being */ - -enum { STAT_HEA = 0, STAT_ATT, STAT_DEF, STAT_MAG, STAT_ACC, STAT_SPD, NB_CSTAT }; +enum { + STAT_HEA = 0, + STAT_ATT, + STAT_DEF, + STAT_MAG, + STAT_ACC, + STAT_SPD, + NB_CSTAT +}; /** * Structure type for the computed statistics of a Being. @@ -67,7 +81,7 @@ struct Statistics * Generic Being (living object). * Used for players & monsters (all animated objects). */ -class Being: public MovingObject +class Being : public MovingObject { public: /** @@ -102,7 +116,7 @@ class Being: public MovingObject Statistics mStats; /**< stats modifiers or computed stats */ }; -class Player: public Being +class Player : public Being { public: @@ -272,17 +286,19 @@ class Player: public Being Player(Player const &); Player &operator=(Player const &); - std::string mName; /**< name of the being */ - Gender mGender; /**< gender of the being */ - unsigned char mHairStyle;/**< Hair Style of the being */ - unsigned char mHairColor;/**< Hair Color of the being */ - unsigned char mLevel; /**< level of the being */ - unsigned int mMoney; /**< wealth of the being */ - RawStatistics mRawStats; /**< raw stats of the being */ + std::string mName; /**< name of the being */ + Gender mGender; /**< gender of the being */ + unsigned char mHairStyle; /**< Hair Style of the being */ + unsigned char mHairColor; /**< Hair Color of the being */ + unsigned char mLevel; /**< level of the being */ + unsigned int mMoney; /**< wealth of the being */ + RawStatistics mRawStats; /**< raw stats of the being */ std::vector<unsigned int> inventory; /**< Player inventory */ - unsigned int equipment[MAX_EQUIP_SLOTS]; /**< Equipped item ID's (from inventory) */ -}; + + /** Equipped item ID's (from inventory) */ + unsigned int equipment[MAX_EQUIP_SLOTS]; +}; /** * Type definition for a smart pointer to Being. diff --git a/src/defines.h b/src/defines.h index 0d83b999..bd50207c 100644 --- a/src/defines.h +++ b/src/defines.h @@ -139,6 +139,8 @@ enum { CPMSG_CONNECT_RESPONSE = 0x0054, // B error // Game + GPMSG_PLAYER_MAP_CHANGE = 0x0100, // S filename, W x, W y, B newserv + // [, S32 token, S server, W port] PGMSG_PICKUP = 0x0110, GPMSG_PICKUP_RESPONSE = 0x0111, GPMSG_BEING_ENTER = 0x0200, // B type, L being id diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index af08db46..a133e5fa 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -254,8 +254,13 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) } } -void GameHandler::sendTo(PlayerPtr beingPtr, MessageOut &msg) +void +GameHandler::sendTo(PlayerPtr beingPtr, MessageOut &msg) { + /* TODO: This implementation is very inefficient. An alternative would be + * store the NetComputer reference with the player class, so that it can + * be directly accessed. + */ for (NetComputers::iterator i = clients.begin(); i != clients.end(); ++i) { PlayerPtr clientChar = static_cast<GameClient *>(*i)->getCharacter(); diff --git a/src/state.cpp b/src/state.cpp index 870be52a..0d3b8636 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -27,6 +27,7 @@ #include "map.h" #include "mapmanager.h" #include "messageout.h" +#include "storage.h" #include "utils/logger.h" @@ -115,9 +116,8 @@ State::addObject(ObjectPtr objectPtr) if (!loadMap(mapId)) return; maps[mapId].objects.push_back(objectPtr); if (objectPtr->getType() != OBJECT_PLAYER) return; - PlayerPtr playerPtr(objectPtr); Players &players = maps[mapId].players; - players.push_back(playerPtr); + PlayerPtr playerPtr(objectPtr); /* Currently when a player is added, all existing players are notified * about this. This will need to be modified so that players only know @@ -136,6 +136,20 @@ State::addObject(ObjectPtr objectPtr) { gameHandler->sendTo(*p, msg); } + + // Add the new player to the list + players.push_back(playerPtr); + + /* Since the player doesn't know yet where on the world he is after + * connecting to the map server, we send him an initial change map message. + */ + Storage &store = Storage::instance("tmw"); + MessageOut mapChangeMessage(GPMSG_PLAYER_MAP_CHANGE); + mapChangeMessage.writeString(store.getMapNameFromId(mapId)); + mapChangeMessage.writeShort(playerPtr->getX()); + mapChangeMessage.writeShort(playerPtr->getY()); + mapChangeMessage.writeByte(0); + gameHandler->sendTo(playerPtr, mapChangeMessage); } void @@ -182,11 +196,14 @@ State::informPlayer(PlayerPtr playerPtr) if (m == maps.end()) return; Players &players = m->second.players; + /* Here the player is informed about all the other players on the map. + * However, the player should only be told about other players within + * visual range. See also notes at addObject and removeObject. + */ for (Players::iterator p = players.begin(), p_end = players.end(); p != p_end; ++p) { - MessageOut msg; - msg.writeShort(GPMSG_BEING_ENTER); + MessageOut msg(GPMSG_BEING_ENTER); msg.writeByte(OBJECT_PLAYER); msg.writeLong((*p)->getID()); msg.writeString((*p)->getName()); diff --git a/src/state.h b/src/state.h index b9654f92..5a114e20 100644 --- a/src/state.h +++ b/src/state.h @@ -31,26 +31,26 @@ class Map; /** - * Combined map/entity structure + * Combined map/entity structure. */ struct MapComposite { /** - * Default constructor + * Default constructor. */ MapComposite() : map(NULL) { } /** - * Actual map + * Actual map. */ Map *map; /** - * Objects (items, players, monsters, etc) located on the map + * Objects (items, players, monsters, etc) located on the map. */ Objects objects; /** - * Players located on the map + * Players located on the map. */ Players players; }; @@ -62,7 +62,7 @@ struct MapComposite { class State { /** - * List of maps + * List of maps. */ std::map<unsigned int, MapComposite> maps; @@ -71,27 +71,27 @@ class State ~State(); /** - * Update game state (contains core server logic) + * Update game state (contains core server logic). */ void update(); /** - * Send game state to given player + * Send game state to given player. */ void informPlayer(PlayerPtr playerPtr); /** - * Load map into game world + * Load map into game world. */ bool loadMap(unsigned mapId); /** - * Add object to the map + * Add object to the map. */ void addObject(ObjectPtr objectPtr); /** - * Remove an object from the map + * Remove an object from the map. */ void removeObject(ObjectPtr objectPtr); }; |