Debian Codegraph @

engine/action/action_move.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name action_move.cpp - The move action. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "stratagus.h"
39 #include "video.h"
40 #include "unittype.h"
41 #include "animation.h"
42 #include "player.h"
43 #include "unit.h"
44 #include "map.h"
45 #include "actions.h"
46 #include "pathfinder.h"
47 #include "sound.h"
48 #include "interface.h"
49 #include "map.h"
50 #include "ai.h"
51
52 /*----------------------------------------------------------------------------
53 -- Variables
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 -- Function
58 ----------------------------------------------------------------------------*/
59
60 /**
61 ** Test if unit can move.
62 ** For the moment only check for move animation.
63 **
64 ** @param unit unit to test if it can move.
65 **
66 ** @return 0 if unit cannot move.
67 */
68 bool CanMove(const CUnit *unit)
69 {
70 Assert(unit);
71 Assert(unit->Type);
72 return unit->Type->Animations && unit->Type->Animations->Move;
73 }
74
75
76 /**
77 ** Unit moves! Generic function called from other actions.
78 **
79 ** @param unit Pointer to unit.
80 **
81 ** @return >0 remaining path length, 0 wait for path, -1
82 ** reached goal, -2 can't reach the goal.
83 */
84 int DoActionMove(CUnit *unit)
85 {
86 int xd; // X movement in tile.
87 int yd; // Y movement in tile.
88 int d;
89 int x; // Unit->X
90 int y; // Unit->Y
91 int move;
92
93 Assert(CanMove(unit));
94 if (!unit->Moving &&
95 (unit->Type->Animations->Move != unit->Anim.CurrAnim || !unit->Anim.Wait)) {
96 Assert(!unit->Anim.Unbreakable);
97
98 // FIXME: So units flying up and down are not affected.
99 unit->IX = unit->IY = 0;
100
101 UnmarkUnitFieldFlags(unit);
102 d = NextPathElement(unit, &xd, &yd);
103 MarkUnitFieldFlags(unit);
104 switch (d) {
105 case PF_UNREACHABLE: // Can't reach, stop
106 if (unit->Player->AiEnabled) {
107 AiCanNotMove(unit);
108 }
109 unit->Moving = 0;
110 return d;
111 case PF_REACHED: // Reached goal, stop
112 unit->Moving = 0;
113 return d;
114 case PF_WAIT: // No path, wait
115 // Reset frame to still frame while we wait
116 // FIXME: Unit doesn't animate.
117 unit->Frame = unit->Type->StillFrame;
118 UnitUpdateHeading(unit);
119 unit->Wait = 10;
120
121 unit->Moving = 0;
122 return d;
123 default: // On the way moving
124 unit->Moving = 1;
125 break;
126 }
127 x = unit->X;
128 y = unit->Y;
129 //
130 // Transporter (un)docking?
131 //
132 // FIXME: This is an ugly hack
133 if (unit->Type->CanTransport &&
134 ((Map.WaterOnMap(x, y) && Map.CoastOnMap(x + xd, y + yd)) ||
135 (Map.CoastOnMap(x, y) && Map.WaterOnMap(x + xd, y + yd)))) {
136 PlayUnitSound(unit, VoiceDocking);
137 }
138
139 x = unit->X + xd;
140 y = unit->Y + yd;
141 unit->MoveToXY(x, y);
142
143 // Remove unit from the current selection
144 if (unit->Selected && !Map.IsFieldVisible(ThisPlayer, x, y)) {
145 if (NumSelected == 1) { // Remove building cursor
146 CancelBuildingMode();
147 }
148 if (!ReplayRevealMap) {
149 UnSelectUnit(unit);
150 SelectionChanged();
151 }
152 }
153
154 unit->IX = -xd * TileSizeX;
155 unit->IY = -yd * TileSizeY;
156 unit->Frame = unit->Type->StillFrame;
157 UnitHeadingFromDeltaXY(unit, xd, yd);
158 } else {
159 xd = Heading2X[unit->Direction / NextDirection];
160 yd = Heading2Y[unit->Direction / NextDirection];
161 d = unit->Data.Move.Length + 1;
162 }
163
164 move = UnitShowAnimationScaled(unit, unit->Type->Animations->Move,
165 (unit->Type->UnitType == UnitTypeFly)
166 ? MapFieldNormalCost : Map.Field(unit->X, unit->Y)->Cost);
167
168 unit->IX += xd * move;
169 unit->IY += yd * move;
170
171 // Finished move animation, set Moving to 0 so we recalculate the path
172 // next frame
173 // FIXME: this is broken for subtile movement
174 if (!unit->Anim.Unbreakable && !unit->IX && !unit->IY) {
175 unit->Moving = 0;
176 }
177
178 return d;
179 }
180
181 /**
182 ** Unit move action:
183 **
184 ** Move to a place or to a unit (can move).
185 ** Tries 10x to reach the target, note this are the complete tries.
186 ** If the target entered another unit, move to it's position.
187 ** If the target unit is destroyed, continue to move to it's last position.
188 **
189 ** @param unit Pointer to unit.
190 */
191 void HandleActionMove(CUnit *unit)
192 {
193 CUnit *goal;
194
195 Assert(unit);
196 Assert(CanMove(unit));
197
198 if (unit->Wait) {
199 unit->Wait--;
200 return;
201 }
202
203 if (!unit->SubAction) { // first entry
204 unit->SubAction = 1;
205 NewResetPath(unit);
206
207 Assert(unit->State == 0);
208 }
209
210 // FIXME: (mr-russ) Make a reachable goal here with GoalReachable ...
211
212 switch (DoActionMove(unit)) { // reached end-point?
213 case PF_UNREACHABLE:
214 //
215 // Some tries to reach the goal
216 //
217 if (unit->Orders[0]->Range <= Map.Info.MapWidth ||
218 unit->Orders[0]->Range <= Map.Info.MapHeight) {
219 unit->Orders[0]->Range++;
220 break;
221 }
222 // FALL THROUGH
223 case PF_REACHED:
224 // Release target, if any.
225 if ((goal = unit->Orders[0]->Goal)) {
226 goal->RefsDecrease();
227 unit->Orders[0]->Goal = NoUnitP;
228 }
229 unit->ClearAction();
230 return;
231
232 default:
233 break;
234 }
235
236 //
237 // Target destroyed?
238 //
239 if ((goal = unit->Orders[0]->Goal) && goal->Destroyed) {
240 DebugPrint("Goal dead\n");
241 unit->Orders[0]->X = goal->X + goal->Type->TileWidth / 2;
242 unit->Orders[0]->Y = goal->Y + goal->Type->TileHeight / 2;
243 unit->Orders[0]->Goal = NoUnitP;
244 goal->RefsDecrease();
245 NewResetPath(unit);
246 }
247 }
248
249 //@}