Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ab6207d4f3 | |||
| 1267b30806 | |||
| 5d217d7cf9 |
@@ -22,7 +22,7 @@
|
|||||||
#include "barrier_manager.hpp"
|
#include "barrier_manager.hpp"
|
||||||
|
|
||||||
void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
||||||
for (auto& it : elementMap) {
|
for (auto& it : barrierMap) {
|
||||||
it.second.DrawTo(dest, x, y);
|
it.second.DrawTo(dest, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,38 +47,26 @@ void BarrierManager::UnloadTemplateImages() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Create(int index) {
|
BaseBarrier* BarrierManager::Create(int index) {
|
||||||
elementMap.emplace(index, BaseBarrier(baseImage, templateImages));
|
barrierMap.emplace(index, BaseBarrier(baseImage, templateImages));
|
||||||
return &elementMap[index];
|
return &barrierMap[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
void BarrierManager::Unload(int i) {
|
void BarrierManager::Unload(int i) {
|
||||||
elementMap.erase(i);
|
barrierMap.erase(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BarrierManager::UnloadAll() {
|
void BarrierManager::UnloadAll() {
|
||||||
elementMap.clear();
|
barrierMap.clear();
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn) {
|
|
||||||
std::map<int, BaseBarrier>::iterator it = elementMap.begin();
|
|
||||||
while (it != elementMap.end()) {
|
|
||||||
if (fn(*it)) {
|
|
||||||
it = elementMap.erase(it);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int BarrierManager::Size() {
|
int BarrierManager::Size() {
|
||||||
return elementMap.size();
|
return barrierMap.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Find(int i) {
|
BaseBarrier* BarrierManager::Find(int i) {
|
||||||
std::map<int, BaseBarrier>::iterator it = elementMap.find(i);
|
std::map<int, BaseBarrier>::iterator it = barrierMap.find(i);
|
||||||
|
|
||||||
if (it == elementMap.end()) {
|
if (it == barrierMap.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +74,7 @@ BaseBarrier* BarrierManager::Find(int i) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::map<int, BaseBarrier>* BarrierManager::GetContainer() {
|
std::map<int, BaseBarrier>* BarrierManager::GetContainer() {
|
||||||
return &elementMap;
|
return &barrierMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, Image>* BarrierManager::GetTemplateContainer() {
|
std::map<std::string, Image>* BarrierManager::GetTemplateContainer() {
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ public:
|
|||||||
BaseBarrier* Create(int index);
|
BaseBarrier* Create(int index);
|
||||||
void Unload(int i);
|
void Unload(int i);
|
||||||
void UnloadAll();
|
void UnloadAll();
|
||||||
void UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn);
|
|
||||||
|
|
||||||
int Size();
|
int Size();
|
||||||
|
|
||||||
@@ -53,5 +52,5 @@ public:
|
|||||||
private:
|
private:
|
||||||
Image baseImage;
|
Image baseImage;
|
||||||
std::map<std::string, Image> templateImages;
|
std::map<std::string, Image> templateImages;
|
||||||
std::map<int, BaseBarrier> elementMap;
|
std::map<int, BaseBarrier> barrierMap;
|
||||||
};
|
};
|
||||||
@@ -116,8 +116,8 @@ World::World(int* const argClientIndex, int* const argAccountIndex):
|
|||||||
"slot 7 red.png",
|
"slot 7 red.png",
|
||||||
"slot 8 red.png"
|
"slot 8 red.png"
|
||||||
};
|
};
|
||||||
barrierMgr.LoadBaseImage(GetRenderer(), config["dir.sprites"] + "barrier/base.png");
|
barrierMgr.LoadBaseImage(GetRenderer(), config["dir.sprites"] + "/barrier/base.png");
|
||||||
barrierMgr.LoadTemplateImages(GetRenderer(), config["dir.sprites"] + "barrier/", slotNames);
|
barrierMgr.LoadTemplateImages(GetRenderer(), config["dir.sprites"] + "/barrier/", slotNames);
|
||||||
|
|
||||||
std::cout << "Templates loaded: " << barrierMgr.GetTemplateContainer()->size() << std::endl;
|
std::cout << "Templates loaded: " << barrierMgr.GetTemplateContainer()->size() << std::endl;
|
||||||
}
|
}
|
||||||
@@ -185,27 +185,7 @@ void World::Update() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: (0) regular query interval
|
//TODO: (1) regular query interval
|
||||||
if (Clock::now() - queryTime > std::chrono::seconds(3)) {
|
|
||||||
queryTime = Clock::now();
|
|
||||||
//query the world state (room)
|
|
||||||
CharacterPacket characterPacket;
|
|
||||||
memset(&characterPacket, 0, MAX_PACKET_SIZE);
|
|
||||||
characterPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS;
|
|
||||||
characterPacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &characterPacket);
|
|
||||||
|
|
||||||
CreaturePacket creaturePacket;
|
|
||||||
creaturePacket.type = SerialPacketType::QUERY_CREATURE_EXISTS;
|
|
||||||
creaturePacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &creaturePacket);
|
|
||||||
|
|
||||||
BarrierPacket barrierPacket;
|
|
||||||
barrierPacket.type = SerialPacketType::QUERY_BARRIER_EXISTS;
|
|
||||||
barrierPacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &barrierPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
//cull creatures
|
//cull creatures
|
||||||
for (std::map<int, BaseCreature>::iterator it = creatureMap.begin(); it != creatureMap.end(); /* */) {
|
for (std::map<int, BaseCreature>::iterator it = creatureMap.begin(); it != creatureMap.end(); /* */) {
|
||||||
if ( (localCharacter->GetOrigin() - it->second.GetOrigin()).Length() > INFLUENCE_RADIUS) {
|
if ( (localCharacter->GetOrigin() - it->second.GetOrigin()).Length() > INFLUENCE_RADIUS) {
|
||||||
@@ -216,10 +196,7 @@ void World::Update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//cull barriers
|
//TODO: cull barriers
|
||||||
barrierMgr.UnloadIf([&](std::pair<const int, BaseBarrier const&> barrierIt) -> bool {
|
|
||||||
return (localCharacter->GetOrigin() - barrierIt.second.GetOrigin()).Length() > INFLUENCE_RADIUS;
|
|
||||||
});
|
|
||||||
|
|
||||||
//get the collidable boxes
|
//get the collidable boxes
|
||||||
std::list<BoundingBox> boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH());
|
std::list<BoundingBox> boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH());
|
||||||
@@ -750,9 +727,6 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
if (character->GetOwner() == accountIndex) {
|
if (character->GetOwner() == accountIndex) {
|
||||||
localCharacter = static_cast<LocalCharacter*>(character);
|
localCharacter = static_cast<LocalCharacter*>(character);
|
||||||
|
|
||||||
//reset queries
|
|
||||||
queryTime = Clock::now() - std::chrono::seconds(4); //back 4 seconds to trigger automatically
|
|
||||||
|
|
||||||
//focus the camera on this character's sprite
|
//focus the camera on this character's sprite
|
||||||
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetClipW() / 2);
|
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetClipW() / 2);
|
||||||
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetClipH() / 2);
|
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetClipH() / 2);
|
||||||
@@ -760,6 +734,26 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
//focus on this character's info
|
//focus on this character's info
|
||||||
characterIndex = argPacket->characterIndex;
|
characterIndex = argPacket->characterIndex;
|
||||||
roomIndex = argPacket->roomIndex;
|
roomIndex = argPacket->roomIndex;
|
||||||
|
|
||||||
|
//query the world state (room)
|
||||||
|
CharacterPacket characterPacket;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
@@ -800,12 +794,12 @@ void World::hCharacterUnload(CharacterPacket* const argPacket) {
|
|||||||
void World::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
void World::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
||||||
//prevent a double message about this player's character
|
//prevent a double message about this player's character
|
||||||
//TODO: why is this commented out?
|
//TODO: why is this commented out?
|
||||||
if (argPacket->accountIndex == accountIndex) {
|
// if (argPacket->accountIndex == accountIndex) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
//ignore characters in a different room (sub-optimal)
|
//ignore characters in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
if (argPacket->roomIndex != roomIndex) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,12 +916,10 @@ void World::hCreatureUnload(CreaturePacket* const argPacket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::hQueryCreatureExists(CreaturePacket* const argPacket) {
|
void World::hQueryCreatureExists(CreaturePacket* const argPacket) {
|
||||||
if (!localCharacter) {
|
std::cout << "Creature Query" << std::endl;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ignore creatures in a different room (sub-optimal)
|
//ignore creatures in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
if (argPacket->roomIndex != roomIndex) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1032,12 +1024,10 @@ void World::hBarrierUnload(BarrierPacket* const argPacket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::hQueryBarrierExists(BarrierPacket* const argPacket) {
|
void World::hQueryBarrierExists(BarrierPacket* const argPacket) {
|
||||||
if (!localCharacter) {
|
std::cout << "Barrier Query" << std::endl;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ignore barriers in a different room (sub-optimal)
|
//ignore barriers in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
if (argPacket->roomIndex != roomIndex) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -168,7 +168,6 @@ private:
|
|||||||
//TODO: (2) Heartbeat needs it's own utility
|
//TODO: (2) Heartbeat needs it's own utility
|
||||||
typedef std::chrono::steady_clock Clock;
|
typedef std::chrono::steady_clock Clock;
|
||||||
Clock::time_point lastBeat = Clock::now();
|
Clock::time_point lastBeat = Clock::now();
|
||||||
Clock::time_point queryTime = Clock::now() - std::chrono::seconds(4); //back 4 seconds to trigger automatically
|
|
||||||
int attemptedBeats = 0;
|
int attemptedBeats = 0;
|
||||||
|
|
||||||
//ugly references; I hate this
|
//ugly references; I hate this
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 738 B After Width: | Height: | Size: 738 B |
|
Before Width: | Height: | Size: 763 B After Width: | Height: | Size: 763 B |
|
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 273 B |
|
Before Width: | Height: | Size: 270 B After Width: | Height: | Size: 270 B |
|
Before Width: | Height: | Size: 305 B After Width: | Height: | Size: 305 B |
|
Before Width: | Height: | Size: 302 B After Width: | Height: | Size: 302 B |
|
Before Width: | Height: | Size: 423 B After Width: | Height: | Size: 423 B |
|
Before Width: | Height: | Size: 422 B After Width: | Height: | Size: 422 B |
|
Before Width: | Height: | Size: 301 B After Width: | Height: | Size: 301 B |
|
Before Width: | Height: | Size: 299 B After Width: | Height: | Size: 299 B |
|
Before Width: | Height: | Size: 385 B After Width: | Height: | Size: 385 B |
|
Before Width: | Height: | Size: 383 B After Width: | Height: | Size: 383 B |
|
Before Width: | Height: | Size: 412 B After Width: | Height: | Size: 412 B |
|
Before Width: | Height: | Size: 402 B After Width: | Height: | Size: 402 B |
|
Before Width: | Height: | Size: 431 B After Width: | Height: | Size: 431 B |
|
Before Width: | Height: | Size: 429 B After Width: | Height: | Size: 429 B |
|
Before Width: | Height: | Size: 407 B After Width: | Height: | Size: 407 B |
|
Before Width: | Height: | Size: 400 B After Width: | Height: | Size: 400 B |
|
Before Width: | Height: | Size: 656 B After Width: | Height: | Size: 656 B |
|
After Width: | Height: | Size: 680 B |
@@ -130,14 +130,14 @@ roomManagerAPI.SetOnCreate(function(room, index)
|
|||||||
--
|
--
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
|
||||||
|
|
||||||
--creatureManager with SetOnCreate, SetOnUnload & create & unload
|
--creatureManager with SetOnCreate, SetOnUnload & create & unload
|
||||||
barrierManagerAPI.SetOnCreate(roomAPI.GetBarrierMgr(room), function(barrier)
|
barrierManagerAPI.SetOnCreate(roomAPI.GetBarrierMgr(room), function(barrier)
|
||||||
barrierAPI.SetScript(barrier, barrierTick)
|
barrierAPI.SetScript(barrier, barrierTick)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
roomAPI.SetOnTick(room, function(room)
|
roomAPI.SetOnTick(room, function(room)
|
||||||
ret = 0
|
|
||||||
--placeholders
|
|
||||||
roomAPI.ForEachCharacter(room, function(character)
|
roomAPI.ForEachCharacter(room, function(character)
|
||||||
--
|
--
|
||||||
end)
|
end)
|
||||||
@@ -146,19 +146,7 @@ roomManagerAPI.SetOnCreate(function(room, index)
|
|||||||
--
|
--
|
||||||
end)
|
end)
|
||||||
|
|
||||||
roomAPI.ForEachBarrier(room, function(creature)
|
--TODO: for each barrier
|
||||||
--
|
|
||||||
end)
|
|
||||||
|
|
||||||
--respawn a new rabbit when needed
|
|
||||||
if creatureManagerAPI.GetLoadedCount(roomAPI.GetCreatureMgr(room)) < 1 and
|
|
||||||
barrierManagerAPI.GetLoadedCount(roomAPI.GetBarrierMgr(room)) < 2
|
|
||||||
then
|
|
||||||
--make a new creature
|
|
||||||
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
|
|
||||||
ret = 1
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#include "lua_utilities.hpp"
|
#include "lua_utilities.hpp"
|
||||||
|
|
||||||
|
#include "barrier_defines.hpp"
|
||||||
|
|
||||||
BarrierManager::BarrierManager() {
|
BarrierManager::BarrierManager() {
|
||||||
//EMPTY
|
//EMPTY
|
||||||
}
|
}
|
||||||
@@ -32,12 +34,57 @@ BarrierManager::~BarrierManager() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//arg: a list of barriers to be updated in the clients
|
//arg: a list of barriers to be updated in the clients
|
||||||
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll) {
|
void BarrierManager::Update(
|
||||||
int ret;
|
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) {
|
for (auto& it : elementMap) {
|
||||||
ret = it.second.Update(lua);
|
//normal update
|
||||||
if (ret || updateAll) {
|
ret = it.second.Update(lua) ? 1 : 0;
|
||||||
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second));
|
|
||||||
|
//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) {
|
||||||
|
//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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "barrier_data.hpp"
|
#include "barrier_data.hpp"
|
||||||
|
#include "character_data.hpp"
|
||||||
|
#include "creature_data.hpp"
|
||||||
|
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
@@ -29,6 +31,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
class BarrierManager {
|
class BarrierManager {
|
||||||
public:
|
public:
|
||||||
@@ -36,7 +39,12 @@ public:
|
|||||||
~BarrierManager();
|
~BarrierManager();
|
||||||
|
|
||||||
//common public methods
|
//common public methods
|
||||||
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll);
|
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);
|
int Create(int instanceIndex);
|
||||||
void Unload(int uid);
|
void Unload(int uid);
|
||||||
|
|||||||
@@ -32,12 +32,39 @@ CreatureManager::~CreatureManager() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//arg: a list of creatures to be updated in the clients
|
//arg: a list of creatures to be updated in the clients
|
||||||
void CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList, bool updateAll) {
|
void CreatureManager::Update(
|
||||||
int ret;
|
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) {
|
for (auto& it : elementMap) {
|
||||||
ret = it.second.Update(lua);
|
//normal update
|
||||||
if (ret || updateAll) {
|
ret = it.second.Update(lua) ? 1 : 0;
|
||||||
creatureList->push_back(std::pair<const int, CreatureData*>(it.first, &it.second));
|
|
||||||
|
//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) {
|
||||||
|
//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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "character_data.hpp"
|
||||||
#include "creature_data.hpp"
|
#include "creature_data.hpp"
|
||||||
|
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
@@ -30,6 +31,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
class CreatureManager {
|
class CreatureManager {
|
||||||
public:
|
public:
|
||||||
@@ -37,7 +39,11 @@ public:
|
|||||||
~CreatureManager();
|
~CreatureManager();
|
||||||
|
|
||||||
//common public methods
|
//common public methods
|
||||||
void Update(std::list<std::pair<const int, CreatureData*>>* creatureList, bool updateAll);
|
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);
|
int Create(std::string avatar, int scriptRef);
|
||||||
void Unload(int uid);
|
void Unload(int uid);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#config
|
#config
|
||||||
INCLUDES+=. .. ../entities ../../common/gameplay ../../common/utilities
|
INCLUDES+=. .. ../characters ../entities ../../common/gameplay ../../common/utilities
|
||||||
LIBS+=
|
LIBS+=
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ BoundingBox Entity::GetBounds() const {
|
|||||||
return bounds;
|
return bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BoundingBox Entity::GetRealBounds() const {
|
||||||
|
return bounds + origin;
|
||||||
|
}
|
||||||
|
|
||||||
const char* Entity::GetType() const {
|
const char* Entity::GetType() const {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@@ -41,6 +41,7 @@ public:
|
|||||||
Vector2 GetOrigin() const;
|
Vector2 GetOrigin() const;
|
||||||
Vector2 GetMotion() const;
|
Vector2 GetMotion() const;
|
||||||
BoundingBox GetBounds() const;
|
BoundingBox GetBounds() const;
|
||||||
|
BoundingBox GetRealBounds() const;
|
||||||
|
|
||||||
const char* GetType() const;
|
const char* GetType() const;
|
||||||
|
|
||||||
|
|||||||
@@ -109,24 +109,6 @@ static int forEachCreature(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int forEachBarrier(lua_State* L) {
|
|
||||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
|
||||||
BarrierManager* barrierMgr = room->GetBarrierMgr();
|
|
||||||
//pass each barrier to the given function
|
|
||||||
for (auto& it : *barrierMgr->GetContainer()) {
|
|
||||||
lua_pushvalue(L, -1);
|
|
||||||
lua_pushlightuserdata(L, static_cast<void*>(&it.second));
|
|
||||||
//call each iteration, throwing an exception if something happened
|
|
||||||
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
|
|
||||||
std::ostringstream os;
|
|
||||||
os << "Lua error: ";
|
|
||||||
os << lua_tostring(L, -1);
|
|
||||||
throw(std::runtime_error(os.str()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setOnTick(lua_State* L) {
|
static int setOnTick(lua_State* L) {
|
||||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference());
|
luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference());
|
||||||
@@ -140,19 +122,6 @@ static int getOnTick(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: autogen docs
|
|
||||||
static int setTag(lua_State* L) {
|
|
||||||
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
|
||||||
room->SetTag(lua_tostring(L, 2), lua_tostring(L, 3));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTag(lua_State* L) {
|
|
||||||
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushstring(L, room->GetTag(lua_tostring(L, 2)).c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int initialize(lua_State* L) {
|
static int initialize(lua_State* L) {
|
||||||
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
|
||||||
@@ -179,14 +148,10 @@ static const luaL_Reg roomLib[] = {
|
|||||||
|
|
||||||
{"ForEachCharacter", forEachCharacter},
|
{"ForEachCharacter", forEachCharacter},
|
||||||
{"ForEachCreature", forEachCreature},
|
{"ForEachCreature", forEachCreature},
|
||||||
{"ForEachBarrier", forEachBarrier},
|
|
||||||
|
|
||||||
{"SetOnTick", setOnTick},
|
{"SetOnTick", setOnTick},
|
||||||
{"GetOnTick", getOnTick},
|
{"GetOnTick", getOnTick},
|
||||||
|
|
||||||
{"SetTag", setTag},
|
|
||||||
{"GetTag", getTag},
|
|
||||||
|
|
||||||
{"Initialize", initialize},
|
{"Initialize", initialize},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -30,42 +30,70 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
//TODO: (9) character collisions should be preformed client-side
|
//TODO: (9) character collisions should be preformed client-side
|
||||||
void RoomData::RunFrame() {
|
void RoomData::RunFrame() {
|
||||||
//get the hook
|
//get the hook
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef);
|
lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef);
|
||||||
|
|
||||||
//returning 1 means pump all
|
|
||||||
bool updateAll = false;
|
|
||||||
if (!lua_isnil(lua, -1)) {
|
if (!lua_isnil(lua, -1)) {
|
||||||
//call the tick function, with this as a parameter
|
//call the tick function, with this as a parameter
|
||||||
lua_pushlightuserdata(lua, this);
|
lua_pushlightuserdata(lua, this);
|
||||||
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
|
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
||||||
}
|
}
|
||||||
|
|
||||||
//pump creatures & barriers
|
|
||||||
if (lua_tonumber(lua, -1)) {
|
|
||||||
updateAll = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pop(lua, 1);
|
lua_pop(lua, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//lists of non-character entities that need updating client-side
|
//lists of non-character entities that need updating client-side
|
||||||
std::list<std::pair<const int, CreatureData*>> creatureList;
|
//types are index, ptr, action (0 = update, 1 = unload)
|
||||||
std::list<std::pair<const int, BarrierData*>> barrierList;
|
std::list<std::tuple<const int, CreatureData*, int>> creatureList;
|
||||||
|
std::list<std::tuple<const int, BarrierData*, int>> barrierList;
|
||||||
|
|
||||||
//update the entities in the room
|
//update the entities in the room
|
||||||
for (auto& it : characterList) {
|
for (auto& it : characterList) {
|
||||||
it->Update();
|
it->Update();
|
||||||
}
|
}
|
||||||
creatureMgr.Update(&creatureList, updateAll);
|
creatureMgr.Update(&creatureList, &characterList);
|
||||||
barrierMgr.Update(&barrierList, updateAll);
|
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
|
//build a list of entities for use with the triggers
|
||||||
std::stack<Entity*> entityStack;
|
std::stack<Entity*> entityStack;
|
||||||
@@ -75,67 +103,6 @@ void RoomData::RunFrame() {
|
|||||||
|
|
||||||
//Compare the triggers to the entities, using their real hitboxes
|
//Compare the triggers to the entities, using their real hitboxes
|
||||||
triggerMgr.Compare(entityStack);
|
triggerMgr.Compare(entityStack);
|
||||||
|
|
||||||
//Creature/character collisions, O(m*n)
|
|
||||||
for (auto characterIt : characterList) {
|
|
||||||
BoundingBox characterBox = characterIt->GetBounds() + characterIt->GetOrigin();
|
|
||||||
|
|
||||||
std::list<int> creatureUnloadList;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
//delete this creature
|
|
||||||
creatureUnloadList.push_back(creatureIt.first);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//actually unload these creatures
|
|
||||||
for (auto& it : creatureUnloadList) {
|
|
||||||
CreatureData* creatureData = creatureMgr.Find(it);
|
|
||||||
|
|
||||||
CreaturePacket creaturePacket;
|
|
||||||
creaturePacket.type = SerialPacketType::CREATURE_UNLOAD;
|
|
||||||
copyCreatureToPacket(&creaturePacket, creatureData, it);
|
|
||||||
|
|
||||||
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&creaturePacket), roomIndex, creatureData->GetOrigin(), INFLUENCE_RADIUS);
|
|
||||||
|
|
||||||
creatureMgr.Unload(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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) {
|
std::string RoomData::SetName(std::string s) {
|
||||||
@@ -212,11 +179,3 @@ int RoomData::SetTickReference(int i) {
|
|||||||
int RoomData::GetTickReference() {
|
int RoomData::GetTickReference() {
|
||||||
return tickRef;
|
return tickRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RoomData::SetTag(std::string key, std::string value) {
|
|
||||||
return tags[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string RoomData::GetTag(std::string key) {
|
|
||||||
return tags[key];
|
|
||||||
}
|
|
||||||
@@ -70,9 +70,6 @@ public:
|
|||||||
int GetTickReference();
|
int GetTickReference();
|
||||||
//TODO: other triggers like player entry & exit, etc.
|
//TODO: other triggers like player entry & exit, etc.
|
||||||
|
|
||||||
std::string SetTag(std::string key, std::string value);
|
|
||||||
std::string GetTag(std::string key);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//metadata
|
//metadata
|
||||||
std::string roomName;
|
std::string roomName;
|
||||||
@@ -82,7 +79,7 @@ private:
|
|||||||
int roomIndex = 0;
|
int roomIndex = 0;
|
||||||
BarrierManager barrierMgr;
|
BarrierManager barrierMgr;
|
||||||
std::list<CharacterData*> characterList;
|
std::list<CharacterData*> characterList;
|
||||||
CombatInstanceManager CombatInstanceMgr;
|
CombatInstanceManager combatInstanceMgr;
|
||||||
CreatureManager creatureMgr;
|
CreatureManager creatureMgr;
|
||||||
RegionPagerLua pager;
|
RegionPagerLua pager;
|
||||||
TriggerManager triggerMgr;
|
TriggerManager triggerMgr;
|
||||||
@@ -93,5 +90,4 @@ private:
|
|||||||
|
|
||||||
//hooks
|
//hooks
|
||||||
int tickRef = LUA_NOREF;
|
int tickRef = LUA_NOREF;
|
||||||
std::map<std::string, std::string> tags;
|
|
||||||
};
|
};
|
||||||
|
|||||||