File: action_attack.cpp

package info (click to toggle)
boswars 2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96,652 kB
  • sloc: cpp: 57,250; python: 1,715; sh: 25; makefile: 17
file content (543 lines) | stat: -rw-r--r-- 15,097 bytes parent folder | download | duplicates (5)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//     ____                _       __               
//    / __ )____  _____   | |     / /___ ___________
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
//                                              
//       A futuristic real-time strategy game.
//          This file is part of Bos Wars.
//
/**@name action_attack.cpp - The attack action. */
//
//      (c) Copyright 1998-2007 by Lutz Sammer and Jimmy Salmon
//
//      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; only version 2 of the License.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.
//

//@{

/**
**  @todo FIXME: I should rewrite this action, if only the
**               new orders are supported.
*/

/*----------------------------------------------------------------------------
--  Includes
----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stratagus.h"
#include "unittype.h"
#include "animation.h"
#include "player.h"
#include "unit.h"
#include "missile.h"
#include "actions.h"
#include "sound.h"
#include "map.h"
#include "pathfinder.h"

/*----------------------------------------------------------------------------
--  Defines
----------------------------------------------------------------------------*/

#define WEAK_TARGET      2  /// Weak target, could be changed
#define MOVE_TO_TARGET   4  /// Move to target state
#define ATTACK_TARGET    5  /// Attack target state

/*----------------------------------------------------------------------------
--  Functions
----------------------------------------------------------------------------*/

/**
**  Animate unit attack!
**
**  @param unit  Unit, for that the attack animation is played.
**
**  @todo manage correctly unit with no animation attack.
*/
void AnimateActionAttack(CUnit *unit)
{
	//  No animation.
	//  So direct fire missile.
	//  FIXME : wait a little.
	if (!unit->Type->Animations || !unit->Type->Animations->Attack) {
		FireMissile(unit);
		return;
	}
	UnitShowAnimation(unit, unit->Type->Animations->Attack);
}

/**
**  Check for dead goal.
**
**  @warning  The caller must check, if he likes the restored SavedOrder!
**
**  @todo     If a unit enters an building, than the attack choose an
**            other goal, perhaps it is better to wait for the goal?
**
**  @param unit  Unit using the goal.
**
**  @return      1 if order have changed, 0 else.
*/
static int CheckForDeadGoal(CUnit *unit)
{
	CUnit *goal; // unit->Order[0].Goal

	goal = unit->Orders[0]->Goal;
	// Position or valid target, it is ok.
	if (!goal || goal->IsVisibleAsGoal(unit->Player)) {
		return 0;
	}
	// Goal could be destroyed or unseen
	// So, cannot use type.
	unit->Orders[0]->X = goal->X;
	unit->Orders[0]->Y = goal->Y;
	unit->Orders[0]->MinRange = 0;
	unit->Orders[0]->Range = 0;

	goal->RefsDecrease();
	unit->Orders[0]->Goal = NoUnitP;
	unit->SubAction &= ~WEAK_TARGET; // No target at all, so not weak

	//
	// If we have a saved order continue this saved order.
	//
	if (unit->SavedOrder.Action != UnitActionStill) {
		unit->ClearAction();
		*unit->Orders[0] = unit->SavedOrder;
		unit->SavedOrder.Action = UnitActionStill;
		unit->SavedOrder.Goal = NoUnitP;
		// Restart order state.
		unit->State = 0;
		NewResetPath(unit);
		return 1;
	}
//	NewResetPath(unit); // Should be useless.
	return 0;
}

/**
**  Change invalid target for new target in range.
**
**  @param unit  Unit to check if goal is in range
**
**  @return      1 if order(action) have changed, 0 else (if goal change retrun 0).
*/
static int CheckForTargetInRange(CUnit *unit)
{
	CUnit *goal;
	CUnit *temp;

	//
	// Target is dead?
	//
	if (CheckForDeadGoal(unit)) {
		return 1;
	}
	goal = unit->Orders[0]->Goal;

	//
	// No goal: if meeting enemy attack it.
	//
	if (!goal && unit->Orders[0]->Action != UnitActionAttackGround) {
		goal = AttackUnitsInReactRange(unit);
		if (goal) {
			if (unit->SavedOrder.Action == UnitActionStill) {
				// Save current command to continue it later.
				Assert(!unit->Orders[0]->Goal);
				unit->SavedOrder = *unit->Orders[0];
			}
			goal->RefsIncrease();
			unit->Orders[0]->Goal = goal;
			unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
			unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
			unit->Orders[0]->X = unit->Orders[0]->Y = -1;
			unit->SubAction |= WEAK_TARGET; // weak target
			NewResetPath(unit);
		}

	//
	// Have a weak target, try a better target.
	//
	} else if (goal && (unit->SubAction & WEAK_TARGET)) {
		temp = AttackUnitsInReactRange(unit);
		if (temp && temp->Type->Priority > goal->Type->Priority) {
			goal->RefsDecrease();
			temp->RefsIncrease();
			if (unit->SavedOrder.Action == UnitActionStill) {
				// Save current command to come back.
				unit->SavedOrder = *unit->Orders[0];
				if ((goal = unit->SavedOrder.Goal)) {
					unit->SavedOrder.X = goal->X + goal->Type->TileWidth / 2;
					unit->SavedOrder.Y = goal->Y + goal->Type->TileHeight / 2;
					unit->SavedOrder.MinRange = 0;
					unit->SavedOrder.Range = 0;
					unit->SavedOrder.Goal = NoUnitP;
				}
			}
			unit->Orders[0]->Goal = goal = temp;
			unit->Orders[0]->X = unit->Orders[0]->Y = -1;
			NewResetPath(unit);
		}
	}

	Assert(!unit->Type->Vanishes && !unit->Destroyed && !unit->Removed);
	Assert(unit->Orders[0]->Action == UnitActionAttack ||
		unit->Orders[0]->Action == UnitActionAttackGround);

	return 0;
}

/**
**  Controls moving a unit to its target when attacking
**
**  @param unit  Unit that is attacking and moving
*/
static void MoveToTarget(CUnit *unit)
{
	CUnit *goal;
	int err;

	Assert(unit);
	Assert(!unit->Type->Vanishes && !unit->Destroyed && !unit->Removed);
	Assert(unit->Orders[0]->Action == UnitActionAttack ||
		unit->Orders[0]->Action == UnitActionAttackGround);
	Assert(CanMove(unit));
	Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));

	err = DoActionMove(unit);

	if (unit->Anim.Unbreakable) {
		return;
	}
	//
	// Look if we have reached the target.
	//
	goal = unit->Orders[0]->Goal;
	if (err == 0 && !goal) {
		// Check if we're in range when attacking a location and we are waiting
		if (MapDistanceToUnit(unit->Orders[0]->X, unit->Orders[0]->Y, unit) <
				unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
			err = PF_REACHED;
		}
	}
	if (err >= 0) {
		if (CheckForTargetInRange(unit)) {
			return;
		}
		return;
	}
	if (err == PF_REACHED) {
		//
		// Have reached target? FIXME: could use the new return code?
		//
		if (goal && MapDistanceBetweenUnits(unit, goal) <=
				unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
			// Reached another unit, now attacking it
			unit->State = 0;
			UnitHeadingFromDeltaXY(unit,
				goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
				goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
			unit->SubAction++;
			return;
		}
		//
		// Attacking ground.
		//
		if (!goal && unit->Orders[0]->Action == UnitActionAttackGround &&
				MapDistanceToUnit(unit->Orders[0]->X, unit->Orders[0]->Y, unit) <=
					unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
			// Reached ground, now attacking it
			unit->State = 0;
			UnitHeadingFromDeltaXY(unit, unit->Orders[0]->X - unit->X,
				unit->Orders[0]->Y - unit->Y);
			unit->SubAction &= WEAK_TARGET;
			unit->SubAction |= ATTACK_TARGET;
			return;
		}
	}
	//
	// Unreachable.
	//
	if (err == PF_UNREACHABLE) {
		unit->State = 0;
		if (!goal && unit->Orders[0]->Range == 0) {
			// Try again with unit's max attack range
			unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
			unit->Wait = 15;
			return;
		}
	}
	//
	// Return to old task?
	//
	unit->State = unit->SubAction = 0;
	if (unit->Orders[0]->Goal) {
		unit->Orders[0]->Goal->RefsDecrease();
	}
	*unit->Orders[0] = unit->SavedOrder;
	NewResetPath(unit);

	// Must finish, if saved command finishes
	unit->SavedOrder.Action = UnitActionStill;
	unit->SavedOrder.Goal = NoUnitP;
}

/**
**  Handle attacking the target.
**
**  @param unit  Unit, for that the attack is handled.
*/
static void AttackTarget(CUnit *unit)
{
	CUnit *goal; // unit->Order[0].Goal
	CUnit *temp;

	Assert(unit);
	Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));

	AnimateActionAttack(unit);
	if (unit->Anim.Unbreakable) {
		return;
	}
	//
	// Goal is "weak".
	//
	goal = unit->Orders[0]->Goal;
	if (!goal && unit->Orders[0]->Action == UnitActionAttackGround) {
		return;
	}

	//
	// Target is dead ? Change order ?
	//
	if (CheckForDeadGoal(unit)) {
		return;
	}
	goal = unit->Orders[0]->Goal;

	//
	// No target choose one.
	//
	if (!goal) {
		unit->State = 0;
		goal = AttackUnitsInReactRange(unit);
		//
		// No new goal, continue way to destination.
		//
		if (!goal) {
			// Return to old task ?
			if (unit->SavedOrder.Action != UnitActionStill) {
				unit->SubAction = 0;
				Assert(unit->Orders[0]->Goal == NoUnitP);
				*unit->Orders[0] = unit->SavedOrder;
				NewResetPath(unit);
				// Must finish, if saved command finishes
				unit->SavedOrder.Action = UnitActionStill;

				// This isn't supported
				Assert(unit->SavedOrder.Goal == NoUnitP);
				return;
			}
			// There's no goal, so the goal can't be weak.
			Assert(!(unit->SubAction & WEAK_TARGET));
			unit->SubAction = MOVE_TO_TARGET;
			return;
		}

		//
		// Save current command to come back.
		//
		if (unit->SavedOrder.Action == UnitActionStill) {
			Assert(unit->Orders[0]->Goal == NULL);
			unit->SavedOrder = *unit->Orders[0];
		}

		goal->RefsIncrease();
		unit->Orders[0]->Goal = goal;
		unit->Orders[0]->X = unit->Orders[0]->Y = -1;
		unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
		unit->Orders[0]->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
		NewResetPath(unit);
		unit->SubAction |= WEAK_TARGET;

	//
	// Have a weak target, try a better target.
	// FIXME: if out of range also try another target quick
	//
	} else if (goal && (unit->SubAction & WEAK_TARGET)) {
		temp = AttackUnitsInReactRange(unit);
		if (temp && temp->Type->Priority > goal->Type->Priority) {
			goal->RefsDecrease();
			temp->RefsIncrease();

			if (unit->SavedOrder.Action == UnitActionStill) {
				// Save current order to come back or to continue it.
				unit->SavedOrder = *unit->Orders[0];
				if ((goal = unit->SavedOrder.Goal)) {
					DebugPrint("Have goal to come back %d\n" _C_
						UnitNumber(goal));
					unit->SavedOrder.X = goal->X + goal->Type->TileWidth / 2;
					unit->SavedOrder.Y = goal->Y + goal->Type->TileHeight / 2;
					unit->SavedOrder.MinRange = 0;
					unit->SavedOrder.Range = 0;
					unit->SavedOrder.Goal = NoUnitP;
				}
			}
			unit->Orders[0]->Goal = goal = temp;
			unit->Orders[0]->X = unit->Orders[0]->Y = -1;
			unit->Orders[0]->MinRange = unit->Type->MinAttackRange;
			unit->SubAction = MOVE_TO_TARGET | WEAK_TARGET;
			NewResetPath(unit);
		}
	}

	//
	// Still near to target, if not goto target.
	//
	if (MapDistanceBetweenUnits(unit, goal) > unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
		if (unit->SavedOrder.Action == UnitActionStill) {
			// Save current order to come back or to continue it.
			unit->SavedOrder = *unit->Orders[0];
			if ((temp = unit->SavedOrder.Goal)) {
				DebugPrint("Have goal to come back %d\n" _C_
					UnitNumber(temp));
				unit->SavedOrder.X = temp->X + temp->Type->TileWidth / 2;
				unit->SavedOrder.Y = temp->Y + temp->Type->TileHeight / 2;
				unit->SavedOrder.MinRange = 0;
				unit->SavedOrder.Range = 0;
				unit->SavedOrder.Goal = NoUnitP;
			}
		}
		NewResetPath(unit);
		unit->Frame = 0;
		unit->State = 0;
		unit->SubAction &= WEAK_TARGET;
		unit->SubAction |= MOVE_TO_TARGET;
	}
	if (MapDistanceBetweenUnits(unit, goal) < unit->Type->MinAttackRange) {
		unit->SubAction &= WEAK_TARGET;
		unit->SubAction |= MOVE_TO_TARGET;
	}

	//
	// Turn always to target
	//
	if (goal) {
		UnitHeadingFromDeltaXY(unit,
			goal->X + (goal->Type->TileWidth - 1) / 2 - unit->X,
			goal->Y + (goal->Type->TileHeight - 1) / 2 - unit->Y);
	}
}

/**
**  Unit attacks!
**
**  I added a little trick, if SubAction&WEAK_TARGET is true the goal is
**  a weak goal.  This means the unit AI (little AI) could choose a new
**  better goal.
**
**  @todo  Lets do some tries to reach the target.
**         If target place is not reachable, choose better goal to reduce
**         the pathfinder load.
**
**  @param unit  Unit, for that the attack is handled.
*/
void HandleActionAttack(CUnit *unit)
{
	int dist;          // dist between unit and unit->Orders[0]->Goal.

	Assert(unit->Orders[0]->Action == UnitActionAttackGround ||
		unit->Orders[0]->Action == UnitActionAttack);
	Assert(unit->Orders[0]->Goal || (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1));

	if (unit->Wait) {
		unit->Wait--;
		return;
	}

	switch (unit->SubAction) {
		//
		// First entry
		//
		case 0:
			// did Order change ?
			if (CheckForTargetInRange(unit)) {
				return;
			}
			// CheckForTargetInRange may have set
			// SubAction |= WEAK_TARGET.

			// Can we already attack ?
			if (unit->Orders[0]->Goal) {
				dist = MapDistanceBetweenUnits(unit, unit->Orders[0]->Goal);
				if (unit->Type->MinAttackRange < dist && dist <= unit->Stats->Variables[ATTACKRANGE_INDEX].Max) {
					UnitHeadingFromDeltaXY(unit,
						unit->Orders[0]->Goal->X + (unit->Orders[0]->Goal->Type->TileWidth - 1) / 2 - unit->X,
						unit->Orders[0]->Goal->Y + (unit->Orders[0]->Goal->Type->TileHeight - 1) / 2 - unit->Y);
					unit->SubAction &= WEAK_TARGET;
					unit->SubAction |= ATTACK_TARGET;
					AttackTarget(unit);
					return;
				}
			}
			unit->SubAction &= WEAK_TARGET;
			unit->SubAction |= MOVE_TO_TARGET;
			NewResetPath(unit);
			//
			// FIXME: should use a reachable place to reduce pathfinder time.
			//
			Assert(unit->State == 0);
			// FALL THROUGH
		//
		// Move near to the target.
		//
		case MOVE_TO_TARGET:
		case MOVE_TO_TARGET + WEAK_TARGET:
			if (!CanMove(unit)) {
				// Release order.
				if (unit->Orders[0]->Goal) {
					unit->Orders[0]->Goal->RefsDecrease();
				}
				*unit->Orders[0] = unit->SavedOrder;
				unit->SavedOrder.Action = UnitActionStill;
				unit->SavedOrder.Goal = NoUnitP;
				unit->State = 0;
				unit->SubAction = 0;
				NewResetPath(unit);
				return;
			}
			MoveToTarget(unit);
			break;

		//
		// Attack the target.
		//
		case ATTACK_TARGET:
		case ATTACK_TARGET + WEAK_TARGET:
			AttackTarget(unit);
			break;

		case WEAK_TARGET:
			DebugPrint("FIXME: wrong entry.\n");
			break;
	}
}

//@}