Moving some stuff around before writing a test to confirm the EntityAI.
This commit is contained in:
parent
f3e157a0f7
commit
db5a371766
6 changed files with 49 additions and 38 deletions
13
ai.cpp
13
ai.cpp
|
@ -157,4 +157,17 @@ namespace ai {
|
||||||
AIProfile* profile() {
|
AIProfile* profile() {
|
||||||
return &AIMGR.profile;
|
return &AIMGR.profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool EntityAI::wants_to(std::string name) {
|
||||||
|
return plan.script[0].name == name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityAI::set_state(std::string name, bool setting) {
|
||||||
|
ai::set(start, name, setting);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityAI::update() {
|
||||||
|
plan = ai::plan(script, start, goal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
ai.hpp
20
ai.hpp
|
@ -9,6 +9,26 @@
|
||||||
#include "goap.hpp"
|
#include "goap.hpp"
|
||||||
|
|
||||||
namespace ai {
|
namespace ai {
|
||||||
|
struct EntityAI {
|
||||||
|
std::string script;
|
||||||
|
ai::State start;
|
||||||
|
ai::State goal;
|
||||||
|
ai::ActionPlan plan;
|
||||||
|
|
||||||
|
EntityAI(std::string script, ai::State start, ai::State goal) :
|
||||||
|
script(script), start(start), goal(goal)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityAI() {};
|
||||||
|
|
||||||
|
bool wants_to(std::string name);
|
||||||
|
|
||||||
|
void set_state(std::string name, bool setting);
|
||||||
|
|
||||||
|
void update();
|
||||||
|
};
|
||||||
|
|
||||||
struct AIManager {
|
struct AIManager {
|
||||||
AIProfile profile;
|
AIProfile profile;
|
||||||
|
|
||||||
|
|
|
@ -341,7 +341,7 @@ namespace gui {
|
||||||
|
|
||||||
void FSM::run_systems() {
|
void FSM::run_systems() {
|
||||||
System::generate_paths($level);
|
System::generate_paths($level);
|
||||||
System::enemy_ai($level);
|
System::enemy_ai_initialize($level);
|
||||||
System::enemy_pathing($level);
|
System::enemy_pathing($level);
|
||||||
System::collision($level);
|
System::collision($level);
|
||||||
System::motion($level);
|
System::motion($level);
|
||||||
|
|
46
systems.cpp
46
systems.cpp
|
@ -18,32 +18,6 @@ using namespace components;
|
||||||
using lighting::LightSource;
|
using lighting::LightSource;
|
||||||
using ftxui::Color;
|
using ftxui::Color;
|
||||||
|
|
||||||
struct EntityAI {
|
|
||||||
std::string script;
|
|
||||||
ai::State start;
|
|
||||||
ai::State goal;
|
|
||||||
ai::ActionPlan plan;
|
|
||||||
|
|
||||||
EntityAI(std::string script, ai::State start, ai::State goal) :
|
|
||||||
script(script), start(start), goal(goal)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityAI() {};
|
|
||||||
|
|
||||||
bool wants_to(std::string name) {
|
|
||||||
return plan.script[0].name == name;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_state(std::string name, bool setting) {
|
|
||||||
ai::set(start, name, setting);
|
|
||||||
}
|
|
||||||
|
|
||||||
void update() {
|
|
||||||
plan = ai::plan(script, start, goal);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void System::lighting(GameLevel &level) {
|
void System::lighting(GameLevel &level) {
|
||||||
auto &light = *level.lights;
|
auto &light = *level.lights;
|
||||||
auto &world = *level.world;
|
auto &world = *level.world;
|
||||||
|
@ -70,25 +44,25 @@ void System::generate_paths(GameLevel &level) {
|
||||||
level.map->make_paths();
|
level.map->make_paths();
|
||||||
}
|
}
|
||||||
|
|
||||||
void System::enemy_ai(GameLevel &level) {
|
void System::enemy_ai_initialize(GameLevel &level) {
|
||||||
auto &world = *level.world;
|
auto &world = *level.world;
|
||||||
auto &map = *level.map;
|
auto &map = *level.map;
|
||||||
|
|
||||||
world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
|
world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
|
||||||
if(world.has<EntityAI>(ent)) {
|
if(world.has<ai::EntityAI>(ent)) {
|
||||||
auto&enemy = world.get<EntityAI>(ent);
|
auto&enemy = world.get<ai::EntityAI>(ent);
|
||||||
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
|
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
|
||||||
enemy.update();
|
enemy.update();
|
||||||
} else {
|
} else {
|
||||||
auto ai_start = ai::load_state(config.ai_start_name);
|
auto ai_start = ai::load_state(config.ai_start_name);
|
||||||
auto ai_goal = ai::load_state(config.ai_goal_name);
|
auto ai_goal = ai::load_state(config.ai_goal_name);
|
||||||
|
|
||||||
EntityAI enemy(config.ai_script, ai_start, ai_goal);
|
ai::EntityAI enemy(config.ai_script, ai_start, ai_goal);
|
||||||
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
|
enemy.set_state("detect_enemy", map.distance(pos.location) < config.hearing_distance);
|
||||||
enemy.update();
|
enemy.update();
|
||||||
|
|
||||||
ai::dump_script("\n\n\n-----ENEMY SCRIPT", enemy.start, enemy.plan.script);
|
ai::dump_script("\n\n\n-----ENEMY SCRIPT", enemy.start, enemy.plan.script);
|
||||||
world.set<EntityAI>(ent, enemy);
|
world.set<ai::EntityAI>(ent, enemy);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -102,9 +76,9 @@ void System::enemy_pathing(GameLevel &level) {
|
||||||
|
|
||||||
world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
|
world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
|
||||||
if(ent != player.entity) {
|
if(ent != player.entity) {
|
||||||
auto& action = world.get<EntityAI>(ent);
|
auto& enemy_ai = world.get<ai::EntityAI>(ent);
|
||||||
|
|
||||||
if(action.wants_to("find_enemy")) {
|
if(enemy_ai.wants_to("find_enemy")) {
|
||||||
Point out = position.location; // copy
|
Point out = position.location; // copy
|
||||||
map.neighbors(out, motion.random);
|
map.neighbors(out, motion.random);
|
||||||
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
|
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
|
||||||
|
@ -187,7 +161,7 @@ void System::death(GameLevel &level, components::ComponentMap& components) {
|
||||||
world.remove<Motion>(ent);
|
world.remove<Motion>(ent);
|
||||||
world.remove<Combat>(ent);
|
world.remove<Combat>(ent);
|
||||||
world.remove<EnemyConfig>(ent);
|
world.remove<EnemyConfig>(ent);
|
||||||
world.remove<EntityAI>(ent);
|
world.remove<ai::EntityAI>(ent);
|
||||||
world.remove<Animation>(ent);
|
world.remove<Animation>(ent);
|
||||||
|
|
||||||
if(auto snd = world.get_if<Sound>(ent)) {
|
if(auto snd = world.get_if<Sound>(ent)) {
|
||||||
|
@ -217,8 +191,8 @@ void System::combat(GameLevel &level) {
|
||||||
|
|
||||||
if(found) {
|
if(found) {
|
||||||
for(auto entity : nearby) {
|
for(auto entity : nearby) {
|
||||||
if(world.has<EntityAI>(entity)) {
|
if(world.has<ai::EntityAI>(entity)) {
|
||||||
auto& enemy_ai = world.get<EntityAI>(entity);
|
auto& enemy_ai = world.get<ai::EntityAI>(entity);
|
||||||
enemy_ai.set_state("enemy_found", true);
|
enemy_ai.set_state("enemy_found", true);
|
||||||
enemy_ai.update();
|
enemy_ai.update();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace System {
|
||||||
void death(GameLevel &level, components::ComponentMap& components);
|
void death(GameLevel &level, components::ComponentMap& components);
|
||||||
void generate_paths(GameLevel &level);
|
void generate_paths(GameLevel &level);
|
||||||
void enemy_pathing(GameLevel &level);
|
void enemy_pathing(GameLevel &level);
|
||||||
void enemy_ai(GameLevel &level);
|
void enemy_ai_initialize(GameLevel &level);
|
||||||
|
|
||||||
void init_positions(DinkyECS::World &world, SpatialMap &collider);
|
void init_positions(DinkyECS::World &world, SpatialMap &collider);
|
||||||
void device(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::Entity item);
|
void device(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::Entity item);
|
||||||
|
|
|
@ -170,3 +170,7 @@ TEST_CASE("ai autowalker ai test", "[ai]") {
|
||||||
REQUIRE(ai::test(result, "enemy_dead"));
|
REQUIRE(ai::test(result, "enemy_dead"));
|
||||||
REQUIRE(ai::test(result, "no_more_enemies"));
|
REQUIRE(ai::test(result, "no_more_enemies"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Confirm EntityAI behaves as expected", "[ai]") {
|
||||||
|
// nothing yet
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue