summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAaron Marks <nymacro@gmail.com>2005-07-31 05:26:08 +0000
committerAaron Marks <nymacro@gmail.com>2005-07-31 05:26:08 +0000
commit18b778662388e98e90b13f28f28a83911ee96e95 (patch)
tree5e16ba5adc9cdc3d4f84769e0a4e93ff7155c7a0 /src
parentd593cb2395ddc144844297ea12122c12e60d3bba (diff)
downloadmanaserv-18b778662388e98e90b13f28f28a83911ee96e95.tar.gz
manaserv-18b778662388e98e90b13f28f28a83911ee96e95.tar.bz2
manaserv-18b778662388e98e90b13f28f28a83911ee96e95.tar.xz
manaserv-18b778662388e98e90b13f28f28a83911ee96e95.zip
Added beginnings of game core logic and state.
Fixed a few problems with SQL queries using different SQL DB backends. I forget everything else.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/accounthandler.cpp6
-rw-r--r--src/chathandler.cpp1
-rw-r--r--src/connectionhandler.cpp12
-rw-r--r--src/connectionhandler.h10
-rw-r--r--src/dalstorage.cpp16
-rw-r--r--src/defines.h7
-rw-r--r--src/items.h2
-rw-r--r--src/main.cpp18
-rw-r--r--src/state.cpp64
-rw-r--r--src/state.h71
11 files changed, 190 insertions, 19 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 8c479b40..e7c893cb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,6 +14,8 @@ tmwserv_SOURCES = main.cpp \
connectionhandler.cpp \
gamehandler.h \
gamehandler.cpp \
+ state.h \
+ state.cpp \
debug.h \
debug.cpp \
defines.h \
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index 59956242..2934afde 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -132,6 +132,8 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
result.writeShort(SMSG_CHAR_CREATE_RESPONSE);
result.writeByte(CREATE_OK);
+
+ store.flush(); // flush changes
}
break;
@@ -140,7 +142,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
if (computer.getAccount() == NULL)
break; // not logged in
- char charNum = message.readByte();
+ unsigned char charNum = message.readByte();
tmwserv::Beings &chars = computer.getAccount()->getCharacters();
@@ -150,6 +152,8 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
result.writeByte(SELECT_INVALID);
break;
}
+
+ computer.setCharacter(chars[charNum].get());
result.writeByte(SELECT_OK);
}
diff --git a/src/chathandler.cpp b/src/chathandler.cpp
index bd48a3d5..9884861d 100644
--- a/src/chathandler.cpp
+++ b/src/chathandler.cpp
@@ -34,6 +34,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
case CMSG_SAY:
{
+ // chat to people around area
std::string text = message.readString();
short channel = message.readShort();
std::cout << "Say (" << channel << "): " << text << std::endl;
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp
index 2583e3fc..9a58b882 100644
--- a/src/connectionhandler.cpp
+++ b/src/connectionhandler.cpp
@@ -247,3 +247,15 @@ void ConnectionHandler::registerHandler(
{
handlers[msgId] = handler;
}
+
+void ConnectionHandler::sendTo(tmwserv::Being *being, MessageOut &msg)
+{
+ for (NetComputers::iterator i = clients.begin();
+ i != clients.end();
+ i++) {
+ if ((*i)->getCharacter() == being) {
+ (*i)->send(msg.getPacket());
+ break;
+ }
+ }
+}
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index 92f7658d..bafdcdd6 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -27,6 +27,8 @@
#include "messagehandler.h"
#include "netcomputer.h"
#include "packet.h"
+#include "being.h"
+#include "messageout.h"
#include <map>
#include <SDL_net.h>
@@ -59,6 +61,8 @@ class ClientData
class ConnectionHandler
{
public:
+ typedef std::list<NetComputer*> NetComputers;
+
/**
* Constructor.
*/
@@ -99,10 +103,14 @@ class ConnectionHandler
*/
void registerHandler(unsigned int msgId, MessageHandler *handler);
+ /**
+ * Send packet to client with matching Being*
+ */
+ void sendTo(tmwserv::Being*, MessageOut &);
+
private:
std::map<unsigned int, MessageHandler*> handlers;
- typedef std::list<NetComputer*> NetComputers;
NetComputers clients;
};
diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp
index 938b06a9..1bc71dab 100644
--- a/src/dalstorage.cpp
+++ b/src/dalstorage.cpp
@@ -446,7 +446,7 @@ DALStorage::_addAccount(const AccountPtr& account)
RawStatistics& stats = (*it)->getRawStatistics();
std::ostringstream sql3;
sql3 << "insert into " << CHARACTERS_TBL_NAME
- << " (name, gender, level, money, x, y, map_id, str, agi, int, dex luck)"
+ << " (name, gender, level, money, x, y, map_id, str, agi, vit, int, dex luck)"
<< " values ("
<< (account_it->second).id << ", '"
<< (*it)->getName() << "', '"
@@ -531,10 +531,18 @@ DALStorage::_updAccount(const AccountPtr& account)
std::ostringstream sql3;
if (charInfo.rows() == 0) {
- sql3 << "inpsert into " << CHARACTERS_TBL_NAME
- << " (name, gender, level, money, x, y, map_id, str, agi, vit, int, dex, luck)"
+ sql3 << "insert into " << CHARACTERS_TBL_NAME
+ << " ("
+#ifdef SQLITE_SUPPORT
+ << "user_id, "
+#endif
+ << "name, gender, level, money, x, y, map_id, str, agi, vit, int, dex, luck)"
<< " values ("
+#ifdef SQLITE_SUPPORT
<< (account_it->second).id << ", '"
+#else
+ << "'"
+#endif
<< (*it)->getName() << "', "
<< (*it)->getGender() << ", "
<< (*it)->getLevel() << ", "
@@ -561,7 +569,7 @@ DALStorage::_updAccount(const AccountPtr& account)
<< " str = " << stats.strength << ", "
<< " agi = " << stats.agility << ", "
<< " vit = " << stats.vitality << ", "
-#ifdef MYSQL_SUPPORT
+#if defined(MYSQL_SUPPORT) || defined(POSTGRESQL_SUPPORT)
<< " `int` = " << stats.intelligence << ", "
#else
<< " int = " << stats.intelligence << ", "
diff --git a/src/defines.h b/src/defines.h
index 12d7e0a6..571a96be 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -144,5 +144,12 @@ enum {
SELECT_INVALID
};
+// Object type enumeration
+enum {
+ OBJECT_ITEM = 0,
+ OBJECT_PLAYER,
+ OBJECT_MONSTER
+};
+
#endif // _TMWSERV_DEFINES_H_
diff --git a/src/items.h b/src/items.h
index a3daddc2..c50da65e 100644
--- a/src/items.h
+++ b/src/items.h
@@ -47,7 +47,7 @@ class Item : public Object
Equipment
};
- virtual ~Item() throw () { }
+ virtual ~Item() throw() { }
/**
* The function called to use an item
diff --git a/src/main.cpp b/src/main.cpp
index ff939e12..30b547f7 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -35,6 +35,7 @@
#include "chathandler.h"
#include "storage.h"
#include "configuration.h"
+#include "state.h"
#include "skill.h"
@@ -178,17 +179,6 @@ void deinitialize()
/**
- * Update game world
- */
-void updateWorld()
-{
-#ifdef SCRIPT_SUPPORT
- script->update();
-#endif
-}
-
-
-/**
* Main function, initializes and runs server.
*/
int main(int argc, char *argv[])
@@ -238,6 +228,10 @@ int main(int argc, char *argv[])
store.open();
//
+ // create state machine
+ State &state = State::instance();
+ //
+
SDL_Event event;
while (running) {
@@ -253,7 +247,7 @@ int main(int argc, char *argv[])
// - Handle all messages that are in the message queue
// - Update all active objects/beings
- updateWorld();
+ state.update(*connectionHandler.get());
}
else if (event.type == SDL_QUIT) {
running = false;
diff --git a/src/state.cpp b/src/state.cpp
new file mode 100644
index 00000000..86c7e1aa
--- /dev/null
+++ b/src/state.cpp
@@ -0,0 +1,64 @@
+/*
+ * 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$
+ */
+
+#include "state.h"
+#include <iostream>
+#include "messageout.h"
+
+namespace tmwserv
+{
+
+void State::update(ConnectionHandler &connectionHandler)
+{
+ // update game state (update AI, etc.)
+
+ // notify clients about changes in the game world
+ // NOTE: This isn't finished ;)
+ for (std::map<std::string, Beings>::iterator i = beings.begin();
+ i != beings.end();
+ i++) {
+ //
+ for (std::vector<BeingPtr>::iterator b = i->second.begin();
+ b != i->second.end();
+ b++) {
+ // send info about other players
+ for (std::vector<BeingPtr>::iterator b2 = i->second.begin();
+ b2 != i->second.end();
+ b2++) {
+ if (b != b2) {
+ MessageOut msg;
+ msg.writeShort(SMSG_NEW_OBJECT);
+ msg.writeLong(OBJECT_PLAYER); // type
+ msg.writeLong((int)b2->get()); // id
+ msg.writeLong(0); // x
+ msg.writeLong(0); // y
+
+ connectionHandler.sendTo(b->get(), msg);
+ }
+ }
+ }
+ }
+
+}
+
+} // namespace tmwserv
diff --git a/src/state.h b/src/state.h
new file mode 100644
index 00000000..31142d11
--- /dev/null
+++ b/src/state.h
@@ -0,0 +1,71 @@
+/*
+ * 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 _TMW_SERVER_STATE_
+#define _TMW_SERVER_STATE_
+
+#include <string>
+#include <map>
+#include "connectionhandler.h"
+#include "being.h"
+#include "map.h"
+#include "utils/singleton.h"
+
+namespace tmwserv
+{
+
+/**
+ * State class contains all information/procedures associated with the game
+ * state.
+ */
+class State : public utils::Singleton<State>
+{
+ friend class utils::Singleton<State>;
+
+ State() throw() { }
+ ~State() throw() { }
+
+ public:
+ /**
+ * The key/value pair conforms to:
+ * First - map name
+ * Second - list of beings/players on the map
+ *
+ * NOTE: This could possibly be optimized by making first Being & second string. This will make many operations easier.
+ */
+ std::map<std::string, Beings> beings;
+
+ /**
+ * Container for loaded maps.
+ */
+ std::map<std::string, Map*> maps;
+
+ /**
+ * Update game state (contains core server logic)
+ */
+ void update(ConnectionHandler &);
+};
+
+} // namespace tmwserv
+
+#endif