summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-06-16 16:14:18 -0400
committerSteph Enders <smenders@gmail.com>2022-06-16 16:16:34 -0400
commitc57ae8c42c1f2f2ed576719c00cff5cf613fe650 (patch)
tree0e0c6533b099edbe2c283a8961b1b240d29ad904
parent750b308d70cf1c903812316deeb0a8c4befa37ce (diff)
Added onUpdate logic to move the enemies etc
Created some algorithm logic for enemy movement Allowed for default overrides
-rw-r--r--.clang-format56
-rw-r--r--include/algs.lua100
-rw-r--r--include/constants.lua6
-rw-r--r--include/defaults.lua (renamed from include/default_proc.lua)29
-rw-r--r--include/queue.lua29
-rw-r--r--maps/lvl1/proc.lua2
-rw-r--r--src/Api.h93
-rw-r--r--src/Level.cpp55
-rw-r--r--src/Level.h16
-rw-r--r--src/main.cpp58
10 files changed, 334 insertions, 110 deletions
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/defaults.lua
index 13ef15d..a6cefe5 100644
--- a/include/default_proc.lua
+++ b/include/defaults.lua
@@ -14,8 +14,11 @@ 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)
@@ -32,11 +35,27 @@ function onKeyPress(pressedKey)
dx = 1
end
- if c_player_can_move(dx, dy) then
- c_update_player_pos(dx, dy)
- 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
-end \ No newline at end of file
+--- 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<Level> 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<int>(lua_tonumber(L, -1));
int dx = static_cast<int>(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<int>(lua_tonumber(L, -1));
int dx = static_cast<int>(lua_tonumber(L, -2));
+ int id = static_cast<int>(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 <iostream>
#include <string>
-void
-Level::loadLevelFromFile(const char* filePath)
-{
+bool canStep(Pos pos, int dx, int dy, std::vector<std::vector<char>> 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 <memory>
#include <vector>
-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<std::vector<char>> map;
Pos player;
std::vector<Pos> enemyPositions;
+
std::vector<Pos> 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 <iostream>
#include <lua.hpp>
-const char* DEFAULT_PROC = "include/default_proc.lua";
+const char *DEFAULT_PROC = "include/defaults.lua";
std::shared_ptr<Level> 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<Level>();
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