diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index fded0c3e1..a7f73c486 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -22,8 +22,7 @@ using namespace mwmp; using namespace std; Networking *Networking::sThis = 0; - -static BaseEvent *baseEvent = nullptr; +BaseEvent baseEvent = BaseEvent(); Networking::Networking(RakNet::RakPeerInterface *peer) { @@ -472,13 +471,9 @@ void Networking::processWorldPacket(RakNet::Packet *packet) WorldPacket *myPacket = worldController->GetPacket(packet->data[0]); - if (baseEvent) - { - delete baseEvent; - baseEvent = nullptr; - } - - baseEvent = new BaseEvent(player->guid); + // Clear our baseEvent before loading new data in it + baseEvent.cell.blank(); + baseEvent.objectChanges.objects.clear(); switch (packet->data[0]) { @@ -488,12 +483,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_PLACE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -503,12 +498,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_DELETE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -518,12 +513,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_SCALE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -533,12 +528,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_LOCK from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -548,12 +543,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_UNLOCK from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -563,8 +558,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_MOVE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -574,8 +569,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_ROTATE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -585,8 +580,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_OBJECT_ANIM_PLAY from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -596,12 +591,12 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_DOOR_STATE from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -611,21 +606,21 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_CONTAINER from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); + myPacket->Read(&baseEvent); - LOG_APPEND(Log::LOG_WARN, "- action: %i", baseEvent->action); + LOG_APPEND(Log::LOG_WARN, "- action: %i", baseEvent.action); // Until we have a timestamp-based system, send packets pertaining to more // than one container (i.e. replies to server requests for container contents) // only to players who have the container's cell loaded - if (baseEvent->action == BaseEvent::SET && baseEvent->objectChanges.count > 1) - CellController::get()->getCell(&baseEvent->cell)->sendToLoaded(myPacket, baseEvent); + if (baseEvent.action == BaseEvent::SET && baseEvent.objectChanges.count > 1) + CellController::get()->getCell(&baseEvent.cell)->sendToLoaded(myPacket, &baseEvent); else - myPacket->Send(baseEvent, true); + myPacket->Send(&baseEvent, true); Script::Call( player->getId(), - baseEvent->cell.getDescription().c_str()); + baseEvent.cell.getDescription().c_str()); break; } @@ -635,8 +630,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_SCRIPT_LOCAL_SHORT from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -646,8 +641,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_SCRIPT_LOCAL_FLOAT from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -657,8 +652,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_SCRIPT_MEMBER_SHORT from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -668,8 +663,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_SCRIPT_GLOBAL_SHORT from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -679,8 +674,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_MUSIC_PLAY from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -690,8 +685,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_VIDEO_PLAY from %s", player->npc.mName.c_str()); - myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + myPacket->Read(&baseEvent); + myPacket->Send(&baseEvent, true); break; } @@ -804,7 +799,7 @@ WorldPacketController *Networking::getWorldController() const BaseEvent *Networking::getLastEvent() { - return baseEvent; + return &baseEvent; } const Networking &Networking::get() diff --git a/apps/openmw-mp/Script/Functions/World.cpp b/apps/openmw-mp/Script/Functions/World.cpp index 713a56b43..443823941 100644 --- a/apps/openmw-mp/Script/Functions/World.cpp +++ b/apps/openmw-mp/Script/Functions/World.cpp @@ -9,24 +9,16 @@ using namespace mwmp; -static BaseEvent *baseEvent = nullptr; -static WorldObject tempWorldObject; -static ContainerItem tempContainerItem; +BaseEvent scriptEvent = BaseEvent(); +WorldObject tempWorldObject; +ContainerItem tempContainerItem; std::regex exteriorCellPattern("^(-?\\d+), (-?\\d+)$"); -void WorldFunctions::CreateBaseEvent(unsigned short pid) noexcept +void WorldFunctions::ClearScriptEvent() noexcept { - Player *player; - GET_PLAYER(pid, player, ); - - if (baseEvent) - { - delete baseEvent; - baseEvent = nullptr; - } - - baseEvent = new BaseEvent(player->guid); + scriptEvent.cell.blank(); + scriptEvent.objectChanges.objects.clear(); } unsigned int WorldFunctions::GetObjectChangesSize() noexcept @@ -34,7 +26,7 @@ unsigned int WorldFunctions::GetObjectChangesSize() noexcept return mwmp::Networking::getPtr()->getLastEvent()->objectChanges.count; } -unsigned int WorldFunctions::GetBaseEventAction() noexcept +unsigned int WorldFunctions::GetLastEventAction() noexcept { return mwmp::Networking::getPtr()->getLastEvent()->action; } @@ -138,32 +130,32 @@ int WorldFunctions::GetContainerItemActionCount(unsigned int objectIndex, unsign .containerChanges.items.at(itemIndex).actionCount; } -void WorldFunctions::SetBaseEventCell(const char* cellDescription) noexcept +void WorldFunctions::SetScriptEventCell(const char* cellDescription) noexcept { std::string description = cellDescription; std::smatch baseMatch; if (std::regex_match(description, baseMatch, exteriorCellPattern)) { - baseEvent->cell.mData.mFlags &= ~ESM::Cell::Interior; + scriptEvent.cell.mData.mFlags &= ~ESM::Cell::Interior; // The first sub match is the whole string, so check for a length of 3 if (baseMatch.size() == 3) { - baseEvent->cell.mData.mX = stoi(baseMatch[1].str()); - baseEvent->cell.mData.mY = stoi(baseMatch[2].str()); + scriptEvent.cell.mData.mX = stoi(baseMatch[1].str()); + scriptEvent.cell.mData.mY = stoi(baseMatch[2].str()); } } else { - baseEvent->cell.mData.mFlags |= ESM::Cell::Interior; - baseEvent->cell.mName = description; + scriptEvent.cell.mData.mFlags |= ESM::Cell::Interior; + scriptEvent.cell.mName = description; } } -void WorldFunctions::SetBaseEventAction(int action) noexcept +void WorldFunctions::SetScriptEventAction(int action) noexcept { - baseEvent->action = action; + scriptEvent.action = action; } void WorldFunctions::SetObjectRefId(const char* refId) noexcept @@ -249,7 +241,7 @@ void WorldFunctions::AddWorldObject() noexcept worldObject.pos = tempWorldObject.pos; worldObject.containerChanges.items = tempWorldObject.containerChanges.items; - baseEvent->objectChanges.objects.push_back(worldObject); + scriptEvent.objectChanges.objects.push_back(worldObject); tempWorldObject.containerChanges.items.clear(); } @@ -264,39 +256,60 @@ void WorldFunctions::AddContainerItem() noexcept tempWorldObject.containerChanges.items.push_back(containerItem); } -void WorldFunctions::SendObjectDelete() noexcept +void WorldFunctions::SendObjectDelete(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_DELETE)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_DELETE)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendObjectPlace() noexcept +void WorldFunctions::SendObjectPlace(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_PLACE)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_PLACE)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendObjectScale() noexcept +void WorldFunctions::SendObjectScale(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_SCALE)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_SCALE)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendObjectLock() noexcept +void WorldFunctions::SendObjectLock(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_LOCK)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_LOCK)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendObjectUnlock() noexcept +void WorldFunctions::SendObjectUnlock(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_UNLOCK)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_OBJECT_UNLOCK)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendDoorState() noexcept +void WorldFunctions::SendDoorState(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_DOOR_STATE)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_DOOR_STATE)->Send(&scriptEvent, player->guid); } -void WorldFunctions::SendContainer() noexcept +void WorldFunctions::SendContainer(unsigned short pid) noexcept { - mwmp::Networking::get().getWorldController()->GetPacket(ID_CONTAINER)->Send(baseEvent, baseEvent->guid); + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Networking::get().getWorldController()->GetPacket(ID_CONTAINER)->Send(&scriptEvent, player->guid); } void WorldFunctions::SetHour(unsigned short pid, double hour) noexcept diff --git a/apps/openmw-mp/Script/Functions/World.hpp b/apps/openmw-mp/Script/Functions/World.hpp index 21b5607da..b50a77f5f 100644 --- a/apps/openmw-mp/Script/Functions/World.hpp +++ b/apps/openmw-mp/Script/Functions/World.hpp @@ -2,10 +2,10 @@ #define OPENMW_WORLD_HPP #define WORLDFUNCTIONS \ - {"CreateBaseEvent", WorldFunctions::CreateBaseEvent},\ + {"ClearScriptEvent", WorldFunctions::ClearScriptEvent},\ \ {"GetObjectChangesSize", WorldFunctions::GetObjectChangesSize},\ - {"GetBaseEventAction", WorldFunctions::GetBaseEventAction},\ + {"GetLastEventAction", WorldFunctions::GetLastEventAction},\ \ {"GetObjectRefId", WorldFunctions::GetObjectRefId},\ {"GetObjectRefNumIndex", WorldFunctions::GetObjectRefNumIndex},\ @@ -28,8 +28,8 @@ {"GetContainerItemCharge", WorldFunctions::GetContainerItemCharge},\ {"GetContainerItemActionCount", WorldFunctions::GetContainerItemActionCount},\ \ - {"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\ - {"SetBaseEventAction", WorldFunctions::SetBaseEventAction},\ + {"SetScriptEventCell", WorldFunctions::SetScriptEventCell},\ + {"SetScriptEventAction", WorldFunctions::SetScriptEventAction},\ \ {"SetObjectRefId", WorldFunctions::SetObjectRefId},\ {"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\ @@ -65,10 +65,10 @@ class WorldFunctions { public: - static void CreateBaseEvent(unsigned short pid) noexcept; + static void ClearScriptEvent() noexcept; static unsigned int GetObjectChangesSize() noexcept; - static unsigned int GetBaseEventAction() noexcept; + static unsigned int GetLastEventAction() noexcept; static const char *GetObjectRefId(unsigned int i) noexcept; static int GetObjectRefNumIndex(unsigned int i) noexcept; @@ -91,8 +91,8 @@ public: static int GetContainerItemCharge(unsigned int objectIndex, unsigned int itemIndex) noexcept; static int GetContainerItemActionCount(unsigned int objectIndex, unsigned int itemIndex) noexcept; - static void SetBaseEventCell(const char* cellDescription) noexcept; - static void SetBaseEventAction(int action) noexcept; + static void SetScriptEventCell(const char* cellDescription) noexcept; + static void SetScriptEventAction(int action) noexcept; static void SetObjectRefId(const char* refId) noexcept; static void SetObjectRefNumIndex(int refNumIndex) noexcept; @@ -112,13 +112,13 @@ public: static void AddWorldObject() noexcept; static void AddContainerItem() noexcept; - static void SendObjectDelete() noexcept; - static void SendObjectPlace() noexcept; - static void SendObjectScale() noexcept; - static void SendObjectLock() noexcept; - static void SendObjectUnlock() noexcept; - static void SendDoorState() noexcept; - static void SendContainer() noexcept; + static void SendObjectDelete(unsigned short pid) noexcept; + static void SendObjectPlace(unsigned short pid) noexcept; + static void SendObjectScale(unsigned short pid) noexcept; + static void SendObjectLock(unsigned short pid) noexcept; + static void SendObjectUnlock(unsigned short pid) noexcept; + static void SendDoorState(unsigned short pid) noexcept; + static void SendContainer(unsigned short pid) noexcept; static void SetHour(unsigned short pid, double hour) noexcept; static void SetMonth(unsigned short pid, int month) noexcept;