The Battle for Wesnoth  1.19.5+dev
stage_side_formulas.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  * @file
18  * Defines formula ai side formulas stage
19  */
20 
22 #include "ai/formula/ai.hpp"
23 
24 #include "formula/formula.hpp"
25 #include "log.hpp"
26 
27 static lg::log_domain log_ai("ai/stage/side_formulas");
28 #define LOG_AI LOG_STREAM(info, log_ai)
29 #define WRN_AI LOG_STREAM(warn, log_ai)
30 #define ERR_AI LOG_STREAM(err, log_ai)
31 
32 namespace ai {
33 
35  : stage(context,cfg), cfg_(cfg), fai_(fai), move_formula_()
36 {
37 
38 }
39 
41 {
42 }
43 
45 {
47  try {
48  if (move_formula_) {
49  while( !fai_.make_action(move_formula_,callable).is_empty() ) { }
50  } else {
51  WRN_AI << "Side formula skipped, maybe it's empty or incorrect";
52  }
53  }
54  catch(wfl::formula_error& e) {
55  if(e.filename == "formula") {
56  e.line = 0;
57  }
58  fai_.handle_exception( e, "Formula error");
59  }
60  return false;
61 }
62 
64 {
66 }
67 
69 {
70  config cfg = stage::to_config();
71  cfg.append(cfg_);
72  return cfg;
73 }
74 
75 } // end of namespace ai
wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable &variables)
Definition: ai.cpp:176
void handle_exception(const wfl::formula_error &e) const
Definition: ai.cpp:107
wfl::formula_ptr create_optional_formula(const std::string &formula_string) const
Create a new formula from the string, using the symbol table which is stored in the AI.
Definition: ai.cpp:133
bool do_play_stage()
Play the turn - implementation.
config to_config() const
serialize
wfl::const_formula_ptr move_formula_
stage_side_formulas(ai_context &context, const config &cfg, formula_ai &fai)
void on_create()
Initialization.
virtual config to_config() const
serialize
Definition: stage.cpp:65
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
void append(const config &cfg)
Append data from another config object to this one.
Definition: config.cpp:203
formula_callable_ptr fake_ptr()
Definition: callable.hpp:42
bool is_empty() const
Definition: variant.cpp:262
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
#define WRN_AI
static lg::log_domain log_ai("ai/stage/side_formulas")
Stage which executes side formulas.
#define e