From a6de5f9e69582919fc62d91826a68b6b93dcfc92 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 05:28:06 +1000 Subject: [PATCH 01/13] Imported the files from branch 'fixed-map' --- common/utilities/simple_rng.hpp | 42 ++++++++++++++++++++++++++ common/utilities/vector2.hpp | 14 ++++----- server/chunk_data.hpp | 44 ++++++++++++++++++++++++++++ server/overworld_generator.cpp | 35 ++++++++++++++++++++++ server/overworld_generator.hpp | 52 +++++++++++++++++++++++++++++++++ server/terrain_type.hpp | 40 +++++++++++++++++++++++++ 6 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 common/utilities/simple_rng.hpp create mode 100644 server/chunk_data.hpp create mode 100644 server/overworld_generator.cpp create mode 100644 server/overworld_generator.hpp create mode 100644 server/terrain_type.hpp diff --git a/common/utilities/simple_rng.hpp b/common/utilities/simple_rng.hpp new file mode 100644 index 0000000..52ca91b --- /dev/null +++ b/common/utilities/simple_rng.hpp @@ -0,0 +1,42 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 SIMPLERNG_HPP_ +#define SIMPLERNG_HPP_ + +//a simple, stateless, random number generator +class SimpleRNG { +public: + SimpleRNG() { SetSeed(8891.0); } + SimpleRNG(double x) { SetSeed(x); } + + double SetSeed(double s) { return seed = s; } + double GetSeed() { return seed; } + + double operator()(double x) { + return (x + seed) * 11235.0 + 81321.0; + }; + +private: + double seed; +}; + +#endif \ No newline at end of file diff --git a/common/utilities/vector2.hpp b/common/utilities/vector2.hpp index 1d8882d..2be3d34 100644 --- a/common/utilities/vector2.hpp +++ b/common/utilities/vector2.hpp @@ -41,6 +41,11 @@ public: double SquaredLength() const { return x*x+y*y; } + void Normalize() { + double l = Length(); + x /= l; + y /= l; + } //Arithmetic operators Vector2 operator+(Vector2 v) const { @@ -97,15 +102,6 @@ public: template bool operator!=(T t) { return (x != t || y != t); } }; -//non-member templates (flip the order) -template Vector2 operator+(T t, Vector2 v) { return v + t; } -template Vector2 operator-(T t, Vector2 v) { return v - t; } -template Vector2 operator*(T t, Vector2 v) { return v * t; } -template Vector2 operator/(T t, Vector2 v) { return v / t; } - -template bool operator==(T t, Vector2 v) { return v == t; } -template bool operator!=(T t, Vector2 v) { return v != t; } - //This is explicitly a POD static_assert(std::is_pod::value, "Vector2 is not a POD"); diff --git a/server/chunk_data.hpp b/server/chunk_data.hpp new file mode 100644 index 0000000..c0c1621 --- /dev/null +++ b/server/chunk_data.hpp @@ -0,0 +1,44 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 CHUNKDATA_HPP_ +#define CHUNKDATA_HPP_ + +#include "terrain_type.hpp" + +#include + +struct ChunkData { + enum class Moddable { + LOCKED, //do not change + HARD, //minor changes + SOFT, //major changes + CLEAR, //untouched + }; + + TerrainType type; +// int fountainCount; + Moddable mod; +}; + +static_assert(std::is_pod::value, "ChunkData is not a POD"); + +#endif diff --git a/server/overworld_generator.cpp b/server/overworld_generator.cpp new file mode 100644 index 0000000..2b56bba --- /dev/null +++ b/server/overworld_generator.cpp @@ -0,0 +1,35 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "overworld_generator.hpp" + +OverworldGenerator::OverworldGenerator() { + for (int i = 0; i < OVERWORLD_WIDTH; i++) { + for (int j = 0; j < OVERWORLD_HEIGHT; j++) { + chunks[i][j].type = TerrainType::NONE; + chunks[i][j].mod = ChunkData::Moddable::CLEAR; + } + } +} + +OverworldGenerator::~OverworldGenerator() { + // +} \ No newline at end of file diff --git a/server/overworld_generator.hpp b/server/overworld_generator.hpp new file mode 100644 index 0000000..a3724be --- /dev/null +++ b/server/overworld_generator.hpp @@ -0,0 +1,52 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 OVERWORLDGENERATOR_HPP_ +#define OVERWORLDGENERATOR_HPP_ + +#include "chunk_data.hpp" + +#include "lua/lua.hpp" + +#define OVERWORLD_GENERATOR_PSEUDOINDEX "OverworldGenerator" + +constexpr int OVERWORLD_WIDTH = 256; +constexpr int OVERWORLD_HEIGHT = 256; + +//TODO: SuperGenerator API +//TODO: SuperGenerator DOCS +class OverworldGenerator { +public: + OverworldGenerator(); + ~OverworldGenerator(); + + //accessors and mutators + ChunkData* GetChunk(int x, int y) { return &chunks[x][y]; } + + lua_State* SetLuaState(lua_State* L) { return luaState = L; } + lua_State* GetLuaState() { return luaState; } + +private: + ChunkData chunks[OVERWORLD_WIDTH][OVERWORLD_HEIGHT]; + lua_State* luaState = nullptr; +}; + +#endif diff --git a/server/terrain_type.hpp b/server/terrain_type.hpp new file mode 100644 index 0000000..aadfaa9 --- /dev/null +++ b/server/terrain_type.hpp @@ -0,0 +1,40 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 TERRAINTYPE_HPP_ +#define TERRAINTYPE_HPP_ + +enum class TerrainType { + //default: something's wrong + NONE = 0, + + //standard overworld + PLAINS, + GRASS, + DIRT, + SAND, + WATER, + + //not used + LAST, +}; + +#endif From d3bf099a981ebd54b0cd692bc8d8f1f8a73760f5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 05:52:27 +1000 Subject: [PATCH 02/13] Arranged server's source into directories --- server/{ => accounts}/account_data.hpp | 0 server/{ => accounts}/account_manager.cpp | 0 server/{ => accounts}/account_manager.hpp | 0 server/accounts/makefile | 37 +++++++++++++++++++ server/{ => characters}/character_manager.cpp | 0 server/{ => characters}/character_manager.hpp | 0 server/characters/makefile | 37 +++++++++++++++++++ server/{ => combat}/combat_manager.cpp | 0 server/{ => combat}/combat_manager.hpp | 0 server/combat/makefile | 37 +++++++++++++++++++ .../{ => enemies}/enemy_factory_generic.cpp | 0 .../{ => enemies}/enemy_factory_generic.hpp | 0 .../{ => enemies}/enemy_factory_interface.hpp | 0 server/{ => enemies}/enemy_manager.cpp | 0 server/{ => enemies}/enemy_manager.hpp | 0 server/enemies/makefile | 37 +++++++++++++++++++ server/makefile | 10 ++++- server/{ => mapgen}/chunk_data.hpp | 0 server/mapgen/makefile | 37 +++++++++++++++++++ server/{ => mapgen}/overworld_generator.cpp | 0 server/{ => mapgen}/overworld_generator.hpp | 0 server/{ => mapgen}/terrain_type.hpp | 0 server/rooms/makefile | 37 +++++++++++++++++++ server/{ => rooms}/room_api.cpp | 0 server/{ => rooms}/room_api.hpp | 0 server/{ => rooms}/room_data.hpp | 0 server/{ => rooms}/room_manager.cpp | 0 server/{ => rooms}/room_manager.hpp | 0 28 files changed, 230 insertions(+), 2 deletions(-) rename server/{ => accounts}/account_data.hpp (100%) rename server/{ => accounts}/account_manager.cpp (100%) rename server/{ => accounts}/account_manager.hpp (100%) create mode 100644 server/accounts/makefile rename server/{ => characters}/character_manager.cpp (100%) rename server/{ => characters}/character_manager.hpp (100%) create mode 100644 server/characters/makefile rename server/{ => combat}/combat_manager.cpp (100%) rename server/{ => combat}/combat_manager.hpp (100%) create mode 100644 server/combat/makefile rename server/{ => enemies}/enemy_factory_generic.cpp (100%) rename server/{ => enemies}/enemy_factory_generic.hpp (100%) rename server/{ => enemies}/enemy_factory_interface.hpp (100%) rename server/{ => enemies}/enemy_manager.cpp (100%) rename server/{ => enemies}/enemy_manager.hpp (100%) create mode 100644 server/enemies/makefile rename server/{ => mapgen}/chunk_data.hpp (100%) create mode 100644 server/mapgen/makefile rename server/{ => mapgen}/overworld_generator.cpp (100%) rename server/{ => mapgen}/overworld_generator.hpp (100%) rename server/{ => mapgen}/terrain_type.hpp (100%) create mode 100644 server/rooms/makefile rename server/{ => rooms}/room_api.cpp (100%) rename server/{ => rooms}/room_api.hpp (100%) rename server/{ => rooms}/room_data.hpp (100%) rename server/{ => rooms}/room_manager.cpp (100%) rename server/{ => rooms}/room_manager.hpp (100%) diff --git a/server/account_data.hpp b/server/accounts/account_data.hpp similarity index 100% rename from server/account_data.hpp rename to server/accounts/account_data.hpp diff --git a/server/account_manager.cpp b/server/accounts/account_manager.cpp similarity index 100% rename from server/account_manager.cpp rename to server/accounts/account_manager.cpp diff --git a/server/account_manager.hpp b/server/accounts/account_manager.hpp similarity index 100% rename from server/account_manager.hpp rename to server/accounts/account_manager.hpp diff --git a/server/accounts/makefile b/server/accounts/makefile new file mode 100644 index 0000000..8d12afe --- /dev/null +++ b/server/accounts/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/character_manager.cpp b/server/characters/character_manager.cpp similarity index 100% rename from server/character_manager.cpp rename to server/characters/character_manager.cpp diff --git a/server/character_manager.hpp b/server/characters/character_manager.hpp similarity index 100% rename from server/character_manager.hpp rename to server/characters/character_manager.hpp diff --git a/server/characters/makefile b/server/characters/makefile new file mode 100644 index 0000000..68368e7 --- /dev/null +++ b/server/characters/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../../common/gameplay ../../common/utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/combat_manager.cpp b/server/combat/combat_manager.cpp similarity index 100% rename from server/combat_manager.cpp rename to server/combat/combat_manager.cpp diff --git a/server/combat_manager.hpp b/server/combat/combat_manager.hpp similarity index 100% rename from server/combat_manager.hpp rename to server/combat/combat_manager.hpp diff --git a/server/combat/makefile b/server/combat/makefile new file mode 100644 index 0000000..68368e7 --- /dev/null +++ b/server/combat/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../../common/gameplay ../../common/utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/enemy_factory_generic.cpp b/server/enemies/enemy_factory_generic.cpp similarity index 100% rename from server/enemy_factory_generic.cpp rename to server/enemies/enemy_factory_generic.cpp diff --git a/server/enemy_factory_generic.hpp b/server/enemies/enemy_factory_generic.hpp similarity index 100% rename from server/enemy_factory_generic.hpp rename to server/enemies/enemy_factory_generic.hpp diff --git a/server/enemy_factory_interface.hpp b/server/enemies/enemy_factory_interface.hpp similarity index 100% rename from server/enemy_factory_interface.hpp rename to server/enemies/enemy_factory_interface.hpp diff --git a/server/enemy_manager.cpp b/server/enemies/enemy_manager.cpp similarity index 100% rename from server/enemy_manager.cpp rename to server/enemies/enemy_manager.cpp diff --git a/server/enemy_manager.hpp b/server/enemies/enemy_manager.hpp similarity index 100% rename from server/enemy_manager.hpp rename to server/enemies/enemy_manager.hpp diff --git a/server/enemies/makefile b/server/enemies/makefile new file mode 100644 index 0000000..5055ccf --- /dev/null +++ b/server/enemies/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../rooms ../../common/gameplay ../../common/map ../../common/utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/makefile b/server/makefile index 1d8df31..d596f04 100644 --- a/server/makefile +++ b/server/makefile @@ -1,6 +1,6 @@ #config -INCLUDES+=. ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities -LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 +INCLUDES+=. accounts characters combat enemies mapgen rooms ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities +LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) #source @@ -16,6 +16,12 @@ OUT=$(addprefix $(OUTDIR)/,server) #targets all: $(OBJ) $(OUT) + $(MAKE) -C accounts + $(MAKE) -C characters + $(MAKE) -C combat + $(MAKE) -C enemies + $(MAKE) -C mapgen + $(MAKE) -C rooms $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) $(OBJ): | $(OBJDIR) diff --git a/server/chunk_data.hpp b/server/mapgen/chunk_data.hpp similarity index 100% rename from server/chunk_data.hpp rename to server/mapgen/chunk_data.hpp diff --git a/server/mapgen/makefile b/server/mapgen/makefile new file mode 100644 index 0000000..8d12afe --- /dev/null +++ b/server/mapgen/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/overworld_generator.cpp b/server/mapgen/overworld_generator.cpp similarity index 100% rename from server/overworld_generator.cpp rename to server/mapgen/overworld_generator.cpp diff --git a/server/overworld_generator.hpp b/server/mapgen/overworld_generator.hpp similarity index 100% rename from server/overworld_generator.hpp rename to server/mapgen/overworld_generator.hpp diff --git a/server/terrain_type.hpp b/server/mapgen/terrain_type.hpp similarity index 100% rename from server/terrain_type.hpp rename to server/mapgen/terrain_type.hpp diff --git a/server/rooms/makefile b/server/rooms/makefile new file mode 100644 index 0000000..5eb6456 --- /dev/null +++ b/server/rooms/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../../common/map +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/room_api.cpp b/server/rooms/room_api.cpp similarity index 100% rename from server/room_api.cpp rename to server/rooms/room_api.cpp diff --git a/server/room_api.hpp b/server/rooms/room_api.hpp similarity index 100% rename from server/room_api.hpp rename to server/rooms/room_api.hpp diff --git a/server/room_data.hpp b/server/rooms/room_data.hpp similarity index 100% rename from server/room_data.hpp rename to server/rooms/room_data.hpp diff --git a/server/room_manager.cpp b/server/rooms/room_manager.cpp similarity index 100% rename from server/room_manager.cpp rename to server/rooms/room_manager.cpp diff --git a/server/room_manager.hpp b/server/rooms/room_manager.hpp similarity index 100% rename from server/room_manager.hpp rename to server/rooms/room_manager.hpp From 924ebc2ee9c21f7e55d5e0c536f537b6f25fc56c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 06:47:30 +1000 Subject: [PATCH 03/13] Created the generator hierarchy --- server/enemies/enemy_factory_interface.hpp | 11 ++--- server/enemies/makefile | 2 +- server/linit.cpp | 4 ++ server/mapgen/base_generator.cpp | 35 ++++++++++++++++ server/mapgen/base_generator.hpp | 48 ++++++++++++++++++++++ server/mapgen/caves_generator.cpp | 30 ++++++++++++++ server/mapgen/caves_generator.hpp | 38 +++++++++++++++++ server/mapgen/forests_generator.cpp | 30 ++++++++++++++ server/mapgen/forests_generator.hpp | 38 +++++++++++++++++ server/mapgen/generator_api.cpp | 37 +++++++++++++++++ server/mapgen/generator_api.hpp | 31 ++++++++++++++ server/mapgen/map_type.hpp | 33 +++++++++++++++ server/mapgen/overworld_generator.cpp | 7 +--- server/mapgen/overworld_generator.hpp | 20 ++------- server/mapgen/ruins_generator.cpp | 30 ++++++++++++++ server/mapgen/ruins_generator.hpp | 38 +++++++++++++++++ server/mapgen/towers_generator.cpp | 30 ++++++++++++++ server/mapgen/towers_generator.hpp | 38 +++++++++++++++++ server/rooms/makefile | 2 +- server/rooms/room_data.hpp | 12 ++---- 20 files changed, 475 insertions(+), 39 deletions(-) create mode 100644 server/mapgen/base_generator.cpp create mode 100644 server/mapgen/base_generator.hpp create mode 100644 server/mapgen/caves_generator.cpp create mode 100644 server/mapgen/caves_generator.hpp create mode 100644 server/mapgen/forests_generator.cpp create mode 100644 server/mapgen/forests_generator.hpp create mode 100644 server/mapgen/generator_api.cpp create mode 100644 server/mapgen/generator_api.hpp create mode 100644 server/mapgen/map_type.hpp create mode 100644 server/mapgen/ruins_generator.cpp create mode 100644 server/mapgen/ruins_generator.hpp create mode 100644 server/mapgen/towers_generator.cpp create mode 100644 server/mapgen/towers_generator.hpp diff --git a/server/enemies/enemy_factory_interface.hpp b/server/enemies/enemy_factory_interface.hpp index 335814e..8e97e78 100644 --- a/server/enemies/enemy_factory_interface.hpp +++ b/server/enemies/enemy_factory_interface.hpp @@ -23,7 +23,7 @@ #define ENEMYFACTORYINTERFACE_HPP_ #include "enemy_data.hpp" -#include "room_data.hpp" +#include "map_type.hpp" #include @@ -36,13 +36,14 @@ public: virtual void Generate(std::list* container) = 0; //control the difficulty of the room - RoomData::RoomType SetType(RoomData::RoomType t) { return type = t; } + MapType SetType(MapType t) { return type = t; } + MapType GetType() { return type; } + int SetDifficulty(int d) { return difficulty = d; } - RoomData::RoomType GetType() { return type; } int GetDifficulty() { return difficulty; } protected: - RoomData::RoomType type; - int difficulty; + MapType type; + int difficulty = 0; }; #endif diff --git a/server/enemies/makefile b/server/enemies/makefile index 5055ccf..8c2156a 100644 --- a/server/enemies/makefile +++ b/server/enemies/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../rooms ../../common/gameplay ../../common/map ../../common/utilities +INCLUDES+=. ../mapgen ../../common/gameplay ../../common/map ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/linit.cpp b/server/linit.cpp index 13541f2..b23af1f 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -38,6 +38,8 @@ #include "region_api.hpp" #include "pager_api.hpp" +#include "room_api.hpp" +#include "generator_api.hpp" //these libs are loaded by lua.c and are readily available to any Lua program static const luaL_Reg loadedlibs[] = { @@ -56,6 +58,8 @@ static const luaL_Reg loadedlibs[] = { //custom libs {LUA_REGIONLIBNAME, luaopen_regionapi}, {LUA_PAGERLIBNAME, luaopen_pagerapi}, + {LUA_ROOMLIBNAME, luaopen_roomapi}, + {LUA_GENERATORLIBNAME, luaopen_generatorapi}, {NULL, NULL} }; diff --git a/server/mapgen/base_generator.cpp b/server/mapgen/base_generator.cpp new file mode 100644 index 0000000..8ea5974 --- /dev/null +++ b/server/mapgen/base_generator.cpp @@ -0,0 +1,35 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "base_generator.hpp" + +BaseGenerator::BaseGenerator() { + for (int i = 0; i < MAP_WIDTH; i++) { + for (int j = 0; j < MAP_HEIGHT; j++) { + chunks[i][j].type = TerrainType::NONE; + chunks[i][j].mod = ChunkData::Moddable::CLEAR; + } + } +} + +BaseGenerator::~BaseGenerator() { + // +} \ No newline at end of file diff --git a/server/mapgen/base_generator.hpp b/server/mapgen/base_generator.hpp new file mode 100644 index 0000000..ef93c38 --- /dev/null +++ b/server/mapgen/base_generator.hpp @@ -0,0 +1,48 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 BASEGENERATOR_HPP_ +#define BASEGENERATOR_HPP_ + +#include "chunk_data.hpp" + +#include "lua/lua.hpp" + +constexpr int MAP_WIDTH = 256; +constexpr int MAP_HEIGHT = 256; + +class BaseGenerator { +public: + BaseGenerator(); + virtual ~BaseGenerator(); + + //accessors and mutators + virtual ChunkData* GetChunk(int x, int y) { return &chunks[x][y]; } + + lua_State* SetLuaState(lua_State* L) { return luaState = L; } + lua_State* GetLuaState() { return luaState; } + +protected: + ChunkData chunks[MAP_WIDTH][MAP_HEIGHT]; + lua_State* luaState = nullptr; +}; + +#endif diff --git a/server/mapgen/caves_generator.cpp b/server/mapgen/caves_generator.cpp new file mode 100644 index 0000000..a2cbaed --- /dev/null +++ b/server/mapgen/caves_generator.cpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "caves_generator.hpp" + +CavesGenerator::CavesGenerator() { + // +} + +CavesGenerator::~CavesGenerator() { + // +} \ No newline at end of file diff --git a/server/mapgen/caves_generator.hpp b/server/mapgen/caves_generator.hpp new file mode 100644 index 0000000..975ed8c --- /dev/null +++ b/server/mapgen/caves_generator.hpp @@ -0,0 +1,38 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 CAVESGENERATOR_HPP_ +#define CAVESGENERATOR_HPP_ + +#include "base_generator.hpp" + +#define CAVES_GENERATOR_PSEUDOINDEX "CavesGenerator" + +class CavesGenerator : public BaseGenerator { +public: + CavesGenerator(); + ~CavesGenerator(); + +private: + // +}; + +#endif diff --git a/server/mapgen/forests_generator.cpp b/server/mapgen/forests_generator.cpp new file mode 100644 index 0000000..5ff8127 --- /dev/null +++ b/server/mapgen/forests_generator.cpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "forests_generator.hpp" + +ForestsGenerator::ForestsGenerator() { + // +} + +ForestsGenerator::~ForestsGenerator() { + // +} \ No newline at end of file diff --git a/server/mapgen/forests_generator.hpp b/server/mapgen/forests_generator.hpp new file mode 100644 index 0000000..e87d89e --- /dev/null +++ b/server/mapgen/forests_generator.hpp @@ -0,0 +1,38 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 FORESTSGENERATOR_HPP_ +#define FORESTSGENERATOR_HPP_ + +#include "base_generator.hpp" + +#define FORESTS_GENERATOR_PSEUDOINDEX "ForestsGenerator" + +class ForestsGenerator : public BaseGenerator { +public: + ForestsGenerator(); + ~ForestsGenerator(); + +private: + // +}; + +#endif diff --git a/server/mapgen/generator_api.cpp b/server/mapgen/generator_api.cpp new file mode 100644 index 0000000..2593fb5 --- /dev/null +++ b/server/mapgen/generator_api.cpp @@ -0,0 +1,37 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "generator_api.hpp" + +static int getGenerator(lua_State* L) { + //TODO: return a generator based on the given parameter + return 0; +} + +static const luaL_Reg generatorlib[] = { + {"getgenerator", getGenerator}, + {nullptr, nullptr} +}; + +LUAMOD_API int luaopen_generatorapi(lua_State* L) { + luaL_newlib(L, generatorlib); + return 1; +} diff --git a/server/mapgen/generator_api.hpp b/server/mapgen/generator_api.hpp new file mode 100644 index 0000000..a0b9fb3 --- /dev/null +++ b/server/mapgen/generator_api.hpp @@ -0,0 +1,31 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 GENERATORAPI_HPP_ +#define GENERATORAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_GENERATORLIBNAME "generator" +LUAMOD_API int luaopen_generatorapi(lua_State* L); + + +#endif \ No newline at end of file diff --git a/server/mapgen/map_type.hpp b/server/mapgen/map_type.hpp new file mode 100644 index 0000000..3127e1a --- /dev/null +++ b/server/mapgen/map_type.hpp @@ -0,0 +1,33 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 MAPTYPE_HPP_ +#define MAPTYPE_HPP_ + +enum class MapType { + OVERWORLD = 0, + RUINS = 1, + TOWERS = 2, + FORESTS = 3, + CAVES = 4, +}; + +#endif \ No newline at end of file diff --git a/server/mapgen/overworld_generator.cpp b/server/mapgen/overworld_generator.cpp index 2b56bba..8119158 100644 --- a/server/mapgen/overworld_generator.cpp +++ b/server/mapgen/overworld_generator.cpp @@ -22,12 +22,7 @@ #include "overworld_generator.hpp" OverworldGenerator::OverworldGenerator() { - for (int i = 0; i < OVERWORLD_WIDTH; i++) { - for (int j = 0; j < OVERWORLD_HEIGHT; j++) { - chunks[i][j].type = TerrainType::NONE; - chunks[i][j].mod = ChunkData::Moddable::CLEAR; - } - } + // } OverworldGenerator::~OverworldGenerator() { diff --git a/server/mapgen/overworld_generator.hpp b/server/mapgen/overworld_generator.hpp index a3724be..e47026d 100644 --- a/server/mapgen/overworld_generator.hpp +++ b/server/mapgen/overworld_generator.hpp @@ -22,31 +22,17 @@ #ifndef OVERWORLDGENERATOR_HPP_ #define OVERWORLDGENERATOR_HPP_ -#include "chunk_data.hpp" - -#include "lua/lua.hpp" +#include "base_generator.hpp" #define OVERWORLD_GENERATOR_PSEUDOINDEX "OverworldGenerator" -constexpr int OVERWORLD_WIDTH = 256; -constexpr int OVERWORLD_HEIGHT = 256; - -//TODO: SuperGenerator API -//TODO: SuperGenerator DOCS -class OverworldGenerator { +class OverworldGenerator : public BaseGenerator { public: OverworldGenerator(); ~OverworldGenerator(); - //accessors and mutators - ChunkData* GetChunk(int x, int y) { return &chunks[x][y]; } - - lua_State* SetLuaState(lua_State* L) { return luaState = L; } - lua_State* GetLuaState() { return luaState; } - private: - ChunkData chunks[OVERWORLD_WIDTH][OVERWORLD_HEIGHT]; - lua_State* luaState = nullptr; + // }; #endif diff --git a/server/mapgen/ruins_generator.cpp b/server/mapgen/ruins_generator.cpp new file mode 100644 index 0000000..24a7c3e --- /dev/null +++ b/server/mapgen/ruins_generator.cpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "ruins_generator.hpp" + +RuinsGenerator::RuinsGenerator() { + // +} + +RuinsGenerator::~RuinsGenerator() { + // +} \ No newline at end of file diff --git a/server/mapgen/ruins_generator.hpp b/server/mapgen/ruins_generator.hpp new file mode 100644 index 0000000..309bf45 --- /dev/null +++ b/server/mapgen/ruins_generator.hpp @@ -0,0 +1,38 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 RUINSGENERATOR_HPP_ +#define RUINSGENERATOR_HPP_ + +#include "base_generator.hpp" + +#define RUINS_GENERATOR_PSEUDOINDEX "RuinsGenerator" + +class RuinsGenerator : public BaseGenerator { +public: + RuinsGenerator(); + ~RuinsGenerator(); + +private: + // +}; + +#endif diff --git a/server/mapgen/towers_generator.cpp b/server/mapgen/towers_generator.cpp new file mode 100644 index 0000000..3e8a2c6 --- /dev/null +++ b/server/mapgen/towers_generator.cpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 "towers_generator.hpp" + +TowersGenerator::TowersGenerator() { + // +} + +TowersGenerator::~TowersGenerator() { + // +} \ No newline at end of file diff --git a/server/mapgen/towers_generator.hpp b/server/mapgen/towers_generator.hpp new file mode 100644 index 0000000..0dcb9d9 --- /dev/null +++ b/server/mapgen/towers_generator.hpp @@ -0,0 +1,38 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 TOWERSGENERATOR_HPP_ +#define TOWERSGENERATOR_HPP_ + +#include "base_generator.hpp" + +#define TOWERS_GENERATOR_PSEUDOINDEX "TowersGenerator" + +class TowersGenerator : public BaseGenerator { +public: + TowersGenerator(); + ~TowersGenerator(); + +private: + // +}; + +#endif diff --git a/server/rooms/makefile b/server/rooms/makefile index 5eb6456..1316fb4 100644 --- a/server/rooms/makefile +++ b/server/rooms/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../../common/map +INCLUDES+=. ../mapgen ../../common/map LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index cf1b6cb..872ffb6 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -22,21 +22,15 @@ #ifndef ROOMDATA_HPP_ #define ROOMDATA_HPP_ +#include "map_type.hpp" + //map system #include "region_pager_lua.hpp" struct RoomData { - enum class RoomType { - OVERWORLD = 0, - RUINS = 1, - TOWERS = 2, - FORESTS = 3, - CAVE = 4, - }; - //members RegionPagerLua pager; - RoomType type; + MapType type; //TODO: collision map //TODO: NPCs? From 82c776df832301c6bdcdeeea3ade80a7a3680401 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 07:00:48 +1000 Subject: [PATCH 04/13] Moved the generators into a subdirectory --- server/makefile | 2 +- .../{ => generators}/base_generator.cpp | 0 .../{ => generators}/base_generator.hpp | 0 .../{ => generators}/caves_generator.cpp | 0 .../{ => generators}/caves_generator.hpp | 0 .../{ => generators}/forests_generator.cpp | 0 .../{ => generators}/forests_generator.hpp | 0 server/mapgen/generators/makefile | 37 +++++++++++++++++++ .../{ => generators}/overworld_generator.cpp | 0 .../{ => generators}/overworld_generator.hpp | 0 .../{ => generators}/ruins_generator.cpp | 0 .../{ => generators}/ruins_generator.hpp | 0 .../{ => generators}/towers_generator.cpp | 0 .../{ => generators}/towers_generator.hpp | 0 server/mapgen/makefile | 1 + server/rooms/makefile | 2 +- 16 files changed, 40 insertions(+), 2 deletions(-) rename server/mapgen/{ => generators}/base_generator.cpp (100%) rename server/mapgen/{ => generators}/base_generator.hpp (100%) rename server/mapgen/{ => generators}/caves_generator.cpp (100%) rename server/mapgen/{ => generators}/caves_generator.hpp (100%) rename server/mapgen/{ => generators}/forests_generator.cpp (100%) rename server/mapgen/{ => generators}/forests_generator.hpp (100%) create mode 100644 server/mapgen/generators/makefile rename server/mapgen/{ => generators}/overworld_generator.cpp (100%) rename server/mapgen/{ => generators}/overworld_generator.hpp (100%) rename server/mapgen/{ => generators}/ruins_generator.cpp (100%) rename server/mapgen/{ => generators}/ruins_generator.hpp (100%) rename server/mapgen/{ => generators}/towers_generator.cpp (100%) rename server/mapgen/{ => generators}/towers_generator.hpp (100%) diff --git a/server/makefile b/server/makefile index d596f04..642dec3 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. accounts characters combat enemies mapgen rooms ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities +INCLUDES+=. accounts characters combat enemies mapgen mapgen/generators rooms ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/mapgen/base_generator.cpp b/server/mapgen/generators/base_generator.cpp similarity index 100% rename from server/mapgen/base_generator.cpp rename to server/mapgen/generators/base_generator.cpp diff --git a/server/mapgen/base_generator.hpp b/server/mapgen/generators/base_generator.hpp similarity index 100% rename from server/mapgen/base_generator.hpp rename to server/mapgen/generators/base_generator.hpp diff --git a/server/mapgen/caves_generator.cpp b/server/mapgen/generators/caves_generator.cpp similarity index 100% rename from server/mapgen/caves_generator.cpp rename to server/mapgen/generators/caves_generator.cpp diff --git a/server/mapgen/caves_generator.hpp b/server/mapgen/generators/caves_generator.hpp similarity index 100% rename from server/mapgen/caves_generator.hpp rename to server/mapgen/generators/caves_generator.hpp diff --git a/server/mapgen/forests_generator.cpp b/server/mapgen/generators/forests_generator.cpp similarity index 100% rename from server/mapgen/forests_generator.cpp rename to server/mapgen/generators/forests_generator.cpp diff --git a/server/mapgen/forests_generator.hpp b/server/mapgen/generators/forests_generator.hpp similarity index 100% rename from server/mapgen/forests_generator.hpp rename to server/mapgen/generators/forests_generator.hpp diff --git a/server/mapgen/generators/makefile b/server/mapgen/generators/makefile new file mode 100644 index 0000000..3e4ed5b --- /dev/null +++ b/server/mapgen/generators/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. .. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=../.. +OUT=$(addprefix $(OUTDIR)/,server.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/server/mapgen/overworld_generator.cpp b/server/mapgen/generators/overworld_generator.cpp similarity index 100% rename from server/mapgen/overworld_generator.cpp rename to server/mapgen/generators/overworld_generator.cpp diff --git a/server/mapgen/overworld_generator.hpp b/server/mapgen/generators/overworld_generator.hpp similarity index 100% rename from server/mapgen/overworld_generator.hpp rename to server/mapgen/generators/overworld_generator.hpp diff --git a/server/mapgen/ruins_generator.cpp b/server/mapgen/generators/ruins_generator.cpp similarity index 100% rename from server/mapgen/ruins_generator.cpp rename to server/mapgen/generators/ruins_generator.cpp diff --git a/server/mapgen/ruins_generator.hpp b/server/mapgen/generators/ruins_generator.hpp similarity index 100% rename from server/mapgen/ruins_generator.hpp rename to server/mapgen/generators/ruins_generator.hpp diff --git a/server/mapgen/towers_generator.cpp b/server/mapgen/generators/towers_generator.cpp similarity index 100% rename from server/mapgen/towers_generator.cpp rename to server/mapgen/generators/towers_generator.cpp diff --git a/server/mapgen/towers_generator.hpp b/server/mapgen/generators/towers_generator.hpp similarity index 100% rename from server/mapgen/towers_generator.hpp rename to server/mapgen/generators/towers_generator.hpp diff --git a/server/mapgen/makefile b/server/mapgen/makefile index 8d12afe..d949a54 100644 --- a/server/mapgen/makefile +++ b/server/mapgen/makefile @@ -17,6 +17,7 @@ OUT=$(addprefix $(OUTDIR)/,server.a) #targets all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) + $(MAKE) -C generators $(OBJ): | $(OBJDIR) diff --git a/server/rooms/makefile b/server/rooms/makefile index 1316fb4..f4921ea 100644 --- a/server/rooms/makefile +++ b/server/rooms/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../mapgen ../../common/map +INCLUDES+=. ../mapgen ../mapgen/generators ../../common/map LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) From 8afd0e7c8ac1ce3f76d879edd7aea32dbc443391 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 07:19:51 +1000 Subject: [PATCH 05/13] Created a separate API for the RoomManager class --- server/linit.cpp | 2 ++ server/rooms/room_api.cpp | 13 +--------- server/rooms/room_data.hpp | 7 +++--- server/rooms/room_mgr_api.cpp | 46 +++++++++++++++++++++++++++++++++++ server/rooms/room_mgr_api.hpp | 30 +++++++++++++++++++++++ 5 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 server/rooms/room_mgr_api.cpp create mode 100644 server/rooms/room_mgr_api.hpp diff --git a/server/linit.cpp b/server/linit.cpp index b23af1f..7898184 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -39,6 +39,7 @@ #include "region_api.hpp" #include "pager_api.hpp" #include "room_api.hpp" +#include "room_mgr_api.hpp" #include "generator_api.hpp" //these libs are loaded by lua.c and are readily available to any Lua program @@ -59,6 +60,7 @@ static const luaL_Reg loadedlibs[] = { {LUA_REGIONLIBNAME, luaopen_regionapi}, {LUA_PAGERLIBNAME, luaopen_pagerapi}, {LUA_ROOMLIBNAME, luaopen_roomapi}, + {LUA_ROOMMGRLIBNAME, luaopen_roommgrapi}, {LUA_GENERATORLIBNAME, luaopen_generatorapi}, {NULL, NULL} diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index ea7ea52..9f09f3c 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -38,22 +38,11 @@ static int getRegionPager(lua_State* L) { return 1; } -//RoomManager only -static int getRoom(lua_State* L) { - //get the room manager - lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); - lua_gettable(L, LUA_REGISTRYINDEX); - RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); - - //push the room and return it - lua_pushlightuserdata(L, reinterpret_cast( roomMgr->GetRoom(lua_tointeger(L, -2)) )); - return 1; -} +//TODO: generators static const luaL_Reg roomlib[] = { {"gettype",getType}, {"getregionpager",getRegionPager}, - {"getroom",getRoom}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 872ffb6..4b9e811 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -22,15 +22,16 @@ #ifndef ROOMDATA_HPP_ #define ROOMDATA_HPP_ -#include "map_type.hpp" - //map system +#include "map_type.hpp" #include "region_pager_lua.hpp" +#include "base_generator.hpp" struct RoomData { //members - RegionPagerLua pager; MapType type; + RegionPagerLua pager; + BaseGenerator* generator = nullptr; //TODO: collision map //TODO: NPCs? diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp new file mode 100644 index 0000000..44050dc --- /dev/null +++ b/server/rooms/room_mgr_api.cpp @@ -0,0 +1,46 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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_mgr_api.hpp" + +#include "room_manager.hpp" +#include "room_data.hpp" + +static int getRoom(lua_State* L) { + //get the room manager + lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); + + //push the room and return it + lua_pushlightuserdata(L, reinterpret_cast( roomMgr->GetRoom(lua_tointeger(L, -2)) )); + return 1; +} + +static const luaL_Reg roommgrlib[] = { + {"getroom",getRoom}, + {nullptr, nullptr} +}; + +LUAMOD_API int luaopen_roommgrapi(lua_State* L) { + luaL_newlib(L, roommgrlib); + return 1; +} \ No newline at end of file diff --git a/server/rooms/room_mgr_api.hpp b/server/rooms/room_mgr_api.hpp new file mode 100644 index 0000000..1382b4d --- /dev/null +++ b/server/rooms/room_mgr_api.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * 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 ROOMMGRAPI_HPP_ +#define ROOMMGRAPI_HPP_ + +#include "lua/lua.hpp" + +#define LUA_ROOMMGRLIBNAME "roommgr" +LUAMOD_API int luaopen_roommgrapi(lua_State* L); + +#endif From 3662a97475d0d182724770c72216efef93af8c5a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 18:10:57 +1000 Subject: [PATCH 06/13] Added mapType to BaseGenerator --- server/mapgen/generators/base_generator.cpp | 3 ++- server/mapgen/generators/base_generator.hpp | 8 +++++++- server/mapgen/generators/caves_generator.cpp | 2 +- server/mapgen/generators/caves_generator.hpp | 2 -- server/mapgen/generators/forests_generator.cpp | 2 +- server/mapgen/generators/forests_generator.hpp | 2 -- server/mapgen/generators/overworld_generator.cpp | 2 +- server/mapgen/generators/overworld_generator.hpp | 2 -- server/mapgen/generators/ruins_generator.cpp | 2 +- server/mapgen/generators/ruins_generator.hpp | 2 -- server/mapgen/generators/towers_generator.cpp | 2 +- server/mapgen/generators/towers_generator.hpp | 2 -- server/mapgen/map_type.hpp | 11 ++++++----- 13 files changed, 20 insertions(+), 22 deletions(-) diff --git a/server/mapgen/generators/base_generator.cpp b/server/mapgen/generators/base_generator.cpp index 8ea5974..9afab6b 100644 --- a/server/mapgen/generators/base_generator.cpp +++ b/server/mapgen/generators/base_generator.cpp @@ -21,7 +21,8 @@ */ #include "base_generator.hpp" -BaseGenerator::BaseGenerator() { +BaseGenerator::BaseGenerator(MapType t) { + mapType = t; for (int i = 0; i < MAP_WIDTH; i++) { for (int j = 0; j < MAP_HEIGHT; j++) { chunks[i][j].type = TerrainType::NONE; diff --git a/server/mapgen/generators/base_generator.hpp b/server/mapgen/generators/base_generator.hpp index ef93c38..6d629cd 100644 --- a/server/mapgen/generators/base_generator.hpp +++ b/server/mapgen/generators/base_generator.hpp @@ -22,6 +22,7 @@ #ifndef BASEGENERATOR_HPP_ #define BASEGENERATOR_HPP_ +#include "map_type.hpp" #include "chunk_data.hpp" #include "lua/lua.hpp" @@ -31,17 +32,22 @@ constexpr int MAP_HEIGHT = 256; class BaseGenerator { public: - BaseGenerator(); virtual ~BaseGenerator(); //accessors and mutators virtual ChunkData* GetChunk(int x, int y) { return &chunks[x][y]; } + MapType GetMapType() { return mapType; } + lua_State* SetLuaState(lua_State* L) { return luaState = L; } lua_State* GetLuaState() { return luaState; } protected: + BaseGenerator() = delete; + BaseGenerator(MapType t); + ChunkData chunks[MAP_WIDTH][MAP_HEIGHT]; + MapType mapType = MapType::NONE; lua_State* luaState = nullptr; }; diff --git a/server/mapgen/generators/caves_generator.cpp b/server/mapgen/generators/caves_generator.cpp index a2cbaed..0019bf5 100644 --- a/server/mapgen/generators/caves_generator.cpp +++ b/server/mapgen/generators/caves_generator.cpp @@ -21,7 +21,7 @@ */ #include "caves_generator.hpp" -CavesGenerator::CavesGenerator() { +CavesGenerator::CavesGenerator() : BaseGenerator(MapType::CAVES) { // } diff --git a/server/mapgen/generators/caves_generator.hpp b/server/mapgen/generators/caves_generator.hpp index 975ed8c..062a86d 100644 --- a/server/mapgen/generators/caves_generator.hpp +++ b/server/mapgen/generators/caves_generator.hpp @@ -24,8 +24,6 @@ #include "base_generator.hpp" -#define CAVES_GENERATOR_PSEUDOINDEX "CavesGenerator" - class CavesGenerator : public BaseGenerator { public: CavesGenerator(); diff --git a/server/mapgen/generators/forests_generator.cpp b/server/mapgen/generators/forests_generator.cpp index 5ff8127..cbff079 100644 --- a/server/mapgen/generators/forests_generator.cpp +++ b/server/mapgen/generators/forests_generator.cpp @@ -21,7 +21,7 @@ */ #include "forests_generator.hpp" -ForestsGenerator::ForestsGenerator() { +ForestsGenerator::ForestsGenerator() : BaseGenerator(MapType::FORESTS) { // } diff --git a/server/mapgen/generators/forests_generator.hpp b/server/mapgen/generators/forests_generator.hpp index e87d89e..459428f 100644 --- a/server/mapgen/generators/forests_generator.hpp +++ b/server/mapgen/generators/forests_generator.hpp @@ -24,8 +24,6 @@ #include "base_generator.hpp" -#define FORESTS_GENERATOR_PSEUDOINDEX "ForestsGenerator" - class ForestsGenerator : public BaseGenerator { public: ForestsGenerator(); diff --git a/server/mapgen/generators/overworld_generator.cpp b/server/mapgen/generators/overworld_generator.cpp index 8119158..7bcd1c5 100644 --- a/server/mapgen/generators/overworld_generator.cpp +++ b/server/mapgen/generators/overworld_generator.cpp @@ -21,7 +21,7 @@ */ #include "overworld_generator.hpp" -OverworldGenerator::OverworldGenerator() { +OverworldGenerator::OverworldGenerator() : BaseGenerator(MapType::OVERWORLD) { // } diff --git a/server/mapgen/generators/overworld_generator.hpp b/server/mapgen/generators/overworld_generator.hpp index e47026d..63fd418 100644 --- a/server/mapgen/generators/overworld_generator.hpp +++ b/server/mapgen/generators/overworld_generator.hpp @@ -24,8 +24,6 @@ #include "base_generator.hpp" -#define OVERWORLD_GENERATOR_PSEUDOINDEX "OverworldGenerator" - class OverworldGenerator : public BaseGenerator { public: OverworldGenerator(); diff --git a/server/mapgen/generators/ruins_generator.cpp b/server/mapgen/generators/ruins_generator.cpp index 24a7c3e..164de89 100644 --- a/server/mapgen/generators/ruins_generator.cpp +++ b/server/mapgen/generators/ruins_generator.cpp @@ -21,7 +21,7 @@ */ #include "ruins_generator.hpp" -RuinsGenerator::RuinsGenerator() { +RuinsGenerator::RuinsGenerator() : BaseGenerator(MapType::RUINS) { // } diff --git a/server/mapgen/generators/ruins_generator.hpp b/server/mapgen/generators/ruins_generator.hpp index 309bf45..fbc0518 100644 --- a/server/mapgen/generators/ruins_generator.hpp +++ b/server/mapgen/generators/ruins_generator.hpp @@ -24,8 +24,6 @@ #include "base_generator.hpp" -#define RUINS_GENERATOR_PSEUDOINDEX "RuinsGenerator" - class RuinsGenerator : public BaseGenerator { public: RuinsGenerator(); diff --git a/server/mapgen/generators/towers_generator.cpp b/server/mapgen/generators/towers_generator.cpp index 3e8a2c6..ad50a09 100644 --- a/server/mapgen/generators/towers_generator.cpp +++ b/server/mapgen/generators/towers_generator.cpp @@ -21,7 +21,7 @@ */ #include "towers_generator.hpp" -TowersGenerator::TowersGenerator() { +TowersGenerator::TowersGenerator() : BaseGenerator(MapType::TOWERS) { // } diff --git a/server/mapgen/generators/towers_generator.hpp b/server/mapgen/generators/towers_generator.hpp index 0dcb9d9..48a29bb 100644 --- a/server/mapgen/generators/towers_generator.hpp +++ b/server/mapgen/generators/towers_generator.hpp @@ -24,8 +24,6 @@ #include "base_generator.hpp" -#define TOWERS_GENERATOR_PSEUDOINDEX "TowersGenerator" - class TowersGenerator : public BaseGenerator { public: TowersGenerator(); diff --git a/server/mapgen/map_type.hpp b/server/mapgen/map_type.hpp index 3127e1a..e56b924 100644 --- a/server/mapgen/map_type.hpp +++ b/server/mapgen/map_type.hpp @@ -23,11 +23,12 @@ #define MAPTYPE_HPP_ enum class MapType { - OVERWORLD = 0, - RUINS = 1, - TOWERS = 2, - FORESTS = 3, - CAVES = 4, + NONE, + OVERWORLD, + RUINS, + TOWERS, + FORESTS, + CAVES, }; #endif \ No newline at end of file From a64411a567e553af4ff42804c13e2d834907e8ef Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 18:24:58 +1000 Subject: [PATCH 07/13] Filled out the APIs a bit --- server/mapgen/generator_api.cpp | 50 ++++++++++++++++++++++++++++++--- server/mapgen/makefile | 2 +- server/rooms/room_api.cpp | 23 +++++++-------- server/rooms/room_data.hpp | 1 - server/rooms/room_mgr_api.cpp | 25 +++++++++++++++++ 5 files changed, 82 insertions(+), 19 deletions(-) diff --git a/server/mapgen/generator_api.cpp b/server/mapgen/generator_api.cpp index 2593fb5..c3b0eb9 100644 --- a/server/mapgen/generator_api.cpp +++ b/server/mapgen/generator_api.cpp @@ -21,13 +21,55 @@ */ #include "generator_api.hpp" -static int getGenerator(lua_State* L) { - //TODO: return a generator based on the given parameter - return 0; +#include "base_generator.hpp" + +static int getMapType(lua_State* L) { + BaseGenerator* ptr = reinterpret_cast(lua_touserdata(L, 1)); + switch(ptr->GetMapType()) { + case MapType::NONE: + lua_pushstring(L, "none"); + break; + case MapType::OVERWORLD: + lua_pushstring(L, "overworld"); + break; + case MapType::RUINS: + lua_pushstring(L, "ruins"); + break; + case MapType::TOWERS: + lua_pushstring(L, "towers"); + break; + case MapType::FORESTS: + lua_pushstring(L, "forests"); + break; + case MapType::CAVES: + lua_pushstring(L, "caves"); + break; + } + return 1; +} + +static int getChunk(lua_State* L) { + BaseGenerator* ptr = reinterpret_cast(lua_touserdata(L, 1)); + ChunkData* chunk = ptr->GetChunk(lua_tointeger(L, 2), lua_tointeger(L, 3)); + lua_pushlightuserdata(L, reinterpret_cast(chunk)); + return 1; +} + +static int getMapWidth(lua_State* L) { + lua_pushinteger(L, MAP_WIDTH); + return 1; +} + +static int getMapHeight(lua_State* L) { + lua_pushinteger(L, MAP_HEIGHT); + return 1; } static const luaL_Reg generatorlib[] = { - {"getgenerator", getGenerator}, + {"gettype", getMapType}, + {"getchunk", getChunk}, + {"getmapwidth", getMapWidth}, + {"getmapheight", getMapHeight}, {nullptr, nullptr} }; diff --git a/server/mapgen/makefile b/server/mapgen/makefile index d949a54..9a44bb4 100644 --- a/server/mapgen/makefile +++ b/server/mapgen/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. +INCLUDES+=. generators LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 9f09f3c..51c0101 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -21,28 +21,25 @@ */ #include "room_api.hpp" -#include "room_manager.hpp" #include "room_data.hpp" -static int getType(lua_State* L) { - RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); - lua_pushinteger(L, static_cast(room->type)); - return 1; -} - -//TODO: parameters - -static int getRegionPager(lua_State* L) { +static int getPager(lua_State* L) { RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); lua_pushlightuserdata(L, reinterpret_cast(&room->pager)); return 1; } -//TODO: generators +static int getGenerator(lua_State* L) { + RoomData* room = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushlightuserdata(L, reinterpret_cast(room->generator)); + return 1; +} + +//TODO: parameters static const luaL_Reg roomlib[] = { - {"gettype",getType}, - {"getregionpager",getRegionPager}, + {"getpager",getPager}, + {"getgenerator",getGenerator}, {nullptr, nullptr} }; diff --git a/server/rooms/room_data.hpp b/server/rooms/room_data.hpp index 4b9e811..660e057 100644 --- a/server/rooms/room_data.hpp +++ b/server/rooms/room_data.hpp @@ -29,7 +29,6 @@ struct RoomData { //members - MapType type; RegionPagerLua pager; BaseGenerator* generator = nullptr; diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index 44050dc..6539002 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -35,8 +35,33 @@ static int getRoom(lua_State* L) { return 1; } +static int createRoom(lua_State* L) { + //get the room manager + lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); + + //TODO: create room + + return 0; +} + +static int unloadRoom(lua_State* L) { + //get the room manager + lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); + + //unload the specified room + roomMgr->UnloadRoom(lua_tointeger(L, -2)); + + return 0; +} + static const luaL_Reg roommgrlib[] = { {"getroom",getRoom}, + {"createroom",createRoom}, + {"unloadroom",unloadRoom}, {nullptr, nullptr} }; From e19b6fbc23974ea17f39add6ddcb56caa3747cd8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 19:02:43 +1000 Subject: [PATCH 08/13] Updated RoomManager and the API --- server/rooms/room_manager.cpp | 67 +++++++++++++++++++++++------------ server/rooms/room_manager.hpp | 7 ++-- server/rooms/room_mgr_api.cpp | 19 ++++++++-- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 8f60ba4..cc0b330 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -21,37 +21,64 @@ */ #include "room_manager.hpp" +//the generator types +#include "overworld_generator.hpp" +#include "ruins_generator.hpp" +#include "towers_generator.hpp" +#include "forests_generator.hpp" +#include "caves_generator.hpp" + #include //------------------------- //public access methods //------------------------- -RoomData* RoomManager::CreateRoom(int uid) { - //don't overwrite existing rooms - std::map::iterator it = roomMap.find(uid); - if (it != roomMap.end()) { - throw(std::runtime_error("Cannot overwrite an existing room")); - } - roomMap[uid] = new RoomData(); - //TODO: create room in the API +int RoomManager::CreateRoom(MapType mapType) { + //create the room + RoomData* newRoom = new RoomData(); + + //set the state if (luaState) { - roomMap[uid]->pager.SetLuaState(luaState); + newRoom->pager.SetLuaState(luaState); } - return roomMap[uid]; + + //create the generator + newRoom->generator = [mapType]() -> BaseGenerator* { + switch(mapType) { + case MapType::NONE: //use overworld as a default + case MapType::OVERWORLD: return new OverworldGenerator(); + case MapType::RUINS: return new RuinsGenerator(); + case MapType::TOWERS: return new TowersGenerator(); + case MapType::FORESTS: return new ForestsGenerator(); + case MapType::CAVES: return new CavesGenerator(); + } + throw(std::runtime_error("Failed to set the room's generator")); + }(); + + //finish the routine + roomMap[counter] = newRoom; + return counter++; } -RoomData* RoomManager::UnloadRoom(int uid) { - //TODO: unload room in the API - delete roomMap[uid]; +int RoomManager::UnloadRoom(int uid) { + RoomData* room = FindRoom(uid); + if (!room) { + return -1; + } + + delete room->generator; + delete room; roomMap.erase(uid); + + return 0; } RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - ptr = CreateRoom(uid); - return ptr; + int newIndex = CreateRoom(MapType::NONE); + return FindRoom(newIndex); } RoomData* RoomManager::FindRoom(int uid) { @@ -62,11 +89,7 @@ RoomData* RoomManager::FindRoom(int uid) { return it->second; } -RoomData* RoomManager::PushRoom(int uid, RoomData* room) { - //unload existing rooms with this index - std::map::iterator it = roomMap.find(uid); - if (it != roomMap.end()) { - UnloadRoom(uid); - } - roomMap[uid] = room; +int RoomManager::PushRoom(RoomData* room) { + roomMap[counter] = room; + return counter++; } diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 80480f7..30129c1 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -36,12 +36,12 @@ public: ~RoomManager() = default; //public access methods - RoomData* CreateRoom(int uid); - RoomData* UnloadRoom(int uid); + int CreateRoom(MapType); + int UnloadRoom(int uid); RoomData* GetRoom(int uid); RoomData* FindRoom(int uid); - RoomData* PushRoom(int uid, RoomData*); + int PushRoom(RoomData*); //accessors and mutators std::map* GetContainer() { return &roomMap; } @@ -52,6 +52,7 @@ public: private: std::map roomMap; lua_State* luaState = nullptr; + int counter = 0; }; #endif \ No newline at end of file diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index 6539002..0e58729 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -24,6 +24,8 @@ #include "room_manager.hpp" #include "room_data.hpp" +#include + static int getRoom(lua_State* L) { //get the room manager lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); @@ -41,9 +43,22 @@ static int createRoom(lua_State* L) { lua_gettable(L, LUA_REGISTRYINDEX); RoomManager* roomMgr = reinterpret_cast(lua_touserdata(L, -1)); - //TODO: create room + //determine the specified room type + MapType mapType = [L]() -> MapType { + if (std::string("overworld") == lua_tostring(L, -2)) return MapType::OVERWORLD; + if (std::string("ruins") == lua_tostring(L, -2)) return MapType::RUINS; + if (std::string("towers") == lua_tostring(L, -2)) return MapType::TOWERS; + if (std::string("forests") == lua_tostring(L, -2)) return MapType::FORESTS; + if (std::string("caves") == lua_tostring(L, -2)) return MapType::CAVES; + return MapType::NONE; + }(); - return 0; + //create the room + int newIndex = roomMgr->CreateRoom(mapType); + + //return the index + lua_pushinteger(L, newIndex); + return 1; } static int unloadRoom(lua_State* L) { From 64baa63d128e3b7c7e49d8b36b1937fb2908aacc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 03:45:30 +1000 Subject: [PATCH 09/13] Changed the naming conventions (read more) I've changed some naming concentions in the lua APIs. I've also made a few other tweaks, like region_pager_api.cpp delegating to the passed RegionPager object. This won't explicitly run, becuase there's still a few more changes needed. --- common/map/region_api.cpp | 44 +++++----- common/map/region_api.hpp | 4 +- .../{pager_api.cpp => region_pager_api.cpp} | 84 +++++-------------- .../{pager_api.hpp => region_pager_api.hpp} | 4 +- common/map/region_pager_lua.cpp | 20 ++--- server/linit.cpp | 14 ++-- server/mapgen/generator_api.cpp | 14 ++-- server/mapgen/generator_api.hpp | 5 +- server/rooms/room_api.cpp | 22 +++-- server/rooms/room_api.hpp | 4 +- server/rooms/room_mgr_api.cpp | 12 +-- server/rooms/room_mgr_api.hpp | 4 +- 12 files changed, 98 insertions(+), 133 deletions(-) rename common/map/{pager_api.cpp => region_pager_api.cpp} (59%) rename common/map/{pager_api.hpp => region_pager_api.hpp} (91%) diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index b30f04f..98f5ee1 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -64,43 +64,43 @@ static int getDepth(lua_State* L) { return 1; } -static int load(lua_State* L) { - //TODO: fill this +static int onLoad(lua_State* L) { + //TODO: onLoad() lua_pushboolean(L, false); return 1; } -static int save(lua_State* L) { - //TODO: fill this +static int onSave(lua_State* L) { + //TODO: onSave() return 0; } -static int create(lua_State* L) { - //TODO: fill this +static int onCreate(lua_State* L) { + //TODO: onCreate() return 0; } -static int unload(lua_State* L) { - //TODO: fill this +static int onUnload(lua_State* L) { + //TODO: onUnload() return 0; } -static const luaL_Reg regionlib[] = { - {"settile",setTile}, - {"gettile",getTile}, - {"getx",getX}, - {"gety",getY}, - {"getwidth",getWidth}, - {"getheight",getHeight}, - {"getdepth",getDepth}, - {"load",load}, - {"save",save}, - {"create",create}, - {"unload",unload}, +static const luaL_Reg regionLib[] = { + {"SetTile",setTile}, + {"SetTile",getTile}, + {"GetX",getX}, + {"GetY",getY}, + {"GetWidth",getWidth}, + {"GetHeight",getHeight}, + {"GetDepth",getDepth}, + {"OnLoad",onLoad}, + {"OnSave",onSave}, + {"OnCreate",onCreate}, + {"OnUnload",onUnload}, {nullptr, nullptr} }; -LUAMOD_API int luaopen_regionapi(lua_State* L) { - luaL_newlib(L, regionlib); +LUAMOD_API int openRegionAPI(lua_State* L) { + luaL_newlib(L, regionLib); return 1; } \ No newline at end of file diff --git a/common/map/region_api.hpp b/common/map/region_api.hpp index 310074d..6e6733f 100644 --- a/common/map/region_api.hpp +++ b/common/map/region_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_REGIONLIBNAME "region" -LUAMOD_API int luaopen_regionapi(lua_State* L); +#define TORTUGA_REGION_NAME "Region" +LUAMOD_API int openRegionAPI(lua_State* L); #endif diff --git a/common/map/pager_api.cpp b/common/map/region_pager_api.cpp similarity index 59% rename from common/map/pager_api.cpp rename to common/map/region_pager_api.cpp index 8f60e61..f38d1e2 100644 --- a/common/map/pager_api.cpp +++ b/common/map/region_pager_api.cpp @@ -19,7 +19,7 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "pager_api.hpp" +#include "region_pager_api.hpp" #include "region_pager_lua.hpp" #include "region.hpp" @@ -27,6 +27,8 @@ #include #include +//DOCS: These functions are just wrappers for the RegionPagerLua class + static int setTile(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); @@ -63,94 +65,46 @@ static int getDirectory(lua_State* L) { } static int loadRegion(lua_State* L) { - //get the parameters RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - std::string s = pager->GetDirectory(); - - //push the parameters - lua_getglobal(L, "region"); - lua_getfield(L, -1, "load"); + Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); lua_pushlightuserdata(L, region); - lua_pushstring(L, s.c_str()); - - //call the method - if (lua_pcall(L, 2, 1, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); - } return 1; } static int saveRegion(lua_State* L) { - //get the parameters RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - std::string s = pager->GetDirectory(); - - //push the parameters - lua_getglobal(L, "region"); - lua_getfield(L, -1, "save"); + Region* region = pager->SaveRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); lua_pushlightuserdata(L, region); - lua_pushstring(L, s.c_str()); - - //call the method - if (lua_pcall(L, 2, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); - } - return 0; + return 1; } static int createRegion(lua_State* L) { - //get the parameters RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - - //push the parameters - lua_getglobal(L, "region"); - lua_getfield(L, -1, "create"); + Region* region = pager->CreateRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); lua_pushlightuserdata(L, region); - //TODO: parameters - - //call the method - if (lua_pcall(L, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); - } - return 0; + return 1; } static int unloadRegion(lua_State* L) { - //get the parameters RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - std::string s = pager->GetDirectory(); - - //push the parameters - lua_getglobal(L, "region"); - lua_getfield(L, -1, "unload"); - lua_pushlightuserdata(L, region); - lua_pushstring(L, s.c_str()); - - //call the method - if (lua_pcall(L, 2, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); - } + pager->UnloadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); return 0; } static const luaL_Reg pagerlib[] = { - {"settile", setTile}, - {"gettile", getTile}, - {"getregion", getRegion}, - {"setdirectory", setDirectory}, - {"getdirectory", getDirectory}, - {"loadregion", loadRegion}, - {"saveregion", saveRegion}, - {"createregion", createRegion}, - {"unloadregion", unloadRegion}, + {"SetTile", setTile}, + {"GetTile", getTile}, + {"GetRegion", getRegion}, + {"SetDirectory", setDirectory}, + {"GetDirectory", getDirectory}, + {"LoadRegion", loadRegion}, + {"SaveRegion", saveRegion}, + {"CreateRegion", createRegion}, + {"UnloadRegion", unloadRegion}, {nullptr, nullptr} }; -LUAMOD_API int luaopen_pagerapi(lua_State* L) { +LUAMOD_API int openRegionPagerAPI(lua_State* L) { luaL_newlib(L, pagerlib); return 1; } \ No newline at end of file diff --git a/common/map/pager_api.hpp b/common/map/region_pager_api.hpp similarity index 91% rename from common/map/pager_api.hpp rename to common/map/region_pager_api.hpp index 2531e94..bc40981 100644 --- a/common/map/pager_api.hpp +++ b/common/map/region_pager_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_PAGERLIBNAME "pager" -LUAMOD_API int luaopen_pagerapi(lua_State* L); +#define TORTUGA_REGION_PAGER_NAME "pager" +LUAMOD_API int openRegionPagerAPI(lua_State* L); #endif diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index a183be8..a402f27 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/region_pager_lua.cpp @@ -32,8 +32,8 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { regionList.emplace_front(x, y); //API hook - lua_getglobal(luaState, "region"); - lua_getfield(luaState, -1, "load"); + lua_getglobal(luaState, "Region"); + lua_getfield(luaState, -1, "OnLoad"); lua_pushlightuserdata(luaState, ®ionList.front()); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { @@ -54,8 +54,8 @@ Region* RegionPagerLua::SaveRegion(int x, int y) { Region* ptr = FindRegion(x, y); if (ptr) { //API hook - lua_getglobal(luaState, "region"); - lua_getfield(luaState, -1, "save"); + lua_getglobal(luaState, "Region"); + lua_getfield(luaState, -1, "OnSave"); lua_pushlightuserdata(luaState, ptr); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { @@ -75,8 +75,8 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { regionList.emplace_front(x, y); //API hook - lua_getglobal(luaState, "region"); - lua_getfield(luaState, -1, "create"); + lua_getglobal(luaState, "Region"); + lua_getfield(luaState, -1, "OnCreate"); lua_pushlightuserdata(luaState, ®ionList.front()); //TODO: parameters if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { @@ -87,13 +87,13 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { } void RegionPagerLua::UnloadRegion(int x, int y) { - lua_getglobal(luaState, "region"); + lua_getglobal(luaState, "Region"); regionList.remove_if([&](Region& region) -> bool { if (region.GetX() == x && region.GetY() == y) { //API hook - lua_getfield(luaState, -1, "unload"); + lua_getfield(luaState, -1, "OnUnload"); lua_pushlightuserdata(luaState, ®ion); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { @@ -109,11 +109,11 @@ void RegionPagerLua::UnloadRegion(int x, int y) { } void RegionPagerLua::UnloadAll() { - lua_getglobal(luaState, "region"); + lua_getglobal(luaState, "Region"); for (auto& it : regionList) { //API hook - lua_getfield(luaState, -1, "unload"); + lua_getfield(luaState, -1, "OnUnload"); lua_pushlightuserdata(luaState, &it); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { diff --git a/server/linit.cpp b/server/linit.cpp index 7898184..8108a49 100644 --- a/server/linit.cpp +++ b/server/linit.cpp @@ -37,7 +37,7 @@ #include "lua/lua.hpp" #include "region_api.hpp" -#include "pager_api.hpp" +#include "region_pager_api.hpp" #include "room_api.hpp" #include "room_mgr_api.hpp" #include "generator_api.hpp" @@ -56,12 +56,12 @@ static const luaL_Reg loadedlibs[] = { {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, - //custom libs - {LUA_REGIONLIBNAME, luaopen_regionapi}, - {LUA_PAGERLIBNAME, luaopen_pagerapi}, - {LUA_ROOMLIBNAME, luaopen_roomapi}, - {LUA_ROOMMGRLIBNAME, luaopen_roommgrapi}, - {LUA_GENERATORLIBNAME, luaopen_generatorapi}, + //Tortuga's API + {TORTUGA_REGION_NAME, openRegionAPI}, + {TORTUGA_REGION_PAGER_NAME, openRegionPagerAPI}, + {TORTUGA_ROOM_NAME, openRoomAPI}, + {TORTUGA_ROOM_MGR_NAME, openRoomMgrAPI}, + {TORTUGA_GENRATOR_NAME, openGeneratorAPI}, {NULL, NULL} }; diff --git a/server/mapgen/generator_api.cpp b/server/mapgen/generator_api.cpp index c3b0eb9..f3cc84f 100644 --- a/server/mapgen/generator_api.cpp +++ b/server/mapgen/generator_api.cpp @@ -65,15 +65,15 @@ static int getMapHeight(lua_State* L) { return 1; } -static const luaL_Reg generatorlib[] = { - {"gettype", getMapType}, - {"getchunk", getChunk}, - {"getmapwidth", getMapWidth}, - {"getmapheight", getMapHeight}, +static const luaL_Reg generatorLib[] = { + {"GetMapType", getMapType}, + {"GetChunk", getChunk}, + {"GetMapWidth", getMapWidth}, + {"GetMapHeight", getMapHeight}, {nullptr, nullptr} }; -LUAMOD_API int luaopen_generatorapi(lua_State* L) { - luaL_newlib(L, generatorlib); +LUAMOD_API int openGeneratorAPI(lua_State* L) { + luaL_newlib(L, generatorLib); return 1; } diff --git a/server/mapgen/generator_api.hpp b/server/mapgen/generator_api.hpp index a0b9fb3..78c24c2 100644 --- a/server/mapgen/generator_api.hpp +++ b/server/mapgen/generator_api.hpp @@ -24,8 +24,7 @@ #include "lua/lua.hpp" -#define LUA_GENERATORLIBNAME "generator" -LUAMOD_API int luaopen_generatorapi(lua_State* L); - +#define TORTUGA_GENRATOR_NAME "Generator" +LUAMOD_API int openGeneratorAPI(lua_State* L); #endif \ No newline at end of file diff --git a/server/rooms/room_api.cpp b/server/rooms/room_api.cpp index 51c0101..3f217a8 100644 --- a/server/rooms/room_api.cpp +++ b/server/rooms/room_api.cpp @@ -35,15 +35,27 @@ static int getGenerator(lua_State* L) { return 1; } +static int onCreate(lua_State* L) { + //TODO: onCreate() + return 0; +} + +static int onUnload(lua_State* L) { + //TODO: onUnload() + return 0; +} + //TODO: parameters -static const luaL_Reg roomlib[] = { - {"getpager",getPager}, - {"getgenerator",getGenerator}, +static const luaL_Reg roomLib[] = { + {"GetPager",getPager}, + {"GetGenerator",getGenerator}, + {"OnCreate", onCreate}, + {"OnUnload", onUnload}, {nullptr, nullptr} }; -LUAMOD_API int luaopen_roomapi(lua_State* L) { - luaL_newlib(L, roomlib); +LUAMOD_API int openRoomAPI(lua_State* L) { + luaL_newlib(L, roomLib); return 1; } \ No newline at end of file diff --git a/server/rooms/room_api.hpp b/server/rooms/room_api.hpp index a9c31a8..5d2eab8 100644 --- a/server/rooms/room_api.hpp +++ b/server/rooms/room_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_ROOMLIBNAME "room" -LUAMOD_API int luaopen_roomapi(lua_State* L); +#define TORTUGA_ROOM_NAME "Room" +LUAMOD_API int openRoomAPI(lua_State* L); #endif diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index 0e58729..aae40df 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -73,14 +73,14 @@ static int unloadRoom(lua_State* L) { return 0; } -static const luaL_Reg roommgrlib[] = { - {"getroom",getRoom}, - {"createroom",createRoom}, - {"unloadroom",unloadRoom}, +static const luaL_Reg roomMgrLib[] = { + {"GetRoom",getRoom}, + {"CreateRoom",createRoom}, + {"UnloadRoom",unloadRoom}, {nullptr, nullptr} }; -LUAMOD_API int luaopen_roommgrapi(lua_State* L) { - luaL_newlib(L, roommgrlib); +LUAMOD_API int openRoomMgrAPI(lua_State* L) { + luaL_newlib(L, roomMgrLib); return 1; } \ No newline at end of file diff --git a/server/rooms/room_mgr_api.hpp b/server/rooms/room_mgr_api.hpp index 1382b4d..f870e5e 100644 --- a/server/rooms/room_mgr_api.hpp +++ b/server/rooms/room_mgr_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define LUA_ROOMMGRLIBNAME "roommgr" -LUAMOD_API int luaopen_roommgrapi(lua_State* L); +#define TORTUGA_ROOM_MGR_NAME "RoomMgr" +LUAMOD_API int openRoomMgrAPI(lua_State* L); #endif From 46ed196bf466e9c0b673ad7dfd0ee62d1200ebdb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 04:10:21 +1000 Subject: [PATCH 10/13] All current lua hooks are being called --- common/map/region_pager_lua.hpp | 1 + server/rooms/room_manager.cpp | 61 +++++++++++++++++++++++++++------ server/rooms/room_manager.hpp | 2 ++ server/server_application.cpp | 2 +- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/common/map/region_pager_lua.hpp b/common/map/region_pager_lua.hpp index 78aed17..9468a18 100644 --- a/common/map/region_pager_lua.hpp +++ b/common/map/region_pager_lua.hpp @@ -41,6 +41,7 @@ public: void UnloadAll() override; + //accessors & mutators std::string SetDirectory(std::string s) { return directory = s; } std::string GetDirectory() { return directory; } diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index cc0b330..53347be 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -38,12 +38,7 @@ int RoomManager::CreateRoom(MapType mapType) { //create the room RoomData* newRoom = new RoomData(); - //set the state - if (luaState) { - newRoom->pager.SetLuaState(luaState); - } - - //create the generator + //create the generator, use a lambda because I'm lazy newRoom->generator = [mapType]() -> BaseGenerator* { switch(mapType) { case MapType::NONE: //use overworld as a default @@ -56,17 +51,47 @@ int RoomManager::CreateRoom(MapType mapType) { throw(std::runtime_error("Failed to set the room's generator")); }(); + //set the state + if (luaState) { + newRoom->pager.SetLuaState(luaState); + newRoom->generator->SetLuaState(luaState); + } + + //register the room + roomMap[counter++] = newRoom; + + //TODO: pass the room's index to the lua hooks? + + //API hook + lua_getglobal(luaState, "Room"); + lua_getfield(luaState, -1, "OnCreate"); + lua_pushlightuserdata(luaState, newRoom); + if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + lua_pop(luaState, 1); + //finish the routine - roomMap[counter] = newRoom; - return counter++; + return counter; } int RoomManager::UnloadRoom(int uid) { + //find the room RoomData* room = FindRoom(uid); if (!room) { return -1; } + //API hook + lua_getglobal(luaState, "Room"); + lua_getfield(luaState, -1, "OnUnload"); + lua_pushlightuserdata(luaState, room); + if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + lua_pop(luaState, 1); + + //free the memory delete room->generator; delete room; roomMap.erase(uid); @@ -90,6 +115,22 @@ RoomData* RoomManager::FindRoom(int uid) { } int RoomManager::PushRoom(RoomData* room) { - roomMap[counter] = room; - return counter++; + roomMap[counter++] = room; + return counter; } + +void RoomManager::UnloadAll() { + lua_getglobal(luaState, "Room"); + + for (auto& it : roomMap) { + //API hook + lua_getfield(luaState, -1, "OnUnload"); + lua_pushlightuserdata(luaState, it.second); + if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + } + } + + lua_pop(luaState, 1); + roomMap.clear(); +} \ No newline at end of file diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 30129c1..5aad423 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -43,6 +43,8 @@ public: RoomData* FindRoom(int uid); int PushRoom(RoomData*); + void UnloadAll(); + //accessors and mutators std::map* GetContainer() { return &roomMap; } diff --git a/server/server_application.cpp b/server/server_application.cpp index c60da3b..d9d763e 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -157,7 +157,7 @@ void ServerApplication::Quit() { characterMgr.UnloadAll(); //TODO: unload combats //TODO: unload enemies - //TODO: unload rooms + roomMgr.UnloadAll(); //APIs lua_close(luaState); From 316db43b0acd5ff7e8f1b9966534690cb7a09744 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 04:54:01 +1000 Subject: [PATCH 11/13] Fixed CreateRoom()'s return type --- common/map/region_pager_api.hpp | 2 +- rsc/scripts/setup_server.lua | 19 ------------------- server/rooms/room_manager.cpp | 15 +++++---------- server/rooms/room_manager.hpp | 4 ++-- server/rooms/room_mgr_api.cpp | 7 ++++--- 5 files changed, 12 insertions(+), 35 deletions(-) diff --git a/common/map/region_pager_api.hpp b/common/map/region_pager_api.hpp index bc40981..466af61 100644 --- a/common/map/region_pager_api.hpp +++ b/common/map/region_pager_api.hpp @@ -24,7 +24,7 @@ #include "lua/lua.hpp" -#define TORTUGA_REGION_PAGER_NAME "pager" +#define TORTUGA_REGION_PAGER_NAME "RegionPager" LUAMOD_API int openRegionPagerAPI(lua_State* L); #endif diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index bae4a74..3511fa9 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,20 +1 @@ print("Lua script check (./rsc)") - -------------------------- ---Map API overrides -------------------------- - -function region.create(r) - for i = 1, region.getwidth() do - for j = 1, region.getheight() do - if math.abs(region.getx(r) + i -1) == math.abs(region.gety(r) + j -1) then - region.settile(r, i, j, 1, 50) - else - region.settile(r, i, j, 1, 14) - end - end - end - - --signal - region.settile(r, 4, 5, 2, 86) -end diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 53347be..46beb53 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -34,7 +34,7 @@ //public access methods //------------------------- -int RoomManager::CreateRoom(MapType mapType) { +RoomData* RoomManager::CreateRoom(MapType mapType) { //create the room RoomData* newRoom = new RoomData(); @@ -60,8 +60,6 @@ int RoomManager::CreateRoom(MapType mapType) { //register the room roomMap[counter++] = newRoom; - //TODO: pass the room's index to the lua hooks? - //API hook lua_getglobal(luaState, "Room"); lua_getfield(luaState, -1, "OnCreate"); @@ -72,14 +70,14 @@ int RoomManager::CreateRoom(MapType mapType) { lua_pop(luaState, 1); //finish the routine - return counter; + return newRoom; } -int RoomManager::UnloadRoom(int uid) { +void RoomManager::UnloadRoom(int uid) { //find the room RoomData* room = FindRoom(uid); if (!room) { - return -1; + return; } //API hook @@ -95,15 +93,12 @@ int RoomManager::UnloadRoom(int uid) { delete room->generator; delete room; roomMap.erase(uid); - - return 0; } RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - int newIndex = CreateRoom(MapType::NONE); - return FindRoom(newIndex); + return CreateRoom(MapType::NONE); } RoomData* RoomManager::FindRoom(int uid) { diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 5aad423..02c5b32 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -36,8 +36,8 @@ public: ~RoomManager() = default; //public access methods - int CreateRoom(MapType); - int UnloadRoom(int uid); + RoomData* CreateRoom(MapType); + void UnloadRoom(int uid); RoomData* GetRoom(int uid); RoomData* FindRoom(int uid); diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index aae40df..1d7825b 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -21,6 +21,7 @@ */ #include "room_mgr_api.hpp" +#include "room_api.hpp" #include "room_manager.hpp" #include "room_data.hpp" @@ -54,10 +55,10 @@ static int createRoom(lua_State* L) { }(); //create the room - int newIndex = roomMgr->CreateRoom(mapType); + RoomData* newRoom = roomMgr->CreateRoom(mapType); - //return the index - lua_pushinteger(L, newIndex); + //return the new room + lua_pushlightuserdata(L, newRoom); return 1; } From f5c58bf5ad525f498df9de102049ed281c8b9145 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 05:58:54 +1000 Subject: [PATCH 12/13] Minor tweaks after a failed attempt at metatables --- server/rooms/room_manager.cpp | 4 ++-- server/rooms/room_mgr_api.cpp | 2 ++ todo.txt | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 46beb53..637513e 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -41,7 +41,6 @@ RoomData* RoomManager::CreateRoom(MapType mapType) { //create the generator, use a lambda because I'm lazy newRoom->generator = [mapType]() -> BaseGenerator* { switch(mapType) { - case MapType::NONE: //use overworld as a default case MapType::OVERWORLD: return new OverworldGenerator(); case MapType::RUINS: return new RuinsGenerator(); case MapType::TOWERS: return new TowersGenerator(); @@ -98,7 +97,8 @@ void RoomManager::UnloadRoom(int uid) { RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - return CreateRoom(MapType::NONE); + //TODO: proper Get() method + return nullptr; } RoomData* RoomManager::FindRoom(int uid) { diff --git a/server/rooms/room_mgr_api.cpp b/server/rooms/room_mgr_api.cpp index 1d7825b..9907896 100644 --- a/server/rooms/room_mgr_api.cpp +++ b/server/rooms/room_mgr_api.cpp @@ -39,6 +39,8 @@ static int getRoom(lua_State* L) { } static int createRoom(lua_State* L) { + //TODO: check parameter count for the glue functions + //get the room manager lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); lua_gettable(L, LUA_REGISTRYINDEX); diff --git a/todo.txt b/todo.txt index 78b1506..f3394b6 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,7 @@ TODO: encapsulate the data structures TODO: Get the rooms working +TODO: make the whole thing more fault tolerant TODO: Authentication TODO: server is slaved to the client From 95362286f8d8e70f936eeb5fd28fc5f864f69afa Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 23 Jun 2014 10:29:39 +1000 Subject: [PATCH 13/13] Committing the island generator script, and a bugfix region_api.cpp had a bug, where a glue function's name was used twice. It was an easy catch, but there was an issue in the new script, where I was counting from 0 instead of 1. As a result, I was chasing a segfault for 5 hours. --- common/map/region_api.cpp | 2 +- common/map/region_pager_lua.cpp | 13 ++++++----- makefile | 1 + rsc/scripts/setup_server.lua | 41 +++++++++++++++++++++++++++++++++ server/rooms/room_manager.cpp | 5 ++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 98f5ee1..fdff4f7 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -87,7 +87,7 @@ static int onUnload(lua_State* L) { static const luaL_Reg regionLib[] = { {"SetTile",setTile}, - {"SetTile",getTile}, + {"GetTile",getTile}, {"GetX",getX}, {"GetY",getY}, {"GetWidth",getWidth}, diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index a402f27..85ea167 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/region_pager_lua.cpp @@ -29,12 +29,12 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { //load the region if possible //something to work on - regionList.emplace_front(x, y); + Region tmpRegion(x, y); //API hook lua_getglobal(luaState, "Region"); lua_getfield(luaState, -1, "OnLoad"); - lua_pushlightuserdata(luaState, ®ionList.front()); + lua_pushlightuserdata(luaState, &tmpRegion); lua_pushstring(luaState, directory.c_str()); if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); @@ -42,10 +42,10 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { //success or failure if (!lua_toboolean(luaState, -1)) { lua_pop(luaState, 2); - regionList.pop_front(); return nullptr; } lua_pop(luaState, 2); + regionList.push_front(tmpRegion); return ®ionList.front(); } @@ -72,18 +72,19 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { } //something to work on - regionList.emplace_front(x, y); + Region tmpRegion(x, y); //API hook lua_getglobal(luaState, "Region"); lua_getfield(luaState, -1, "OnCreate"); - lua_pushlightuserdata(luaState, ®ionList.front()); + lua_pushlightuserdata(luaState, &tmpRegion); //TODO: parameters if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); } lua_pop(luaState, 1); - return ®ionList.front();; + regionList.push_front(tmpRegion); + return ®ionList.front(); } void RegionPagerLua::UnloadRegion(int x, int y) { diff --git a/makefile b/makefile index 080a22d..56e39a3 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,7 @@ #MKDIR=mkdir #RM=del /y +#CXXFLAGS+=-static-libgcc -static-libstdc++ -g -fno-inline-functions -Wall CXXFLAGS+=-static-libgcc -static-libstdc++ export diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 3511fa9..6bc65f0 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1 +1,42 @@ print("Lua script check (./rsc)") + +--uber lazy declarations +function square(x) return x*x end +function distance(x, y, i, j) return math.sqrt(square(x - i) + square(y - j)) end + +--tile macros, mapped to the tilesheet +local base = 14 +local shift = 36 +plains = base + shift * 0 +grass = base + shift * 1 +dirt = base + shift * 2 +sand = base + shift * 3 +water = base + shift * 4 + +--Overwrite the original OnCreate with my own version +Region.hcOnCreate = Region.OnCreate +Region.OnCreate = function(region) + local ret = Region.hcOnCreate(region) --best practices + for i = 1, Region.GetWidth() do + for j = 1, Region.GetHeight() do + if distance(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1) > 10 then + Region.SetTile(region, i, j, 1, water) + else + Region.SetTile(region, i, j, 1, plains) + end + end + end + return ret +end + +--Get some regions +newRoom = RoomMgr.CreateRoom("overworld") +pager = Room.GetPager(newRoom) +regionTable = { + RegionPager.GetRegion(pager, 0, 0), + RegionPager.GetRegion(pager, 0, -20), + RegionPager.GetRegion(pager, -20, 0), + RegionPager.GetRegion(pager, -20, -20) +} + +print("Finished the lua script") \ No newline at end of file diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 637513e..3e4ec87 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -41,6 +41,8 @@ RoomData* RoomManager::CreateRoom(MapType mapType) { //create the generator, use a lambda because I'm lazy newRoom->generator = [mapType]() -> BaseGenerator* { switch(mapType) { + //BUG: Not having a map type results in an overworld generator by default + case MapType::NONE: case MapType::OVERWORLD: return new OverworldGenerator(); case MapType::RUINS: return new RuinsGenerator(); case MapType::TOWERS: return new TowersGenerator(); @@ -97,8 +99,7 @@ void RoomManager::UnloadRoom(int uid) { RoomData* RoomManager::GetRoom(int uid) { RoomData* ptr = FindRoom(uid); if (ptr) return ptr; - //TODO: proper Get() method - return nullptr; + return CreateRoom(MapType::NONE); } RoomData* RoomManager::FindRoom(int uid) {