summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-20 16:30:43 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-20 16:30:43 +0000
commitd8bbcea0d4f5164776a7a2e6c05b1b5d37289965 (patch)
tree18b64c129503f13b792bf11eecf358c47f6b937e /src
parent14931fad97e23229b6321850bae457b6bb680c0b (diff)
downloadmanaserv-d8bbcea0d4f5164776a7a2e6c05b1b5d37289965.tar.gz
manaserv-d8bbcea0d4f5164776a7a2e6c05b1b5d37289965.tar.bz2
manaserv-d8bbcea0d4f5164776a7a2e6c05b1b5d37289965.tar.xz
manaserv-d8bbcea0d4f5164776a7a2e6c05b1b5d37289965.zip
Some code format changes, logging improvals and commenting.
Diffstat (limited to 'src')
-rw-r--r--src/connectionhandler.cpp11
-rw-r--r--src/connectionhandler.h6
-rw-r--r--src/dalstorage.cpp13
-rw-r--r--src/mapmanager.cpp5
-rw-r--r--src/netcomputer.cpp3
-rw-r--r--src/state.cpp72
6 files changed, 72 insertions, 38 deletions
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp
index 2a89c00f..1c73d758 100644
--- a/src/connectionhandler.cpp
+++ b/src/connectionhandler.cpp
@@ -33,9 +33,7 @@
#include "script.h"
#endif
-/**
- * Convert a IP4 address into its string representation
- */
+
std::string
ip4ToString(unsigned int ip4addr)
{
@@ -122,9 +120,6 @@ void ConnectionHandler::process()
// representation
std::string ipaddr = ip4ToString(event.peer->address.host);
- LOG_INFO("A packet of length " << event.packet->dataLength <<
- " was received from " << ipaddr, 2);
-
NetComputer *comp = (NetComputer *)event.peer->data;
#ifdef SCRIPT_SUPPORT
@@ -143,6 +138,10 @@ void ConnectionHandler::process()
if (event.packet->dataLength >= 2) {
MessageIn msg((char *)event.packet->data,
event.packet->dataLength);
+ LOG_INFO("Received message " << msg.getId() << " ("
+ << event.packet->dataLength << " B) from "
+ << ipaddr, 2);
+
processMessage(comp, msg);
} else {
LOG_ERROR("Message too short from " << ipaddr, 0);
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index 7f3355db..d09362ef 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -34,6 +34,12 @@ class MessageOut;
class NetComputer;
/**
+ * Convert a IP4 address into its string representation
+ */
+std::string
+ip4ToString(unsigned int ip4addr);
+
+/**
* Data related to a connected client. This includes the buffer for incoming
* messages and the related socket.
*/
diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp
index 5330e42f..68f19a64 100644
--- a/src/dalstorage.cpp
+++ b/src/dalstorage.cpp
@@ -388,6 +388,8 @@ bool DALStorage::doesCharacterNameExist(const std::string& name)
const std::string
DALStorage::getMapNameFromId(const unsigned int mapId)
{
+ std::string name = "None";
+
// If not opened already
open();
@@ -399,22 +401,19 @@ DALStorage::getMapNameFromId(const unsigned int mapId)
sql << mapId;
sql << ";";
- const dal::RecordSet& mapInfo = mDb->execSql(sql.str());
+ const dal::RecordSet &mapInfo = mDb->execSql(sql.str());
// If the map return is empty then we have no choice but to return None.
- if (mapInfo.isEmpty()) {
- return "None";
+ if (!mapInfo.isEmpty()) {
+ name = mapInfo(0, 0);
}
-
- std::string strMap(mapInfo(0,0));
- return strMap;
}
catch (const dal::DbSqlQueryExecFailure& e) {
// TODO: throw an exception.
LOG_ERROR("SQL query failure: " << e.what(), 0);
}
- return "None";
+ return name;
}
std::map<short, ChatChannel>
diff --git a/src/mapmanager.cpp b/src/mapmanager.cpp
index b1a307db..169c357f 100644
--- a/src/mapmanager.cpp
+++ b/src/mapmanager.cpp
@@ -41,11 +41,12 @@ Map *MapManager::loadMap(const unsigned int mapId)
Map *map = MapReader::readMap("maps/" + mapFile);
if (map == NULL)
{
- LOG_ERROR("Error: Unable to load map file (" << mapFile << ")", 0);
+ LOG_ERROR("Unable to load map \"" << mapFile << "\" (id " << mapId
+ << ")", 0);
}
else
{
- LOG_INFO("Loaded map " << mapId << " (" << mapFile << ")", 0);
+ LOG_INFO("Loaded map \"" << mapFile << "\" (id " << mapId << ")", 0);
maps[mapId] = map;
}
return map;
diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp
index bce31788..1c3c50b6 100644
--- a/src/netcomputer.cpp
+++ b/src/netcomputer.cpp
@@ -49,7 +49,8 @@ void NetComputer::disconnect(const std::string &reason)
void NetComputer::send(const MessageOut &msg)
{
- LOG_INFO("Sending packet of length " << msg.getDataSize(), 2);
+ LOG_INFO("Sending packet of length " << msg.getDataSize() << " to "
+ << ip4ToString(mPeer->address.host), 2);
// Create a reliable packet.
ENetPacket *packet = enet_packet_create(msg.getData(), msg.getDataSize(),
diff --git a/src/state.cpp b/src/state.cpp
index 2cb46aa2..870be52a 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -30,28 +30,33 @@
#include "utils/logger.h"
-State::State() {
+State::State()
+{
}
-State::~State() {
+State::~State()
+{
for (std::map<unsigned int, MapComposite>::iterator i = maps.begin();
i != maps.end();
- i++) {
+ i++)
+ {
delete i->second.map;
}
}
-void State::update()
+void
+State::update()
{
// update game state (update AI, etc.)
for (std::map<unsigned int, MapComposite>::iterator m = maps.begin(),
- m_end = maps.end(); m != m_end; ++m) {
-
+ m_end = maps.end(); m != m_end; ++m)
+ {
typedef std::vector< utils::CountedPtr<MovingObject> > Movings;
Movings movings;
for (Objects::iterator o = m->second.objects.begin(),
- o_end = m->second.objects.end(); o != o_end; ++o) {
+ o_end = m->second.objects.end(); o != o_end; ++o)
+ {
(*o)->update();
int t = (*o)->getType();
if (t == OBJECT_NPC || t == OBJECT_PLAYER || t == OBJECT_MONSTER) {
@@ -61,17 +66,18 @@ void State::update()
}
}
-
Players &players = m->second.players;
for (Players::iterator p = players.begin(),
- p_end = players.end(); p != p_end; ++p) {
+ p_end = players.end(); p != p_end; ++p)
+ {
std::pair<unsigned, unsigned> ps = (*p)->getXY();
std::pair<unsigned, unsigned> pn = (*p)->getNextPosition();
MessageOut msg;
msg.writeShort(GPMSG_BEINGS_MOVE);
for (Movings::iterator o = movings.begin(),
- o_end = movings.end(); o != o_end; ++o) {
+ o_end = movings.end(); o != o_end; ++o)
+ {
std::pair<unsigned, unsigned> os = (*o)->getXY();
std::pair<unsigned, unsigned> on = (*o)->getNextPosition();
@@ -94,14 +100,17 @@ void State::update()
}
for (Movings::iterator o = movings.begin(),
- o_end = movings.end(); o != o_end; ++o) {
+ o_end = movings.end(); o != o_end; ++o)
+ {
std::pair<unsigned, unsigned> pos = (*o)->getNextPosition();
(*o)->setXY(pos.first, pos.second);
}
}
}
-void State::addObject(ObjectPtr objectPtr) {
+void
+State::addObject(ObjectPtr objectPtr)
+{
unsigned mapId = objectPtr->getMapId();
if (!loadMap(mapId)) return;
maps[mapId].objects.push_back(objectPtr);
@@ -109,21 +118,29 @@ void State::addObject(ObjectPtr objectPtr) {
PlayerPtr playerPtr(objectPtr);
Players &players = maps[mapId].players;
players.push_back(playerPtr);
- MessageOut msg;
- msg.writeShort(GPMSG_BEING_ENTER);
+
+ /* Currently when a player is added, all existing players are notified
+ * about this. This will need to be modified so that players only know
+ * about players close to them.
+ */
+ MessageOut msg(GPMSG_BEING_ENTER);
msg.writeByte(OBJECT_PLAYER);
msg.writeLong(playerPtr->getID());
msg.writeString(playerPtr->getName());
msg.writeByte(playerPtr->getHairStyle());
msg.writeByte(playerPtr->getHairColor());
msg.writeByte(playerPtr->getGender());
+
for (Players::iterator p = players.begin(),
- p_end = players.end(); p != p_end; ++p) {
+ p_end = players.end(); p != p_end; ++p)
+ {
gameHandler->sendTo(*p, msg);
}
}
-void State::removeObject(ObjectPtr objectPtr) {
+void
+State::removeObject(ObjectPtr objectPtr)
+{
unsigned mapId = objectPtr->getMapId();
std::map<unsigned, MapComposite>::iterator m = maps.find(mapId);
if (m == maps.end()) return;
@@ -138,12 +155,17 @@ void State::removeObject(ObjectPtr objectPtr) {
if (objectPtr->getType() != OBJECT_PLAYER) return;
PlayerPtr playerPtr(objectPtr);
Players &players = maps[mapId].players;
- MessageOut msg;
- msg.writeShort(GPMSG_BEING_LEAVE);
+
+ /* Also see note add addObject. All other players are notified about this
+ * player leaving, but not all of them need to know.
+ */
+ MessageOut msg(GPMSG_BEING_LEAVE);
msg.writeByte(OBJECT_PLAYER);
msg.writeLong(playerPtr->getID());
+
Players::iterator p_end = players.end(), j = p_end;
- for (Players::iterator p = players.begin(); p != p_end; ++p) {
+ for (Players::iterator p = players.begin(); p != p_end; ++p)
+ {
if (p->get() == playerPtr.get())
j = p;
else
@@ -152,13 +174,17 @@ void State::removeObject(ObjectPtr objectPtr) {
if (j != players.end()) players.erase(j);
}
-void State::informPlayer(PlayerPtr playerPtr) {
+void
+State::informPlayer(PlayerPtr playerPtr)
+{
unsigned mapId = playerPtr->getMapId();
std::map<unsigned, MapComposite>::iterator m = maps.find(mapId);
if (m == maps.end()) return;
Players &players = m->second.players;
+
for (Players::iterator p = players.begin(),
- p_end = players.end(); p != p_end; ++p) {
+ p_end = players.end(); p != p_end; ++p)
+ {
MessageOut msg;
msg.writeShort(GPMSG_BEING_ENTER);
msg.writeByte(OBJECT_PLAYER);
@@ -171,7 +197,9 @@ void State::informPlayer(PlayerPtr playerPtr) {
}
}
-bool State::loadMap(const unsigned int mapId) {
+bool
+State::loadMap(const unsigned int mapId)
+{
if (maps.find(mapId) != maps.end()) return true;
Map *tmp = MapManager::instance().loadMap(mapId);
if (!tmp) return false;