From 4e5c8873e0040d33c9b5df60f964de2f13d73039 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Mon, 29 May 2017 06:59:05 +0300 Subject: [PATCH] [General] Use ObjectSpawn instead of ObjectPlace for actors --- apps/openmw-mp/Script/Functions/World.cpp | 23 +++---- apps/openmw-mp/Script/Functions/World.hpp | 8 +-- apps/openmw-mp/Script/ScriptFunctions.hpp | 4 +- apps/openmw/mwmp/MechanicsHelper.cpp | 4 +- apps/openmw/mwmp/WorldEvent.cpp | 62 ++++++++++++++++++- apps/openmw/mwmp/WorldEvent.hpp | 3 + .../processors/world/ProcessorObjectSpawn.hpp | 3 +- .../mwscript/transformationextensions.cpp | 17 +++-- apps/openmw/mwworld/worldimp.cpp | 4 +- components/openmw-mp/Base/BaseEvent.hpp | 1 - .../Packets/Actor/PacketActorCellChange.cpp | 2 +- .../Packets/World/PacketObjectPlace.cpp | 1 - 12 files changed, 101 insertions(+), 31 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/World.cpp b/apps/openmw-mp/Script/Functions/World.cpp index a807706a6..97264023b 100644 --- a/apps/openmw-mp/Script/Functions/World.cpp +++ b/apps/openmw-mp/Script/Functions/World.cpp @@ -119,11 +119,6 @@ double WorldFunctions::GetObjectRotZ(unsigned int i) noexcept return readEvent->worldObjects.at(i).position.rot[2]; } -bool WorldFunctions::GetObjectIsActor(unsigned int i) noexcept -{ - return readEvent->worldObjects.at(i).isActor; -} - unsigned int WorldFunctions::GetContainerChangesSize(unsigned int objectIndex) noexcept { return readEvent->worldObjects.at(objectIndex).containerItemCount; @@ -256,18 +251,24 @@ void WorldFunctions::AddContainerItem() noexcept tempContainerItem = emptyContainerItem; } -void WorldFunctions::SendObjectDelete() noexcept -{ - mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_DELETE)->setEvent(&writeEvent); - mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_DELETE)->Send(writeEvent.guid); -} - void WorldFunctions::SendObjectPlace() noexcept { mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_PLACE)->setEvent(&writeEvent); mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_PLACE)->Send(writeEvent.guid); } +void WorldFunctions::SendObjectSpawn() noexcept +{ + mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_SPAWN)->setEvent(&writeEvent); + mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_SPAWN)->Send(writeEvent.guid); +} + +void WorldFunctions::SendObjectDelete() noexcept +{ + mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_DELETE)->setEvent(&writeEvent); + mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_DELETE)->Send(writeEvent.guid); +} + void WorldFunctions::SendObjectLock() noexcept { mwmp::Networking::get().getWorldPacketController()->GetPacket(ID_OBJECT_LOCK)->setEvent(&writeEvent); diff --git a/apps/openmw-mp/Script/Functions/World.hpp b/apps/openmw-mp/Script/Functions/World.hpp index 06316d1c5..9bb683142 100644 --- a/apps/openmw-mp/Script/Functions/World.hpp +++ b/apps/openmw-mp/Script/Functions/World.hpp @@ -23,7 +23,6 @@ {"GetObjectRotX", WorldFunctions::GetObjectRotX},\ {"GetObjectRotY", WorldFunctions::GetObjectRotY},\ {"GetObjectRotZ", WorldFunctions::GetObjectRotZ},\ - {"GetObjectIsActor", WorldFunctions::GetObjectIsActor},\ \ {"GetContainerChangesSize", WorldFunctions::GetContainerChangesSize},\ {"GetContainerItemRefId", WorldFunctions::GetContainerItemRefId},\ @@ -54,8 +53,9 @@ {"AddWorldObject", WorldFunctions::AddWorldObject},\ {"AddContainerItem", WorldFunctions::AddContainerItem},\ \ - {"SendObjectDelete", WorldFunctions::SendObjectDelete},\ {"SendObjectPlace", WorldFunctions::SendObjectPlace},\ + {"SendObjectSpawn", WorldFunctions::SendObjectSpawn},\ + {"SendObjectDelete", WorldFunctions::SendObjectDelete},\ {"SendObjectLock", WorldFunctions::SendObjectLock},\ {"SendObjectTrap", WorldFunctions::SendObjectTrap},\ {"SendObjectScale", WorldFunctions::SendObjectScale},\ @@ -91,7 +91,6 @@ public: static double GetObjectRotX(unsigned int i) noexcept; static double GetObjectRotY(unsigned int i) noexcept; static double GetObjectRotZ(unsigned int i) noexcept; - static bool GetObjectIsActor(unsigned int i) noexcept; static unsigned int GetContainerChangesSize(unsigned int objectIndex) noexcept; static const char *GetContainerItemRefId(unsigned int objectIndex, unsigned int itemIndex) noexcept; @@ -122,8 +121,9 @@ public: static void AddWorldObject() noexcept; static void AddContainerItem() noexcept; - static void SendObjectDelete() noexcept; static void SendObjectPlace() noexcept; + static void SendObjectSpawn() noexcept; + static void SendObjectDelete() noexcept; static void SendObjectLock() noexcept; static void SendObjectTrap() noexcept; static void SendObjectScale() noexcept; diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index f96ffd116..7b11839b3 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -144,11 +144,11 @@ public: {"OnCellDeletion", Function()}, {"OnContainer", Function()}, {"OnDoorState", Function()}, + {"OnObjectPlace", Function()}, + {"OnObjectSpawn", Function()}, {"OnObjectDelete", Function()}, {"OnObjectLock", Function()}, - {"OnObjectPlace", Function()}, {"OnObjectScale", Function()}, - {"OnObjectSpawn", Function()}, {"OnObjectTrap", Function()}, {"OnActorList", Function()}, {"OnActorEquipment", Function()}, diff --git a/apps/openmw/mwmp/MechanicsHelper.cpp b/apps/openmw/mwmp/MechanicsHelper.cpp index d9b53b843..6ce4600a0 100644 --- a/apps/openmw/mwmp/MechanicsHelper.cpp +++ b/apps/openmw/mwmp/MechanicsHelper.cpp @@ -63,12 +63,12 @@ void MechanicsHelper::spawnLeveledCreatures(MWWorld::CellStore* cellStore) MWWorld::ManualRef manualRef(store, id); manualRef.getPtr().getCellRef().setPosition(ptr.getCellRef().getPosition()); MWWorld::Ptr placed = MWBase::Environment::get().getWorld()->placeObject(manualRef.getPtr(), ptr.getCell(), ptr.getCellRef().getPosition()); - worldEvent->addObjectPlace(placed); + worldEvent->addObjectSpawn(placed); MWBase::Environment::get().getWorld()->deleteObject(placed); } } - worldEvent->sendObjectPlace(); + worldEvent->sendObjectSpawn(); } Attack *MechanicsHelper::getLocalAttack(const MWWorld::Ptr& ptr) diff --git a/apps/openmw/mwmp/WorldEvent.cpp b/apps/openmw/mwmp/WorldEvent.cpp index b9379fb47..9b1d191af 100644 --- a/apps/openmw/mwmp/WorldEvent.cpp +++ b/apps/openmw/mwmp/WorldEvent.cpp @@ -162,6 +162,36 @@ void WorldEvent::placeObjects(MWWorld::CellStore* cellStore) } } +void WorldEvent::spawnObjects(MWWorld::CellStore* cellStore) +{ + WorldObject worldObject; + + for (unsigned int i = 0; i < worldObjectCount; i++) + { + worldObject = worldObjects.at(i); + + LOG_APPEND(Log::LOG_VERBOSE, "- cellRef: %s, %i, %i", worldObject.refId.c_str(), + worldObject.refNumIndex, worldObject.mpNum); + + MWWorld::Ptr ptrFound = cellStore->searchExact(0, worldObject.mpNum); + + // Only create this object if it doesn't already exist + if (!ptrFound) + { + MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), worldObject.refId, 1); + MWWorld::Ptr newPtr = ref.getPtr(); + + newPtr.getCellRef().setMpNum(worldObject.mpNum); + + newPtr = MWBase::Environment::get().getWorld()->placeObject(newPtr, cellStore, worldObject.position); + } + else + { + LOG_APPEND(Log::LOG_VERBOSE, "-- Object already existed!"); + } + } +} + void WorldEvent::deleteObjects(MWWorld::CellStore* cellStore) { WorldObject worldObject; @@ -506,7 +536,21 @@ void WorldEvent::addObjectPlace(const MWWorld::Ptr& ptr) // Get the real count of gold in a stack worldObject.goldValue = ptr.getCellRef().getGoldValue(); - worldObject.isActor = ptr.getClass().isActor(); + addObject(worldObject); +} + +void WorldEvent::addObjectSpawn(const MWWorld::Ptr& ptr) +{ + cell = *ptr.getCell()->getCell(); + + mwmp::WorldObject worldObject; + worldObject.refId = ptr.getCellRef().getRefId(); + worldObject.refNumIndex = ptr.getCellRef().getRefNum().mIndex; + worldObject.mpNum = 0; + + // Make sure we send the RefData position instead of the CellRef one, because that's what + // we actually see on this client + worldObject.position = ptr.getRefData().getPosition(); addObject(worldObject); } @@ -658,6 +702,22 @@ void WorldEvent::sendObjectPlace() mwmp::Main::get().getNetworking()->getWorldPacket(ID_OBJECT_PLACE)->Send(); } +void WorldEvent::sendObjectSpawn() +{ + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Sending ID_OBJECT_SPAWN about %s", cell.getDescription().c_str()); + + for (std::vector::iterator it = worldObjects.begin(); it != worldObjects.end(); ++it) + { + mwmp::WorldObject worldObject = (*it); + + LOG_APPEND(Log::LOG_VERBOSE, "- cellRef: %s-%i", + worldObject.refId.c_str(), worldObject.refNumIndex); + } + + mwmp::Main::get().getNetworking()->getWorldPacket(ID_OBJECT_SPAWN)->setEvent(this); + mwmp::Main::get().getNetworking()->getWorldPacket(ID_OBJECT_SPAWN)->Send(); +} + void WorldEvent::sendObjectDelete() { mwmp::Main::get().getNetworking()->getWorldPacket(ID_OBJECT_DELETE)->setEvent(this); diff --git a/apps/openmw/mwmp/WorldEvent.hpp b/apps/openmw/mwmp/WorldEvent.hpp index 30fec4af4..c7a674d8a 100644 --- a/apps/openmw/mwmp/WorldEvent.hpp +++ b/apps/openmw/mwmp/WorldEvent.hpp @@ -21,6 +21,7 @@ namespace mwmp void editContainers(MWWorld::CellStore* cellStore); void placeObjects(MWWorld::CellStore* cellStore); + void spawnObjects(MWWorld::CellStore* cellStore); void deleteObjects(MWWorld::CellStore* cellStore); void lockObjects(MWWorld::CellStore* cellStore); void triggerTrapObjects(MWWorld::CellStore* cellStore); @@ -39,6 +40,7 @@ namespace mwmp void playVideo(); void addObjectPlace(const MWWorld::Ptr& ptr); + void addObjectSpawn(const MWWorld::Ptr& ptr); void addObjectDelete(const MWWorld::Ptr& ptr); void addObjectLock(const MWWorld::Ptr& ptr, int lockLevel); void addObjectTrap(const MWWorld::Ptr& ptr, const ESM::Position& pos, bool isDisarmed); @@ -53,6 +55,7 @@ namespace mwmp void addScriptGlobalShort(std::string varName, int shortVal); void sendObjectPlace(); + void sendObjectSpawn(); void sendObjectDelete(); void sendObjectLock(); void sendObjectTrap(); diff --git a/apps/openmw/mwmp/processors/world/ProcessorObjectSpawn.hpp b/apps/openmw/mwmp/processors/world/ProcessorObjectSpawn.hpp index c04ca4bc4..bd00c2086 100644 --- a/apps/openmw/mwmp/processors/world/ProcessorObjectSpawn.hpp +++ b/apps/openmw/mwmp/processors/world/ProcessorObjectSpawn.hpp @@ -17,9 +17,8 @@ namespace mwmp { BaseObjectProcessor::Do(packet, event); - //event.spawnObjects(ptrCellStore); + event.spawnObjects(ptrCellStore); } - }; } diff --git a/apps/openmw/mwscript/transformationextensions.cpp b/apps/openmw/mwscript/transformationextensions.cpp index 6c10f90dc..9e10b1eeb 100644 --- a/apps/openmw/mwscript/transformationextensions.cpp +++ b/apps/openmw/mwscript/transformationextensions.cpp @@ -554,13 +554,22 @@ namespace MWScript /* Start of tes3mp addition - Send an ID_OBJECT_PLACE packet every time an object is placed in the world - through a script + Send an ID_OBJECT_PLACE or ID_OBJECT_SPAWN packet every time an object is placed + in the world through a script */ mwmp::WorldEvent *worldEvent = mwmp::Main::get().getNetworking()->getWorldEvent(); worldEvent->reset(); - worldEvent->addObjectPlace(ptr); - worldEvent->sendObjectPlace(); + + if (ptr.getClass().isActor()) + { + worldEvent->addObjectSpawn(ptr); + worldEvent->sendObjectSpawn(); + } + else + { + worldEvent->addObjectPlace(ptr); + worldEvent->sendObjectPlace(); + } /* End of tes3mp addition */ diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 784efdbab..68474b359 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -3402,8 +3402,8 @@ namespace MWWorld mwmp::WorldEvent *worldEvent = mwmp::Main::get().getNetworking()->getWorldEvent(); worldEvent->reset(); - worldEvent->addObjectPlace(ptr); - worldEvent->sendObjectPlace(); + worldEvent->addObjectSpawn(ptr); + worldEvent->sendObjectSpawn(); MWBase::Environment::get().getWorld()->deleteObject(ptr); /* diff --git a/components/openmw-mp/Base/BaseEvent.hpp b/components/openmw-mp/Base/BaseEvent.hpp index aaab985d4..9624992c8 100644 --- a/components/openmw-mp/Base/BaseEvent.hpp +++ b/components/openmw-mp/Base/BaseEvent.hpp @@ -47,7 +47,6 @@ namespace mwmp float floatVal; std::string varName; - bool isActor; bool isDisarmed; std::vector containerItems; diff --git a/components/openmw-mp/Packets/Actor/PacketActorCellChange.cpp b/components/openmw-mp/Packets/Actor/PacketActorCellChange.cpp index 02d9bd234..2d187ca6f 100644 --- a/components/openmw-mp/Packets/Actor/PacketActorCellChange.cpp +++ b/components/openmw-mp/Packets/Actor/PacketActorCellChange.cpp @@ -36,7 +36,7 @@ void PacketActorCellChange::Packet(RakNet::BitStream *bs, bool send) RW(actor.cell.mName, send, 1); RW(actor.position, send, 1); - RW(actor.direction, send, 1); + RW(actor.direction.pos, send, 1); if (!send) { diff --git a/components/openmw-mp/Packets/World/PacketObjectPlace.cpp b/components/openmw-mp/Packets/World/PacketObjectPlace.cpp index 5a6eb0e1b..4c2ac62e1 100644 --- a/components/openmw-mp/Packets/World/PacketObjectPlace.cpp +++ b/components/openmw-mp/Packets/World/PacketObjectPlace.cpp @@ -40,7 +40,6 @@ void PacketObjectPlace::Packet(RakNet::BitStream *bs, bool send) RW(worldObject.charge, send); RW(worldObject.goldValue, send); RW(worldObject.position, send); - RW(worldObject.isActor, send); if (!send) {