The Battle for Wesnoth  1.17.0-dev
action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
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 "whiteboard/action.hpp"
21 #include "whiteboard/move.hpp"
22 #include "whiteboard/attack.hpp"
23 #include "whiteboard/recruit.hpp"
24 #include "whiteboard/recall.hpp"
26 
27 #include "config.hpp"
28 #include "game_board.hpp"
29 #include "resources.hpp"
30 #include "units/unit.hpp"
31 
32 namespace wb {
33 
34 std::ostream& operator<<(std::ostream& s, action_ptr action)
35 {
36  assert(action);
37  return action->print(s);
38 }
39 
40 std::ostream& operator<<(std::ostream& s, action_const_ptr action)
41 {
42  assert(action);
43  return action->print(s);
44 }
45 
46 std::ostream& action::print(std::ostream& s) const
47 {
48  return s;
49 }
50 
52 {
53  config final_cfg;
54  final_cfg["type"]="action";
55  final_cfg["team_index_"]=static_cast<int>(team_index_);
56  return final_cfg;
57 }
58 
59 /* static */
61 {
62  std::string type = cfg["type"];
63 
64  try {
65  if(type=="move")
66  return action_ptr(new move(cfg,hidden));
67  else if(type=="attack")
68  return action_ptr(new attack(cfg,hidden));
69  else if(type=="recruit")
70  return action_ptr(new recruit(cfg,hidden));
71  else if(type=="recall")
72  return action_ptr(new recall(cfg,hidden));
73  else if(type=="suppose_dead")
74  return action_ptr(new suppose_dead(cfg,hidden));
75  } catch(const action::ctor_err&) {}
76 
77  return action_ptr();
78 }
79 
81 {
82  if(hidden_)
83  return;
84  hidden_ = true;
85  do_hide();
86 }
87 
89 {
90  if(!hidden_)
91  return;
92  hidden_ = false;
93  do_show();
94 }
95 
96 action::action(std::size_t team_index, bool hidden)
97  : team_index_(team_index)
98  , hidden_(hidden)
99 {
100 }
101 
102 action::action(const config& cfg, bool hidden)
103  : team_index_()
104  , hidden_(hidden)
105 {
106  // Construct and validate team_index_
107  int team_index_temp = cfg["team_index_"].to_int(-1); //default value: -1
108  if(team_index_temp < 0
109  || team_index_temp >= static_cast<int>(resources::gameboard->teams().size()))
110  throw ctor_err("action: Invalid team_index_");
111  team_index_ = team_index_temp;
112 }
113 
115 {
116 }
117 
118 std::size_t action::get_unit_id() const
119 {
120  unit_ptr ret = get_unit();
121  return ret ? ret->underlying_id() : 0;
122 }
123 
124 } // end namespace wb
bool hidden_
Definition: action.hpp:143
static action_ptr from_config(const config &, bool hidden)
Constructs an object of a subclass of wb::action using a config.
Definition: action.cpp:60
Definitions for the interface to Wesnoth Markup Language (WML).
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
virtual void do_show()
Definition: action.hpp:140
game_board * gameboard
Definition: resources.cpp:21
action(std::size_t team_index, bool hidden)
Definition: action.cpp:96
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
virtual std::ostream & print(std::ostream &s) const =0
Definition: action.cpp:46
virtual unit_ptr get_unit() const =0
Return the unit targeted by this action.
virtual std::size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: action.cpp:118
static map_location::DIRECTION s
void hide()
Sets whether or not the action should be drawn on the screen.
Definition: action.cpp:80
virtual ~action()
Definition: action.cpp:114
bool hidden() const
Definition: action.hpp:64
void show()
Definition: action.cpp:88
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
std::size_t team_index_
Definition: action.hpp:142
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
A planned move, represented on the map by an arrow and a ghosted unit in the destination hex...
Definition: move.hpp:35
Definition: display.hpp:49
A planned action that temporarily removes a unit from the map for planning purposes.
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: action.hpp:139
std::shared_ptr< action const > action_const_ptr
Definition: typedefs.hpp:63