The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
3  Part of the Battle for Wesnoth Project http://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 "action.hpp"
20 #include "move.hpp"
21 #include "attack.hpp"
22 #include "recruit.hpp"
23 #include "recall.hpp"
24 #include "suppose_dead.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 */
59 action_ptr action::from_config(config const& cfg, bool hidden)
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(action::ctor_err const&) {}
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(size_t team_index, bool hidden)
96  : team_index_(team_index)
97  , hidden_(hidden)
98 {
99 }
100 
101 action::action(config const& 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::teams->size()))
109  throw ctor_err("action: Invalid team_index_");
110  team_index_ = team_index_temp;
111 }
112 
114 {
115 }
116 
117 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
boost::shared_ptr< action > action_ptr
Definition: typedefs.hpp:70
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
size_t team_index_
Definition: action.hpp:140
bool hidden_
Definition: action.hpp:141
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< team > * teams
Definition: resources.cpp:29
virtual void do_show()
Definition: action.hpp:138
virtual std::ostream & print(std::ostream &s) const =0
Definition: action.cpp:45
size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: action.cpp:117
virtual unit_ptr get_unit() const =0
Return the unit targeted by this action.
static action_ptr from_config(config const &, bool hidden)
Constructs an object of a subclass of wb::action using a config.
Definition: action.cpp:59
void hide()
Sets whether or not the action should be drawn on the screen.
Definition: action.cpp:79
virtual ~action()
Definition: action.cpp:113
GLsizeiptr size
Definition: glew.h:1649
void show()
Definition: action.cpp:87
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:50
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:33
action(size_t team_index, bool hidden)
Definition: action.cpp:95
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:83
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
A planned move, represented on the map by an arrow and a ghosted unit in the destination hex...
Definition: move.hpp:33
Definition: display.hpp:47
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:137