diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index fdb953bd4..8e6919949 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -45,6 +45,7 @@ set(SERVER stacktrace.cpp Window.cpp Weather.cpp + Worldstate.cpp BaseMgr.cpp Script/CommandController.cpp Script/EventController.cpp Script/LuaState.cpp Script/luaUtils.cpp ) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index e771fdc64..00a6bd875 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -388,6 +388,11 @@ BaseWorldstate *Networking::getLastWorldstate() return &baseWorldstate; } +Worldstate *Networking::getServerWorldstate() +{ + return &serverWorldstate; +} + int Networking::getCurrentMpNum() { return currentMpNum; @@ -547,8 +552,12 @@ int Networking::mainLoop() } } timerCtrl.tick(); + if (updated) + { Players::processUpdated(); + serverWorldstate.update(); + } const int limit = 60; diff --git a/apps/openmw-mp/Networking.hpp b/apps/openmw-mp/Networking.hpp index 89bc7f8a0..9747e59e0 100644 --- a/apps/openmw-mp/Networking.hpp +++ b/apps/openmw-mp/Networking.hpp @@ -7,6 +7,7 @@ #include #include #include +#include class MasterClient; namespace mwmp @@ -57,6 +58,7 @@ namespace mwmp BaseActorList *getLastActorList(); BaseObjectList *getLastObjectList(); BaseWorldstate *getLastWorldstate(); + Worldstate *getServerWorldstate(); int getCurrentMpNum(); void setCurrentMpNum(int value); @@ -93,6 +95,8 @@ namespace mwmp BaseObjectList baseObjectList; BaseWorldstate baseWorldstate; + Worldstate serverWorldstate; + std::unique_ptr playerPacketController; std::unique_ptr actorPacketController; std::unique_ptr objectPacketController; diff --git a/apps/openmw-mp/Script/LuaState.cpp b/apps/openmw-mp/Script/LuaState.cpp index 79999e415..ced0f6edd 100644 --- a/apps/openmw-mp/Script/LuaState.cpp +++ b/apps/openmw-mp/Script/LuaState.cpp @@ -32,6 +32,7 @@ #include "../Settings.hpp" #include "../Spells.hpp" #include "../Weather.hpp" +#include "../Worldstate.hpp" using namespace std; @@ -94,6 +95,7 @@ LuaState::LuaState() WeatherMgr::Init(*this); Players::Init(*this); + Worldstate::Init(*this); timerCtrl = make_unique(); TimerController::Init(*this); @@ -256,144 +258,6 @@ LuaState::LuaState() mwmp::Networking::getPtr()->setServerPassword(passw); }); - lua->set_function("setHour", [](double hour) { - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.hour = hour; - - Players::for_each([&hour, &packet](Player *player){ - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setMonth", [](int month) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.month = month; - - Players::for_each([&month, &packet](Player *player){ - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setDay", [](int day) { - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.day = day; - - Players::for_each([&day, &packet](Player *player){ - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setYear", [](int year) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.year = year; - - Players::for_each([&year, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setDaysPassed", [](int daysPassed) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.daysPassed = daysPassed; - - Players::for_each([&daysPassed, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setTimeScale", [](float timeScale) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME); - - tempWorldstate.timeScale = timeScale; - - Players::for_each([&timeScale, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setPlayerCollisionState", [](bool state) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_COLLISION_OVERRIDE); - - tempWorldstate.hasPlayerCollision = state; - - Players::for_each([&state, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setActorCollisionState", [](bool state) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_COLLISION_OVERRIDE); - - tempWorldstate.hasActorCollision = state; - - Players::for_each([&state, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("setPlacedObjectCollisionState", [](bool state) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_COLLISION_OVERRIDE); - - tempWorldstate.hasPlacedObjectCollision = state; - - Players::for_each([&state, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - - lua->set_function("useActorCollisionForPlacedObjects", [](bool state) { - - auto packet = mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_COLLISION_OVERRIDE); - - tempWorldstate.useActorCollisionForPlacedObjects = state; - - Players::for_each([&state, &packet](Player *player) { - - tempWorldstate.guid = player->guid; - packet->setWorldstate(&tempWorldstate); - packet->Send(false); - }); - }); - lua->set_function("createChannel", [](){ return mwmp::Networking::get().createChannel(); }); diff --git a/apps/openmw-mp/Worldstate.cpp b/apps/openmw-mp/Worldstate.cpp new file mode 100644 index 000000000..8427df744 --- /dev/null +++ b/apps/openmw-mp/Worldstate.cpp @@ -0,0 +1,133 @@ +#include + +#include "Worldstate.hpp" +#include "Networking.hpp" + +using namespace std; + +void Worldstate::Init(LuaState &lua) +{ + sol::table worldstateTable = lua.getState()->create_named_table("Worldstate"); + + worldstateTable.set_function("setHour", [](double hour) { + mwmp::Networking::get().get().getServerWorldstate()->setHour(hour); + }); + + worldstateTable.set_function("setDay", [](int day) { + mwmp::Networking::get().get().getServerWorldstate()->setDay(day); + }); + + worldstateTable.set_function("setMonth", [](int month) { + mwmp::Networking::get().get().getServerWorldstate()->setMonth(month); + }); + + worldstateTable.set_function("setYear", [](int year) { + mwmp::Networking::get().get().getServerWorldstate()->setYear(year); + }); + + worldstateTable.set_function("setDaysPassed", [](int daysPassed) { + mwmp::Networking::get().get().getServerWorldstate()->setDaysPassed(daysPassed); + }); + + worldstateTable.set_function("setTimeScale", [](float timeScale) { + mwmp::Networking::get().get().getServerWorldstate()->setTimeScale(timeScale); + }); + + worldstateTable.set_function("setPlayerCollisionState", [](bool state) { + mwmp::Networking::get().get().getServerWorldstate()->setPlayerCollisionState(state); + }); + + worldstateTable.set_function("setActorCollisionState", [](bool state) { + mwmp::Networking::get().get().getServerWorldstate()->setActorCollisionState(state); + }); + + worldstateTable.set_function("setPlacedObjectCollisionState", [](bool state) { + mwmp::Networking::get().get().getServerWorldstate()->setPlacedObjectCollisionState(state); + }); + + worldstateTable.set_function("setActorCollisionForPlacedObjects", [](bool state) { + mwmp::Networking::get().get().getServerWorldstate()->setActorCollisionForPlacedObjects(state); + }); +} + +void Worldstate::update() +{ + auto worldstateController = mwmp::Networking::get().getWorldstatePacketController(); + + if (shouldUpdateTime) + { + auto packet = worldstateController->GetPacket(ID_WORLD_TIME); + packet->setWorldstate(this); + packet->Send(true); + + shouldUpdateTime = false; + } + + if (shouldUpdateCollisionOverrides) + { + auto packet = worldstateController->GetPacket(ID_WORLD_COLLISION_OVERRIDE); + packet->setWorldstate(this); + packet->Send(true); + + shouldUpdateCollisionOverrides = false; + } +} + +void Worldstate::setHour(double inputHour) +{ + hour = inputHour; + shouldUpdateTime = true; +} + +void Worldstate::setDay(int inputDay) +{ + day = inputDay; + shouldUpdateTime = true; +} + +void Worldstate::setMonth(int inputMonth) +{ + month = inputMonth; + shouldUpdateTime = true; +} + +void Worldstate::setYear(int inputYear) +{ + year = inputYear; + shouldUpdateTime = true; +} + +void Worldstate::setDaysPassed(int inputDaysPassed) +{ + daysPassed = inputDaysPassed; + shouldUpdateTime = true; +} + +void Worldstate::setTimeScale(float inputTimeScale) +{ + timeScale = inputTimeScale; + shouldUpdateTime = true; +} + +void Worldstate::setPlayerCollisionState(bool state) +{ + hasPlayerCollision = state; + shouldUpdateCollisionOverrides = true; +} + +void Worldstate::setActorCollisionState(bool state) +{ + hasActorCollision = state; + shouldUpdateCollisionOverrides = true; +} +void Worldstate::setPlacedObjectCollisionState(bool state) +{ + hasPlacedObjectCollision = state; + shouldUpdateCollisionOverrides = true; +} + +void Worldstate::setActorCollisionForPlacedObjects(bool state) +{ + useActorCollisionForPlacedObjects = state; + shouldUpdateCollisionOverrides = true; +} diff --git a/apps/openmw-mp/Worldstate.hpp b/apps/openmw-mp/Worldstate.hpp new file mode 100644 index 000000000..1d087c8cd --- /dev/null +++ b/apps/openmw-mp/Worldstate.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include +#include +#include + +class LuaState; + + +class Worldstate : public mwmp::BaseWorldstate +{ +public: + + static void Init(LuaState &lua); + + void update(); + + void setHour(double hour); + void setDay(int day); + void setMonth(int month); + void setYear(int year); + void setDaysPassed(int daysPassed); + void setTimeScale(float timeScale); + + void setPlayerCollisionState(bool state); + void setActorCollisionState(bool state); + void setPlacedObjectCollisionState(bool state); + void setActorCollisionForPlacedObjects(bool state); + +private: + + bool shouldUpdateTime, shouldUpdateCollisionOverrides; + +};