Debian Codegraph @

engine/action/action_repair.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_repair.cpp - The repair action. */
11 //
12 // (c) Copyright 1999-2008 by Vladi Shabanski 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 "unittype.h"
40 #include "animation.h"
41 #include "player.h"
42 #include "unit.h"
43 #include "missile.h"
44 #include "actions.h"
45 #include "sound.h"
46 #include "map.h"
47 #include "pathfinder.h"
48 #include "interface.h"
49 #include "ai.h"
50
51 /*----------------------------------------------------------------------------
52 -- Functions
53 ----------------------------------------------------------------------------*/
54
55 /**
56 ** Restore the saved order
57 ** FIXME: this should be moved to a more central location
58 **
59 ** @param unit Unit to restore
60 **
61 ** @return True if the saved order was restored
62 */
63 static bool RestoreSavedOrder(CUnit *unit)
64 {
65 if (unit->SavedOrder.Action != UnitActionStill) {
66 unit->SubAction = 0;
67 Assert(unit->Orders[0]->Goal == NoUnitP);
68 *unit->Orders[0] = unit->SavedOrder;
69 NewResetPath(unit);
70 unit->SavedOrder.Action = UnitActionStill;
71
72 // This isn't supported
73 Assert(unit->SavedOrder.Goal == NoUnitP);
74 return true;
75 }
76 return false;
77 }
78
79 /**
80 ** Move to build location
81 **
82 ** @param unit Unit to move
83 */
84 static void MoveToLocation(CUnit *unit)
85 {
86 // First entry
87 if (!unit->SubAction) {
88 unit->SubAction = 1;
89 NewResetPath(unit);
90 unit->Orders[0]->MinRange = 1;
91 unit->Orders[0]->Range = unit->Type->RepairRange;
92 }
93
94 if (unit->Wait) {
95 // FIXME: show still animation while we wait?
96 unit->Wait--;
97 return;
98 }
99
100 CUnit *goal = unit->Orders[0]->Goal;
101 int err = 0;
102
103 switch (DoActionMove(unit)) { // reached end-point?
104 case PF_FAILED:
105 case PF_UNREACHABLE:
106 //
107 // Some tries to reach the goal
108 //
109 if (unit->SubAction++ < 10) {
110 // To keep the load low, retry each 10 cycles
111 // NOTE: we can already inform the AI about this problem?
112 unit->Wait = 10;
113 return;
114 }
115
116 if (goal) { // release reference
117 goal->RefsDecrease();
118 unit->Orders[0]->Goal = NoUnitP;
119 }
120 unit->ClearAction();
121 return;
122
123 case PF_REACHED:
124 break;
125
126 default:
127 // Moving...
128 break;
129 }
130
131 if (unit->Anim.Unbreakable) {
132 return;
133 }
134
135 //
136 // Target is dead, choose new one.
137 //
138 // Check if goal is correct unit.
139 if (goal) {
140 if (!goal->IsVisibleAsGoal(unit->Player)) {
141 DebugPrint("repair target gone.\n");
142 unit->Orders[0]->X = goal->X;
143 unit->Orders[0]->Y = goal->Y;
144 goal->RefsDecrease();
145 unit->Orders[0]->Goal = goal = NULL;
146 unit->SubAction = 0;
147 }
148 } else if (unit->Player->AiEnabled) {
149 // Ai players workers should stop if target is killed
150 err = -1;
151 }
152
153 //
154 // Have reached target?
155 // FIXME: could use return value of DoActionMove
156 //
157 bool goalReached = goal && MapDistanceBetweenUnits(unit, goal) <= unit->Type->RepairRange;
158 if (goalReached && goal->Variable[HP_INDEX].Value < goal->Variable[HP_INDEX].Max) {
159 unit->State = 0;
160 unit->SubAction = 20;
161 unit->Data.Repair.Progress = 0;
162 UnitHeadingFromDeltaXY(unit,
163 goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
164 goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
165
166 int costs[MaxCosts];
167 CalculateRequestedAmount(unit->Type, unit->Orders[0]->Goal->Type->ProductionCosts, costs);
168 unit->Player->AddToUnitsConsumingResources(unit, costs);
169 } else if (err < 0 || goalReached) {
170 if (goal) { // release reference
171 goal->RefsDecrease();
172 unit->Orders[0]->Goal = NoUnitP;
173 }
174 if (!RestoreSavedOrder(unit)) {
175 unit->ClearAction();
176 unit->State = 0;
177 }
178 return;
179 }
180
181 // FIXME: Should be it already?
182 Assert(unit->Orders[0]->Action == UnitActionRepair);
183 }
184
185 /**
186 ** Animate unit repair
187 **
188 ** @param unit Unit to animate.
189 ** @param goal Unit being repaired.
190 */
191 static int AnimateActionRepair(CUnit *unit, const CUnit *goal)
192 {
193 if (goal) {
194 int dx = (goal->X - unit->X) * TileSizeX
195 + (goal->Type->TileWidth - unit->Type->TileWidth)
196 * TileSizeX / 2
197 + (goal->IX - unit->IX);
198 int dy = (goal->Y - unit->Y) * TileSizeY
199 + (goal->Type->TileHeight - unit->Type->TileHeight)
200 * TileSizeY / 2
201 + (goal->IY - unit->IY);
202 UnitHeadingFromDeltaXY(unit, dx, dy);
203 }
204 UnitShowAnimation(unit, unit->Type->Animations->Repair);
205 return 0;
206 }
207
208 /**
209 ** Do the actual repair.
210 **
211 ** @param unit unit repairing
212 ** @param goal unit being repaired
213 **
214 ** @return true if goal is healed, false otherwise
215 */
216 static bool DoRepair(CUnit *unit, CUnit *goal)
217 {
218 int *pcosts = goal->Type->ProductionCosts;
219 int pcost = pcosts[EnergyCost] ? pcosts[EnergyCost] : pcosts[MagmaCost];
220 bool healed = false;
221
222 if (goal->Orders[0]->Action != UnitActionBuilt) {
223 Assert(goal->Variable[HP_INDEX].Max);
224
225 int *costs = unit->Player->UnitsConsumingResourcesActual[unit];
226 int cost = costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost];
227 int step = pcost / goal->Variable[HP_INDEX].Max;
228 int hp = 0;
229
230 unit->Data.Repair.Progress += cost;
231 while (unit->Data.Repair.Progress > step) {
232 unit->Data.Repair.Progress -= step;
233 ++hp;
234 }
235 goal->Variable[HP_INDEX].Value += hp;
236 if (goal->Variable[HP_INDEX].Value >= goal->Variable[HP_INDEX].Max) {
237 goal->Variable[HP_INDEX].Value = goal->Variable[HP_INDEX].Max;
238 healed = true;
239 }
240 } else {
241 // hp is the current damage taken by the unit.
242 int hp = (goal->Data.Built.Progress * goal->Variable[HP_INDEX].Max) / pcost - goal->Variable[HP_INDEX].Value;
243
244 // Update build progress
245 int *costs = unit->Player->UnitsConsumingResourcesActual[unit];
246 int cost = costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost];
247 goal->Data.Built.Progress += cost * SpeedBuild;
248 if (goal->Data.Built.Progress > pcost) {
249 goal->Data.Built.Progress = pcost;
250 }
251
252 // Keep the same level of damage while increasing HP.
253 goal->Variable[HP_INDEX].Value = (goal->Data.Built.Progress * goal->Stats->Variables[HP_INDEX].Max) / pcost - hp;
254 if (goal->Variable[HP_INDEX].Value >= goal->Variable[HP_INDEX].Max) {
255 goal->Variable[HP_INDEX].Value = goal->Variable[HP_INDEX].Max;
256 healed = true;
257 }
258 }
259 return healed;
260 }
261
262 /**
263 ** Repair unit
264 **
265 ** @param unit Unit that's doing the repairing
266 */
267 static void RepairUnit(CUnit *unit)
268 {
269 CUnit *goal = unit->Orders[0]->Goal;
270 bool visible = goal->IsVisibleAsGoal(unit->Player);
271 bool inrange = MapDistanceBetweenUnits(unit, goal) <= unit->Type->RepairRange;
272 bool healed = false;
273
274 if (goal && visible && inrange) {
275 healed = DoRepair(unit, goal);
276 goal = unit->Orders[0]->Goal;
277 }
278
279 AnimateActionRepair(unit, goal);
280 if (unit->Anim.Unbreakable) {
281 return;
282 }
283
284 // Check if goal is gone.
285 if (goal && !visible) {
286 DebugPrint("repair goal is gone\n");
287 unit->Orders[0]->X = goal->X;
288 unit->Orders[0]->Y = goal->Y;
289 goal->RefsDecrease();
290 unit->Orders[0]->Goal = goal = NoUnitP;
291 NewResetPath(unit);
292 }
293
294 // If goal has moved, chase after it
295 if (goal && !inrange && !healed) {
296 unit->Player->RemoveFromUnitsConsumingResources(unit);
297 unit->State = 0;
298 unit->SubAction = 0;
299 }
300
301 // Done repairing
302 if (!goal || healed) {
303 unit->Player->RemoveFromUnitsConsumingResources(unit);
304 if (goal) { // release reference
305 goal->RefsDecrease();
306 unit->Orders[0]->Goal = NULL;
307 }
308 if (!RestoreSavedOrder(unit)) {
309 unit->ClearAction();
310 unit->State = 0;
311 }
312 return;
313 }
314 }
315
316 /**
317 ** Unit repairs
318 **
319 ** @param unit Unit that's doing the repairing
320 */
321 void HandleActionRepair(CUnit *unit)
322 {
323 if (unit->SubAction <= 10) {
324 MoveToLocation(unit);
325 }
326 if (unit->SubAction == 20) {
327 RepairUnit(unit);
328 }
329 }
330
331 //@}