[Server] Create Worldstate class and implement associated table

sol2-server-rewrite
David Cernat 7 years ago
parent 07b781cd32
commit 49bf605e8e

@ -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
)

@ -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;

@ -7,6 +7,7 @@
#include <components/openmw-mp/Controllers/WorldstatePacketController.hpp>
#include <components/openmw-mp/Packets/PacketPreInit.hpp>
#include <apps/openmw-mp/Script/LuaState.hpp>
#include <apps/openmw-mp/Worldstate.hpp>
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> playerPacketController;
std::unique_ptr<ActorPacketController> actorPacketController;
std::unique_ptr<ObjectPacketController> objectPacketController;

@ -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>();
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();
});

@ -0,0 +1,133 @@
#include <components/openmw-mp/NetworkMessages.hpp>
#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;
}

@ -0,0 +1,36 @@
#pragma once
#include <RakNetTypes.h>
#include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/Base/BaseWorldstate.hpp>
#include <components/openmw-mp/Packets/Worldstate/WorldstatePacket.hpp>
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;
};
Loading…
Cancel
Save