The Battle for Wesnoth  1.19.0+dev
action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 */
60 action_ptr action::from_config(const config& cfg, bool hidden)
61 {
62  std::string type = cfg["type"];
63 
64  try {
65  if(type == "move")
66  return std::make_shared<move>(cfg, hidden);
67  else if(type == "attack")
68  return std::make_shared<attack>(cfg, hidden);
69  else if(type == "recruit")
70  return std::make_shared<recruit>(cfg, hidden);
71  else if(type == "recall")
72  return std::make_shared<recall>(cfg, hidden);
73  else if(type == "suppose_dead")
74  return std::make_shared<suppose_dead>(cfg, hidden);
75  } catch(const action::ctor_err&) {
76  }
77 
78  return nullptr;
79 }
80 
82 {
83  if(hidden_)
84  return;
85  hidden_ = true;
86  do_hide();
87 }
88 
90 {
91  if(!hidden_)
92  return;
93  hidden_ = false;
94  do_show();
95 }
96 
97 action::action(std::size_t team_index, bool hidden)
98  : team_index_(team_index)
99  , hidden_(hidden)
100 {
101 }
102 
103 action::action(const config& cfg, bool hidden)
104  : team_index_()
105  , hidden_(hidden)
106 {
107  // Construct and validate team_index_
108  int team_index_temp = cfg["team_index_"].to_int(-1); //default value: -1
109  if(team_index_temp < 0
110  || team_index_temp >= static_cast<int>(resources::gameboard->teams().size()))
111  throw ctor_err("action: Invalid team_index_");
112  team_index_ = team_index_temp;
113 }
114 
116 {
117 }
118 
119 std::size_t action::get_unit_id() const
120 {
121  unit_ptr ret = get_unit();
122  return ret ? ret->underlying_id() : 0;
123 }
124 
125 } // end namespace wb
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
virtual unit_ptr get_unit() const =0
Return the unit targeted by this action.
std::size_t team_index_
Definition: action.hpp:142
bool hidden() const
Definition: action.hpp:64
void hide()
Sets whether or not the action should be drawn on the screen.
Definition: action.cpp:81
virtual void do_show()
Definition: action.hpp:140
virtual ~action()
Definition: action.cpp:115
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: action.hpp:139
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
action(std::size_t team_index, bool hidden)
Definition: action.cpp:97
virtual std::size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: action.cpp:119
void show()
Definition: action.cpp:89
virtual std::ostream & print(std::ostream &s) const =0
Definition: action.cpp:46
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
bool hidden_
Definition: action.hpp:143
game_board * gameboard
Definition: resources.cpp:20
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
Definition: display.hpp:45
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
std::shared_ptr< action const > action_const_ptr
Definition: typedefs.hpp:63
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
static map_location::DIRECTION s