The Battle for Wesnoth  1.17.0-dev
stage_side_formulas.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Defines formula ai side formulas stage
18  */
19 
21 #include "ai/formula/ai.hpp"
22 
23 #include "formula/formula.hpp"
24 #include "formula/function.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" << std::endl;
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
Defines formula ai.
wfl::const_formula_ptr move_formula_
void handle_exception(const wfl::formula_error &e) const
Definition: ai.cpp:116
void on_create()
Initialization.
void append(const config &cfg)
Append data from another config object to this one.
Definition: config.cpp:285
std::string filename
Definition: formula.hpp:107
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:60
bool do_play_stage()
Play the turn - implementation.
formula_callable_ptr fake_ptr()
Definition: callable.hpp:41
config to_config() const
serialize
virtual config to_config() const
serialize
Definition: stage.cpp:66
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:142
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
wfl::variant make_action(wfl::const_formula_ptr formula_, const wfl::formula_callable &variables)
Definition: ai.cpp:185
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:265