Compare commits

...

6 Commits

  1. 2
      Makefile
  2. 12
      README.md
  3. BIN
      assets/axe_ranger-256.png
  4. 2
      assets/config.json
  5. BIN
      assets/evil_eye-sprites.png
  6. BIN
      assets/undead_peasant-spritesheet.png
  7. 13
      lel.cpp
  8. 62
      main.cpp
  9. 6
      main_ui.cpp
  10. 2
      matrix.cpp
  11. 79
      meson.build
  12. 2
      scripts/reset_build.sh
  13. BIN
      scripts/win_installer.ifp
  14. 266
      tests/goap.cpp

@ -22,7 +22,7 @@ tracy_build:
meson compile -j 10 -C builddir meson compile -j 10 -C builddir
test: build test: build
./builddir/runtests ./builddir/runtests "[goap]"
run: build test run: build test
powershell "cp ./builddir/zedcaster.exe ." powershell "cp ./builddir/zedcaster.exe ."

@ -127,6 +127,18 @@ I would also like statistics that show it's better, not just your word.
It's early so probably a bunch of bugs. It's early so probably a bunch of bugs.
## Linux Build Notes
Libraries Needed:
* libxi-dev
* libfreetype-dev
It uses c++ so you may need to install a libg++ or libc++ for your system. Usually this is all you
need:
apt install build-essential
## OSX Build Notes ## OSX Build Notes
* Quite a bad experience. Need to install Python, cmake, meson, and ninja all which are in homebrew but if you don't use homebrew then this is a problem. * Quite a bad experience. Need to install Python, cmake, meson, and ninja all which are in homebrew but if you don't use homebrew then this is a problem.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 KiB

After

Width:  |  Height:  |  Size: 38 KiB

@ -54,7 +54,7 @@
"tunnel_with_rocks_stage": "assets/tunnel_with_rocks_stage.png" "tunnel_with_rocks_stage": "assets/tunnel_with_rocks_stage.png"
}, },
"worldgen": { "worldgen": {
"enemy_probability": 80, "enemy_probability": 30,
"empty_room_probability": 10, "empty_room_probability": 10,
"device_probability": 10 "device_probability": 10
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 316 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 52 KiB

@ -43,18 +43,14 @@ namespace lel {
for(auto& row : grid) { for(auto& row : grid) {
size_t columns = row.size(); size_t columns = row.size();
int cell_width = grid_w / columns;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
for(auto& name : row) { for(auto& name : row) {
if(name == "_") continue; if(name == "_") continue;
auto& cell = cells.at(name); auto& cell = cells.at(name);
int cell_width = grid_w / columns;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
// ZED: getting a bit hairy but this should work // ZED: getting a bit hairy but this should work
if(cell.percent) { if(cell.percent) {
// when percent mode we have to take unset to 100% // when percent mode we have to take unset to 100%
@ -73,6 +69,9 @@ namespace lel {
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
// keep the midpoint since it is used a lot // keep the midpoint since it is used a lot
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w); cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h); cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);

@ -2,41 +2,49 @@
#include "textures.hpp" #include "textures.hpp"
#include "sound.hpp" #include "sound.hpp"
#include "autowalker.hpp" #include "autowalker.hpp"
#include <iostream>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
textures::init(); try {
sound::init(); textures::init();
sound::mute(false); sound::init();
gui::FSM main; sound::mute(true);
main.event(gui::Event::STARTED); gui::FSM main;
Autowalker walker(main); main.event(gui::Event::STARTED);
Autowalker walker(main);
sound::play("ambient_1", true); sound::play("ambient_1", true);
if(argc > 1 && argv[1][0] == 't') { if(argc > 1 && argv[1][0] == 't') {
walker.start_autowalk(); walker.start_autowalk();
} }
while(main.active()) { while(main.active()) {
main.render(); main.render();
// ZED: need to sort out how to deal with this in the FSM // ZED: need to sort out how to deal with this in the FSM
if(main.in_state(gui::State::IDLE) if(main.in_state(gui::State::IDLE)
|| main.in_state(gui::State::NEXT_LEVEL) || main.in_state(gui::State::NEXT_LEVEL)
|| main.in_state(gui::State::MAPPING) || main.in_state(gui::State::MAPPING)
|| main.in_state(gui::State::IN_COMBAT)) || main.in_state(gui::State::IN_COMBAT))
{ {
if(main.autowalking) { if(main.autowalking) {
walker.autowalk(); walker.autowalk();
} else { } else {
main.keyboard_mouse(); main.keyboard_mouse();
}
} else{
main.event(gui::Event::TICK);
} }
} else{
main.event(gui::Event::TICK); main.handle_world_events();
} }
main.handle_world_events(); return 0;
} catch(const std::system_error& e) {
std::cout << "WARNING: On OSX you'll get this error on shutdown.\n";
std::cout << "Caught system_error with code "
"[" << e.code() << "] meaning "
"[" << e.what() << "]\n";
} }
return 0;
} }

@ -128,9 +128,11 @@ namespace gui {
std::optional<Point> MainUI::play_move() { std::optional<Point> MainUI::play_move() {
if($camera.play_move($rayview)) { if($camera.play_move($rayview)) {
$needs_render = false; $needs_render = false;
return std::make_optional<Point>({ Point pos{
size_t($camera.target_x), size_t($camera.target_x),
size_t($camera.target_y)}); size_t($camera.target_y)};
return std::make_optional<Point>(pos);
} else { } else {
$needs_render = true; $needs_render = true;
return std::nullopt; return std::nullopt;

@ -20,7 +20,7 @@ namespace matrix {
} else if(cell == WALL_PATH_LIMIT) { } else if(cell == WALL_PATH_LIMIT) {
print("# "); print("# ");
} else if(cell > 15) { } else if(cell > 15) {
print("* "); print("[{:x}]", cell);
} else { } else {
print("{:x} ", cell); print("{:x} ", cell);
} }

@ -5,19 +5,51 @@ project('raycaster', 'cpp',
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1', 'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',
]) ])
add_global_link_arguments(
'-static-libgcc',
'-static-libstdc++',
'-static',
language: 'cpp',
)
# use this for common options only for our executables # use this for common options only for our executables
cpp_args=[] cpp_args=[]
link_args=[]
# these are passed as override_defaults # these are passed as override_defaults
exe_defaults = ['warning_level=2', 'werror=true'] exe_defaults = [ 'warning_level=2' ]
cc = meson.get_compiler('cpp') cc = meson.get_compiler('cpp')
dependencies = []
if build_machine.system() == 'windows'
add_global_link_arguments(
'-static-libgcc',
'-static-libstdc++',
'-static',
language: 'cpp',
)
sfml_main = dependency('sfml_main')
opengl32 = cc.find_library('opengl32', required: true)
winmm = cc.find_library('winmm', required: true)
gdi32 = cc.find_library('gdi32', required: true)
dependencies += [
opengl32, winmm, gdi32, sfml_main
]
exe_defaults += ['werror=true']
elif build_machine.system() == 'darwin'
add_global_link_arguments(
language: 'cpp',
)
opengl = dependency('OpenGL')
corefoundation = dependency('CoreFoundation')
carbon = dependency('Carbon')
cocoa = dependency('Cocoa')
iokit = dependency('IOKit')
corevideo = dependency('CoreVideo')
link_args += ['-ObjC']
exe_defaults += ['werror=false']
dependencies += [
opengl, corefoundation, carbon, cocoa, iokit, corevideo
]
endif
catch2 = dependency('catch2-with-main') catch2 = dependency('catch2-with-main')
fmt = subproject('fmt').get_variable('fmt_dep') fmt = subproject('fmt').get_variable('fmt_dep')
@ -33,12 +65,14 @@ sfml_audio = dependency('sfml_audio')
sfml_graphics = dependency('sfml_graphics') sfml_graphics = dependency('sfml_graphics')
sfml_network = dependency('sfml_network') sfml_network = dependency('sfml_network')
sfml_system = dependency('sfml_system') sfml_system = dependency('sfml_system')
sfml_window = dependency('sfml_window') sfml_window = dependency('sfml_window',
default_options: ['default_library=shared'])
ftxui_screen = dependency('ftxui-screen') ftxui_screen = dependency('ftxui-screen')
ftxui_dom = dependency('ftxui-dom') ftxui_dom = dependency('ftxui-dom')
ftxui_component = dependency('ftxui-component') ftxui_component = dependency('ftxui-component')
dependencies = [ dependencies += [
fmt, json, freetype2, fmt, json, freetype2,
flac, ogg, vorbis, vorbisfile, vorbisenc, flac, ogg, vorbis, vorbisfile, vorbisenc,
sfml_audio, sfml_graphics, sfml_audio, sfml_graphics,
@ -46,16 +80,6 @@ dependencies = [
sfml_window, ftxui_screen, ftxui_dom, ftxui_component sfml_window, ftxui_screen, ftxui_dom, ftxui_component
] ]
if build_machine.system() == 'windows'
sfml_main = dependency('sfml_main')
opengl32 = cc.find_library('opengl32', required: true)
winmm = cc.find_library('winmm', required: true)
gdi32 = cc.find_library('gdi32', required: true)
dependencies += [
opengl32, winmm, gdi32, sfml_main
]
endif
sources = [ sources = [
@ -96,6 +120,14 @@ sources = [
'textures.cpp', 'textures.cpp',
'tilemap.cpp', 'tilemap.cpp',
'worldbuilder.cpp', 'worldbuilder.cpp',
'goap2/Action.cpp',
'goap2/Action.h',
'goap2/Node.cpp',
'goap2/Node.h',
'goap2/Planner.cpp',
'goap2/Planner.h',
'goap2/WorldState.cpp',
'goap2/WorldState.h',
] ]
executable('runtests', sources + [ executable('runtests', sources + [
@ -106,6 +138,7 @@ executable('runtests', sources + [
'tests/dbc.cpp', 'tests/dbc.cpp',
'tests/dinkyecs.cpp', 'tests/dinkyecs.cpp',
'tests/fsm.cpp', 'tests/fsm.cpp',
'tests/goap.cpp',
'tests/guecs.cpp', 'tests/guecs.cpp',
'tests/inventory.cpp', 'tests/inventory.cpp',
'tests/lel.cpp', 'tests/lel.cpp',
@ -118,12 +151,16 @@ executable('runtests', sources + [
'tests/spatialmap.cpp', 'tests/spatialmap.cpp',
'tests/textures.cpp', 'tests/textures.cpp',
'tests/tilemap.cpp', 'tests/tilemap.cpp',
], override_options: exe_defaults, ],
cpp_args: cpp_args,
link_args: link_args,
override_options: exe_defaults,
dependencies: dependencies + [catch2]) dependencies: dependencies + [catch2])
executable('zedcaster', executable('zedcaster',
sources + [ 'main.cpp' ], sources + [ 'main.cpp' ],
cpp_args: cpp_args, cpp_args: cpp_args,
link_args: link_args,
override_options: exe_defaults, override_options: exe_defaults,
dependencies: dependencies) dependencies: dependencies)

@ -7,4 +7,4 @@ mv -f packagecache ./subprojects/ && true
mkdir builddir mkdir builddir
cp wraps/*.wrap subprojects/ cp wraps/*.wrap subprojects/
# on OSX you can't do this with static # on OSX you can't do this with static
meson setup builddir meson setup --default-library=static --prefer-static builddir

@ -0,0 +1,266 @@
#include <catch2/catch_test_macros.hpp>
#include "dbc.hpp"
#include <iostream>
#include <vector>
#include "levelmanager.hpp"
#include "matrix.hpp"
#include "components.hpp"
#include <bitset>
#include <limits>
using namespace dbc;
using namespace components;
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
enum StateNames {
ENEMY_IN_RANGE,
ENEMY_DEAD,
STATE_MAX
};
using GOAPState = std::bitset<STATE_MAX>;
bool is_subset(GOAPState& source, GOAPState& target) {
GOAPState result = source & target;
std::cout << "IS_SUBSET: source: " << source << " target: " << target << " result: " << result << " is it? " << (result == target) << std::endl;
return result == target;
}
struct Action {
std::string name;
int cost = 0;
std::unordered_map<StateNames, bool> preconds;
std::unordered_map<StateNames, bool> effects;
bool can_effect(GOAPState& state) {
for(auto [name, setting] : preconds) {
if(state[name] != setting) return false;
}
return true;
}
GOAPState apply_effect(GOAPState& state) {
// RCR SUGGEST: state = (state & ~write_mask) | effect
auto state_cp = state;
for(auto [name, setting] : effects) {
state_cp[name] = setting;
}
return state_cp;
}
bool operator==(const Action& other) const {
return other.name == name;
}
};
template<> struct std::hash<Action> {
size_t operator()(const Action& p) const {
return std::hash<std::string>{}(p.name);
}
};
struct ActionState {
Action action;
GOAPState state;
bool operator==(const ActionState& other) const {
return other.action == action && other.state == state;
}
};
template<> struct std::hash<ActionState> {
size_t operator()(const ActionState& p) const {
return std::hash<Action>{}(p.action) ^ std::hash<GOAPState>{}(p.state);
}
};
using AStarPath = std::deque<Action>;
int distance_to_goal(GOAPState& from, GOAPState& to) {
auto result = from ^ to;
return result.count();
}
AStarPath reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
AStarPath total_path{current};
while(came_from.contains(current)) {
current = came_from[current];
total_path.push_front(current);
}
return total_path;
}
inline int h(GOAPState& start, GOAPState& goal) {
return distance_to_goal(start, goal);
}
inline int d(GOAPState& start, GOAPState& goal) {
return distance_to_goal(start, goal);
}
inline ActionState find_lowest(std::unordered_map<ActionState, int>& open_set) {
dbc::check(!open_set.empty(), "open set can't be empty in find_lowest");
ActionState result;
int lowest_score = SCORE_MAX;
for(auto [as, score] : open_set) {
if(score < lowest_score) {
lowest_score = score;
result = as;
}
}
return result;
}
std::optional<Action> first_action(std::vector<Action>& actions, GOAPState& start) {
Action start_action;
for(auto& action : actions) {
if(action.can_effect(start)) {
return std::make_optional<Action>(action);
}
}
return std::nullopt;
}
// map is the list of possible actions
// start and goal are two world states
std::optional<AStarPath> plan_actions(std::vector<Action>& actions, GOAPState& start, GOAPState& goal) {
std::unordered_map<ActionState, int> open_set;
std::unordered_map<Action, Action> came_from;
std::unordered_map<GOAPState, int> g_score;
auto start_action = first_action(actions, start);
dbc::check(start_action != std::nullopt, "no action can start");
ActionState start_state{*start_action, start};
g_score[start] = 0;
open_set[start_state] = g_score[start] + h(start, goal);
while(!open_set.empty()) {
auto current = find_lowest(open_set);
if(current.state == goal) {
return std::make_optional<AStarPath>(reconstruct_path(came_from, current.action));
}
open_set.erase(current);
for(auto& neighbor_action : actions) {
// calculate the GOAPState being current/neighbor
if(!neighbor_action.can_effect(current.state)) continue;
auto neighbor = neighbor_action.apply_effect(current.state);
int d_score = d(current.state, neighbor);
int tentative_g_score = g_score[current.state] + d_score;
int neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : SCORE_MAX;
if(tentative_g_score < neighbor_g_score) {
// action attached?
came_from[neighbor_action] = current.action;
g_score[neighbor] = tentative_g_score;
// open_set gets the fScore
ActionState neighbor_as{neighbor_action, neighbor};
open_set[neighbor_as] = tentative_g_score + h(neighbor, goal);
}
}
}
return std::nullopt;
}
TEST_CASE("worldstate works", "[goap]") {
GOAPState goal;
GOAPState start;
std::vector<Action> actions;
// start off enemy not dead and not in range
start[ENEMY_DEAD] = false;
start[ENEMY_IN_RANGE] = false;
// end goal is enemy is dead
goal[ENEMY_DEAD] = true;
Action move_closer;
move_closer.name = "move_closer";
move_closer.cost = 10;
move_closer.preconds[ENEMY_IN_RANGE] = false;
move_closer.effects[ENEMY_IN_RANGE] = true;
REQUIRE(move_closer.can_effect(start));
auto after_move_state = move_closer.apply_effect(start);
REQUIRE(start[ENEMY_IN_RANGE] == false);
REQUIRE(after_move_state[ENEMY_IN_RANGE] == true);
REQUIRE(after_move_state[ENEMY_DEAD] == false);
// start is clean but after move is dirty
REQUIRE(move_closer.can_effect(start));
REQUIRE(!move_closer.can_effect(after_move_state));
REQUIRE(distance_to_goal(start, after_move_state) == 1);
Action kill_it;
kill_it.name = "kill_it";
kill_it.cost = 10;
kill_it.preconds[ENEMY_DEAD] = false;
kill_it.effects[ENEMY_DEAD] = true;
REQUIRE(kill_it.can_effect(start));
REQUIRE(kill_it.can_effect(after_move_state));
auto after_kill_state = kill_it.apply_effect(after_move_state);
REQUIRE(!kill_it.can_effect(after_kill_state));
REQUIRE(distance_to_goal(after_move_state, after_kill_state) == 1);
actions.push_back(kill_it);
actions.push_back(move_closer);
REQUIRE(start != goal);
}
TEST_CASE("basic feature tests", "[goap]") {
GOAPState goal;
GOAPState start;
std::vector<Action> actions;
// start off enemy not dead and not in range
start[ENEMY_DEAD] = false;
start[ENEMY_IN_RANGE] = false;
// end goal is enemy is dead
goal[ENEMY_DEAD] = true;
Action move_closer;
move_closer.name = "move_closer";
move_closer.cost = 10;
move_closer.preconds[ENEMY_IN_RANGE] = false;
move_closer.effects[ENEMY_IN_RANGE] = true;
Action kill_it;
kill_it.name = "kill_it";
kill_it.cost = 10;
kill_it.preconds[ENEMY_DEAD] = false;
kill_it.effects[ENEMY_DEAD] = true;
actions.push_back(kill_it);
actions.push_back(move_closer);
auto result = plan_actions(actions, start, goal);
REQUIRE(result != std::nullopt);
for(auto& action : *result) {
fmt::println("ACTION: {}", action.name);
}
}
Loading…
Cancel
Save