1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 00:49:54 +00:00
openmw-tes3mp/apps/openmw-mp/Script/Functions/World.cpp
David Cernat 5f6ddcfc59 [General] Rework container sync to prevent item duping
A main priority in TES3MP development is to avoid making major changes to OpenMW code, so as to avoid merge conflicts in the future. Whenever avoiding potential conflicts seems especially difficult for the proper implementation of a particular multiplayer feature, that multiplayer feature is often put off until later or partially implemented with the intent of being revisited in the future.

Container sync is the perfect example. Previously, the OpenMW code for container actions was kept exactly as it was, with clients unilaterally accepting their own container changes as per singleplayer-specific code, with only the addition that clients sent container packets every time they made a change in a container, packets which were then forwarded unquestioningly by the server to other players. This meant that two players clicking on the same item in a container at the same time both managed to take it, thus duplicating the item.

Immediately after the packets were already forwarded, server scripts were able to check for incorrect changes, such as the removal of more items than should have existed in a container, but they had to send their own packets that attempted to fix what had already been accepted on the initial client and then forwarded to all clients, which was quite onerous in some scenarios, such as when a player on a slow connection immediately dropped items in the world after taking them from a container (which is why the default TES3MP serverside scripts made no attempt at sending corrective packets at all, preferring to expect the matter to be solved in a later C++ implementation).

This commit fixes item duping in containers by preventing container actions from initially running on clients and by ending the automatic forwarding of container packets by the server. Instead, clients now send container packets that act as requests for container actions, and serverside scripts have to forward these requests themselves. In other words, without a matching Container event in the server's Lua scripts, players are completely unable to affect containers for themselves or for others.

To forward a received Container packet, the following line must be used in a Container event in the Lua scripts:

tes3mp.SendContainer(true, true)

When an invalid action count is used in a container request, the serverside scripts can amend it using the following new function:

tes3mp.SetReceivedContainerItemActionCount(objectIndex, itemIndex, actionCount)

Thus, the serverside scripts are able to allow only container actions that are correct based on their own recorded contents for that container.

The OpenMW code allowing unilateral container actions in mwgui/container.cpp is now prevented from executing. When a player's container request is returned to them, code in mwmp/WorldEvent.cpp simulates those container actions instead.
2018-03-26 19:27:36 +03:00

458 lines
12 KiB
C++

#include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Base/BaseEvent.hpp>
#include <apps/openmw-mp/Networking.hpp>
#include <apps/openmw-mp/Player.hpp>
#include <apps/openmw-mp/Utils.hpp>
#include <apps/openmw-mp/Script/ScriptFunctions.hpp>
#include "World.hpp"
using namespace mwmp;
BaseEvent *readEvent;
BaseEvent writeEvent;
WorldObject tempWorldObject;
const WorldObject emptyWorldObject = {};
ContainerItem tempContainerItem;
const ContainerItem emptyContainerItem = {};
void WorldFunctions::ReadLastEvent() noexcept
{
readEvent = mwmp::Networking::getPtr()->getLastEvent();
}
void WorldFunctions::InitializeEvent(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, );
writeEvent.cell.blank();
writeEvent.worldObjects.clear();
writeEvent.guid = player->guid;
}
unsigned int WorldFunctions::GetObjectChangesSize() noexcept
{
return readEvent->worldObjectCount;
}
unsigned char WorldFunctions::GetEventAction() noexcept
{
return readEvent->action;
}
unsigned char WorldFunctions::GetEventContainerSubAction() noexcept
{
return readEvent->containerSubAction;
}
const char *WorldFunctions::GetObjectRefId(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).refId.c_str();
}
int WorldFunctions::GetObjectRefNumIndex(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).refNumIndex;
}
int WorldFunctions::GetObjectMpNum(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).mpNum;
}
int WorldFunctions::GetObjectCount(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).count;
}
int WorldFunctions::GetObjectCharge(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).charge;
}
double WorldFunctions::GetObjectEnchantmentCharge(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).enchantmentCharge;
}
int WorldFunctions::GetObjectGoldValue(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).goldValue;
}
double WorldFunctions::GetObjectScale(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).scale;
}
bool WorldFunctions::GetObjectState(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).objectState;
}
int WorldFunctions::GetObjectDoorState(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).doorState;
}
int WorldFunctions::GetObjectLockLevel(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).lockLevel;
}
double WorldFunctions::GetObjectPosX(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.pos[0];
}
double WorldFunctions::GetObjectPosY(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.pos[1];
}
double WorldFunctions::GetObjectPosZ(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.pos[2];
}
double WorldFunctions::GetObjectRotX(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.rot[0];
}
double WorldFunctions::GetObjectRotY(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.rot[1];
}
double WorldFunctions::GetObjectRotZ(unsigned int i) noexcept
{
return readEvent->worldObjects.at(i).position.rot[2];
}
unsigned int WorldFunctions::GetContainerChangesSize(unsigned int objectIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex).containerItemCount;
}
const char *WorldFunctions::GetContainerItemRefId(unsigned int objectIndex, unsigned int itemIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex)
.containerItems.at(itemIndex).refId.c_str();
}
int WorldFunctions::GetContainerItemCount(unsigned int objectIndex, unsigned int itemIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex)
.containerItems.at(itemIndex).count;
}
int WorldFunctions::GetContainerItemCharge(unsigned int objectIndex, unsigned int itemIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex)
.containerItems.at(itemIndex).charge;
}
double WorldFunctions::GetContainerItemEnchantmentCharge(unsigned int objectIndex, unsigned int itemIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex)
.containerItems.at(itemIndex).enchantmentCharge;
}
int WorldFunctions::GetContainerItemActionCount(unsigned int objectIndex, unsigned int itemIndex) noexcept
{
return readEvent->worldObjects.at(objectIndex)
.containerItems.at(itemIndex).actionCount;
}
void WorldFunctions::SetEventCell(const char* cellDescription) noexcept
{
writeEvent.cell = Utils::getCellFromDescription(cellDescription);
}
void WorldFunctions::SetEventAction(unsigned char action) noexcept
{
writeEvent.action = action;
}
void WorldFunctions::SetEventConsoleCommand(const char* consoleCommand) noexcept
{
writeEvent.consoleCommand = consoleCommand;
}
void WorldFunctions::SetObjectRefId(const char* refId) noexcept
{
tempWorldObject.refId = refId;
}
void WorldFunctions::SetObjectRefNumIndex(int refNumIndex) noexcept
{
tempWorldObject.refNumIndex = refNumIndex;
}
void WorldFunctions::SetObjectMpNum(int mpNum) noexcept
{
tempWorldObject.mpNum = mpNum;
}
void WorldFunctions::SetObjectCount(int count) noexcept
{
tempWorldObject.count = count;
}
void WorldFunctions::SetObjectCharge(int charge) noexcept
{
tempWorldObject.charge = charge;
}
void WorldFunctions::SetObjectEnchantmentCharge(double enchantmentCharge) noexcept
{
tempWorldObject.enchantmentCharge = enchantmentCharge;
}
void WorldFunctions::SetObjectGoldValue(int goldValue) noexcept
{
tempWorldObject.goldValue = goldValue;
}
void WorldFunctions::SetObjectScale(double scale) noexcept
{
tempWorldObject.scale = scale;
}
void WorldFunctions::SetObjectState(bool objectState) noexcept
{
tempWorldObject.objectState = objectState;
}
void WorldFunctions::SetObjectDoorState(int doorState) noexcept
{
tempWorldObject.doorState = doorState;
}
void WorldFunctions::SetObjectLockLevel(int lockLevel) noexcept
{
tempWorldObject.lockLevel = lockLevel;
}
void WorldFunctions::SetObjectDisarmState(bool disarmState) noexcept
{
tempWorldObject.isDisarmed = disarmState;
}
void WorldFunctions::SetObjectMasterState(bool masterState) noexcept
{
tempWorldObject.hasMaster = masterState;
}
void WorldFunctions::SetObjectPosition(double x, double y, double z) noexcept
{
tempWorldObject.position.pos[0] = x;
tempWorldObject.position.pos[1] = y;
tempWorldObject.position.pos[2] = z;
}
void WorldFunctions::SetObjectRotation(double x, double y, double z) noexcept
{
tempWorldObject.position.rot[0] = x;
tempWorldObject.position.rot[1] = y;
tempWorldObject.position.rot[2] = z;
}
void WorldFunctions::SetPlayerAsObject(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, );
tempWorldObject.guid = player->guid;
tempWorldObject.isPlayer = true;
}
void WorldFunctions::SetContainerItemRefId(const char* refId) noexcept
{
tempContainerItem.refId = refId;
}
void WorldFunctions::SetContainerItemCount(int count) noexcept
{
tempContainerItem.count = count;
}
void WorldFunctions::SetContainerItemCharge(int charge) noexcept
{
tempContainerItem.charge = charge;
}
void WorldFunctions::SetContainerItemEnchantmentCharge(double enchantmentCharge) noexcept
{
tempContainerItem.enchantmentCharge = enchantmentCharge;
}
void WorldFunctions::SetReceivedContainerItemActionCount(unsigned int objectIndex, unsigned int itemIndex, int actionCount) noexcept
{
readEvent->worldObjects.at(objectIndex).containerItems.at(itemIndex).actionCount = actionCount;
}
void WorldFunctions::AddWorldObject() noexcept
{
tempWorldObject.droppedByPlayer = false;
writeEvent.worldObjects.push_back(tempWorldObject);
tempWorldObject = emptyWorldObject;
}
void WorldFunctions::AddContainerItem() noexcept
{
tempWorldObject.containerItems.push_back(tempContainerItem);
tempContainerItem = emptyContainerItem;
}
void WorldFunctions::SendObjectPlace(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_PLACE);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectSpawn(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_SPAWN);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectDelete(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_DELETE);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectLock(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_LOCK);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectTrap(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_TRAP);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectScale(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_SCALE);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendObjectState(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_STATE);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendDoorState(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_DOOR_STATE);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendContainer(bool broadcast, bool useLastReadEvent) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_CONTAINER);
if (useLastReadEvent)
packet->setEvent(readEvent);
else
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SendConsoleCommand(bool broadcast) noexcept
{
mwmp::WorldPacket *packet = mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_CONSOLE_COMMAND);
packet->setEvent(&writeEvent);
packet->Send(false);
if (broadcast)
packet->Send(true);
}
void WorldFunctions::SetHour(unsigned short pid, double hour) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
player->hour = hour;
player->month = -1;
player->day = -1;
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->setPlayer(player);
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->Send(false);
}
void WorldFunctions::SetMonth(unsigned short pid, int month) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
player->hour = -1;
player->month = month;
player->day = -1;
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->setPlayer(player);
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->Send(false);
}
void WorldFunctions::SetDay(unsigned short pid, int day) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
player->hour = -1;
player->month = -1;
player->day = day;
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->setPlayer(player);
mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_TIME)->Send(false);
}