Renamed the waypoint system to trogger system

This commit is contained in:
Kayne Ruse
2015-02-27 05:21:40 +11:00
parent 84c4dd0497
commit 3431d323e5
11 changed files with 106 additions and 106 deletions
+37
View File
@@ -0,0 +1,37 @@
#config
INCLUDES+=. ../entities ../server_utilities ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
#output
OUTDIR=..
OUT=$(addprefix $(OUTDIR)/,server.a)
#targets
all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(OBJ): | $(OBJDIR)
$(OUT): | $(OUTDIR)
$(OBJDIR):
mkdir $(OBJDIR)
$(OUTDIR):
mkdir $(OUTDIR)
$(OBJDIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
$(RM) *.o *.a *.exe
rebuild: clean all
+91
View File
@@ -0,0 +1,91 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "trigger_api.hpp"
#include "trigger_data.hpp"
//origin
static int setOrigin(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
trigger->SetOrigin(Vector2(lua_tonumber(L, 2), lua_tonumber(L, 3)));
return 0;
}
static int getOrigin(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushnumber(L, trigger->GetOrigin().x);
lua_pushnumber(L, trigger->GetOrigin().y);
return 2;
}
//bounds
static int setBoundingBox(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
trigger->SetBoundingBox(BoundingBox(
lua_tonumber(L, 2),
lua_tonumber(L, 3),
lua_tonumber(L, 4),
lua_tonumber(L, 5)
));
return 0;
}
static int getBoundingBox(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushnumber(L, trigger->GetBoundingBox().x);
lua_pushnumber(L, trigger->GetBoundingBox().y);
lua_pushnumber(L, trigger->GetBoundingBox().w);
lua_pushnumber(L, trigger->GetBoundingBox().h);
return 4;
}
//triggers
static int setReference(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
luaL_unref(L, LUA_REGISTRYINDEX, trigger->GetScriptReference());
trigger->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static int getReference(lua_State* L) {
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushinteger(L, trigger->GetScriptReference());
lua_gettable(L, LUA_REGISTRYINDEX);
return 1;
}
static const luaL_Reg triggerLib[] = {
{"SetOrigin",setOrigin},
{"GetOrigin",getOrigin},
{"SetBounds",setBoundingBox},
{"GetBounds",getBoundingBox},
{"SetScript",setReference},
{"GetScript",getReference},
{nullptr, nullptr}
};
LUAMOD_API int openTriggerAPI(lua_State* L) {
luaL_newlib(L, triggerLib);
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#ifndef TRIGGERAPI_HPP_
#define TRIGGERAPI_HPP_
#include "lua.hpp"
#define TORTUGA_TRIGGER_API "trigger"
LUAMOD_API int openTriggerAPI(lua_State* L);
#endif
+46
View File
@@ -0,0 +1,46 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "trigger_data.hpp"
int TriggerData::SetScriptReference(int i) {
return scriptRef = i;
}
int TriggerData::GetScriptReference() {
return scriptRef;
}
BoundingBox TriggerData::SetBoundingBox(BoundingBox b) {
return bounds = b;
}
BoundingBox TriggerData::GetBoundingBox() {
return bounds;
}
Vector2 TriggerData::SetOrigin(Vector2 v) {
return origin = v;
}
Vector2 TriggerData::GetOrigin() {
return origin;
}
+54
View File
@@ -0,0 +1,54 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#ifndef TRIGGERDATA_HPP_
#define TRIGGERDATA_HPP_
#include "bounding_box.hpp"
#include "vector2.hpp"
#include "lua.hpp"
#include <string>
class TriggerData {
public:
TriggerData() = default;
~TriggerData() = default;
Vector2 SetOrigin(Vector2 v);
Vector2 GetOrigin();
BoundingBox SetBoundingBox(BoundingBox b);
BoundingBox GetBoundingBox();
int SetScriptReference(int i);
int GetScriptReference();
private:
friend class TriggerManager;
Vector2 origin;
BoundingBox bounds;
int scriptRef = LUA_NOREF;
};
#endif
+98
View File
@@ -0,0 +1,98 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "trigger_manager.hpp"
TriggerManager::TriggerManager() {
//EMPTY
}
TriggerManager::~TriggerManager() {
UnloadAll();
}
int TriggerManager::Create() {
//implicitly creates the element
TriggerData& triggerData = elementMap[counter];
//no real values set
triggerData.origin = {0, 0};
triggerData.bounds = {0, 0, 0, 0};
return counter++;
}
int TriggerManager::Create(Vector2 origin, BoundingBox bounds) {
//implicitly creates the element
TriggerData& triggerData = elementMap[counter];
triggerData.origin = origin;
triggerData.bounds = bounds;
return counter++;
}
void TriggerManager::Unload(int uid) {
elementMap.erase(uid);
}
void TriggerManager::UnloadAll() {
elementMap.clear();
}
void TriggerManager::UnloadIf(std::function<bool(std::pair<const int, TriggerData const&>)> fn) {
std::map<int, TriggerData>::iterator it = elementMap.begin();
while (it != elementMap.end()) {
if (fn(*it)) {
it = elementMap.erase(it);
}
else {
++it;
}
}
}
TriggerData* TriggerManager::Get(int uid) {
std::map<int, TriggerData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) {
return nullptr;
}
return &it->second;
}
int TriggerManager::GetLoadedCount() {
return elementMap.size();
}
std::map<int, TriggerData>* TriggerManager::GetContainer() {
return &elementMap;
}
//hooks
lua_State* TriggerManager::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* TriggerManager::GetLuaState() {
return lua;
}
+65
View File
@@ -0,0 +1,65 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#ifndef TRIGGERMANAGER_HPP_
#define TRIGGERMANAGER_HPP_
#include "bounding_box.hpp"
#include "vector2.hpp"
#include "trigger_data.hpp"
#include "lua.hpp"
#include <functional>
#include <map>
#include <string>
//TODO: (1) rename this system to the "trigger" system
class TriggerManager {
public:
TriggerManager();
~TriggerManager();
//common public methods
int Create();
int Create(Vector2 origin, BoundingBox bounds);
void Unload(int uid);
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, TriggerData const&>)> fn);
//accessors & mutators
TriggerData* Get(int uid);
int GetLoadedCount();
std::map<int, TriggerData>* GetContainer();
//hooks
lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState();
private:
//members
std::map<int, TriggerData> elementMap;
lua_State* lua = nullptr;
int counter = 0;
};
#endif
+59
View File
@@ -0,0 +1,59 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "trigger_manager_api.hpp"
#include "trigger_manager.hpp"
//TODO: figure out a way to iterate through elements of managers from lua
static int create(lua_State* L) {
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
}
static int unload(lua_State* L) {
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
}
static int getTrigger(lua_State* L) { //TODO: (1) named triggers
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2)));
return 1;
}
static int getLoadedCount(lua_State* L) {
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
lua_pushinteger(L, mgr->GetLoadedCount());
return 1;
}
static const luaL_Reg triggerManagerLib[] = {
{"Create",create},
{"Unload",unload},
{"GetTrigger",getTrigger},
{"GetCount",getLoadedCount},
{nullptr, nullptr}
};
LUAMOD_API int openTriggerManagerAPI(lua_State* L) {
luaL_newlib(L, triggerManagerLib);
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#ifndef TRIGGERMANAGERAPI_HPP_
#define TRIGGERMANAGERAPI_HPP_
#include "lua.hpp"
#define TORTUGA_TRIGGER_MANAGER_API "trigger_manager"
LUAMOD_API int openTriggerManagerAPI(lua_State* L);
#endif
+55
View File
@@ -0,0 +1,55 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "trigger_system_api.hpp"
//all trigger API headers
#include "trigger_api.hpp"
#include "trigger_manager_api.hpp"
//useful "globals"
//...
//This mimics linit.c to create a nested collection of all trigger modules.
static const luaL_Reg funcs[] = {
{nullptr, nullptr}
};
static const luaL_Reg libs[] = {
{"Trigger", openTriggerAPI},
{"TriggerManager", openTriggerManagerAPI},
{nullptr, nullptr}
};
int openTriggerSystemAPI(lua_State* L) {
//create the table
luaL_newlibtable(L, libs);
//push the "global" functions
luaL_setfuncs(L, funcs, 0);
//push the substable
for (const luaL_Reg* lib = libs; lib->func; lib++) {
lib->func(L);
lua_setfield(L, -2, lib->name);
}
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#ifndef TRIGGERSYSTEMAPI_HPP_
#define TRIGGERSYSTEMAPI_HPP_
#include "lua.hpp"
#define TORTUGA_TRIGGER_SYSTEM_API "trigger_system"
LUAMOD_API int openTriggerSystemAPI(lua_State* L);
#endif