diff --git a/client/character.hpp b/client/character.hpp index ca2d9a4..ba008b1 100644 --- a/client/character.hpp +++ b/client/character.hpp @@ -25,7 +25,7 @@ //components #include "character_defines.hpp" #include "vector2.hpp" -#include "statistics.hpp" +#include "bounding_box.hpp" //graphics #include "sprite_sheet.hpp" @@ -46,9 +46,6 @@ public: void CorrectSprite(); SpriteSheet* GetSprite() { return &sprite; } - //gameplay - Statistics* GetStats() { return &stats; } - //accessors and mutators //metadata @@ -64,18 +61,13 @@ public: Vector2 GetOrigin() const { return origin; } Vector2 SetMotion(Vector2 v) { return motion = v; } Vector2 GetMotion() const { return motion; } - Vector2 SetBounds(Vector2 v) { return bounds = v; } - Vector2 GetBounds() const { return bounds; } + BoundingBox SetBoundingBox(BoundingBox b) { return bounds = b; } + BoundingBox GetBoundingBox() const { return bounds; } private: //graphics SpriteSheet sprite; - //base statistics - Statistics stats; - - //TODO: gameplay components: equipment, items, buffs, debuffs - //metadata int owner; std::string handle; @@ -84,7 +76,7 @@ private: //position Vector2 origin = {0.0,0.0}; Vector2 motion = {0.0,0.0}; - Vector2 bounds = {CHARACTER_BOUNDS_WIDTH,CHARACTER_BOUNDS_HEIGHT}; + BoundingBox bounds; }; //tmp diff --git a/client/client_application.cpp b/client/client_application.cpp index 991cfd6..5f3934d 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -47,9 +47,6 @@ void ClientApplication::Init(int argc, char** argv) { std::cout << "Beginning " << argv[0] << std::endl; - //load the prerequisites - config.Load("rsc\\config.cfg"); - //------------------------- //Initialize the APIs //------------------------- @@ -67,10 +64,38 @@ void ClientApplication::Init(int argc, char** argv) { network.Open(0); std::cout << "Initialized SDL_net" << std::endl; + //initialize lua + lua = luaL_newstate(); + if (!lua) { + throw(std::runtime_error("Failed to initialize lua")); + } + luaL_openlibs(lua); + std::cout << "Initialized lua" << std::endl; + + if (luaL_dofile(lua, "rsc\\setup.lua")) { + throw(std::runtime_error("Failed to initialize lua's startup script")); + } + std::cout << "Initialized lua's setup script" << std::endl; + //------------------------- //Setup the screen //------------------------- + lua_getglobal(lua, "config"); + lua_getfield(lua, 1, "screen"); + lua_getfield(lua, 2, "width"); + lua_getfield(lua, 2, "height"); + lua_getfield(lua, 2, "fullscreen"); + + int w = lua_tointeger(lua, 3); + int h = lua_tointeger(lua, 4); + int f = lua_toboolean(lua, 5); + + lua_pop(lua, 5); + + BaseScene::SetScreen(w ? w : 800, h ? h : 600, 0, f ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF); + std::cout << "Initialized the screen" << std::endl; + int w = config.Int("client.screen.w"); int h = config.Int("client.screen.h"); int f = config.Bool("client.screen.f") ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF; @@ -149,6 +174,7 @@ void ClientApplication::Proc() { void ClientApplication::Quit() { std::cout << "Shutting down" << std::endl; + lua_close(lua); network.Close(); SDLNet_Quit(); SDL_Quit(); diff --git a/client/client_application.hpp b/client/client_application.hpp index 2efb742..275667f 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -25,10 +25,11 @@ #include "scene_list.hpp" #include "base_scene.hpp" -#include "config_utility.hpp" #include "udp_network_utility.hpp" #include "character.hpp" +#include "lua/lua.hpp" + #include class ClientApplication { @@ -48,7 +49,7 @@ private: BaseScene* activeScene = nullptr; //shared parameters - ConfigUtility config; + lua_State* lua = nullptr; UDPNetworkUtility network; int clientIndex = -1; int accountIndex = -1; diff --git a/client/linit.cpp b/client/linit.cpp new file mode 100644 index 0000000..eef8367 --- /dev/null +++ b/client/linit.cpp @@ -0,0 +1,85 @@ +/* $Id: linit.c,v 1.32, modified + * Initialization of libraries for lua.c and other clients + * See Copyright Notice in lua.h + * + * If you embed Lua in your program and need to open the standard + * libraries, call luaL_openlibs in your program. If you need a + * different set of libraries, copy this file to your project and edit + * it to suit your needs. + * + * Modified for use in Tortuga, renamed to linit.cpp + * Modifications are released under the zlib license: + * + * 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. +*/ +#define linit_c +#define LUA_LIB + +#include "lua/lua.hpp" + +#include "region_api.hpp" +#include "region_pager_api.hpp" +#include "tile_sheet_api.hpp" + +//these libs are loaded by lua.c and are readily available to any Lua program +static const luaL_Reg loadedlibs[] = { + //Standard libs + {"_G", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_COLIBNAME, luaopen_coroutine}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_BITLIBNAME, luaopen_bit32}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + + //Tortuga's API + {TORTUGA_REGION_NAME, openRegionAPI}, + {TORTUGA_REGION_PAGER_NAME, openRegionPagerAPI}, + {TORTUGA_TILE_SHEET_NAME, openTileSheetAPI}, + + {NULL, NULL} +}; + + +//these libs are preloaded and must be required before used +static const luaL_Reg preloadedlibs[] = { + {NULL, NULL} +}; + +LUALIB_API void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib; + //call open functions from 'loadedlibs' and set results to global table + for (lib = loadedlibs; lib->func; lib++) { + luaL_requiref(L, lib->name, lib->func, 1); + lua_pop(L, 1); //remove lib + } + //add open functions from 'preloadedlibs' into 'package.preload' table + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); + for (lib = preloadedlibs; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } + lua_pop(L, 1); //remove _PRELOAD table +} \ No newline at end of file diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 4f997a0..6498067 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -48,6 +48,60 @@ InWorld::InWorld( characterIndex(*argCharacterIndex), characterMap(*argCharacterMap) { + //register the pager + lua_pushstring(lua, TORTUGA_REGION_PAGER_PSEUDO_INDEX); + lua_pushlightuserdata(lua, &pager); + lua_settable(L, LUA_REGISTRYINDEX); + + //register the tilesheet + lua_pushstring(lua, TORTUGA_TILE_SHEET_PSEUDO_INDEX); + lua_pushlightuserdata(lua, &tileSheet); + lua_settable(L, LUA_REGISTRYINDEX); + + //setup the component objecrs + pager.SetLuaState(lua); + + //get the config info + lua_getglobal(lua, "config"); + lua_getfield(lua, -1, "dir"); + lua_getfield(lua, -1, "fonts"); + std::string fonts = lua_tostring(lua, -1); + lua_getfield(lua, -2, "interface"); + std::string interface = lua_tostring(lua, -1); + lua_getfield(lua, -3, "sprites"); + std::string sprites = lua_tostring(lua, -1); + lua_getfield(lua, -4, "scripts"); + std::string scripts = lua_tostring(lua, -1); + lua_pop(lua, 6); + + //run the additional scripts + if (luaL_dofile(lua, (scripts + "in_world.lua").c_str())) { + throw(std::runtime_error(std::string() + "Failed to run in_world.lua: " + lua_tostring(lua, -1) )); + } + + //setup the utility objects + buttonImage.LoadSurface(interface + "button_menu.bmp"); + buttonImage.SetClipH(buttonImage.GetClipH()/3); + font.LoadSurface(fonts + "pk_white_8.bmp"); + + //pass the utility objects + backButton.SetImage(&buttonImage); + backButton.SetFont(&font); + + //set the button positions + backButton.SetX(50); + backButton.SetY(50 + buttonImage.GetClipH() * 0); + + //set the button texts + backButton.SetText("Back"); + + //entities + character.GetSprite()->LoadSurface(sprites + "elliot2.bmp", 4, 4); + character.SetBoundingBox({0, 0, + character.GetSprite()->GetImage()->GetClipW(), + character.GetSprite()->GetImage()->GetClipH() + }); +//------------------------- //setup the utility objects buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); buttonImage.SetClipH(buttonImage.GetClipH()/3); @@ -81,7 +135,13 @@ InWorld::InWorld( } InWorld::~InWorld() { - // + //unregister the map components + lua_pushstring(lua, TORTUGA_REGION_PAGER_PSEUDO_INDEX); + lua_pushstring(lua, TORTUGA_TILE_SHEET_PSEUDO_INDEX); + lua_pushnil(lua); + lua_settable(lua, LUA_REGISTRYINDEX); + lua_pushnil(lua); + lua_settable(lua, LUA_REGISTRYINDEX); } //------------------------- @@ -106,6 +166,28 @@ void InWorld::Update(double delta) { } //TODO: Check collisions here + //check for collisions with the map + BoundingBox wallBounds = {0, 0, tileSheet.GetTileW(), tileSheet.GetTileH()}; + const int xCount = character.GetBoundingBox().w / wallBounds.w + 1; + const int yCount = character.GetBoundingBox().h / wallBounds.h + 1; + + for (int i = -1; i <= xCount; ++i) { + for (int j = -1; j <= yCount; ++j) { + //set the wall's position + wallBounds.x = wallBounds.w * i + snapToBase((double)wallBounds.w, character.GetOrigin().x); + wallBounds.y = wallBounds.h * j + snapToBase((double)wallBounds.h, character.GetOrigin().y); + + if (!pager.GetSolid(wallBounds.x / wallBounds.w, wallBounds.y / wallBounds.h)) { + continue; + } + + if ((character.GetOrigin() + character.GetBoundingBox()).CheckOverlap(wallBounds)) { + character.SetOrigin(character.GetOrigin() - (character.GetMotion() * delta)); + character.SetMotion({0,0}); + character.CorrectSprite(); + } + } + } //update the camera if(localCharacter) { diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 34ce942..eeae7b5 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -22,26 +22,28 @@ #ifndef INWORLD_HPP_ #define INWORLD_HPP_ -//maps -#include "region_pager_base.hpp" +//map stuff +#include "tile_sheet.hpp" +#include "region_pager_lua.hpp" //networking #include "udp_network_utility.hpp" -//graphics +//graphics & ui #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" -#include "tile_sheet.hpp" -//common -#include "config_utility.hpp" +//utilities #include "frame_rate.hpp" - -#include "character.hpp" +#include "timer.hpp" //client #include "base_scene.hpp" +#include "character.hpp" + +//APIs +#include "lua/lua.hpp" //STL #include @@ -93,6 +95,7 @@ protected: //utilities void UpdateMap(); + //TODO: Streamline this with lua //shared parameters ConfigUtility& config; UDPNetworkUtility& network; @@ -114,10 +117,11 @@ protected: Button shutDownButton; //TODO: Review the camera struct { - int x = 0, y = 0; - int width = 0, height = 0; - int marginX = 0, marginY = 0; - } camera; + struct { + int x, y; + }origin, margin; + }camera; + FrameRate fps; //game diff --git a/client/scenes/main_menu.cpp b/client/scenes/main_menu.cpp index 735e2fb..bfe63ec 100644 --- a/client/scenes/main_menu.cpp +++ b/client/scenes/main_menu.cpp @@ -25,13 +25,20 @@ //Public access members //------------------------- -MainMenu::MainMenu(ConfigUtility* const argConfig): - config(*argConfig) -{ +MainMenu::MainMenu(lua_State* L): lua(L) { + //get the config info + lua_getglobal(lua, "config"); + lua_getfield(lua, -1, "dir"); + lua_getfield(lua, -1, "interface"); + std::string interface = lua_tostring(lua, -1); + lua_getfield(lua, -2, "fonts"); + std::string fonts = lua_tostring(lua, -1); + lua_pop(lua, 4); + //setup the utility objects - image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); + image.LoadSurface(interface + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); - font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); + font.LoadSurface(fonts + "pk_white_8.bmp"); //pass the utility objects startButton.SetImage(&image); @@ -102,7 +109,7 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (startButton.MouseButtonUp(button) == Button::State::HOVER) { - SetNextScene(SceneList::LOBBYMENU); + SetNextScene(SceneList::INWORLD); } if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) { SetNextScene(SceneList::OPTIONSMENU); diff --git a/client/scenes/main_menu.hpp b/client/scenes/main_menu.hpp index 3816482..f163501 100644 --- a/client/scenes/main_menu.hpp +++ b/client/scenes/main_menu.hpp @@ -24,15 +24,16 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" +#include "lua/lua.hpp" + class MainMenu : public BaseScene { public: //Public access members - MainMenu(ConfigUtility* const); + MainMenu(lua_State* L); ~MainMenu(); protected: @@ -50,7 +51,7 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //shared parameters - ConfigUtility& config; + lua_State* lua = nullptr; //members Image image; diff --git a/client/scenes/options_menu.cpp b/client/scenes/options_menu.cpp index 19b3b9e..3178055 100644 --- a/client/scenes/options_menu.cpp +++ b/client/scenes/options_menu.cpp @@ -25,13 +25,20 @@ //Public access members //------------------------- -OptionsMenu::OptionsMenu(ConfigUtility* const argConfig): - config(*argConfig) -{ +OptionsMenu::OptionsMenu(lua_State* L): lua(L) { + //get the config info + lua_getglobal(lua, "config"); + lua_getfield(lua, -1, "dir"); + lua_getfield(lua, -1, "interface"); + std::string interface = lua_tostring(lua, -1); + lua_getfield(lua, -2, "fonts"); + std::string fonts = lua_tostring(lua, -1); + lua_pop(lua, 4); + //setup the utility objects - image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); + image.LoadSurface(interface + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); - font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); + font.LoadSurface(fonts + "pk_white_8.bmp"); //pass the utility objects backButton.SetImage(&image); diff --git a/client/scenes/options_menu.hpp b/client/scenes/options_menu.hpp index 37e4e64..aff1fc6 100644 --- a/client/scenes/options_menu.hpp +++ b/client/scenes/options_menu.hpp @@ -24,16 +24,17 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" +#include "lua/lua.hpp" + //TODO: The options screen needs to be USED class OptionsMenu : public BaseScene { public: //Public access members - OptionsMenu(ConfigUtility* const); + OptionsMenu(lua_State* L); ~OptionsMenu(); protected: @@ -51,7 +52,7 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //shared parameters - ConfigUtility& config; + lua_State* lua = nullptr; //members Image image; diff --git a/client/scenes/splash_screen.cpp b/client/scenes/splash_screen.cpp index a363a2c..d3e15a5 100644 --- a/client/scenes/splash_screen.cpp +++ b/client/scenes/splash_screen.cpp @@ -21,14 +21,21 @@ */ #include "splash_screen.hpp" +#include + //------------------------- //Public access members //------------------------- -SplashScreen::SplashScreen(ConfigUtility* const argConfig): - config(*argConfig) -{ - logo.LoadSurface(config["dir.logos"] + "krstudios.bmp"); +SplashScreen::SplashScreen(lua_State* L): lua(L) { + //get the config info + lua_getglobal(lua, "config"); + lua_getfield(lua, -1, "dir"); + lua_getfield(lua, -1, "logos"); + std::string logos = lua_tostring(lua, -1); + lua_pop(lua, 3); + + logo.LoadSurface(logos + "krstudios.bmp"); startTick = std::chrono::steady_clock::now(); } diff --git a/client/scenes/splash_screen.hpp b/client/scenes/splash_screen.hpp index 2b2000b..8c92014 100644 --- a/client/scenes/splash_screen.hpp +++ b/client/scenes/splash_screen.hpp @@ -24,15 +24,16 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" +#include "lua/lua.hpp" + #include class SplashScreen : public BaseScene { public: //Public access members - SplashScreen(ConfigUtility* const); + SplashScreen(lua_State* L); ~SplashScreen(); protected: @@ -41,7 +42,7 @@ protected: void Render(SDL_Surface* const); //shared parameters - ConfigUtility& config; + lua_State* lua = nullptr; //members std::chrono::steady_clock::time_point startTick; diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 014a873..2fd3dbb 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -78,28 +78,27 @@ static int getDepth(lua_State* L) { return 1; } -static int onLoad(lua_State* L) { - //TODO: onLoad() +static int load(lua_State* L) { + //EMPTY lua_pushboolean(L, false); return 1; } -static int onSave(lua_State* L) { - //TODO: onSave() +static int save(lua_State* L) { + //EMPTY return 0; } -static int onCreate(lua_State* L) { - //TODO: onCreate() +static int create(lua_State* L) { + //EMPTY return 0; } -static int onUnload(lua_State* L) { - //TODO: onUnload() +static int unload(lua_State* L) { + //EMPTY return 0; } -//TODO: wrappers for the collision map static const luaL_Reg regionLib[] = { {"SetTile",setTile}, {"GetTile",getTile}, @@ -110,10 +109,10 @@ static const luaL_Reg regionLib[] = { {"GetWidth",getWidth}, {"GetHeight",getHeight}, {"GetDepth",getDepth}, - {"OnLoad",onLoad}, - {"OnSave",onSave}, - {"OnCreate",onCreate}, - {"OnUnload",onUnload}, + {"Load",load}, + {"Save",save}, + {"Create",create}, + {"Unload",unload}, {nullptr, nullptr} }; diff --git a/common/map/region_pager_api.cpp b/common/map/region_pager_api.cpp index 40c4d76..cc8c3d1 100644 --- a/common/map/region_pager_api.cpp +++ b/common/map/region_pager_api.cpp @@ -24,10 +24,14 @@ #include "region_pager_lua.hpp" #include "region.hpp" -#include #include -//DOCS: These functions are just wrappers for the RegionPagerLua class +static int getRegionPager(lua_State* L) { + lua_pushstring(L, TORTUGA_REGION_PAGER_PSEUDO_INDEX); + lua_gettable(L, LUA_REGISTRYINDEX); +} + +//DOCS: These glue functions simply wrap RegionPagerLua's methods static int setTile(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); @@ -64,20 +68,6 @@ static int getRegion(lua_State* L) { return 1; } -static int setDirectory(lua_State* L) { - RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - std::string s = pager->SetDirectory(lua_tostring(L, 2)); - lua_pushstring(L, s.c_str()); - return 1; -} - -static int getDirectory(lua_State* L) { - RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); - std::string s = pager->GetDirectory(); - lua_pushstring(L, s.c_str()); - return 1; -} - static int loadRegion(lua_State* L) { RegionPagerLua* pager = reinterpret_cast(lua_touserdata(L, 1)); Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); @@ -105,14 +95,13 @@ static int unloadRegion(lua_State* L) { return 0; } -static const luaL_Reg pagerlib[] = { +static const luaL_Reg regionPagerLib[] = { + {"GetRegionPager", getRegionPager}, {"SetTile", setTile}, {"GetTile", getTile}, {"SetSolid", setSolid}, {"GetSolid", getSolid}, {"GetRegion", getRegion}, - {"SetDirectory", setDirectory}, - {"GetDirectory", getDirectory}, {"LoadRegion", loadRegion}, {"SaveRegion", saveRegion}, {"CreateRegion", createRegion}, @@ -121,6 +110,6 @@ static const luaL_Reg pagerlib[] = { }; LUAMOD_API int openRegionPagerAPI(lua_State* L) { - luaL_newlib(L, pagerlib); + luaL_newlib(L, regionPagerLib); return 1; } \ No newline at end of file diff --git a/common/map/region_pager_api.hpp b/common/map/region_pager_api.hpp index 466af61..56a7cec 100644 --- a/common/map/region_pager_api.hpp +++ b/common/map/region_pager_api.hpp @@ -19,11 +19,12 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef PAGERAPI_HPP_ -#define PAGERAPI_HPP_ +#ifndef REGIONPAGERAPI_HPP_ +#define REGIONPAGERAPI_HPP_ #include "lua/lua.hpp" +#define TORTUGA_REGION_PAGER_PSEUDO_INDEX "RegionPagerPseudoIndex" #define TORTUGA_REGION_PAGER_NAME "RegionPager" LUAMOD_API int openRegionPagerAPI(lua_State* L); diff --git a/common/map/region_pager_base.cpp b/common/map/region_pager_base.cpp index fbe633d..8c31df1 100644 --- a/common/map/region_pager_base.cpp +++ b/common/map/region_pager_base.cpp @@ -47,6 +47,9 @@ bool RegionPagerBase::GetSolid(int x, int y) { } Region* RegionPagerBase::GetRegion(int x, int y) { + x = snapToBase(REGION_WIDTH, x); + y = snapToBase(REGION_HEIGHT, y); + //get the region by various means Region* ptr = nullptr; ptr = FindRegion(x, y); diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp index 85ea167..368f950 100644 --- a/common/map/region_pager_lua.cpp +++ b/common/map/region_pager_lua.cpp @@ -32,19 +32,18 @@ Region* RegionPagerLua::LoadRegion(int x, int y) { Region tmpRegion(x, y); //API hook - lua_getglobal(luaState, "Region"); - lua_getfield(luaState, -1, "OnLoad"); - 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) )); + lua_getglobal(lua, "Region"); + lua_getfield(lua, -1, "Load"); + lua_pushlightuserdata(lua, &tmpRegion); + if (lua_pcall(lua, 1, 1, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } //success or failure - if (!lua_toboolean(luaState, -1)) { - lua_pop(luaState, 2); + if (!lua_toboolean(lua, -1)) { + lua_pop(lua, 2); return nullptr; } - lua_pop(luaState, 2); + lua_pop(lua, 2); regionList.push_front(tmpRegion); return ®ionList.front(); } @@ -54,14 +53,13 @@ Region* RegionPagerLua::SaveRegion(int x, int y) { Region* ptr = FindRegion(x, y); if (ptr) { //API hook - 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) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + lua_getglobal(lua, "Region"); + lua_getfield(lua, -1, "Save"); + lua_pushlightuserdata(lua, ptr); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } - lua_pop(luaState, 1); + lua_pop(lua, 1); } return ptr; } @@ -75,30 +73,29 @@ Region* RegionPagerLua::CreateRegion(int x, int y) { Region tmpRegion(x, y); //API hook - lua_getglobal(luaState, "Region"); - lua_getfield(luaState, -1, "OnCreate"); - lua_pushlightuserdata(luaState, &tmpRegion); + lua_getglobal(lua, "Region"); + lua_getfield(lua, -1, "Create"); + lua_pushlightuserdata(lua, &tmpRegion); //TODO: parameters - if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } - lua_pop(luaState, 1); + lua_pop(lua, 1); regionList.push_front(tmpRegion); return ®ionList.front(); } void RegionPagerLua::UnloadRegion(int x, int y) { - lua_getglobal(luaState, "Region"); + lua_getglobal(lua, "Region"); regionList.remove_if([&](Region& region) -> bool { if (region.GetX() == x && region.GetY() == y) { //API hook - lua_getfield(luaState, -1, "OnUnload"); - lua_pushlightuserdata(luaState, ®ion); - lua_pushstring(luaState, directory.c_str()); - if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + lua_getfield(lua, -1, "Unload"); + lua_pushlightuserdata(lua, ®ion); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } return true; @@ -106,22 +103,21 @@ void RegionPagerLua::UnloadRegion(int x, int y) { return false; }); - lua_pop(luaState, 1); + lua_pop(lua, 1); } void RegionPagerLua::UnloadAll() { - lua_getglobal(luaState, "Region"); + lua_getglobal(lua, "Region"); for (auto& it : regionList) { //API hook - lua_getfield(luaState, -1, "OnUnload"); - lua_pushlightuserdata(luaState, &it); - lua_pushstring(luaState, directory.c_str()); - if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { - throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); + lua_getfield(lua, -1, "Unload"); + lua_pushlightuserdata(lua, &it); + if (lua_pcall(lua, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); } } - lua_pop(luaState, 1); + lua_pop(lua, 1); regionList.clear(); } \ No newline at end of file diff --git a/common/map/region_pager_lua.hpp b/common/map/region_pager_lua.hpp index 9468a18..8543e2d 100644 --- a/common/map/region_pager_lua.hpp +++ b/common/map/region_pager_lua.hpp @@ -42,14 +42,10 @@ public: void UnloadAll() override; //accessors & mutators - std::string SetDirectory(std::string s) { return directory = s; } - std::string GetDirectory() { return directory; } - - lua_State* SetLuaState(lua_State* L) { return luaState = L; } - lua_State* GetLuaState() { return luaState; } + lua_State* SetLuaState(lua_State* L) { return lua = L; } + lua_State* GetLuaState() { return lua; } protected: - std::string directory; - lua_State* luaState = nullptr; + lua_State* lua = nullptr; }; #endif diff --git a/common/graphics/tile_sheet.cpp b/common/map/tile_sheet.cpp similarity index 73% rename from common/graphics/tile_sheet.cpp rename to common/map/tile_sheet.cpp index bb9a4cb..819d709 100644 --- a/common/graphics/tile_sheet.cpp +++ b/common/map/tile_sheet.cpp @@ -21,24 +21,24 @@ */ #include "tile_sheet.hpp" -void TileSheet::Load(std::string fname, int xc, int yc) { - XCount = xc; - YCount = yc; +void TileSheet::Load(std::string fname, int tileWidth, int tileHeight) { image.LoadSurface(fname); - image.SetClipW(image.GetClipW()/XCount); - image.SetClipH(image.GetClipH()/YCount); + image.SetClipW(tileWidth); + image.SetClipH(tileHeight); + xCount = image.GetSurface()->w / image.GetClipW(); + yCount = image.GetSurface()->h / image.GetClipH(); } void TileSheet::Unload() { image.FreeSurface(); - XCount = YCount = 0; + xCount = yCount = 0; } -void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) { +void TileSheet::DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) { //0 is invisible if (tile == 0) return; - image.SetClipX((tile-1) % XCount * image.GetClipW()); - image.SetClipY((tile-1) / XCount * image.GetClipH()); + image.SetClipX((tile-1) % xCount * image.GetClipW()); + image.SetClipY((tile-1) / xCount * image.GetClipH()); image.DrawTo(dest, x, y); } @@ -50,8 +50,8 @@ void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int tile = region->GetTile(i, j, k); //0 is invisible if (tile == 0) continue; - image.SetClipX((tile-1) % XCount * image.GetClipW()); - image.SetClipY((tile-1) / XCount * image.GetClipH()); + image.SetClipX((tile-1) % xCount * image.GetClipW()); + image.SetClipY((tile-1) / xCount * image.GetClipH()); image.DrawTo(dest, (region->GetX() + i) * image.GetClipW() - camX, (region->GetY() + j) * image.GetClipH() - camY); diff --git a/common/graphics/tile_sheet.hpp b/common/map/tile_sheet.hpp similarity index 82% rename from common/graphics/tile_sheet.hpp rename to common/map/tile_sheet.hpp index 368fcec..f9bf8ea 100644 --- a/common/graphics/tile_sheet.hpp +++ b/common/map/tile_sheet.hpp @@ -31,24 +31,24 @@ class TileSheet { public: TileSheet() = default; - TileSheet(std::string f, int x, int y) { Load(f, x, y); } + TileSheet(std::string f, int w, int h) { Load(f, w, h); } ~TileSheet() = default; - void Load(std::string fname, int XCount, int YCount); + void Load(std::string fname, int tileWidth, int tileHeight); void Unload(); - void DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile); + void DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile); void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY); //accessors Image* GetImage() { return ℑ } - int GetXCount() { return XCount; } - int GetYCount() { return YCount; } + int GetXCount() { return xCount; } + int GetYCount() { return yCount; } int GetTileW() { return image.GetClipW(); } int GetTileH() { return image.GetClipH(); } private: Image image; - int XCount = 0, YCount = 0; + int xCount = 0, yCount = 0; }; #endif diff --git a/common/map/tile_sheet_api.cpp b/common/map/tile_sheet_api.cpp new file mode 100644 index 0000000..496dd16 --- /dev/null +++ b/common/map/tile_sheet_api.cpp @@ -0,0 +1,82 @@ +/* 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 "tile_sheet_api.hpp" + +#include "tile_sheet.hpp" + +static int getTileSheet(lua_State* L) { + lua_pushstring(L, TORTUGA_TILE_SHEET_PSEUDO_INDEX); + lua_gettable(L, LUA_REGISTRYINDEX); + return 1; +} + +static int load(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + sheet->Load(lua_tostring(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); + return 0; +} + +static int unload(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + sheet->Unload(); + return 0; +} + +static int getXCount(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, sheet->GetXCount()); + return 1; +} + +static int getYCount(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, sheet->GetYCount()); + return 1; +} + +static int getTileW(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, sheet->GetTileW()); + return 1; +} + +static int getTileH(lua_State* L) { + TileSheet* sheet = reinterpret_cast(lua_touserdata(L, 1)); + lua_pushinteger(L, sheet->GetTileH()); + return 1; +} + +static const luaL_Reg tileSheetLib[] = { + {"GetTileSheet",getTileSheet}, + {"Load",load}, + {"Unload",unload}, + {"GetXCount",getXCount}, + {"GetYCount",getYCount}, + {"GetTileW",getTileW}, + {"GetTileH",getTileH}, + {nullptr, nullptr} +}; + +LUAMOD_API int openTileSheetAPI(lua_State* L) { + luaL_newlib(L, tileSheetLib); + return 1; +} \ No newline at end of file diff --git a/common/utilities/check_bounds.hpp b/common/map/tile_sheet_api.hpp similarity index 76% rename from common/utilities/check_bounds.hpp rename to common/map/tile_sheet_api.hpp index e02d6cd..0614e24 100644 --- a/common/utilities/check_bounds.hpp +++ b/common/map/tile_sheet_api.hpp @@ -19,12 +19,13 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef CHECKBOUNDS_HPP_ -#define CHECKBOUNDS_HPP_ +#ifndef TILESHEETAPI_HPP_ +#define TILESHEETAPI_HPP_ -#include "vector2.hpp" +#include "lua/lua.hpp" -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); +#define TORTUGA_TILE_SHEET_PSEUDO_INDEX "TileSheetPseudoIndex" +#define TORTUGA_TILE_SHEET_NAME "TileSheet" +LUAMOD_API int openTileSheetAPI(lua_State* L); #endif diff --git a/common/utilities/check_bounds.cpp b/common/utilities/check_bounds.cpp deleted file mode 100644 index b0d42f8..0000000 --- a/common/utilities/check_bounds.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* 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 "check_bounds.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point) { - return !( - point.x < origin.x || - point.y < origin.y || - point.x >= origin.x + bound.x || - point.y >= origin.y + bound.y - ); -} - -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo) { - return !( - originOne.x >= originTwo.x + boundTwo.x || - originOne.x + boundOne.x >= originTwo.x || - originOne.y >= originTwo.y + boundTwo.y || - originOne.y + boundOne.y >= originTwo.y - ); -} diff --git a/common/utilities/config_utility.cpp b/common/utilities/config_utility.cpp deleted file mode 100644 index 02970b1..0000000 --- a/common/utilities/config_utility.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013, 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 "config_utility.hpp" - -#include -#include -#include - -using namespace std; - -void ConfigUtility::Load(string fname) { - ifstream is(fname); - - if (!is.is_open()) { - throw(runtime_error("Failed to open config file")); - } - - string key, val; - - for (;;) { //forever - //eat whitespace - while(isspace(is.peek())) - is.ignore(); - - //end of file - if (is.eof()) - break; - - //skip comment lines - if (is.peek() == '#') { - while(is.peek() != '\n' && !is.eof()) { - is.ignore(); - } - continue; - } - - //read in the pair - getline(is, key,'='); - getline(is, val); - - //trim the strings at the start & end - while(key.size() && isspace(*key.begin())) key.erase(0, 1); - while(val.size() && isspace(*val.begin())) val.erase(0, 1); - - while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 1); - while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1); - - //allow empty/wiped values - if (key.size() == 0) { - continue; - } - - //save the pair - table[key] = val; - } - - is.close(); -} - -std::string& ConfigUtility::String(std::string s) { - return table[s]; -} - -int ConfigUtility::Integer(std::string s) { - std::map::iterator it = table.find(s); - if (it == table.end()) { - return 0; - } - return atoi(it->second.c_str()); -} - -double ConfigUtility::Double(std::string s) { - std::map::iterator it = table.find(s); - if (it == table.end()) { - return 0.0; - } - return atof(it->second.c_str()); -} - -bool ConfigUtility::Boolean(std::string s) { - std::map::iterator it = table.find(s); - if (it == table.end()) { - return false; - } - return it->second == "true"; -} diff --git a/common/utilities/config_utility.hpp b/common/utilities/config_utility.hpp deleted file mode 100644 index 6e944ef..0000000 --- a/common/utilities/config_utility.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013, 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 CONFIGUTILITY_HPP_ -#define CONFIGUTILITY_HPP_ - -#include -#include - -class ConfigUtility { -public: - ConfigUtility() = default; - ConfigUtility(std::string s) { Load(s); } - - void Load(std::string fname); - - //convert to a type - std::string& String(std::string); - int Integer(std::string); - double Double(std::string); - bool Boolean(std::string); - - //shorthand - std::string& operator[](std::string s) { - return String(s); - } - int Int(std::string s) { - return Integer(s); - } - bool Bool(std::string s) { - return Boolean(s); - } - - //OO breaker - std::map* GetMap() { - return &table; - } -private: - std::map table; -}; - -#endif diff --git a/rsc/config.cfg b/rsc/config.cfg deleted file mode 100644 index b2f34d9..0000000 --- a/rsc/config.cfg +++ /dev/null @@ -1,29 +0,0 @@ -#configuration of the programs - -#server specific settings -server.host = 255.255.255.255 -server.port = 21795 -server.name = local - -server.dbname = database.db - -#client specific settings -client.screen.f = false - -client.username = Kayne Ruse -client.handle = Ratstail91 -client.avatar = elliot2.bmp - -#directories -dir.fonts = rsc/graphics/fonts/ -dir.logos = rsc/graphics/logos/ -dir.sprites = rsc/graphics/sprites/ -dir.tilesets = rsc/graphics/tilesets/ -dir.interface = rsc/graphics/interface/ -dir.scripts = rsc/scripts/ -dir.maps = rsc/maps/ - -#map system -map.savename = servermap - -#debugging diff --git a/rsc/scripts/file_format.lua b/rsc/scripts/file_format.lua new file mode 100644 index 0000000..5cff60a --- /dev/null +++ b/rsc/scripts/file_format.lua @@ -0,0 +1,9 @@ +--overwriting the existing hard coded methods +function Region.Load(region) + --the return value signals if this succeeded or failed + return false +end + +function Region.Save(region) + -- +end diff --git a/rsc/scripts/generator.lua b/rsc/scripts/generator.lua new file mode 100644 index 0000000..f736ac1 --- /dev/null +++ b/rsc/scripts/generator.lua @@ -0,0 +1,16 @@ +--uber lazy declarations +function math.sqr(x) return x*x end +function math.dist(x, y, i, j) return math.sqrt(math.sqr(x-i) + math.sqr(y-j)) end + +--define these +function Region.Create(region) + for i = 1, Region.GetWidth() do + for j = 1, Region.GetHeight() do + Region.SetTile(region, i, j, 1, 1) --to show the basics are working + end + end +end + +function Region.Unload(region) + -- +end diff --git a/rsc/scripts/in_world.lua b/rsc/scripts/in_world.lua new file mode 100644 index 0000000..43350aa --- /dev/null +++ b/rsc/scripts/in_world.lua @@ -0,0 +1,33 @@ +local sheet = TileSheet.GetTileSheet() +local pager = RegionPager.GetRegionPager() + +--the selected tilesheet +TileSheet.Load(sheet, config.dir.tilesets .. "terrain.bmp", 32, 32) + +--tile macros, mapped to this tilesheet +local base = 14 +local shift = 36 +tiles = { + plains = base + shift * 0, + grass = base + shift * 1, + dirt = base + shift * 2, + sand = base + shift * 3, + water = base + shift * 4 +} + +--could set custom generation systems here, that differ from the global generators, etc. +function Region.Create(region) + for i = 1, Region.GetWidth() do + for j = 1, Region.GetHeight() do + local dist = math.dist(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1) + if dist < 10 then + Region.SetTile(region, i, j, 1, tiles.plains) + elseif dist < 12 then + Region.SetTile(region, i, j, 1, tiles.sand) + else + Region.SetTile(region, i, j, 1, tiles.water) + Region.SetSolid(region, i, j, true) + end + end + end +end diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 582cacf..ceb735e 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -32,14 +32,4 @@ Region.OnCreate = function(region) return ret end ---Get some regions -newRoom = RoomMgr.CreateRoom("overworld") -pager = Room.GetPager(newRoom) -regionTable = { - RegionPager.GetRegion(pager, Region.GetWidth() * 0, Region.GetHeight() * 0), - RegionPager.GetRegion(pager, Region.GetWidth() *-1, Region.GetHeight() * 0), - RegionPager.GetRegion(pager, Region.GetWidth() * 0, Region.GetHeight() *-1), - RegionPager.GetRegion(pager, Region.GetWidth() *-1, Region.GetHeight() *-1) -} - print("Finished the lua script") \ No newline at end of file diff --git a/rsc/setup.lua b/rsc/setup.lua new file mode 100644 index 0000000..5ccb482 --- /dev/null +++ b/rsc/setup.lua @@ -0,0 +1,55 @@ +--[[ + --reroute the program while in development + config = {debug = true} + dofile("../rsc/setup.lua") +--]] + +--catch the debug signal if the program was rerouted +local debug = false +if config ~= nil then + debug = config.debug +end + +--the program's configuration +config = { + --common stuff + debug = debug, + dir = { + fonts = "rsc/graphics/fonts/", + logos = "rsc/graphics/logos/", + sprites = "rsc/graphics/sprites/", + tilesets = "rsc/graphics/tilesets/", + interface = "rsc/graphics/interface/", + scripts = "rsc/scripts/", + maps = "rsc/maps/" + }, + + --client specific stuff + client = { + screen = { + width = 800, + height = 600, + fullscreen = false + }, + username = "Kayne", + handle = "Ratstail91", + avatar = "elliot2.bmp" + }, + + --server specific stuff + server = { + mapname = "mapsave", + host = "255.255.255.255", + port = 21795, + name = "local", + dbname = "database.db" + } +} + +--development environment +if config.debug then + for k, v in pairs(config.dir) do + config.dir[k] = string.format("../%s", v) + end +end +