Compare commits

...

3 Commits

Author SHA1 Message Date
Kayne Ruse ab6207d4f3 Barriers are working corretly, but are way too slow 2016-04-14 12:43:00 +10:00
Kayne Ruse 1267b30806 Creatures are replaced with barriers 2016-04-14 04:45:30 +10:00
Kayne Ruse 5d217d7cf9 CreatureManager now uses Tuples 2016-04-14 04:19:59 +10:00
10 changed files with 149 additions and 59 deletions
+3
View File
@@ -740,16 +740,19 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) {
memset(&characterPacket, 0, MAX_PACKET_SIZE);
characterPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS;
characterPacket.roomIndex = roomIndex;
characterPacket.origin = localCharacter->GetOrigin();
network.SendTo(Channels::SERVER, &characterPacket);
CreaturePacket creaturePacket;
creaturePacket.type = SerialPacketType::QUERY_CREATURE_EXISTS;
creaturePacket.roomIndex = roomIndex;
creaturePacket.origin = localCharacter->GetOrigin();
network.SendTo(Channels::SERVER, &creaturePacket);
BarrierPacket barrierPacket;
barrierPacket.type = SerialPacketType::QUERY_BARRIER_EXISTS;
barrierPacket.roomIndex = roomIndex;
barrierPacket.origin = localCharacter->GetOrigin();
network.SendTo(Channels::SERVER, &barrierPacket);
}
+51 -4
View File
@@ -23,6 +23,8 @@
#include "lua_utilities.hpp"
#include "barrier_defines.hpp"
BarrierManager::BarrierManager() {
//EMPTY
}
@@ -32,12 +34,57 @@ BarrierManager::~BarrierManager() {
}
//arg: a list of barriers to be updated in the clients
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList) {
int ret;
void BarrierManager::Update(
std::list<std::tuple<const int, BarrierData*, int>>* barrierList,
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
)
{
//for each given creature, if a collision was detected, make a new barrier
for (auto& it : *creatureList) {
if (std::get<2>(it) & 2) {
int index = Create(-1); //instance from creature index?
BarrierData* barrierData = Find(index);
barrierData->SetOrigin({
(CREATURE_BOUNDS_WIDTH - BARRIER_BOUNDS_WIDTH) / 2 + std::get<1>(it)->GetOrigin().x,
(CREATURE_BOUNDS_HEIGHT - BARRIER_BOUNDS_HEIGHT) / 2 + std::get<1>(it)->GetOrigin().y
});
}
}
//TODO: merge barriers
//TODO: absorb creatures into existing barriers
//update the barriers
//TODO: how to delete the barriers?
int ret; //0 = no action, ret&1 = update clients, ret&2 = collision detected
for (auto& it : elementMap) {
ret = it.second.Update(lua);
//normal update
ret = it.second.Update(lua) ? 1 : 0;
//check for collision with a character
BoundingBox barrierBox = it.second.GetRealBounds();
for (auto& it : *characterList) {
if (barrierBox.CheckOverlap(it->GetRealBounds())) {
//this will need updating
ret |= 2;
}
//TODO: absorb characters
}
if (ret) {
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second));
//push to the return list
barrierList->push_back(std::make_tuple(it.first, &it.second, ret));
}
}
}
void BarrierManager::Cleanup(std::list<std::tuple<const int, BarrierData*, int>>* barrierList) {
//unload the given barrier objects
for (auto& it : *barrierList) {
if (std::get<2>(it) & 4) {
Unload(std::get<0>(it));
}
}
}
+9 -1
View File
@@ -22,6 +22,8 @@
#pragma once
#include "barrier_data.hpp"
#include "character_data.hpp"
#include "creature_data.hpp"
#include "lua.hpp"
#include "sqlite3.h"
@@ -29,6 +31,7 @@
#include <algorithm>
#include <list>
#include <map>
#include <tuple>
class BarrierManager {
public:
@@ -36,7 +39,12 @@ public:
~BarrierManager();
//common public methods
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList);
void Update(
std::list<std::tuple<const int, BarrierData*, int>>* barrierList,
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
);
void Cleanup(std::list<std::tuple<const int, BarrierData*, int>>* barrierList);
int Create(int instanceIndex);
void Unload(int uid);
+31 -4
View File
@@ -32,12 +32,39 @@ CreatureManager::~CreatureManager() {
}
//arg: a list of creatures to be updated in the clients
void CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList) {
int ret;
void CreatureManager::Update(
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
)
{
//for each creature
int ret; //0 = no action, ret&1 = update clients, ret&2 = collision detected
for (auto& it : elementMap) {
ret = it.second.Update(lua);
//normal update
ret = it.second.Update(lua) ? 1 : 0;
//check for collision with a character
BoundingBox creatureBox = it.second.GetRealBounds();
for (auto& it : *characterList) {
if (creatureBox.CheckOverlap(it->GetRealBounds())) {
//this will need updating
ret |= 2;
break;
}
}
if (ret) {
creatureList->push_back(std::pair<const int, CreatureData*>(it.first, &it.second));
//push to the return list
creatureList->push_back(std::make_tuple(it.first, &it.second, ret));
}
}
}
void CreatureManager::Cleanup(std::list<std::tuple<const int, CreatureData*, int>>* creatureList) {
//unload the given creature objects
for (auto& it : *creatureList) {
if (std::get<2>(it) & 2) {
Unload(std::get<0>(it));
}
}
}
+7 -1
View File
@@ -21,6 +21,7 @@
*/
#pragma once
#include "character_data.hpp"
#include "creature_data.hpp"
#include "lua.hpp"
@@ -30,6 +31,7 @@
#include <list>
#include <map>
#include <string>
#include <tuple>
class CreatureManager {
public:
@@ -37,7 +39,11 @@ public:
~CreatureManager();
//common public methods
void Update(std::list<std::pair<const int, CreatureData*>>* creatureList);
void Update(
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
);
void Cleanup(std::list<std::tuple<const int, CreatureData*, int>>* creatureList);
int Create(std::string avatar, int scriptRef);
void Unload(int uid);
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. .. ../entities ../../common/gameplay ../../common/utilities
INCLUDES+=. .. ../characters ../entities ../../common/gameplay ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+4
View File
@@ -63,6 +63,10 @@ BoundingBox Entity::GetBounds() const {
return bounds;
}
BoundingBox Entity::GetRealBounds() const {
return bounds + origin;
}
const char* Entity::GetType() const {
return type;
}
+1
View File
@@ -41,6 +41,7 @@ public:
Vector2 GetOrigin() const;
Vector2 GetMotion() const;
BoundingBox GetBounds() const;
BoundingBox GetRealBounds() const;
const char* GetType() const;
+41 -47
View File
@@ -30,6 +30,7 @@
#include <iostream>
#include <stack>
#include <stdexcept>
#include <tuple>
//TODO: (9) character collisions should be preformed client-side
void RoomData::RunFrame() {
@@ -48,15 +49,51 @@ void RoomData::RunFrame() {
}
//lists of non-character entities that need updating client-side
std::list<std::pair<const int, CreatureData*>> creatureList;
std::list<std::pair<const int, BarrierData*>> barrierList;
//types are index, ptr, action (0 = update, 1 = unload)
std::list<std::tuple<const int, CreatureData*, int>> creatureList;
std::list<std::tuple<const int, BarrierData*, int>> barrierList;
//update the entities in the room
for (auto& it : characterList) {
it->Update();
}
creatureMgr.Update(&creatureList);
barrierMgr.Update(&barrierList);
creatureMgr.Update(&creatureList, &characterList);
barrierMgr.Update(&barrierList, &creatureList, &characterList);
//send the creature updates
for (auto& it : creatureList) {
CreaturePacket packet;
copyCreatureToPacket(&packet, std::get<1>(it), std::get<0>(it));
//interpret the event
if (std::get<2>(it) & 1) {
packet.type = SerialPacketType::CREATURE_UPDATE;
}
if (std::get<2>(it) & 2) {
packet.type = SerialPacketType::CREATURE_UNLOAD;
}
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, std::get<1>(it)->GetOrigin(), INFLUENCE_RADIUS);
}
//send the barrier updates
for (auto& it : barrierList) {
BarrierPacket packet;
copyBarrierToPacket(&packet, std::get<1>(it), std::get<0>(it));
//interpret the event
if (std::get<2>(it) & 1 || std::get<2>(it) & 2) {
packet.type = SerialPacketType::BARRIER_UPDATE;
}
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, std::get<1>(it)->GetOrigin(), INFLUENCE_RADIUS);
}
//cleanup the lists
creatureMgr.Cleanup(&creatureList);
barrierMgr.Cleanup(&barrierList);
//build a list of entities for use with the triggers
std::stack<Entity*> entityStack;
@@ -66,49 +103,6 @@ void RoomData::RunFrame() {
//Compare the triggers to the entities, using their real hitboxes
triggerMgr.Compare(entityStack);
//Creature/character collisions, O(m*n)
for (auto characterIt : characterList) {
BoundingBox characterBox = characterIt->GetBounds() + characterIt->GetOrigin();
for (auto creatureIt : *creatureMgr.GetContainer()) {
BoundingBox creatureBox = creatureIt.second.GetBounds() + creatureIt.second.GetOrigin();
if (characterBox.CheckOverlap(creatureBox)) {
int barrierIndex = barrierMgr.Create(-1);
BarrierData* barrierData = barrierMgr.Find(barrierIndex);
barrierData->SetRoomIndex(roomIndex);
barrierData->SetOrigin({
(CREATURE_BOUNDS_WIDTH - BARRIER_BOUNDS_WIDTH) / 2 + creatureBox.x,
(CREATURE_BOUNDS_HEIGHT - BARRIER_BOUNDS_HEIGHT) / 2 + creatureBox.y,
});
BarrierPacket barrierPacket;
barrierPacket.type = SerialPacketType::BARRIER_CREATE;
copyBarrierToPacket(&barrierPacket, barrierData, barrierIndex);
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&barrierPacket), roomIndex, characterIt->GetOrigin(), INFLUENCE_RADIUS);
}
}
}
//send the creature updates
for (auto& it : creatureList) {
CreaturePacket packet;
copyCreatureToPacket(&packet, it.second, it.first);
packet.type = SerialPacketType::CREATURE_UPDATE;
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS);
}
//send the barrier 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);
}
}
std::string RoomData::SetName(std::string s) {
+1 -1
View File
@@ -79,7 +79,7 @@ private:
int roomIndex = 0;
BarrierManager barrierMgr;
std::list<CharacterData*> characterList;
CombatInstanceManager CombatInstanceMgr;
CombatInstanceManager combatInstanceMgr;
CreatureManager creatureMgr;
RegionPagerLua pager;
TriggerManager triggerMgr;