The Battle for Wesnoth  1.17.0-dev
engine_fai.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
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"
24 #include "ai/formula/ai.hpp"
28 #include "log.hpp"
29 #include "units/unit.hpp"
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_(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"]<<"]"<< std::endl;
84  DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg << std::endl;
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  if (!cfg) {
95  return;
96  }
97  const std::string &name = cfg["name"];
98  stage_ptr st_ptr;
99 
100  //dropped from 1.8, as it's not ready
101  //if (name=="rca_formulas") {
102  // st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
103 
104  if (name=="side_formulas") {
105  st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
106  } else if (name=="unit_formulas") {
107  st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
108  } else {
109  ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
110  }
111  if (st_ptr) {
112  st_ptr->on_create();
113  *b = st_ptr;
114  }
115 }
116 
117 std::string engine_fai::evaluate(const std::string &str)
118 {
119  return formula_ai_->evaluate(str);
120 }
121 
123 {
124  if (context!=nullptr) {
125  DBG_AI_ENGINE_FAI << "fai engine: ai_context is set" << std::endl;
126  } else {
127  DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared" << std::endl;
128  }
129  formula_ai_->set_ai_context(context);
130 }
131 
133 {
134  config cfg = engine::to_config();
135  cfg.add_child("formula_ai",formula_ai_->to_config());
136  return cfg;
137 }
138 
139 } //end of namespace ai
Defines formula ai.
std::shared_ptr< candidate_action > candidate_action_ptr
Definition: rca.hpp:145
std::shared_ptr< stage > stage_ptr
Definition: game_info.hpp:102
Defines formula ai candidate actions - headers.
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 execute()
Execute the candidate action.
Definition: engine_fai.cpp:54
bool execute_candidate_action(wfl::candidate_action_ptr fai_ca)
Execute the fai candidate action.
Definition: ai.cpp:696
#define ERR_AI_ENGINE_FAI
Definition: engine_fai.cpp:36
FAI AI Support engine - creating specific ai components from config.
virtual config to_config() const
serialize
Definition: engine.cpp:134
Stage which executes unit formulas.
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
virtual config to_config() const
serialize
Definition: engine_fai.cpp:59
wfl::candidate_action_ptr fai_ca_
Definition: engine_fai.cpp:64
#define b
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
std::shared_ptr< formula_ai > formula_ai_
Definition: engine_fai.hpp:47
virtual std::string evaluate(const std::string &str)
Definition: engine_fai.cpp:117
virtual ~engine_fai()
Definition: engine_fai.cpp:76
void evaluate_candidate_action(wfl::candidate_action_ptr fai_ca)
Evaluate the fai candidate action.
Definition: ai.cpp:690
#define DBG_AI_ENGINE_FAI
Definition: engine_fai.cpp:34
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: engine_fai.cpp:48
engine_fai(readonly_context &context, const config &cfg)
Definition: engine_fai.cpp:69
config & add_child(config_key_type key)
Definition: config.cpp:514
Stage which executes side formulas.
static lg::log_domain log_ai_engine_fai("ai/engine/fai")
std::string name_
Definition: engine.hpp:102
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
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:132
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
readonly_context & ai_
Definition: engine.hpp:96
virtual void set_ai_context(ai_context *context)
Definition: engine_fai.cpp:122
candidate action framework