diff --git a/server/creatures/creature_data.cpp b/server/creatures/creature_data.cpp index 8f86fdb..58fc81a 100644 --- a/server/creatures/creature_data.cpp +++ b/server/creatures/creature_data.cpp @@ -21,6 +21,9 @@ */ #include "creature_data.hpp" +#include +#include + CreatureData::CreatureData(std::string _avatar, int _scriptRef): Entity("creature"), avatar(_avatar), @@ -29,9 +32,29 @@ CreatureData::CreatureData(std::string _avatar, int _scriptRef): //EMPTY } -void CreatureData::Update() { - Entity::Update(); - //TODO: (0) call the script reference +int CreatureData::Update(lua_State* L) { + int ret = 0; + + if (scriptRef != 0) { + //Call the script reference + lua_pushinteger(L, scriptRef); + lua_gettable(L, LUA_REGISTRYINDEX); + lua_pushlightuserdata(L, reinterpret_cast(this)); + + //check for errors + if(lua_pcall(L, 1, 1, 0) != LUA_OK) { + std::ostringstream msg; + msg << "Error running creature script: " << lua_tostring(L, -1); + lua_pop(L, 1); + throw(std::runtime_error(msg.str())); + } + + ret += lua_tonumber(L, -1); + } + + ret += Entity::Update(); + + return ret; } //------------------------- diff --git a/server/creatures/creature_data.hpp b/server/creatures/creature_data.hpp index 5f841a9..cf4efae 100644 --- a/server/creatures/creature_data.hpp +++ b/server/creatures/creature_data.hpp @@ -39,7 +39,7 @@ public: CreatureData(std::string avatar, int scriptRef); ~CreatureData() = default; - virtual void Update(); + virtual int Update(lua_State*); //accessors & mutators diff --git a/server/creatures/creature_manager.cpp b/server/creatures/creature_manager.cpp index 4b207e8..78fdb37 100644 --- a/server/creatures/creature_manager.cpp +++ b/server/creatures/creature_manager.cpp @@ -29,9 +29,14 @@ CreatureManager::~CreatureManager() { UnloadAll(); } -void CreatureManager::Update() { +//arg: a list of creatures to be updated in the clients +int CreatureManager::Update(std::list* creatureList) { + int ret; for (auto& it : elementMap) { - it.second.Update(); + ret = it.second.Update(lua); + if (ret) { + creatureList->push_back(&it.second); + } } } diff --git a/server/creatures/creature_manager.hpp b/server/creatures/creature_manager.hpp index 7a3f1eb..39a8d29 100644 --- a/server/creatures/creature_manager.hpp +++ b/server/creatures/creature_manager.hpp @@ -27,6 +27,7 @@ #include "sqlite3.h" #include +#include #include #include @@ -36,7 +37,7 @@ public: ~CreatureManager(); //common public methods - void Update(); + int Update(std::list* creatureList); int Create(std::string avatar, int scriptRef); void Unload(int uid); diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index 86ae141..b55d85c 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -25,8 +25,10 @@ Entity::Entity(const char* _type): type(_type) { //EMPTY } -void Entity::Update() { +int Entity::Update() { origin += motion; + + return motion != 0; } int Entity::SetRoomIndex(int i) { diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index cdce31e..f5fca84 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -29,7 +29,7 @@ //The base class for all objects in the world class Entity { public: - virtual void Update(); + virtual int Update(); //accessors & mutators int SetRoomIndex(int i); diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index aef217e..f3c1b0b 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -43,20 +43,17 @@ void RoomData::RunFrame() { } //update the entities in the room - creatureMgr.Update(); for (auto& it : characterList) { it->Update(); } - //TODO: (3) trigger script for monsters - //build a list of game entities + //build a list of characters for use with the triggers std::stack entityStack; for (auto& it : characterList) { entityStack.push(it); } - //TODO: (3) push the monster entities - //compare the triggers to the entities, using their real hitboxes + //Compare the triggers to the entities, using their real hitboxes //NOTE: this stack solution should prevent problems when modifying the various lists while(entityStack.size()) { //get the entity & hitbox @@ -101,6 +98,12 @@ void RoomData::RunFrame() { entityStack.pop(); } + //a list of creatures that need to be updated client-side + std::list creatureList; + creatureMgr.Update(&creatureList); + + //TODO: (0) send the updates + //TODO: creature/character collisions } @@ -138,6 +141,7 @@ TriggerManager* RoomData::GetTriggerMgr() { lua_State* RoomData::SetLuaState(lua_State* L) { lua = L; + creatureMgr.SetLuaState(lua); pager.SetLuaState(lua); triggerMgr.SetLuaState(lua); return lua;