Debian Codegraph @

engine/action/action_attack.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_attack.cpp - The attack action. */
11 //
12 // (c) Copyright 1998-2007 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 ** @todo FIXME: I should rewrite this action, if only the
33 ** new orders are supported.
34 */
35
36 /*----------------------------------------------------------------------------
37 -- Includes
38 ----------------------------------------------------------------------------*/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "stratagus.h"
45 #include "unittype.h"
46 #include "animation.h"
47 #include "player.h"
48 #include "unit.h"
49 #include "missile.h"
50 #include "actions.h"
51 #include "sound.h"
52 #include "map.h"
53 #include "pathfinder.h"
54
55 /*----------------------------------------------------------------------------
56 -- Defines
57 ----------------------------------------------------------------------------*/
58
59 #define WEAK_TARGET 2 /// Weak target, could be changed
60 #define MOVE_TO_TARGET 4 /// Move to target state
61 #define ATTACK_TARGET 5 /// Attack target state
62
63 /*----------------------------------------------------------------------------
64 -- Functions
65 ----------------------------------------------------------------------------*/
66
67 /**
68 ** Animate unit attack!
69 **
70 ** @param unit Unit, for that the attack animation is played.
71 **
72 ** @todo manage correctly unit with no animation attack.
73 */
74 void AnimateActionAttack(CUnit *unit)
75 {
76 // No animation.
77 // So direct fire missile.
78 // FIXME : wait a little.
79 if (!unit->Type->Animations || !unit->Type->Animations->Attack) {
80 FireMissile(unit);
81 return;
82 }
83 UnitShowAnimation(unit, unit->Type->Animations->Attack);
84 }
85
86 /**
87 ** Check for dead goal.
88 **
89 ** @warning The caller must check, if he likes the restored SavedOrder!
90 **
91 ** @todo If a unit enters an building, than the attack choose an
92 ** other goal, perhaps it is better to wait for the goal?
93 **
94 ** @param unit Unit using the goal.
95 **
96 ** @return 1 if order have changed, 0 else.
97 */
98 static int CheckForDeadGoal(CUnit *unit)
99 {
100 CUnit *goal; // unit->Order[0].Goal
101
102 goal = unit->Orders[0]->Goal;
103 // Position or valid target, it is ok.
104 if (!goal || goal->IsVisibleAsGoal(unit->Player)) {
105 return 0;
106 }
107 // Goal could be destroyed or unseen
108 // So, cannot use type.
109 unit->Orders[0]->X = goal->X;
110 unit->Orders[0]->Y = goal->Y;
111 unit->Orders[0]->MinRange = 0;
112 unit->Orders[0]->Range = 0;
113
114 goal->RefsDecrease();
115 unit->Orders[0]->Goal = NoUnitP;
116 unit->SubAction &= ~WEAK_TARGET; // No target at all, so not weak
117
118 //
119 // If we have a saved order continue this saved order.
120 //
121 if (unit->SavedOrder.Action != UnitActionStill) {
122 unit->ClearAction();
123 *unit->Orders[0] = unit->SavedOrder;
124 unit->SavedOrder.Action = UnitActionStill;
125 unit->SavedOrder.Goal = NoUnitP;
126 // Restart order state.
127 unit->State = 0;
128 NewResetPath(unit);
129 return 1;
130 }
131 // NewResetPath(unit); // Should be useless.
132 return 0;
133 }
134
135 /**
136 ** Change invalid target for new target in range.
137 **
138 ** @param unit Unit to check if goal is in range
139 **
140 ** @return 1 if order(action) have changed, 0 else (if goal change retrun 0).
141 */
142 static int CheckForTargetInRange(CUnit *unit)
143 {
144 CUnit *goal;
145 CUnit *temp;
146
147 //
148 // Target is dead?
149 //
150 if (CheckForDeadGoal(unit)) {
151 return 1;
152 }
153 goal = unit->Orders[0]->Goal;
154
155 //
156 // No goal: if meeting enemy attack it.
157 //
158 if (!goal && unit->Orders[0]->Action != UnitActionAttackGround) {
159 goal = AttackUnitsInReactRange(unit);
160 if (goal) {
161 if (unit->SavedOrder.Action == UnitActionStill) {
162 // Save current command to continue it later.
163 Assert(!unit->Orders[0]->Goal);
164 unit->SavedOrder = *unit->Orders[0];
165 }
166 goal->RefsIncrease();
167 unit->Orders[0]->Goal = goal;
168 unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
169 unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
170 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
171 unit->SubAction |= WEAK_TARGET; // weak target
172 NewResetPath(unit);
173 }
174
175 //
176 // Have a weak target, try a better target.
177 //
178 } else if (goal && (unit->SubAction & WEAK_TARGET)) {
179 temp = AttackUnitsInReactRange(unit);
180 if (temp && temp->Type->Priority > goal->Type->Priority) {
181 goal->RefsDecrease();
182 temp->RefsIncrease();
183 if (unit->SavedOrder.Action == UnitActionStill) {
184 // Save current command to come back.
185 unit->SavedOrder = *unit->Orders[0];
186 if ((goal = unit->SavedOrder.Goal)) {
187 unit->SavedOrder.X = goal->X + goal->Type->TileWidth / 2;
188 unit->SavedOrder.Y = goal->Y + goal->Type->TileHeight / 2;
189 unit->SavedOrder.MinRange = 0;
190 unit->SavedOrder.Range = 0;
191 unit->SavedOrder.Goal = NoUnitP;
192 }
193 }
194 unit->Orders[0]->Goal = goal = temp;
195 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
196 NewResetPath(unit);
197 }
198 }
199
200 Assert(!unit->Type->Vanishes && !unit->Destroyed && !unit->Removed);
201 Assert(unit->Orders[0]->Action == UnitActionAttack ||
202 unit->Orders[0]->Action == UnitActionAttackGround);
203
204 return 0;
205 }
206
207 /**
208 ** Controls moving a unit to its target when attacking
209 **
210 ** @param unit Unit that is attacking and moving
211 */
212 static void MoveToTarget(CUnit *unit)
213 {
214 CUnit *goal;
215 int err;
216
217 Assert(unit);
218 Assert(!unit->Type->Vanishes && !unit->Destroyed && !unit->Removed);
219 Assert(unit->Orders[0]->Action == UnitActionAttack ||
220 unit->Orders[0]->Action == UnitActionAttackGround);
221 Assert(CanMove(unit));
222 Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));
223
224 err = DoActionMove(unit);
225
226 if (unit->Anim.Unbreakable) {
227 return;
228 }
229 //
230 // Look if we have reached the target.
231 //
232 goal = unit->Orders[0]->Goal;
233 if (err == 0 && !goal) {
234 // Check if we're in range when attacking a location and we are waiting
235 if (MapDistanceToUnit(unit->Orders[0]->X, unit->Orders[0]->Y, unit) <
236 unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
237 err = PF_REACHED;
238 }
239 }
240 if (err >= 0) {
241 if (CheckForTargetInRange(unit)) {
242 return;
243 }
244 return;
245 }
246 if (err == PF_REACHED) {
247 //
248 // Have reached target? FIXME: could use the new return code?
249 //
250 if (goal && MapDistanceBetweenUnits(unit, goal) <=
251 unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
252 // Reached another unit, now attacking it
253 unit->State = 0;
254 UnitHeadingFromDeltaXY(unit,
255 goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
256 goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
257 unit->SubAction++;
258 return;
259 }
260 //
261 // Attacking ground.
262 //
263 if (!goal && unit->Orders[0]->Action == UnitActionAttackGround &&
264 MapDistanceToUnit(unit->Orders[0]->X, unit->Orders[0]->Y, unit) <=
265 unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
266 // Reached ground, now attacking it
267 unit->State = 0;
268 UnitHeadingFromDeltaXY(unit, unit->Orders[0]->X - unit->X,
269 unit->Orders[0]->Y - unit->Y);
270 unit->SubAction &= WEAK_TARGET;
271 unit->SubAction |= ATTACK_TARGET;
272 return;
273 }
274 }
275 //
276 // Unreachable.
277 //
278 if (err == PF_UNREACHABLE) {
279 unit->State = 0;
280 if (!goal && unit->Orders[0]->Range == 0) {
281 // Try again with unit's max attack range
282 unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
283 unit->Wait = 15;
284 return;
285 }
286 }
287 //
288 // Return to old task?
289 //
290 unit->State = unit->SubAction = 0;
291 if (unit->Orders[0]->Goal) {
292 unit->Orders[0]->Goal->RefsDecrease();
293 }
294 *unit->Orders[0] = unit->SavedOrder;
295 NewResetPath(unit);
296
297 // Must finish, if saved command finishes
298 unit->SavedOrder.Action = UnitActionStill;
299 unit->SavedOrder.Goal = NoUnitP;
300 }
301
302 /**
303 ** Handle attacking the target.
304 **
305 ** @param unit Unit, for that the attack is handled.
306 */
307 static void AttackTarget(CUnit *unit)
308 {
309 CUnit *goal; // unit->Order[0].Goal
310 CUnit *temp;
311
312 Assert(unit);
313 Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));
314
315 AnimateActionAttack(unit);
316 if (unit->Anim.Unbreakable) {
317 return;
318 }
319 //
320 // Goal is "weak".
321 //
322 goal = unit->Orders[0]->Goal;
323 if (!goal && unit->Orders[0]->Action == UnitActionAttackGround) {
324 return;
325 }
326
327 //
328 // Target is dead ? Change order ?
329 //
330 if (CheckForDeadGoal(unit)) {
331 return;
332 }
333 goal = unit->Orders[0]->Goal;
334
335 //
336 // No target choose one.
337 //
338 if (!goal) {
339 unit->State = 0;
340 goal = AttackUnitsInReactRange(unit);
341 //
342 // No new goal, continue way to destination.
343 //
344 if (!goal) {
345 // Return to old task ?
346 if (unit->SavedOrder.Action != UnitActionStill) {
347 unit->SubAction = 0;
348 Assert(unit->Orders[0]->Goal == NoUnitP);
349 *unit->Orders[0] = unit->SavedOrder;
350 NewResetPath(unit);
351 // Must finish, if saved command finishes
352 unit->SavedOrder.Action = UnitActionStill;
353
354 // This isn't supported
355 Assert(unit->SavedOrder.Goal == NoUnitP);
356 return;
357 }
358 // There's no goal, so the goal can't be weak.
359 Assert(!(unit->SubAction & WEAK_TARGET));
360 unit->SubAction = MOVE_TO_TARGET;
361 return;
362 }
363
364 //
365 // Save current command to come back.
366 //
367 if (unit->SavedOrder.Action == UnitActionStill) {
368 Assert(unit->Orders[0]->Goal == NULL);
369 unit->SavedOrder = *unit->Orders[0];
370 }
371
372 goal->RefsIncrease();
373 unit->Orders[0]->Goal = goal;
374 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
375 unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
376 unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
377 NewResetPath(unit);
378 unit->SubAction |= WEAK_TARGET;
379
380 //
381 // Have a weak target, try a better target.
382 // FIXME: if out of range also try another target quick
383 //
384 } else if (goal && (unit->SubAction & WEAK_TARGET)) {
385 temp = AttackUnitsInReactRange(unit);
386 if (temp && temp->Type->Priority > goal->Type->Priority) {
387 goal->RefsDecrease();
388 temp->RefsIncrease();
389
390 if (unit->SavedOrder.Action == UnitActionStill) {
391 // Save current order to come back or to continue it.
392 unit->SavedOrder = *unit->Orders[0];
393 if ((goal = unit->SavedOrder.Goal)) {
394 DebugPrint("Have goal to come back %d\n" _C_
395 UnitNumber(goal));
396 unit->SavedOrder.X = goal->X + goal->Type->TileWidth / 2;
397 unit->SavedOrder.Y = goal->Y + goal->Type->TileHeight / 2;
398 unit->SavedOrder.MinRange = 0;
399 unit->SavedOrder.Range = 0;
400 unit->SavedOrder.Goal = NoUnitP;
401 }
402 }
403 unit->Orders[0]->Goal = goal = temp;
404 unit->Orders[0]->X = unit->Orders[0]->Y = -1;
405 unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
406 unit->SubAction = MOVE_TO_TARGET | WEAK_TARGET;
407 NewResetPath(unit);
408 }
409 }
410
411 //
412 // Still near to target, if not goto target.
413 //
414 if (MapDistanceBetweenUnits(unit, goal) > unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
415 if (unit->SavedOrder.Action == UnitActionStill) {
416 // Save current order to come back or to continue it.
417 unit->SavedOrder = *unit->Orders[0];
418 if ((temp = unit->SavedOrder.Goal)) {
419 DebugPrint("Have goal to come back %d\n" _C_
420 UnitNumber(temp));
421 unit->SavedOrder.X = temp->X + temp->Type->TileWidth / 2;
422 unit->SavedOrder.Y = temp->Y + temp->Type->TileHeight / 2;
423 unit->SavedOrder.MinRange = 0;
424 unit->SavedOrder.Range = 0;
425 unit->SavedOrder.Goal = NoUnitP;
426 }
427 }
428 NewResetPath(unit);
429 unit->Frame = 0;
430 unit->State = 0;
431 unit->SubAction &= WEAK_TARGET;
432 unit->SubAction |= MOVE_TO_TARGET;
433 }
434 if (MapDistanceBetweenUnits(unit, goal) < unit->Type->MinAttackRange) {
435 unit->SubAction &= WEAK_TARGET;
436 unit->SubAction |= MOVE_TO_TARGET;
437 }
438
439 //
440 // Turn always to target
441 //
442 if (goal) {
443 UnitHeadingFromDeltaXY(unit,
444 goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
445 goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
446 }
447 }
448
449 /**
450 ** Unit attacks!
451 **
452 ** I added a little trick, if SubAction&WEAK_TARGET is true the goal is
453 ** a weak goal. This means the unit AI (little AI) could choose a new
454 ** better goal.
455 **
456 ** @todo Lets do some tries to reach the target.
457 ** If target place is not reachable, choose better goal to reduce
458 ** the pathfinder load.
459 **
460 ** @param unit Unit, for that the attack is handled.
461 */
462 void HandleActionAttack(CUnit *unit)
463 {
464 int dist; // dist between unit and unit->Orders[0]->Goal.
465
466 Assert(unit->Orders[0]->Action == UnitActionAttackGround ||
467 unit->Orders[0]->Action == UnitActionAttack);
468 Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));
469
470 if (unit->Wait) {
471 unit->Wait--;
472 return;
473 }
474
475 switch (unit->SubAction) {
476 //
477 // First entry
478 //
479 case 0:
480 // did Order change ?
481 if (CheckForTargetInRange(unit)) {
482 return;
483 }
484 // CheckForTargetInRange may have set
485 // SubAction |= WEAK_TARGET.
486
487 // Can we already attack ?
488 if (unit->Orders[0]->Goal) {
489 dist = MapDistanceBetweenUnits(unit, unit->Orders[0]->Goal);
490 if (unit->Type->MinAttackRange < dist && dist <= unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
491 UnitHeadingFromDeltaXY(unit,
492 unit->Orders[0]->Goal->X + (unit->Orders[0]->Goal->Type->TileWidth - 1) / 2 - unit->X,
493 unit->Orders[0]->Goal->Y + (unit->Orders[0]->Goal->Type->TileHeight - 1) / 2 - unit->Y);
494 unit->SubAction &= WEAK_TARGET;
495 unit->SubAction |= ATTACK_TARGET;
496 AttackTarget(unit);
497 return;
498 }
499 }
500 unit->SubAction &= WEAK_TARGET;
501 unit->SubAction |= MOVE_TO_TARGET;
502 NewResetPath(unit);
503 //
504 // FIXME: should use a reachable place to reduce pathfinder time.
505 //
506 Assert(unit->State == 0);
507 // FALL THROUGH
508 //
509 // Move near to the target.
510 //
511 case MOVE_TO_TARGET:
512 case MOVE_TO_TARGET + WEAK_TARGET:
513 if (!CanMove(unit)) {
514 // Release order.
515 if (unit->Orders[0]->Goal) {
516 unit->Orders[0]->Goal->RefsDecrease();
517 }
518 *unit->Orders[0] = unit->SavedOrder;
519 unit->SavedOrder.Action = UnitActionStill;
520 unit->SavedOrder.Goal = NoUnitP;
521 unit->State = 0;
522 unit->SubAction = 0;
523 NewResetPath(unit);
524 return;
525 }
526 MoveToTarget(unit);
527 break;
528
529 //
530 // Attack the target.
531 //
532 case ATTACK_TARGET:
533 case ATTACK_TARGET + WEAK_TARGET:
534 AttackTarget(unit);
535 break;
536
537 case WEAK_TARGET:
538 DebugPrint("FIXME: wrong entry.\n");
539 break;
540 }
541 }
542
543 //@}