Trying out Ragel's state machine generation as an alternative to the DinkyFSM style.

master
Zed A. Shaw 5 days ago
parent 9468990f76
commit 7fc32b0248
  1. 2
      .vimrc_proj
  2. 8
      Makefile
  3. 4
      assets/devices.json
  4. 14
      gui/dnd_events.hpp
  5. 18
      gui/dnd_loot.cpp
  6. 230
      gui/dnd_loot_2.cpp
  7. 148
      gui/dnd_loot_2.rl
  8. 16
      gui/fsm.cpp
  9. 1
      meson.build
  10. 2
      systems.cpp

@ -1 +1 @@
set makeprg=meson\ compile\ -C\ . set makeprg=make\ -f\ ../Makefile\ build

@ -1,3 +1,5 @@
ROOT_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
all: build test all: build test
reset: reset:
@ -8,10 +10,10 @@ else
endif endif
%.cpp : %.rl %.cpp : %.rl
ragel -o $@ $< ragel -G1 -o $@ $<
build: build: $(ROOT_DIR)/gui/dnd_loot_2.cpp
meson compile -j 10 -C builddir meson compile -j 10 -C $(ROOT_DIR)/builddir
release_build: release_build:
meson --wipe builddir -Db_ndebug=true --buildtype release meson --wipe builddir -Db_ndebug=true --buildtype release

@ -61,13 +61,13 @@
{"_type": "Sprite", "name": "barrel_small", "width": 256, "height": 256, "scale": 1.0}, {"_type": "Sprite", "name": "barrel_small", "width": 256, "height": 256, "scale": 1.0},
{"_type": "Sound", "attack": "pickup", "death": "blank"} {"_type": "Sound", "attack": "pickup", "death": "blank"}
], ],
"inventory_count": 1 "inventory_count": 0
}, },
"GRAVE_STONE": { "GRAVE_STONE": {
"id": "GRAVE_STONE", "id": "GRAVE_STONE",
"name": "Grave Stone", "name": "Grave Stone",
"description": "Something died here. Was this your doing?", "description": "Something died here. Was this your doing?",
"inventory_count": 1, "inventory_count": 0,
"components": [ "components": [
{"_type": "Tile", "display": 8687, {"_type": "Tile", "display": 8687,
"foreground": [32, 123, 164], "foreground": [32, 123, 164],

@ -0,0 +1,14 @@
#pragma once
enum class DNDEvent {
STARTED=0,
LOOT_OPEN=14,
LOOT_ITEM=15,
LOOT_SELECT=16,
INV_SELECT=17,
MOUSE_CLICK=19,
MOUSE_MOVE=20,
MOUSE_DRAG=21,
MOUSE_DRAG_START=22,
MOUSE_DROP=23
};

@ -10,6 +10,7 @@ namespace gui {
$window(window), $window(window),
$router(router) $router(router)
{ {
event(Event::STARTED);
} }
bool DNDLoot::event(Event ev, std::any data) { bool DNDLoot::event(Event ev, std::any data) {
@ -28,7 +29,7 @@ namespace gui {
void DNDLoot::START(Event ev) { void DNDLoot::START(Event ev) {
dbc::check(ev == Event::STARTED, "START not given a STARTED event."); dbc::check(ev == Event::STARTED, "START not given a STARTED event.");
state(DNDState::LOOTING); state(DNDState::END);
} }
void DNDLoot::LOOTING(Event ev, std::any data) { void DNDLoot::LOOTING(Event ev, std::any data) {
@ -142,9 +143,22 @@ namespace gui {
} }
void DNDLoot::END(Event ev) { void DNDLoot::END(Event ev) {
dbc::check(ev == Event::STARTED, "END not given a STARTED event."); using enum Event;
switch(ev) {
case LOOT_ITEM:
$loot_ui.active = true;
$grab_source = std::nullopt;
state(DNDState::LOOTING);
break;
case LOOT_OPEN:
$loot_ui.active = true;
$grab_source = std::nullopt; $grab_source = std::nullopt;
state(DNDState::LOOTING); state(DNDState::LOOTING);
break;
default:
dbc::sentinel(fmt::format("invalid event: {}", int(ev)));
}
} }
sf::Vector2f DNDLoot::mouse_position() { sf::Vector2f DNDLoot::mouse_position() {

@ -0,0 +1,230 @@
#line 1 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
#include <iostream>
#include "gui/dnd_events.hpp"
#include <any>
#include "gui/guecstra.hpp"
#include "gui/uisystems.hpp"
#include <guecs/ui.hpp>
#include "gui/status_ui.hpp"
#include "gui/loot_ui.hpp"
#include "gui/event_router.hpp"
#line 95 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
#line 15 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.cpp"
static const int DNDLoot_start = 1;
static const int DNDLoot_first_final = 6;
static const int DNDLoot_error = 0;
static const int DNDLoot_en_main = 1;
static const int DNDLoot_en_main_looting = 2;
#line 98 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
namespace gui {
class DNDLoot2 {
public:
std::optional<guecs::Entity> $grab_source = std::nullopt;
StatusUI& $status_ui;
LootUI& $loot_ui;
sf::RenderWindow& $window;
routing::Router& $router;
DNDLoot2(StatusUI& status_ui,
LootUI& loot_ui, sf::RenderWindow& window,
routing::Router& router);
bool event(DNDEvent ev, std::any data={});
void mouse_action(bool hover);
sf::Vector2f mouse_position();
};
sf::Vector2f DNDLoot2::mouse_position() {
return $window.mapPixelToCoords($router.position);
}
void DNDLoot2::mouse_action(bool hover) {
sf::Vector2f pos = mouse_position();
$status_ui.mouse(pos.x, pos.y, hover);
if($loot_ui.active) $loot_ui.mouse(pos.x, pos.y, hover);
}
DNDLoot2::DNDLoot2(StatusUI& status_ui, LootUI& loot_ui, sf::RenderWindow &window, routing::Router& router) :
$status_ui(status_ui),
$loot_ui(loot_ui),
$window(window),
$router(router)
{
event(DNDEvent::STARTED);
}
bool DNDLoot2::event(DNDEvent event, std::any data) {
int cs = 0;
int *p = (int *)&event;
int *pe = p+1;
#line 67 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.cpp"
{
cs = DNDLoot_start;
}
#line 144 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
#line 70 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.cpp"
{
if ( p == pe )
goto _test_eof;
if ( cs == 0 )
goto _out;
_resume:
switch ( cs ) {
case 1:
if ( (*p) == 0 )
goto tr0;
goto tr1;
case 0:
goto _out;
case 2:
switch( (*p) ) {
case 14: goto tr2;
case 16: goto tr3;
case 17: goto tr4;
case 19: goto tr5;
}
if ( (*p) > 21 ) {
if ( 22 <= (*p) && (*p) <= 23 )
goto tr5;
} else if ( (*p) >= 20 )
goto tr6;
goto tr1;
case 3:
switch( (*p) ) {
case 14: goto tr7;
case 16: goto tr8;
case 17: goto tr9;
case 19: goto tr10;
}
if ( (*p) > 21 ) {
if ( 22 <= (*p) && (*p) <= 23 )
goto tr10;
} else if ( (*p) >= 20 )
goto tr11;
goto tr1;
case 4:
switch( (*p) ) {
case 14: goto tr0;
case 15: goto tr3;
}
goto tr1;
case 5:
switch( (*p) ) {
case 14: goto tr12;
case 16: goto tr13;
case 17: goto tr14;
case 19: goto tr15;
}
if ( (*p) > 21 ) {
if ( 22 <= (*p) && (*p) <= 23 )
goto tr15;
} else if ( (*p) >= 20 )
goto tr16;
goto tr1;
}
tr1: cs = 0; goto _again;
tr0: cs = 2; goto _again;
tr2: cs = 2; goto f0;
tr8: cs = 2; goto f1;
tr14: cs = 2; goto f2;
tr5: cs = 2; goto f3;
tr6: cs = 2; goto f4;
tr9: cs = 2; goto f5;
tr13: cs = 2; goto f6;
tr3: cs = 3; goto f1;
tr10: cs = 3; goto f3;
tr11: cs = 3; goto f4;
tr12: cs = 4; goto f0;
tr7: cs = 4; goto f1;
tr4: cs = 5; goto f2;
tr15: cs = 5; goto f3;
tr16: cs = 5; goto f4;
f0:
#line 18 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
$loot_ui.active = false;
}
goto _again;
f1:
#line 22 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
// NOTE: when grab_source could work to do the if that was here
$grab_source = UISystem::loot_grab($loot_ui.$gui, data);
}
goto _again;
f2:
#line 27 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
$grab_source = UISystem::loot_grab($status_ui.$gui, data);
}
goto _again;
f6:
#line 31 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
if(UISystem::loot_drop($status_ui.$gui,
$loot_ui.$gui, $grab_source, data))
{
cs = 2;
}
}
goto _again;
f5:
#line 39 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
if(UISystem::loot_drop($loot_ui.$gui,
$status_ui.$gui, $grab_source, data))
{
cs = 2;
}
}
goto _again;
f3:
#line 51 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
mouse_action(false);
}
goto _again;
f4:
#line 55 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
{
if($grab_source) {
auto& source = $loot_ui.$gui.get<guecs::GrabSource>(*$grab_source);
source.move($window.mapPixelToCoords($router.position));
}
mouse_action(true);
}
goto _again;
_again:
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
_out: {}
}
#line 145 "C:/Users/lcthw/Projects/raycaster//gui/dnd_loot_2.rl"
return false;
}
}

@ -0,0 +1,148 @@
#include <iostream>
#include "gui/dnd_events.hpp"
#include <any>
#include "gui/guecstra.hpp"
#include "gui/uisystems.hpp"
#include <guecs/ui.hpp>
#include "gui/status_ui.hpp"
#include "gui/loot_ui.hpp"
#include "gui/event_router.hpp"
%%{
machine DNDLoot;
alphtype int;
import "../gui/dnd_events.hpp";
action loot_close {
$loot_ui.active = false;
}
action loot_grab {
// NOTE: when grab_source could work to do the if that was here
$grab_source = UISystem::loot_grab($loot_ui.$gui, data);
}
action inv_grab {
$grab_source = UISystem::loot_grab($status_ui.$gui, data);
}
action loot_drop {
if(UISystem::loot_drop($status_ui.$gui,
$loot_ui.$gui, $grab_source, data))
{
fnext looting;
}
}
action inv_drop {
if(UISystem::loot_drop($loot_ui.$gui,
$status_ui.$gui, $grab_source, data))
{
fnext looting;
}
}
action end {
$grab_source = std::nullopt;
}
action mouse_click {
mouse_action(false);
}
action mouse_move {
if($grab_source) {
auto& source = $loot_ui.$gui.get<guecs::GrabSource>(*$grab_source);
source.move($window.mapPixelToCoords($router.position));
}
mouse_action(true);
}
mouse_click = (MOUSE_DRAG_START | MOUSE_CLICK | MOUSE_DROP);
mouse_move = (MOUSE_MOVE | MOUSE_DRAG);
main :=
start: (
STARTED -> looting
),
looting: (
LOOT_OPEN @loot_close -> looting |
LOOT_SELECT @loot_grab -> loot_grab |
INV_SELECT @inv_grab -> inv_grab |
mouse_click @mouse_click -> looting |
mouse_move @mouse_move -> looting
),
loot_grab: (
LOOT_OPEN @loot_grab -> end |
LOOT_SELECT @loot_grab -> looting |
INV_SELECT @inv_drop -> looting |
mouse_click @mouse_click -> loot_grab |
mouse_move @mouse_move -> loot_grab
),
inv_grab: (
LOOT_OPEN @loot_close -> end |
LOOT_SELECT @loot_drop -> looting |
INV_SELECT @inv_grab -> looting |
mouse_click @mouse_click -> inv_grab |
mouse_move @mouse_move -> inv_grab
),
end: (
LOOT_ITEM @loot_grab -> loot_grab |
LOOT_OPEN -> looting
);
}%%
%% write data;
namespace gui {
class DNDLoot2 {
public:
std::optional<guecs::Entity> $grab_source = std::nullopt;
StatusUI& $status_ui;
LootUI& $loot_ui;
sf::RenderWindow& $window;
routing::Router& $router;
DNDLoot2(StatusUI& status_ui,
LootUI& loot_ui, sf::RenderWindow& window,
routing::Router& router);
bool event(DNDEvent ev, std::any data={});
void mouse_action(bool hover);
sf::Vector2f mouse_position();
};
sf::Vector2f DNDLoot2::mouse_position() {
return $window.mapPixelToCoords($router.position);
}
void DNDLoot2::mouse_action(bool hover) {
sf::Vector2f pos = mouse_position();
$status_ui.mouse(pos.x, pos.y, hover);
if($loot_ui.active) $loot_ui.mouse(pos.x, pos.y, hover);
}
DNDLoot2::DNDLoot2(StatusUI& status_ui, LootUI& loot_ui, sf::RenderWindow &window, routing::Router& router) :
$status_ui(status_ui),
$loot_ui(loot_ui),
$window(window),
$router(router)
{
event(DNDEvent::STARTED);
}
bool DNDLoot2::event(DNDEvent event, std::any data) {
int cs = 0;
int *p = (int *)&event;
int *pe = p+1;
%%write init;
%%write exec;
return false;
}
}

@ -116,8 +116,6 @@ namespace gui {
void FSM::LOOTING(Event ev, std::any data) { void FSM::LOOTING(Event ev, std::any data) {
if(!$dnd_loot.event(ev, data)) { if(!$dnd_loot.event(ev, data)) {
state(State::IDLE); state(State::IDLE);
} else {
dbc::log("!!!!!!!!! LOOTING ENDED!");
} }
} }
@ -168,9 +166,12 @@ namespace gui {
sound::stop("ambient"); sound::stop("ambient");
state(State::NEXT_LEVEL); state(State::NEXT_LEVEL);
break; break;
case LOOT_ITEM:
$dnd_loot.event(Event::LOOT_ITEM);
state(State::LOOTING);
break;
case LOOT_OPEN: case LOOT_OPEN:
$dnd_loot.event(Event::STARTED); $dnd_loot.event(Event::LOOT_OPEN);
$loot_ui.active = true;
state(State::LOOTING); state(State::LOOTING);
break; break;
case INV_SELECT: case INV_SELECT:
@ -457,13 +458,10 @@ namespace gui {
auto gui_id = $loot_ui.$gui.entity("item_0"); auto gui_id = $loot_ui.$gui.entity("item_0");
$loot_ui.contents.insert_or_assign(gui_id, entity); $loot_ui.contents.insert_or_assign(gui_id, entity);
$loot_ui.update(); $loot_ui.update();
event(Event::LOOT_OPEN); event(Event::LOOT_ITEM);
} break; } break;
case eGUI::LOOT_CONTAINER: { case eGUI::LOOT_CONTAINER: {
dbc::check(world.has<components::InventoryItem>(entity), dbc::log("YEP container works.");
"INVALID LOOT_ITEM, that entity has no InventoryItem");
dbc::log("everything is empty for now");
event(Event::LOOT_OPEN); event(Event::LOOT_OPEN);
} break; } break;
case eGUI::HP_STATUS: case eGUI::HP_STATUS:

@ -96,6 +96,7 @@ sources = [
'gui/combat_ui.cpp', 'gui/combat_ui.cpp',
'gui/debug_ui.cpp', 'gui/debug_ui.cpp',
'gui/dnd_loot.cpp', 'gui/dnd_loot.cpp',
'gui/dnd_loot_2.cpp',
'gui/event_router.cpp', 'gui/event_router.cpp',
'gui/fsm.cpp', 'gui/fsm.cpp',
'gui/guecstra.cpp', 'gui/guecstra.cpp',

@ -140,6 +140,7 @@ void System::motion(GameLevel &level) {
} }
void System::distribute_loot(DinkyECS::World &world, DinkyECS::Entity& ent, nlohmann::json& entity_data) { void System::distribute_loot(DinkyECS::World &world, DinkyECS::Entity& ent, nlohmann::json& entity_data) {
dbc::log("!!!!!!!!!!!!! THIS is where you update the dead body contents");
int inventory_count = entity_data["inventory_count"]; int inventory_count = entity_data["inventory_count"];
world.set<InventoryItem>(ent, {inventory_count, entity_data}); world.set<InventoryItem>(ent, {inventory_count, entity_data});
// use the inventory_level to fill the blanket with new items // use the inventory_level to fill the blanket with new items
@ -209,6 +210,7 @@ void System::death(GameLevel &level) {
auto entity_data = config.devices["GRAVE_STONE"]; auto entity_data = config.devices["GRAVE_STONE"];
components::configure_entity(world, ent, entity_data["components"]); components::configure_entity(world, ent, entity_data["components"]);
if(entity_data["inventory_count"] > 0) { if(entity_data["inventory_count"] > 0) {
dbc::sentinel("BOOM! this is where you fill in the dead bodies.");
System::distribute_loot(world, ent, entity_data); System::distribute_loot(world, ent, entity_data);
} }
} }

Loading…
Cancel
Save