diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/accounthandler.cpp | 7 | ||||
-rw-r--r-- | src/dalstorage.cpp | 5 | ||||
-rw-r--r-- | src/gamehandler.cpp | 7 | ||||
-rw-r--r-- | src/map.cpp | 8 | ||||
-rw-r--r-- | src/map.h | 2 | ||||
-rw-r--r-- | src/object.h | 18 | ||||
-rw-r--r-- | src/point.h | 56 | ||||
-rw-r--r-- | src/state.cpp | 42 |
10 files changed, 104 insertions, 51 deletions
@@ -1,3 +1,10 @@ +2006-08-27 Bjørn Lindeijer <bjorn@lindeijer.nl> + + * src/map.cpp, src/accounthandler.cpp, src/point.h, + src/dalstorage.cpp, src/object.h, src/state.cpp, src/gamehandler.cpp, + src/map.h, src/Makefile.am: Introduced Point class to replace the + confusing and clumsy usage of std::pair. + 2006-08-27 Guillaume Melquiond <guillaume.melquiond@gmail.com> * src/object.cpp: Fixed assertion failure on being movements. @@ -9,7 +16,7 @@ 2006-08-26 Rogier Polak <rogier_polak@users.sourceforge.net> * src/netcomputer.h, src/netcomputer.cpp, src/connectionhandler.cpp: - Added private overloaded << operator to NetComputer, for logging of the + Added the stream operator << to NetComputer, for logging of the peers ip-address (patch applied by Bjørn Lindeijer). 2006-08-26 Guillaume Melquiond <guillaume.melquiond@gmail.com> diff --git a/src/Makefile.am b/src/Makefile.am index ffdf3f8a..d543290b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -50,6 +50,7 @@ tmwserv_SOURCES = main.cpp \ messageout.cpp \ netcomputer.h \ netcomputer.cpp \ + point.h \ skill.h \ skill.cpp \ resourcemanager.cpp \ diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 4c02d4c4..9e35505c 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -33,6 +33,7 @@ #include "messagein.h" #include "messageout.h" #include "netcomputer.h" +#include "point.h" #include "storage.h" #include "utils/logger.h" @@ -662,9 +663,9 @@ AccountHandler::handleCharacterCreateMessage(AccountClient &computer, newCharacter->setHairStyle(hairStyle); newCharacter->setHairColor(hairColor); newCharacter->setMapId((int) config.getValue("defaultMap", 1)); - newCharacter->setXY( - (int) config.getValue("startX", 0), - (int) config.getValue("startY", 0)); + newCharacter->setXY(Point( + (int) config.getValue("startX", 0), + (int) config.getValue("startY", 0))); computer.getAccount()->addCharacter(newCharacter); LOG_INFO("Character " << name << " was created for " diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 68f19a64..5fabfce6 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -26,6 +26,7 @@ #include "configuration.h" #include "dalstoragesql.h" +#include "point.h" #include "dal/dalexcept.h" #include "dal/dataproviderfactory.h" @@ -260,8 +261,8 @@ DALStorage::getAccount(const std::string& userName) player->setHairColor(toUshort(strCharInfo[k][5])); player->setLevel(toUshort(strCharInfo[k][6])); player->setMoney(toUint(strCharInfo[k][7])); - player->setXY(toUshort(strCharInfo[k][8]), - toUshort(strCharInfo[k][9])); + player->setXY(Point(toUshort(strCharInfo[k][8]), + toUshort(strCharInfo[k][9]))); for (int i = 0; i < NB_RSTAT; ++i) player->setRawStat(i, toUshort(strCharInfo[k][11 + i])); diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 6305f410..eeb49478 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -231,7 +231,7 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) msg.writeString(text); unsigned speakerMapId = beingPtr->getMapId(); - std::pair<unsigned, unsigned> speakerXY = beingPtr->getXY(); + Point speakerPosition = beingPtr->getXY(); for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); i != i_end; ++i) @@ -244,10 +244,7 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) continue; } - std::pair<unsigned, unsigned> listenerXY = listener->getXY(); - - if (areAround(listenerXY.first, listenerXY.second, - speakerXY.first, speakerXY.second)) + if (speakerPosition.inRangeOf(listener->getXY())) { (*i)->send(msg); } diff --git a/src/map.cpp b/src/map.cpp index 8a768e07..6487b700 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -41,7 +41,7 @@ Location::Location(int x, int y, MetaTile *tile): bool Location::operator< (const Location &loc) const { - return tile->Fcost > loc.tile->Fcost; + return tile->Fcost > loc.tile->Fcost; } @@ -281,9 +281,3 @@ Map::findPath(int startX, int startY, int destX, int destY) return path; } - -bool areAround(unsigned x1, unsigned y1, unsigned x2, unsigned y2) -{ - return (abs(x1 - x2) <= (int)AROUND_AREA && - abs(y1 - y2) <= (int)AROUND_AREA); -} @@ -174,6 +174,4 @@ class Map : public Properties int onClosedList, onOpenList; }; -bool areAround(unsigned x1, unsigned y1, unsigned x2, unsigned y2); - #endif diff --git a/src/object.h b/src/object.h index ecd06985..9f3071a8 100644 --- a/src/object.h +++ b/src/object.h @@ -24,10 +24,10 @@ #ifndef _TMWSERV_OBJECT_H_ #define _TMWSERV_OBJECT_H_ -#include <utility> #include <vector> #include "defines.h" +#include "point.h" #include "utils/countedptr.h" /** @@ -112,16 +112,16 @@ class Object * @param x the x coordinate. * @param y the y coordinate. */ - void setXY(unsigned int x, unsigned int y) - { mX = x; mY = y; } + void setXY(const Point &p) + { mX = p.x; mY = p.y; } /** * Get the coordinates. * * @return the coordinates as a pair. */ - std::pair<unsigned int, unsigned int> getXY() const - { return std::make_pair(mX, mY); } + Point getXY() const + { return Point(mX, mY); } /** * Update the internal status. @@ -183,8 +183,8 @@ class MovingObject: public Object /** * Gets the destination coordinates of the object. */ - std::pair<unsigned, unsigned> getDestination() const - { return std::make_pair(mDstX, mDstY); } + Point getDestination() const + { return Point(mDstX, mDstY); } /** * Sets the destination coordinates of the object. @@ -195,8 +195,8 @@ class MovingObject: public Object /** * Gets the next coordinates of the object. */ - std::pair<unsigned, unsigned> getNextPosition() const - { return std::make_pair(mNewX, mNewY); } + Point getNextPosition() const + { return Point(mNewX, mNewY); } /** * Sets object speed. diff --git a/src/point.h b/src/point.h new file mode 100644 index 00000000..2d6b337d --- /dev/null +++ b/src/point.h @@ -0,0 +1,56 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMWSERV_POINT_H_ +#define _TMWSERV_POINT_H_ + +#include "defines.h" + +/** + * A point in positive space. Usually representing pixel coordinates on a map. + */ +class Point +{ + public: + unsigned int x; /**< x coordinate */ + unsigned int y; /**< y coordinate */ + + /** + * Constructor. + */ + Point(unsigned int x, unsigned int y) + : x(x), + y(y) + {} + + /** + * Check whether the given point is range of this point. This is + * defined as lying within the distance of client awareness. + */ + bool inRangeOf(const Point &p) const + { + return (abs(x - p.x) <= (int) AROUND_AREA && + abs(y - p.y) <= (int) AROUND_AREA); + } +}; + +#endif // _TMWSERV_POINT_H_ diff --git a/src/state.cpp b/src/state.cpp index 0d4fb28d..dc21e766 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -29,6 +29,7 @@ #include "map.h" #include "mapmanager.h" #include "messageout.h" +#include "point.h" #include "storage.h" #include "utils/logger.h" @@ -73,27 +74,27 @@ State::update() for (Players::iterator p = players.begin(), p_end = players.end(); p != p_end; ++p) { - std::pair<unsigned, unsigned> ps = (*p)->getXY(); - std::pair<unsigned, unsigned> pn = (*p)->getNextPosition(); + Point ps = (*p)->getXY(); + Point pn = (*p)->getNextPosition(); bool po = !(*p)->isNew(); // Is p old? MessageOut msg(GPMSG_BEINGS_MOVE); for (Movings::iterator o = movings.begin(), o_end = movings.end(); o != o_end; ++o) { - std::pair<unsigned, unsigned> os = (*o)->getXY(); - std::pair<unsigned, unsigned> on = (*o)->getNextPosition(); + Point os = (*o)->getXY(); + Point on = (*o)->getNextPosition(); bool oo = po && !(*o)->isNew(); // Are p and o both old? /* Look whether p and o "were" around the last time and whether they "will" be around the next time. */ - bool were = areAround(ps.first, ps.second, os.first, os.second) && oo; - bool will = areAround(pn.first, pn.second, on.first, on.second); + bool wereInRange = ps.inRangeOf(os) && oo; + bool willBeInRange = pn.inRangeOf(on); - if (!were) + if (!wereInRange) { // o was outside p's range. - if (!will) + if (!willBeInRange) { // Nothing to report: o will not be inside p's range. continue; @@ -117,7 +118,7 @@ State::update() } gameHandler->sendTo(*p, msg2); } - else if (!will) + else if (!willBeInRange) { // o is no longer visible from p. MessageOut msg2(GPMSG_BEING_LEAVE); @@ -126,7 +127,7 @@ State::update() gameHandler->sendTo(*p, msg2); continue; } - else if (os.first == on.first && os.second == on.second) + else if (os.x == on.x && os.y == on.y) { // o does not move, nothing to report. continue; @@ -134,12 +135,12 @@ State::update() /* At this point, either o has entered p's range, either o is moving inside p's range. Report o's movements. */ - std::pair<unsigned, unsigned> od = (*o)->getDestination(); + Point od = (*o)->getDestination(); msg.writeLong((*o)->getID()); - msg.writeShort(on.first); - msg.writeShort(on.second); - msg.writeShort(od.first); - msg.writeShort(od.second); + msg.writeShort(on.x); + msg.writeShort(on.y); + msg.writeShort(od.x); + msg.writeShort(od.y); } // Don't send a packet if nothing happed in p's range. @@ -150,8 +151,7 @@ State::update() for (Movings::iterator o = movings.begin(), o_end = movings.end(); o != o_end; ++o) { - std::pair<unsigned, unsigned> pos = (*o)->getNextPosition(); - (*o)->setXY(pos.first, pos.second); + (*o)->setXY((*o)->getNextPosition()); (*o)->setNew(false); } } @@ -184,21 +184,19 @@ State::removeObject(ObjectPtr objectPtr) } if (objectPtr->getType() != OBJECT_PLAYER) return; - PlayerPtr playerPtr(objectPtr); - std::pair< unsigned, unsigned > pos = playerPtr->getXY(); MessageOut msg(GPMSG_BEING_LEAVE); msg.writeByte(OBJECT_PLAYER); - msg.writeLong(playerPtr->getID()); + msg.writeLong(objectPtr->getID()); Players &players = maps[mapId].players; Players::iterator p_end = players.end(), j = p_end; for (Players::iterator p = players.begin(); p != p_end; ++p) { - if (p->get() == playerPtr.get()) + if (p->get() == objectPtr.get()) { j = p; } - else if (areAround(pos.first, pos.second, (*p)->getX(), (*p)->getY())) + else if (objectPtr->getXY().inRangeOf((*p)->getXY())) { gameHandler->sendTo(*p, msg); } |