Debian Codegraph @

engine/action/actions.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 actions.cpp - The actions. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer, Russell Smith, 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 #include <string.h>
38 #include <time.h>
39
40 #include "stratagus.h"
41 #include "version.h"
42 #include "unittype.h"
43 #include "animation.h"
44 #include "player.h"
45 #include "unit.h"
46 #include "actions.h"
47 #include "missile.h"
48 #include "interface.h"
49 #include "map.h"
50 #include "sound.h"
51 #include "spells.h"
52
53 /*----------------------------------------------------------------------------
54 -- Variables
55 ----------------------------------------------------------------------------*/
56
57 unsigned SyncHash; /// Hash calculated to find sync failures
58
59 /*----------------------------------------------------------------------------
60 -- Functions
61 ----------------------------------------------------------------------------*/
62
63 /*----------------------------------------------------------------------------
64 -- Animation
65 ----------------------------------------------------------------------------*/
66
67 /**
68 ** Rotate a unit
69 **
70 ** @param unit Unit to rotate
71 ** @param rotate Number of frames to rotate (>0 clockwise, <0 counterclockwise)
72 */
73 static void UnitRotate(CUnit *unit, int rotate)
74 {
75 unit->Direction += rotate * 256 / unit->Type->NumDirections;
76 UnitUpdateHeading(unit);
77 }
78
79 /**
80 ** Show unit animation.
81 **
82 ** @param unit Unit of the animation.
83 ** @param anim Animation script to handle.
84 **
85 ** @return The flags of the current script step.
86 */
87 int UnitShowAnimation(CUnit *unit, const CAnimation *anim)
88 {
89 return UnitShowAnimationScaled(unit, anim, MapFieldNormalCost);
90 }
91
92 /**
93 ** Show unit animation.
94 **
95 ** @param unit Unit of the animation.
96 ** @param anim Animation script to handle.
97 ** @param scale Scaling factor of the wait times in animation
98 ** (MapFieldNormalCost means no scaling).
99 **
100 ** @return The flags of the current script step.
101 */
102 int UnitShowAnimationScaled(CUnit *unit, const CAnimation *anim, int scale)
103 {
104 int move;
105
106 // Changing animations
107 if (unit->Anim.CurrAnim != anim) {
108 // Assert fails when transforming unit (upgrade-to).
109 Assert(!unit->Anim.Unbreakable);
110 unit->Anim.Anim = unit->Anim.CurrAnim = anim;
111 unit->Anim.Wait = 0;
112 }
113
114 // Currently waiting
115 if (unit->Anim.Wait) {
116 --unit->Anim.Wait;
117 if (!unit->Anim.Wait) {
118 // Advance to next frame
119 unit->Anim.Anim = unit->Anim.Anim->Next;
120 if (!unit->Anim.Anim) {
121 unit->Anim.Anim = unit->Anim.CurrAnim;
122 }
123 }
124 return 0;
125 }
126
127 move = 0;
128 while (!unit->Anim.Wait) {
129 switch (unit->Anim.Anim->Type) {
130 case AnimationFrame:
131 unit->Frame = unit->Anim.Anim->D.Frame.Frame;
132 UnitUpdateHeading(unit);
133 break;
134 case AnimationExactFrame:
135 unit->Frame = unit->Anim.Anim->D.Frame.Frame;
136 break;
137 case AnimationRandomFrame:
138 unit->Frame = unit->Anim.Anim->D.RandomFrame.MinFrame +
139 SyncRand() % (unit->Anim.Anim->D.RandomFrame.MaxFrame - unit->Anim.Anim->D.RandomFrame.MinFrame + 1);
140 UnitUpdateHeading(unit);
141 break;
142
143 case AnimationWait:
144 unit->Anim.Wait = (unit->Anim.Anim->D.Wait.Wait * scale) >> MapFieldNormalSpeed;
145 if (unit->Anim.Wait <= 0)
146 unit->Anim.Wait = 1;
147 break;
148 case AnimationRandomWait:
149 unit->Anim.Wait = unit->Anim.Anim->D.RandomWait.MinWait +
150 SyncRand() % (unit->Anim.Anim->D.RandomWait.MaxWait - unit->Anim.Anim->D.RandomWait.MinWait + 1);
151 break;
152
153 case AnimationSound:
154 if (unit->IsVisible(ThisPlayer) || ReplayRevealMap) {
155 PlayUnitSound(unit, unit->Anim.Anim->D.Sound.Sound);
156 }
157 break;
158 case AnimationRandomSound:
159 if (unit->IsVisible(ThisPlayer) || ReplayRevealMap) {
160 int sound;
161 sound = SyncRand() % unit->Anim.Anim->D.RandomSound.NumSounds;
162 PlayUnitSound(unit, unit->Anim.Anim->D.RandomSound.Sound[sound]);
163 }
164 break;
165
166 case AnimationAttack:
167 if (unit->Orders[0]->Action == UnitActionSpellCast) {
168 if (unit->Orders[0]->Goal &&
169 !unit->Orders[0]->Goal->IsVisibleAsGoal(unit->Player)) {
170 unit->ReCast = 0;
171 } else {
172 unit->ReCast = SpellCast(unit, unit->Orders[0]->Arg1.Spell,
173 unit->Orders[0]->Goal, unit->Orders[0]->X, unit->Orders[0]->Y);
174 }
175 } else {
176 FireMissile(unit);
177 }
178 break;
179
180 case AnimationRotate:
181 UnitRotate(unit, unit->Anim.Anim->D.Rotate.Rotate);
182 break;
183 case AnimationRandomRotate:
184 if ((SyncRand() >> 8) & 1) {
185 UnitRotate(unit, -unit->Anim.Anim->D.Rotate.Rotate);
186 } else {
187 UnitRotate(unit, unit->Anim.Anim->D.Rotate.Rotate);
188 }
189 break;
190
191 case AnimationMove:
192 Assert(!move);
193 move = unit->Anim.Anim->D.Move.Move;
194 break;
195
196 case AnimationUnbreakable:
197 Assert(unit->Anim.Unbreakable ^ unit->Anim.Anim->D.Unbreakable.Begin);
198 unit->Anim.Unbreakable = unit->Anim.Anim->D.Unbreakable.Begin;
199 break;
200
201 case AnimationNone:
202 case AnimationLabel:
203 break;
204
205 case AnimationGoto:
206 unit->Anim.Anim = unit->Anim.Anim->D.Goto.Goto;
207 break;
208 case AnimationRandomGoto:
209 if (SyncRand() % 100 < unit->Anim.Anim->D.RandomGoto.Random) {
210 unit->Anim.Anim = unit->Anim.Anim->D.RandomGoto.Goto;
211 }
212 break;
213 }
214
215 if (!unit->Anim.Wait) {
216 // Advance to next frame
217 unit->Anim.Anim = unit->Anim.Anim->Next;
218 if (!unit->Anim.Anim) {
219 unit->Anim.Anim = unit->Anim.CurrAnim;
220 }
221 }
222 }
223
224 --unit->Anim.Wait;
225 if (!unit->Anim.Wait) {
226 // Advance to next frame
227 unit->Anim.Anim = unit->Anim.Anim->Next;
228 if (!unit->Anim.Anim) {
229 unit->Anim.Anim = unit->Anim.CurrAnim;
230 }
231 }
232 return move;
233 }
234
235 /*----------------------------------------------------------------------------
236 -- Actions
237 ----------------------------------------------------------------------------*/
238
239 /**
240 ** Unit does nothing!
241 **
242 ** @param unit Unit pointer for none action.
243 */
244 static void HandleActionNone(CUnit *unit)
245 {
246 DebugPrint("FIXME: Should not happen!\n");
247 DebugPrint("FIXME: Unit (%d) %s has action none.!\n" _C_
248 UnitNumber(unit) _C_ unit->Type->Ident.c_str());
249 }
250
251 /**
252 ** Jump table for actions.
253 **
254 ** @note can move function into unit structure.
255 */
256 static void (*HandleActionTable[])(CUnit *) = {
257 HandleActionNone,
258 HandleActionStill,
259 HandleActionStandGround,
260 HandleActionFollow,
261 HandleActionMove,
262 HandleActionAttack,
263 HandleActionAttack, // HandleActionAttackGround,
264 HandleActionDie,
265 HandleActionSpellCast,
266 HandleActionTrain,
267 HandleActionBuilt,
268 HandleActionBoard,
269 HandleActionUnload,
270 HandleActionPatrol,
271 HandleActionBuild,
272 HandleActionRepair,
273 HandleActionResource,
274 };
275
276 /**
277 ** Increment a unit's health
278 **
279 ** @param unit the unit to operate on
280 */
281 static void HandleRegenerations(CUnit *unit)
282 {
283 int f = 0;
284
285 // Burn
286 if (!unit->Removed && !unit->Destroyed && unit->Variable[HP_INDEX].Max &&
287 unit->Orders[0]->Action != UnitActionBuilt &&
288 unit->Orders[0]->Action != UnitActionDie) {
289 f = (100 * unit->Variable[HP_INDEX].Value) / unit->Variable[HP_INDEX].Max;
290 if (f <= unit->Type->BurnPercent && unit->Type->BurnDamageRate) {
291 HitUnit(NoUnitP, unit, unit->Type->BurnDamageRate);
292 f = 1;
293 } else {
294 f = 0;
295 }
296 }
297
298 // Health doesn't regenerate while burning.
299 unit->Variable[HP_INDEX].Increase = f ? 0 : unit->Stats->Variables[HP_INDEX].Increase;
300 }
301
302 /**
303 ** Handle things about the unit that decay over time
304 **
305 ** @param unit The unit that the decay is handled for
306 ** @param amount The amount of time to make up for.(in cycles)
307 */
308 static void HandleBuffs(CUnit *unit, int amount)
309 {
310 //
311 // Look if the time to live is over.
312 //
313 if (unit->TTL && unit->TTL < (GameCycle - unit->Variable[HP_INDEX].Value)) {
314 DebugPrint("Unit must die %lu %lu!\n" _C_ unit->TTL _C_ GameCycle);
315 //
316 // Hit unit does some funky stuff...
317 //
318 unit->Variable[HP_INDEX].Value -= amount;
319 if (unit->Variable[HP_INDEX].Value <= 0) {
320 LetUnitDie(unit);
321 }
322 }
323
324 // User defined variables
325 for (int i = 0; i < UnitTypeVar.NumberVariable; i++) {
326 if (unit->Variable[i].Enable && unit->Variable[i].Increase) {
327 unit->Variable[i].Value += unit->Variable[i].Increase;
328 if (unit->Variable[i].Value <= 0) {
329 unit->Variable[i].Value = 0;
330 } else if (unit->Variable[i].Value > unit->Variable[i].Max) {
331 unit->Variable[i].Value = unit->Variable[i].Max;
332 }
333 }
334 }
335 }
336
337
338 static void RunAction(unsigned char action, CUnit *unit)
339 {
340 HandleActionTable[action](unit);
341 }
342
343
344 /**
345 ** Handle the action of a unit.
346 **
347 ** @param unit Pointer to handled unit.
348 */
349 static void HandleUnitAction(CUnit *unit)
350 {
351 //
352 // If current action is breakable proceed with next one.
353 //
354 if (!unit->Anim.Unbreakable) {
355 //
356 // o Look if we have a new order and old finished.
357 // o Or the order queue should be flushed.
358 //
359 if (unit->OrderCount > 1 &&
360 (unit->Orders[0]->Action == UnitActionStill || unit->OrderFlush)) {
361
362 if (unit->Removed) { // FIXME: johns I see this as an error
363 DebugPrint("Flushing removed unit\n");
364 // This happens, if building with ALT+SHIFT.
365 return;
366 }
367
368 //
369 // Release pending references.
370 //
371 if (unit->Orders[0]->Goal) {
372 // Still shouldn't have a reference unless attacking
373 Assert(!(unit->Orders[0]->Action == UnitActionStill && !unit->SubAction));
374 unit->Orders[0]->Goal->RefsDecrease();
375 }
376
377 UnitRemoveConsumingResources(unit);
378
379 //
380 // Shift queue with structure assignment.
381 //
382 unit->OrderCount--;
383 unit->OrderFlush = 0;
384 delete unit->Orders[0];
385 for (int z = 0; z < unit->OrderCount; ++z) {
386 unit->Orders[z] = unit->Orders[z + 1];
387 }
388 unit->Orders.pop_back();
389
390 //
391 // Note subaction 0 should reset.
392 //
393 unit->SubAction = unit->State = 0;
394 unit->Wait = 0;
395
396 if (IsOnlySelected(unit)) { // update display for new action
397 SelectedUnitChanged();
398 }
399 }
400 }
401
402 //
403 // Select action.
404 //
405 RunAction(unit->Orders[0]->Action, unit);
406 }
407
408 /**
409 ** Update the actions of all units each game cycle.
410 **
411 ** @todo To improve the preformance use slots for waiting.
412 */
413 void UnitActions(void)
414 {
415 CUnit *table[UnitMax];
416 CUnit *unit;
417 int blinkthiscycle;
418 int buffsthiscycle;
419 int regenthiscycle;
420 int i;
421 int tabsize;
422
423 buffsthiscycle = regenthiscycle = blinkthiscycle =
424 !(GameCycle % CYCLES_PER_SECOND);
425
426 memcpy(table, Units, NumUnits * sizeof(CUnit *));
427 tabsize = NumUnits;
428
429 //
430 // Check for things that only happen every few cycles
431 // (faster in their own loops.)
432 //
433
434 // 1) Blink flag.
435 if (blinkthiscycle) {
436 for (i = 0; i < tabsize; ++i) {
437 if (table[i]->Destroyed) {
438 table[i--] = table[--tabsize];
439 continue;
440 }
441 if (table[i]->Blink) {
442 --table[i]->Blink;
443 }
444 }
445 }
446
447 // 2) Buffs...
448 if (buffsthiscycle) {
449 for (i = 0; i < tabsize; ++i) {
450 if (table[i]->Destroyed) {
451 table[i--] = table[--tabsize];
452 continue;
453 }
454 HandleBuffs(table[i], CYCLES_PER_SECOND);
455 }
456 }
457
458 // 3) Increase health, charge, burn and stuff
459 if (regenthiscycle) {
460 for (i = 0; i < tabsize; ++i) {
461 if (table[i]->Destroyed) {
462 table[i--] = table[--tabsize];
463 continue;
464 }
465 HandleRegenerations(table[i]);
466 }
467 }
468
469 //
470 // Do all actions
471 //
472 for (i = 0; i < tabsize; ++i) {
473 while (table[i]->Destroyed) {
474 table[i] = table[--tabsize];
475 }
476 unit = table[i];
477
478 HandleUnitAction(unit);
479
480 #ifdef DEBUG_LOG
481 //
482 // Dump the unit to find the network sync bugs.
483 //
484 {
485 static FILE *logf;
486
487 if (!logf) {
488 time_t now;
489 char buf[256];
490
491 sprintf_s(buf, sizeof(buf), "log_of_boswars_%d.log", ThisPlayer->Index);
492 logf = fopen(buf, "wb");
493 if (!logf) {
494 return;
495 }
496 fprintf(logf, ";;; Log file generated by Bos Wars Version "
497 VERSION "\n");
498 time(&now);
499 fprintf(logf, ";;;\tDate: %s", ctime(&now));
500 fprintf(logf, ";;;\tMap: %s\n\n", Map.Info.Description.c_str());
501 }
502
503 fprintf(logf, "%lu: ", GameCycle);
504 fprintf(logf, "%d %s S%d/%d-%d P%d Refs %d: %X %d,%d %d,%d\n",
505 UnitNumber(unit), unit->Type ? unit->Type->Ident.c_str() : "unit-killed",
506 unit->State, unit->SubAction,
507 !unit->Orders.empty() ? unit->Orders[0]->Action : -1,
508 unit->Player ? unit->Player->Index : -1, unit->Refs,SyncRandSeed,
509 unit->X, unit->Y, unit->IX, unit->IY);
510
511 // SaveUnit(unit,logf);
512 fflush(NULL);
513 }
514 #endif
515 //
516 // Calculate some hash.
517 //
518 SyncHash = (SyncHash << 5) | (SyncHash >> 27);
519 SyncHash ^= !unit->Orders.empty() ? unit->Orders[0]->Action << 18 : 0;
520 SyncHash ^= unit->State << 12;
521 SyncHash ^= unit->SubAction << 6;
522 SyncHash ^= unit->Refs << 3;
523 }
524 }
525
526 //@}