The Battle for Wesnoth  1.19.0-dev
engine_fai.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * FAI AI Support engine - creating specific ai components from config
18  * @file
19  */
20 
21 #include "ai/formula/ai.hpp"
23 #include "ai/composite/rca.hpp"
27 #include "log.hpp"
28 
29 namespace ai {
30 
31 static lg::log_domain log_ai_engine_fai("ai/engine/fai");
32 #define DBG_AI_ENGINE_FAI LOG_STREAM(debug, log_ai_engine_fai)
33 #define LOG_AI_ENGINE_FAI LOG_STREAM(info, log_ai_engine_fai)
34 #define ERR_AI_ENGINE_FAI LOG_STREAM(err, log_ai_engine_fai)
35 
37 public:
39  : candidate_action(context,cfg),fai_ca_(fai_ca),formula_ai_(_formula_ai),cfg_(cfg)
40  {
41 
42 }
43 
45 
46  virtual double evaluate()
47  {
49  return fai_ca_->get_score();
50  }
51 
52  virtual void execute()
53  {
55  }
56 
57  virtual config to_config() const
58  {
59  return cfg_;
60  }
61 private:
64  const config cfg_;
65 };
66 
68  : engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
69 {
70  name_ = "fai";
71  formula_ai_->on_create();
72 }
73 
75 {
76 }
77 
78 void engine_fai::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b ){
79  wfl::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
80  if (!fai_ca) {
81  ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]";
82  DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg;
83  return;
84  }
85  auto ca = std::make_shared<fai_candidate_action_wrapper>(context, cfg, fai_ca, *formula_ai_);
86  *b = ca;
87 
88 }
89 
90 void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
91 {
92  // This checekd for !cfg but oter implementation of do_parse_stage_from_config didn't.
93  const std::string &name = cfg["name"];
94  stage_ptr st_ptr;
95 
96  //dropped from 1.8, as it's not ready
97  //if (name=="rca_formulas") {
98  // st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
99 
100  if (name=="side_formulas") {
101  st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
102  } else if (name=="unit_formulas") {
103  st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
104  } else {
105  ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]";
106  }
107  if (st_ptr) {
108  st_ptr->on_create();
109  *b = st_ptr;
110  }
111 }
112 
113 std::string engine_fai::evaluate(const std::string &str)
114 {
115  return formula_ai_->evaluate(str);
116 }
117 
119 {
120  if (context!=nullptr) {
121  DBG_AI_ENGINE_FAI << "fai engine: ai_context is set";
122  } else {
123  DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared";
124  }
125  formula_ai_->set_ai_context(context);
126 }
127 
129 {
130  config cfg = engine::to_config();
131  cfg.add_child("formula_ai",formula_ai_->to_config());
132  return cfg;
133 }
134 
135 } //end of namespace ai
Defines formula ai candidate actions - headers.
virtual config to_config() const
serialize
Definition: engine_fai.cpp:128
virtual std::string evaluate(const std::string &str)
Definition: engine_fai.cpp:113
virtual ~engine_fai()
Definition: engine_fai.cpp:74
virtual void set_ai_context(ai_context *context)
Definition: engine_fai.cpp:118
std::shared_ptr< formula_ai > formula_ai_
Definition: engine_fai.hpp:46
virtual void do_parse_candidate_action_from_config(rca_context &context, const config &cfg, std::back_insert_iterator< std::vector< candidate_action_ptr >> b)
Definition: engine_fai.cpp:78
virtual void do_parse_stage_from_config(ai_context &context, const config &cfg, std::back_insert_iterator< std::vector< stage_ptr >> b)
Definition: engine_fai.cpp:90
engine_fai(readonly_context &context, const config &cfg)
Definition: engine_fai.cpp:67
std::string name_
Definition: engine.hpp:99
virtual config to_config() const
serialize
Definition: engine.cpp:134
readonly_context & ai_
Definition: engine.hpp:93
wfl::candidate_action_ptr fai_ca_
Definition: engine_fai.cpp:62
fai_candidate_action_wrapper(rca_context &context, const config &cfg, wfl::candidate_action_ptr fai_ca, formula_ai &_formula_ai)
Definition: engine_fai.cpp:38
virtual config to_config() const
serialize
Definition: engine_fai.cpp:57
virtual void execute()
Execute the candidate action.
Definition: engine_fai.cpp:52
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: engine_fai.cpp:46
void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:689
bool execute_candidate_action(wfl::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:695
virtual side_number get_side() const =0
Get the side number.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & add_child(config_key_type key)
Definition: config.cpp:441
#define ERR_AI_ENGINE_FAI
Definition: engine_fai.cpp:34
#define DBG_AI_ENGINE_FAI
Definition: engine_fai.cpp:32
FAI AI Support engine - creating specific ai components from config.
Defines formula ai.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:102
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
static lg::log_domain log_ai_engine_fai("ai/engine/fai")
candidate action framework
Stage which executes side formulas.
Stage which executes unit formulas.
#define b