The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rca.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://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  * Candidate actions framework
17  * @file
18  */
19 
20 #include "ai/composite/ai.hpp"
21 #include "ai/composite/engine.hpp"
22 #include "ai/composite/rca.hpp"
23 #include "log.hpp"
24 
25 namespace ai {
26 
27 static lg::log_domain log_ai_stage_rca("ai/stage/rca");
28 #define DBG_AI_STAGE_RCA LOG_STREAM(debug, log_ai_stage_rca)
29 #define LOG_AI_STAGE_RCA LOG_STREAM(info, log_ai_stage_rca)
30 #define ERR_AI_STAGE_RCA LOG_STREAM(err, log_ai_stage_rca)
31 
32 const double candidate_action::BAD_SCORE = 0;
33 const double candidate_action::HIGH_SCORE = 10000000;
34 
36  recursion_counter_(context.get_recursion_count()),
37  enabled_(cfg["enabled"].to_bool(true)), engine_(cfg["engine"]),
38  score_(cfg["score"].to_double(BAD_SCORE)),
39  max_score_(cfg["max_score"].to_double(HIGH_SCORE)),
40  id_(cfg["id"]), name_(cfg["name"]), type_(cfg["type"]), to_be_removed_(false)
41 {
42  init_rca_context_proxy(context);
43 }
44 
46 {
47 }
48 
49 
51 {
52  return enabled_;
53 }
54 
55 
57 {
58  enabled_ = true;
59 }
60 
62 {
64 }
65 
67 {
68  enabled_ = false;
69 }
70 
71 
73 {
74  return score_;
75 }
76 
77 
79 {
80  return max_score_;
81 }
82 
84 {
85  return type_;
86 }
87 
89 {
90  config cfg;
91  cfg["enabled"] = enabled_;
92  cfg["engine"] = engine_;
93  cfg["id"] = id_;
94  cfg["name"] = name_;
95  cfg["score"] = score_;
96  cfg["max_score"] = max_score_;
97  cfg["type"] = type_;
98  return cfg;
99 }
100 
102 {
103  to_be_removed_ = true;
104 }
105 
107 {
108  return to_be_removed_;
109 }
110 
111 // This is defined in the source file so that it can easily access the logger
113 {
114  if (get_list().find(name) != get_list().end()) {
115  ERR_AI_STAGE_RCA << "Error: Attempt to double-register candidate action " << name << std::endl;
116  return true;
117  }
118  return false;
119 }
120 
121 //============================================================================
122 
123 std::ostream &operator<<(std::ostream &s, ai::candidate_action const &ca) {
124  s << "candidate action with name ["<< ca.get_name() <<"]";
125  return s;
126 }
127 
128 } // of namespace ai
static factory_map & get_list()
Definition: rca.hpp:159
std::string engine_
Definition: rca.hpp:126
double max_score_
Definition: rca.hpp:132
double get_max_score() const
Get the upper bound of the score of the candidate action without re-evaluation.
Definition: rca.cpp:78
std::string id_
Definition: formula.cpp:636
static lg::log_domain log_ai_stage_rca("ai/stage/rca")
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:93
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:72
AI Support engine - creating specific ai components from config.
std::ostream & operator<<(std::ostream &s, ai::candidate_action const &ca)
Definition: rca.cpp:123
std::string name_
Definition: rca.hpp:138
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
GLuint GLuint end
Definition: glew.h:1221
bool is_enabled() const
Is this candidate action enabled ?
Definition: rca.cpp:50
recursion_counter recursion_counter_
Definition: rca.hpp:121
Composite AI with turn sequence which is a vector of stages.
void enable()
Enable the candidate action.
Definition: rca.cpp:56
virtual config to_config() const
serialize
Definition: rca.cpp:88
virtual void set_to_be_removed()
Definition: rca.cpp:101
int get_recursion_count() const
Get the value of the recursion counter.
Definition: rca.cpp:61
void init_rca_context_proxy(rca_context &target)
Definition: contexts.hpp:141
virtual bool to_be_removed()
Definition: rca.cpp:106
static const double HIGH_SCORE
Definition: rca.hpp:41
candidate_action(rca_context &context, const config &cfg)
Definition: rca.cpp:35
bool is_duplicate(const std::string &name)
Definition: rca.cpp:112
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:79
std::string id_
Definition: rca.hpp:135
std::string type_
Definition: rca.hpp:141
#define ERR_AI_STAGE_RCA
Definition: rca.cpp:30
GLuint const GLchar * name
Definition: glew.h:1782
void disable()
Disable the candidate action.
Definition: rca.cpp:66
bool find(E event, F functor)
Tests whether an event handler is available.
const std::string & get_type() const
Get the type of the candidate action (useful for debug purposes)
Definition: rca.cpp:83
virtual ~candidate_action()
Destructor.
Definition: rca.cpp:45
Standard logging facilities (interface).
static const double BAD_SCORE
Definition: rca.hpp:38
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
candidate action framework