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