The Battle for Wesnoth  1.17.0-dev
stage_side_formulas.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  * @file
18  * Defines formula ai side formulas stage
19  */
20 
22 #include "ai/formula/ai.hpp"
23 
24 #include "formula/formula.hpp"
25 #include "formula/function.hpp"
26 #include "log.hpp"
27 
28 static lg::log_domain log_ai("ai/stage/side_formulas");
29 #define LOG_AI LOG_STREAM(info, log_ai)
30 #define WRN_AI LOG_STREAM(warn, log_ai)
31 #define ERR_AI LOG_STREAM(err, log_ai)
32 
33 namespace ai {
34 
36  : stage(context,cfg), cfg_(cfg), fai_(fai), move_formula_()
37 {
38 
39 }
40 
42 {
43 }
44 
46 {
48  try {
49  if (move_formula_) {
50  while( !fai_.make_action(move_formula_,callable).is_empty() ) { }
51  } else {
52  WRN_AI << "Side formula skipped, maybe it's empty or incorrect" << std::endl;
53  }
54  }
55  catch(wfl::formula_error& e) {
56  if(e.filename == "formula") {
57  e.line = 0;
58  }
59  fai_.handle_exception( e, "Formula error");
60  }
61  return false;
62 }
63 
65 {
67 }
68 
70 {
71  config cfg = stage::to_config();
72  cfg.append(cfg_);
73  return cfg;
74 }
75 
76 } // end of namespace ai
Defines formula ai.
wfl::const_formula_ptr move_formula_
void handle_exception(const wfl::formula_error &e) const
Definition: ai.cpp:117
void on_create()
Initialization.
void append(const config &cfg)
Append data from another config object to this one.
Definition: config.cpp:269
std::string filename
Definition: formula.hpp:108
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
bool do_play_stage()
Play the turn - implementation.
formula_callable_ptr fake_ptr()
Definition: callable.hpp:42
config to_config() const
serialize
virtual config to_config() const
serialize
Definition: stage.cpp:67
Stage which executes side formulas.
Standard logging facilities (interface).
#define e
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:143
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable &variables)
Definition: ai.cpp:186
stage_side_formulas(ai_context &context, const config &cfg, formula_ai &fai)
#define WRN_AI
static lg::log_domain log_ai("ai/stage/side_formulas")
bool is_empty() const
Definition: variant.cpp:266