diff --git a/common/network/packet_types/barrier_packet.cpp b/common/network/packet_types/barrier_packet.cpp new file mode 100644 index 0000000..c410b1e --- /dev/null +++ b/common/network/packet_types/barrier_packet.cpp @@ -0,0 +1,76 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "barrier_packet.hpp" + +#include "serial_utility.hpp" + +void serializeBarrier(void* buffer, BarrierPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //identify the barrier + serialCopy(&buffer, &packet->barrierIndex, sizeof(int)); + + //bounds + serialCopy(&buffer, &packet->bounds.x, sizeof(int)); + serialCopy(&buffer, &packet->bounds.y, sizeof(int)); + serialCopy(&buffer, &packet->bounds.w, sizeof(int)); + serialCopy(&buffer, &packet->bounds.h, sizeof(int)); + + + //location + serialCopy(&buffer, &packet->roomIndex, sizeof(int)); + serialCopy(&buffer, &packet->origin.x, sizeof(double)); + serialCopy(&buffer, &packet->origin.y, sizeof(double)); + serialCopy(&buffer, &packet->motion.x, sizeof(double)); + serialCopy(&buffer, &packet->motion.y, sizeof(double)); + + //graphical data + for (int i = 0; i < 8; i++) { + serialCopy(&buffer, &packet->status[i], sizeof(int)); + } +} + +void deserializeBarrier(void* buffer, BarrierPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //identify the barrier + deserialCopy(&buffer, &packet->barrierIndex, sizeof(int)); + + //bounds + deserialCopy(&buffer, &packet->bounds.x, sizeof(int)); + deserialCopy(&buffer, &packet->bounds.y, sizeof(int)); + deserialCopy(&buffer, &packet->bounds.w, sizeof(int)); + deserialCopy(&buffer, &packet->bounds.h, sizeof(int)); + + + //location + deserialCopy(&buffer, &packet->roomIndex, sizeof(int)); + deserialCopy(&buffer, &packet->origin.x, sizeof(double)); + deserialCopy(&buffer, &packet->origin.y, sizeof(double)); + deserialCopy(&buffer, &packet->motion.x, sizeof(double)); + deserialCopy(&buffer, &packet->motion.y, sizeof(double)); + + //graphical data + for (int i = 0; i < 8; i++) { + deserialCopy(&buffer, &packet->status[i], sizeof(int)); + } +} diff --git a/common/network/packet_types/barrier_packet.hpp b/common/network/packet_types/barrier_packet.hpp new file mode 100644 index 0000000..a2a367a --- /dev/null +++ b/common/network/packet_types/barrier_packet.hpp @@ -0,0 +1,44 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "serial_packet_base.hpp" + +#include "bounding_box.hpp" +#include "vector2.hpp" + +struct BarrierPacket : SerialPacketBase { + //identify the barrier + int barrierIndex; + BoundingBox bounds; + + //location + int roomIndex; + Vector2 origin; + Vector2 motion; + + //graphical data: 0 blank, 1 green, 2 red + int status[8]; +}; + +void serializeBarrier(void* buffer, BarrierPacket* packet); +void deserializeBarrier(void* buffer, BarrierPacket* packet); diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index c6b07fa..9b23f20 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -22,6 +22,7 @@ #pragma once #include "serial_packet_base.hpp" +#include "barrier_packet.hpp" #include "character_packet.hpp" #include "client_packet.hpp" #include "creature_packet.hpp" diff --git a/common/network/serial_packet_type.hpp b/common/network/serial_packet_type.hpp index 103638e..65171f5 100644 --- a/common/network/serial_packet_type.hpp +++ b/common/network/serial_packet_type.hpp @@ -142,6 +142,20 @@ enum class SerialPacketType { FORMAT_END_CREATURE = 599, + //------------------------- + //BarrierPacket + // barrier index, + // bounds, + // roomIndex, origin, motion + // status + //------------------------- + + FORMAT_COMBAT = 700, + + BARRIER_UPDATE = 701, + + FORMAT_END_COMBAT = 799, + //------------------------- //TextPacket // name, text @@ -169,5 +183,5 @@ enum class SerialPacketType { //not used //------------------------- - LAST = 700 + LAST = 800 }; diff --git a/common/network/serial_utility.cpp b/common/network/serial_utility.cpp index 1a546a6..79a86c3 100644 --- a/common/network/serial_utility.cpp +++ b/common/network/serial_utility.cpp @@ -22,6 +22,7 @@ #include "serial_utility.hpp" //packet types +#include "barrier_packet.hpp" #include "character_packet.hpp" #include "client_packet.hpp" #include "creature_packet.hpp" @@ -69,6 +70,10 @@ void serializePacket(void* buffer, SerialPacketBase* packet) { serializeCreature(buffer, static_cast(packet)); } + if (BOUNDS(packet->type, SerialPacketType::FORMAT_COMBAT, SerialPacketType::FORMAT_END_COMBAT)) { + serializeBarrier(buffer, static_cast(packet)); + } + if (BOUNDS(packet->type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) { serializeText(buffer, static_cast(packet)); } @@ -99,6 +104,10 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) { deserializeCreature(buffer, static_cast(packet)); } + if (BOUNDS(type, SerialPacketType::FORMAT_COMBAT, SerialPacketType::FORMAT_END_COMBAT)) { + deserializeBarrier(buffer, static_cast(packet)); + } + if (BOUNDS(type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) { deserializeText(buffer, static_cast(packet)); } diff --git a/server/combat/barrier_data.cpp b/server/combat/barrier_data.cpp index 3282b0d..d07662c 100644 --- a/server/combat/barrier_data.cpp +++ b/server/combat/barrier_data.cpp @@ -21,12 +21,13 @@ */ #include "barrier_data.hpp" +#include #include BarrierData::BarrierData(int i): Entity::Entity("barrier") { - // + memcpy(status, 0, sizeof(int) * 8); } BarrierData::~BarrierData() { @@ -81,3 +82,7 @@ int BarrierData::SetInstanceIndex(int i) { int BarrierData::GetInstanceIndex() const { return instanceIndex; } + +int* BarrierData::GetStatusArray() { + return status; +} \ No newline at end of file diff --git a/server/combat/barrier_data.hpp b/server/combat/barrier_data.hpp index ab2b38a..b534906 100644 --- a/server/combat/barrier_data.hpp +++ b/server/combat/barrier_data.hpp @@ -44,9 +44,13 @@ public: int SetInstanceIndex(int i); int GetInstanceIndex() const; + int* GetStatusArray(); + private: int scriptRef = LUA_NOREF; std::map tags; int instanceIndex = -1; + + int status[8]; }; \ No newline at end of file diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 6a22c45..5e85b20 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -115,6 +115,19 @@ void RoomData::RunFrame() { pumpPacketProximity(reinterpret_cast(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS); } + //a list of barriers that need to be updated client-side + std::list< std::pair> barrierList; + barrierMgr.Update(&barrierList); + + //send the updates + for (auto& it : barrierList) { + BarrierPacket packet; + copyBarrierToPacket(&packet, it.second, it.first); + packet.type = SerialPacketType::BARRIER_UPDATE; + packet.roomIndex = roomIndex; + pumpPacketProximity(reinterpret_cast(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS); + } + //TODO: creature/character collisions } diff --git a/server/server_utilities.cpp b/server/server_utilities.cpp index 36ddc63..e21fe05 100644 --- a/server/server_utilities.cpp +++ b/server/server_utilities.cpp @@ -177,6 +177,16 @@ void copyCreatureToPacket(CreaturePacket* const packet, CreatureData* const crea packet->bounds = creatureData->GetBounds(); } +void copyBarrierToPacket(BarrierPacket* const packet, BarrierData* const barrierData, int barrierIndex) { + packet->barrierIndex = barrierIndex; + packet->roomIndex = barrierData->GetRoomIndex(); + packet->origin = barrierData->GetOrigin(); + packet->motion = barrierData->GetMotion(); + packet->bounds = barrierData->GetBounds(); + + memcpy(barrierData->GetStatusArray(), &packet->status, sizeof(int) * 8); +} + void pumpAndChangeRooms(int characterIndex, int newRoomIndex) { //get the character object CharacterData* character = CharacterManager::GetSingleton().Find(characterIndex); diff --git a/server/server_utilities.hpp b/server/server_utilities.hpp index a701f8f..eb49e25 100644 --- a/server/server_utilities.hpp +++ b/server/server_utilities.hpp @@ -21,6 +21,7 @@ */ #pragma once +#include "barrier_data.hpp" #include "character_data.hpp" #include "creature_data.hpp" #include "serial_packet.hpp" @@ -38,6 +39,8 @@ void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const c void copyCreatureToPacket(CreaturePacket* const packet, CreatureData* const creatureData, int creatureIndex); +void copyBarrierToPacket(BarrierPacket* const packet, BarrierData* const barrierData, int barrierIndex); + void pumpAndChangeRooms(int characterIndex, int newRoomIndex); void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);