From e7a3a5bb9df0609a76323b8e2546a7a02fcebc81 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 27 Aug 2006 22:55:37 +0000 Subject: Added a Controller class meant to implement behaviour loosely coupled to the actual being. Used it to control 10 testing maggots that are now randomly walking around. --- src/Makefile.am | 3 +++ src/being.cpp | 33 ++++++++++++++++++++++++ src/being.h | 18 +++++++++++++ src/controller.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/controller.h | 61 ++++++++++++++++++++++++++++++++++++++++++++ src/object.cpp | 4 +-- src/object.h | 5 ++++ src/state.cpp | 21 ++++++++++++++-- 8 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 src/being.cpp create mode 100644 src/controller.cpp create mode 100644 src/controller.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index b981b40f..b752b0c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,7 @@ tmwclient_SOURCES = client.cpp \ tmwserv_SOURCES = main.cpp \ being.h \ + being.cpp \ account.h \ account.cpp \ accountclient.h \ @@ -29,6 +30,8 @@ tmwserv_SOURCES = main.cpp \ configuration.cpp \ connectionhandler.h \ connectionhandler.cpp \ + controller.h \ + controller.cpp \ debug.h \ debug.cpp \ defines.h \ diff --git a/src/being.cpp b/src/being.cpp new file mode 100644 index 00000000..512d682b --- /dev/null +++ b/src/being.cpp @@ -0,0 +1,33 @@ +/* + * 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 "being.h" + +#include "controller.h" + +#include "utils/logger.h" + +void Being::update() +{ + if (mController) + mController->update(); +} diff --git a/src/being.h b/src/being.h index 6ca8f293..2782a265 100644 --- a/src/being.h +++ b/src/being.h @@ -30,6 +30,8 @@ #include "object.h" #include "utils/countedptr.h" +class Controller; + /** * Raw statistics of a Player. */ @@ -104,11 +106,27 @@ class Being : public MovingObject unsigned short getStat(int numStat) { return mStats.stats[numStat]; } + /** + * When a controller is set, updates the controller. + */ + void + update(); + + /** + * Notification that this being is now possessed by the given + * controller. This means that events regarding what happens to this + * being should be send there. + */ + void + possessedBy(Controller *controller) + { mController = controller; } + private: Being(Being const &rhs); Being &operator=(Being const &rhs); Statistics mStats; /**< stats modifiers or computed stats */ + Controller *mController; }; /** diff --git a/src/controller.cpp b/src/controller.cpp new file mode 100644 index 00000000..17f15017 --- /dev/null +++ b/src/controller.cpp @@ -0,0 +1,74 @@ +/* + * 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 "controller.h" + +#include "utils/logger.h" + +Controller::Controller(): + mCountDown(0) +{ +} + +void Controller::possess(BeingPtr being) +{ + unPossess(); + + mBeing = being; + + if (mBeing.get()) + mBeing->possessedBy(this); +} + +void Controller::unPossess() +{ + if (mBeing.get()) + mBeing->possessedBy(NULL); + + mBeing = BeingPtr(); +} + +void Controller::update() +{ + /* Temporary "AI" behaviour that is purely artificial and not at all + * intelligent. + */ + if (mCountDown == 0) + { + if (mBeing.get()) + { + unsigned int randomX = rand() % 320 + 720; + unsigned int randomY = rand() % 320 + 840; + + LOG_INFO("Setting new random destination " << randomX << "," + << randomY << " for being " << mBeing->getID(), 2); + mBeing->setDestination(randomX, randomY); + } + + mCountDown = 10 + rand() % 10; + } + else + { + mCountDown--; + } +} + diff --git a/src/controller.h b/src/controller.h new file mode 100644 index 00000000..43d1d3f4 --- /dev/null +++ b/src/controller.h @@ -0,0 +1,61 @@ +/* + * 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_CONTROLLER_H_ +#define _TMWSERV_CONTROLLER_H_ + +#include "being.h" + +/** + * A controller can take control of a being. + */ +class Controller +{ + public: + /** + * Constructor. + */ + Controller(); + + /** + * Take possession of the given being. + */ + void possess(BeingPtr being); + + /** + * Releave possession of any being. + */ + void unPossess(); + + /** + * Performs one step of controller logic. + */ + void update(); + + private: + BeingPtr mBeing; + + /** Count down till next random movement (temporary). */ + unsigned int mCountDown; +}; + +#endif // _TMWSERV_CONTROLLER_H_ diff --git a/src/object.cpp b/src/object.cpp index b20fa7dc..ddbd10c7 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -67,8 +67,8 @@ void MovingObject::move() int vecX = 0, vecY = 0, cost = 0; for (std::list::const_iterator it = path.begin(), - it_end = path.end(); it != it_end; ++it) { - + it_end = path.end(); it != it_end; ++it) + { int tileNX = it->x, tileNY = it->y; assert((tileNX != tileCX || tileNY != tileCY) && (tileNX != tileDX || tileNY != tileDY)); diff --git a/src/object.h b/src/object.h index 9f3071a8..793c3a66 100644 --- a/src/object.h +++ b/src/object.h @@ -222,6 +222,11 @@ class MovingObject: public Object */ typedef utils::CountedPtr ObjectPtr; +/** + * Type definition for a smart pointer to MovingObject. + */ +typedef utils::CountedPtr MovingObjectPtr; + /** * Type definition for a list of Objects. diff --git a/src/state.cpp b/src/state.cpp index fb5ff7f5..71c9fad0 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -25,6 +25,7 @@ #include "state.h" +#include "controller.h" #include "gamehandler.h" #include "map.h" #include "mapmanager.h" @@ -36,6 +37,18 @@ State::State() { + // Create 10 maggots for testing purposes + for (int i = 0; i < 10; i++) + { + // TODO: Unique object numbering + BeingPtr being = BeingPtr(new Being(OBJECT_MONSTER, 1000 + i)); + being->setSpeed(21); + being->setMapId(1); + being->setXY(Point(720, 900)); + Controller *controller = new Controller(); + controller->possess(being); + addObject(ObjectPtr(being)); + } } State::~State() @@ -55,7 +68,7 @@ State::update() for (std::map::iterator m = maps.begin(), m_end = maps.end(); m != m_end; ++m) { - typedef std::vector< utils::CountedPtr > Movings; + typedef std::vector Movings; Movings movings; for (Objects::iterator o = m->second.objects.begin(), @@ -64,7 +77,7 @@ State::update() (*o)->update(); int t = (*o)->getType(); if (t == OBJECT_NPC || t == OBJECT_PLAYER || t == OBJECT_MONSTER) { - utils::CountedPtr ptr(*o); + MovingObjectPtr ptr(*o); ptr->move(); movings.push_back(ptr); } @@ -111,6 +124,10 @@ State::update() msg2.writeByte(q->getHairColor()); msg2.writeByte(q->getGender()); } break; + case OBJECT_MONSTER: + { + msg2.writeShort(0); // TODO: The monster ID + } break; default: assert(false); // TODO } -- cgit v1.2.3-60-g2f50