From caab51fc69c2ce98bd00e967fd99c428d9385285 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 25 Jan 2022 19:40:37 +0200 Subject: [PATCH] implemented Lua manager and unit data manager --- .gitmodules | 3 + CMakeLists.txt | 9 +- Scripts/defConfigs.lua | 7 ++ Scripts/unitData.lua | 272 +++++++++++++++++++++++++++++++++++++++++ external/lua | 1 + luaManager.cpp | 79 ++++++++++++ luaManager.h | 39 ++++++ unitDataManager.cpp | 76 ++++++++++++ unitDataManager.h | 35 ++++++ 9 files changed, 519 insertions(+), 2 deletions(-) create mode 100644 Scripts/defConfigs.lua create mode 100644 Scripts/unitData.lua create mode 160000 external/lua create mode 100644 luaManager.cpp create mode 100644 luaManager.h create mode 100644 unitDataManager.cpp create mode 100644 unitDataManager.h diff --git a/.gitmodules b/.gitmodules index 115645b..ecd2e7a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "external/vb01"] path = external/vb01 url = https://github.com/devZoGok/vb01 +[submodule "external/lua"] + path = external/lua + url = https://github.com/lua/lua diff --git a/CMakeLists.txt b/CMakeLists.txt index d1a8764..236d873 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,13 +7,14 @@ cmake_policy(SET CMP0015 NEW) set(states activeGameState.cpp inGameAppState.cpp guiAppState.cpp) set(util util.cpp binds.h) set(gui tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) -set(core gameManager.cpp main.cpp) +set(core gameManager.cpp luaManager.cpp main.cpp) set(ships vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp) set(projectiles projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp) set(aircraft jet.cpp demoJet.cpp missileJet.cpp) set(fx explosion.cpp) -set(content player.cpp unit.cpp map.cpp ${ships} ${projectiles} ${aircraft} ${fx}) +set(content player.cpp unitDataManager.cpp unit.cpp map.cpp ${ships} ${projectiles} ${aircraft} ${fx}) +set(LUA_DIR external/lua) set(SFML_DIR external/SFML) set(VB01_DIR external/vb01) set(FREETYPE_DIR ${VB01_DIR}/external/freetype) @@ -53,6 +54,10 @@ set(GLFW_LIB_DIR build/${GLFW_DIR}/src) include_directories(${GLFW_DIR}/include/GLFW) target_link_directories(${GAME_NAME} PUBLIC ${GLFW_LIB_DIR}) +set(LUA_LIB_DIR ${LUA_DIR}/testes/libs) +include_directories(${LUA_DIR}) + +target_link_libraries(${GAME_NAME} lua) target_link_libraries(${GAME_NAME} vb01) target_link_libraries(${GAME_NAME} vb01Gui) target_link_libraries(${GAME_NAME} gameBase) diff --git a/Scripts/defConfigs.lua b/Scripts/defConfigs.lua new file mode 100644 index 0000000..7d52e22 --- /dev/null +++ b/Scripts/defConfigs.lua @@ -0,0 +1,7 @@ +PATH = "/home/dominykas/c++/Battleship/Assets/" +DEFAULT_TEXTURE = PATH .. "Textures/defaultTexture.jpg" + +numAppStates = 3 +numMaxBinds = 23 +numBinds = {12, 1, 23} +numConfBinds = {0, 1, 13} diff --git a/Scripts/unitData.lua b/Scripts/unitData.lua new file mode 100644 index 0000000..4d42141 --- /dev/null +++ b/Scripts/unitData.lua @@ -0,0 +1,272 @@ +--dofile("defConfigs.lua") + +numUnits = 11 +UnitType = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6}; +unitType = { + UnitType.VESSEL, UnitType.VESSEL, + UnitType.DESTROYER, UnitType.DESTROYER, + UnitType.CRUISER, UnitType.CRUISER, + UnitType.AIRCRAFT_CARRIER, UnitType.AIRCRAFT_CARRIER, + UnitType.SUBMARINE, + UnitType.MISSILE_JET, UnitType.DEMO_JET +}; +health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500} +speed = {.025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15} +destinationOffset = {.75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5} +anglePrecision = {.1, .1, .1, .1, .1, .1, .1, .1, .1, 3, 3} +cost = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500} +maxTurnAngle = {1, 1, .5, .5, 1, 1, .5, .5, 1, 2, 2} +range = {15, 15, 14, 14, 13, 13, 12, 12,15,25} + +unitCornerPoints = { + { + {x = .9, y = -1, z = -6}, + {x = -.9, y = -1, z = -6}, + {x = -.9, y = -1, z = 5.8}, + {x = .9, y = -1, z = 5.8}, + {x = .9, y = 3.5, z = -6}, + {x = -.9, y = 3.5, z = -6}, + {x = -.9, y = 3.5, z = 5.8}, + {x = .9, y = 3.5, z = 5.8} + }, + { + {x = .5, y = -.5, z = -7.05}, + {x = -.5, y = -.5, z = -7.05}, + {x = -.5, y = -.5, z = 6.4}, + {x = .5, y = -.5, z = 6.4}, + {x = .5, y = 3, z = -7.05}, + {x = -.5, y = 3, z = -7.05}, + {x = -.5, y = 3, z = 6.4}, + {x = .5, y = 3, z = 6.4} + }, + { + {x = .5, y = -.5, z = -3.4}, + {x = -.5, y = -.5, z = -3.4}, + {x = -.5, y = -.5, z = 3.4}, + {x = .5, y = -.4, z = 3.4}, + {x = .5, y = 2.1, z = -3.4}, + {x = -.5, y = 2, z = -3.4}, + {x = -.5, y = 2, z = 3.4}, + {x = .5, y = 2, z = 3.4} + }, + { + {x = .5, y = -.25, z = -4.8}, + {x = -.5, y = -.25, z = -4.8}, + {x = -.5, y = -.25, z = 4.7}, + {x = .5, y = -.25, z = 4.7}, + {x = .5, y = .8, z = -4.8}, + {x = -.5, y = .8, z = -4.8}, + {x = -.5, y = .8, z = 4.7}, + {x = .5, y = .8, z = 4.7} + }, + { + {x = .7, y = -.3, z = -4}, + {x = -.7, y = -.3, z = -4}, + {x = -.7, y = -.3, z = 3.4}, + {x = .7, y = -.3, z = 3.4}, + {x = .7, y = 2.9, z = -4}, + {x = -.7, y = 2.9, z = -4}, + {x = -.7, y = 2.9, z = 3.4}, + {x = .7, y = 2.9, z = 3.4} + }, + { + {x = .5, y = -.5, z = -6.1}, + {x = -.5, y = -.5, z = -6.1}, + {x = -.5, y = -.5, z = 5.2}, + {x = .5, y = -.5, z = 5.2}, + {x = .5, y = 2.2, z = -6.1}, + {x = -.5, y = 2.2, z = -6.1}, + {x = -.5, y = 2.2, z = 5.2}, + {x = .5, y = 2.2, z = 5.2} + }, + { + {x = 2, y = -.5, z = -10}, + {x = -2, y = -.5, z = -10}, + {x = -2, y = -.5, z = 9}, + {x = 2, y = -.5, z = 9}, + {x = 2, y = 5.5, z = -10}, + {x = -2, y = 5.5, z = -10}, + {x = -2, y = 5.5, z = 9}, + {x = 2, y = 5.5, z = 9} + }, + { + {x = 2, y = -1, z = -10}, + {x = -2, y = -1, z = -10}, + {x = -2, y = -1, z = 9}, + {x = 2, y = -1, z = 9}, + {x = 2, y = 4.5, z = -10}, + {x = -2, y = 4.5, z = -10}, + {x = -2, y = 4.5, z = 9}, + {x = 2, y = 4.5, z = 9} + }, + { + {x = .25, y = -.5, z = -2}, + {x = -.25, y = -.5, z = -2}, + {x = -.25, y = -.5, z = 3}, + {x = .25, y = -.5, z = 3}, + {x = .25, y = .7, z = -2}, + {x = -.25, y = .7, z = -2}, + {x = -.25, y = .7, z = 3}, + {x = .25, y = .7, z = 3} + }, + { + {x = .6, y =-.1, z =-1.3}, + {x = -.6, y =-.1, z =-1.3}, + {x = -.6, y =-.1, z =.8}, + {x = .6, y =-.1, z =.8}, + {x = .6, y =.3, z =-1.3}, + {x = -.6, y =.3, z =-1.3}, + {x = -.6, y =.3, z =.8}, + {x = .6, y =.3, z =.8} + }, + { + {x = .5, y =-.1, z =-1.3}, + {x = -.5, y =-.1, z =-1.3}, + {x = -.5, y =-.1, z =.7}, + {x = .5, y =-.1, z =.7}, + {x = .5, y =.15, z =-1.3}, + {x = -.5, y =.15, z =-1.3}, + {x = -.5, y =.15, z =.7}, + {x = .5, y =.15, z =.7} + } +}; +unitCuboidDimensions = { + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1}, + {x = 1, y = 1, z = 1} + }, + { + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1} + }, + { + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1} + }, + { + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1}, + {x = .1, y = .1, z = .1} + } +}; + +unitAxisLength = {8, 8,6,6,5, 5,8, 8, 7,2,2} +lineOfSight = {5, 8, 4, 4, 3, 3, 6, 6, 3, 3, 8} +name = { + "Battleship", "Battleship", + "Destroyer", "Destroyer", + "Cruiser", "Cruiser", + "Aircraft carrier", "Aircraft carrier", + "Submarine", + "Jet", "Jet" +} +meshPath = { + "battleship00.vb", "battleship01.vb", + "destroyer00.vb", "destroyer01.vb", + "cruiser00.vb", "cruiser01.vb", + "aircraftCarrier00.vb", "aircraftCarrier01.vb", + "submarine.vb", + "jet00.x", "jet01.x" +} +PATH = "" +basePath = { + PATH .. "Models/Battleships/", PATH .. "Models/Battleships/", + PATH .. "Models/Destroyers/", PATH .. "Models/Destroyers/", + PATH .. "Models/Cruisers/", PATH .. "Models/Cruisers/", + PATH .. "Models/Aircraft carriers/", PATH .. "Models/Aircraft carriers/", + PATH .. "Models/Submarines/", + PATH .. "Models/Jets/", PATH .. "Models/Jets/" +}; diff --git a/external/lua b/external/lua new file mode 160000 index 0000000..c33b172 --- /dev/null +++ b/external/lua @@ -0,0 +1 @@ +Subproject commit c33b1728aeb7dfeec4013562660e07d32697aa6b diff --git a/luaManager.cpp b/luaManager.cpp new file mode 100644 index 0000000..33ffd2e --- /dev/null +++ b/luaManager.cpp @@ -0,0 +1,79 @@ +#include "luaManager.h" + +#include + +using namespace std; + +namespace battleship{ + static LuaManager *luaManager = nullptr; + + LuaManager* LuaManager::getSingleton(){ + if(!luaManager) luaManager = new LuaManager(); + + return luaManager; + } + + LuaManager::LuaManager(){ + state = luaL_newstate(); + luaL_openlibs(state); + } + + void LuaManager::readFile(string file){ + if(luaL_dofile(state, file.c_str()) && lua_pcall(state, 0, 0, 0)) + cout << "Error reading Lua file" << file << "\n"; + } + + int LuaManager::getInt(string varName){ + lua_getglobal(state, varName.c_str()); + int isNum; + int var = (int)lua_tointegerx(state, -1, &isNum); + lua_pop(state, 1); + return var; + } + + void LuaManager::prepareTableForRetrieval(string table, vector &indices){ + lua_getglobal(state, table.c_str()); + + if(!lua_istable(state, -1)) + cout << table << " is not a table\n"; + + for(int i = 0; i < indices.size(); i++){ + if(indices[i].string) + lua_pushstring(state, indices[i].index.c_str()); + else + lua_pushnumber(state, atoi(indices[i].index.c_str())); + + lua_gettable(state, -2); + } + } + + int LuaManager::getIntFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + + int isNum; + int result = lua_tointegerx(state, -1, &isNum); + + lua_pop(state, 1 + indices.size()); + + return result; + } + + float LuaManager::getFloatFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + + int isNum; + float result = lua_tonumberx(state, -1, &isNum); + + lua_pop(state, 1 + indices.size()); + + return result; + } + + string LuaManager::getStringFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + string result = lua_tostring(state, -1); + lua_pop(state, 1 + indices.size()); + + return result; + } +} diff --git a/luaManager.h b/luaManager.h new file mode 100644 index 0000000..a513790 --- /dev/null +++ b/luaManager.h @@ -0,0 +1,39 @@ +#ifndef LUA_MANAGER_H +#define LUA_MANAGER_H + +#include +#include +#include + +#include +#include + +namespace battleship{ + class LuaManager{ + public: + struct Index{ + std::string index; + bool string; + + Index(std::string index, bool string){ + this->index = index; + this->string = string; + } + }; + + static LuaManager* getSingleton(); + inline lua_State* getState(){return state;} + void readFile(std::string); + int getInt(std::string); + int getIntFromTable(std::string, std::vector); + float getFloatFromTable(std::string, std::vector); + std::string getStringFromTable(std::string, std::vector); + private: + LuaManager(); + void prepareTableForRetrieval(std::string, std::vector&); + + lua_State *state = nullptr; + }; +} + +#endif diff --git a/unitDataManager.cpp b/unitDataManager.cpp new file mode 100644 index 0000000..afb66af --- /dev/null +++ b/unitDataManager.cpp @@ -0,0 +1,76 @@ +#include "unitDataManager.h" +#include "luaManager.h" + +#include +#include + +#include +#include + +using namespace std; +using namespace vb01; + +namespace battleship{ + typedef LuaManager::Index Index; + + UnitDataManager *unitDataManager = nullptr; + + UnitDataManager* UnitDataManager::getSingleton(){ + if(!unitDataManager) + unitDataManager = new UnitDataManager(); + + return unitDataManager; + } + + UnitDataManager::UnitDataManager(){ + LuaManager *luaManager = LuaManager::getSingleton(); + luaManager->readFile("../Scripts/unitData.lua"); + + numUnits = luaManager->getInt("numUnits"); + + health = new int[numUnits]; + speed = new float[numUnits]; + destinationOffset = new float[numUnits]; + anglePrecision = new float[numUnits]; + cost = new int[numUnits]; + maxTurnAngle = new float[numUnits]; + range = new float[numUnits]; + unitAxisLength = new float[numUnits]; + lineOfSight = new float[numUnits]; + name = new string[numUnits]; + meshPath = new string[numUnits]; + basePath = new string[numUnits]; + unitCornerPoints = new Vector3*[numUnits]; + unitCuboidDimensions = new Vector3*[numUnits]; + + for(int i = 0; i < numUnits; i++){ + health[i] = luaManager->getIntFromTable("health", vector{Index(to_string(i + 1), false)}); + speed[i] = luaManager->getFloatFromTable("speed", vector{Index(to_string(i + 1), false)}); + destinationOffset[i] = luaManager->getFloatFromTable("destinationOffset", vector{Index(to_string(i + 1), false)}); + anglePrecision[i] = luaManager->getFloatFromTable("anglePrecision", vector{Index(to_string(i + 1), false)}); + cost[i] = luaManager->getIntFromTable("cost", vector{Index(to_string(i + 1), false)}); + maxTurnAngle[i] = luaManager->getFloatFromTable("maxTurnAngle", vector{Index(to_string(i + 1), false)}); + range[i] = luaManager->getFloatFromTable("range", vector{Index(to_string(i + 1), false)}); + unitAxisLength[i] = luaManager->getFloatFromTable("unitAxisLength", vector{Index(to_string(i + 1), false)}); + lineOfSight[i] = luaManager->getFloatFromTable("lineOfSight", vector{Index(to_string(i + 1), false)}); + name[i] = luaManager->getStringFromTable("name", vector{Index(to_string(i + 1), false)}); + meshPath[i] = luaManager->getStringFromTable("meshPath", vector{Index(to_string(i + 1), false)}); + basePath[i] = luaManager->getStringFromTable("basePath", vector{Index(to_string(i + 1), false)}); + + unitCornerPoints[i] = new Vector3[8]; + unitCuboidDimensions[i] = new Vector3[8]; + + for(int j = 0; j < 8; j++){ + float cornerX = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("x", true)}); + float cornerY = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("y", true)}); + float cornerZ = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("z", true)}); + unitCornerPoints[i][j] = Vector3(cornerX, cornerY, cornerZ); + + float dimX = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("x", true)}); + float dimY = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("y", true)}); + float dimZ = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(to_string(i + 1), false), Index(to_string(j + 1), false), Index("z", true)}); + unitCuboidDimensions[i][j] = Vector3(dimX, dimY, dimZ); + } + } + } +} diff --git a/unitDataManager.h b/unitDataManager.h new file mode 100644 index 0000000..d336f2f --- /dev/null +++ b/unitDataManager.h @@ -0,0 +1,35 @@ +#ifndef UNIT_DATA_MANAGER_H +#define UNIT_DATA_MANAGER_H + +#include + +#include + +namespace battleship{ + class UnitDataManager{ + public: + static UnitDataManager* getSingleton(); + private: + UnitDataManager(); + + enum class UnitType {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET}; + int numUnits; + UnitType unitType; + int *health; + float *speed; + float *destinationOffset; + float *anglePrecision; + int *cost; + float *maxTurnAngle; + float *range; + vb01::Vector3 **unitCornerPoints; + vb01::Vector3 **unitCuboidDimensions; + float *unitAxisLength; + float *lineOfSight; + std::string *name; + std::string *meshPath; + std::string *basePath; + }; +} + +#endif