The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
aspect.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  * @file
17  */
18 
19 #include "ai/composite/aspect.hpp"
20 #include "ai/manager.hpp"
21 #include "log.hpp"
22 
23 namespace ai {
24 
25 static lg::log_domain log_ai_aspect("ai/aspect");
26 #define DBG_AI_ASPECT LOG_STREAM(debug, log_ai_aspect)
27 #define LOG_AI_ASPECT LOG_STREAM(info, log_ai_aspect)
28 #define WRN_AI_ASPECT LOG_STREAM(warn, log_ai_aspect)
29 #define ERR_AI_ASPECT LOG_STREAM(err, log_ai_aspect)
30 
31 aspect::aspect(readonly_context &context, const config &cfg, const std::string &id):
32  time_of_day_(cfg["time_of_day"]),turns_(cfg["turns"]),
33  valid_(false), valid_variant_(false), valid_lua_(false), cfg_(cfg),
34  invalidate_on_turn_start_(cfg["invalidate_on_turn_start"].to_bool(true)),
35  invalidate_on_tod_change_(cfg["invalidate_on_tod_change"].to_bool(true)),
36  invalidate_on_gamestate_change_(cfg["invalidate_on_gamestate_change"].to_bool()),
37  invalidate_on_minor_gamestate_change_(cfg["invalidate_on_minor_gamestate_change"].to_bool()),
38  engine_(cfg["engine"]), name_(cfg["name"]), id_(id)
39  {
40  DBG_AI_ASPECT << "creating new aspect: engine=["<<engine_<<"], name=["<<name_<<"], id=["<<id_<<"]"<< std::endl;
42  redeploy(cfg,id);
43  DBG_AI_ASPECT << "aspect has time_of_day=["<<time_of_day_<<"], turns=["<<turns_<<"]" << std::endl;
44  }
45 
46 
48  {
51  }
54  }
57  }
59  ///@todo 1.9 add minor_gamestate_change_observer
60  //manager::remove_minor_gamestate_observer(this);
61  }
62  }
63 
65 {
66  return log_ai_aspect;
67 }
68 
70 {
71 }
72 
73 bool aspect::redeploy(const config &cfg, const std::string& /*id*/)
74 {
77  }
80  }
83  }
85  ///@todo 1.9 add minor_gamestate_change_observer
86  //manager::remove_minor_gamestate_observer(this);
87  }
88 
89  valid_ = false;
90  valid_variant_ =false;
91  valid_lua_ = false;
92  cfg_ = cfg;
93  invalidate_on_turn_start_ = cfg["invalidate_on_turn_start"].to_bool(true);
94  invalidate_on_tod_change_ = cfg["invalidate_on_tod_change"].to_bool(true);
95  invalidate_on_gamestate_change_ = cfg["invalidate_on_gamestate_change"].to_bool();
96  invalidate_on_minor_gamestate_change_ = cfg["invalidate_on_minor_gamestate_change"].to_bool();
97  engine_ = cfg["engine"].str();
98  name_ = cfg["name"].str();
99  id_ = cfg["id"].str();
100  DBG_AI_ASPECT << "redeploying aspect: engine=["<<engine_<<"], name=["<<name_<<"], id=["<<id_<<"]"<< std::endl;
103  }
106  }
109  }
111  ///@todo 1.9 add minor_gamestate_change_observer
112  //manager::add_minor_gamestate_observer(this);
113  }
114  return true;
115 }
116 
118 {
119  config cfg;
120  cfg["invalidate_on_turn_start"] = invalidate_on_turn_start_;
121  cfg["invalidate_on_tod_change"] = invalidate_on_tod_change_;
122  cfg["invalidate_on_gamestate_change"] = invalidate_on_gamestate_change_;
123  cfg["invalidate_on_minor_gamestate_change"] = invalidate_on_minor_gamestate_change_;
124  if (!time_of_day_.empty()) {
125  cfg["time_of_day"] = time_of_day_;
126  }
127  if (!turns_.empty()) {
128  cfg["turns"] = turns_;
129  }
130  cfg["engine"] = engine_;
131  cfg["name"] = name_;
132  cfg["id"] = id_;
133  return cfg;
134 }
135 
136 bool aspect::active() const
137 {
138  return this->is_active(time_of_day_,turns_);
139 }
140 
142 {
143  return false;
144 }
145 
147  : name_(name)
148 {
149 }
150 
152 {
153  return name_;
154 }
155 
157 {
158 }
159 
161 {
162  if (s.find_first_of('"') == std::string::npos) {
163  return '"' + s + '"';
164  } else if (s.find_first_of("'") == std::string::npos) {
165  return "'" + s + "'";
166  } else {
167  return "[=====[" + s + "]=====]";
168  }
169 }
170 
171 // This is defined in the source file so that it can easily access the logger
173 {
174  if (get_list().find(name) != get_list().end()) {
175  ERR_AI_ASPECT << "Error: Attempt to double-register aspect " << name << std::endl;
176  return true;
177  }
178  return false;
179 }
180 
181 } //end of namespace ai
virtual config to_config() const
Definition: aspect.cpp:117
bool invalidate_on_tod_change_
Definition: aspect.hpp:106
static lg::log_domain log_ai_aspect("ai/aspect")
bool valid_variant_
Definition: aspect.hpp:101
std::string id_
Definition: formula.cpp:636
const std::string & get_name() const
Definition: aspect.cpp:151
std::string turns_
Definition: aspect.hpp:98
bool is_duplicate(const std::string &name)
Definition: aspect.cpp:172
virtual ~aspect()
Definition: aspect.cpp:47
bool invalidate_on_minor_gamestate_change_
Definition: aspect.hpp:108
static void add_turn_started_observer(events::observer *event_observer)
Adds an observer of 'ai_turn_started' event.
Definition: manager.cpp:408
virtual bool redeploy(const config &cfg, const std::string &id)
Definition: aspect.cpp:73
std::string time_of_day_
Definition: aspect.hpp:97
bool valid_lua_
Definition: aspect.hpp:102
static void add_tod_changed_observer(events::observer *event_observer)
Adds an observer of 'ai_tod_changed' event.
Definition: manager.cpp:385
virtual bool delete_all_facets()
Definition: aspect.cpp:141
virtual bool active() const
Definition: aspect.cpp:136
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
static void remove_gamestate_observer(events::observer *event_observer)
Removes an observer of game events except ai_user_interact event and ai_sync_network event...
Definition: manager.cpp:378
std::string name_
Definition: aspect.hpp:110
GLuint GLuint end
Definition: glew.h:1221
static void remove_turn_started_observer(events::observer *event_observer)
Deletes an observer of 'ai_turn_started' event.
Definition: manager.cpp:426
static lg::log_domain & log()
Definition: aspect.cpp:64
static std::string quote_string(const std::string &s)
Definition: aspect.cpp:160
known_aspect(const std::string &name)
Definition: aspect.cpp:146
const std::string name_
Definition: aspect.hpp:212
Managing the AIs lifecycle - headers.
std::string id_
Definition: aspect.hpp:111
virtual bool is_active(const std::string &time_of_day, const std::string &turns) const
Definition: contexts.hpp:891
aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.cpp:31
bool invalidate_on_turn_start_
Definition: aspect.hpp:105
virtual void on_create()
Definition: aspect.cpp:69
static void remove_tod_changed_observer(events::observer *event_observer)
Deletes an observer of 'ai_tod_changed' event.
Definition: manager.cpp:390
GLuint const GLchar * name
Definition: glew.h:1782
bool valid_
Definition: aspect.hpp:100
#define ERR_AI_ASPECT
Definition: aspect.cpp:29
#define DBG_AI_ASPECT
Definition: aspect.cpp:26
bool find(E event, F functor)
Tests whether an event handler is available.
static void add_gamestate_observer(events::observer *event_observer)
Adds observer of game events except ai_user_interact event and ai_sync_network event.
Definition: manager.cpp:371
static factory_map & get_list()
Definition: aspect.hpp:470
Standard logging facilities (interface).
config cfg_
Definition: aspect.hpp:104
std::string engine_
Definition: aspect.hpp:109
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual ~known_aspect()
Definition: aspect.cpp:156
GLdouble s
Definition: glew.h:1358
bool invalidate_on_gamestate_change_
Definition: aspect.hpp:107
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void init_readonly_context_proxy(readonly_context &target)
Definition: contexts.hpp:521