The Battle for Wesnoth  1.19.7+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 
24 #include "ai/composite/rca.hpp"
28 #include "log.hpp"
29 #include <utility>
30 
31 namespace ai {
32 
33 static lg::log_domain log_ai_engine_fai("ai/engine/fai");
34 #define DBG_AI_ENGINE_FAI LOG_STREAM(debug, log_ai_engine_fai)
35 #define LOG_AI_ENGINE_FAI LOG_STREAM(info, log_ai_engine_fai)
36 #define ERR_AI_ENGINE_FAI LOG_STREAM(err, log_ai_engine_fai)
37 
39 public:
41  : candidate_action(context,cfg),fai_ca_(std::move(fai_ca)),formula_ai_(_formula_ai),cfg_(cfg)
42  {
43 
44 }
45 
47 
48  virtual double evaluate()
49  {
51  return fai_ca_->get_score();
52  }
53 
54  virtual void execute()
55  {
57  }
58 
59  virtual config to_config() const
60  {
61  return cfg_;
62  }
63 private:
66  const config cfg_;
67 };
68 
70  : engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
71 {
72  name_ = "fai";
73  formula_ai_->on_create();
74 }
75 
77 {
78 }
79 
80 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 ){
81  wfl::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
82  if (!fai_ca) {
83  ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]";
84  DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg;
85  return;
86  }
87  auto ca = std::make_shared<fai_candidate_action_wrapper>(context, cfg, fai_ca, *formula_ai_);
88  *b = ca;
89 
90 }
91 
92 void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
93 {
94  // This checekd for !cfg but oter implementation of do_parse_stage_from_config didn't.
95  const std::string &name = cfg["name"];
96  stage_ptr st_ptr;
97 
98  //dropped from 1.8, as it's not ready
99  //if (name=="rca_formulas") {
100  // st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
101 
102  if (name=="side_formulas") {
103  st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
104  } else if (name=="unit_formulas") {
105  st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
106  } else {
107  ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]";
108  }
109  if (st_ptr) {
110  st_ptr->on_create();
111  *b = st_ptr;
112  }
113 }
114 
115 std::string engine_fai::evaluate(const std::string &str)
116 {
117  return formula_ai_->evaluate(str);
118 }
119 
121 {
122  if (context!=nullptr) {
123  DBG_AI_ENGINE_FAI << "fai engine: ai_context is set";
124  } else {
125  DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared";
126  }
127  formula_ai_->set_ai_context(context);
128 }
129 
131 {
132  config cfg = engine::to_config();
133  cfg.add_child("formula_ai",formula_ai_->to_config());
134  return cfg;
135 }
136 
137 } //end of namespace ai
Defines formula ai candidate actions - headers.
virtual config to_config() const
serialize
Definition: engine_fai.cpp:130
virtual std::string evaluate(const std::string &str)
Definition: engine_fai.cpp:115
virtual ~engine_fai()
Definition: engine_fai.cpp:76
virtual void set_ai_context(ai_context *context)
Definition: engine_fai.cpp:120
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:80
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:92
engine_fai(readonly_context &context, const config &cfg)
Definition: engine_fai.cpp:69
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:64
fai_candidate_action_wrapper(rca_context &context, const config &cfg, wfl::candidate_action_ptr fai_ca, formula_ai &_formula_ai)
Definition: engine_fai.cpp:40
virtual config to_config() const
serialize
Definition: engine_fai.cpp:59
virtual void execute()
Execute the candidate action.
Definition: engine_fai.cpp:54
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: engine_fai.cpp:48
bool execute_candidate_action(const wfl::candidate_action_ptr &fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:664
void evaluate_candidate_action(const wfl::candidate_action_ptr &fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:658
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:172
config & add_child(config_key_type key)
Definition: config.cpp:440
#define ERR_AI_ENGINE_FAI
Definition: engine_fai.cpp:36
#define DBG_AI_ENGINE_FAI
Definition: engine_fai.cpp:34
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