From c57ae8c42c1f2f2ed576719c00cff5cf613fe650 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Thu, 16 Jun 2022 16:14:18 -0400 Subject: Added onUpdate logic to move the enemies etc Created some algorithm logic for enemy movement Allowed for default overrides --- .clang-format | 56 +++++++++++++------------- include/algs.lua | 100 +++++++++++++++++++++++++++++++++++++++++++++++ include/constants.lua | 6 +-- include/default_proc.lua | 42 -------------------- include/defaults.lua | 61 +++++++++++++++++++++++++++++ include/queue.lua | 29 ++++++++++++++ maps/lvl1/proc.lua | 2 + src/Api.h | 93 +++++++++++++++++++++++++++++++------------ src/Level.cpp | 55 +++++++++++++------------- src/Level.h | 16 ++++---- src/main.cpp | 58 ++++++++++++++++++++------- 11 files changed, 371 insertions(+), 147 deletions(-) create mode 100644 include/algs.lua delete mode 100644 include/default_proc.lua create mode 100644 include/defaults.lua create mode 100644 include/queue.lua diff --git a/.clang-format b/.clang-format index c0d69d8..b806607 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,6 @@ --- Language: Cpp -# BasedOnStyle: Mozilla +# BasedOnStyle: LLVM AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignArrayOfStructures: None @@ -13,65 +13,65 @@ AlignOperands: Align AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllConstructorInitializersOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: true AllowShortEnumsOnASingleLine: true AllowShortBlocksOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: Inline +AllowShortFunctionsOnASingleLine: All AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: Never AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: TopLevel -AlwaysBreakAfterReturnType: TopLevel +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: Yes +AlwaysBreakTemplateDeclarations: MultiLine AttributeMacros: - __capability -BinPackArguments: false -BinPackParameters: false +BinPackArguments: true +BinPackParameters: true BraceWrapping: AfterCaseLabel: false - AfterClass: true + AfterClass: false AfterControlStatement: Never - AfterEnum: true - AfterFunction: true + AfterEnum: false + AfterFunction: false AfterNamespace: false AfterObjCDeclaration: false - AfterStruct: true - AfterUnion: true - AfterExternBlock: true + AfterStruct: false + AfterUnion: false + AfterExternBlock: false BeforeCatch: false BeforeElse: false BeforeLambdaBody: false BeforeWhile: false IndentBraces: false SplitEmptyFunction: true - SplitEmptyRecord: false + SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBinaryOperators: None BreakBeforeConceptDeclarations: true -BreakBeforeBraces: Mozilla +BreakBeforeBraces: Attach BreakBeforeInheritanceComma: false -BreakInheritanceList: BeforeComma +BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeComma +BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true ColumnLimit: 80 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 2 -ContinuationIndentWidth: 2 -Cpp11BracedListStyle: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false DisableFormat: false EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: false +FixNamespaceComments: true ForEachMacros: - foreach - Q_FOREACH @@ -95,7 +95,7 @@ IncludeCategories: IncludeIsMainRegex: '(Test)?$' IncludeIsMainSourceRegex: '' IndentAccessModifiers: false -IndentCaseLabels: true +IndentCaseLabels: false IndentCaseBlocks: false IndentGotoLabels: true IndentPPDirectives: None @@ -115,8 +115,8 @@ NamespaceIndentation: None ObjCBinPackProtocolList: Auto ObjCBlockIndentWidth: 2 ObjCBreakBeforeNestedBlockParam: true -ObjCSpaceAfterProperty: true -ObjCSpaceBeforeProtocolList: false +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 @@ -124,9 +124,9 @@ PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 200 +PenaltyReturnTypeOnItsOwnLine: 60 PenaltyIndentedWhitespace: 0 -PointerAlignment: Left +PointerAlignment: Right PPIndentWidth: -1 ReferenceAlignment: Pointer ReflowComments: true @@ -136,7 +136,7 @@ SortJavaStaticImport: Before SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: false +SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false SpaceBeforeCpp11BracedList: false diff --git a/include/algs.lua b/include/algs.lua new file mode 100644 index 0000000..ad0016a --- /dev/null +++ b/include/algs.lua @@ -0,0 +1,100 @@ +require "include.constants" +local Queue = require "include.queue" + +local function can_move(x, y, map) + return map[y][x] == MAP_SPACE +end + +---@param x number +---@param y number +---@param origin table +---@param map table +---@param queue table +local function push_moves(x, y, origin, map, queue) + -- UP + if can_move(x, y -1, map) then + queue:push({ + x = x, + y = y - 1, + origin = origin + }) + map[y-1][x] = MAP_VISITED + end + -- DOWN + if can_move(x, y +1, map) then + queue:push({ + x = x, + y = y + 1, + origin = origin + }) + map[y+1][x] = MAP_VISITED + end + -- LEFT + if can_move(x - 1, y, map) then + queue:push({ + x = x - 1, + y = y, + origin = origin + }) + map[y][x - 1] = MAP_VISITED + end + -- RIGHT + if can_move(x + 1, y, map) then + queue:push({ + x = x + 1, + y = y, + origin = origin + }) + map[y][x+1] = MAP_VISITED + end +end + +--- +---@param start_pos table [x, y] +---@param target_pos table [x, y] +---@param map table 2D map array +---@return table best move to target [x, y] +--- +local function pathfind(start_pos, target_pos, map) + local queue = Queue:new() + local visit_map = {} + for k, v in ipairs(map) do + row = {} + for ik, iv in ipairs(v) do + row[ik] = iv + end + visit_map[k] = row + end + + push_moves(start_pos.x, start_pos.y, nil, visit_map, queue) + while queue:empty() ~= true do + local pos = queue:pop() + if (pos.x == target_pos.x and pos.y == target_pos.y) then + return { dx = pos.origin.x - start_pos.x, dy = pos.origin.y - start_pos.y } + end + origin = pos.origin or { x = pos.x, y = pos.y } + push_moves(pos.x, pos.y, origin, map, queue) + end + return { dx = 0, dy = 0 } +end + +---@param map table +local function print_map(map) + for i = 1, #map do + row = map[i] + line = "" + for j = 1, #row do + if row[j] == MAP_WALL then + line = line .. "# " + else + line = line .. " " + end + end + print(line) + end +end + +return { + pathfind = pathfind, + print_map = print_map, +} diff --git a/include/constants.lua b/include/constants.lua index 5d8e860..922a35b 100644 --- a/include/constants.lua +++ b/include/constants.lua @@ -5,6 +5,6 @@ KEY_D = 100 KEY_SPACE = ' ' -TILE_WALL = 'w' -TILE_SPACE = ' ' -TILE_ENEMY = 'e' \ No newline at end of file +MAP_WALL = 1 +MAP_SPACE = 0 +MAP_VISITED = -1 diff --git a/include/default_proc.lua b/include/default_proc.lua deleted file mode 100644 index 13ef15d..0000000 --- a/include/default_proc.lua +++ /dev/null @@ -1,42 +0,0 @@ ---[[ -These are the default implementations of the override actions. -If you want to add custom logic into your game you can define a "proc.lua" in your map dir. - -The following functions are also available via our C library: - -void c_update_player_pos (dx, dy) -boolean c_player_can_move (dx, dy) -boolean c_enemy_can_move (id, dx, dy) -c_spawn_enemy (x, y) -c_destroy_enemy (id) -c_trigger_win() -c_trigger_loss(msg) -c_fatal(msg) - ---]] - -require "include.constants"; - ----@param pressedKey number -function onKeyPress(pressedKey) - - dx = 0 - dy = 0 - if (pressedKey == KEY_W) then - dy = -1 - elseif pressedKey == KEY_A then - dx = -1 - elseif pressedKey == KEY_S then - dy = 1 - elseif pressedKey == KEY_D then - dx = 1 - end - - if c_player_can_move(dx, dy) then - c_update_player_pos(dx, dy) - end -end - -function onUpdate() - -end \ No newline at end of file diff --git a/include/defaults.lua b/include/defaults.lua new file mode 100644 index 0000000..a6cefe5 --- /dev/null +++ b/include/defaults.lua @@ -0,0 +1,61 @@ +--[[ +These are the default implementations of the override actions. +If you want to add custom logic into your game you can define a "proc.lua" in your map dir. + +The following functions are also available via our C library: + +void c_update_player_pos (dx, dy) +boolean c_player_can_move (dx, dy) +boolean c_enemy_can_move (id, dx, dy) +c_spawn_enemy (x, y) +c_destroy_enemy (id) +c_trigger_win() +c_trigger_loss(msg) +c_fatal(msg) + +--]] +require "include.constants"; +local algs = require "include.algs"; + +--- setup random +--math.randomseed(os.time()) + +---@param pressedKey number +function onKeyPress(pressedKey) + + dx = 0 + dy = 0 + if (pressedKey == KEY_W) then + dy = -1 + elseif pressedKey == KEY_A then + dx = -1 + elseif pressedKey == KEY_S then + dy = 1 + elseif pressedKey == KEY_D then + dx = 1 + end + + c_move_player(dx, dy) +end + +function onUpdate() + enemies = c_get_enemies() -- external + assert(type(enemies) == "table", "Enemies not a table") + + player = c_get_player_position() -- external + assert(type(player) == "table", "Player is not a table") + + map = c_get_map(); + assert(type(map) == "table", "map is not a table") + + for _, v in ipairs(enemies) do + local next = algs.pathfind(v, player, map) + c_move_enemy(v.id, next.dx, next.dy) + end +end + +--- allow for requiring in other files for usage +return { + onKeyPress = onKeyPress, + onUpdate = onUpdate, +} \ No newline at end of file diff --git a/include/queue.lua b/include/queue.lua new file mode 100644 index 0000000..2eb77c6 --- /dev/null +++ b/include/queue.lua @@ -0,0 +1,29 @@ +Queue = {} + +function Queue:new() + o = { first = 1, top = 0, data = {} } + self.__index = self + return setmetatable(o, self) +end + +function Queue:push(val) + local top = self.top + 1 + self.top = top + self.data[top] = val +end + +function Queue:pop() + if self:empty() then + return nil + end + local val = self.data[self.first] + self.data[self.first] = nil + self.first = self.first + 1 + return val +end + +function Queue:empty() + return self.top < self.first +end + +return Queue diff --git a/maps/lvl1/proc.lua b/maps/lvl1/proc.lua index fec9ed5..579b5ba 100644 --- a/maps/lvl1/proc.lua +++ b/maps/lvl1/proc.lua @@ -1,4 +1,6 @@ require "include.constants"; +local default = require "include.defaults" function onKeyPress(key) + default.onKeyPress(key) end \ No newline at end of file diff --git a/src/Api.h b/src/Api.h index da763c2..f9e7e8d 100644 --- a/src/Api.h +++ b/src/Api.h @@ -7,16 +7,24 @@ extern std::shared_ptr lvl; -static int -c_update_player_pos(lua_State* L) -{ +static int c_get_player_position(lua_State *L) { + lua_createtable(L, 0, 2); + lua_pushnumber(L, lvl->player.y + 1); + lua_setfield(L, -2, "y"); + lua_pushnumber(L, lvl->player.x + 1); + lua_setfield(L, -2, "x"); + + return 1; +} + +static int c_move_player(lua_State *L) { // stack ordering int dy = static_cast(lua_tonumber(L, -1)); int dx = static_cast(lua_tonumber(L, -2)); bool res = false; - if (lvl->canStep(dx, dy)) { + if (lvl->playerCanStep(dx, dy)) { lvl->player.x += dx; lvl->player.y += dy; res = true; @@ -27,45 +35,80 @@ c_update_player_pos(lua_State* L) return 1; } -static int -c_player_can_move(lua_State* L) -{ +static int c_move_enemy(lua_State *L) { // stack ordering int dy = static_cast(lua_tonumber(L, -1)); int dx = static_cast(lua_tonumber(L, -2)); + int id = static_cast(lua_tonumber(L, -3)); + + int i = lvl->getEnemyIndex(id); + // guard against enemy not found + if (i == -1) { + lua_pushboolean(L, false); + return 1; + } + + bool res = false; + if (lvl->enemyCanStep(lvl->enemyPositions[i], dx, dy)) { + lvl->enemyPositions[i].x += dx; + lvl->enemyPositions[i].y += dy; + res = true; + } - bool res = lvl->canStep(dx, dy); lua_pushboolean(L, res); return 1; } -static int -c_enemy_can_move(lua_State* L) -{ - return 1; -} +static int c_get_enemies(lua_State *L) { + lua_createtable(L, int(lvl->enemyPositions.size()), 0); + + int idx = 0; + + for (auto &pos : lvl->enemyPositions) { + lua_pushnumber(L, ++idx); + lua_createtable(L, 0, 3); + lua_pushnumber(L, pos.id); + lua_setfield(L, -2, "id"); + lua_pushnumber(L, pos.x + 1); + lua_setfield(L, -2, "x"); + lua_pushnumber(L, pos.y + 1); + lua_setfield(L, -2, "y"); + lua_settable(L, -3); + } -static int -c_spawn_enemy(lua_State* L) -{ return 1; } -static int -c_destroy_enemy(lua_State* L) -{ +static int c_spawn_enemy(lua_State *L) { return 1; } + +static int c_destroy_enemy(lua_State *L) { return 1; } + +static int c_get_map(lua_State *L) { + lua_createtable(L, int(lvl->map.size()), 0); + int idx = 0; + for (auto &vec : lvl->map) { + lua_pushnumber(L, ++idx); + lua_createtable(L, int(vec.size()), 0); + int inner_idx = 0; + for (auto &c : vec) { + lua_pushnumber(L, ++inner_idx); + lua_pushnumber(L, c == WALL_SPACE ? 1 : 0); + lua_rawset(L, -3); + } + lua_rawset(L, -3); + } return 1; } -static void -init_c_api(lua_State* L) -{ - lua_register(L, "c_update_player_pos", c_update_player_pos); - lua_register(L, "c_player_can_move", c_player_can_move); - lua_register(L, "c_enemy_can_move", c_enemy_can_move); +static void init_c_api(lua_State *L) { + lua_register(L, "c_move_player", c_move_player); + lua_register(L, "c_move_enemy", c_move_enemy); lua_register(L, "c_spawn_enemy", c_spawn_enemy); lua_register(L, "c_destroy_enemy", c_destroy_enemy); + lua_register(L, "c_get_enemies", c_get_enemies); + lua_register(L, "c_get_player_position", c_get_player_position); + lua_register(L, "c_get_map", c_get_map); } #endif // DNG_API_H \ No newline at end of file diff --git a/src/Level.cpp b/src/Level.cpp index 013c2a1..5f06def 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -3,9 +3,11 @@ #include #include -void -Level::loadLevelFromFile(const char* filePath) -{ +bool canStep(Pos pos, int dx, int dy, std::vector> map) { + return map[pos.y + dy][pos.x + dx] != WALL_SPACE; +} + +void Level::loadLevelFromFile(const char *filePath) { std::ifstream mapFile(filePath); if (mapFile.is_open()) { @@ -15,7 +17,6 @@ Level::loadLevelFromFile(const char* filePath) // from 1 -> N each enemy and treasure has its own unique ID // IDs are unique entirely, not just per enemy or treasure - std::string line; int y = 0; while (std::getline(mapFile, line)) { @@ -28,14 +29,14 @@ Level::loadLevelFromFile(const char* filePath) this->map[y].push_back(BLANK_SPACE); } else if (c == ENEMY_TKN) { this->enemyPositions.push_back( - { .id = this->nextId(), .x = x, .y = y }); + {.id = this->nextId(), .x = x, .y = y}); this->map[y].push_back(BLANK_SPACE); } else if (c == PLAYER_TKN) { - this->player = { .id = playerId, .x = x, .y = y }; + this->player = {.id = playerId, .x = x, .y = y}; this->map[y].push_back(BLANK_SPACE); } else if (c == TREASURE_TKN) { this->treasurePositions.push_back( - { .id = this->nextId(), .x = x, .y = y }); + {.id = this->nextId(), .x = x, .y = y}); this->map[y].push_back(BLANK_SPACE); } else { continue; @@ -48,26 +49,17 @@ Level::loadLevelFromFile(const char* filePath) mapFile.close(); } -bool -Level::isEmpty(int x, int y) -{ - return map[y][x] == BLANK_SPACE; -} +bool Level::isEmpty(int x, int y) { return map[y][x] == BLANK_SPACE; } -bool -Level::canStep(int dx, int dy) -{ - bool res = map[player.y + dy][player.x + dx] != WALL_SPACE; - return res; +bool Level::playerCanStep(int dx, int dy) { + return canStep(player, dx, dy, map); } -void -Level::print() -{ +void Level::print() { int x = 0; int y = 0; - for (auto& row : map) { - for (auto& tile : row) { + for (auto &row : map) { + for (auto &tile : row) { bool printed = false; if (player.x == x && player.y == y) { std::cout << "p"; @@ -101,8 +93,17 @@ Level::print() } } -int -Level::nextId() -{ - return idCounter++; -} \ No newline at end of file +int Level::nextId() { return idCounter++; } + +int Level::getEnemyIndex(int id) { + for (int i = 0; i < enemyPositions.size(); i++) { + if (enemyPositions[i].id == id) { + return i; + } + } + + return -1; +} +bool Level::enemyCanStep(Pos pos, int dx, int dy) { + return canStep(pos, dx, dy, map); +} diff --git a/src/Level.h b/src/Level.h index 1607f0c..7791ab6 100644 --- a/src/Level.h +++ b/src/Level.h @@ -12,30 +12,32 @@ static const char WALL_SPACE = '#'; #include #include -struct Pos -{ +struct Pos { int id; int x; int y; } typedef Coord; -class Level -{ +class Level { public: - void loadLevelFromFile(const char* filePath); + void loadLevelFromFile(const char *filePath); bool isEmpty(int x, int y); - bool canStep(int dx, int dy); + bool playerCanStep(int dx, int dy); + + int getEnemyIndex(int id); + + bool enemyCanStep(Pos pos, int dx, int dy); void print(); int nextId(); - std::vector> map; Pos player; std::vector enemyPositions; + std::vector treasurePositions; private: diff --git a/src/main.cpp b/src/main.cpp index 6f649cd..eef9777 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,15 +5,18 @@ #include #include -const char* DEFAULT_PROC = "include/default_proc.lua"; +const char *DEFAULT_PROC = "include/defaults.lua"; std::shared_ptr lvl; -bool -call_onkeypress(lua_State* L, char pressedKey); +struct LState { + lua_State *onkeypress; + lua_State *onupdate; +} typedef LState; -int -main(int argc, char** argv) -{ +bool call_onkeypress(lua_State *L, char pressedKey); +bool call_onupdate(lua_State *L); + +int main(int argc, char **argv) { if (argc <= 1) { return -1; @@ -21,18 +24,18 @@ main(int argc, char** argv) std::string lvl_pfx = argv[1]; - std::filesystem::path mapFile{ lvl_pfx + "/dng.map" }; - std::filesystem::path luaFile{ lvl_pfx + "/proc.lua" }; + std::filesystem::path mapFile{lvl_pfx + "/dng.map"}; + std::filesystem::path luaFile{lvl_pfx + "/proc.lua"}; lvl = std::make_shared(); lvl->loadLevelFromFile(mapFile.c_str()); - lua_State* L_lvl = luaL_newstate(); + lua_State *L_lvl = luaL_newstate(); luaL_openlibs(L_lvl); init_c_api(L_lvl); - lua_State* L_default = luaL_newstate(); + lua_State *L_default = luaL_newstate(); luaL_openlibs(L_default); init_c_api(L_default); @@ -43,8 +46,22 @@ main(int argc, char** argv) return EXIT_FAILURE; } + // Initialize to default + LState l_state = {.onkeypress = L_default, .onupdate = L_default}; + if (std::filesystem::exists(luaFile) && - luaL_dofile(L_lvl, luaFile.c_str()) != LUA_OK) { + luaL_dofile(L_lvl, luaFile.c_str()) == LUA_OK) { + + // overwrite defaults + lua_getglobal(L_lvl, "onKeyPress"); + if (lua_isfunction(L_lvl, -1)) { + l_state.onkeypress = L_lvl; + } + lua_getglobal(L_lvl, "onUpdate"); + if (lua_isfunction(L_lvl, -1)) { + l_state.onupdate = L_lvl; + } + } else if (std::filesystem::exists(luaFile)) { std::cout << "[C] No Good" << std::endl; luaL_error(L_lvl, "Error: %s\n", lua_tostring(L_lvl, -1)); return EXIT_FAILURE; @@ -57,7 +74,10 @@ main(int argc, char** argv) do { lvl->print(); std::cin >> in; - if (!call_onkeypress(L_default, in)) { + if (!call_onkeypress(l_state.onkeypress, in)) { + quit = true; + } + if (!call_onupdate(l_state.onupdate)) { quit = true; } if (!quit && in == 'q') { @@ -70,9 +90,7 @@ main(int argc, char** argv) return EXIT_SUCCESS; } -bool -call_onkeypress(lua_State* L, char pressedKey) -{ +bool call_onkeypress(lua_State *L, char pressedKey) { lua_getglobal(L, "onKeyPress"); if (!lua_isfunction(L, -1)) { std::cout << "[C] Error onKeyPress not function | not found" << std::endl; @@ -81,4 +99,14 @@ call_onkeypress(lua_State* L, char pressedKey) lua_pushinteger(L, pressedKey); lua_pcall(L, 1, 1, 0); return true; +} + +bool call_onupdate(lua_State *L) { + lua_getglobal(L, "onUpdate"); + if (!lua_isfunction(L, -1)) { + std::cout << "[C] Error onUpdate not function | not found" << std::endl; + return false; + } + lua_pcall(L, 0, 1, 0); + return true; } \ No newline at end of file -- cgit v1.2.3-54-g00ecf