From 41d6314bebae9ffe0df0aae7d24c8ea0e2d0b76d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Mar 2015 06:16:12 +1100 Subject: [PATCH] Triggers have a basic response to character collision I've hacked the trigger system to create a really basic teleport pad, using the dirt tile as an indicator. This behaviour is not coded into the engine, but is in fact scripted in lua. This commit is messy, due to lack of sleep. --- rsc/scripts/map_maker.lua | 21 ---------------- rsc/scripts/setup_server.lua | 14 +++++++++++ server/entities/entity.cpp | 4 +++ server/entities/entity.hpp | 2 ++ server/rooms/room_data.cpp | 49 +++++++++++++++++++++++++++++++----- todo.txt | 1 + 6 files changed, 64 insertions(+), 27 deletions(-) diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index aec9cdd..62e4e06 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -62,24 +62,6 @@ function mapMaker.SmoothEdgesSimple(r) end end -function mapMaker.PlaceMonsterSpawn(r, x, y, script) - --place monster spawns here, highlighted by dirt patches - - --wrong region - if x < Region.GetX(r) or x >= Region.GetX(r) + Region.GetWidth(r) or - y < Region.GetY(r) or y >= Region.GetY(r) + Region.GetHeight(r) - then - return - end - - --place a dirt tile, clearing the above layers - Region.SetTile(r, x - Region.GetX(r), y - Region.GetY(r), 1, mapMaker.dirt) - Region.SetTile(r, x - Region.GetX(r), y - Region.GetY(r), 2, mapMaker.blank) - Region.SetTile(r, x - Region.GetX(r), y - Region.GetY(r), 3, mapMaker.blank) - - --TODO: (1) create a monster spawn trigger using the given script -end - --custom generation systems here function mapMaker.DebugIsland(r) --basic distance check for each tile, placing an island around the world origin @@ -109,9 +91,6 @@ function mapMaker.DebugIsland(r) --A generic edge system mapMaker.SmoothEdgesSimple(r) - - --place monster spawns - mapMaker.PlaceMonsterSpawn(r, -5, -5, nil) end return mapMaker \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 3edc755..56084c4 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -2,6 +2,7 @@ print("Lua script check") mapMaker = require("map_maker") mapSaver = require("map_saver") +mapSystem = require("map_system") roomSystem = require("room_system") characterSystem = require("character_system") networkSystem = require("network") @@ -47,6 +48,17 @@ end) local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) +--debug: test the trigger system +local pager = roomSystem.Room.GetPager(overworld) +mapSystem.RegionPager.SetTile(pager, 0, 0, 0, mapMaker.dirt) +local triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) +--TODO: (1) What does the trigger script take as a parameter? +triggerSystem.TriggerManager.Create(triggerMgr, "dirt", 0, 0, 0, 0, 32, 32, function(character) + local x, y = characterSystem.Character.GetOrigin(character) + characterSystem.Character.SetOrigin(character, x, y + 128) + networkSystem.PumpCharacterUpdate(character) +end) + --debugging function dumpTrigger(t) local originX, originY = triggerSystem.Trigger.GetOrigin(t) @@ -55,6 +67,7 @@ function dumpTrigger(t) print(triggerSystem.Trigger.GetHandle(t), originX, originY, bx, by, bw, bh, s) end +--[[ triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle2", 30.2, 40.2) @@ -63,5 +76,6 @@ trigger3, uid3 = triggerSystem.TriggerManager.Create(triggerMgr, "handle3", 30.2 dumpTrigger(trigger1) dumpTrigger(trigger2) dumpTrigger(trigger3) +--]] print("Finished the lua script") diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index 2c3ec33..b77ea57 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -21,6 +21,10 @@ */ #include "entity.hpp" +void Entity::Update() { + origin += motion; +} + int Entity::SetRoomIndex(int i) { return roomIndex = i; } diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index ab819c7..70b0ea8 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -28,6 +28,8 @@ //The base class for all objects in the world class Entity { public: + virtual void Update(); + //accessors & mutators int SetRoomIndex(int i); Vector2 SetOrigin(Vector2 v); diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 1529470..a51b09e 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -21,19 +21,56 @@ */ #include "room_data.hpp" +#include +#include + void RoomData::RunFrame() { //get the hook lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef); - if (lua_isnil(lua, -1)) { + if (!lua_isnil(lua, -1)) { + //call the tick function, with this as a parameter + lua_pushlightuserdata(lua, this); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); + } + } + else { lua_pop(lua, 1); - return; } - //call the tick function, with this as a parameter - lua_pushlightuserdata(lua, this); - if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); + //update the entities in the room + for (auto& it : characterList) { + it->Update(); + } + for (auto& it : *monsterMgr.GetContainer()) { + it.second.Update(); + } + + //compare the triggers to the entities + for (auto& it : *triggerMgr.GetContainer()) { + for (auto& character : characterList) { + //positional boxes + BoundingBox hitBox = character->GetBounds() + character->GetOrigin(); + BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); + + if ( itBox.CheckOverlap(hitBox) ) { + //TODO: trigger script + lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); + lua_pushlightuserdata(lua, character); + + //run the script + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + //error + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); + } + } + } +// for (auto& monster : *monsterMgr.GetContainer()) { +// if (it.second.Compare(static_cast(&monster.second))) { +// //TODO: (1) trigger script +// } +// } } } diff --git a/todo.txt b/todo.txt index 7916115..580efed 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ TODO: upgrade to lua 5.3 TODO: Split config.cfg in two, one for the server and the client +TODO: Consistency for bounds names TODO: Account passwords (list) * backbone account server OR