The Battle for Wesnoth  1.15.1+dev
aspect.hpp
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 #pragma once
20 
23 #include "ai/lua/lua_object.hpp"
24 #include "ai/lua/core.hpp"
26 
27 #include "log.hpp"
28 
29 #include "utils/functional.hpp"
30 
31 #include <boost/range/adaptor/reversed.hpp>
32 
33 namespace ai {
34 
35 class aspect : public readonly_context_proxy, public events::observer, public component {
36 public:
37  aspect(readonly_context &context, const config &cfg, const std::string &id);
38 
39  virtual ~aspect();
40 
41  void invalidate() const
42  {
43  valid_ = false;
44  valid_variant_ = false;
45  valid_lua_ = false;
46  }
47 
48 
49  virtual const wfl::variant& get_variant() const = 0;
50 
51 
52  virtual std::shared_ptr<wfl::variant> get_variant_ptr() const = 0;
53 
54 
55  virtual void recalculate() const = 0;
56 
57 
58  virtual void on_create();
59 
60 
61  virtual bool redeploy(const config &cfg, const std::string & id);
62 
63 
64  virtual config to_config() const;
65 
66 
67  virtual bool delete_all_facets();
68 
69 
70  void handle_generic_event(const std::string &/*event_name*/)
71  {
72  invalidate();
73  }
74 
75 
76  virtual bool active() const;
77 
78  virtual std::string get_name() const
79  { return name_; }
80 
81  virtual std::string get_id() const
82  { return id_; }
83 
84  virtual std::string get_engine() const
85  { return engine_; }
86 
87  static lg::log_domain& log();
88 
89 protected:
90  std::string time_of_day_;
91  std::string turns_;
92 
93  mutable bool valid_;
94  mutable bool valid_variant_;
95  mutable bool valid_lua_;
96 
101  std::string engine_;
102  std::string name_;
103  std::string id_;
104 
105 };
106 
107 template<typename T>
108 class typesafe_aspect : public aspect {
109 public:
110  typesafe_aspect(readonly_context &context, const config &cfg, const std::string &id)
111  : aspect(context,cfg,id)
112  , value_()
113  , value_variant_()
114  , value_lua_()
115  {
116  }
117 
119  {
120  }
121 
122 
123  virtual const T& get() const
124  {
125  return *get_ptr();
126  }
127 
128 
129  virtual const wfl::variant& get_variant() const
130  {
131  return *get_variant_ptr();
132  }
133 
134  virtual std::shared_ptr<wfl::variant> get_variant_ptr() const
135  {
136  if (!valid_variant_) {
137  if (!valid_) {
138  recalculate();
139  }
140 
141  if (!valid_variant_ && valid_ ) {
142  value_variant_.reset(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
143  valid_variant_ = true;
144  } else if (!valid_variant_ && valid_lua_) {
145  value_ = value_lua_->get();
146  value_variant_.reset(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
147  valid_variant_ = true; // @note: temporary workaround
148  } else {
149  assert(valid_variant_);
150  }
151  }
152  return value_variant_;
153  }
154 
155  virtual void recalculate() const = 0;
156 
157 
158  virtual std::shared_ptr<T> get_ptr() const
159  {
160  if (!valid_) {
161  if (!(valid_variant_ || valid_lua_)) {
162  recalculate();
163  }
164 
165  if (!valid_ ) {
166  if (valid_variant_) {
168  valid_ = true;
169  } else if (valid_lua_){
170  value_ = value_lua_->get();
171  valid_ = true;
172  } else {
173  assert(valid_);
174  }
175  }
176  }
177  return value_;
178  }
179 
180 protected:
181  mutable std::shared_ptr<T> value_;
182  mutable std::shared_ptr<wfl::variant> value_variant_;
183  mutable std::shared_ptr< lua_object<T>> value_lua_;
184 };
185 
186 
188 public:
189  known_aspect(const std::string &name);
190 
191 
192  virtual ~known_aspect();
193 
194 
195  virtual void set(aspect_ptr a) = 0;
196 
197 
198  virtual void add_facet(const config &cfg) = 0;
199 
200 
201  const std::string& get_name() const;
202 
203 protected:
204  const std::string name_;
205 };
206 
207 
208 template<class T>
210 
211 template<typename T>
213 public:
214  typesafe_known_aspect(const std::string &name, typesafe_aspect_ptr<T>& where, aspect_map &aspects)
215  : known_aspect(name), where_(where), aspects_(aspects)
216  {
217  }
218 
219  void set(aspect_ptr a)
220  {
221  typesafe_aspect_ptr<T> c = std::dynamic_pointer_cast<typesafe_aspect<T>>(a);
222  if (c) {
223  assert (c->get_id()== this->get_name());
224  where_ = c;
225  aspects_.emplace(this->get_name(),c);
226  } else {
227  LOG_STREAM(debug, aspect::log()) << "typesafe_known_aspect [" << this->get_name() << "] : while setting aspect, got null. this might be caused by invalid [aspect] WML" << std::endl;
228  }
229  }
230 
231  virtual void add_facet(const config &cfg)
232  {
233  std::shared_ptr< composite_aspect <T>> c = std::dynamic_pointer_cast< composite_aspect<T>>(where_);
234  if (c) {
235  assert (c->get_id()==this->get_name());
236  c->add_facet(-1, cfg);
237  c->invalidate();
238  } else {
239  LOG_STREAM(debug, aspect::log()) << "typesafe_known_aspect [" << this->get_name() << "] : while adding facet to aspect, got null. this might be caused by target [aspect] being not composite" << std::endl;
240  }
241  }
242 
243 protected:
246 };
247 
248 
249 template<typename T>
250 class composite_aspect : public typesafe_aspect<T> {
251 public:
252 
253  composite_aspect(readonly_context &context, const config &cfg, const std::string &id)
254  : typesafe_aspect<T>(context, cfg, id)
255  , facets_()
256  , default_()
257  , parent_id_(id)
258  {
259  for (const config &cfg_element : this->cfg_.child_range("facet")) {
260  add_facet(-1,cfg_element);
261  }
262 
263  config _default = this->cfg_.child("default");
264  if (_default) {
265  _default["id"] = "default_facet";
266  std::vector< aspect_ptr > default_aspects;
267  engine::parse_aspect_from_config(*this,_default,parent_id_,std::back_inserter(default_aspects));
268  if (!default_aspects.empty()) {
269  typesafe_aspect_ptr<T> b = std::dynamic_pointer_cast< typesafe_aspect<T>>(default_aspects.front());
270  if (composite_aspect<T>* c = dynamic_cast<composite_aspect<T>*>(b.get())) {
271  c->parent_id_ = parent_id_;
272  }
273  default_ = b;
274  }
275  }
276 
277  std::function<void(typesafe_aspect_vector<T>&, const config&)> factory_facets =
278  std::bind(&ai::composite_aspect<T>::create_facet,*this,_1,_2);
279 
280  register_facets_property(this->property_handlers(),"facet",facets_,default_, factory_facets);
281 
282  }
283 
284 
286  {
287  std::vector<aspect_ptr> facets_base;
288  engine::parse_aspect_from_config(*this,cfg,parent_id_,std::back_inserter(facets_base));
289  for (aspect_ptr a : facets_base) {
290  typesafe_aspect_ptr<T> b = std::dynamic_pointer_cast< typesafe_aspect<T>> (a);
291  if (composite_aspect<T>* c = dynamic_cast<composite_aspect<T>*>(b.get())) {
292  c->parent_id_ = parent_id_;
293  }
294  facets.push_back(b);
295  }
296  }
297 
298 
299  virtual void recalculate() const
300  {
301  for(const auto& f : boost::adaptors::reverse(facets_)) {
302  if (f->active()) {
303  this->value_ = f->get_ptr();
304  this->valid_ = true;
305  return;
306  }
307  }
308  if (default_) {
309  this->value_ = default_->get_ptr();
310  this->valid_ = true;
311  }
312  }
313 
314 
315  virtual config to_config() const
316  {
317  config cfg = aspect::to_config();
318  for (const typesafe_aspect_ptr<T> f : facets_) {
319  cfg.add_child("facet",f->to_config());
320  }
321  if (default_) {
322  cfg.add_child("default",default_->to_config());
323  }
324  return cfg;
325  }
326 
327 
329  virtual bool add_facet(int pos, const config &cfg)
330  {
331  if (pos<0) {
332  pos = facets_.size();
333  }
334  std::vector< aspect_ptr > facets;
335  engine::parse_aspect_from_config(*this,cfg,parent_id_,std::back_inserter(facets));
336  int j=0;
337  for (aspect_ptr a : facets) {
338  typesafe_aspect_ptr<T> b = std::dynamic_pointer_cast< typesafe_aspect<T>> (a);
339  if (composite_aspect<T>* c = dynamic_cast<composite_aspect<T>*>(b.get())) {
340  c->parent_id_ = parent_id_;
341  }
342  facets_.insert(facets_.begin()+pos+j,b);
343  j++;
344  }
345  return (j>0);
346  }
347 
348 
349  virtual bool delete_all_facets()
350  {
351  bool b = !facets_.empty();
352  facets_.clear();
353  return b;
354  }
355 
357  {
358  for(aspect_ptr a : facets_) {
359  if(a->active()) {
360  return *a;
361  }
362  }
363  return *default_;
364  }
365 
366 protected:
369  std::string parent_id_;
370 
371 };
372 
373 template<typename T>
374 class standard_aspect : public typesafe_aspect<T> {
375 public:
376  standard_aspect(readonly_context &context, const config &cfg, const std::string &id)
377  : typesafe_aspect<T>(context, cfg, id)
378  {
379  this->name_ = "standard_aspect";
380  this->value_ = std::make_shared<T>(config_value_translator<T>::cfg_to_value(this->cfg_));
381  LOG_STREAM(debug, aspect::log()) << "standard aspect has value: "<< std::endl << config_value_translator<T>::value_to_cfg(this->get()) << std::endl;
382  }
383 
384 
385  void recalculate() const
386  {
387  //nothing to recalculate
388  this->valid_ = true;
389  }
390 
391 
393  {
394  config cfg = aspect::to_config();
396  return cfg;
397  }
398 
399 };
400 
401 class lua_aspect_visitor : public boost::static_visitor<std::string> {
402  static std::string quote_string(const std::string& s);
403 public:
404  std::string operator()(bool b) const {return utils::bool_string(b);}
405  std::string operator()(int i) const {return std::to_string(i);}
406  std::string operator()(unsigned long long i) const {return std::to_string(i);}
407  std::string operator()(double i) const {return std::to_string(i);}
408  std::string operator()(const std::string& s) const {return quote_string(s);}
409  std::string operator()(const t_string& s) const {return quote_string(s.str());}
410  std::string operator()(boost::blank) const {return "nil";}
411 };
412 
413 template<typename T>
414 class lua_aspect : public typesafe_aspect<T>
415 {
416 public:
417  lua_aspect(readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr<lua_ai_context>& l_ctx)
418  : typesafe_aspect<T>(context, cfg, id)
419  , handler_(), code_(), params_(cfg.child_or_empty("args"))
420  {
421  this->name_ = "lua_aspect";
422  if (cfg.has_attribute("code"))
423  {
424  code_ = cfg["code"].str();
425  }
426  else if (cfg.has_attribute("value"))
427  {
428  code_ = "return " + cfg["value"].apply_visitor(lua_aspect_visitor());
429  }
430  else
431  {
432  // error
433  return;
434  }
435  handler_.reset(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
436  }
437 
438  void recalculate() const
439  {
440  this->valid_lua_ = true;
441  this->value_lua_.reset(new lua_object<T>);
442  handler_->handle(params_, true, this->value_lua_);
443  }
444 
446  {
447  config cfg = aspect::to_config();
448  cfg["code"] = code_;
449  if (!params_.empty()) {
450  cfg.add_child("args", params_);
451  }
452  return cfg;
453  }
454 
455 private:
456  std::shared_ptr<lua_ai_action_handler> handler_;
457  std::string code_;
459 };
460 
461 
463  bool is_duplicate(const std::string &name);
464 public:
465  typedef std::shared_ptr< aspect_factory > factory_ptr;
466  typedef std::map<std::string, factory_ptr> factory_map;
467  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
468 
469  static factory_map& get_list() {
470  static factory_map *aspect_factories;
471  if (aspect_factories==nullptr) {
472  aspect_factories = new factory_map;
473  }
474  return *aspect_factories;
475  }
476 
477  virtual aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id) = 0;
478 
479  aspect_factory( const std::string &name )
480  {
481  if (is_duplicate(name)) {
482  return;
483  }
484  factory_ptr ptr_to_this(this);
485  get_list().emplace(name,ptr_to_this);
486  }
487 
488  virtual ~aspect_factory() {}
489 };
490 
491 
492 template<class ASPECT>
494 public:
495  register_aspect_factory( const std::string &name )
496  : aspect_factory( name )
497  {
498  }
499 
500  aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id)
501  {
502  aspect_ptr a = std::make_shared<ASPECT>(context, cfg, id);
503  a->on_create();
504  return a;
505  }
506 };
507 
509 public:
510  typedef std::shared_ptr< lua_aspect_factory > factory_ptr;
511  typedef std::map<std::string, factory_ptr> factory_map;
512  typedef std::pair<const std::string, factory_ptr> factory_map_pair;
513 
514  static factory_map& get_list() {
515  static factory_map *aspect_factories;
516  if (aspect_factories==nullptr) {
517  aspect_factories = new factory_map;
518  }
519  return *aspect_factories;
520  }
521 
522  virtual aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr<lua_ai_context>& l_ctx) = 0;
523 
524  lua_aspect_factory( const std::string &name )
525  {
526  factory_ptr ptr_to_this(this);
527  get_list().emplace(name,ptr_to_this);
528  }
529 
530  virtual ~lua_aspect_factory() {}
531 };
532 
533 template<class ASPECT>
535 public:
536  register_lua_aspect_factory( const std::string &name )
537  : lua_aspect_factory( name )
538  {
539  }
540 
541  aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr<lua_ai_context>& l_ctx)
542  {
543  aspect_ptr a = std::make_shared<ASPECT>(context, cfg, id, l_ctx);
544  a->on_create();
545  return a;
546  }
547 };
548 
549 
550 } //end of namespace ai
static void parse_aspect_from_config(readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr >> b)
Definition: engine.cpp:51
virtual void recalculate() const
Definition: aspect.hpp:299
static std::unique_ptr< class sdl_event_handler > handler_
Definition: handler.cpp:62
std::string operator()(bool b) const
Definition: aspect.hpp:404
lua_aspect_factory(const std::string &name)
Definition: aspect.hpp:524
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
bool invalidate_on_tod_change_
Definition: aspect.hpp:99
aspect_ptr get_new_instance(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.hpp:500
std::string operator()(const std::string &s) const
Definition: aspect.hpp:408
std::string operator()(const t_string &s) const
Definition: aspect.hpp:409
virtual bool add_facet(int pos, const config &cfg)
Definition: aspect.hpp:329
bool valid_variant_
Definition: aspect.hpp:94
virtual ~lua_aspect_factory()
Definition: aspect.hpp:530
std::map< std::string, factory_ptr > factory_map
Definition: aspect.hpp:511
virtual std::shared_ptr< wfl::variant > get_variant_ptr() const
Definition: aspect.hpp:134
std::string turns_
Definition: aspect.hpp:91
static factory_map & get_list()
Definition: aspect.hpp:514
aspect_factory(const std::string &name)
Definition: aspect.hpp:479
bool has_attribute(config_key_type key) const
Definition: config.cpp:213
#define a
property_handler_map & property_handlers()
Definition: component.cpp:132
virtual void recalculate() const =0
virtual ~aspect()
Definition: aspect.cpp:46
typesafe_known_aspect(const std::string &name, typesafe_aspect_ptr< T > &where, aspect_map &aspects)
Definition: aspect.hpp:214
child_itors child_range(config_key_type key)
Definition: config.cpp:362
void handle_generic_event(const std::string &)
Definition: aspect.hpp:70
virtual bool redeploy(const config &cfg, const std::string &id)
Definition: aspect.cpp:69
register_aspect_factory(const std::string &name)
Definition: aspect.hpp:495
std::string time_of_day_
Definition: aspect.hpp:90
bool valid_lua_
Definition: aspect.hpp:95
virtual void add_facet(const std::string &id, const config &cfg) const override
Definition: contexts.hpp:650
void recalculate() const
Definition: aspect.hpp:438
std::shared_ptr< T > value_
Definition: aspect.hpp:181
#define LOG_STREAM(level, domain)
Definition: log.hpp:189
void invalidate() const
Definition: aspect.hpp:41
lua_aspect(readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr< lua_ai_context > &l_ctx)
Definition: aspect.hpp:417
Lua object(value) wrapper implementation.
config to_config() const
Definition: aspect.hpp:392
virtual bool delete_all_facets()
Definition: aspect.cpp:129
virtual std::shared_ptr< T > get_ptr() const
Definition: aspect.hpp:158
std::shared_ptr< aspect > aspect_ptr
Definition: game_info.hpp:94
virtual std::string get_name() const
Definition: aspect.hpp:78
#define b
virtual void add_facet(const config &cfg)
Definition: aspect.hpp:231
std::shared_ptr< wfl::variant > value_variant_
Definition: aspect.hpp:182
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
std::shared_ptr< typesafe_aspect< T > > typesafe_aspect_ptr
Definition: game_info.hpp:57
std::string name_
Definition: aspect.hpp:102
std::string operator()(double i) const
Definition: aspect.hpp:407
virtual config to_config() const
Definition: aspect.hpp:315
static lg::log_domain & log()
Definition: aspect.cpp:60
virtual std::string get_id() const
Definition: aspect.hpp:81
typesafe_aspect_ptr< T > & where_
Definition: aspect.hpp:244
virtual const wfl::variant & get_variant() const =0
aspect_map & aspects_
Definition: aspect.hpp:245
typesafe_aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.hpp:110
void recalculate() const
Definition: aspect.hpp:385
const std::string name_
Definition: aspect.hpp:204
virtual std::shared_ptr< wfl::variant > get_variant_ptr() const =0
virtual config to_config() const
Definition: aspect.cpp:106
virtual ~typesafe_aspect()
Definition: aspect.hpp:118
std::string parent_id_
Definition: aspect.hpp:369
std::string id_
Definition: aspect.hpp:103
std::string operator()(int i) const
Definition: aspect.hpp:405
void create_facet(typesafe_aspect_vector< T > &facets, const config &cfg)
Definition: aspect.hpp:285
aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.cpp:31
bool invalidate_on_turn_start_
Definition: aspect.hpp:98
aspect & find_active()
Definition: aspect.hpp:356
typesafe_aspect_ptr< T > default_
Definition: aspect.hpp:368
typesafe_aspect_vector< T > facets_
Definition: aspect.hpp:367
static T cfg_to_value(const config &cfg)
std::size_t i
Definition: function.cpp:933
std::shared_ptr< lua_object< T > > value_lua_
Definition: aspect.hpp:183
static map_location::DIRECTION s
virtual bool delete_all_facets()
Definition: aspect.hpp:349
std::string bool_string(const bool value)
Converts a bool value to &#39;true&#39; or &#39;false&#39;.
virtual void on_create()
Definition: aspect.cpp:65
std::shared_ptr< lua_aspect_factory > factory_ptr
Definition: aspect.hpp:510
#define debug(x)
virtual bool active() const
Definition: aspect.cpp:124
config & add_child(config_key_type key)
Definition: config.cpp:476
const config params_
Definition: aspect.hpp:458
bool valid_
Definition: aspect.hpp:93
std::shared_ptr< lua_ai_action_handler > handler_
Definition: aspect.hpp:456
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: aspect.hpp:512
Composite AI component.
std::map< std::string, aspect_ptr > aspect_map
Definition: game_info.hpp:103
#define f
std::shared_ptr< aspect_factory > factory_ptr
Definition: aspect.hpp:465
virtual ~aspect_factory()
Definition: aspect.hpp:488
static void value_to_cfg(const T &value, config &cfg)
std::string operator()(boost::blank) const
Definition: aspect.hpp:410
static factory_map & get_list()
Definition: aspect.hpp:469
Standard logging facilities (interface).
virtual std::string get_engine() const
Definition: aspect.hpp:84
config cfg_
Definition: aspect.hpp:97
game_lua_kernel * lua_kernel
Definition: resources.cpp:25
std::string engine_
Definition: aspect.hpp:101
register_lua_aspect_factory(const std::string &name)
Definition: aspect.hpp:536
static void register_facets_property(property_handler_map &property_handlers, const std::string &property, std::vector< std::shared_ptr< X >> &values, std::shared_ptr< X > &def, std::function< void(std::vector< std::shared_ptr< X >> &, const config &)> construction_factory)
std::pair< const std::string, factory_ptr > factory_map_pair
Definition: aspect.hpp:467
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:193
config to_config() const
Definition: aspect.hpp:445
std::map< std::string, factory_ptr > factory_map
Definition: aspect.hpp:466
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
mock_char c
const std::string & str() const
Definition: tstring.hpp:186
std::string code_
Definition: aspect.hpp:457
composite_aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.hpp:253
bool invalidate_on_gamestate_change_
Definition: aspect.hpp:100
standard_aspect(readonly_context &context, const config &cfg, const std::string &id)
Definition: aspect.hpp:376
std::string operator()(unsigned long long i) const
Definition: aspect.hpp:406
aspect_ptr get_new_instance(readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr< lua_ai_context > &l_ctx)
Definition: aspect.hpp:541
virtual const wfl::variant & get_variant() const
Definition: aspect.hpp:129
std::vector< typesafe_aspect_ptr< T > > typesafe_aspect_vector
Definition: game_info.hpp:60