Compare commits

...

5 Commits

Author SHA1 Message Date
Kayne Ruse f978cff98f lobby_server.cpp compiles 2014-07-31 15:58:56 +10:00
Kayne Ruse 35363c05c3 Updated a number of simpler client scenes (read more)
Updated the ClientApplication class to help support the new design, but
it's not finished because of that module's dependence on the various
scenes.

Updated the following scenes:

* CleanUp
* MainMenu
* OptionsMenu

The following source files (in client/scenes/) compile correctly:

* base_scene.cpp
* splash_screen.cpp
* clean_up.cpp
* main_menu.cpp
* options_menu.cpp

The folling source files (in client/scenes/) do not compile:

* in_world.cpp
* in_combat.cpp
* lobby_menu.cpp
2014-07-31 14:53:08 +10:00
Kayne Ruse a8eb67d619 Updated ClientApplication 2014-07-31 13:57:46 +10:00
Kayne Ruse 67c8bb6f11 rsc/ and common/ seem OK from the splice (read more)
common compiles correctly, although there are issues:

* There needs to be support for multiple pagers at once
* There needs to be a way to support multiple generation algorithms
2014-07-30 22:55:22 +10:00
Kayne Ruse 03f1723d88 Lazily committing the entire splice at once (read more)
(this message is copied from my blog)

So I took the lazy route and just committed the entire splice between the jam branch and the development branch all at once. It is exactly as terrible as you think it is, but I've been out of coding for 2 weeks, and I want to get some shit done. I'll pick through each piece one by one, by comparing the diffs from the develop branch to the current jam-merge branch.

Those developers I met on Monday said they were impressed by my ability to manage a large codebase, but if they saw this I'd expect they'd probably take that back.

And oh god it's been 3 weeks since I touched the main branch WTF?
2014-07-30 22:42:24 +10:00
34 changed files with 673 additions and 519 deletions
+1
View File
@@ -15,6 +15,7 @@ Out/
*.o *.o
*.a *.a
*.exe *.exe
*.diff
#Shell files #Shell files
*.bat *.bat
+4 -12
View File
@@ -25,7 +25,7 @@
//components //components
#include "character_defines.hpp" #include "character_defines.hpp"
#include "vector2.hpp" #include "vector2.hpp"
#include "statistics.hpp" #include "bounding_box.hpp"
//graphics //graphics
#include "sprite_sheet.hpp" #include "sprite_sheet.hpp"
@@ -46,9 +46,6 @@ public:
void CorrectSprite(); void CorrectSprite();
SpriteSheet* GetSprite() { return &sprite; } SpriteSheet* GetSprite() { return &sprite; }
//gameplay
Statistics* GetStats() { return &stats; }
//accessors and mutators //accessors and mutators
//metadata //metadata
@@ -64,18 +61,13 @@ public:
Vector2 GetOrigin() const { return origin; } Vector2 GetOrigin() const { return origin; }
Vector2 SetMotion(Vector2 v) { return motion = v; } Vector2 SetMotion(Vector2 v) { return motion = v; }
Vector2 GetMotion() const { return motion; } Vector2 GetMotion() const { return motion; }
Vector2 SetBounds(Vector2 v) { return bounds = v; } BoundingBox SetBoundingBox(BoundingBox b) { return bounds = b; }
Vector2 GetBounds() const { return bounds; } BoundingBox GetBoundingBox() const { return bounds; }
private: private:
//graphics //graphics
SpriteSheet sprite; SpriteSheet sprite;
//base statistics
Statistics stats;
//TODO: gameplay components: equipment, items, buffs, debuffs
//metadata //metadata
int owner; int owner;
std::string handle; std::string handle;
@@ -84,7 +76,7 @@ private:
//position //position
Vector2 origin = {0.0,0.0}; Vector2 origin = {0.0,0.0};
Vector2 motion = {0.0,0.0}; Vector2 motion = {0.0,0.0};
Vector2 bounds = {CHARACTER_BOUNDS_WIDTH,CHARACTER_BOUNDS_HEIGHT}; BoundingBox bounds;
}; };
//tmp //tmp
+52 -16
View File
@@ -47,9 +47,6 @@
void ClientApplication::Init(int argc, char** argv) { void ClientApplication::Init(int argc, char** argv) {
std::cout << "Beginning " << argv[0] << std::endl; std::cout << "Beginning " << argv[0] << std::endl;
//load the prerequisites
config.Load("rsc\\config.cfg");
//------------------------- //-------------------------
//Initialize the APIs //Initialize the APIs
//------------------------- //-------------------------
@@ -67,22 +64,53 @@ void ClientApplication::Init(int argc, char** argv) {
network.Open(0); network.Open(0);
std::cout << "Initialized SDL_net" << std::endl; 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;
//run the setup script
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;
//place the config table onto the stack
lua_getglobal(lua, "config");
//------------------------- //-------------------------
//Setup the screen //Setup the screen
//------------------------- //-------------------------
int w = config.Int("client.screen.w"); //get each field
int h = config.Int("client.screen.h"); lua_getfield(lua, -1, "client");
int f = config.Bool("client.screen.f") ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF; lua_getfield(lua, -1, "screen");
lua_getfield(lua, -1, "width");
lua_getfield(lua, -2, "height");
lua_getfield(lua, -3, "fullscreen");
BaseScene::SetScreen(w ? w : 800, h ? h : 600, 0, f); int w = lua_tointeger(lua, -3);
int h = lua_tointeger(lua, -2);
int f = lua_toboolean(lua, -1);
//pop the screen members
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; std::cout << "Initialized the screen" << std::endl;
//------------------------- //-------------------------
//debug output //debug output
//------------------------- //-------------------------
//TODO: enable/disable these with a switch lua_getfield(lua, -1, "debug");
if (lua_toboolean(lua, -1)) {
#define DEBUG_OUTPUT_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl; #define DEBUG_OUTPUT_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl;
std::cout << "Internal sizes:" << std::endl; std::cout << "Internal sizes:" << std::endl;
@@ -98,11 +126,18 @@ void ClientApplication::Init(int argc, char** argv) {
DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE); DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE);
#undef DEBUG_OUTPUT_VAR #undef DEBUG_OUTPUT_VAR
}
//pop the debug value
lua_pop(lua, 1);
//------------------------- //-------------------------
//finalize the startup //finalize the startup
//------------------------- //-------------------------
//pop the config table
lua_pop(lua, 1);
std::cout << "Startup completed successfully" << std::endl; std::cout << "Startup completed successfully" << std::endl;
//------------------------- //-------------------------
@@ -136,7 +171,7 @@ void ClientApplication::Proc() {
//simulate game time //simulate game time
while (simTime < realTime) { while (simTime < realTime) {
//call each user defined function //call each user defined function
activeScene->RunFrame(double(delta.count()) / std::chrono::duration<int, std::milli>::period::den); activeScene->RunFrame(constexpr(double(delta.count()) / std::chrono::duration<int, std::milli>::period::den));
simTime += delta; simTime += delta;
} }
@@ -149,6 +184,7 @@ void ClientApplication::Proc() {
void ClientApplication::Quit() { void ClientApplication::Quit() {
std::cout << "Shutting down" << std::endl; std::cout << "Shutting down" << std::endl;
lua_close(lua);
network.Close(); network.Close();
SDLNet_Quit(); SDLNet_Quit();
SDL_Quit(); SDL_Quit();
@@ -165,25 +201,25 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
//add scene creation calls here //add scene creation calls here
case SceneList::FIRST: case SceneList::FIRST:
case SceneList::SPLASHSCREEN: case SceneList::SPLASHSCREEN:
activeScene = new SplashScreen(&config); activeScene = new SplashScreen(lua);
break; break;
case SceneList::MAINMENU: case SceneList::MAINMENU:
activeScene = new MainMenu(&config); activeScene = new MainMenu(lua);
break; break;
case SceneList::OPTIONSMENU: case SceneList::OPTIONSMENU:
activeScene = new OptionsMenu(&config); activeScene = new OptionsMenu(lua);
break; break;
case SceneList::LOBBYMENU: case SceneList::LOBBYMENU:
activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex); activeScene = new LobbyMenu(lua, network, characterMap);
break; break;
case SceneList::INWORLD: case SceneList::INWORLD:
activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); activeScene = new InWorld(lua, network, characterMap);
break; break;
case SceneList::INCOMBAT: case SceneList::INCOMBAT:
activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); activeScene = new InCombat(lua, network, characterMap);
break; break;
case SceneList::CLEANUP: case SceneList::CLEANUP:
activeScene = new CleanUp(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); activeScene = new CleanUp(lua, network, characterMap);
break; break;
default: default:
throw(std::logic_error("Failed to recognize the scene index")); throw(std::logic_error("Failed to recognize the scene index"));
+3 -2
View File
@@ -25,10 +25,11 @@
#include "scene_list.hpp" #include "scene_list.hpp"
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "character.hpp" #include "character.hpp"
#include "lua/lua.hpp"
#include <map> #include <map>
class ClientApplication { class ClientApplication {
@@ -48,7 +49,7 @@ private:
BaseScene* activeScene = nullptr; BaseScene* activeScene = nullptr;
//shared parameters //shared parameters
ConfigUtility config; lua_State* lua = nullptr;
UDPNetworkUtility network; UDPNetworkUtility network;
int clientIndex = -1; int clientIndex = -1;
int accountIndex = -1; int accountIndex = -1;
+85
View File
@@ -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
}
+37 -23
View File
@@ -29,25 +29,43 @@
//Public access members //Public access members
//------------------------- //-------------------------
CleanUp::CleanUp( CleanUp::CleanUp(lua_State* L, UDPNetworkUtility& aNetwork, CharacterMap& aCharacterMap):
ConfigUtility* const argConfig, lua(L),
UDPNetworkUtility* const argNetwork, network(aNetwork),
int* const argClientIndex, characterMap(aCharacterMap)
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex),
characterIndex(*argCharacterIndex),
characterMap(*argCharacterMap)
{ {
//get the config table
lua_getglobal(lua, "config");
//TODO: I need to figure out an alternative to loading these over and over again
//get the directories
lua_getfield(lua, -1, "dir");
lua_getfield(lua, -1, "interface");
lua_getfield(lua, -2, "fonts");
std::string interfaceDir = lua_tostring(lua, -2);
std::string fontsDir = lua_tostring(lua, -1);
lua_pop(lua, 3);
//clear the indicies
lua_getfield(lua, -1, "client");
lua_pushnil(lua);
lua_pushnil(lua);
lua_pushnil(lua);
lua_setfield(lua, -4, "clientIndex");
lua_setfield(lua, -3, "accountIndex");
lua_setfield(lua, -2, "characterIndex");
//pop the remaining objects from the stack
lua_pop(lua, 2);
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(interfaceDir + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(fontsDir + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
backButton.SetImage(&image); backButton.SetImage(&image);
@@ -62,12 +80,7 @@ CleanUp::CleanUp(
//full reset //full reset
network.Unbind(Channels::SERVER); network.Unbind(Channels::SERVER);
clientIndex = -1;
accountIndex = -1;
characterIndex = -1;
// combatMap.clear();
characterMap.clear(); characterMap.clear();
// enemyMap.clear();
//auto return //auto return
startTick = std::chrono::steady_clock::now(); startTick = std::chrono::steady_clock::now();
@@ -83,7 +96,7 @@ CleanUp::~CleanUp() {
void CleanUp::Update(double delta) { void CleanUp::Update(double delta) {
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) { if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) {
QuitEvent(); SetNextScene(SceneList::MAINMENU);
} }
//BUGFIX: Eat incoming packets //BUGFIX: Eat incoming packets
@@ -119,7 +132,8 @@ void CleanUp::MouseButtonDown(SDL_MouseButtonEvent const& button) {
} }
void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) { void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (backButton.MouseButtonUp(button) == Button::State::HOVER) { if (backButton.MouseButtonUp(button) == Button::State::HOVER &&
button.button & SDL_BUTTON_LMASK) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
} }
+8 -21
View File
@@ -25,19 +25,18 @@
//network //network
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
//graphics //graphics & ui
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
//common
#include "config_utility.hpp"
#include "frame_rate.hpp" #include "frame_rate.hpp"
#include "character.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
#include "character.hpp"
//APIs
#include "lua/lua.hpp"
//std namespace //std namespace
#include <chrono> #include <chrono>
@@ -45,14 +44,7 @@
class CleanUp : public BaseScene { class CleanUp : public BaseScene {
public: public:
//Public access members //Public access members
CleanUp( CleanUp(lua_State*, UDPNetworkUtility&, CharacterMap&);
ConfigUtility* const argConfig,
UDPNetworkUtility* const argNetwork,
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
);
~CleanUp(); ~CleanUp();
protected: protected:
@@ -70,18 +62,13 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
UDPNetworkUtility& network; UDPNetworkUtility& network;
int& clientIndex;
int& accountIndex;
int& characterIndex;
CharacterMap& characterMap; CharacterMap& characterMap;
//graphics //graphics & ui
Image image; Image image;
RasterFont font; RasterFont font;
//UI
Button backButton; Button backButton;
FrameRate fps; FrameRate fps;
+83 -1
View File
@@ -48,6 +48,60 @@ InWorld::InWorld(
characterIndex(*argCharacterIndex), characterIndex(*argCharacterIndex),
characterMap(*argCharacterMap) 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 //setup the utility objects
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
buttonImage.SetClipH(buttonImage.GetClipH()/3); buttonImage.SetClipH(buttonImage.GetClipH()/3);
@@ -81,7 +135,13 @@ InWorld::InWorld(
} }
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 //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 //update the camera
if(localCharacter) { if(localCharacter) {
+15 -11
View File
@@ -22,26 +22,28 @@
#ifndef INWORLD_HPP_ #ifndef INWORLD_HPP_
#define INWORLD_HPP_ #define INWORLD_HPP_
//maps //map stuff
#include "region_pager_base.hpp" #include "tile_sheet.hpp"
#include "region_pager_lua.hpp"
//networking //networking
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
//graphics //graphics & ui
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
#include "tile_sheet.hpp"
//common //utilities
#include "config_utility.hpp"
#include "frame_rate.hpp" #include "frame_rate.hpp"
#include "timer.hpp"
#include "character.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
#include "character.hpp"
//APIs
#include "lua/lua.hpp"
//STL //STL
#include <map> #include <map>
@@ -93,6 +95,7 @@ protected:
//utilities //utilities
void UpdateMap(); void UpdateMap();
//TODO: Streamline this with lua
//shared parameters //shared parameters
ConfigUtility& config; ConfigUtility& config;
UDPNetworkUtility& network; UDPNetworkUtility& network;
@@ -114,10 +117,11 @@ protected:
Button shutDownButton; Button shutDownButton;
//TODO: Review the camera //TODO: Review the camera
struct { struct {
int x = 0, y = 0; struct {
int width = 0, height = 0; int x, y;
int marginX = 0, marginY = 0; }origin, margin;
}camera; }camera;
FrameRate fps; FrameRate fps;
//game //game
+57 -23
View File
@@ -30,21 +30,25 @@
//Public access members //Public access members
//------------------------- //-------------------------
LobbyMenu::LobbyMenu( LobbyMenu::LobbyMenu(lua_State* L, UDPNetworkUtility& aNetwork):
ConfigUtility* const argConfig, lua(L),
UDPNetworkUtility* const argNetwork, network(aNetwork)
int* const argClientIndex,
int* const argAccountIndex
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex)
{ {
//get the config info
lua_getglobal(lua, "config");
lua_getfield(lua, -1, "dir");
lua_getfield(lua, -1, "interface");
lua_getfield(lua, -2, "fonts");
std::string interfaceDir = lua_tostring(lua, -2);
std::string fontsDir = lua_tostring(lua, -1);
lua_pop(lua, 4);
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(interfaceDir + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(fontsDir + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
search.SetImage(&image); search.SetImage(&image);
@@ -88,11 +92,11 @@ void LobbyMenu::FrameStart() {
void LobbyMenu::Update(double delta) { void LobbyMenu::Update(double delta) {
//suck in and process all waiting packets //suck in and process all waiting packets
SerialPacket* packetBuffer = static_cast<SerialPacket*>(malloc(MAX_PACKET_SIZE)); SerialPacket* packetBuffer = new SerialPacket();
while(network.Receive(packetBuffer)) { while(network.Receive(packetBuffer)) {
HandlePacket(packetBuffer); HandlePacket(packetBuffer);
} }
free(static_cast<void*>(packetBuffer)); delete packetBuffer;
} }
void LobbyMenu::FrameEnd() { void LobbyMenu::FrameEnd() {
@@ -148,26 +152,44 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
} }
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//prep
lua_getglobal(lua, "config");
if (search.MouseButtonUp(button) == Button::State::HOVER) { if (search.MouseButtonUp(button) == Button::State::HOVER) {
//get the set parameters
lua_getfield(lua, -1, "server");
lua_getfield(lua, -1, "host");
lua_getfield(lua, -2, "port");
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
SerialPacket packet; SerialPacket packet;
packet.type = SerialPacketType::BROADCAST_REQUEST; packet.type = SerialPacketType::BROADCAST_REQUEST;
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); network.SendTo(lua_tostring(lua, -2), lua_tointeger(lua, -1), &packet);
//reset the server list //reset the server list
serverInfo.clear(); serverInfo.clear();
selection = nullptr; selection = nullptr;
//clear the parameters
lua_pop(lua, 3);
} }
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
//get the parameters
lua_getfield(lua, -1, "client");
lua_getfield(lua, -1, "username");
//pack the packet //pack the packet
ClientPacket packet; ClientPacket packet;
packet.type = SerialPacketType::JOIN_REQUEST; packet.type = SerialPacketType::JOIN_REQUEST;
strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); strncpy(packet.username, lua_tostring(lua, -1), PACKET_STRING_SIZE);
//join the selected server //join the selected server
network.SendTo(&selection->address, &packet); network.SendTo(&selection->address, &packet);
selection = nullptr; selection = nullptr;
//clear the parameters
lua_pop(lua, 2);
} }
else if (back.MouseButtonUp(button) == Button::State::HOVER) { else if (back.MouseButtonUp(button) == Button::State::HOVER) {
@@ -187,6 +209,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
else { else {
selection = nullptr; selection = nullptr;
} }
//clear the parameters
lua_pop(lua, 1);
} }
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
@@ -204,10 +229,10 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) { void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->type) { switch(argPacket->type) {
case SerialPacketType::BROADCAST_RESPONSE: case SerialPacketType::BROADCAST_RESPONSE:
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket)); HandleBroadcastResponse(dynamic_cast<ServerPacket*>(argPacket));
break; break;
case SerialPacketType::JOIN_RESPONSE: case SerialPacketType::JOIN_RESPONSE:
HandleJoinResponse(static_cast<ClientPacket*>(argPacket)); HandleJoinResponse(dynamic_cast<ClientPacket*>(argPacket));
break; break;
//handle errors //handle errors
default: default:
@@ -232,16 +257,25 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
} }
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
clientIndex = argPacket->clientIndex; lua_getglobal(lua, "config");
accountIndex = argPacket->accountIndex; lua_getfield(lua, -1, "client");
lua_getfield(lua, -1, "handle");
lua_getfield(lua, -2, "avatar");
lua_pushinteger(lua, argPacket->clientIndex);
lua_pushinteger(lua, argPacket->accountIndex);
lua_setfield(lua, -5, "accountIndex");
lua_setfield(lua, -4, "clientIndex");
network.Bind(&argPacket->srcAddress, Channels::SERVER); network.Bind(&argPacket->srcAddress, Channels::SERVER);
SetNextScene(SceneList::INWORLD); SetNextScene(SceneList::INWORLD);
//send this player's character info //send this player's character info
CharacterPacket newPacket; CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_NEW; newPacket.type = SerialPacketType::CHARACTER_NEW;
strncpy(newPacket.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE); strncpy(newPacket.handle, lua_tostring(lua, -2), PACKET_STRING_SIZE);
strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); strncpy(newPacket.avatar, lua_tostring(lua, -1), PACKET_STRING_SIZE);
newPacket.accountIndex = accountIndex; newPacket.accountIndex = argPacket->accountIndex;
network.SendTo(Channels::SERVER, &newPacket); network.SendTo(Channels::SERVER, &newPacket);
lua_pop(lua, 4);
} }
+6 -11
View File
@@ -28,24 +28,21 @@
#include "button.hpp" #include "button.hpp"
//utilities //utilities
#include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
//APIs
#include "lua/lua.hpp"
//STL //STL
#include <vector> #include <vector>
class LobbyMenu : public BaseScene { class LobbyMenu : public BaseScene {
public: public:
//Public access members //Public access members
LobbyMenu( LobbyMenu(lua_State*, UDPNetworkUtility&);
ConfigUtility* const argConfig,
UDPNetworkUtility* const argNetwork,
int* const argClientIndex,
int* const argAccountIndex
);
~LobbyMenu(); ~LobbyMenu();
protected: protected:
@@ -68,10 +65,8 @@ protected:
void HandleJoinResponse(ClientPacket* const); void HandleJoinResponse(ClientPacket* const);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
UDPNetworkUtility& network; UDPNetworkUtility& network;
int& clientIndex;
int& accountIndex;
//members //members
Image image; Image image;
@@ -92,7 +87,7 @@ protected:
std::vector<ServerInformation> serverInfo; std::vector<ServerInformation> serverInfo;
//a terrible hack, forgive me //a terrible hack, forgive me
//I'd love a proper gui system for this //TODO: I'd love a proper gui system for this
SDL_Rect listBox; SDL_Rect listBox;
ServerInformation* selection = nullptr; ServerInformation* selection = nullptr;
}; };
+17 -7
View File
@@ -25,13 +25,22 @@
//Public access members //Public access members
//------------------------- //-------------------------
MainMenu::MainMenu(ConfigUtility* const argConfig): MainMenu::MainMenu(lua_State* L): lua(L) {
config(*argConfig) //get the config info
{ lua_getglobal(lua, "config");
lua_getfield(lua, -1, "dir");
lua_getfield(lua, -1, "interface");
lua_getfield(lua, -2, "fonts");
std::string interfaceDir = lua_tostring(lua, -2);
std::string fontsDir = lua_tostring(lua, -1);
lua_pop(lua, 4);
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(interfaceDir + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(fontsDir + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
startButton.SetImage(&image); startButton.SetImage(&image);
@@ -102,12 +111,13 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (startButton.MouseButtonUp(button) == Button::State::HOVER) { if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::LOBBYMENU); SetNextScene(SceneList::INWORLD);
} }
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) { if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::OPTIONSMENU); SetNextScene(SceneList::OPTIONSMENU);
} }
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) { if (quitButton.MouseButtonUp(button) == Button::State::HOVER &&
button.button & SDL_BUTTON_LMASK) {
QuitEvent(); QuitEvent();
} }
} }
+4 -3
View File
@@ -24,15 +24,16 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
#include "lua/lua.hpp"
class MainMenu : public BaseScene { class MainMenu : public BaseScene {
public: public:
//Public access members //Public access members
MainMenu(ConfigUtility* const); MainMenu(lua_State* L);
~MainMenu(); ~MainMenu();
protected: protected:
@@ -50,7 +51,7 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
//members //members
Image image; Image image;
+16 -6
View File
@@ -25,13 +25,22 @@
//Public access members //Public access members
//------------------------- //-------------------------
OptionsMenu::OptionsMenu(ConfigUtility* const argConfig): OptionsMenu::OptionsMenu(lua_State* L): lua(L) {
config(*argConfig) //get the config info
{ lua_getglobal(lua, "config");
lua_getfield(lua, -1, "dir");
lua_getfield(lua, -1, "interface");
lua_getfield(lua, -2, "fonts");
std::string interfaceDir = lua_tostring(lua, -2);
std::string fontsDir = lua_tostring(lua, -1);
lua_pop(lua, 4);
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(interfaceDir + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(fontsDir + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
backButton.SetImage(&image); backButton.SetImage(&image);
@@ -84,7 +93,8 @@ void OptionsMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
} }
void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (backButton.MouseButtonUp(button) == Button::State::HOVER) { if (backButton.MouseButtonUp(button) == Button::State::HOVER &&
button.button & SDL_BUTTON_LMASK) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
} }
+4 -3
View File
@@ -24,16 +24,17 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
#include "lua/lua.hpp"
//TODO: The options screen needs to be USED //TODO: The options screen needs to be USED
class OptionsMenu : public BaseScene { class OptionsMenu : public BaseScene {
public: public:
//Public access members //Public access members
OptionsMenu(ConfigUtility* const); OptionsMenu(lua_State* L);
~OptionsMenu(); ~OptionsMenu();
protected: protected:
@@ -51,7 +52,7 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
//members //members
Image image; Image image;
+11 -4
View File
@@ -21,14 +21,21 @@
*/ */
#include "splash_screen.hpp" #include "splash_screen.hpp"
#include <string>
//------------------------- //-------------------------
//Public access members //Public access members
//------------------------- //-------------------------
SplashScreen::SplashScreen(ConfigUtility* const argConfig): SplashScreen::SplashScreen(lua_State* L): lua(L) {
config(*argConfig) //get the config info
{ lua_getglobal(lua, "config");
logo.LoadSurface(config["dir.logos"] + "krstudios.bmp"); 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(); startTick = std::chrono::steady_clock::now();
} }
+4 -3
View File
@@ -24,15 +24,16 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "image.hpp" #include "image.hpp"
#include "lua/lua.hpp"
#include <chrono> #include <chrono>
class SplashScreen : public BaseScene { class SplashScreen : public BaseScene {
public: public:
//Public access members //Public access members
SplashScreen(ConfigUtility* const); SplashScreen(lua_State* L);
~SplashScreen(); ~SplashScreen();
protected: protected:
@@ -41,7 +42,7 @@ protected:
void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
//members //members
std::chrono::steady_clock::time_point startTick; std::chrono::steady_clock::time_point startTick;
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../utilities INCLUDES+=. ../utilities ../graphics
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+12 -13
View File
@@ -78,28 +78,27 @@ static int getDepth(lua_State* L) {
return 1; return 1;
} }
static int onLoad(lua_State* L) { static int load(lua_State* L) {
//TODO: onLoad() //EMPTY
lua_pushboolean(L, false); lua_pushboolean(L, false);
return 1; return 1;
} }
static int onSave(lua_State* L) { static int save(lua_State* L) {
//TODO: onSave() //EMPTY
return 0; return 0;
} }
static int onCreate(lua_State* L) { static int create(lua_State* L) {
//TODO: onCreate() //EMPTY
return 0; return 0;
} }
static int onUnload(lua_State* L) { static int unload(lua_State* L) {
//TODO: onUnload() //EMPTY
return 0; return 0;
} }
//TODO: wrappers for the collision map
static const luaL_Reg regionLib[] = { static const luaL_Reg regionLib[] = {
{"SetTile",setTile}, {"SetTile",setTile},
{"GetTile",getTile}, {"GetTile",getTile},
@@ -110,10 +109,10 @@ static const luaL_Reg regionLib[] = {
{"GetWidth",getWidth}, {"GetWidth",getWidth},
{"GetHeight",getHeight}, {"GetHeight",getHeight},
{"GetDepth",getDepth}, {"GetDepth",getDepth},
{"OnLoad",onLoad}, {"Load",load},
{"OnSave",onSave}, {"Save",save},
{"OnCreate",onCreate}, {"Create",create},
{"OnUnload",onUnload}, {"Unload",unload},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+9 -20
View File
@@ -24,10 +24,14 @@
#include "region_pager_lua.hpp" #include "region_pager_lua.hpp"
#include "region.hpp" #include "region.hpp"
#include <stdexcept>
#include <string> #include <string>
//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) { static int setTile(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1)); RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
@@ -64,20 +68,6 @@ static int getRegion(lua_State* L) {
return 1; return 1;
} }
static int setDirectory(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(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<RegionPagerLua*>(lua_touserdata(L, 1));
std::string s = pager->GetDirectory();
lua_pushstring(L, s.c_str());
return 1;
}
static int loadRegion(lua_State* L) { static int loadRegion(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1)); RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
@@ -105,14 +95,13 @@ static int unloadRegion(lua_State* L) {
return 0; return 0;
} }
static const luaL_Reg pagerlib[] = { static const luaL_Reg regionPagerLib[] = {
{"GetRegionPager", getRegionPager},
{"SetTile", setTile}, {"SetTile", setTile},
{"GetTile", getTile}, {"GetTile", getTile},
{"SetSolid", setSolid}, {"SetSolid", setSolid},
{"GetSolid", getSolid}, {"GetSolid", getSolid},
{"GetRegion", getRegion}, {"GetRegion", getRegion},
{"SetDirectory", setDirectory},
{"GetDirectory", getDirectory},
{"LoadRegion", loadRegion}, {"LoadRegion", loadRegion},
{"SaveRegion", saveRegion}, {"SaveRegion", saveRegion},
{"CreateRegion", createRegion}, {"CreateRegion", createRegion},
@@ -121,6 +110,6 @@ static const luaL_Reg pagerlib[] = {
}; };
LUAMOD_API int openRegionPagerAPI(lua_State* L) { LUAMOD_API int openRegionPagerAPI(lua_State* L) {
luaL_newlib(L, pagerlib); luaL_newlib(L, regionPagerLib);
return 1; return 1;
} }
+3 -2
View File
@@ -19,11 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef PAGERAPI_HPP_ #ifndef REGIONPAGERAPI_HPP_
#define PAGERAPI_HPP_ #define REGIONPAGERAPI_HPP_
#include "lua/lua.hpp" #include "lua/lua.hpp"
#define TORTUGA_REGION_PAGER_PSEUDO_INDEX "RegionPagerPseudoIndex"
#define TORTUGA_REGION_PAGER_NAME "RegionPager" #define TORTUGA_REGION_PAGER_NAME "RegionPager"
LUAMOD_API int openRegionPagerAPI(lua_State* L); LUAMOD_API int openRegionPagerAPI(lua_State* L);
+3
View File
@@ -47,6 +47,9 @@ bool RegionPagerBase::GetSolid(int x, int y) {
} }
Region* RegionPagerBase::GetRegion(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 //get the region by various means
Region* ptr = nullptr; Region* ptr = nullptr;
ptr = FindRegion(x, y); ptr = FindRegion(x, y);
+32 -36
View File
@@ -32,19 +32,18 @@ Region* RegionPagerLua::LoadRegion(int x, int y) {
Region tmpRegion(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnLoad"); lua_getfield(lua, -1, "Load");
lua_pushlightuserdata(luaState, &tmpRegion); lua_pushlightuserdata(lua, &tmpRegion);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 1, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
//success or failure //success or failure
if (!lua_toboolean(luaState, -1)) { if (!lua_toboolean(lua, -1)) {
lua_pop(luaState, 2); lua_pop(lua, 2);
return nullptr; return nullptr;
} }
lua_pop(luaState, 2); lua_pop(lua, 2);
regionList.push_front(tmpRegion); regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
@@ -54,14 +53,13 @@ Region* RegionPagerLua::SaveRegion(int x, int y) {
Region* ptr = FindRegion(x, y); Region* ptr = FindRegion(x, y);
if (ptr) { if (ptr) {
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnSave"); lua_getfield(lua, -1, "Save");
lua_pushlightuserdata(luaState, ptr); lua_pushlightuserdata(lua, ptr);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
} }
return ptr; return ptr;
} }
@@ -75,30 +73,29 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
Region tmpRegion(x, y); Region tmpRegion(x, y);
//API hook //API hook
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
lua_getfield(luaState, -1, "OnCreate"); lua_getfield(lua, -1, "Create");
lua_pushlightuserdata(luaState, &tmpRegion); lua_pushlightuserdata(lua, &tmpRegion);
//TODO: parameters //TODO: parameters
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
regionList.push_front(tmpRegion); regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
void RegionPagerLua::UnloadRegion(int x, int y) { void RegionPagerLua::UnloadRegion(int x, int y) {
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
regionList.remove_if([&](Region& region) -> bool { regionList.remove_if([&](Region& region) -> bool {
if (region.GetX() == x && region.GetY() == y) { if (region.GetX() == x && region.GetY() == y) {
//API hook //API hook
lua_getfield(luaState, -1, "OnUnload"); lua_getfield(lua, -1, "Unload");
lua_pushlightuserdata(luaState, &region); lua_pushlightuserdata(lua, &region);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
return true; return true;
@@ -106,22 +103,21 @@ void RegionPagerLua::UnloadRegion(int x, int y) {
return false; return false;
}); });
lua_pop(luaState, 1); lua_pop(lua, 1);
} }
void RegionPagerLua::UnloadAll() { void RegionPagerLua::UnloadAll() {
lua_getglobal(luaState, "Region"); lua_getglobal(lua, "Region");
for (auto& it : regionList) { for (auto& it : regionList) {
//API hook //API hook
lua_getfield(luaState, -1, "OnUnload"); lua_getfield(lua, -1, "Unload");
lua_pushlightuserdata(luaState, &it); lua_pushlightuserdata(lua, &it);
lua_pushstring(luaState, directory.c_str()); if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(luaState, 2, 0, 0) != LUA_OK) { throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
} }
} }
lua_pop(luaState, 1); lua_pop(lua, 1);
regionList.clear(); regionList.clear();
} }
+3 -7
View File
@@ -42,14 +42,10 @@ public:
void UnloadAll() override; void UnloadAll() override;
//accessors & mutators //accessors & mutators
std::string SetDirectory(std::string s) { return directory = s; } lua_State* SetLuaState(lua_State* L) { return lua = L; }
std::string GetDirectory() { return directory; } lua_State* GetLuaState() { return lua; }
lua_State* SetLuaState(lua_State* L) { return luaState = L; }
lua_State* GetLuaState() { return luaState; }
protected: protected:
std::string directory; lua_State* lua = nullptr;
lua_State* luaState = nullptr;
}; };
#endif #endif
@@ -21,24 +21,24 @@
*/ */
#include "tile_sheet.hpp" #include "tile_sheet.hpp"
void TileSheet::Load(std::string fname, int xc, int yc) { void TileSheet::Load(std::string fname, int tileWidth, int tileHeight) {
XCount = xc;
YCount = yc;
image.LoadSurface(fname); image.LoadSurface(fname);
image.SetClipW(image.GetClipW()/XCount); image.SetClipW(tileWidth);
image.SetClipH(image.GetClipH()/YCount); image.SetClipH(tileHeight);
xCount = image.GetSurface()->w / image.GetClipW();
yCount = image.GetSurface()->h / image.GetClipH();
} }
void TileSheet::Unload() { void TileSheet::Unload() {
image.FreeSurface(); 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 //0 is invisible
if (tile == 0) return; if (tile == 0) return;
image.SetClipX((tile-1) % XCount * image.GetClipW()); image.SetClipX((tile-1) % xCount * image.GetClipW());
image.SetClipY((tile-1) / XCount * image.GetClipH()); image.SetClipY((tile-1) / xCount * image.GetClipH());
image.DrawTo(dest, x, y); 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); tile = region->GetTile(i, j, k);
//0 is invisible //0 is invisible
if (tile == 0) continue; if (tile == 0) continue;
image.SetClipX((tile-1) % XCount * image.GetClipW()); image.SetClipX((tile-1) % xCount * image.GetClipW());
image.SetClipY((tile-1) / XCount * image.GetClipH()); image.SetClipY((tile-1) / xCount * image.GetClipH());
image.DrawTo(dest, image.DrawTo(dest,
(region->GetX() + i) * image.GetClipW() - camX, (region->GetX() + i) * image.GetClipW() - camX,
(region->GetY() + j) * image.GetClipH() - camY); (region->GetY() + j) * image.GetClipH() - camY);
@@ -31,24 +31,24 @@
class TileSheet { class TileSheet {
public: public:
TileSheet() = default; 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; ~TileSheet() = default;
void Load(std::string fname, int XCount, int YCount); void Load(std::string fname, int tileWidth, int tileHeight);
void Unload(); 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); void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY);
//accessors //accessors
Image* GetImage() { return &image; } Image* GetImage() { return &image; }
int GetXCount() { return XCount; } int GetXCount() { return xCount; }
int GetYCount() { return YCount; } int GetYCount() { return yCount; }
int GetTileW() { return image.GetClipW(); } int GetTileW() { return image.GetClipW(); }
int GetTileH() { return image.GetClipH(); } int GetTileH() { return image.GetClipH(); }
private: private:
Image image; Image image;
int XCount = 0, YCount = 0; int xCount = 0, yCount = 0;
}; };
#endif #endif
+82
View File
@@ -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<TileSheet*>(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<TileSheet*>(lua_touserdata(L, 1));
sheet->Unload();
return 0;
}
static int getXCount(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetXCount());
return 1;
}
static int getYCount(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetYCount());
return 1;
}
static int getTileW(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
lua_pushinteger(L, sheet->GetTileW());
return 1;
}
static int getTileH(lua_State* L) {
TileSheet* sheet = reinterpret_cast<TileSheet*>(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;
}
@@ -19,12 +19,13 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef CHECKBOUNDS_HPP_ #ifndef TILESHEETAPI_HPP_
#define CHECKBOUNDS_HPP_ #define TILESHEETAPI_HPP_
#include "vector2.hpp" #include "lua/lua.hpp"
bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); #define TORTUGA_TILE_SHEET_PSEUDO_INDEX "TileSheetPseudoIndex"
bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); #define TORTUGA_TILE_SHEET_NAME "TileSheet"
LUAMOD_API int openTileSheetAPI(lua_State* L);
#endif #endif
-40
View File
@@ -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
);
}
-105
View File
@@ -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 <cstdlib>
#include <fstream>
#include <stdexcept>
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<std::string, std::string>::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<std::string, std::string>::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<std::string, std::string>::iterator it = table.find(s);
if (it == table.end()) {
return false;
}
return it->second == "true";
}
-60
View File
@@ -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 <map>
#include <string>
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<std::string, std::string>* GetMap() {
return &table;
}
private:
std::map<std::string, std::string> table;
};
#endif
-29
View File
@@ -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
+35 -35
View File
@@ -1,45 +1,45 @@
print("Lua script check") print("Lua script check")
--uber lazy declarations --uber lazy declarations
function square(x) return x*x end function math.sqr(x) return x*x end
function distance(x, y, i, j) return math.sqrt(square(x - i) + square(y - j)) end function math.dist(x, y, i, j) return math.sqrt(math.sqr(x - i) + math.sqr(y - j)) end
--tile macros, mapped to the tilesheet --objects to work on
--TODO: sheet is not accessable in the server
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 base = 14
local shift = 36 local shift = 36
plains = base + shift * 0 tiles = {
grass = base + shift * 1 plains = base + shift * 0,
dirt = base + shift * 2 grass = base + shift * 1,
sand = base + shift * 3 dirt = base + shift * 2,
sand = base + shift * 3,
water = base + shift * 4 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
local dist = distance(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1)
if dist < 10 then
Region.SetTile(region, i, j, 1, plains)
elseif dist < 12 then
Region.SetTile(region, i, j, 1, sand)
else
Region.SetTile(region, i, j, 1, water)
end
end
end
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)
} }
--TODO: could set custom generation systems here, that differ from the global generators, etc.
--TODO: I need a way to allow for different generation algorithms for different pager objects
--TODO: This design requires only one pager, but this is not good.
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
print("Finished the lua script") print("Finished the lua script")
+60
View File
@@ -0,0 +1,60 @@
--[[
--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",
--NOTE: these generally go here
-- clientIndex
-- accountIndex
-- characterIndex
},
--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