-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
action.cpp
123 lines (103 loc) · 2.52 KB
/
action.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
*/
#include "action.hpp"
#include "move.hpp"
#include "attack.hpp"
#include "recruit.hpp"
#include "recall.hpp"
#include "suppose_dead.hpp"
#include "config.hpp"
#include "game_board.hpp"
#include "resources.hpp"
#include "units/unit.hpp"
namespace wb {
std::ostream& operator<<(std::ostream& s, action_ptr action)
{
assert(action);
return action->print(s);
}
std::ostream& operator<<(std::ostream& s, action_const_ptr action)
{
assert(action);
return action->print(s);
}
std::ostream& action::print(std::ostream& s) const
{
return s;
}
config action::to_config() const
{
config final_cfg;
final_cfg["type"]="action";
final_cfg["team_index_"]=static_cast<int>(team_index_);
return final_cfg;
}
/* static */
action_ptr action::from_config(config const& cfg, bool hidden)
{
std::string type = cfg["type"];
try {
if(type=="move")
return action_ptr(new move(cfg,hidden));
else if(type=="attack")
return action_ptr(new attack(cfg,hidden));
else if(type=="recruit")
return action_ptr(new recruit(cfg,hidden));
else if(type=="recall")
return action_ptr(new recall(cfg,hidden));
else if(type=="suppose_dead")
return action_ptr(new suppose_dead(cfg,hidden));
} catch(action::ctor_err const&) {}
return action_ptr();
}
void action::hide()
{
if(hidden_)
return;
hidden_ = true;
do_hide();
}
void action::show()
{
if(!hidden_)
return;
hidden_ = false;
do_show();
}
action::action(size_t team_index, bool hidden)
: team_index_(team_index)
, hidden_(hidden)
{
}
action::action(config const& cfg, bool hidden)
: team_index_()
, hidden_(hidden)
{
// Construct and validate team_index_
int team_index_temp = cfg["team_index_"].to_int(-1); //default value: -1
if(team_index_temp < 0
|| team_index_temp >= static_cast<int>(resources::teams->size()))
throw ctor_err("action: Invalid team_index_");
team_index_ = team_index_temp;
}
action::~action()
{
}
size_t action::get_unit_id() const
{
unit_ptr ret = get_unit();
return ret ? ret->underlying_id() : 0;
}
} // end namespace wb