[Server] Iterate only through updated players on each frame

sol2-server-rewrite
Koncord 7 years ago
parent 4ab338bbb1
commit 8f5d31cb03

@ -0,0 +1,26 @@
//
// Created by koncord on 02.01.18.
//
#include "BaseMgr.hpp"
#include "Player.hpp"
BaseMgr::BaseMgr(Player *player) : player(player), changed(false)
{
}
void BaseMgr::update()
{
if (!changed)
return;
changed = false;
processUpdate();
}
void BaseMgr::setChanged()
{
changed = true;
player->addToUpdateQueue();
}

@ -0,0 +1,21 @@
//
// Created by koncord on 02.01.18.
//
#pragma once
class Player;
class BaseMgr
{
public:
explicit BaseMgr(Player *player);
void update();
protected:
bool isChanged() const { return changed; };
void setChanged();
virtual void processUpdate() = 0;
Player *player;
private:
bool changed;
};

@ -20,22 +20,16 @@ void Books::Init(LuaState &lua)
); );
} }
Books::Books(Player *player) : player(player), changed(false) Books::Books(Player *player) : BaseMgr(player)
{ {
}
Books::~Books()
{
} }
void Books::addBook(const std::string &bookId) void Books::addBook(const std::string &bookId)
{ {
if (!changed) if (!isChanged())
reset(); reset();
player->bookChanges.books.push_back({bookId}); player->bookChanges.books.push_back({bookId});
changed = true; setChanged();
} }
std::string Books::getBookId(unsigned i) const std::string Books::getBookId(unsigned i) const
@ -56,12 +50,8 @@ void Books::reset()
player->bookChanges.books.clear(); player->bookChanges.books.clear();
} }
void Books::update() void Books::processUpdate()
{ {
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK);
packet->setPlayer(player); packet->setPlayer(player);

@ -5,28 +5,22 @@
#pragma once #pragma once
#include <string> #include <string>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
class Books class Books final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit Books(Player *player); explicit Books(Player *player);
~Books();
void addBook(const std::string &bookId); void addBook(const std::string &bookId);
std::string getBookId(unsigned i) const; std::string getBookId(unsigned i) const;
unsigned getChanges() const; unsigned getChanges() const;
void reset(); void reset();
void update();
private: private:
Player *player; void processUpdate() final;
bool changed;
}; };

@ -51,6 +51,7 @@ set(SERVER
stacktrace.cpp stacktrace.cpp
Window.cpp Window.cpp
Weather.cpp Weather.cpp
BaseMgr.cpp
Script/CommandController.cpp Script/EventController.cpp Script/LuaState.cpp Script/luaUtils.cpp Script/CommandController.cpp Script/EventController.cpp Script/LuaState.cpp Script/luaUtils.cpp
) )

@ -5,6 +5,7 @@
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include "Script/LuaState.hpp" #include "Script/LuaState.hpp"
#include "Player.hpp"
#include "Networking.hpp" #include "Networking.hpp"
#include "Cells.hpp" #include "Cells.hpp"
@ -45,13 +46,20 @@ std::string Cells::getDescription() const
return netActor->getNetCreature()->cell.getDescription(); return netActor->getNetCreature()->cell.getDescription();
} }
inline void Cells::setChanged()
{
if (!changedCell && netActor->isPlayer())
netActor->toPlayer()->addToUpdateQueue();
changedCell = true;
}
void Cells::setDescription(const std::string &cellDescription) void Cells::setDescription(const std::string &cellDescription)
{ {
/*LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Script is moving %s from %s to %s", netActor->getNetCreature()->npc.mName.c_str(), /*LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Script is moving %s from %s to %s", netActor->getNetCreature()->npc.mName.c_str(),
netActor->getNetCreature()->cell.getDescription().c_str(), cellDescription.c_str());*/ netActor->getNetCreature()->cell.getDescription().c_str(), cellDescription.c_str());*/
netActor->getNetCreature()->cell = Utils::getCellFromDescription(cellDescription); netActor->getNetCreature()->cell = Utils::getCellFromDescription(cellDescription);
changedCell = true; setChanged();
} }
std::tuple<int, int> Cells::getExterior() const std::tuple<int, int> Cells::getExterior() const
@ -70,7 +78,7 @@ void Cells::setExterior(int x, int y)
netActor->getNetCreature()->cell.mData.mX = x; netActor->getNetCreature()->cell.mData.mX = x;
netActor->getNetCreature()->cell.mData.mY = y; netActor->getNetCreature()->cell.mData.mY = y;
changedCell = true; setChanged();
} }
bool Cells::isExterior() const bool Cells::isExterior() const

@ -35,9 +35,11 @@ public:
bool isChangedCell() const; bool isChangedCell() const;
void resetChangedCell(); void resetChangedCell();
private: private:
NetActor *netActor; NetActor *netActor;
bool changedCell; bool changedCell;
void setChanged();
}; };

@ -35,14 +35,8 @@ void CharClass::Init(LuaState &lua)
); );
} }
CharClass::CharClass(Player *player) : player(player), changed(false) CharClass::CharClass(Player *player) : BaseMgr(player)
{ {
printf("CharClass::CharClass()\n");
}
CharClass::~CharClass()
{
printf("CharClass::~CharClass()\n");
} }
string CharClass::getDefault() const string CharClass::getDefault() const
@ -53,7 +47,7 @@ string CharClass::getDefault() const
void CharClass::setDefault(const string &className) void CharClass::setDefault(const string &className)
{ {
player->charClass.mId = className; player->charClass.mId = className;
changed = true; setChanged();
printf("CharClass::setDefault()\n"); printf("CharClass::setDefault()\n");
} }
@ -65,7 +59,7 @@ bool CharClass::isCustom() const
void CharClass::setName(const string &className) void CharClass::setName(const string &className)
{ {
player->charClass.mName = className; player->charClass.mName = className;
changed = true; setChanged();
} }
string CharClass::getName() const string CharClass::getName() const
@ -81,7 +75,7 @@ std::string CharClass::getDescription() const
void CharClass::setDescription(const string &desc) void CharClass::setDescription(const string &desc)
{ {
player->charClass.mDescription = desc; player->charClass.mDescription = desc;
changed = true; setChanged();
} }
std::tuple<int, int> CharClass::getMajorAttributes() const std::tuple<int, int> CharClass::getMajorAttributes() const
@ -95,7 +89,7 @@ void CharClass::setMajorAttributes(int first, int second)
auto &data = player->charClass.mData; auto &data = player->charClass.mData;
data.mAttribute[0] = first; data.mAttribute[0] = first;
data.mAttribute[1] = second; data.mAttribute[1] = second;
changed = true; setChanged();
} }
int CharClass::getSpecialization() const int CharClass::getSpecialization() const
@ -107,7 +101,7 @@ void CharClass::setSpecialization(int spec)
{ {
auto &data = player->charClass.mData; auto &data = player->charClass.mData;
data.mSpecialization = spec; data.mSpecialization = spec;
changed = true; setChanged();
} }
std::tuple<int, int, int, int, int> CharClass::getMinorSkills() const std::tuple<int, int, int, int, int> CharClass::getMinorSkills() const
@ -124,7 +118,7 @@ void CharClass::setMinorSkills(int first, int second, int third, int fourth, int
data.mSkills[2][0] = third; data.mSkills[2][0] = third;
data.mSkills[3][0] = fourth; data.mSkills[3][0] = fourth;
data.mSkills[4][0] = fifth; data.mSkills[4][0] = fifth;
changed = true; setChanged();
} }
std::tuple<int, int, int, int, int> CharClass::getMajorSkills() const std::tuple<int, int, int, int, int> CharClass::getMajorSkills() const
@ -141,14 +135,11 @@ void CharClass::setMajorSkills(int first, int second, int third, int fourth, int
data.mSkills[2][1] = third; data.mSkills[2][1] = third;
data.mSkills[3][1] = fourth; data.mSkills[3][1] = fourth;
data.mSkills[4][1] = fifth; data.mSkills[4][1] = fifth;
changed = true; setChanged();
} }
void CharClass::update() void CharClass::processUpdate()
{ {
if (!changed)
return;
changed = false;
printf("CharClass::update()\n"); printf("CharClass::update()\n");
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_CHARCLASS); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_CHARCLASS);
packet->setPlayer(player); packet->setPlayer(player);

@ -6,19 +6,18 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
class CharClass class CharClass final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit CharClass(Player *player); explicit CharClass(Player *player);
~CharClass();
void update();
std::string getDefault() const; std::string getDefault() const;
void setDefault(const std::string &className); void setDefault(const std::string &className);
@ -41,10 +40,9 @@ public:
std::tuple<int, int, int, int, int> getMajorSkills() const; std::tuple<int, int, int, int, int> getMajorSkills() const;
void setMajorSkills(int fisrt, int second, int third, int fourth, int fifth); void setMajorSkills(int fisrt, int second, int third, int fourth, int fifth);
private: private:
// not controlled pointer void processUpdate() final;
Player *player;
bool changed;
}; };

@ -21,7 +21,7 @@ void Dialogue::Init(LuaState &lua)
Dialogue::Dialogue(Player *player) : player(player), changed(false) Dialogue::Dialogue(Player *player) : BaseMgr(player)
{ {
} }
@ -32,12 +32,8 @@ void Dialogue::reset()
player->topicChanges.topics.clear(); player->topicChanges.topics.clear();
} }
void Dialogue::update() void Dialogue::processUpdate()
{ {
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC);
packet->setPlayer(player); packet->setPlayer(player);
@ -46,9 +42,9 @@ void Dialogue::update()
void Dialogue::addTopic(const std::string &topicId) void Dialogue::addTopic(const std::string &topicId)
{ {
if (!changed) if (!isChanged())
reset(); reset();
changed = true; setChanged();
player->topicChanges.topics.push_back({topicId}); player->topicChanges.topics.push_back({topicId});
} }

@ -5,11 +5,12 @@
#pragma once #pragma once
#include <string> #include <string>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
class Dialogue class Dialogue final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
@ -24,10 +25,8 @@ public:
void playSpeech(const std::string &sound); void playSpeech(const std::string &sound);
void reset(); void reset();
void update();
private: private:
Player *player; void processUpdate() final;
bool changed;
}; };

@ -22,7 +22,7 @@ void Factions::Init(LuaState &lua)
); );
} }
Factions::Factions(Player *player): player(player), changed(false) Factions::Factions(Player *player): BaseMgr(player)
{ {
} }
@ -32,12 +32,8 @@ Factions::~Factions()
} }
void Factions::update() void Factions::processUpdate()
{ {
if (!changed)
return;
changed = false;
auto packet =mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION); auto packet =mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(/*toOthers*/ false); packet->Send(/*toOthers*/ false);
@ -52,13 +48,13 @@ int Factions::getFactionChangesAction() const
void Factions::setFactionChangesAction(int action) void Factions::setFactionChangesAction(int action)
{ {
player->factionChanges.action = action; player->factionChanges.action = action;
changed = true; setChanged();
} }
void Factions::addFaction(Faction faction) void Factions::addFaction(Faction faction)
{ {
player->factionChanges.factions.push_back(faction.faction); player->factionChanges.factions.push_back(faction.faction);
changed = true; setChanged();
} }
@ -70,13 +66,13 @@ Faction Factions::getFaction(int id) const
void Factions::setFaction(int id, Faction faction) void Factions::setFaction(int id, Faction faction)
{ {
player->factionChanges.factions.at(id) = faction.faction; player->factionChanges.factions.at(id) = faction.faction;
changed = true; setChanged();
} }
void Factions::clear() void Factions::clear()
{ {
player->factionChanges.factions.clear(); player->factionChanges.factions.clear();
changed = true; setChanged();
} }
size_t Factions::size() const size_t Factions::size() const

@ -6,6 +6,7 @@
#include <string> #include <string>
#include <components/openmw-mp/Base/BasePlayer.hpp> #include <components/openmw-mp/Base/BasePlayer.hpp>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
@ -33,7 +34,7 @@ public:
mwmp::Faction faction; mwmp::Faction faction;
}; };
class Factions class Factions final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
@ -41,7 +42,6 @@ public:
explicit Factions(Player *player); explicit Factions(Player *player);
~Factions(); ~Factions();
void update();
int getFactionChangesAction() const; int getFactionChangesAction() const;
void setFactionChangesAction(int action); void setFactionChangesAction(int action);
@ -53,7 +53,5 @@ public:
void clear(); void clear();
private: private:
mwmp::Faction tempFaction; void processUpdate() final;
Player *player;
bool changed;
}; };

@ -23,22 +23,13 @@ void GUI::Init(LuaState &lua)
Window::Init(lua); Window::Init(lua);
} }
GUI::GUI(Player *player): player(player), changed(false) GUI::GUI(Player *player): BaseMgr(player)
{ {
} }
GUI::~GUI() void GUI::processUpdate()
{ {
}
void GUI::update()
{
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GUI_MESSAGEBOX); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GUI_MESSAGEBOX);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(false); packet->Send(false);
@ -50,7 +41,7 @@ void GUI::messageBox(int id, const char *label)
player->guiMessageBox.label = label; player->guiMessageBox.label = label;
player->guiMessageBox.type = Player::GUIMessageBox::MessageBox; player->guiMessageBox.type = Player::GUIMessageBox::MessageBox;
changed = true; setChanged();
} }
void GUI::customMessageBox(int id, const char *label, const char *buttons) void GUI::customMessageBox(int id, const char *label, const char *buttons)
@ -60,7 +51,7 @@ void GUI::customMessageBox(int id, const char *label, const char *buttons)
player->guiMessageBox.buttons = buttons; player->guiMessageBox.buttons = buttons;
player->guiMessageBox.type = Player::GUIMessageBox::CustomMessageBox; player->guiMessageBox.type = Player::GUIMessageBox::CustomMessageBox;
changed = true; setChanged();
} }
void GUI::inputDialog(int id, const char *label) void GUI::inputDialog(int id, const char *label)
@ -69,7 +60,7 @@ void GUI::inputDialog(int id, const char *label)
player->guiMessageBox.label = label; player->guiMessageBox.label = label;
player->guiMessageBox.type = Player::GUIMessageBox::InputDialog; player->guiMessageBox.type = Player::GUIMessageBox::InputDialog;
changed = true; setChanged();
} }
void GUI::passwordDialog(int id, const char *label, const char *note) void GUI::passwordDialog(int id, const char *label, const char *note)
@ -79,7 +70,7 @@ void GUI::passwordDialog(int id, const char *label, const char *note)
player->guiMessageBox.note = note; player->guiMessageBox.note = note;
player->guiMessageBox.type = Player::GUIMessageBox::PasswordDialog; player->guiMessageBox.type = Player::GUIMessageBox::PasswordDialog;
changed = true; setChanged();
} }
void GUI::listBox(int id, const char *label, const char *items) void GUI::listBox(int id, const char *label, const char *items)
@ -89,7 +80,7 @@ void GUI::listBox(int id, const char *label, const char *items)
player->guiMessageBox.data = items; player->guiMessageBox.data = items;
player->guiMessageBox.type = Player::GUIMessageBox::ListBox; player->guiMessageBox.type = Player::GUIMessageBox::ListBox;
changed = true; setChanged();
} }
void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state) void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state)
@ -155,32 +146,23 @@ void QuickKeys::Init(LuaState &lua)
); );
} }
QuickKeys::QuickKeys(Player *player) : player(player), changed(false) QuickKeys::QuickKeys(Player *player) : BaseMgr(player)
{ {
} }
QuickKeys::~QuickKeys() void QuickKeys::processUpdate()
{ {
}
void QuickKeys::update()
{
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_QUICKKEYS); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_QUICKKEYS);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(false); packet->Send(false);
clear(); clear();
} }
void QuickKeys::addQuickKey(QuickKey quickKey) void QuickKeys::addQuickKey(const QuickKey &quickKey)
{ {
player->quickKeyChanges.quickKeys.push_back(quickKey.quickKey); player->quickKeyChanges.quickKeys.push_back(quickKey.quickKey);
changed = true; setChanged();
} }
@ -192,13 +174,13 @@ QuickKey QuickKeys::getQuickKey(int id) const
void QuickKeys::setQuickKey(int id, const QuickKey &quickKey) void QuickKeys::setQuickKey(int id, const QuickKey &quickKey)
{ {
player->quickKeyChanges.quickKeys.at(id) = quickKey.quickKey; player->quickKeyChanges.quickKeys.at(id) = quickKey.quickKey;
changed = true; setChanged();
} }
void QuickKeys::clear() void QuickKeys::clear()
{ {
player->quickKeyChanges.quickKeys.clear(); player->quickKeyChanges.quickKeys.clear();
changed = true; setChanged();
} }
size_t QuickKeys::size() const size_t QuickKeys::size() const

@ -5,19 +5,17 @@
#pragma once #pragma once
#include "Window.hpp" #include "Window.hpp"
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
class GUI class GUI final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit GUI(Player *player); explicit GUI(Player *player);
~GUI();
void update();
void messageBox(int id, const char *label); void messageBox(int id, const char *label);
@ -40,8 +38,7 @@ public:
unsigned int getChanges() const; unsigned int getChanges() const;
private: private:
Player *player; void processUpdate() final;
bool changed;
std::unordered_map<int, std::shared_ptr<Window>> windows; std::unordered_map<int, std::shared_ptr<Window>> windows;
int lastWindowId; int lastWindowId;
}; };
@ -57,8 +54,8 @@ public:
int getSlot() const; int getSlot() const;
void setSlot(unsigned short slot); void setSlot(unsigned short slot);
int getType() const; mwmp::QuickKey::Type getType() const;
void setType(int slot); void setType(mwmp::QuickKey::Type slot);
std::string getItemId() const; std::string getItemId() const;
void setItemId(const std::string &itemId); void setItemId(const std::string &itemId);
@ -66,15 +63,12 @@ public:
mwmp::QuickKey quickKey; mwmp::QuickKey quickKey;
}; };
class QuickKeys class QuickKeys final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit QuickKeys(Player *player); explicit QuickKeys(Player *player);
~QuickKeys();
void update();
void addQuickKey(const QuickKey &quickKey); void addQuickKey(const QuickKey &quickKey);
QuickKey getQuickKey(int id) const; QuickKey getQuickKey(int id) const;
@ -83,6 +77,5 @@ public:
void clear(); void clear();
private: private:
Player *player; void processUpdate() final;
bool changed;
}; };

@ -12,6 +12,7 @@
#include "Inventory.hpp" #include "Inventory.hpp"
#include "NetActor.hpp" #include "NetActor.hpp"
#include <Player.hpp>
using namespace std; using namespace std;
@ -45,6 +46,26 @@ Inventory::~Inventory()
void Inventory::update() void Inventory::update()
{ {
printf("Inventory::update()"); printf("Inventory::update()");
/*if(isEquipmentChanged())
{
if (netActor->isPlayer())
{
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT);
packet->setPlayer(dynamic_cast<Player *>(netActor));
packet->Send(false);
packet->Send(true);
}
else
{
auto packet = mwmp::Networking::get().getActorPacketController()->GetPacket(ID_ACTOR_EQUIPMENT);
packet->setActorList(&actorList);
packet->Send(actorList.guid);
if (sendToAll)
serverCell->sendToLoaded(packet, &actorList);
}
resetEquipmentFlag();
}*/
/*if (equipmentChanged) /*if (equipmentChanged)
{ {
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT);
@ -86,6 +107,9 @@ void Inventory::equipItem(unsigned short slot, const std::string& refId, unsigne
if (!Utils::vectorContains(&netActor->getNetCreature()->equipmentIndexChanges, slot)) if (!Utils::vectorContains(&netActor->getNetCreature()->equipmentIndexChanges, slot))
netActor->getNetCreature()->equipmentIndexChanges.push_back(slot); netActor->getNetCreature()->equipmentIndexChanges.push_back(slot);
if(!equipmentChanged && netActor->isPlayer())
netActor->toPlayer()->addToUpdateQueue();
equipmentChanged = true; equipmentChanged = true;
} }
@ -110,6 +134,8 @@ void Inventory::addItem(const std::string &refId, unsigned int count, int charge
netActor->getNetCreature()->inventoryChanges.items.push_back(item); netActor->getNetCreature()->inventoryChanges.items.push_back(item);
netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::ADD; netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::ADD;
if (inventoryChanged == 0 && netActor->isPlayer())
netActor->toPlayer()->addToUpdateQueue();
inventoryChanged = netActor->getNetCreature()->inventoryChanges.action; inventoryChanged = netActor->getNetCreature()->inventoryChanges.action;
} }
@ -126,6 +152,8 @@ void Inventory::removeItem(const std::string &refId, unsigned short count)
netActor->getNetCreature()->inventoryChanges.items.push_back(item); netActor->getNetCreature()->inventoryChanges.items.push_back(item);
netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::REMOVE; netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::REMOVE;
if (inventoryChanged == 0 && netActor->isPlayer())
netActor->toPlayer()->addToUpdateQueue();
inventoryChanged = netActor->getNetCreature()->inventoryChanges.action; inventoryChanged = netActor->getNetCreature()->inventoryChanges.action;
} }

@ -9,10 +9,11 @@
#include "Networking.hpp" #include "Networking.hpp"
#include "NetActor.hpp" #include "NetActor.hpp"
#include "Player.hpp"
using namespace std; using namespace std;
NetActor::NetActor() : inventory(this), cellAPI(this) NetActor::NetActor() : inventory(this), cellAPI(this), isActorPlayer(false)
{ {
} }
@ -37,6 +38,9 @@ void NetActor::setPosition(float x, float y, float z)
netCreature->position.pos[0] = x; netCreature->position.pos[0] = x;
netCreature->position.pos[1] = y; netCreature->position.pos[1] = y;
netCreature->position.pos[2] = z; netCreature->position.pos[2] = z;
if(!positionChanged && isPlayer())
toPlayer()->addToUpdateQueue();
positionChanged = true; positionChanged = true;
} }
@ -49,6 +53,9 @@ void NetActor::setRotation(float x, float z)
{ {
netCreature->position.rot[0] = x; netCreature->position.rot[0] = x;
netCreature->position.rot[2] = z; netCreature->position.rot[2] = z;
if (!positionChanged && isPlayer())
toPlayer()->addToUpdateQueue();
positionChanged = true; positionChanged = true;
} }
@ -65,6 +72,8 @@ void NetActor::setHealth(float base, float current)
if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 0)) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 0))
netCreature->statsDynamicIndexChanges.push_back(0); netCreature->statsDynamicIndexChanges.push_back(0);
if (!statsChanged && isPlayer())
toPlayer()->addToUpdateQueue();
statsChanged = true; statsChanged = true;
} }
@ -81,6 +90,8 @@ void NetActor::setMagicka(float base, float current)
if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 1)) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 1))
netCreature->statsDynamicIndexChanges.push_back(1); netCreature->statsDynamicIndexChanges.push_back(1);
if (!statsChanged && isPlayer())
toPlayer()->addToUpdateQueue();
statsChanged = true; statsChanged = true;
} }
@ -97,6 +108,8 @@ void NetActor::setFatigue(float base, float current)
if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 2)) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 2))
netCreature->statsDynamicIndexChanges.push_back(2); netCreature->statsDynamicIndexChanges.push_back(2);
if (!statsChanged && isPlayer())
toPlayer()->addToUpdateQueue();
statsChanged = true; statsChanged = true;
} }
@ -109,3 +122,10 @@ Cells &NetActor::getCell()
{ {
return cellAPI; return cellAPI;
} }
Player *NetActor::toPlayer()
{
if (isPlayer())
return dynamic_cast<Player*>(this);
return nullptr;
}

@ -20,6 +20,7 @@ class NetActor
{ {
public: public:
NetActor(); NetActor();
virtual ~NetActor() = default;
void resetUpdateFlags(); void resetUpdateFlags();
@ -63,13 +64,18 @@ public:
Cells &getCell(); Cells &getCell();
mwmp::BaseNetCreature *getNetCreature() { return netCreature; } mwmp::BaseNetCreature *getNetCreature() { return netCreature; }
bool isPlayer() const { return isActorPlayer; }
Player *toPlayer();
protected: protected:
bool baseInfoChanged, levelChanged, statsChanged, positionChanged, attributesChanged, skillsChanged; bool baseInfoChanged, levelChanged, statsChanged, positionChanged, attributesChanged, skillsChanged;
mwmp::BasePlayer *basePlayer; mwmp::BasePlayer *basePlayer;
mwmp::BaseNetCreature *netCreature; mwmp::BaseNetCreature *netCreature;
Inventory inventory; Inventory inventory;
Cells cellAPI; Cells cellAPI;
bool isActorPlayer;
}; };

@ -474,13 +474,7 @@ int Networking::mainLoop()
} }
timerCtrl.tick(); timerCtrl.tick();
if (updated) if (updated)
{ Players::processUpdated();
// fixme: not good to call for_each every frame. Maybe for_each_Updated will be better
Players::for_each([](shared_ptr<Player> pl) {
pl->update();
});
}
this_thread::sleep_for (chrono::milliseconds(1));
} }
timerCtrl.terminate(); timerCtrl.terminate();

@ -123,6 +123,7 @@ Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid), NetActor(), changedM
markedForDeletion = false; markedForDeletion = false;
storedData = mwmp::Networking::get().getState().getState()->create_table(); storedData = mwmp::Networking::get().getState().getState()->create_table();
customData = mwmp::Networking::get().getState().getState()->create_table(); customData = mwmp::Networking::get().getState().getState()->create_table();
isActorPlayer = true;
} }
Player::~Player() Player::~Player()
@ -131,6 +132,14 @@ Player::~Player()
CellController::get().deletePlayer(this); CellController::get().deletePlayer(this);
} }
void Player::addToUpdateQueue()
{
if (inUpdateQueue)
return;
inUpdateQueue = true;
Players::addToQueue(this);
}
void Player::update() void Player::update()
{ {
auto plPCtrl = mwmp::Networking::get().getPlayerPacketController(); auto plPCtrl = mwmp::Networking::get().getPlayerPacketController();
@ -236,6 +245,7 @@ void Player::update()
weatherMgr.update(); weatherMgr.update();
resetUpdateFlags(); resetUpdateFlags();
inUpdateQueue = false;
} }
unsigned short Player::getId() unsigned short Player::getId()

@ -191,6 +191,8 @@ public:
bool isMarkedForDeleteion() const; bool isMarkedForDeleteion() const;
void addToUpdateQueue();
private: private:
CellController::TContainer cells; CellController::TContainer cells;
int handshakeCounter; int handshakeCounter;
@ -209,6 +211,7 @@ private:
sol::table storedData; sol::table storedData;
sol::table customData; sol::table customData;
bool markedForDeletion; bool markedForDeletion;
bool inUpdateQueue;
}; };
#endif //OPENMW_PLAYER_HPP #endif //OPENMW_PLAYER_HPP

@ -7,6 +7,7 @@
using namespace std; using namespace std;
Players::Store Players::store; Players::Store Players::store;
std::queue<Player*> Players::updateQueue;
void Players::Init(LuaState &lua) void Players::Init(LuaState &lua)
{ {
@ -124,3 +125,19 @@ size_t Players::size()
{ {
return store.size(); return store.size();
} }
void Players::processUpdated()
{
while (!updateQueue.empty())
{
Player *player = updateQueue.front();
updateQueue.pop();
player->update();
}
}
void Players::addToQueue(Player *player)
{
updateQueue.push(player);
}

@ -11,6 +11,7 @@
#include <boost/multi_index/global_fun.hpp> #include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/mem_fun.hpp> #include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/random_access_index.hpp> #include <boost/multi_index/random_access_index.hpp>
#include <queue>
#include "Player.hpp" #include "Player.hpp"
class LuaState; class LuaState;
@ -18,6 +19,7 @@ class LuaState;
class Players class Players
{ {
friend class Player;
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
@ -47,7 +49,11 @@ public:
static void for_each(std::function<void(std::shared_ptr<Player>)> func); static void for_each(std::function<void(std::shared_ptr<Player>)> func);
static void processUpdated();
private: private:
static void addToQueue(Player *player);
static Store store; static Store store;
static std::queue<Player*> updateQueue;
}; };

@ -85,17 +85,12 @@ void Quests::Init(LuaState &lua)
); );
} }
Quests::Quests(Player *player) : player(player), changedKills(false), changedJournal(false) Quests::Quests(Player *player) : BaseMgr(player), changedKills(false), changedJournal(false)
{ {
} }
Quests::~Quests() void Quests::processUpdate()
{
}
void Quests::update()
{ {
if (changedJournal) if (changedJournal)
{ {
@ -130,13 +125,14 @@ void Quests::addJournalItem(JournalItem item)
{ {
player->journalChanges.journalItems.push_back(item.item); player->journalChanges.journalItems.push_back(item.item);
changedJournal = true; changedJournal = true;
setChanged();
} }
void Quests::setJournalItem(unsigned int id, JournalItem item) void Quests::setJournalItem(unsigned int id, JournalItem item)
{ {
player->journalChanges.journalItems.at(id) = item.item; player->journalChanges.journalItems.at(id) = item.item;
changedJournal = true; changedJournal = true;
setChanged();
} }
JournalItem Quests::getJournalItem(unsigned int id) JournalItem Quests::getJournalItem(unsigned int id)
@ -148,6 +144,7 @@ void Quests::addKill(const std::string &refId, int number)
{ {
player->killChanges.kills.push_back({refId, number}); player->killChanges.kills.push_back({refId, number});
changedKills = true; changedKills = true;
setChanged();
} }
std::tuple<std::string, int> Quests::getKill(unsigned int i) const std::tuple<std::string, int> Quests::getKill(unsigned int i) const

@ -6,6 +6,7 @@
#include <cstddef> #include <cstddef>
#include <components/openmw-mp/Base/BasePlayer.hpp> #include <components/openmw-mp/Base/BasePlayer.hpp>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
@ -34,15 +35,13 @@ public:
mwmp::JournalItem item; mwmp::JournalItem item;
}; };
class Quests class Quests final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit Quests(Player *player); explicit Quests(Player *player);
~Quests();
void update();
size_t getJournalChangesSize() const; size_t getJournalChangesSize() const;
size_t getKillChangesSize() const; size_t getKillChangesSize() const;
@ -55,7 +54,7 @@ public:
std::tuple<std::string, int> getKill(unsigned int i) const; std::tuple<std::string, int> getKill(unsigned int i) const;
private: private:
Player *player; void processUpdate() final;
bool changedKills, changedJournal; bool changedKills, changedJournal;
}; };

@ -21,51 +21,43 @@ void GameSettings::Init(LuaState &lua)
); );
} }
GameSettings::GameSettings(Player *player) : player(player), changed(false) GameSettings::GameSettings(Player *player) : BaseMgr(player)
{ {
} }
GameSettings::~GameSettings()
{
}
void GameSettings::setConsoleAllowed(bool state) void GameSettings::setConsoleAllowed(bool state)
{ {
player->consoleAllowed = state; player->consoleAllowed = state;
changed = true; setChanged();
} }
void GameSettings::setDifficulty(int difficulty) void GameSettings::setDifficulty(int difficulty)
{ {
player->difficulty = difficulty; player->difficulty = difficulty;
changed = true; setChanged();
} }
void GameSettings::setBedRestAllowed(bool state) void GameSettings::setBedRestAllowed(bool state)
{ {
player->bedRestAllowed = state; player->bedRestAllowed = state;
changed = true; setChanged();
} }
void GameSettings::setWildernessRestAllowed(bool state) void GameSettings::setWildernessRestAllowed(bool state)
{ {
player->wildernessRestAllowed = state; player->wildernessRestAllowed = state;
changed = true; setChanged();
} }
void GameSettings::setWaitAllowed(bool state) void GameSettings::setWaitAllowed(bool state)
{ {
player->waitAllowed = state; player->waitAllowed = state;
changed = true; setChanged();
} }
void GameSettings::update() void GameSettings::processUpdate()
{ {
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_SETTINGS); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_SETTINGS);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(false); packet->Send(false);

@ -4,18 +4,19 @@
#pragma once #pragma once
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
class GameSettings class GameSettings final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit GameSettings(Player *player); explicit GameSettings(Player *player);
~GameSettings();
void setConsoleAllowed(bool state); void setConsoleAllowed(bool state);
@ -27,8 +28,7 @@ public:
void setWaitAllowed(bool state); void setWaitAllowed(bool state);
void update();
private: private:
Player *player; void processUpdate() final;
bool changed;
}; };

@ -217,22 +217,13 @@ void Spells::Init(LuaState &lua)
); );
} }
Spells::Spells(Player *player) : player(player), changed(false) Spells::Spells(Player *player) : BaseMgr(player)
{ {
} }
Spells::~Spells() void Spells::processUpdate()
{ {
}
void Spells::update()
{
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(false); packet->Send(false);
@ -240,18 +231,18 @@ void Spells::update()
void Spells::addCustomSpell(Spell spell) void Spells::addCustomSpell(Spell spell)
{ {
if (!changed) if (!isChanged())
clear(); clear();
changed = true; setChanged();
player->spellbookChanges.spells.push_back(spell.spell); player->spellbookChanges.spells.push_back(spell.spell);
} }
void Spells::addDefaultSpell(const std::string &spell) void Spells::addDefaultSpell(const std::string &spell)
{ {
if (!changed) if (!isChanged())
clear(); clear();
changed = true; setChanged();
ESM::Spell s; ESM::Spell s;
s.mId = spell; s.mId = spell;

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <components/esm/records.hpp> #include <components/esm/records.hpp>
#include "BaseMgr.hpp"
class LuaState; class LuaState;
class Player; class Player;
@ -72,15 +73,12 @@ public:
ESM::Spell spell; ESM::Spell spell;
}; };
class Spells class Spells final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
explicit Spells(Player *player); explicit Spells(Player *player);
~Spells();
void update();
void addCustomSpell(Spell spell); void addCustomSpell(Spell spell);
Spell getCustomSpell(unsigned int i); Spell getCustomSpell(unsigned int i);
@ -90,9 +88,7 @@ public:
private: private:
void clear(); void clear();
void processUpdate() final;
Player *player;
bool changed;
}; };

@ -25,25 +25,22 @@ void WeatherMgr::Init(LuaState &lua)
); );
} }
WeatherMgr::WeatherMgr(Player *player) : player(player), changed(false) WeatherMgr::WeatherMgr(Player *player) : BaseMgr(player)
{ {
} }
void WeatherMgr::setWeather(int weather) void WeatherMgr::setWeather(int weather)
{ {
changed = true; setChanged();
player->weather.nextWeather = weather; player->weather.nextWeather = weather;
player->weather.transitionFactor = 0.0f; player->weather.transitionFactor = 0.0f;
player->weather.updateTime = 0.0f; player->weather.updateTime = 0.0f;
} }
void WeatherMgr::update() void WeatherMgr::processUpdate()
{ {
if (!changed)
return;
changed = false;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_WEATHER); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_WEATHER);
@ -55,7 +52,7 @@ void WeatherMgr::update()
void WeatherMgr::setCurrent(int weather) void WeatherMgr::setCurrent(int weather)
{ {
player->weather.currentWeather = weather; player->weather.currentWeather = weather;
changed = true; setChanged();
} }
int WeatherMgr::getCurrent() const int WeatherMgr::getCurrent() const
@ -66,7 +63,7 @@ int WeatherMgr::getCurrent() const
void WeatherMgr::setNext(int weather) void WeatherMgr::setNext(int weather)
{ {
player->weather.nextWeather = weather; player->weather.nextWeather = weather;
changed = true; setChanged();
} }
int WeatherMgr::getNext() const int WeatherMgr::getNext() const
@ -77,7 +74,7 @@ int WeatherMgr::getNext() const
void WeatherMgr::setTransition(float time) void WeatherMgr::setTransition(float time)
{ {
player->weather.transitionFactor = time; player->weather.transitionFactor = time;
changed = true; setChanged();
} }
float WeatherMgr::getTransition() const float WeatherMgr::getTransition() const
@ -88,7 +85,7 @@ float WeatherMgr::getTransition() const
void WeatherMgr::setUpdate(float time) void WeatherMgr::setUpdate(float time)
{ {
player->weather.updateTime = time; player->weather.updateTime = time;
changed = true; setChanged();
} }
float WeatherMgr::getUpdate() const float WeatherMgr::getUpdate() const
@ -104,8 +101,8 @@ void WeatherMgr::requestWeather()
void WeatherMgr::copy(const WeatherMgr &other) void WeatherMgr::copy(const WeatherMgr &other)
{ {
if(other.player == player) if (other.player == player)
return; return;
player->weather = other.player->weather; player->weather = other.player->weather;
changed = true; setChanged();
} }

@ -6,10 +6,11 @@
#include <components/openmw-mp/Base/BasePlayer.hpp> #include <components/openmw-mp/Base/BasePlayer.hpp>
#include <apps/openmw-mp/Script/LuaState.hpp> #include <apps/openmw-mp/Script/LuaState.hpp>
#include "BaseMgr.hpp"
class Player; class Player;
class WeatherMgr class WeatherMgr final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
@ -17,8 +18,6 @@ public:
explicit WeatherMgr(Player *player); explicit WeatherMgr(Player *player);
~WeatherMgr() = default; ~WeatherMgr() = default;
void update();
void setWeather(int weather); void setWeather(int weather);
void setCurrent(int weather); void setCurrent(int weather);
@ -38,8 +37,7 @@ public:
void copy(const WeatherMgr &other); void copy(const WeatherMgr &other);
private: private:
Player *player; void processUpdate() final;
bool changed;
}; };

@ -26,16 +26,11 @@ void Window::Init(LuaState &lua)
); );
} }
Window::Window(Player *player, int id) : player(player) Window::Window(Player *player, int id) : BaseMgr(player)
{ {
guiWindow.id = id; guiWindow.id = id;
} }
Window::~Window()
{
}
void Window::show() void Window::show()
{ {
printf("Trying to show window: %d\n", guiWindow.id); printf("Trying to show window: %d\n", guiWindow.id);
@ -213,3 +208,8 @@ void Window::addSlider(short x, short y, short w, short h, const std::string &id
widget.disabled = false; widget.disabled = false;
guiWindow.widgets.push_back(widget); guiWindow.widgets.push_back(widget);
} }
void Window::processUpdate()
{
// Currently not used
}

@ -6,16 +6,16 @@
#include <components/openmw-mp/Base/BasePlayer.hpp> #include <components/openmw-mp/Base/BasePlayer.hpp>
#include <apps/openmw-mp/Script/LuaState.hpp> #include <apps/openmw-mp/Script/LuaState.hpp>
#include "BaseMgr.hpp"
class Player; class Player;
class Window class Window final: public BaseMgr
{ {
public: public:
static void Init(LuaState &lua); static void Init(LuaState &lua);
public: public:
Window(Player *player, int id); Window(Player *player, int id);
~Window();
void setSize(short x, short y); void setSize(short x, short y);
@ -34,10 +34,9 @@ public:
void addPassiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional<bool> active); void addPassiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional<bool> active);
void addActiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional<bool> active); void addActiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional<bool> active);
void addSlider(short x, short y, short w, short h,const std::string &id, sol::optional<bool> active); void addSlider(short x, short y, short w, short h,const std::string &id, sol::optional<bool> active);
private:
Player *player;
bool changed;
private:
void processUpdate() final;
sol::function callback; sol::function callback;
mwmp::BasePlayer::GUIWindow guiWindow; mwmp::BasePlayer::GUIWindow guiWindow;
}; };

Loading…
Cancel
Save