Debian Codegraph @

src/level/Unit.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Unit.h"
10
11 #include "Cube.h"
12 #include "Rules.h"
13 #include "InputProvider.h"
14 #include "Anim.h"
15 #include "Shape.h"
16
17 #include <assert.h>
18
19 //-----------------------------------------------------------------
20 /**
21 * Create new driveable unit.
22 * @param buttons control buttons
23 * @param symbols move symbols stored in load/save
24 * @param a_startActive whether unit should be active at start
25 */
26 Unit::Unit(const KeyControl &buttons, const ControlSym &symbols,
27 bool a_startActive)
28 : m_buttons(buttons), m_symbols(symbols)
29 {
30 m_model = NULL;
31 m_startActive = a_startActive;
32 }
33 //-----------------------------------------------------------------
34 bool
35 Unit::canDrive() const
36 {
37 return m_model->isAlive()
38 && !m_model->isLost()
39 && !m_model->isBusy();
40 }
41 //-----------------------------------------------------------------
42 /**
43 * Return true when we can move in future.
44 */
45 bool
46 Unit::willMove() const
47 {
48 return m_model->isAlive()
49 && !m_model->isLost();
50 }
51 //-----------------------------------------------------------------
52 /**
53 * Test keys and try move.
54 * @return a symbol when unit has moved
55 */
56 char
57 Unit::drive(const InputProvider *input)
58 {
59 return driveBorrowed(input, m_buttons);
60 }
61 //-----------------------------------------------------------------
62 /**
63 * Test keys and try move, use borrowed controls.
64 * @return a symbol when unit has moved or SYM_NONE
65 */
66 char
67 Unit::driveBorrowed(const InputProvider *input, const KeyControl &buttons)
68 {
69 if (canDrive()) {
70 if (input->isPressed(buttons.getLeft())) {
71 return goLeft();
72 }
73 if (input->isPressed(buttons.getRight())) {
74 return goRight();
75 }
76 if (input->isPressed(buttons.getUp())) {
77 return goUp();
78 }
79 if (input->isPressed(buttons.getDown())) {
80 return goDown();
81 }
82 }
83 return ControlSym::SYM_NONE;
84 }
85 //-----------------------------------------------------------------
86 /**
87 * Greet the player.
88 */
89 void
90 Unit::activate()
91 {
92 m_model->rules()->actionActivate();
93 }
94 //-----------------------------------------------------------------
95 /**
96 * Translate this key to symbol.
97 * @return symbol or SYM_NONE for unknown key
98 */
99 char
100 Unit::mySymbol(SDLKey key) const
101 {
102 return mySymbolBorrowed(key, m_buttons);
103 }
104 //-----------------------------------------------------------------
105 /**
106 * Translate this key to symbol, compare with borrowed buttons.
107 * @return symbol or SYM_NONE for unknown key
108 */
109 char
110 Unit::mySymbolBorrowed(SDLKey key, const KeyControl &buttons) const
111 {
112 if (key == buttons.getLeft()) {
113 return m_symbols.getLeft();
114 }
115 if (key == buttons.getRight()) {
116 return m_symbols.getRight();
117 }
118 if (key == buttons.getUp()) {
119 return m_symbols.getUp();
120 }
121 if (key == buttons.getDown()) {
122 return m_symbols.getDown();
123 }
124 return ControlSym::SYM_NONE;
125 }
126 //-----------------------------------------------------------------
127 /**
128 * Returns symbol for this direction.
129 */
130 char
131 Unit::myOrder(Dir::eDir dir) const
132 {
133 switch (dir) {
134 case Dir::DIR_LEFT:
135 return m_symbols.getLeft();
136 case Dir::DIR_RIGHT:
137 return m_symbols.getRight();
138 case Dir::DIR_UP:
139 return m_symbols.getUp();
140 case Dir::DIR_DOWN:
141 return m_symbols.getDown();
142 default:
143 assert(!"unknown dir");
144 }
145 return ControlSym::SYM_NONE;
146 }
147 //-----------------------------------------------------------------
148 /**
149 * Make move.
150 * @return move symbol or SYM_NONE for bad move
151 */
152 char
153 Unit::driveOrder(char move)
154 {
155 if (canDrive()) {
156 if (m_symbols.getLeft() == move) {
157 return goLeft();
158 }
159 if (m_symbols.getRight() == move) {
160 return goRight();
161 }
162 if (m_symbols.getUp() == move) {
163 return goUp();
164 }
165 if (m_symbols.getDown() == move) {
166 return goDown();
167 }
168 }
169 return ControlSym::SYM_NONE;
170 }
171 //-----------------------------------------------------------------
172 char
173 Unit::goLeft()
174 {
175 char symbol = ControlSym::SYM_NONE;
176 if (m_model->isLeft()) {
177 if (m_model->rules()->actionMoveDir(Dir::DIR_LEFT)) {
178 symbol = m_symbols.getLeft();
179 }
180 }
181 else {
182 m_model->rules()->actionTurnSide();
183 symbol = m_symbols.getLeft();
184 }
185 return symbol;
186 }
187 //-----------------------------------------------------------------
188 char
189 Unit::goRight()
190 {
191 char symbol = ControlSym::SYM_NONE;
192 if (!m_model->isLeft()) {
193 if (m_model->rules()->actionMoveDir(Dir::DIR_RIGHT)) {
194 symbol = m_symbols.getRight();
195 }
196 }
197 else {
198 m_model->rules()->actionTurnSide();
199 symbol = m_symbols.getRight();
200 }
201 return symbol;
202 }
203 //-----------------------------------------------------------------
204 char
205 Unit::goUp()
206 {
207 char symbol = ControlSym::SYM_NONE;
208 if (m_model->rules()->actionMoveDir(Dir::DIR_UP)) {
209 symbol = m_symbols.getUp();
210 }
211 return symbol;
212 }
213 //-----------------------------------------------------------------
214 char
215 Unit::goDown()
216 {
217 char symbol = ControlSym::SYM_NONE;
218 if (m_model->rules()->actionMoveDir(Dir::DIR_DOWN)) {
219 symbol = m_symbols.getDown();
220 }
221 return symbol;
222 }
223
224 //-----------------------------------------------------------------
225 bool
226 Unit::isMoving() const
227 {
228 bool result = false;
229 if (canDrive()) {
230 std::string action = m_model->rules()->getAction();
231 result = action == "move_left" || action == "move_right"
232 || action == "move_up" || action == "move_down"
233 || action == "turn";
234 }
235 return result;
236 }
237 //-----------------------------------------------------------------
238 bool
239 Unit::isMovingDown() const
240 {
241 return m_model->rules()->getDir() == Dir::DIR_DOWN;
242 }
243 //-----------------------------------------------------------------
244 bool
245 Unit::isTurning() const
246 {
247 std::string action = m_model->rules()->getAction();
248 return action == "turn";
249 }
250 //-----------------------------------------------------------------
251 bool
252 Unit::isPushing() const
253 {
254 return m_model->rules()->isPushing();
255 }
256 //-----------------------------------------------------------------
257 bool
258 Unit::isDrivenBy(char symbol) const
259 {
260 return m_symbols.getLeft() == symbol
261 || m_symbols.getRight() == symbol
262 || m_symbols.getUp() == symbol
263 || m_symbols.getDown() == symbol;
264 }
265 //-----------------------------------------------------------------
266 /**
267 * Returns true when models are equal.
268 */
269 bool
270 Unit::equalsModel(const Cube *other) const
271 {
272 return m_model == other;
273 }
274 //-----------------------------------------------------------------
275 V2
276 Unit::getLoc() const
277 {
278 return m_model->getLocation();
279 }
280 //-----------------------------------------------------------------
281 int
282 Unit::getW() const
283 {
284 return m_model->shape()->getW();
285 }
286 //-----------------------------------------------------------------
287 int
288 Unit::getH() const
289 {
290 return m_model->shape()->getH();
291 }
292 //-----------------------------------------------------------------
293 bool
294 Unit::isFreePlace(const V2 &loc) const
295 {
296 return m_model->const_rules()->isFreePlace(loc);
297 }
298 //-----------------------------------------------------------------
299 int
300 Unit::countAnimPhases(const std::string &anim) const
301 {
302 return m_model->anim()->countAnimPhases(anim);
303 }
304 //-----------------------------------------------------------------
305 bool
306 Unit::isPowerful() const {
307 return m_model->getPower() >= Cube::HEAVY;
308 }
309