From 1210d2d24fcdc2ea99ccf1a78b6cc20bf7930b78 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 03:06:29 +1100 Subject: [PATCH 01/28] Minor comment tweaks --- server/entities/entity_api.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index 8669417..bd7d702 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -24,6 +24,7 @@ #include "entity.hpp" static int setRoomIndex(lua_State* L) { + //TODO: (1) if this is a character, push/pop from the room system Entity* entity = static_cast(lua_touserdata(L, 1)); entity->SetRoomIndex(lua_tointeger(L, 2)); return 0; From c45bda645b403fb210265188840844462058fea7 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 03:27:37 +1100 Subject: [PATCH 02/28] Switched TODO: empty to TODO: *function name* --- client/entities/base_monster.cpp | 2 +- client/gameplay_scenes/world_characters.cpp | 8 ++++---- client/gameplay_scenes/world_chat.cpp | 6 +++--- client/gameplay_scenes/world_connections.cpp | 2 +- client/gameplay_scenes/world_monsters.cpp | 8 ++++---- client/menu_scenes/lobby_menu.cpp | 4 ++-- server/characters/character_manager_api.cpp | 10 +++++----- server/server_character_methods.cpp | 4 ++-- server/server_chat.cpp | 6 +++--- server/server_data_queries.cpp | 10 +++++----- server/server_methods.cpp | 4 ++-- server/server_monster_methods.cpp | 2 +- 12 files changed, 33 insertions(+), 33 deletions(-) diff --git a/client/entities/base_monster.cpp b/client/entities/base_monster.cpp index c268021..8d4acec 100644 --- a/client/entities/base_monster.cpp +++ b/client/entities/base_monster.cpp @@ -24,7 +24,7 @@ #include "config_utility.hpp" void BaseMonster::CorrectSprite() { - //TODO: (9) empty + //TODO: (9) BaseMonster::CorrectSprite() } std::string BaseMonster::SetHandle(std::string s) { diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index 85a4dc2..4d40c4c 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -146,11 +146,11 @@ void World::hQueryCharacterExists(CharacterPacket* const argPacket) { } void World::hQueryCharacterStats(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hQueryCharacterStats() } void World::hQueryCharacterLocation(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hQueryCharacterLocation() } void World::hCharacterMovement(CharacterPacket* const argPacket) { @@ -170,11 +170,11 @@ void World::hCharacterMovement(CharacterPacket* const argPacket) { } void World::hCharacterAttack(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hCharacterAttack() } void World::hCharacterDamage(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hCharacterDamage() } //------------------------- diff --git a/client/gameplay_scenes/world_chat.cpp b/client/gameplay_scenes/world_chat.cpp index 3a29d21..09ac1bb 100644 --- a/client/gameplay_scenes/world_chat.cpp +++ b/client/gameplay_scenes/world_chat.cpp @@ -26,14 +26,14 @@ //------------------------- void World::hTextBroadcast(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hTextBroadcast() } void World::hTextSpeech(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hTextSpeech() } void World::hTextWhisper(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hTextWhisper() } diff --git a/client/gameplay_scenes/world_connections.cpp b/client/gameplay_scenes/world_connections.cpp index 7ace62d..f0babeb 100644 --- a/client/gameplay_scenes/world_connections.cpp +++ b/client/gameplay_scenes/world_connections.cpp @@ -91,7 +91,7 @@ void World::SendDisconnectRequest() { } void World::SendAdminDisconnectForced() { - //TODO: (9) empty + //TODO: (9) World::SendAdminDisconnectForced() } diff --git a/client/gameplay_scenes/world_monsters.cpp b/client/gameplay_scenes/world_monsters.cpp index 8891fad..ada5786 100644 --- a/client/gameplay_scenes/world_monsters.cpp +++ b/client/gameplay_scenes/world_monsters.cpp @@ -99,11 +99,11 @@ void World::hQueryMonsterExists(MonsterPacket* const argPacket) { } void World::hQueryMonsterStats(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hQueryMonsterStats() } void World::hQueryMonsterLocation(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hQueryMonsterLocation() } void World::hMonsterMovement(MonsterPacket* const argPacket) { @@ -118,9 +118,9 @@ void World::hMonsterMovement(MonsterPacket* const argPacket) { } void World::hMonsterAttack(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hMonsterAttack() } void World::hMonsterDamage(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) World::hMonsterDamage() } \ No newline at end of file diff --git a/client/menu_scenes/lobby_menu.cpp b/client/menu_scenes/lobby_menu.cpp index 8ebc361..619eea4 100644 --- a/client/menu_scenes/lobby_menu.cpp +++ b/client/menu_scenes/lobby_menu.cpp @@ -252,11 +252,11 @@ void LobbyMenu::HandleLoginResponse(ClientPacket* const argPacket) { } void LobbyMenu::HandleJoinRejection(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) LobbyMenu::HandleJoinRejection() } void LobbyMenu::HandleLoginRejection(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) LobbyMenu::HandleLoginRejection } //------------------------- diff --git a/server/characters/character_manager_api.cpp b/server/characters/character_manager_api.cpp index eca3d6f..d9792d7 100644 --- a/server/characters/character_manager_api.cpp +++ b/server/characters/character_manager_api.cpp @@ -24,23 +24,23 @@ #include "character_manager.hpp" static int setOnCreate(lua_State* L) { - //TODO: (9) empty + //TODO: (9) setOnCreate() } static int setOnLoad(lua_State* L) { - //TODO: (9) empty + //TODO: (9) setOnLoad() } static int setOnSave(lua_State* L) { - //TODO: (9) empty + //TODO: (9) setOnSave() } static int setOnUnload(lua_State* L) { - //TODO: (9) empty + //TODO: (9) setOnUnload() } static int setOnDelete(lua_State* L) { - //TODO: (9) empty + //TODO: (9) setOnDelete() } static int getCharacter(lua_State* L) { diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 2d701cc..69246de 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -243,9 +243,9 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) { } void ServerApplication::hCharacterAttack(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hCharacterAttack() } void ServerApplication::hCharacterDamage(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hCharacterDamage() } \ No newline at end of file diff --git a/server/server_chat.cpp b/server/server_chat.cpp index ed9c4e4..f5395f0 100644 --- a/server/server_chat.cpp +++ b/server/server_chat.cpp @@ -22,13 +22,13 @@ #include "server_application.hpp" void ServerApplication::hTextBroadcast(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hTextBroadcast() } void ServerApplication::hTextSpeech(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hTextSpeech() } void ServerApplication::hTextWhisper(TextPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hTextWhisper() } diff --git a/server/server_data_queries.cpp b/server/server_data_queries.cpp index 7b04ad0..e911c59 100644 --- a/server/server_data_queries.cpp +++ b/server/server_data_queries.cpp @@ -76,21 +76,21 @@ void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket) } void ServerApplication::hQueryCharacterStats(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hQueryCharacterStats() } void ServerApplication::hQueryCharacterLocation(CharacterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hQueryCharacterLocation() } void ServerApplication::hQueryMonsterExists(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hQueryMonsterExists() } void ServerApplication::hQueryMonsterStats(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hQueryMonsterStats() } void ServerApplication::hQueryMonsterLocation(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hQueryMonsterLocation() } diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 3bcfc96..7d1d632 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -30,7 +30,7 @@ //------------------------- void ServerApplication::hAdminDisconnectForced(ClientPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hAdminDisconnectForced() } void ServerApplication::hAdminShutdownRequest(ClientPacket* const argPacket) { @@ -85,5 +85,5 @@ void ServerApplication::hAdminShutdownRequest(ClientPacket* const argPacket) { } void ServerApplication::SaveServerState() { - //TODO: (9) empty + //TODO: (9) ServerApplication::SaveServerState() } diff --git a/server/server_monster_methods.cpp b/server/server_monster_methods.cpp index ae8ba43..662c255 100644 --- a/server/server_monster_methods.cpp +++ b/server/server_monster_methods.cpp @@ -22,5 +22,5 @@ #include "server_application.hpp" void ServerApplication::hMonsterDamage(MonsterPacket* const argPacket) { - //TODO: (9) empty + //TODO: (9) ServerApplication::hMonsterDamage() } \ No newline at end of file From 3e6a05307efee9c5f8d8974d3512bec4be0768f6 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 03:36:47 +1100 Subject: [PATCH 03/28] Minor TODO tweaks --- client/menu_scenes/lobby_menu.cpp | 1 + common/network/packet_types/client_packet.hpp | 2 +- rsc/scripts/map_saver.lua | 2 +- rsc/scripts/setup_server.sql | 4 ++-- server/server_methods.cpp | 2 +- server/waypoints/waypoint_manager.hpp | 1 + todo.txt | 11 +++-------- 7 files changed, 10 insertions(+), 13 deletions(-) diff --git a/client/menu_scenes/lobby_menu.cpp b/client/menu_scenes/lobby_menu.cpp index 619eea4..d4dcc81 100644 --- a/client/menu_scenes/lobby_menu.cpp +++ b/client/menu_scenes/lobby_menu.cpp @@ -108,6 +108,7 @@ void LobbyMenu::Render(SDL_Surface* const screen) { back.DrawTo(screen); //TODO: (1) draw headers for the server list + //TODO: (1) ping/delay displayed in the server list for (int i = 0; i < serverInfo.size(); i++) { //draw the selected server's highlight if (selection == &serverInfo[i]) { diff --git a/common/network/packet_types/client_packet.hpp b/common/network/packet_types/client_packet.hpp index 2d684b1..0195f58 100644 --- a/common/network/packet_types/client_packet.hpp +++ b/common/network/packet_types/client_packet.hpp @@ -28,7 +28,7 @@ struct ClientPacket : SerialPacketBase { int clientIndex; int accountIndex; char username[PACKET_STRING_SIZE]; - //TODO: (9) password, auth token + //TODO: (3) password, auth token }; void serializeClient(void* buffer, ClientPacket* packet); diff --git a/rsc/scripts/map_saver.lua b/rsc/scripts/map_saver.lua index 0715807..c5b528b 100644 --- a/rsc/scripts/map_saver.lua +++ b/rsc/scripts/map_saver.lua @@ -11,5 +11,5 @@ function mapSaver.Save(r) io.write("map_saver:Save(", Region.GetX(r), ", ", Region.GetY(r), ")\n") end ---TODO: (9) create a flexible saving & loading system +--TODO: (3) create a flexible saving & loading system return mapSaver \ No newline at end of file diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index ddb1826..a4e3e5b 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -1,8 +1,8 @@ ---TODO: (9) An archive table of all dead characters +--TODO: (3) An archive table of all dead characters CREATE TABLE IF NOT EXISTS Accounts ( uid INTEGER PRIMARY KEY AUTOINCREMENT, - username varchar(100) UNIQUE, --TODO: (9) Swap username for email address + username varchar(100) UNIQUE, --TODO: (3) Swap username for email address --server-client security -- passhash varchar(100), diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 7d1d632..e1eb5c3 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -85,5 +85,5 @@ void ServerApplication::hAdminShutdownRequest(ClientPacket* const argPacket) { } void ServerApplication::SaveServerState() { - //TODO: (9) ServerApplication::SaveServerState() + //TODO: (2) Periodic mass server saves } diff --git a/server/waypoints/waypoint_manager.hpp b/server/waypoints/waypoint_manager.hpp index 91bc3d0..b1d9bdf 100644 --- a/server/waypoints/waypoint_manager.hpp +++ b/server/waypoints/waypoint_manager.hpp @@ -32,6 +32,7 @@ #include #include +//TODO: (1) rename this system to the "trigger" system class WaypointManager { public: WaypointManager(); diff --git a/todo.txt b/todo.txt index 7f22396..7916115 100644 --- a/todo.txt +++ b/todo.txt @@ -1,21 +1,18 @@ TODO: upgrade to lua 5.3 TODO: Split config.cfg in two, one for the server and the client -TODO: In need of script APIs (list) - * Characters - TODO: Account passwords (list) * backbone account server OR * social network login OR * ... * salts & hashes + * login screen prompting for username & password TODO: Features * Make sure login errors are sent to the client * Add the "home" parameter to the server's config file - * Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc. + * Waypoints, with positions and trigger zones (collision areas) for doors, monster spawns, etc. (trigger system) * Fix shoddy movement - * Periodic mass server saves * Remove the big "Shut Down" button (currently broken...) * Make a way for the server owner to control the server directly * The TileSheet class should implement the surface itself @@ -24,6 +21,4 @@ TODO: Features * Fix the const-ness of accessors * Add a screenshot of the game to README.md * joystick/gamepad support - * add the tilesheet to the map system - * ping/delay displayed in the lobby - * login screen prompting for username & password \ No newline at end of file + * add the tilesheet to the map system \ No newline at end of file From 84c4dd0497298b07db45542d398cfb75c539e2c8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 04:28:28 +1100 Subject: [PATCH 04/28] Placeholder monster spawn function in map maker --- rsc/scripts/map_maker.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index c48f6bf..aec9cdd 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -7,6 +7,7 @@ function mapMaker.Sqr(x) return x*x end function mapMaker.Dist(x, y, i, j) return math.sqrt(mapMaker.Sqr(x - i) + mapMaker.Sqr(y - j)) end --tile macros, mapped to the tilesheet "overworld.bmp" +mapMaker.blank = 0 mapMaker.water = 18 + 3 * 0 mapMaker.sand = 18 + 3 * 1 mapMaker.plains = 18 + 3 * 2 @@ -61,6 +62,24 @@ 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 @@ -90,6 +109,9 @@ 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 From 3431d323e52f3c13cf8191ab6e08a8cbf69f9256 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 05:21:40 +1100 Subject: [PATCH 05/28] Renamed the waypoint system to trogger system --- server/{waypoints => triggers}/makefile | 0 .../trigger_api.cpp} | 52 +++++++++---------- .../trigger_api.hpp} | 8 +-- .../trigger_data.cpp} | 18 +++---- .../trigger_data.hpp} | 18 +++---- .../trigger_manager.cpp} | 42 +++++++-------- .../trigger_manager.hpp} | 20 +++---- .../trigger_manager_api.cpp} | 22 ++++---- .../trigger_manager_api.hpp} | 8 +-- .../trigger_system_api.cpp} | 16 +++--- .../trigger_system_api.hpp} | 8 +-- 11 files changed, 106 insertions(+), 106 deletions(-) rename server/{waypoints => triggers}/makefile (100%) rename server/{waypoints/waypoint_api.cpp => triggers/trigger_api.cpp} (51%) rename server/{waypoints/waypoint_api.hpp => triggers/trigger_api.hpp} (87%) rename server/{waypoints/waypoint_data.cpp => triggers/trigger_data.cpp} (74%) rename server/{waypoints/waypoint_data.hpp => triggers/trigger_data.hpp} (82%) rename server/{waypoints/waypoint_manager.cpp => triggers/trigger_manager.cpp} (58%) rename server/{waypoints/waypoint_manager.hpp => triggers/trigger_manager.hpp} (80%) rename server/{waypoints/waypoint_manager_api.cpp => triggers/trigger_manager_api.cpp} (69%) rename server/{waypoints/waypoint_system_api.hpp => triggers/trigger_manager_api.hpp} (84%) rename server/{waypoints/waypoint_system_api.cpp => triggers/trigger_system_api.cpp} (80%) rename server/{waypoints/waypoint_manager_api.hpp => triggers/trigger_system_api.hpp} (84%) diff --git a/server/waypoints/makefile b/server/triggers/makefile similarity index 100% rename from server/waypoints/makefile rename to server/triggers/makefile diff --git a/server/waypoints/waypoint_api.cpp b/server/triggers/trigger_api.cpp similarity index 51% rename from server/waypoints/waypoint_api.cpp rename to server/triggers/trigger_api.cpp index cfc8191..025a946 100644 --- a/server/waypoints/waypoint_api.cpp +++ b/server/triggers/trigger_api.cpp @@ -19,28 +19,28 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "waypoint_api.hpp" +#include "trigger_api.hpp" -#include "waypoint_data.hpp" +#include "trigger_data.hpp" //origin static int setOrigin(lua_State* L) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - waypoint->SetOrigin(Vector2(lua_tonumber(L, 2), lua_tonumber(L, 3))); + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + trigger->SetOrigin(Vector2(lua_tonumber(L, 2), lua_tonumber(L, 3))); return 0; } static int getOrigin(lua_State* L) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - lua_pushnumber(L, waypoint->GetOrigin().x); - lua_pushnumber(L, waypoint->GetOrigin().y); + TriggerData* trigger = static_cast(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) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - waypoint->SetBoundingBox(BoundingBox( + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + trigger->SetBoundingBox(BoundingBox( lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), @@ -50,42 +50,42 @@ static int setBoundingBox(lua_State* L) { } static int getBoundingBox(lua_State* L) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - lua_pushnumber(L, waypoint->GetBoundingBox().x); - lua_pushnumber(L, waypoint->GetBoundingBox().y); - lua_pushnumber(L, waypoint->GetBoundingBox().w); - lua_pushnumber(L, waypoint->GetBoundingBox().h); + TriggerData* trigger = static_cast(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 setTriggerReference(lua_State* L) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - luaL_unref(L, LUA_REGISTRYINDEX, waypoint->GetTriggerReference()); - waypoint->SetTriggerReference(luaL_ref(L, LUA_REGISTRYINDEX)); +static int setReference(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + luaL_unref(L, LUA_REGISTRYINDEX, trigger->GetScriptReference()); + trigger->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX)); return 0; } -static int getTriggerReference(lua_State* L) { - WaypointData* waypoint = static_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, waypoint->GetTriggerReference()); +static int getReference(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, trigger->GetScriptReference()); lua_gettable(L, LUA_REGISTRYINDEX); return 1; } -static const luaL_Reg waypointLib[] = { +static const luaL_Reg triggerLib[] = { {"SetOrigin",setOrigin}, {"GetOrigin",getOrigin}, {"SetBounds",setBoundingBox}, {"GetBounds",getBoundingBox}, - {"SetTrigger",setTriggerReference}, - {"GetTrigger",getTriggerReference}, + {"SetScript",setReference}, + {"GetScript",getReference}, {nullptr, nullptr} }; -LUAMOD_API int openWaypointAPI(lua_State* L) { - luaL_newlib(L, waypointLib); +LUAMOD_API int openTriggerAPI(lua_State* L) { + luaL_newlib(L, triggerLib); return 1; } \ No newline at end of file diff --git a/server/waypoints/waypoint_api.hpp b/server/triggers/trigger_api.hpp similarity index 87% rename from server/waypoints/waypoint_api.hpp rename to server/triggers/trigger_api.hpp index 3b07463..171bd04 100644 --- a/server/waypoints/waypoint_api.hpp +++ b/server/triggers/trigger_api.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef WAYPOINTAPI_HPP_ -#define WAYPOINTAPI_HPP_ +#ifndef TRIGGERAPI_HPP_ +#define TRIGGERAPI_HPP_ #include "lua.hpp" -#define TORTUGA_WAYPOINT_API "waypoint" -LUAMOD_API int openWaypointAPI(lua_State* L); +#define TORTUGA_TRIGGER_API "trigger" +LUAMOD_API int openTriggerAPI(lua_State* L); #endif \ No newline at end of file diff --git a/server/waypoints/waypoint_data.cpp b/server/triggers/trigger_data.cpp similarity index 74% rename from server/waypoints/waypoint_data.cpp rename to server/triggers/trigger_data.cpp index 3225834..438750f 100644 --- a/server/waypoints/waypoint_data.cpp +++ b/server/triggers/trigger_data.cpp @@ -19,28 +19,28 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "waypoint_data.hpp" +#include "trigger_data.hpp" -int WaypointData::SetTriggerReference(int i) { - return triggerRef = i; +int TriggerData::SetScriptReference(int i) { + return scriptRef = i; } -int WaypointData::GetTriggerReference() { - return triggerRef; +int TriggerData::GetScriptReference() { + return scriptRef; } -BoundingBox WaypointData::SetBoundingBox(BoundingBox b) { +BoundingBox TriggerData::SetBoundingBox(BoundingBox b) { return bounds = b; } -BoundingBox WaypointData::GetBoundingBox() { +BoundingBox TriggerData::GetBoundingBox() { return bounds; } -Vector2 WaypointData::SetOrigin(Vector2 v) { +Vector2 TriggerData::SetOrigin(Vector2 v) { return origin = v; } -Vector2 WaypointData::GetOrigin() { +Vector2 TriggerData::GetOrigin() { return origin; } diff --git a/server/waypoints/waypoint_data.hpp b/server/triggers/trigger_data.hpp similarity index 82% rename from server/waypoints/waypoint_data.hpp rename to server/triggers/trigger_data.hpp index 6104847..9d2f209 100644 --- a/server/waypoints/waypoint_data.hpp +++ b/server/triggers/trigger_data.hpp @@ -19,8 +19,8 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef WAYPOINTDATA_HPP_ -#define WAYPOINTDATA_HPP_ +#ifndef TRIGGERDATA_HPP_ +#define TRIGGERDATA_HPP_ #include "bounding_box.hpp" #include "vector2.hpp" @@ -29,10 +29,10 @@ #include -class WaypointData { +class TriggerData { public: - WaypointData() = default; - ~WaypointData() = default; + TriggerData() = default; + ~TriggerData() = default; Vector2 SetOrigin(Vector2 v); Vector2 GetOrigin(); @@ -40,15 +40,15 @@ public: BoundingBox SetBoundingBox(BoundingBox b); BoundingBox GetBoundingBox(); - int SetTriggerReference(int i); - int GetTriggerReference(); + int SetScriptReference(int i); + int GetScriptReference(); private: - friend class WaypointManager; + friend class TriggerManager; Vector2 origin; BoundingBox bounds; - int triggerRef = LUA_NOREF; + int scriptRef = LUA_NOREF; }; #endif \ No newline at end of file diff --git a/server/waypoints/waypoint_manager.cpp b/server/triggers/trigger_manager.cpp similarity index 58% rename from server/waypoints/waypoint_manager.cpp rename to server/triggers/trigger_manager.cpp index f81ad90..9ed9b75 100644 --- a/server/waypoints/waypoint_manager.cpp +++ b/server/triggers/trigger_manager.cpp @@ -19,47 +19,47 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "waypoint_manager.hpp" +#include "trigger_manager.hpp" -WaypointManager::WaypointManager() { +TriggerManager::TriggerManager() { //EMPTY } -WaypointManager::~WaypointManager() { +TriggerManager::~TriggerManager() { UnloadAll(); } -int WaypointManager::Create() { +int TriggerManager::Create() { //implicitly creates the element - WaypointData& waypointData = elementMap[counter]; + TriggerData& triggerData = elementMap[counter]; //no real values set - waypointData.origin = {0, 0}; - waypointData.bounds = {0, 0, 0, 0}; + triggerData.origin = {0, 0}; + triggerData.bounds = {0, 0, 0, 0}; return counter++; } -int WaypointManager::Create(Vector2 origin, BoundingBox bounds) { +int TriggerManager::Create(Vector2 origin, BoundingBox bounds) { //implicitly creates the element - WaypointData& waypointData = elementMap[counter]; + TriggerData& triggerData = elementMap[counter]; - waypointData.origin = origin; - waypointData.bounds = bounds; + triggerData.origin = origin; + triggerData.bounds = bounds; return counter++; } -void WaypointManager::Unload(int uid) { +void TriggerManager::Unload(int uid) { elementMap.erase(uid); } -void WaypointManager::UnloadAll() { +void TriggerManager::UnloadAll() { elementMap.clear(); } -void WaypointManager::UnloadIf(std::function)> fn) { - std::map::iterator it = elementMap.begin(); +void TriggerManager::UnloadIf(std::function)> fn) { + std::map::iterator it = elementMap.begin(); while (it != elementMap.end()) { if (fn(*it)) { it = elementMap.erase(it); @@ -70,8 +70,8 @@ void WaypointManager::UnloadIf(std::function::iterator it = elementMap.find(uid); +TriggerData* TriggerManager::Get(int uid) { + std::map::iterator it = elementMap.find(uid); if (it == elementMap.end()) { return nullptr; @@ -80,19 +80,19 @@ WaypointData* WaypointManager::Get(int uid) { return &it->second; } -int WaypointManager::GetLoadedCount() { +int TriggerManager::GetLoadedCount() { return elementMap.size(); } -std::map* WaypointManager::GetContainer() { +std::map* TriggerManager::GetContainer() { return &elementMap; } //hooks -lua_State* WaypointManager::SetLuaState(lua_State* L) { +lua_State* TriggerManager::SetLuaState(lua_State* L) { return lua = L; } -lua_State* WaypointManager::GetLuaState() { +lua_State* TriggerManager::GetLuaState() { return lua; } \ No newline at end of file diff --git a/server/waypoints/waypoint_manager.hpp b/server/triggers/trigger_manager.hpp similarity index 80% rename from server/waypoints/waypoint_manager.hpp rename to server/triggers/trigger_manager.hpp index b1d9bdf..505d506 100644 --- a/server/waypoints/waypoint_manager.hpp +++ b/server/triggers/trigger_manager.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef WAYPOINTMANAGER_HPP_ -#define WAYPOINTMANAGER_HPP_ +#ifndef TRIGGERMANAGER_HPP_ +#define TRIGGERMANAGER_HPP_ #include "bounding_box.hpp" #include "vector2.hpp" -#include "waypoint_data.hpp" +#include "trigger_data.hpp" #include "lua.hpp" @@ -33,10 +33,10 @@ #include //TODO: (1) rename this system to the "trigger" system -class WaypointManager { +class TriggerManager { public: - WaypointManager(); - ~WaypointManager(); + TriggerManager(); + ~TriggerManager(); //common public methods int Create(); @@ -44,12 +44,12 @@ public: void Unload(int uid); void UnloadAll(); - void UnloadIf(std::function)> fn); + void UnloadIf(std::function)> fn); //accessors & mutators - WaypointData* Get(int uid); + TriggerData* Get(int uid); int GetLoadedCount(); - std::map* GetContainer(); + std::map* GetContainer(); //hooks lua_State* SetLuaState(lua_State* L); @@ -57,7 +57,7 @@ public: private: //members - std::map elementMap; + std::map elementMap; lua_State* lua = nullptr; int counter = 0; }; diff --git a/server/waypoints/waypoint_manager_api.cpp b/server/triggers/trigger_manager_api.cpp similarity index 69% rename from server/waypoints/waypoint_manager_api.cpp rename to server/triggers/trigger_manager_api.cpp index a88dc48..f54df9c 100644 --- a/server/waypoints/waypoint_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -19,41 +19,41 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "waypoint_manager_api.hpp" +#include "trigger_manager_api.hpp" -#include "waypoint_manager.hpp" +#include "trigger_manager.hpp" //TODO: figure out a way to iterate through elements of managers from lua static int create(lua_State* L) { - WaypointManager* mgr = static_cast(lua_touserdata(L, 1)); + TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); } static int unload(lua_State* L) { - WaypointManager* mgr = static_cast(lua_touserdata(L, 1)); + TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); } -static int getWaypoint(lua_State* L) { - WaypointManager* mgr = static_cast(lua_touserdata(L, 1)); +static int getTrigger(lua_State* L) { //TODO: (1) named triggers + TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2))); return 1; } static int getLoadedCount(lua_State* L) { - WaypointManager* mgr = static_cast(lua_touserdata(L, 1)); + TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); lua_pushinteger(L, mgr->GetLoadedCount()); return 1; } -static const luaL_Reg waypointManagerLib[] = { +static const luaL_Reg triggerManagerLib[] = { {"Create",create}, {"Unload",unload}, - {"GetWaypoint",getWaypoint}, + {"GetTrigger",getTrigger}, {"GetCount",getLoadedCount}, {nullptr, nullptr} }; -LUAMOD_API int openWaypointManagerAPI(lua_State* L) { - luaL_newlib(L, waypointManagerLib); +LUAMOD_API int openTriggerManagerAPI(lua_State* L) { + luaL_newlib(L, triggerManagerLib); return 1; } \ No newline at end of file diff --git a/server/waypoints/waypoint_system_api.hpp b/server/triggers/trigger_manager_api.hpp similarity index 84% rename from server/waypoints/waypoint_system_api.hpp rename to server/triggers/trigger_manager_api.hpp index ffb748d..fd0ca87 100644 --- a/server/waypoints/waypoint_system_api.hpp +++ b/server/triggers/trigger_manager_api.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef WAYPOINTSYSTEMAPI_HPP_ -#define WAYPOINTSYSTEMAPI_HPP_ +#ifndef TRIGGERMANAGERAPI_HPP_ +#define TRIGGERMANAGERAPI_HPP_ #include "lua.hpp" -#define TORTUGA_WAYPOINT_SYSTEM_API "waypoint_system" -LUAMOD_API int openWaypointSystemAPI(lua_State* L); +#define TORTUGA_TRIGGER_MANAGER_API "trigger_manager" +LUAMOD_API int openTriggerManagerAPI(lua_State* L); #endif \ No newline at end of file diff --git a/server/waypoints/waypoint_system_api.cpp b/server/triggers/trigger_system_api.cpp similarity index 80% rename from server/waypoints/waypoint_system_api.cpp rename to server/triggers/trigger_system_api.cpp index a16f6ea..2c7a231 100644 --- a/server/waypoints/waypoint_system_api.cpp +++ b/server/triggers/trigger_system_api.cpp @@ -19,27 +19,27 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "waypoint_system_api.hpp" +#include "trigger_system_api.hpp" -//all waypoint API headers -#include "waypoint_api.hpp" -#include "waypoint_manager_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 waypoint modules. +//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[] = { - {"Waypoint", openWaypointAPI}, - {"WaypointManager", openWaypointManagerAPI}, + {"Trigger", openTriggerAPI}, + {"TriggerManager", openTriggerManagerAPI}, {nullptr, nullptr} }; -int openWaypointSystemAPI(lua_State* L) { +int openTriggerSystemAPI(lua_State* L) { //create the table luaL_newlibtable(L, libs); diff --git a/server/waypoints/waypoint_manager_api.hpp b/server/triggers/trigger_system_api.hpp similarity index 84% rename from server/waypoints/waypoint_manager_api.hpp rename to server/triggers/trigger_system_api.hpp index 7a7eb19..d917c9d 100644 --- a/server/waypoints/waypoint_manager_api.hpp +++ b/server/triggers/trigger_system_api.hpp @@ -19,12 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef WAYPOINTMANAGERAPI_HPP_ -#define WAYPOINTMANAGERAPI_HPP_ +#ifndef TRIGGERSYSTEMAPI_HPP_ +#define TRIGGERSYSTEMAPI_HPP_ #include "lua.hpp" -#define TORTUGA_WAYPOINT_MANAGER_API "waypoint_manager" -LUAMOD_API int openWaypointManagerAPI(lua_State* L); +#define TORTUGA_TRIGGER_SYSTEM_API "trigger_system" +LUAMOD_API int openTriggerSystemAPI(lua_State* L); #endif \ No newline at end of file From a00ddb314248f01d1ee1ae38bab313d3e46c77af Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 05:33:47 +1100 Subject: [PATCH 06/28] Updated trigger names in the server --- server/linit.cpp | 4 ++-- server/main.cpp | 1 - server/makefile | 4 ++-- server/rooms/makefile | 2 +- server/rooms/room_api.cpp | 6 +++--- server/rooms/room_data.cpp | 6 +++--- server/rooms/room_data.hpp | 6 +++--- server/server_utilities/makefile | 2 +- 8 files changed, 15 insertions(+), 16 deletions(-) diff --git a/server/linit.cpp b/server/linit.cpp index c6bd870..7a83db7 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -42,7 +42,7 @@ #include "monster_system_api.hpp" #include "network_api.hpp" #include "room_system_api.hpp" -#include "waypoint_system_api.hpp" +#include "trigger_system_api.hpp" //these libs are loaded by lua.c and are readily available to any Lua program static const luaL_Reg loadedlibs[] = { @@ -68,7 +68,7 @@ static const luaL_Reg preloadedlibs[] = { {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, {TORTUGA_NETWORK_API, openNetworkAPI}, {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, - {TORTUGA_WAYPOINT_SYSTEM_API, openWaypointSystemAPI}, + {TORTUGA_TRIGGER_SYSTEM_API, openTriggerSystemAPI}, {NULL, NULL} }; diff --git a/server/main.cpp b/server/main.cpp index b7a8953..a69dfb0 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -28,7 +28,6 @@ #include "config_utility.hpp" #include "room_manager.hpp" #include "udp_network_utility.hpp" -#include "waypoint_manager.hpp" #include #include diff --git a/server/makefile b/server/makefile index 0c8fff1..c579660 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #include directories -INCLUDES+=. accounts characters clients entities monsters rooms server_utilities waypoints ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities +INCLUDES+=. accounts characters clients entities monsters rooms server_utilities triggers ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet_types ../common/utilities #libraries #the order of the $(LIBS) is important, at least for MinGW @@ -32,7 +32,7 @@ all: $(OBJ) $(OUT) $(MAKE) -C monsters $(MAKE) -C rooms $(MAKE) -C server_utilities - $(MAKE) -C waypoints + $(MAKE) -C triggers $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) $(OBJ): | $(OBJDIR) diff --git a/server/rooms/makefile b/server/rooms/makefile index 1e22bde..e428985 100644 --- a/server/rooms/makefile +++ b/server/rooms/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../characters ../entities ../monsters ../server_utilities ../waypoints ../../common/gameplay ../../common/map ../../common/utilities +INCLUDES+=. ../characters ../entities ../monsters ../server_utilities ../triggers ../../common/gameplay ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index cac2651..17cc8ae 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -59,9 +59,9 @@ static int getMonsterMgr(lua_State* L) { return 1; } -static int getWaypointMgr(lua_State* L) { +static int getTriggerMgr(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushlightuserdata(L, reinterpret_cast(room->GetWaypointMgr()) ); + lua_pushlightuserdata(L, reinterpret_cast(room->GetTriggerMgr()) ); return 1; } @@ -95,7 +95,7 @@ static const luaL_Reg roomLib[] = { {"GetPager",getPager}, {"GetMonsterMgr",getMonsterMgr}, - {"GetWaypointMgr",getWaypointMgr}, + {"GetTriggerMgr",getTriggerMgr}, {"SetOnTick", setOnTick}, diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 14cf328..1529470 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -61,8 +61,8 @@ MonsterManager* RoomData::GetMonsterMgr() { return &monsterMgr; } -WaypointManager* RoomData::GetWaypointMgr() { - return &waypointMgr; +TriggerManager* RoomData::GetTriggerMgr() { + return &triggerMgr; } std::list* RoomData::GetCharacterList() { @@ -73,7 +73,7 @@ lua_State* RoomData::SetLuaState(lua_State* L) { lua = L; pager.SetLuaState(lua); monsterMgr.SetLuaState(lua); - waypointMgr.SetLuaState(lua); + triggerMgr.SetLuaState(lua); return lua; } diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 1933e68..dabe81d 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -25,7 +25,7 @@ #include "character_data.hpp" #include "monster_manager.hpp" #include "region_pager_lua.hpp" -#include "waypoint_manager.hpp" +#include "trigger_manager.hpp" #include "lua.hpp" @@ -48,7 +48,7 @@ public: RegionPagerLua* GetPager(); MonsterManager* GetMonsterMgr(); - WaypointManager* GetWaypointMgr(); + TriggerManager* GetTriggerMgr(); std::list* GetCharacterList(); //API interfaces @@ -70,7 +70,7 @@ private: //members RegionPagerLua pager; MonsterManager monsterMgr; - WaypointManager waypointMgr; + TriggerManager triggerMgr; std::list characterList; //API diff --git a/server/server_utilities/makefile b/server/server_utilities/makefile index 879e339..2093a63 100644 --- a/server/server_utilities/makefile +++ b/server/server_utilities/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../accounts ../characters ../clients ../entities ../monsters ../rooms ../waypoints ../../common/gameplay ../../common/map ../../common/network ../../common/network/packet_types ../../common/utilities +INCLUDES+=. ../accounts ../characters ../clients ../entities ../monsters ../rooms ../triggers ../../common/gameplay ../../common/map ../../common/network ../../common/network/packet_types ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) From cb63c9b07cecaf5a1451387a1d82699b18e53d46 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 05:43:20 +1100 Subject: [PATCH 07/28] Minor comment tweaks --- client/gameplay_scenes/world_characters.cpp | 2 +- client/gameplay_scenes/world_logic.cpp | 4 ++-- client/menu_scenes/lobby_menu.cpp | 4 ++-- server/characters/character_manager_api.cpp | 2 +- server/server_character_methods.cpp | 2 +- server/server_methods.cpp | 2 +- server/triggers/trigger_manager.hpp | 1 - server/triggers/trigger_manager_api.cpp | 2 +- 8 files changed, 9 insertions(+), 10 deletions(-) diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index 4d40c4c..c790bb6 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -69,7 +69,7 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) { //fill the character's info character->SetOrigin(argPacket->origin); character->SetMotion(argPacket->motion); - character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); //TODO: (1) send the bounds from the server + character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); //TODO: (2) send the bounds from the server character->SetHandle(argPacket->handle); character->SetAvatar(argPacket->avatar); character->SetOwner(argPacket->accountIndex); diff --git a/client/gameplay_scenes/world_logic.cpp b/client/gameplay_scenes/world_logic.cpp index b766140..1085e84 100644 --- a/client/gameplay_scenes/world_logic.cpp +++ b/client/gameplay_scenes/world_logic.cpp @@ -60,7 +60,7 @@ World::World(int* const argClientIndex, int* const argAccountIndex): shutDownButton.SetText("Shut Down"); //load the tilesheet - //TODO: (1) Tile size and tile sheet should be loaded elsewhere + //TODO: (2) Tile size and tile sheet should be loaded elsewhere tileSheet.Load(config["dir.tilesets"] + "overworld.bmp", 32, 32); //Send the character data @@ -221,7 +221,7 @@ void World::KeyDown(SDL_KeyboardEvent const& key) { //hotkeys switch(key.keysym.sym) { case SDLK_ESCAPE: - //TODO: (1) the escape key should actually control menus and stuff + //TODO: (3) the escape key should actually control menus and stuff SendLogoutRequest(); return; } diff --git a/client/menu_scenes/lobby_menu.cpp b/client/menu_scenes/lobby_menu.cpp index d4dcc81..358d5ca 100644 --- a/client/menu_scenes/lobby_menu.cpp +++ b/client/menu_scenes/lobby_menu.cpp @@ -107,8 +107,8 @@ void LobbyMenu::Render(SDL_Surface* const screen) { join.DrawTo(screen); back.DrawTo(screen); - //TODO: (1) draw headers for the server list - //TODO: (1) ping/delay displayed in the server list + //TODO: (3) draw headers for the server list + //TODO: (3) ping/delay displayed in the server list for (int i = 0; i < serverInfo.size(); i++) { //draw the selected server's highlight if (selection == &serverInfo[i]) { diff --git a/server/characters/character_manager_api.cpp b/server/characters/character_manager_api.cpp index d9792d7..378e848 100644 --- a/server/characters/character_manager_api.cpp +++ b/server/characters/character_manager_api.cpp @@ -75,7 +75,7 @@ static int getLoadedCount(lua_State* L) { } static int forEach(lua_State* L) { - //TODO: (1) find a way to update the clients when a script alters a character's data + //TODO: (9) forEach() } static const luaL_Reg characterManagerLib[] = { diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 69246de..57b36e7 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -205,7 +205,7 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) { //check if allowed if (characterData->GetOwner() != argPacket->accountIndex && !accountData->GetModerator() && !accountData->GetAdministrator()) { - //TODO: (2) send to the client? + //TODO: (3) send to the client? std::cerr << "Failed to set character motion due to lack of permissions targeting uid(" << argPacket->characterIndex << ")" << std::endl; return; } diff --git a/server/server_methods.cpp b/server/server_methods.cpp index e1eb5c3..d71c2ad 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -85,5 +85,5 @@ void ServerApplication::hAdminShutdownRequest(ClientPacket* const argPacket) { } void ServerApplication::SaveServerState() { - //TODO: (2) Periodic mass server saves + //TODO: (3) Periodic mass server saves } diff --git a/server/triggers/trigger_manager.hpp b/server/triggers/trigger_manager.hpp index 505d506..2a1c9d7 100644 --- a/server/triggers/trigger_manager.hpp +++ b/server/triggers/trigger_manager.hpp @@ -32,7 +32,6 @@ #include #include -//TODO: (1) rename this system to the "trigger" system class TriggerManager { public: TriggerManager(); diff --git a/server/triggers/trigger_manager_api.cpp b/server/triggers/trigger_manager_api.cpp index f54df9c..6530266 100644 --- a/server/triggers/trigger_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -23,7 +23,7 @@ #include "trigger_manager.hpp" -//TODO: figure out a way to iterate through elements of managers from lua +//TODO: (1) figure out a way to iterate through elements of managers from lua static int create(lua_State* L) { TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); From edcb6f05ced14f2452d8bbe2e6aaa8aac473e452 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 19:35:11 +1100 Subject: [PATCH 08/28] Implemented trigger creation & deletion via lua Triggers now have handles for simple understanding, but there's nothing preventing multiple triggers from sharing the same name. --- client/gameplay_scenes/world_connections.cpp | 1 - rsc/scripts/setup_server.lua | 14 +++ server/triggers/trigger_api.cpp | 17 ++++ server/triggers/trigger_data.cpp | 8 ++ server/triggers/trigger_data.hpp | 6 +- server/triggers/trigger_manager.cpp | 27 +++--- server/triggers/trigger_manager.hpp | 4 +- server/triggers/trigger_manager_api.cpp | 91 +++++++++++++++++++- 8 files changed, 147 insertions(+), 21 deletions(-) diff --git a/client/gameplay_scenes/world_connections.cpp b/client/gameplay_scenes/world_connections.cpp index f0babeb..fb30f96 100644 --- a/client/gameplay_scenes/world_connections.cpp +++ b/client/gameplay_scenes/world_connections.cpp @@ -94,7 +94,6 @@ void World::SendAdminDisconnectForced() { //TODO: (9) World::SendAdminDisconnectForced() } - void World::SendAdminShutdownRequest() { ClientPacket newPacket; diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index f01924f..d336d0e 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -5,6 +5,7 @@ mapSaver = require("map_saver") roomSystem = require("room_system") characterSystem = require("character_system") networkSystem = require("network") +triggerSystem = require("trigger_system") local function dumpTable(t) print(t) @@ -43,4 +44,17 @@ end) local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) +--debugging +triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) +trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") +trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") + +print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr)) + +local deleted = triggerSystem.TriggerManager.Unload(triggerMgr, triggerSystem.Trigger.GetHandle(trigger1)) + +print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr)) + +print("deleted: ", deleted) + print("Finished the lua script") diff --git a/server/triggers/trigger_api.cpp b/server/triggers/trigger_api.cpp index 025a946..8b063c6 100644 --- a/server/triggers/trigger_api.cpp +++ b/server/triggers/trigger_api.cpp @@ -23,6 +23,19 @@ #include "trigger_data.hpp" +//hamdle +static int setHandle(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + trigger->SetHandle(lua_tostring(L, 2)); + return 0; +} + +static int getHandle(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, trigger->GetHandle().c_str()); + return 1; +} + //origin static int setOrigin(lua_State* L) { TriggerData* trigger = static_cast(lua_touserdata(L, 1)); @@ -74,6 +87,9 @@ static int getReference(lua_State* L) { } static const luaL_Reg triggerLib[] = { + {"SetHandle", setHandle}, + {"GetHandle", getHandle}, + {"SetOrigin",setOrigin}, {"GetOrigin",getOrigin}, @@ -82,6 +98,7 @@ static const luaL_Reg triggerLib[] = { {"SetScript",setReference}, {"GetScript",getReference}, + {nullptr, nullptr} }; diff --git a/server/triggers/trigger_data.cpp b/server/triggers/trigger_data.cpp index 438750f..a6521af 100644 --- a/server/triggers/trigger_data.cpp +++ b/server/triggers/trigger_data.cpp @@ -21,6 +21,14 @@ */ #include "trigger_data.hpp" +std::string TriggerData::SetHandle(std::string s) { + return handle = s; +} + +std::string TriggerData::GetHandle() const { + return handle; +} + int TriggerData::SetScriptReference(int i) { return scriptRef = i; } diff --git a/server/triggers/trigger_data.hpp b/server/triggers/trigger_data.hpp index 9d2f209..cb6409c 100644 --- a/server/triggers/trigger_data.hpp +++ b/server/triggers/trigger_data.hpp @@ -34,6 +34,9 @@ public: TriggerData() = default; ~TriggerData() = default; + std::string SetHandle(std::string); + std::string GetHandle() const; + Vector2 SetOrigin(Vector2 v); Vector2 GetOrigin(); @@ -44,8 +47,7 @@ public: int GetScriptReference(); private: - friend class TriggerManager; - + std::string handle; Vector2 origin; BoundingBox bounds; int scriptRef = LUA_NOREF; diff --git a/server/triggers/trigger_manager.cpp b/server/triggers/trigger_manager.cpp index 9ed9b75..238af08 100644 --- a/server/triggers/trigger_manager.cpp +++ b/server/triggers/trigger_manager.cpp @@ -29,23 +29,13 @@ TriggerManager::~TriggerManager() { UnloadAll(); } -int TriggerManager::Create() { +int TriggerManager::Create(std::string handle, Vector2 origin, BoundingBox bounds) { //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; + triggerData.SetHandle(handle); + triggerData.SetOrigin(origin); + triggerData.SetBoundingBox(bounds); return counter++; } @@ -80,6 +70,15 @@ TriggerData* TriggerManager::Get(int uid) { return &it->second; } +TriggerData* TriggerManager::Get(std::string handle) { + for (std::map::iterator it = elementMap.begin(); it != elementMap.end(); ++it) { + if (it->second.GetHandle() == handle) { + return &it->second; + } + } + return nullptr; +} + int TriggerManager::GetLoadedCount() { return elementMap.size(); } diff --git a/server/triggers/trigger_manager.hpp b/server/triggers/trigger_manager.hpp index 2a1c9d7..cd64fb3 100644 --- a/server/triggers/trigger_manager.hpp +++ b/server/triggers/trigger_manager.hpp @@ -38,8 +38,7 @@ public: ~TriggerManager(); //common public methods - int Create(); - int Create(Vector2 origin, BoundingBox bounds); + int Create(std::string handle, Vector2 origin, BoundingBox bounds); void Unload(int uid); void UnloadAll(); @@ -47,6 +46,7 @@ public: //accessors & mutators TriggerData* Get(int uid); + TriggerData* Get(std::string handle); int GetLoadedCount(); std::map* GetContainer(); diff --git a/server/triggers/trigger_manager_api.cpp b/server/triggers/trigger_manager_api.cpp index 6530266..f44e3ac 100644 --- a/server/triggers/trigger_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -27,18 +27,105 @@ static int create(lua_State* L) { TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); + + //pad the stack with default parameters + if (lua_gettop(L) == 2) { + lua_pushnumber(L, 0.0); //vector.x + lua_pushnumber(L, 0.0); //vector.y + } + if (lua_gettop(L) == 4) { + lua_pushinteger(L, 0); //bounds.x + lua_pushinteger(L, 0); //bounds.y + lua_pushinteger(L, 0); //bounds.w + lua_pushinteger(L, 0); //bounds.h + } + + //create the trigger + int index = mgr->Create( + lua_tostring(L, 2), //handle + { + lua_tonumber(L, 3), //vector.x + lua_tonumber(L, 4) //vector.y + }, + { + lua_tointeger(L, 5), //bounds.x + lua_tointeger(L, 6), //bounds.y + lua_tointeger(L, 7), //bounds.w + lua_tointeger(L, 8) //bounds.h + }); + + //push to the scipts + lua_pushlightuserdata(L, static_cast(mgr->Get(index)) ); + lua_pushinteger(L, index); + + return 2; } static int unload(lua_State* L) { TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); + int count = 0; //the number removed + + //based on the type + switch(lua_type(L, 2)) { + //unload this index + case LUA_TNUMBER: + mgr->UnloadIf([L, &count](std::pair it) -> bool { + if (it.first == lua_tointeger(L, 2)) { + count++; + return true; + } + else { + return false; + } + }); + break; + + //unload this name + case LUA_TSTRING: + mgr->UnloadIf([L, &count](std::pair it) -> bool { + if (it.second.GetHandle() == lua_tostring(L, 2)) { + count++; + return true; + } + else { + return false; + } + }); + break; + } + + //return the number removed + lua_pushinteger(L, count); + return 1; } -static int getTrigger(lua_State* L) { //TODO: (1) named triggers +static int getTrigger(lua_State* L) { TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); - lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2))); + TriggerData* triggerData = nullptr; + + switch(lua_type(L, 2)) { + case LUA_TNUMBER: + triggerData = mgr->Get(lua_tointeger(L, 2)); + break; + case LUA_TSTRING: + triggerData = mgr->Get(lua_tostring(L, 2)); + break; + } + + if (triggerData) { + lua_pushlightuserdata(L, static_cast(triggerData)); + } + else { + lua_pushnil(L); + } + return 1; } +static int forEach(lua_State* L) { + //TODO: (9) forEach() +} + static int getLoadedCount(lua_State* L) { TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); lua_pushinteger(L, mgr->GetLoadedCount()); From b8806cc2098bec15399d9d87f1b2560457bdfe63 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 2 Mar 2015 02:40:25 +1100 Subject: [PATCH 09/28] Implemented CharacterManager::ForEach() --- rsc/scripts/setup_server.lua | 6 ++++++ server/characters/character_manager_api.cpp | 22 +++++++++++++++++++-- server/rooms/room_api.cpp | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index d336d0e..5289087 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -20,6 +20,9 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) --called ~60 times per second roomSystem.Room.SetOnTick(room, function(room) + characterSystem.CharacterManager.ForEach(function(character) + print(characterSystem.Character.GetHandle(character)) + end) --[[ local character = characterSystem.CharacterManager.GetCharacter("handle") if character ~= nil then @@ -44,6 +47,7 @@ end) local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) +--[[ --debugging triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") @@ -56,5 +60,7 @@ local deleted = triggerSystem.TriggerManager.Unload(triggerMgr, triggerSystem.Tr print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr)) print("deleted: ", deleted) +--]] + print("Finished the lua script") diff --git a/server/characters/character_manager_api.cpp b/server/characters/character_manager_api.cpp index 378e848..a853f39 100644 --- a/server/characters/character_manager_api.cpp +++ b/server/characters/character_manager_api.cpp @@ -23,6 +23,11 @@ #include "character_manager.hpp" +#include +#include + +//TODO: (1) character hooks? + static int setOnCreate(lua_State* L) { //TODO: (9) setOnCreate() } @@ -75,7 +80,20 @@ static int getLoadedCount(lua_State* L) { } static int forEach(lua_State* L) { - //TODO: (9) forEach() + CharacterManager& characterMgr = CharacterManager::GetSingleton(); + //pass each character to the given function + for (auto& it : *characterMgr.GetContainer()) { + lua_pushvalue(L, -1); + lua_pushlightuserdata(L, static_cast(&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 const luaL_Reg characterManagerLib[] = { @@ -86,7 +104,7 @@ static const luaL_Reg characterManagerLib[] = { // {"SetOnDelete", setOnDelete}, {"GetCharacter", getCharacter}, {"GetLoadedCount", getLoadedCount}, -// {"ForEach", forEach}, + {"ForEach", forEach}, {nullptr, nullptr} }; diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 17cc8ae..4b7db80 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -67,6 +67,8 @@ static int getTriggerMgr(lua_State* L) { //TODO: character list +//TODO: (1) forEachCharacter + static int setOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference()); From bd68af5875b4ab3a95ef6621e43a375161e5ece8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 2 Mar 2015 04:26:34 +1100 Subject: [PATCH 10/28] Added character iteration to the rooms --- rsc/scripts/setup_server.lua | 2 +- server/rooms/room_api.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 5289087..6cd1c1e 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -20,7 +20,7 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) --called ~60 times per second roomSystem.Room.SetOnTick(room, function(room) - characterSystem.CharacterManager.ForEach(function(character) + roomSystem.Room.ForEachCharacter(room, function(character) print(characterSystem.Character.GetHandle(character)) end) --[[ diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 4b7db80..8328f99 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -23,6 +23,9 @@ #include "room_data.hpp" +#include +#include + static int setRoomName(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); room->SetName(lua_tostring(L, 2)); @@ -67,7 +70,22 @@ static int getTriggerMgr(lua_State* L) { //TODO: character list -//TODO: (1) forEachCharacter +static int forEachCharacter(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + //pass each character to the given function + for (auto& it : *room->GetCharacterList()) { + lua_pushvalue(L, -1); + lua_pushlightuserdata(L, static_cast(it)); + //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) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); @@ -76,6 +94,12 @@ static int setOnTick(lua_State* L) { return 0; } +static int getOnTick(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference()); + return 1; +} + static int initialize(lua_State* L) { RoomData* room = static_cast(lua_touserdata(L, 1)); @@ -99,7 +123,10 @@ static const luaL_Reg roomLib[] = { {"GetMonsterMgr",getMonsterMgr}, {"GetTriggerMgr",getTriggerMgr}, + {"ForEachCharacter", forEachCharacter}, + {"SetOnTick", setOnTick}, + {"GetOnTick", getOnTick}, {"Initialize", initialize}, {nullptr, nullptr} From 18a71439260d1a2dcd37e2ac6a50edbe0668b8cb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Mar 2015 02:21:56 +1100 Subject: [PATCH 11/28] Full trigger creation --- rsc/scripts/setup_server.lua | 33 ++++++++-------- server/rooms/room_api.cpp | 2 +- server/triggers/trigger_api.cpp | 8 ++-- server/triggers/trigger_data.cpp | 18 ++++----- server/triggers/trigger_manager.cpp | 5 +-- server/triggers/trigger_manager.hpp | 2 +- server/triggers/trigger_manager_api.cpp | 50 +++++++++++++------------ 7 files changed, 61 insertions(+), 57 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 6cd1c1e..3edc755 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -19,10 +19,10 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) print("", "Creating room: ", roomSystem.Room.GetName(room), index) --called ~60 times per second - roomSystem.Room.SetOnTick(room, function(room) - roomSystem.Room.ForEachCharacter(room, function(character) - print(characterSystem.Character.GetHandle(character)) - end) +-- roomSystem.Room.SetOnTick(room, function(room) +-- roomSystem.Room.ForEachCharacter(room, function(character) +-- print(characterSystem.Character.GetHandle(character)) +-- end) --[[ local character = characterSystem.CharacterManager.GetCharacter("handle") if character ~= nil then @@ -36,7 +36,7 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) end end --]] - end) +-- end) end) roomSystem.RoomManager.SetOnUnload(function(room, index) @@ -47,20 +47,21 @@ end) local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) ---[[ --debugging +function dumpTrigger(t) + local originX, originY = triggerSystem.Trigger.GetOrigin(t) + local bx, by, bw, bh = triggerSystem.Trigger.GetBounds(t) + local s = triggerSystem.Trigger.GetScript(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, "handle1") - -print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr)) - -local deleted = triggerSystem.TriggerManager.Unload(triggerMgr, triggerSystem.Trigger.GetHandle(trigger1)) - -print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr)) - -print("deleted: ", deleted) ---]] +trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle2", 30.2, 40.2) +trigger3, uid3 = triggerSystem.TriggerManager.Create(triggerMgr, "handle3", 30.2, 40.2, 0, 16, 32, 32, function() end) +dumpTrigger(trigger1) +dumpTrigger(trigger2) +dumpTrigger(trigger3) print("Finished the lua script") diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 8328f99..a04b558 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -96,7 +96,7 @@ static int setOnTick(lua_State* L) { static int getOnTick(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); - lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference()); + lua_rawgeti(L, LUA_REGISTRYINDEX, room->GetTickReference()); return 1; } diff --git a/server/triggers/trigger_api.cpp b/server/triggers/trigger_api.cpp index 8b063c6..7ea007c 100644 --- a/server/triggers/trigger_api.cpp +++ b/server/triggers/trigger_api.cpp @@ -64,10 +64,10 @@ static int setBoundingBox(lua_State* L) { static int getBoundingBox(lua_State* L) { TriggerData* trigger = static_cast(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); + lua_pushinteger(L, trigger->GetBoundingBox().x); + lua_pushinteger(L, trigger->GetBoundingBox().y); + lua_pushinteger(L, trigger->GetBoundingBox().w); + lua_pushinteger(L, trigger->GetBoundingBox().h); return 4; } diff --git a/server/triggers/trigger_data.cpp b/server/triggers/trigger_data.cpp index a6521af..f4edc80 100644 --- a/server/triggers/trigger_data.cpp +++ b/server/triggers/trigger_data.cpp @@ -29,12 +29,12 @@ std::string TriggerData::GetHandle() const { return handle; } -int TriggerData::SetScriptReference(int i) { - return scriptRef = i; +Vector2 TriggerData::SetOrigin(Vector2 v) { + return origin = v; } -int TriggerData::GetScriptReference() { - return scriptRef; +Vector2 TriggerData::GetOrigin() { + return origin; } BoundingBox TriggerData::SetBoundingBox(BoundingBox b) { @@ -45,10 +45,10 @@ BoundingBox TriggerData::GetBoundingBox() { return bounds; } -Vector2 TriggerData::SetOrigin(Vector2 v) { - return origin = v; +int TriggerData::SetScriptReference(int i) { + return scriptRef = i; } -Vector2 TriggerData::GetOrigin() { - return origin; -} +int TriggerData::GetScriptReference() { + return scriptRef; +} \ No newline at end of file diff --git a/server/triggers/trigger_manager.cpp b/server/triggers/trigger_manager.cpp index 238af08..c6de249 100644 --- a/server/triggers/trigger_manager.cpp +++ b/server/triggers/trigger_manager.cpp @@ -29,13 +29,11 @@ TriggerManager::~TriggerManager() { UnloadAll(); } -int TriggerManager::Create(std::string handle, Vector2 origin, BoundingBox bounds) { +int TriggerManager::Create(std::string handle) { //implicitly creates the element TriggerData& triggerData = elementMap[counter]; triggerData.SetHandle(handle); - triggerData.SetOrigin(origin); - triggerData.SetBoundingBox(bounds); return counter++; } @@ -45,6 +43,7 @@ void TriggerManager::Unload(int uid) { } void TriggerManager::UnloadAll() { + //TODO: save? elementMap.clear(); } diff --git a/server/triggers/trigger_manager.hpp b/server/triggers/trigger_manager.hpp index cd64fb3..9fa541c 100644 --- a/server/triggers/trigger_manager.hpp +++ b/server/triggers/trigger_manager.hpp @@ -38,7 +38,7 @@ public: ~TriggerManager(); //common public methods - int Create(std::string handle, Vector2 origin, BoundingBox bounds); + int Create(std::string handle); void Unload(int uid); void UnloadAll(); diff --git a/server/triggers/trigger_manager_api.cpp b/server/triggers/trigger_manager_api.cpp index f44e3ac..572b7dd 100644 --- a/server/triggers/trigger_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -26,36 +26,40 @@ //TODO: (1) figure out a way to iterate through elements of managers from lua static int create(lua_State* L) { + //DOCS: params: create(triggerMgr, name[, originX, originY[, boundsX, boundsY, boundsW, boundsH]][, script]) + + //get the trigger manager TriggerManager* mgr = static_cast(lua_touserdata(L, 1)); - //pad the stack with default parameters - if (lua_gettop(L) == 2) { - lua_pushnumber(L, 0.0); //vector.x - lua_pushnumber(L, 0.0); //vector.y - } - if (lua_gettop(L) == 4) { - lua_pushinteger(L, 0); //bounds.x - lua_pushinteger(L, 0); //bounds.y - lua_pushinteger(L, 0); //bounds.w - lua_pushinteger(L, 0); //bounds.h + //create the trigger + int index = mgr->Create(lua_tostring(L, 2)); + TriggerData* triggerData = mgr->Get(index); + + //origin + if (lua_gettop(L) >= 4) { + triggerData->SetOrigin({lua_tonumber(L, 3), lua_tonumber(L, 4)}); //vectorX, vectorY } - //create the trigger - int index = mgr->Create( - lua_tostring(L, 2), //handle - { - lua_tonumber(L, 3), //vector.x - lua_tonumber(L, 4) //vector.y - }, - { - lua_tointeger(L, 5), //bounds.x - lua_tointeger(L, 6), //bounds.y - lua_tointeger(L, 7), //bounds.w - lua_tointeger(L, 8) //bounds.h + //bounds + if (lua_gettop(L) >= 8) { + triggerData->SetBoundingBox({ + lua_tointeger(L, 5), //boundsX + lua_tointeger(L, 6), //boundsY + lua_tointeger(L, 7), //boundsW + lua_tointeger(L, 8) //boundsH }); + } + + //if the parameter list isn't capped with a script, append a nil instead + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pushnil(L); + } + + //set the script reference (may be nil) + triggerData->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX)); //push to the scipts - lua_pushlightuserdata(L, static_cast(mgr->Get(index)) ); + lua_pushlightuserdata(L, static_cast(triggerData)); lua_pushinteger(L, index); return 2; From 74ed93ddc789380393fc2ee5d36ac43827f640d4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Mar 2015 04:06:16 +1100 Subject: [PATCH 12/28] Character bounds are stored in the server database This allows for customization of bounding boxes, as well as highlighting aspects needed when adding new fields to the characters. --- client/gameplay_scenes/world_characters.cpp | 9 +-- .../network/packet_types/character_packet.cpp | 14 +++++ .../network/packet_types/character_packet.hpp | 2 + common/network/serial_packet.hpp | 2 +- rsc/scripts/setup_server.sql | 6 +- server/characters/character_manager.cpp | 62 +++++++++++++++++-- server/entities/entity.cpp | 8 +++ server/entities/entity.hpp | 4 ++ server/entities/entity_api.cpp | 22 +++++++ server/server_utilities/server_utilities.cpp | 1 + 10 files changed, 118 insertions(+), 12 deletions(-) diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index c790bb6..b0ada57 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -67,19 +67,20 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) { BaseCharacter* character = &characterMap[argPacket->characterIndex]; //fill the character's info - character->SetOrigin(argPacket->origin); - character->SetMotion(argPacket->motion); - character->SetBounds({CHARACTER_BOUNDS_X, CHARACTER_BOUNDS_Y, CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT}); //TODO: (2) send the bounds from the server character->SetHandle(argPacket->handle); character->SetAvatar(argPacket->avatar); character->SetOwner(argPacket->accountIndex); + character->SetOrigin(argPacket->origin); + character->SetMotion(argPacket->motion); + character->SetBounds(argPacket->bounds); + character->CorrectSprite(); //check for this player's character if (character->GetOwner() == accountIndex) { localCharacter = static_cast(character); - //focus the camera on this character + //focus the camera on this character's sprite camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2); camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2); diff --git a/common/network/packet_types/character_packet.cpp b/common/network/packet_types/character_packet.cpp index 98083f3..7906230 100644 --- a/common/network/packet_types/character_packet.cpp +++ b/common/network/packet_types/character_packet.cpp @@ -36,11 +36,18 @@ void serializeCharacter(void* buffer, CharacterPacket* packet) { //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)); + 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)); + //gameplay components: equipment, items, buffs, debuffs... } @@ -57,10 +64,17 @@ void deserializeCharacter(void* buffer, CharacterPacket* packet) { //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)); + 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)); + //gameplay components: equipment, items, buffs, debuffs... } diff --git a/common/network/packet_types/character_packet.hpp b/common/network/packet_types/character_packet.hpp index c7e1040..a791bf9 100644 --- a/common/network/packet_types/character_packet.hpp +++ b/common/network/packet_types/character_packet.hpp @@ -24,6 +24,7 @@ #include "serial_packet_base.hpp" +#include "bounding_box.hpp" #include "vector2.hpp" struct CharacterPacket : SerialPacketBase { @@ -39,6 +40,7 @@ struct CharacterPacket : SerialPacketBase { int roomIndex; Vector2 origin; Vector2 motion; + BoundingBox bounds; }; void serializeCharacter(void* buffer, CharacterPacket* packet); diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index 180c8dd..dc6083a 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -34,7 +34,7 @@ typedef SerialPacketBase SerialPacket; //DOCS: NETWORK_VERSION is used to discern compatible servers and clients -constexpr int NETWORK_VERSION = 20150221; +constexpr int NETWORK_VERSION = 20150304; union MaxPacket { CharacterPacket a; diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index a4e3e5b..e8f1ec3 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -24,10 +24,14 @@ CREATE TABLE IF NOT EXISTS Characters ( avatar varchar(100), birth timestamp NOT NULL DEFAULT (datetime()), - --position in the world + --physically exists in the world roomIndex INTEGER DEFAULT 0, originX INTEGER DEFAULT 0, originY INTEGER DEFAULT 0, + boundsX INTEGER DEFAULT 0, + boundsY INTEGER DEFAULT 0, + boundsW INTEGER DEFAULT 0, + boundsH INTEGER DEFAULT 0, --statistics baseStats INTEGER REFERENCES StatisticSets(uid), diff --git a/server/characters/character_manager.cpp b/server/characters/character_manager.cpp index 300ee18..a094f51 100644 --- a/server/characters/character_manager.cpp +++ b/server/characters/character_manager.cpp @@ -23,6 +23,8 @@ #include "sqlite3.h" +#include "character_defines.hpp" + #include #include @@ -30,10 +32,45 @@ //Define the queries //------------------------- -static const char* CREATE_CHARACTER = "INSERT INTO Characters (owner, handle, avatar) VALUES (?, ?, ?);"; -static const char* LOAD_CHARACTER = "SELECT * FROM Characters WHERE handle = ?;"; -static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET roomIndex = ?2, originX = ?3, originY = ?4 WHERE uid = ?1;"; +//NOTE: Programmer set variables are NOT zero-indexed +//NOTE: SQLite3 returned variables (i.e. loading) ARE zero-indexed + +static const char* CREATE_CHARACTER = "INSERT INTO Characters (" + "owner, " + "handle, " + "avatar, " + "boundsX, " + "boundsY, " + "boundsW, " + "boundsH" + ") VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7);"; + +static const char* LOAD_CHARACTER = "SELECT " + "uid, " + "owner, " + "handle, " + "avatar, " + "roomIndex, " + "originX, " + "originY, " + "boundsX, " + "boundsY, " + "boundsW, " + "boundsH " + "FROM Characters WHERE handle = ?;"; + +static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET " + "roomIndex = ?2, " + "originX = ?3, " + "originY = ?4, " + "boundsX = ?5, " + "boundsY = ?6, " + "boundsW = ?7, " + "boundsH = ?8 " + "WHERE uid = ?1;"; + static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;"; + static const char* COUNT_CHARACTER_RECORDS = "SELECT COUNT(*) FROM Characters;"; //------------------------- @@ -55,6 +92,10 @@ int CharacterManager::Create(int owner, std::string handle, std::string avatar) ret |= sqlite3_bind_int(statement, 1, owner); ret |= sqlite3_bind_text(statement, 2, handle.c_str(), handle.size() + 1, SQLITE_STATIC); ret |= sqlite3_bind_text(statement, 3, avatar.c_str(), avatar.size() + 1, SQLITE_STATIC); + ret |= sqlite3_bind_int(statement, 4, CHARACTER_BOUNDS_X); + ret |= sqlite3_bind_int(statement, 5, CHARACTER_BOUNDS_Y); + ret |= sqlite3_bind_int(statement, 6, CHARACTER_BOUNDS_WIDTH); + ret |= sqlite3_bind_int(statement, 7, CHARACTER_BOUNDS_HEIGHT); //check for binding errors if (ret) { @@ -121,9 +162,14 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) { //Don't cache the birth //world origin - newChar.roomIndex = sqlite3_column_int(statement, 5); - newChar.origin.x = (double)sqlite3_column_int(statement, 6); - newChar.origin.y = (double)sqlite3_column_int(statement, 7); + newChar.roomIndex = sqlite3_column_int(statement, 4); + newChar.origin.x = (double)sqlite3_column_int(statement, 5); + newChar.origin.y = (double)sqlite3_column_int(statement, 6); + //bounds + newChar.bounds.x = (int)sqlite3_column_int(statement, 7); + newChar.bounds.y = (int)sqlite3_column_int(statement, 8); + newChar.bounds.w = (int)sqlite3_column_int(statement, 9); + newChar.bounds.h = (int)sqlite3_column_int(statement, 10); //gameplay components: equipment, items, buffs, debuffs... @@ -165,6 +211,10 @@ int CharacterManager::Save(int uid) { ret |= sqlite3_bind_int(statement, 2, character.roomIndex) != SQLITE_OK; ret |= sqlite3_bind_int(statement, 3, (int)character.origin.x) != SQLITE_OK; ret |= sqlite3_bind_int(statement, 4, (int)character.origin.y) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 5, character.bounds.x) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 6, character.bounds.y) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 7, character.bounds.w) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 8, character.bounds.h) != SQLITE_OK; //gameplay components: equipment, items, buffs, debuffs... diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index 5587e81..2c3ec33 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -33,6 +33,10 @@ Vector2 Entity::SetMotion(Vector2 v) { return motion = v; } +BoundingBox Entity::SetBounds(BoundingBox b) { + return bounds = b; +} + int Entity::GetRoomIndex() const { return roomIndex; } @@ -43,4 +47,8 @@ Vector2 Entity::GetOrigin() const { Vector2 Entity::GetMotion() const { return motion; +} + +BoundingBox Entity::GetBounds() const { + return bounds; } \ No newline at end of file diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index 6874a7e..ab819c7 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -22,6 +22,7 @@ #ifndef ENTITY_HPP_ #define ENTITY_HPP_ +#include "bounding_box.hpp" #include "vector2.hpp" //The base class for all objects in the world @@ -31,10 +32,12 @@ public: int SetRoomIndex(int i); Vector2 SetOrigin(Vector2 v); Vector2 SetMotion(Vector2 v); + BoundingBox SetBounds(BoundingBox b); int GetRoomIndex() const; Vector2 GetOrigin() const; Vector2 GetMotion() const; + BoundingBox GetBounds() const; protected: Entity() = default; @@ -43,6 +46,7 @@ protected: int roomIndex = -1; Vector2 origin; Vector2 motion; + BoundingBox bounds; }; #endif \ No newline at end of file diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index bd7d702..584df5c 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -42,6 +42,17 @@ static int setMotion(lua_State* L) { return 0; } +static int setBounds(lua_State* L) { + Entity* entity = static_cast(lua_touserdata(L, 1)); + entity->SetBounds({ + lua_tointeger(L, 2), + lua_tointeger(L, 3), + lua_tointeger(L, 4), + lua_tointeger(L, 5) + }); + return 0; +} + static int getRoomIndex(lua_State* L) { Entity* entity = static_cast(lua_touserdata(L, 1)); lua_pushinteger(L, entity->GetRoomIndex()); @@ -62,13 +73,24 @@ static int getMotion(lua_State* L) { return 2; } +static int getBounds(lua_State* L) { + Entity* entity = static_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, entity->GetBounds().x); + lua_pushinteger(L, entity->GetBounds().y); + lua_pushinteger(L, entity->GetBounds().w); + lua_pushinteger(L, entity->GetBounds().h); + return 4; +} + static const luaL_Reg entityLib[] = { {"SetRoomIndex", setRoomIndex}, {"SetOrigin", setOrigin}, {"SetMotion", setMotion}, + {"SetBounds", setBounds}, {"GetRoomIndex", getRoomIndex}, {"GetOrigin", getOrigin}, {"GetMotion", getMotion}, + {"GetBounds", getBounds}, {nullptr, nullptr} }; diff --git a/server/server_utilities/server_utilities.cpp b/server/server_utilities/server_utilities.cpp index 58b81ee..edc664f 100644 --- a/server/server_utilities/server_utilities.cpp +++ b/server/server_utilities/server_utilities.cpp @@ -148,4 +148,5 @@ void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex) { packet->roomIndex = characterData->GetRoomIndex(); packet->origin = characterData->GetOrigin(); packet->motion = characterData->GetMotion(); + packet->bounds = characterData->GetBounds(); } From 41d6314bebae9ffe0df0aae7d24c8ea0e2d0b76d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Mar 2015 06:16:12 +1100 Subject: [PATCH 13/28] 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 From a8c309ec9d301cfdac4c12ab48357d2990324199 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 6 Mar 2015 00:13:12 +1100 Subject: [PATCH 14/28] Minor comment tweaks --- server/accounts/account_manager.cpp | 1 + server/rooms/room_data.cpp | 9 ++++----- server/triggers/trigger_manager_api.cpp | 2 -- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/server/accounts/account_manager.cpp b/server/accounts/account_manager.cpp index 7b2a423..02dc85c 100644 --- a/server/accounts/account_manager.cpp +++ b/server/accounts/account_manager.cpp @@ -27,6 +27,7 @@ //Define the queries //------------------------- +//TODO: (1) ensure this is independant of column order static const char* CREATE_USER_ACCOUNT = "INSERT INTO Accounts (username) VALUES (?);"; static const char* LOAD_USER_ACCOUNT = "SELECT * FROM Accounts WHERE username = ?;"; static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET blacklisted = ?2, whitelisted = ?3, mod = ?4, admin = ?5 WHERE uid = ?1;"; diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index a51b09e..4663735 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -47,17 +47,16 @@ void RoomData::RunFrame() { it.second.Update(); } - //compare the triggers to the entities + //compare the triggers to the entities, using their real hitboxes for (auto& it : *triggerMgr.GetContainer()) { + BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); 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 + //trigger script lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); - lua_pushlightuserdata(lua, character); + lua_pushlightuserdata(lua, character); //TODO: (1) entity type //run the script if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { diff --git a/server/triggers/trigger_manager_api.cpp b/server/triggers/trigger_manager_api.cpp index 572b7dd..580ea5e 100644 --- a/server/triggers/trigger_manager_api.cpp +++ b/server/triggers/trigger_manager_api.cpp @@ -23,8 +23,6 @@ #include "trigger_manager.hpp" -//TODO: (1) figure out a way to iterate through elements of managers from lua - static int create(lua_State* L) { //DOCS: params: create(triggerMgr, name[, originX, originY[, boundsX, boundsY, boundsW, boundsH]][, script]) From 01502372c95e33018c0449c65382bf18352d16ff Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 7 Mar 2015 15:09:19 +1100 Subject: [PATCH 15/28] Correcting a bug with unimplemented MonsterManager --- rsc/scripts/setup_server.lua | 6 ++++++ server/monsters/monster_manager.cpp | 22 +++++++++++----------- server/rooms/room_data.cpp | 4 +--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 56084c4..b69b3b4 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -67,6 +67,12 @@ function dumpTrigger(t) print(triggerSystem.Trigger.GetHandle(t), originX, originY, bx, by, bw, bh, s) end +function dumpTable(t) + for k, v in pairs(t) do + print("", k, v) + end +end + --[[ triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") diff --git a/server/monsters/monster_manager.cpp b/server/monsters/monster_manager.cpp index b0f3d11..cd54bc1 100644 --- a/server/monsters/monster_manager.cpp +++ b/server/monsters/monster_manager.cpp @@ -30,45 +30,45 @@ MonsterManager::~MonsterManager() { } int MonsterManager::Create(std::string) { - //Create + //TODO: (9) MonsterManager::Create() } void MonsterManager::Unload(int uid) { - //Unload + //TODO: (9) MonsterManager::Unload() } void MonsterManager::UnloadAll() { - //UnloadAll + //TODO: (9) MonsterManager::UnloadAll() } void MonsterManager::UnloadIf(std::function)> fn) { - //UnloadIf + //TODO: (9) MonsterManager::UnloadIf() } MonsterData* MonsterManager::Get(int uid) { - //Get + //TODO: (9) MonsterManager::Get() } int MonsterManager::GetLoadedCount() { - //GetLoadedCount + //TODO: (9) MonsterManager::GetLoadedCount() } std::map* MonsterManager::GetContainer() { - //GetContainer + //TODO: (9) MonsterManager::GetContainer() } lua_State* MonsterManager::SetLuaState(lua_State* L) { - //SetLuaState + //TODO: (9) MonsterManager::SetLuaState() } lua_State* MonsterManager::GetLuaState() { - //GetLuaState + //TODO: (9) MonsterManager::GetLuaState() } sqlite3* MonsterManager::SetDatabase(sqlite3* db) { - //SetDatabase + //TODO: (9) MonsterManager::SetDatabase() } sqlite3* MonsterManager::GetDatabase() { - //GetDatabase + //TODO: (9) MonsterManager::GetDatabase() } diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 4663735..1148a53 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -43,9 +43,7 @@ void RoomData::RunFrame() { for (auto& it : characterList) { it->Update(); } - for (auto& it : *monsterMgr.GetContainer()) { - it.second.Update(); - } + //TODO: (3) iterate through the monster map //compare the triggers to the entities, using their real hitboxes for (auto& it : *triggerMgr.GetContainer()) { From 501b1e9814206635dc697f904636618aa8e7bec5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 8 Mar 2015 20:08:12 +1100 Subject: [PATCH 16/28] Reduced script verbosity to a degree --- common/map/map_system_api.cpp | 55 ----------- common/map/map_system_api.hpp | 30 ------ common/map/region_api.hpp | 2 +- common/map/region_pager_api.cpp | 1 + common/map/region_pager_api.hpp | 2 +- rsc/scripts/map_maker.lua | 50 +++++----- rsc/scripts/map_saver.lua | 6 +- rsc/scripts/setup_server.lua | 110 ++++++++------------- server/characters/character_system_api.cpp | 55 ----------- server/characters/character_system_api.hpp | 30 ------ server/linit.cpp | 30 ++++-- server/monsters/monster_system_api.cpp | 55 ----------- server/monsters/monster_system_api.hpp | 30 ------ server/rooms/room_system_api.cpp | 55 ----------- server/rooms/room_system_api.hpp | 30 ------ server/triggers/trigger_system_api.cpp | 55 ----------- server/triggers/trigger_system_api.hpp | 30 ------ 17 files changed, 90 insertions(+), 536 deletions(-) delete mode 100644 common/map/map_system_api.cpp delete mode 100644 common/map/map_system_api.hpp delete mode 100644 server/characters/character_system_api.cpp delete mode 100644 server/characters/character_system_api.hpp delete mode 100644 server/monsters/monster_system_api.cpp delete mode 100644 server/monsters/monster_system_api.hpp delete mode 100644 server/rooms/room_system_api.cpp delete mode 100644 server/rooms/room_system_api.hpp delete mode 100644 server/triggers/trigger_system_api.cpp delete mode 100644 server/triggers/trigger_system_api.hpp diff --git a/common/map/map_system_api.cpp b/common/map/map_system_api.cpp deleted file mode 100644 index 7a980c7..0000000 --- a/common/map/map_system_api.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* 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 "map_system_api.hpp" - -//all map API headers -#include "region_api.hpp" -#include "region_pager_api.hpp" - -//useful "globals" -//... - -//This mimics linit.c to create a nested collection of all map modules. -static const luaL_Reg funcs[] = { - {nullptr, nullptr} -}; - -static const luaL_Reg libs[] = { - {"Region", openRegionAPI}, - {"RegionPager", openRegionPagerAPI}, - {nullptr, nullptr} -}; - -int openMapSystemAPI(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; -} \ No newline at end of file diff --git a/common/map/map_system_api.hpp b/common/map/map_system_api.hpp deleted file mode 100644 index c188e18..0000000 --- a/common/map/map_system_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 MAPSYSTEMAPI_HPP_ -#define MAPSYSTEMAPI_APP_ - -#include "lua.hpp" - -#define TORTUGA_MAP_SYSTEM_API "map_system" -LUAMOD_API int openMapSystemAPI(lua_State* L); - -#endif \ No newline at end of file diff --git a/common/map/region_api.hpp b/common/map/region_api.hpp index cf3ef0e..4529f6e 100644 --- a/common/map/region_api.hpp +++ b/common/map/region_api.hpp @@ -24,7 +24,7 @@ #include "lua.hpp" -#define TORTUGA_REGION_NAME "region" +#define TORTUGA_REGION_API "region" LUAMOD_API int openRegionAPI(lua_State* L); #endif diff --git a/common/map/region_pager_api.cpp b/common/map/region_pager_api.cpp index 73ad6e9..fd366db 100644 --- a/common/map/region_pager_api.cpp +++ b/common/map/region_pager_api.cpp @@ -25,6 +25,7 @@ #include "region.hpp" //DOCS: These glue functions simply wrap RegionPagerLua's methods +//NOTE: zero indexing is used here, but not in the region API static int setTile(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); diff --git a/common/map/region_pager_api.hpp b/common/map/region_pager_api.hpp index 01b01e8..ed36740 100644 --- a/common/map/region_pager_api.hpp +++ b/common/map/region_pager_api.hpp @@ -24,7 +24,7 @@ #include "lua.hpp" -#define TORTUGA_REGION_PAGER_NAME "region_pager" +#define TORTUGA_REGION_PAGER_API "region_pager" LUAMOD_API int openRegionPagerAPI(lua_State* L); #endif diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index 62e4e06..d24f056 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -1,4 +1,4 @@ -local Region = require("map_system").Region +local regionAPI = require("region") local mapMaker = {} @@ -25,38 +25,38 @@ mapMaker.edges.west = -1 function mapMaker.SmoothEdgesSimple(r) --make and pad an array to use local shiftArray = {} - for i = 1, Region.GetWidth(r) do + for i = 1, regionAPI.GetWidth(r) do shiftArray[i] = {} - for j = 1, Region.GetHeight(r) do + for j = 1, regionAPI.GetHeight(r) do shiftArray[i][j] = 0 end end --build the array - for i = 1, Region.GetWidth(r) do - for j = 1, Region.GetHeight(r) do - --if (not region edge) and (west tile < this tile), etc. - if i > 1 and Region.GetTile(r, i - 1, j, 1) < Region.GetTile(r, i, j, 1) then + for i = 1, regionAPI.GetWidth(r) do + for j = 1, regionAPI.GetHeight(r) do + --if (not regionAPI edge) and (west tile < this tile), etc. + if i > 1 and regionAPI.GetTile(r, i - 1, j, 1) < regionAPI.GetTile(r, i, j, 1) then shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.west end - if j > 1 and Region.GetTile(r, i, j - 1, 1) < Region.GetTile(r, i, j, 1) then + if j > 1 and regionAPI.GetTile(r, i, j - 1, 1) < regionAPI.GetTile(r, i, j, 1) then shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.north end - if i < Region.GetWidth(r) and Region.GetTile(r, i + 1, j, 1) < Region.GetTile(r, i, j, 1) then + if i < regionAPI.GetWidth(r) and regionAPI.GetTile(r, i + 1, j, 1) < regionAPI.GetTile(r, i, j, 1) then shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.east end - if j < Region.GetHeight(r) and Region.GetTile(r, i, j + 1, 1) < Region.GetTile(r, i, j, 1) then + if j < regionAPI.GetHeight(r) and regionAPI.GetTile(r, i, j + 1, 1) < regionAPI.GetTile(r, i, j, 1) then shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.south end end end --finally apply this - for i = 1, Region.GetWidth(r) do - for j = 1, Region.GetHeight(r) do + for i = 1, regionAPI.GetWidth(r) do + for j = 1, regionAPI.GetHeight(r) do if shiftArray[i][j] ~= 0 then - Region.SetTile(r, i, j, 2, Region.GetTile(r, i, j, 1) + shiftArray[i][j]) - Region.SetTile(r, i, j, 1, Region.GetTile(r, i, j, 1) - 3) + regionAPI.SetTile(r, i, j, 2, regionAPI.GetTile(r, i, j, 1) + shiftArray[i][j]) + regionAPI.SetTile(r, i, j, 1, regionAPI.GetTile(r, i, j, 1) - 3) end end end @@ -65,27 +65,27 @@ end --custom generation systems here function mapMaker.DebugIsland(r) --basic distance check for each tile, placing an island around the world origin - for i = 1, Region.GetWidth(r) do - for j = 1, Region.GetHeight(r) do - local dist = mapMaker.Dist(0, 0, i + Region.GetX(r) -1, j + Region.GetY(r) -1) + for i = 1, regionAPI.GetWidth(r) do + for j = 1, regionAPI.GetHeight(r) do + local dist = mapMaker.Dist(0, 0, i + regionAPI.GetX(r) -1, j + regionAPI.GetY(r) -1) if dist < 10 then - Region.SetTile(r, i, j, 1, mapMaker.plains) + regionAPI.SetTile(r, i, j, 1, mapMaker.plains) elseif dist < 12 then - Region.SetTile(r, i, j, 1, mapMaker.sand) + regionAPI.SetTile(r, i, j, 1, mapMaker.sand) else - Region.SetTile(r, i, j, 1, mapMaker.water) - Region.SetSolid(r, i, j, true) + regionAPI.SetTile(r, i, j, 1, mapMaker.water) + regionAPI.SetSolid(r, i, j, true) end end end --examples of the smoothing function NOT working correctly --[[ - for j = 1, Region.GetHeight(r) do - Region.SetTile(r, 3, j, 1, mapMaker.dirt) - Region.SetTile(r, 4, j, 1, mapMaker.dirt) + for j = 1, regionAPI.GetHeight(r) do + regionAPI.SetTile(r, 3, j, 1, mapMaker.dirt) + regionAPI.SetTile(r, 4, j, 1, mapMaker.dirt) - Region.SetTile(r, 10, j, 1, mapMaker.dirt) + regionAPI.SetTile(r, 10, j, 1, mapMaker.dirt) end --]] diff --git a/rsc/scripts/map_saver.lua b/rsc/scripts/map_saver.lua index c5b528b..b28d1bd 100644 --- a/rsc/scripts/map_saver.lua +++ b/rsc/scripts/map_saver.lua @@ -1,14 +1,14 @@ -local Region = require("map_system").Region +local region = require("region") local mapSaver = {} function mapSaver.Load(r) --empty - io.write("map_saver:Load(", Region.GetX(r), ", ", Region.GetY(r), ")\n") + io.write("map_saver:Load(", region.GetX(r), ", ", region.GetY(r), ")\n") end function mapSaver.Save(r) --empty - io.write("map_saver:Save(", Region.GetX(r), ", ", Region.GetY(r), ")\n") + io.write("map_saver:Save(", region.GetX(r), ", ", region.GetY(r), ")\n") end --TODO: (3) create a flexible saving & loading system diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index b69b3b4..b056fa4 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,87 +1,55 @@ print("Lua script check") +--requirements +roomManagerAPI = require("room_manager") +roomAPI = require("room") + mapMaker = require("map_maker") mapSaver = require("map_saver") -mapSystem = require("map_system") -roomSystem = require("room_system") -characterSystem = require("character_system") -networkSystem = require("network") -triggerSystem = require("trigger_system") - -local function dumpTable(t) - print(t) - for k, v in pairs(t) do - print("",k,v) - end -end --test the room hooks -roomSystem.RoomManager.SetOnCreate(function(room, index) - print("", "Creating room: ", roomSystem.Room.GetName(room), index) - - --called ~60 times per second --- roomSystem.Room.SetOnTick(room, function(room) --- roomSystem.Room.ForEachCharacter(room, function(character) --- print(characterSystem.Character.GetHandle(character)) --- end) - --[[ - local character = characterSystem.CharacterManager.GetCharacter("handle") - if character ~= nil then - --debugging - local originX, originY = characterSystem.Character.GetOrigin(character) - local motionX, motionY = characterSystem.Character.GetMotion(character) - if motionY < 0 then - characterSystem.Character.SetMotion(character, motionX, 0) - networkSystem.PumpCharacterUpdate(character) - print("Sending: ", motionX, motionY) - end - end - --]] --- end) +roomManagerAPI.SetOnCreate(function(room, index) + print("", "Creating room: ", roomAPI.GetName(room), index) end) -roomSystem.RoomManager.SetOnUnload(function(room, index) - print("", "Unloading room: ", roomSystem.Room.GetName(room), index) +roomManagerAPI.SetOnUnload(function(room, index) + print("", "Unloading room: ", roomAPI.GetName(room), index) end) --NOTE: room 0 is the first that the client asks for, therefore it must exist -local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") -roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) +local overworld, uid = roomManagerAPI.CreateRoom("overworld", "overworld.bmp") +roomAPI.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) +--TODO: (0) What userdata type does the trigger script take as a parameter? + +regionPagerAPI = require("region_pager") +triggerManagerAPI = require("trigger_manager") + +function createTrigger(handle, room, x, y, script) + local pager = roomAPI.GetPager(room) + + --place the indicator tile + regionPagerAPI.SetTile(pager, x, y, 0, mapMaker.dirt) + regionPagerAPI.SetTile(pager, x, y, 1, mapMaker.blank) + regionPagerAPI.SetTile(pager, x, y, 2, mapMaker.blank) + + --create the trigger object + triggerManagerAPI.Create( + roomAPI.GetTriggerMgr(room), handle, x, y, + 0, 0, 32, 32, --size of the tiles + script + ) +end + +characterAPI = require("character") +networkAPI = require("network") + +--simple teleporter +createTrigger("trigger 1", overworld, 0, 0, function(character) + local x, y = characterAPI.GetOrigin(character) + characterAPI.SetOrigin(character, x, y + 128) + networkAPI.PumpCharacterUpdate(character) end) ---debugging -function dumpTrigger(t) - local originX, originY = triggerSystem.Trigger.GetOrigin(t) - local bx, by, bw, bh = triggerSystem.Trigger.GetBounds(t) - local s = triggerSystem.Trigger.GetScript(t) - print(triggerSystem.Trigger.GetHandle(t), originX, originY, bx, by, bw, bh, s) -end - -function dumpTable(t) - for k, v in pairs(t) do - print("", k, v) - end -end - ---[[ -triggerMgr = roomSystem.Room.GetTriggerMgr(overworld) -trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1") -trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle2", 30.2, 40.2) -trigger3, uid3 = triggerSystem.TriggerManager.Create(triggerMgr, "handle3", 30.2, 40.2, 0, 16, 32, 32, function() end) - -dumpTrigger(trigger1) -dumpTrigger(trigger2) -dumpTrigger(trigger3) ---]] - print("Finished the lua script") diff --git a/server/characters/character_system_api.cpp b/server/characters/character_system_api.cpp deleted file mode 100644 index bac7893..0000000 --- a/server/characters/character_system_api.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* 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 "character_system_api.hpp" - -//all character API headers -#include "character_api.hpp" -#include "character_manager_api.hpp" - -//useful "globals" -//... - -//This mimics linit.c to create a nested collection of all character modules. -static const luaL_Reg funcs[] = { - {nullptr, nullptr} -}; - -static const luaL_Reg libs[] = { - {"Character", openCharacterAPI}, - {"CharacterManager", openCharacterManagerAPI}, - {nullptr, nullptr} -}; - -int openCharacterSystemAPI(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; -} diff --git a/server/characters/character_system_api.hpp b/server/characters/character_system_api.hpp deleted file mode 100644 index 67b47a7..0000000 --- a/server/characters/character_system_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 CHARACTERSYSTEMAPI_HPP_ -#define CHARACTERSYSTEMAPI_HPP_ - -#include "lua.hpp" - -#define TORTUGA_CHARACTER_SYSTEM_API "character_system" -LUAMOD_API int openCharacterSystemAPI(lua_State* L); - -#endif \ No newline at end of file diff --git a/server/linit.cpp b/server/linit.cpp index 7a83db7..c0552ea 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -37,12 +37,17 @@ #include "lua.hpp" #include "entity_api.hpp" -#include "character_system_api.hpp" -#include "map_system_api.hpp" -#include "monster_system_api.hpp" +#include "character_api.hpp" +#include "character_manager_api.hpp" +#include "region_api.hpp" +#include "region_pager_api.hpp" +#include "monster_api.hpp" +#include "monster_manager_api.hpp" #include "network_api.hpp" -#include "room_system_api.hpp" -#include "trigger_system_api.hpp" +#include "room_api.hpp" +#include "room_manager_api.hpp" +#include "trigger_api.hpp" +#include "trigger_manager_api.hpp" //these libs are loaded by lua.c and are readily available to any Lua program static const luaL_Reg loadedlibs[] = { @@ -63,12 +68,17 @@ static const luaL_Reg loadedlibs[] = { //these libs are preloaded and must be required before used static const luaL_Reg preloadedlibs[] = { {TORTUGA_ENTITY_API, openEntityAPI}, //required by derived classes - {TORTUGA_CHARACTER_SYSTEM_API, openCharacterSystemAPI}, - {TORTUGA_MAP_SYSTEM_API, openMapSystemAPI}, - {TORTUGA_MONSTER_SYSTEM_API, openMonsterSystemAPI}, + {TORTUGA_CHARACTER_API, openCharacterAPI}, + {TORTUGA_CHARACTER_MANAGER_API, openCharacterManagerAPI}, + {TORTUGA_MONSTER_API, openMonsterAPI}, + {TORTUGA_MONSTER_MANAGER_API, openMonsterManagerAPI}, {TORTUGA_NETWORK_API, openNetworkAPI}, - {TORTUGA_ROOM_SYSTEM_API, openRoomSystemAPI}, - {TORTUGA_TRIGGER_SYSTEM_API, openTriggerSystemAPI}, + {TORTUGA_REGION_API, openRegionAPI}, + {TORTUGA_REGION_PAGER_API, openRegionPagerAPI}, + {TORTUGA_ROOM_API, openRoomAPI}, + {TORTUGA_ROOM_MANAGER_API, openRoomManagerAPI}, + {TORTUGA_TRIGGER_API, openTriggerAPI}, + {TORTUGA_TRIGGER_MANAGER_API, openTriggerManagerAPI}, {NULL, NULL} }; diff --git a/server/monsters/monster_system_api.cpp b/server/monsters/monster_system_api.cpp deleted file mode 100644 index 2ecbf71..0000000 --- a/server/monsters/monster_system_api.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* 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 "monster_system_api.hpp" - -//all monster API headers -#include "monster_api.hpp" -#include "monster_manager_api.hpp" - -//useful "globals" -//... - -//This mimics linit.c to create a nested collection of all monster modules. -static const luaL_Reg funcs[] = { - {nullptr, nullptr} -}; - -static const luaL_Reg libs[] = { - {"Monster", openMonsterAPI}, - {"MonsterManager", openMonsterManagerAPI}, - {nullptr, nullptr} -}; - -int openMonsterSystemAPI(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; -} diff --git a/server/monsters/monster_system_api.hpp b/server/monsters/monster_system_api.hpp deleted file mode 100644 index 843a5e7..0000000 --- a/server/monsters/monster_system_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 MONSTERSYSTEMAPI_HPP_ -#define MONSTERSYSTEMAPI_HPP_ - -#include "lua.hpp" - -#define TORTUGA_MONSTER_SYSTEM_API "monster_system" -LUAMOD_API int openMonsterSystemAPI(lua_State* L); - -#endif \ No newline at end of file diff --git a/server/rooms/room_system_api.cpp b/server/rooms/room_system_api.cpp deleted file mode 100644 index 0d6fa1a..0000000 --- a/server/rooms/room_system_api.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* 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 "room_system_api.hpp" - -//all room API headers -#include "room_api.hpp" -#include "room_manager_api.hpp" - -//useful "globals" -//... - -//This mimics linit.c to create a nested collection of all room modules. -static const luaL_Reg funcs[] = { - {nullptr, nullptr} -}; - -static const luaL_Reg libs[] = { - {"Room", openRoomAPI}, - {"RoomManager", openRoomManagerAPI}, - {nullptr, nullptr} -}; - -int openRoomSystemAPI(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; -} diff --git a/server/rooms/room_system_api.hpp b/server/rooms/room_system_api.hpp deleted file mode 100644 index 29ec4d7..0000000 --- a/server/rooms/room_system_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 ROOMSYSTEMAPI_HPP_ -#define ROOMSYSTEMAPI_HPP_ - -#include "lua.hpp" - -#define TORTUGA_ROOM_SYSTEM_API "room_system" -LUAMOD_API int openRoomSystemAPI(lua_State* L); - -#endif \ No newline at end of file diff --git a/server/triggers/trigger_system_api.cpp b/server/triggers/trigger_system_api.cpp deleted file mode 100644 index 2c7a231..0000000 --- a/server/triggers/trigger_system_api.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* 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; -} diff --git a/server/triggers/trigger_system_api.hpp b/server/triggers/trigger_system_api.hpp deleted file mode 100644 index d917c9d..0000000 --- a/server/triggers/trigger_system_api.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 \ No newline at end of file From decc77e21c5d82d1c4fa65c0e1359820ef74c494 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 9 Mar 2015 09:04:12 +1100 Subject: [PATCH 17/28] Implemented entity.GetType() in lua This will only work for userdata decented from the Entity base class. Using something else results in undefined behavior. --- rsc/scripts/setup_server.lua | 12 ++++++++---- server/characters/character_data.cpp | 4 ++++ server/characters/character_data.hpp | 4 ++-- server/entities/entity.cpp | 8 ++++++++ server/entities/entity.hpp | 13 +++++++++---- server/entities/entity_api.cpp | 7 +++++++ server/monsters/monster_data.cpp | 4 ++++ server/monsters/monster_data.hpp | 6 ++++-- 8 files changed, 46 insertions(+), 12 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index b056fa4..83db60b 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -43,13 +43,17 @@ function createTrigger(handle, room, x, y, script) end characterAPI = require("character") +entityAPI = require("entity") networkAPI = require("network") --simple teleporter -createTrigger("trigger 1", overworld, 0, 0, function(character) - local x, y = characterAPI.GetOrigin(character) - characterAPI.SetOrigin(character, x, y + 128) - networkAPI.PumpCharacterUpdate(character) +createTrigger("trigger 1", overworld, 0, 0, function(entity) + if entityAPI.GetType(entity) ~= "character" then + return + end + local x, y = characterAPI.GetOrigin(entity) + characterAPI.SetOrigin(entity, x, y + 128) + networkAPI.PumpCharacterUpdate(entity) end) print("Finished the lua script") diff --git a/server/characters/character_data.cpp b/server/characters/character_data.cpp index c0549d6..3d9d728 100644 --- a/server/characters/character_data.cpp +++ b/server/characters/character_data.cpp @@ -21,6 +21,10 @@ */ #include "character_data.hpp" +CharacterData::CharacterData(): Entity("character") { + //EMPTY +} + int CharacterData::GetOwner() { return owner; } diff --git a/server/characters/character_data.hpp b/server/characters/character_data.hpp index 6c02134..c61a489 100644 --- a/server/characters/character_data.hpp +++ b/server/characters/character_data.hpp @@ -32,7 +32,7 @@ class CharacterData: public Entity { public: - CharacterData() = default; + CharacterData(); ~CharacterData() = default; //database stuff @@ -43,7 +43,7 @@ public: private: friend class CharacterManager; - int owner; + int owner = -1; std::string handle; std::string avatar; }; diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index b77ea57..dd3249d 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -21,6 +21,10 @@ */ #include "entity.hpp" +Entity::Entity(const char* _type): type(_type) { + //EMPTY +} + void Entity::Update() { origin += motion; } @@ -55,4 +59,8 @@ Vector2 Entity::GetMotion() const { BoundingBox Entity::GetBounds() const { return bounds; +} + +const char* Entity::GetType() const { + return type; } \ No newline at end of file diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index 70b0ea8..623a912 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -25,6 +25,8 @@ #include "bounding_box.hpp" #include "vector2.hpp" +#include + //The base class for all objects in the world class Entity { public: @@ -41,14 +43,17 @@ public: Vector2 GetMotion() const; BoundingBox GetBounds() const; + const char* GetType() const; + protected: - Entity() = default; + Entity(const char*); virtual ~Entity() = default; int roomIndex = -1; - Vector2 origin; - Vector2 motion; - BoundingBox bounds; + Vector2 origin = {0, 0}; + Vector2 motion = {0, 0}; + BoundingBox bounds = {0, 0, 0, 0}; + const char* type; }; #endif \ No newline at end of file diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index 584df5c..30daaea 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -82,6 +82,12 @@ static int getBounds(lua_State* L) { return 4; } +static int getType(lua_State* L) { + Entity* entity = static_cast(lua_touserdata(L, 1)); + lua_pushstring(L, entity->GetType()); + return 1; +} + static const luaL_Reg entityLib[] = { {"SetRoomIndex", setRoomIndex}, {"SetOrigin", setOrigin}, @@ -91,6 +97,7 @@ static const luaL_Reg entityLib[] = { {"GetOrigin", getOrigin}, {"GetMotion", getMotion}, {"GetBounds", getBounds}, + {"GetType", getType}, {nullptr, nullptr} }; diff --git a/server/monsters/monster_data.cpp b/server/monsters/monster_data.cpp index 22f5252..8a29f12 100644 --- a/server/monsters/monster_data.cpp +++ b/server/monsters/monster_data.cpp @@ -21,6 +21,10 @@ */ #include "monster_data.hpp" +MonsterData::MonsterData(): Entity("monster") { + //EMPTY +} + std::string MonsterData::SetAvatar(std::string s) { return avatar = s; } diff --git a/server/monsters/monster_data.hpp b/server/monsters/monster_data.hpp index f02723b..44a5300 100644 --- a/server/monsters/monster_data.hpp +++ b/server/monsters/monster_data.hpp @@ -24,11 +24,13 @@ #include "entity.hpp" +#include "lua.hpp" + #include class MonsterData: public Entity { public: - MonsterData() = default; + MonsterData(); ~MonsterData() = default; std::string SetAvatar(std::string); @@ -41,7 +43,7 @@ private: friend class MonsterManager; std::string avatar; - int scriptRef; + int scriptRef = LUA_NOREF; }; #endif \ No newline at end of file From 3d8ee25ae737b790f0625da1d4e1da69a5282fb3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 9 Mar 2015 09:34:04 +1100 Subject: [PATCH 18/28] AccountManager's SQL is order-independant --- rsc/scripts/setup_server.lua | 2 -- server/accounts/account_manager.cpp | 31 +++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 83db60b..b711273 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -21,8 +21,6 @@ local overworld, uid = roomManagerAPI.CreateRoom("overworld", "overworld.bmp") roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) --debug: test the trigger system ---TODO: (0) What userdata type does the trigger script take as a parameter? - regionPagerAPI = require("region_pager") triggerManagerAPI = require("trigger_manager") diff --git a/server/accounts/account_manager.cpp b/server/accounts/account_manager.cpp index 02dc85c..328ca48 100644 --- a/server/accounts/account_manager.cpp +++ b/server/accounts/account_manager.cpp @@ -27,17 +27,32 @@ //Define the queries //------------------------- -//TODO: (1) ensure this is independant of column order static const char* CREATE_USER_ACCOUNT = "INSERT INTO Accounts (username) VALUES (?);"; -static const char* LOAD_USER_ACCOUNT = "SELECT * FROM Accounts WHERE username = ?;"; -static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET blacklisted = ?2, whitelisted = ?3, mod = ?4, admin = ?5 WHERE uid = ?1;"; + +static const char* LOAD_USER_ACCOUNT = "SELECT " + "uid, " + "blacklisted, " + "whitelisted, " + "mod, " + "admin " + " FROM Accounts WHERE username = ?;"; + +static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET " + "blacklisted = ?2, " + "whitelisted = ?3, " + "mod = ?4, " + "admin = ?5 " + "WHERE uid = ?1;"; + static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;"; + static const char* COUNT_USER_ACCOUNT_RECORDS = "SELECT COUNT(*) FROM Accounts;"; //------------------------- //Define the public methods //------------------------- +//TODO: (1) block blacklisted accounts int AccountManager::Create(std::string username, int clientIndex) { //create this user account, failing if it exists, leave this account in memory sqlite3_stmt* statement = nullptr; @@ -95,11 +110,11 @@ int AccountManager::Load(std::string username, int clientIndex) { //extract the data into memory AccountData& newAccount = elementMap[uid]; - newAccount.username = reinterpret_cast(sqlite3_column_text(statement, 1)); - newAccount.blackListed = sqlite3_column_int(statement, 2); - newAccount.whiteListed = sqlite3_column_int(statement, 3); - newAccount.mod = sqlite3_column_int(statement, 4); - newAccount.admin = sqlite3_column_int(statement, 5); + newAccount.username = username; + newAccount.blackListed = sqlite3_column_int(statement, 1); + newAccount.whiteListed = sqlite3_column_int(statement, 2); + newAccount.mod = sqlite3_column_int(statement, 3); + newAccount.admin = sqlite3_column_int(statement, 4); newAccount.clientIndex = clientIndex; //finish the routine From c3c6d42a805edb649d84ae59bc164f966eb4cb82 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 9 Mar 2015 10:27:50 +1100 Subject: [PATCH 19/28] Characters moving via scripts are pushed & popped from the rooms The delete & create messages still need to be implemented. --- rsc/scripts/setup_server.lua | 14 ++++++++++---- server/characters/character_api.cpp | 25 +++++++++++++++++++------ server/characters/makefile | 2 +- server/entities/entity_api.cpp | 1 - server/monsters/monster_api.cpp | 12 ++++++------ 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index b711273..ebceded 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -7,9 +7,19 @@ roomAPI = require("room") mapMaker = require("map_maker") mapSaver = require("map_saver") +characterAPI = require("character") +entityAPI = require("entity") +networkAPI = require("network") + --test the room hooks roomManagerAPI.SetOnCreate(function(room, index) print("", "Creating room: ", roomAPI.GetName(room), index) + + roomAPI.SetOnTick(room, function(room) + roomAPI.ForEachCharacter(room, function(character) + characterAPI.SetRoomIndex(character, 0) + end) + end) end) roomManagerAPI.SetOnUnload(function(room, index) @@ -40,10 +50,6 @@ function createTrigger(handle, room, x, y, script) ) end -characterAPI = require("character") -entityAPI = require("entity") -networkAPI = require("network") - --simple teleporter createTrigger("trigger 1", overworld, 0, 0, function(entity) if entityAPI.GetType(entity) ~= "character" then diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index c6a0414..bb35147 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -25,6 +25,18 @@ #include "entity_api.hpp" +#include "room_manager.hpp" + +static int setRoomIndex(lua_State* L) { + //NOTE: type-dependant calls to various API functions, see bug #43 + CharacterData* character = static_cast(lua_touserdata(L, 1)); + RoomManager::GetSingleton().PopCharacter(character); + character->SetRoomIndex(lua_tointeger(L, 2)); + RoomManager::GetSingleton().PushCharacter(character); + //TODO: (0) send character room change messages + return 0; +} + static int getOwner(lua_State* L) { CharacterData* character = static_cast(lua_touserdata(L, 1)); lua_pushinteger(L, character->GetOwner()); @@ -45,33 +57,34 @@ static int getAvatar(lua_State* L) { static const luaL_Reg characterLib[] = { // {"GetOwner", getOwner}, //unusable without account API + {"SetRoomIndex", setRoomIndex}, {"GetHandle", getHandle}, {"GetAvatar", getAvatar}, {nullptr, nullptr} }; LUAMOD_API int openCharacterAPI(lua_State* L) { - //the local table - luaL_newlib(L, characterLib); - //get the parent table luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); - //clone the parent table into the local table + //the local table + luaL_newlib(L, characterLib); + + //merge the local table into the parent table lua_pushnil(L); //first key while(lua_next(L, -2)) { //copy the key-value pair lua_pushvalue(L, -2); lua_pushvalue(L, -2); - //push the copy to the local table + //push the copy to the parent table lua_settable(L, -6); //pop the original value before continuing lua_pop(L, 1); } - //remove the parent table, leaving the expanded child table + //remove the local table, leaving the expanded parent table lua_pop(L, 1); return 1; diff --git a/server/characters/makefile b/server/characters/makefile index 7e666d5..998ca93 100644 --- a/server/characters/makefile +++ b/server/characters/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../entities ../server_utilities ../../common/gameplay ../../common/utilities +INCLUDES+=. ../entities ../monsters ../rooms ../server_utilities ../triggers ../../common/gameplay ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index 30daaea..13bda83 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -24,7 +24,6 @@ #include "entity.hpp" static int setRoomIndex(lua_State* L) { - //TODO: (1) if this is a character, push/pop from the room system Entity* entity = static_cast(lua_touserdata(L, 1)); entity->SetRoomIndex(lua_tointeger(L, 2)); return 0; diff --git a/server/monsters/monster_api.cpp b/server/monsters/monster_api.cpp index 06d6551..915b0c3 100644 --- a/server/monsters/monster_api.cpp +++ b/server/monsters/monster_api.cpp @@ -61,27 +61,27 @@ static const luaL_Reg monsterLib[] = { }; LUAMOD_API int openMonsterAPI(lua_State* L) { - //the local table - luaL_newlib(L, monsterLib); - //get the parent table luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false); - //clone the parent table into the local table + //the local table + luaL_newlib(L, monsterLib); + + //merge the local table into the parent table lua_pushnil(L); //first key while(lua_next(L, -2)) { //copy the key-value pair lua_pushvalue(L, -2); lua_pushvalue(L, -2); - //push the copy to the local table + //push the copy to the parent table lua_settable(L, -6); //pop the original value before continuing lua_pop(L, 1); } - //remove the parent table, leaving the expanded child table + //remove the local table, leaving the expanded parent table lua_pop(L, 1); return 1; From 81b376918867e3f4f0c03573604afd15588d2f56 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 9 Mar 2015 21:28:28 +1100 Subject: [PATCH 20/28] Implemented networked room chainging function --- server/characters/character_api.cpp | 8 ++--- server/server_character_methods.cpp | 16 +--------- server/server_utilities/server_utilities.cpp | 32 ++++++++++++++++++++ server/server_utilities/server_utilities.hpp | 6 ++++ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index bb35147..229d297 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -22,18 +22,14 @@ #include "character_api.hpp" #include "character_data.hpp" - #include "entity_api.hpp" - #include "room_manager.hpp" +#include "server_utilities.hpp" static int setRoomIndex(lua_State* L) { //NOTE: type-dependant calls to various API functions, see bug #43 CharacterData* character = static_cast(lua_touserdata(L, 1)); - RoomManager::GetSingleton().PopCharacter(character); - character->SetRoomIndex(lua_tointeger(L, 2)); - RoomManager::GetSingleton().PushCharacter(character); - //TODO: (0) send character room change messages + pumpAndChangeRooms(character, lua_tointeger(L, 2), -1); //TODO: (0) undefined behavior without character index return 0; } diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 57b36e7..5e370d7 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -212,21 +212,7 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) { //check if moving rooms if (characterData->GetRoomIndex() != argPacket->roomIndex) { - //delete from the old room - CharacterPacket newPacket; - copyCharacterToPacket(&newPacket, argPacket->characterIndex); - newPacket.type = SerialPacketType::CHARACTER_DELETE; - pumpPacketProximity(&newPacket, characterData->GetRoomIndex()); - - //move the character between rooms - roomMgr.PopCharacter(characterData); - characterData->SetRoomIndex(argPacket->roomIndex); - roomMgr.PushCharacter(characterData); - - //create in the new room - copyCharacterToPacket(&newPacket, argPacket->characterIndex); - newPacket.type = SerialPacketType::CHARACTER_CREATE; - pumpPacketProximity(&newPacket, characterData->GetRoomIndex()); + pumpAndChangeRooms(characterData, argPacket->roomIndex, argPacket->characterIndex); } //if not moving between rooms else { diff --git a/server/server_utilities/server_utilities.cpp b/server/server_utilities/server_utilities.cpp index edc664f..0583ee5 100644 --- a/server/server_utilities/server_utilities.cpp +++ b/server/server_utilities/server_utilities.cpp @@ -140,6 +140,10 @@ void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex) { throw(std::runtime_error("Failed to copy a character to a packet")); } + copyCharacterToPacket(packet, characterData, characterIndex); +} + +void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const characterData, int characterIndex) { //NOTE: keep this up to date when the character changes packet->characterIndex = characterIndex; strncpy(packet->handle, characterData->GetHandle().c_str(), PACKET_STRING_SIZE); @@ -150,3 +154,31 @@ void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex) { packet->motion = characterData->GetMotion(); packet->bounds = characterData->GetBounds(); } + +void pumpAndChangeRooms(int characterIndex, int newRoomIndex) { + //BUG: three redundant lookups + + //get the character object + CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex); + + //pass ownwards + pumpAndChangeRooms(character, newRoomIndex, characterIndex); +} + +void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex) { + //delete from the old room + CharacterPacket newPacket; + copyCharacterToPacket(&newPacket, characterData, characterIndex); + newPacket.type = SerialPacketType::CHARACTER_DELETE; + pumpPacketProximity(&newPacket, characterData->GetRoomIndex()); + + //move the character between rooms + RoomManager::GetSingleton().PopCharacter(characterData); + characterData->SetRoomIndex(newRoomIndex); + RoomManager::GetSingleton().PushCharacter(characterData); + + //create in the new room + copyCharacterToPacket(&newPacket, characterData, characterIndex); + newPacket.type = SerialPacketType::CHARACTER_CREATE; + pumpPacketProximity(&newPacket, characterData->GetRoomIndex()); +} \ No newline at end of file diff --git a/server/server_utilities/server_utilities.hpp b/server/server_utilities/server_utilities.hpp index 7da41c6..de1f123 100644 --- a/server/server_utilities/server_utilities.hpp +++ b/server/server_utilities/server_utilities.hpp @@ -22,14 +22,20 @@ #ifndef SERVERUTILITIES_HPP_ #define SERVERUTILITIES_HPP_ +#include "character_data.hpp" #include "serial_packet.hpp" #include "vector2.hpp" void fullClientUnload(int index); void fullAccountUnload(int index); void fullCharacterUnload(int index); + void pumpPacket(SerialPacket* const argPacket); void pumpPacketProximity(SerialPacket* const argPacket, int roomIndex, Vector2 position = {0, 0}, int distance = -1); + void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex); +void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const characterData, int characterIndex); +void pumpAndChangeRooms(int characterIndex, int newRoomIndex); +void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex); #endif \ No newline at end of file From 4d71d4cc4080fafa00d8a64adea8434b5b5d80bd Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 9 Mar 2015 23:26:37 +1100 Subject: [PATCH 21/28] Room transitions are working smoothly, read more Although the room transitions are working fairly well, it is still heavy handed, and a number of optimizations can be done. On the whole, this needs a review. --- client/gameplay_scenes/world_characters.cpp | 25 +++++++++--- client/gameplay_scenes/world_logic.cpp | 1 + rsc/scripts/map_maker.lua | 12 ++++++ rsc/scripts/setup_server.lua | 45 +++++++++++++++++---- server/characters/character_api.cpp | 27 ++++++++++++- server/characters/makefile | 2 +- server/rooms/room_data.cpp | 16 +++++++- 7 files changed, 109 insertions(+), 19 deletions(-) diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index b0ada57..8a7628f 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -87,6 +87,15 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) { //focus on this character's info characterIndex = argPacket->characterIndex; roomIndex = argPacket->roomIndex; + + //query the world state + CharacterPacket newPacket; + memset(&newPacket, 0, MAX_PACKET_SIZE); + newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; //TODO: (2) the EXISTS and LOCATION queries are backwards? + newPacket.roomIndex = roomIndex; + network.SendTo(Channels::SERVER, &newPacket); + newPacket.type = SerialPacketType::QUERY_MONSTER_EXISTS; + network.SendTo(Channels::SERVER, &newPacket); } //debug @@ -110,10 +119,14 @@ void World::hCharacterDelete(CharacterPacket* const argPacket) { //clear the room roomIndex = -1; + regionPager.UnloadAll(); + characterMap.clear(); + monsterMap.clear(); + } + else { + //remove this character + characterMap.erase(characterIt); } - - //remove this character - characterMap.erase(characterIt); //debug std::cout << "Character Delete, total: " << characterMap.size() << std::endl; @@ -121,9 +134,9 @@ void World::hCharacterDelete(CharacterPacket* const argPacket) { void World::hQueryCharacterExists(CharacterPacket* const argPacket) { //prevent a double message about this player's character - if (argPacket->accountIndex == accountIndex) { - return; - } +// if (argPacket->accountIndex == accountIndex) { +// return; +// } //ignore characters in a different room (sub-optimal) if (argPacket->roomIndex != roomIndex) { diff --git a/client/gameplay_scenes/world_logic.cpp b/client/gameplay_scenes/world_logic.cpp index 1085e84..5d7df57 100644 --- a/client/gameplay_scenes/world_logic.cpp +++ b/client/gameplay_scenes/world_logic.cpp @@ -71,6 +71,7 @@ World::World(int* const argClientIndex, int* const argAccountIndex): newPacket.accountIndex = accountIndex; network.SendTo(Channels::SERVER, &newPacket); + //TODO: (2) replace this duplication with a request for just this player's character //query the world state memset(&newPacket, 0, MAX_PACKET_SIZE); newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index d24f056..7b1cbfd 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -93,4 +93,16 @@ function mapMaker.DebugIsland(r) mapMaker.SmoothEdgesSimple(r) end +function mapMaker.DebugGrassland(r) + --all dirt + for i = 1, regionAPI.GetWidth(r) do + for j = 1, regionAPI.GetHeight(r) do + regionAPI.SetTile(r, i, j, 1, mapMaker.grass) + end + end + + --A generic edge system +-- mapMaker.SmoothEdgesSimple(r) +end + return mapMaker \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index ebceded..c251851 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -17,7 +17,7 @@ roomManagerAPI.SetOnCreate(function(room, index) roomAPI.SetOnTick(room, function(room) roomAPI.ForEachCharacter(room, function(character) - characterAPI.SetRoomIndex(character, 0) + -- end) end) end) @@ -27,8 +27,10 @@ roomManagerAPI.SetOnUnload(function(room, index) end) --NOTE: room 0 is the first that the client asks for, therefore it must exist -local overworld, uid = roomManagerAPI.CreateRoom("overworld", "overworld.bmp") +local overworld, uidOne = roomManagerAPI.CreateRoom("overworld", "overworld.bmp") roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save) +local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") +roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) --debug: test the trigger system regionPagerAPI = require("region_pager") @@ -38,9 +40,9 @@ function createTrigger(handle, room, x, y, script) local pager = roomAPI.GetPager(room) --place the indicator tile - regionPagerAPI.SetTile(pager, x, y, 0, mapMaker.dirt) - regionPagerAPI.SetTile(pager, x, y, 1, mapMaker.blank) - regionPagerAPI.SetTile(pager, x, y, 2, mapMaker.blank) + regionPagerAPI.SetTile(pager, x / 32, y / 32, 0, mapMaker.dirt) + regionPagerAPI.SetTile(pager, x / 32, y / 32, 1, mapMaker.blank) + regionPagerAPI.SetTile(pager, x / 32, y / 32, 2, mapMaker.blank) --create the trigger object triggerManagerAPI.Create( @@ -50,14 +52,41 @@ function createTrigger(handle, room, x, y, script) ) end ---simple teleporter -createTrigger("trigger 1", overworld, 0, 0, function(entity) +--simple door pair +createTrigger("door 1", overworld, 128, -128, function(entity) if entityAPI.GetType(entity) ~= "character" then return end + + print("mark 1") local x, y = characterAPI.GetOrigin(entity) - characterAPI.SetOrigin(entity, x, y + 128) + print("mark 2") + characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter + print("mark 3") + characterAPI.SetOrigin(entity, 0, 0) + print("mark 4") networkAPI.PumpCharacterUpdate(entity) + print("mark 5") + + return false +end) + +createTrigger("door 1", underworld, 128, -128, function(entity) + if entityAPI.GetType(entity) ~= "character" then + return + end + + print("mark 6") + local x, y = characterAPI.GetOrigin(entity) + print("mark 7") + characterAPI.SetRoomIndex(entity, uidOne) + print("mark 8") + characterAPI.SetOrigin(entity, 0, 0) + print("mark 9") + networkAPI.PumpCharacterUpdate(entity) + print("mark 10") + + return false end) print("Finished the lua script") diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index 229d297..985366b 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -22,14 +22,37 @@ #include "character_api.hpp" #include "character_data.hpp" +#include "character_manager.hpp" #include "entity_api.hpp" #include "room_manager.hpp" #include "server_utilities.hpp" -static int setRoomIndex(lua_State* L) { +#include + +static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a parameter //NOTE: type-dependant calls to various API functions, see bug #43 + + //reverse engineer the character index + int characterIndex = -1; CharacterData* character = static_cast(lua_touserdata(L, 1)); - pumpAndChangeRooms(character, lua_tointeger(L, 2), -1); //TODO: (0) undefined behavior without character index + int roomIndex = lua_tointeger(L, 2); + RoomData* roomData = RoomManager::GetSingleton().Get(roomIndex); + + CharacterManager& characterMgr = CharacterManager::GetSingleton(); + + for (auto& it : *characterMgr.GetContainer()) { + if (character == &it.second) { + characterIndex = it.first; + break; + } + } + + //error checking + if (characterIndex == -1) { + throw(std::runtime_error("Failed to find character index by reference")); + } + + pumpAndChangeRooms(character, lua_tointeger(L, 2), characterIndex); return 0; } diff --git a/server/characters/makefile b/server/characters/makefile index 998ca93..f686f51 100644 --- a/server/characters/makefile +++ b/server/characters/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../entities ../monsters ../rooms ../server_utilities ../triggers ../../common/gameplay ../../common/map ../../common/utilities +INCLUDES+=. ../entities ../monsters ../rooms ../server_utilities ../triggers ../../common/gameplay ../../common/map ../../common/network ../../common/network/packet_types ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 1148a53..911e9e7 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -54,13 +54,25 @@ void RoomData::RunFrame() { if ( itBox.CheckOverlap(hitBox) ) { //trigger script lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); - lua_pushlightuserdata(lua, character); //TODO: (1) entity type + lua_pushlightuserdata(lua, character); + + //BUG: (0) + + std::cout << "running scripts" << std::endl; //run the script - if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + //BUGFIX: changing the character's room via lua invalidates the list, therefore, the script much signal for an early exit + //TODO: (2) fix this somehow (operate on a stack?) + if (lua_pcall(lua, 1, 1, 0) != LUA_OK) { //error throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } + + //true = safe to continue, false = exit early + if (!lua_toboolean(lua, -1)) { + std::cout << "Warning!: Early abort" << std::endl; + return; + } } } // for (auto& monster : *monsterMgr.GetContainer()) { From 670ab22e96714725e7f96b7f7f84e5747927c38c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 11 Mar 2015 18:18:32 +1100 Subject: [PATCH 22/28] Using an entity stack for trigger comparisons --- rsc/scripts/setup_server.lua | 16 +----- server/rooms/room_data.cpp | 51 ++++++++++---------- server/server_utilities/server_utilities.cpp | 2 - 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index c251851..7d19380 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -32,7 +32,7 @@ roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) ---debug: test the trigger system +--NOTE: test the trigger system regionPagerAPI = require("region_pager") triggerManagerAPI = require("trigger_manager") @@ -58,17 +58,10 @@ createTrigger("door 1", overworld, 128, -128, function(entity) return end - print("mark 1") local x, y = characterAPI.GetOrigin(entity) - print("mark 2") characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter - print("mark 3") characterAPI.SetOrigin(entity, 0, 0) - print("mark 4") networkAPI.PumpCharacterUpdate(entity) - print("mark 5") - - return false end) createTrigger("door 1", underworld, 128, -128, function(entity) @@ -76,17 +69,10 @@ createTrigger("door 1", underworld, 128, -128, function(entity) return end - print("mark 6") local x, y = characterAPI.GetOrigin(entity) - print("mark 7") characterAPI.SetRoomIndex(entity, uidOne) - print("mark 8") characterAPI.SetOrigin(entity, 0, 0) - print("mark 9") networkAPI.PumpCharacterUpdate(entity) - print("mark 10") - - return false end) print("Finished the lua script") diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 911e9e7..8b57466 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -22,6 +22,7 @@ #include "room_data.hpp" #include +#include #include void RoomData::RunFrame() { @@ -44,42 +45,40 @@ void RoomData::RunFrame() { it->Update(); } //TODO: (3) iterate through the monster map + //TODO: (3) trigger script for monsters + + //build a list of game entities + std::stack entityStack; + for (auto& it : characterList) { + entityStack.push(it); + } + //TODO: (3) push the monster entities //compare the triggers to the entities, using their real hitboxes - for (auto& it : *triggerMgr.GetContainer()) { - BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); - for (auto& character : characterList) { - BoundingBox hitBox = character->GetBounds() + character->GetOrigin(); + //NOTE: this stack solution should prevent problems when modifying the various lists + while(entityStack.size()) { + //get the entity & hitbox + Entity* entity = entityStack.top(); + BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin(); - if ( itBox.CheckOverlap(hitBox) ) { - //trigger script + //get the trigger & hitbox + for (auto& it : *triggerMgr.GetContainer()) { + BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin(); + + if (entityBox.CheckOverlap(triggerBox)) { + //run the trigger script lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); - lua_pushlightuserdata(lua, character); + lua_pushlightuserdata(lua, entity); - //BUG: (0) - - std::cout << "running scripts" << std::endl; - - //run the script - //BUGFIX: changing the character's room via lua invalidates the list, therefore, the script much signal for an early exit - //TODO: (2) fix this somehow (operate on a stack?) - if (lua_pcall(lua, 1, 1, 0) != LUA_OK) { + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { //error throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } - - //true = safe to continue, false = exit early - if (!lua_toboolean(lua, -1)) { - std::cout << "Warning!: Early abort" << std::endl; - return; - } } } -// for (auto& monster : *monsterMgr.GetContainer()) { -// if (it.second.Compare(static_cast(&monster.second))) { -// //TODO: (1) trigger script -// } -// } + + //next + entityStack.pop(); } } diff --git a/server/server_utilities/server_utilities.cpp b/server/server_utilities/server_utilities.cpp index 0583ee5..5207d4f 100644 --- a/server/server_utilities/server_utilities.cpp +++ b/server/server_utilities/server_utilities.cpp @@ -156,8 +156,6 @@ void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const c } void pumpAndChangeRooms(int characterIndex, int newRoomIndex) { - //BUG: three redundant lookups - //get the character object CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex); From ca6afb72add840f613984e2d41f6498f4696a33d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 11 Mar 2015 20:23:52 +1100 Subject: [PATCH 23/28] Minor comment tweaks --- client/gameplay_scenes/world_characters.cpp | 2 +- client/gameplay_scenes/world_logic.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index 8a7628f..945d834 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -91,7 +91,7 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) { //query the world state CharacterPacket newPacket; memset(&newPacket, 0, MAX_PACKET_SIZE); - newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; //TODO: (2) the EXISTS and LOCATION queries are backwards? + newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; //TODO: (1) the EXISTS and LOCATION queries are backwards? newPacket.roomIndex = roomIndex; network.SendTo(Channels::SERVER, &newPacket); newPacket.type = SerialPacketType::QUERY_MONSTER_EXISTS; diff --git a/client/gameplay_scenes/world_logic.cpp b/client/gameplay_scenes/world_logic.cpp index 5d7df57..c3f342a 100644 --- a/client/gameplay_scenes/world_logic.cpp +++ b/client/gameplay_scenes/world_logic.cpp @@ -71,7 +71,7 @@ World::World(int* const argClientIndex, int* const argAccountIndex): newPacket.accountIndex = accountIndex; network.SendTo(Channels::SERVER, &newPacket); - //TODO: (2) replace this duplication with a request for just this player's character + //TODO: (1) replace this duplication with a request for just this player's character //query the world state memset(&newPacket, 0, MAX_PACKET_SIZE); newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; From d2bb3575fccaca8796ac6edd184aff8dee335644 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 13 Mar 2015 19:12:34 +1100 Subject: [PATCH 24/28] Removed initial world query The world is queried once the client has the player's character. --- client/gameplay_scenes/world_characters.cpp | 4 ++-- client/gameplay_scenes/world_logic.cpp | 8 -------- rsc/scripts/map_maker.lua | 1 + rsc/scripts/setup_server.lua | 2 +- server/characters/character_api.cpp | 2 +- server/triggers/trigger_data.hpp | 1 + 6 files changed, 6 insertions(+), 12 deletions(-) diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index 945d834..fe6459e 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -88,10 +88,10 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) { characterIndex = argPacket->characterIndex; roomIndex = argPacket->roomIndex; - //query the world state + //query the world state (room) CharacterPacket newPacket; memset(&newPacket, 0, MAX_PACKET_SIZE); - newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; //TODO: (1) the EXISTS and LOCATION queries are backwards? + newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; newPacket.roomIndex = roomIndex; network.SendTo(Channels::SERVER, &newPacket); newPacket.type = SerialPacketType::QUERY_MONSTER_EXISTS; diff --git a/client/gameplay_scenes/world_logic.cpp b/client/gameplay_scenes/world_logic.cpp index c3f342a..4c724fb 100644 --- a/client/gameplay_scenes/world_logic.cpp +++ b/client/gameplay_scenes/world_logic.cpp @@ -71,14 +71,6 @@ World::World(int* const argClientIndex, int* const argAccountIndex): newPacket.accountIndex = accountIndex; network.SendTo(Channels::SERVER, &newPacket); - //TODO: (1) replace this duplication with a request for just this player's character - //query the world state - memset(&newPacket, 0, MAX_PACKET_SIZE); - newPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS; - network.SendTo(Channels::SERVER, &newPacket); - newPacket.type = SerialPacketType::QUERY_MONSTER_EXISTS; - network.SendTo(Channels::SERVER, &newPacket); - //set the camera's values camera.width = GetScreen()->w; camera.height = GetScreen()->h; diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua index 7b1cbfd..dbcc2ec 100644 --- a/rsc/scripts/map_maker.lua +++ b/rsc/scripts/map_maker.lua @@ -21,6 +21,7 @@ mapMaker.edges.south = 16 mapMaker.edges.east = 1 mapMaker.edges.west = -1 +--TODO: (1) path system --use these macros (mapped to "overworld.bmp" for now) to smooth the region's edges function mapMaker.SmoothEdgesSimple(r) --make and pad an array to use diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 7d19380..2d86a22 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -59,7 +59,7 @@ createTrigger("door 1", overworld, 128, -128, function(entity) end local x, y = characterAPI.GetOrigin(entity) - characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter + characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (0) take exit coordinates as a parameter characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) end) diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index 985366b..0e858d1 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -29,7 +29,7 @@ #include -static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a parameter +static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a parameter? //NOTE: type-dependant calls to various API functions, see bug #43 //reverse engineer the character index diff --git a/server/triggers/trigger_data.hpp b/server/triggers/trigger_data.hpp index cb6409c..d77433e 100644 --- a/server/triggers/trigger_data.hpp +++ b/server/triggers/trigger_data.hpp @@ -29,6 +29,7 @@ #include +//TODO: (0) state-system for preventing double triggering class TriggerData { public: TriggerData() = default; From 954213f1ff9ed7a38abcd6f21649ab01d0c2f30a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 13 Mar 2015 19:36:03 +1100 Subject: [PATCH 25/28] Minor tweak to hCharacterMovement() --- rsc/scripts/setup_server.lua | 2 +- server/characters/character_api.cpp | 5 +++-- server/server_character_methods.cpp | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 2d86a22..2c4f31c 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -59,7 +59,7 @@ createTrigger("door 1", overworld, 128, -128, function(entity) end local x, y = characterAPI.GetOrigin(entity) - characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (0) take exit coordinates as a parameter + characterAPI.SetRoomIndex(entity, uidTwo) characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) end) diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index 0e858d1..632285a 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -49,9 +49,10 @@ static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a //error checking if (characterIndex == -1) { - throw(std::runtime_error("Failed to find character index by reference")); + throw(std::runtime_error("Lua Error: Failed to find character index by reference")); } + //send the delete & create messages pumpAndChangeRooms(character, lua_tointeger(L, 2), characterIndex); return 0; } @@ -75,8 +76,8 @@ static int getAvatar(lua_State* L) { } static const luaL_Reg characterLib[] = { -// {"GetOwner", getOwner}, //unusable without account API {"SetRoomIndex", setRoomIndex}, +// {"GetOwner", getOwner}, //unusable without account API {"GetHandle", getHandle}, {"GetAvatar", getAvatar}, {nullptr, nullptr} diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 5e370d7..36839ac 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -212,6 +212,11 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) { //check if moving rooms if (characterData->GetRoomIndex() != argPacket->roomIndex) { + //set the character's origin and motion + characterData->SetOrigin(argPacket->origin); + characterData->SetMotion(argPacket->motion); + + //send the delete & create messages pumpAndChangeRooms(characterData, argPacket->roomIndex, argPacket->characterIndex); } //if not moving between rooms From d82e3a8b7913828f25e9b034ac1982df855d0959 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 13 Mar 2015 20:43:20 +1100 Subject: [PATCH 26/28] Triggers now support exclusion lists --- rsc/scripts/setup_server.lua | 50 ++++++++++++++++++++++++++--- server/characters/character_api.cpp | 2 +- server/rooms/room_data.cpp | 27 +++++++++++++--- server/triggers/trigger_api.cpp | 18 +++++++++++ server/triggers/trigger_data.cpp | 4 +++ server/triggers/trigger_data.hpp | 6 +++- 6 files changed, 97 insertions(+), 10 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 2c4f31c..e0086ad 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -11,6 +11,10 @@ characterAPI = require("character") entityAPI = require("entity") networkAPI = require("network") +regionPagerAPI = require("region_pager") +triggerAPI = require("trigger") +triggerManagerAPI = require("trigger_manager") + --test the room hooks roomManagerAPI.SetOnCreate(function(room, index) print("", "Creating room: ", roomAPI.GetName(room), index) @@ -33,8 +37,6 @@ local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bm roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) --NOTE: test the trigger system -regionPagerAPI = require("region_pager") -triggerManagerAPI = require("trigger_manager") function createTrigger(handle, room, x, y, script) local pager = roomAPI.GetPager(room) @@ -52,13 +54,52 @@ function createTrigger(handle, room, x, y, script) ) end +function createDoorPair(handle, roomOne, roomOneUID, Xone, Yone, roomTwo, roomTwoUID, Xtwo, Ytwo) + --create the scripts + local function scriptOne(entity) + if entityAPI.GetType(entity) ~= "character" then return end + + --move the character + characterAPI.SetRoomIndex(entity, roomTwoUID) + characterAPI.SetOrigin(entity, Xtwo, Ytwo-16) + networkAPI.PumpCharacterUpdate(entity) + + --disable the other trigger + local triggerTwo = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomTwo), handle) + triggerAPI.PushExclusionEntity(triggerTwo, entity) + end + + local function scriptTwo(entity) + if entityAPI.GetType(entity) ~= "character" then return end + + --move the character + characterAPI.SetRoomIndex(entity, roomOneUID) + characterAPI.SetOrigin(entity, Xone, Yone-16) --NOTE: the 16 pixel margin for presentation + networkAPI.PumpCharacterUpdate(entity) + + --disable the other trigger + local triggerOne = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomOne), handle) + triggerAPI.PushExclusionEntity(triggerOne, entity) + end + + --create the triggers proper + createTrigger(handle, roomOne, Xone, Yone, scriptOne) + createTrigger(handle, roomTwo, Xtwo, Ytwo, scriptTwo) +end + +--call the monstrosity +createDoorPair("pair 1", overworld, uidOne, 0, -64, underworld, uidTwo, 0, 0) +createDoorPair("pair 2", overworld, uidOne, 64, -64, underworld, uidTwo, 64, 0) +createDoorPair("pair 3", overworld, uidOne, 128, -64, underworld, uidTwo, 128, 0) + +--[[ --simple door pair createTrigger("door 1", overworld, 128, -128, function(entity) if entityAPI.GetType(entity) ~= "character" then return end - local x, y = characterAPI.GetOrigin(entity) + --move the character characterAPI.SetRoomIndex(entity, uidTwo) characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) @@ -69,10 +110,11 @@ createTrigger("door 1", underworld, 128, -128, function(entity) return end - local x, y = characterAPI.GetOrigin(entity) + --move the character characterAPI.SetRoomIndex(entity, uidOne) characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) end) +--]] print("Finished the lua script") diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index 632285a..38091c2 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -29,7 +29,7 @@ #include -static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a parameter? +static int setRoomIndex(lua_State* L) { //TODO: (0) take the room userdata as a parameter //NOTE: type-dependant calls to various API functions, see bug #43 //reverse engineer the character index diff --git a/server/rooms/room_data.cpp b/server/rooms/room_data.cpp index 8b57466..fd0c7e7 100644 --- a/server/rooms/room_data.cpp +++ b/server/rooms/room_data.cpp @@ -21,6 +21,7 @@ */ #include "room_data.hpp" +#include #include #include #include @@ -61,19 +62,37 @@ void RoomData::RunFrame() { Entity* entity = entityStack.top(); BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin(); - //get the trigger & hitbox - for (auto& it : *triggerMgr.GetContainer()) { - BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin(); + //get the trigger pair & hitbox + for (auto& triggerPair : *triggerMgr.GetContainer()) { + BoundingBox triggerBox = triggerPair.second.GetBoundingBox() + triggerPair.second.GetOrigin(); + //find all collisions if (entityBox.CheckOverlap(triggerBox)) { + //skip members of the exclusion list + if (std::any_of(triggerPair.second.GetExclusionList()->begin(), triggerPair.second.GetExclusionList()->end(), [entity](Entity* ptr) -> bool { + return entity == ptr; + })) { + continue; + } + //run the trigger script - lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); + lua_rawgeti(lua, LUA_REGISTRYINDEX, triggerPair.second.GetScriptReference()); lua_pushlightuserdata(lua, entity); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { //error throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } + + //push to the exclusion list + triggerPair.second.GetExclusionList()->push_back(entity); + } + else { + //remove members of the exclusion list + //NOTE: characters in different rooms won't be removed, but that shouldn't be a problem + triggerPair.second.GetExclusionList()->remove_if([entity](Entity* ptr) -> bool { + return entity == ptr; + }); } } diff --git a/server/triggers/trigger_api.cpp b/server/triggers/trigger_api.cpp index 7ea007c..8f9089a 100644 --- a/server/triggers/trigger_api.cpp +++ b/server/triggers/trigger_api.cpp @@ -86,6 +86,21 @@ static int getReference(lua_State* L) { return 1; } +static int pushExclusionEntity(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + trigger->GetExclusionList()->push_back(static_cast(lua_touserdata(L, 2))); + return 0; +} + +static int removeExclusionEntity(lua_State* L) { + TriggerData* trigger = static_cast(lua_touserdata(L, 1)); + Entity* entity = static_cast(lua_touserdata(L, 2)); + trigger->GetExclusionList()->remove_if([entity](Entity* ptr){ + return entity == ptr; + }); + return 0; +} + static const luaL_Reg triggerLib[] = { {"SetHandle", setHandle}, {"GetHandle", getHandle}, @@ -99,6 +114,9 @@ static const luaL_Reg triggerLib[] = { {"SetScript",setReference}, {"GetScript",getReference}, + {"PushExclusionEntity", pushExclusionEntity}, + {"RemoveExclusionEntity", removeExclusionEntity}, + {nullptr, nullptr} }; diff --git a/server/triggers/trigger_data.cpp b/server/triggers/trigger_data.cpp index f4edc80..32ce359 100644 --- a/server/triggers/trigger_data.cpp +++ b/server/triggers/trigger_data.cpp @@ -51,4 +51,8 @@ int TriggerData::SetScriptReference(int i) { int TriggerData::GetScriptReference() { return scriptRef; +} + +std::list* TriggerData::GetExclusionList() { + return &exclusionList; } \ No newline at end of file diff --git a/server/triggers/trigger_data.hpp b/server/triggers/trigger_data.hpp index d77433e..6064285 100644 --- a/server/triggers/trigger_data.hpp +++ b/server/triggers/trigger_data.hpp @@ -23,13 +23,14 @@ #define TRIGGERDATA_HPP_ #include "bounding_box.hpp" +#include "entity.hpp" #include "vector2.hpp" #include "lua.hpp" +#include #include -//TODO: (0) state-system for preventing double triggering class TriggerData { public: TriggerData() = default; @@ -47,11 +48,14 @@ public: int SetScriptReference(int i); int GetScriptReference(); + std::list* GetExclusionList(); + private: std::string handle; Vector2 origin; BoundingBox bounds; int scriptRef = LUA_NOREF; + std::list exclusionList; }; #endif \ No newline at end of file From 4ae58550b582f791d52849c7665c21d4617cfa63 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 13 Mar 2015 21:05:24 +1100 Subject: [PATCH 27/28] Changed SetRoomIndex() to SetRoom() --- rsc/scripts/setup_server.lua | 16 +++++++------- server/characters/character_api.cpp | 34 ++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index e0086ad..183c86f 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -54,13 +54,13 @@ function createTrigger(handle, room, x, y, script) ) end -function createDoorPair(handle, roomOne, roomOneUID, Xone, Yone, roomTwo, roomTwoUID, Xtwo, Ytwo) +function createDoorPair(handle, roomOne, Xone, Yone, roomTwo, Xtwo, Ytwo) --create the scripts local function scriptOne(entity) if entityAPI.GetType(entity) ~= "character" then return end --move the character - characterAPI.SetRoomIndex(entity, roomTwoUID) + characterAPI.SetRoom(entity, roomTwo) characterAPI.SetOrigin(entity, Xtwo, Ytwo-16) networkAPI.PumpCharacterUpdate(entity) @@ -73,7 +73,7 @@ function createDoorPair(handle, roomOne, roomOneUID, Xone, Yone, roomTwo, roomTw if entityAPI.GetType(entity) ~= "character" then return end --move the character - characterAPI.SetRoomIndex(entity, roomOneUID) + characterAPI.SetRoom(entity, roomOne) characterAPI.SetOrigin(entity, Xone, Yone-16) --NOTE: the 16 pixel margin for presentation networkAPI.PumpCharacterUpdate(entity) @@ -88,9 +88,9 @@ function createDoorPair(handle, roomOne, roomOneUID, Xone, Yone, roomTwo, roomTw end --call the monstrosity -createDoorPair("pair 1", overworld, uidOne, 0, -64, underworld, uidTwo, 0, 0) -createDoorPair("pair 2", overworld, uidOne, 64, -64, underworld, uidTwo, 64, 0) -createDoorPair("pair 3", overworld, uidOne, 128, -64, underworld, uidTwo, 128, 0) +createDoorPair("pair 1", overworld, 0, -64, underworld, 0, 0) +createDoorPair("pair 2", overworld, 64, -64, underworld, 64, 0) +createDoorPair("pair 3", overworld, 128, -64, underworld, 128, 0) --[[ --simple door pair @@ -100,7 +100,7 @@ createTrigger("door 1", overworld, 128, -128, function(entity) end --move the character - characterAPI.SetRoomIndex(entity, uidTwo) + characterAPI.SetRoom(entity, uidTwo) characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) end) @@ -111,7 +111,7 @@ createTrigger("door 1", underworld, 128, -128, function(entity) end --move the character - characterAPI.SetRoomIndex(entity, uidOne) + characterAPI.SetRoom(entity, uidOne) characterAPI.SetOrigin(entity, 0, 0) networkAPI.PumpCharacterUpdate(entity) end) diff --git a/server/characters/character_api.cpp b/server/characters/character_api.cpp index 38091c2..99b2cfe 100644 --- a/server/characters/character_api.cpp +++ b/server/characters/character_api.cpp @@ -29,15 +29,10 @@ #include -static int setRoomIndex(lua_State* L) { //TODO: (0) take the room userdata as a parameter - //NOTE: type-dependant calls to various API functions, see bug #43 - +static int setRoom(lua_State* L) { //reverse engineer the character index int characterIndex = -1; CharacterData* character = static_cast(lua_touserdata(L, 1)); - int roomIndex = lua_tointeger(L, 2); - RoomData* roomData = RoomManager::GetSingleton().Get(roomIndex); - CharacterManager& characterMgr = CharacterManager::GetSingleton(); for (auto& it : *characterMgr.GetContainer()) { @@ -52,8 +47,31 @@ static int setRoomIndex(lua_State* L) { //TODO: (0) take the room userdata as a throw(std::runtime_error("Lua Error: Failed to find character index by reference")); } + //get the room index, depending on the parameter type + int roomIndex = -1; + RoomManager& roomMgr = RoomManager::GetSingleton(); + switch(lua_type(L, 2)) { + case LUA_TNUMBER: + roomIndex = lua_tointeger(L, 2); + break; + case LUA_TLIGHTUSERDATA: + //reverse engineer the room index + for (auto& it : *roomMgr.GetContainer()) { + if (lua_touserdata(L, 2) == &it.second) { + roomIndex = it.first; + break; + } + } + break; + } + + //error checking + if (roomIndex == -1) { + throw(std::runtime_error("Lua Error: Failed to find room index by reference")); + } + //send the delete & create messages - pumpAndChangeRooms(character, lua_tointeger(L, 2), characterIndex); + pumpAndChangeRooms(character, roomIndex, characterIndex); return 0; } @@ -76,7 +94,7 @@ static int getAvatar(lua_State* L) { } static const luaL_Reg characterLib[] = { - {"SetRoomIndex", setRoomIndex}, + {"SetRoom", setRoom}, // {"GetOwner", getOwner}, //unusable without account API {"GetHandle", getHandle}, {"GetAvatar", getAvatar}, From 0efb541074c0c4c46a85666b8c420a995891dfa3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 13 Mar 2015 21:23:04 +1100 Subject: [PATCH 28/28] Moved the door pair script to it's own file --- rsc/scripts/door_utility.lua | 63 ++++++++++++++++++++++++++ rsc/scripts/setup_server.lua | 88 +----------------------------------- 2 files changed, 65 insertions(+), 86 deletions(-) create mode 100644 rsc/scripts/door_utility.lua diff --git a/rsc/scripts/door_utility.lua b/rsc/scripts/door_utility.lua new file mode 100644 index 0000000..d1fc648 --- /dev/null +++ b/rsc/scripts/door_utility.lua @@ -0,0 +1,63 @@ +local doorUtility = {} + +roomAPI = require("room") +regionPagerAPI = require("region_pager") + +triggerAPI = require("trigger") +triggerManagerAPI = require("trigger_manager") + +entityAPI = require("entity") +characterAPI = require("character") + +networkAPI = require("network") + +function doorUtility.createTrigger(handle, room, x, y, script) + local pager = roomAPI.GetPager(room) + + --place the indicator tile + regionPagerAPI.SetTile(pager, x / 32, y / 32, 0, mapMaker.dirt) + regionPagerAPI.SetTile(pager, x / 32, y / 32, 1, mapMaker.blank) + regionPagerAPI.SetTile(pager, x / 32, y / 32, 2, mapMaker.blank) + + --create the trigger object + triggerManagerAPI.Create( + roomAPI.GetTriggerMgr(room), handle, x, y, + 0, 0, 32, 32, --size of the tiles + script + ) +end + +function doorUtility.createDoorPair(handle, roomOne, Xone, Yone, roomTwo, Xtwo, Ytwo) + --create the scripts + local function scriptOne(entity) + if entityAPI.GetType(entity) ~= "character" then return end + + --move the character + characterAPI.SetRoom(entity, roomTwo) + characterAPI.SetOrigin(entity, Xtwo, Ytwo-16) + networkAPI.PumpCharacterUpdate(entity) + + --disable the other trigger + local triggerTwo = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomTwo), handle) + triggerAPI.PushExclusionEntity(triggerTwo, entity) + end + + local function scriptTwo(entity) + if entityAPI.GetType(entity) ~= "character" then return end + + --move the character + characterAPI.SetRoom(entity, roomOne) + characterAPI.SetOrigin(entity, Xone, Yone-16) --NOTE: the 16 pixel margin for presentation + networkAPI.PumpCharacterUpdate(entity) + + --disable the other trigger + local triggerOne = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomOne), handle) + triggerAPI.PushExclusionEntity(triggerOne, entity) + end + + --create the triggers proper + doorUtility.createTrigger(handle, roomOne, Xone, Yone, scriptOne) + doorUtility.createTrigger(handle, roomTwo, Xtwo, Ytwo, scriptTwo) +end + +return doorUtility \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 183c86f..02f5059 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -7,13 +7,7 @@ roomAPI = require("room") mapMaker = require("map_maker") mapSaver = require("map_saver") -characterAPI = require("character") -entityAPI = require("entity") -networkAPI = require("network") - -regionPagerAPI = require("region_pager") -triggerAPI = require("trigger") -triggerManagerAPI = require("trigger_manager") +doorUtility = require("door_utility") --test the room hooks roomManagerAPI.SetOnCreate(function(room, index) @@ -36,85 +30,7 @@ roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) ---NOTE: test the trigger system - -function createTrigger(handle, room, x, y, script) - local pager = roomAPI.GetPager(room) - - --place the indicator tile - regionPagerAPI.SetTile(pager, x / 32, y / 32, 0, mapMaker.dirt) - regionPagerAPI.SetTile(pager, x / 32, y / 32, 1, mapMaker.blank) - regionPagerAPI.SetTile(pager, x / 32, y / 32, 2, mapMaker.blank) - - --create the trigger object - triggerManagerAPI.Create( - roomAPI.GetTriggerMgr(room), handle, x, y, - 0, 0, 32, 32, --size of the tiles - script - ) -end - -function createDoorPair(handle, roomOne, Xone, Yone, roomTwo, Xtwo, Ytwo) - --create the scripts - local function scriptOne(entity) - if entityAPI.GetType(entity) ~= "character" then return end - - --move the character - characterAPI.SetRoom(entity, roomTwo) - characterAPI.SetOrigin(entity, Xtwo, Ytwo-16) - networkAPI.PumpCharacterUpdate(entity) - - --disable the other trigger - local triggerTwo = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomTwo), handle) - triggerAPI.PushExclusionEntity(triggerTwo, entity) - end - - local function scriptTwo(entity) - if entityAPI.GetType(entity) ~= "character" then return end - - --move the character - characterAPI.SetRoom(entity, roomOne) - characterAPI.SetOrigin(entity, Xone, Yone-16) --NOTE: the 16 pixel margin for presentation - networkAPI.PumpCharacterUpdate(entity) - - --disable the other trigger - local triggerOne = triggerManagerAPI.GetTrigger(roomAPI.GetTriggerMgr(roomOne), handle) - triggerAPI.PushExclusionEntity(triggerOne, entity) - end - - --create the triggers proper - createTrigger(handle, roomOne, Xone, Yone, scriptOne) - createTrigger(handle, roomTwo, Xtwo, Ytwo, scriptTwo) -end - --call the monstrosity -createDoorPair("pair 1", overworld, 0, -64, underworld, 0, 0) -createDoorPair("pair 2", overworld, 64, -64, underworld, 64, 0) -createDoorPair("pair 3", overworld, 128, -64, underworld, 128, 0) - ---[[ ---simple door pair -createTrigger("door 1", overworld, 128, -128, function(entity) - if entityAPI.GetType(entity) ~= "character" then - return - end - - --move the character - characterAPI.SetRoom(entity, uidTwo) - characterAPI.SetOrigin(entity, 0, 0) - networkAPI.PumpCharacterUpdate(entity) -end) - -createTrigger("door 1", underworld, 128, -128, function(entity) - if entityAPI.GetType(entity) ~= "character" then - return - end - - --move the character - characterAPI.SetRoom(entity, uidOne) - characterAPI.SetOrigin(entity, 0, 0) - networkAPI.PumpCharacterUpdate(entity) -end) ---]] +doorUtility.createDoorPair("pair 1", overworld, 0, -64, underworld, 0, 0) print("Finished the lua script")