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