The Battle for Wesnoth  1.15.11+dev
aspect.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  */
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  engine_(cfg["engine"]), name_(cfg["name"]), id_(id)
38  {
39  DBG_AI_ASPECT << "creating new aspect: engine=["<<engine_<<"], name=["<<name_<<"], id=["<<id_<<"]"<< std::endl;
41  redeploy(cfg,id);
42  DBG_AI_ASPECT << "aspect has time_of_day=["<<time_of_day_<<"], turns=["<<turns_<<"]" << std::endl;
43  }
44 
46  {
49  manager.remove_turn_started_observer(this);
50  }
52  manager.remove_tod_changed_observer(this);
53  }
55  manager.remove_gamestate_observer(this);
56  }
57  }
58 
60 {
61  return log_ai_aspect;
62 }
63 
65 {
66 }
67 
68 bool aspect::redeploy(const config &cfg, const std::string& /*id*/)
69 {
71 
73  manager.remove_turn_started_observer(this);
74  }
76  manager.remove_tod_changed_observer(this);
77  }
79  manager.remove_gamestate_observer(this);
80  }
81 
82  valid_ = false;
83  valid_variant_ =false;
84  valid_lua_ = false;
85  cfg_ = cfg;
86  invalidate_on_turn_start_ = cfg["invalidate_on_turn_start"].to_bool(true);
87  invalidate_on_tod_change_ = cfg["invalidate_on_tod_change"].to_bool(true);
88  invalidate_on_gamestate_change_ = cfg["invalidate_on_gamestate_change"].to_bool();
89  engine_ = cfg["engine"].str();
90  name_ = cfg["name"].str();
91  id_ = cfg["id"].str();
92  DBG_AI_ASPECT << "redeploying aspect: engine=["<<engine_<<"], name=["<<name_<<"], id=["<<id_<<"]"<< std::endl;
94  manager.add_turn_started_observer(this);
95  }
97  manager.add_tod_changed_observer(this);
98  }
100  manager.add_gamestate_observer(this);
101  }
102  return true;
103 }
104 
106 {
107  config cfg;
108  cfg["invalidate_on_turn_start"] = invalidate_on_turn_start_;
109  cfg["invalidate_on_tod_change"] = invalidate_on_tod_change_;
110  cfg["invalidate_on_gamestate_change"] = invalidate_on_gamestate_change_;
111  if (!time_of_day_.empty()) {
112  cfg["time_of_day"] = time_of_day_;
113  }
114  if (!turns_.empty()) {
115  cfg["turns"] = turns_;
116  }
117  cfg["engine"] = engine_;
118  cfg["name"] = name_;
119  cfg["id"] = id_;
120  return cfg;
121 }
122 
123 bool aspect::active() const
124 {
125  return this->is_active(time_of_day_,turns_);
126 }
127 
129 {
130  return false;
131 }
132 
133 known_aspect::known_aspect(const std::string &name)
134  : name_(name)
135 {
136 }
137 
138 const std::string& known_aspect::get_name() const
139 {
140  return name_;
141 }
142 
144 {
145 }
146 
147 std::string lua_aspect_visitor::quote_string(const std::string& s)
148 {
149  if (s.find_first_of('"') == std::string::npos) {
150  return '"' + s + '"';
151  } else if (s.find_first_of("'") == std::string::npos) {
152  return "'" + s + "'";
153  } else {
154  return "[=====[" + s + "]=====]";
155  }
156 }
157 
158 // This is defined in the source file so that it can easily access the logger
159 bool aspect_factory::is_duplicate(const std::string& name)
160 {
161  if (get_list().find(name) != get_list().end()) {
162  ERR_AI_ASPECT << "Error: Attempt to double-register aspect " << name << std::endl;
163  return true;
164  }
165  return false;
166 }
167 
168 } //end of namespace ai
bool invalidate_on_tod_change_
Definition: aspect.hpp:91
static lg::log_domain log_ai_aspect("ai/aspect")
bool valid_variant_
Definition: aspect.hpp:86
virtual bool is_active(const std::string &time_of_day, const std::string &turns) const override
Definition: contexts.hpp:755
std::string turns_
Definition: aspect.hpp:83
static manager & get_singleton()
Definition: manager.hpp:143
bool is_duplicate(const std::string &name)
Definition: aspect.cpp:159
virtual ~aspect()
Definition: aspect.cpp:45
void add_turn_started_observer(events::observer *event_observer)
Adds an observer of &#39;ai_turn_started&#39; event.
Definition: manager.cpp:368
virtual bool redeploy(const config &cfg, const std::string &id)
Definition: aspect.cpp:68
Class that manages AIs for all sides and manages AI redeployment.
Definition: manager.hpp:111
std::string time_of_day_
Definition: aspect.hpp:82
bool valid_lua_
Definition: aspect.hpp:87
void add_tod_changed_observer(events::observer *event_observer)
Adds an observer of &#39;ai_tod_changed&#39; event.
Definition: manager.cpp:350
virtual bool delete_all_facets()
Definition: aspect.cpp:128
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
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:344
std::string name_
Definition: aspect.hpp:94
void remove_turn_started_observer(events::observer *event_observer)
Deletes an observer of &#39;ai_turn_started&#39; event.
Definition: manager.cpp:383
static lg::log_domain & log()
Definition: aspect.cpp:59
static std::string quote_string(const std::string &s)
Definition: aspect.cpp:147
known_aspect(const std::string &name)
Definition: aspect.cpp:133
const std::string name_
Definition: aspect.hpp:195
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
virtual config to_config() const
Definition: aspect.cpp:105
std::string id_
Definition: aspect.hpp:95
aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.cpp:31
bool invalidate_on_turn_start_
Definition: aspect.hpp:90
const std::string & get_name() const
Definition: aspect.cpp:138
static map_location::DIRECTION s
virtual void on_create()
Definition: aspect.cpp:64
void remove_tod_changed_observer(events::observer *event_observer)
Deletes an observer of &#39;ai_tod_changed&#39; event.
Definition: manager.cpp:354
virtual bool active() const
Definition: aspect.cpp:123
bool valid_
Definition: aspect.hpp:85
#define ERR_AI_ASPECT
Definition: aspect.cpp:29
#define DBG_AI_ASPECT
Definition: aspect.cpp:26
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:338
Standard logging facilities (interface).
config cfg_
Definition: aspect.hpp:89
std::string engine_
Definition: aspect.hpp:93
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual ~known_aspect()
Definition: aspect.cpp:143
bool invalidate_on_gamestate_change_
Definition: aspect.hpp:92
void init_readonly_context_proxy(readonly_context &target)
Definition: contexts.hpp:438