Threaded the barriers through the networking system

This commit is contained in:
2016-04-03 22:32:39 +10:00
parent 235f3b57e0
commit 7b9c016082
10 changed files with 181 additions and 2 deletions
@@ -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));
}
}
@@ -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);
+1
View File
@@ -22,6 +22,7 @@
#pragma once #pragma once
#include "serial_packet_base.hpp" #include "serial_packet_base.hpp"
#include "barrier_packet.hpp"
#include "character_packet.hpp" #include "character_packet.hpp"
#include "client_packet.hpp" #include "client_packet.hpp"
#include "creature_packet.hpp" #include "creature_packet.hpp"
+15 -1
View File
@@ -142,6 +142,20 @@ enum class SerialPacketType {
FORMAT_END_CREATURE = 599, FORMAT_END_CREATURE = 599,
//-------------------------
//BarrierPacket
// barrier index,
// bounds,
// roomIndex, origin, motion
// status
//-------------------------
FORMAT_COMBAT = 700,
BARRIER_UPDATE = 701,
FORMAT_END_COMBAT = 799,
//------------------------- //-------------------------
//TextPacket //TextPacket
// name, text // name, text
@@ -169,5 +183,5 @@ enum class SerialPacketType {
//not used //not used
//------------------------- //-------------------------
LAST = 700 LAST = 800
}; };
+9
View File
@@ -22,6 +22,7 @@
#include "serial_utility.hpp" #include "serial_utility.hpp"
//packet types //packet types
#include "barrier_packet.hpp"
#include "character_packet.hpp" #include "character_packet.hpp"
#include "client_packet.hpp" #include "client_packet.hpp"
#include "creature_packet.hpp" #include "creature_packet.hpp"
@@ -69,6 +70,10 @@ void serializePacket(void* buffer, SerialPacketBase* packet) {
serializeCreature(buffer, static_cast<CreaturePacket*>(packet)); serializeCreature(buffer, static_cast<CreaturePacket*>(packet));
} }
if (BOUNDS(packet->type, SerialPacketType::FORMAT_COMBAT, SerialPacketType::FORMAT_END_COMBAT)) {
serializeBarrier(buffer, static_cast<BarrierPacket*>(packet));
}
if (BOUNDS(packet->type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) { if (BOUNDS(packet->type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
serializeText(buffer, static_cast<TextPacket*>(packet)); serializeText(buffer, static_cast<TextPacket*>(packet));
} }
@@ -99,6 +104,10 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) {
deserializeCreature(buffer, static_cast<CreaturePacket*>(packet)); deserializeCreature(buffer, static_cast<CreaturePacket*>(packet));
} }
if (BOUNDS(type, SerialPacketType::FORMAT_COMBAT, SerialPacketType::FORMAT_END_COMBAT)) {
deserializeBarrier(buffer, static_cast<BarrierPacket*>(packet));
}
if (BOUNDS(type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) { if (BOUNDS(type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
deserializeText(buffer, static_cast<TextPacket*>(packet)); deserializeText(buffer, static_cast<TextPacket*>(packet));
} }
+6 -1
View File
@@ -21,12 +21,13 @@
*/ */
#include "barrier_data.hpp" #include "barrier_data.hpp"
#include <cstring>
#include <sstream> #include <sstream>
BarrierData::BarrierData(int i): BarrierData::BarrierData(int i):
Entity::Entity("barrier") Entity::Entity("barrier")
{ {
// memcpy(status, 0, sizeof(int) * 8);
} }
BarrierData::~BarrierData() { BarrierData::~BarrierData() {
@@ -81,3 +82,7 @@ int BarrierData::SetInstanceIndex(int i) {
int BarrierData::GetInstanceIndex() const { int BarrierData::GetInstanceIndex() const {
return instanceIndex; return instanceIndex;
} }
int* BarrierData::GetStatusArray() {
return status;
}
+4
View File
@@ -44,9 +44,13 @@ public:
int SetInstanceIndex(int i); int SetInstanceIndex(int i);
int GetInstanceIndex() const; int GetInstanceIndex() const;
int* GetStatusArray();
private: private:
int scriptRef = LUA_NOREF; int scriptRef = LUA_NOREF;
std::map<std::string, std::string> tags; std::map<std::string, std::string> tags;
int instanceIndex = -1; int instanceIndex = -1;
int status[8];
}; };
+13
View File
@@ -115,6 +115,19 @@ void RoomData::RunFrame() {
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS); pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS);
} }
//a list of barriers that need to be updated client-side
std::list< std::pair<const int, BarrierData*>> 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<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS);
}
//TODO: creature/character collisions //TODO: creature/character collisions
} }
+10
View File
@@ -177,6 +177,16 @@ void copyCreatureToPacket(CreaturePacket* const packet, CreatureData* const crea
packet->bounds = creatureData->GetBounds(); 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) { void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
//get the character object //get the character object
CharacterData* character = CharacterManager::GetSingleton().Find(characterIndex); CharacterData* character = CharacterManager::GetSingleton().Find(characterIndex);
+3
View File
@@ -21,6 +21,7 @@
*/ */
#pragma once #pragma once
#include "barrier_data.hpp"
#include "character_data.hpp" #include "character_data.hpp"
#include "creature_data.hpp" #include "creature_data.hpp"
#include "serial_packet.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 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(int characterIndex, int newRoomIndex);
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex); void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);