-
Notifications
You must be signed in to change notification settings - Fork 6
/
enemy.c
902 lines (715 loc) · 26.2 KB
/
enemy.c
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
*
* Copyright (c) 1994, 2002, 2003 Johannes Prix
* Copyright (c) 1994, 2002, 2003 Reinhard Prix
*
*
* This file is part of Freedroid
*
* Freedroid 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; either version 2 of the License, or
* (at your option) any later version.
*
* Freedroid 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 Freedroid; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
/*----------------------------------------------------------------------
*
* Desc: All enemy - realted functions. This includes their whole behaviour,
* healing, initialization, shuffling them around after evevator-transitions
* of the paradroid, deleting them, collisions of enemys among themselves,
* their fireing, animation and such.
*
*----------------------------------------------------------------------*/
#define _enemy_c
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"
#define COL_SPEED 3 /* wegstossen bei enemy-enemy collision */
#define FIREDIST2 8 // according to the intro, the laser can be "focused on any target
// within a range of eight metres"
// local prototypes
void CheckDroidDistribution (int levelnum);
int DirectLineWalkable( float x1 , float y1 , float x2 , float y2 );
void PermanentHealRobots (void);
void SelectNextWaypointClassical( int EnemyNum );
int CheckIfWayIsFreeOfDroids ( float x1 , float y1 , float x2 , float y2 , int OurLevel , int ExceptedDroid );
void MoveThisRobotThowardsHisWaypoint ( int EnemyNum );
void SelectNextWaypointAdvanced ( int EnemyNum );
void MoveThisEnemy( int EnemyNum );
//----------------------------------------------------------------------
// for debug purposes: check if there are any droid "piles" on this level
//----------------------------------------------------------------------
void
CheckDroidDistribution (int levelnum)
{
int i;
bool ok = TRUE;
for (i=0; i< NumEnemys; i++)
if ( (AllEnemys[i].levelnum == levelnum) && CheckEnemyEnemyCollision (i) )
{
DebugPrintf (0, "We found a droid collision of droid Nr: %d\n", i);
ok = FALSE;
}
if (ok)
DebugPrintf (0, "Seems ok.\n");
DebugPrintf (0, "\n NR. ID X Y ENERGY speedX\n");
for (i = 0; i < NumEnemys; i++)
{
if ( (AllEnemys[i].levelnum == CurLevel->levelnum) && (AllEnemys[i].energy > 0) )
DebugPrintf (0, "%d. %s %d %d %d %g.\n", i,
Druidmap[AllEnemys[i].type].druidname,
(int)AllEnemys[i].pos.x,
(int)AllEnemys[i].pos.y,
(int)AllEnemys[i].energy,
AllEnemys[i].speed.x);
} /* for (i<NumEnemys) */
return;
}
/* ----------------------------------------------------------------------
* This function tests, if a Robot can go a direct straigt line from
* x1 y1 to x2 y2 without hitting a wall or another obstacle.
*
* The return value is TRUE or FALSE accoringly.
* ----------------------------------------------------------------------*/
int
DirectLineWalkable( float x1 , float y1 , float x2 , float y2 )
{
float LargerDistance;
int Steps;
int i;
finepoint step;
finepoint CheckPosition;
DebugPrintf( 1 , "\nint DirectLineWalkable (...) : Checking from %d-%d to %d-%d.", (int) x1, (int) y1 , (int) x2, (int) y2 );
fflush(stdout);
if ( fabsf(x1-x2) > fabsf (y1-y2) ) LargerDistance=fabsf(x1-x2);
else LargerDistance=fabsf(y1-y2);
Steps=LargerDistance * 4 ; // We check four times on each map tile...
if ( Steps == 0 ) return TRUE;
// We determine the step size when walking from (x1,y1) to (x2,y2) in Steps number of steps
step.x = (x2 - x1) / Steps;
step.y = (y2 - y1) / Steps;
DebugPrintf( 2 , "\nint DirectLineWalkable (...) : step.x=%f step.y=%f." , step.x , step.y );
// We start from position (x1, y1)
CheckPosition.x = x1;
CheckPosition.y = y1;
for ( i = 0 ; i < Steps ; i++ )
{
if ( IsPassable ( CheckPosition.x , CheckPosition.y , CENTER ) != CENTER )
{
DebugPrintf( 1 , "\n DirectLineWalkable (...) : Connection analysis revealed : OBSTACLES!! NO WAY!!!");
return FALSE;
}
CheckPosition.x += step.x;
CheckPosition.y += step.y;
}
DebugPrintf( 1 , "\n DirectLineWalkable (...) : Connection analysis revealed : FREE!");
return TRUE;
}; // int DirectLineWalkable( float x1 , float y1 , float x2 , float y2 )
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
PermanentHealRobots (void)
{
int i;
for (i = 0; i < NumEnemys; i++)
{
if (AllEnemys[i].status == OUT || AllEnemys[i].energy <= 0.0)
continue;
if (AllEnemys[i].energy < Druidmap[AllEnemys[i].type].maxenergy)
AllEnemys[i].energy += Druidmap[AllEnemys[i].type].lose_health * Frame_Time();
}
} // void PermanentHealRobots(void)
/*-----------------------------------------------------------------
* @Desc: setzt AllEnemys - Array auf 0
*
*
*-----------------------------------------------------------------*/
void
ClearEnemys (void)
{
int i;
for (i = 0; i < MAX_ENEMYS_ON_SHIP; i++)
{
AllEnemys[i].type = -1;
AllEnemys[i].levelnum = -1;
AllEnemys[i].phase = 0;
AllEnemys[i].nextwaypoint = AllEnemys[i].lastwaypoint = 0;
AllEnemys[i].status = OUT;
AllEnemys[i].warten = 0;
AllEnemys[i].firewait = 0;
AllEnemys[i].energy = -1;
AllEnemys[i].TextVisibleTime = 0;
AllEnemys[i].TextToBeDisplayed = "";
}
NumEnemys = 0;
return;
} /* ClearEnemys() */
/*-----------------------------------------------------------------
* @Desc: Vermischt enemys in CurLevel auf die Waypoints
*
*
*-----------------------------------------------------------------*/
void
ShuffleEnemys (void)
{
int curlevel = CurLevel->levelnum;
int i;
int nth_enemy;
int wp, num_wp;
bool used_wp[MAXWAYPOINTS];
bool warned = FALSE;
num_wp = CurLevel->num_waypoints;
// keep a little list of which waypoints have already been used
for ( i=0 ; i < num_wp; i++)
used_wp[i] = FALSE;
nth_enemy = 0;
for (i = 0; i < NumEnemys ; i++)
{
if (AllEnemys[i].status == OUT || AllEnemys[i].levelnum != curlevel)
continue; /* dont handle dead enemys or on other level */
nth_enemy++;
if (nth_enemy > num_wp)
{
if (!warned)
{
DebugPrintf (0, "\nWARNING: Less waypoints (%d) than enemys on level %d? !\n",
num_wp, CurLevel->levelnum );
DebugPrintf (0, "...cannot insert all droids on this level!\n");
}
warned = TRUE;
AllEnemys[i].status = OUT;
continue;
}
do { wp = MyRandom(num_wp-1);} while(used_wp[wp]);
used_wp[wp] = TRUE;
AllEnemys[i].pos.x = CurLevel->AllWaypoints[wp].x;
AllEnemys[i].pos.y = CurLevel->AllWaypoints[wp].y;
AllEnemys[i].lastwaypoint = wp;
AllEnemys[i].nextwaypoint = wp;
}/* for NumEnemys */
return;
} /* ShuffleEnemys() */
/* ----------------------------------------------------------------------
* This function checks if the connection between two points is free of
* droids.
*
* MAP TILES ARE NOT TAKEN INTO CONSIDERATION, ONLY DROIDS!!!
*
----------------------------------------------------------------------*/
void
SelectNextWaypointClassical( int EnemyNum )
{
int num_con;
finepoint Restweg;
Waypoint WpList; /* Pointer to waypoint-liste */
int nextwp;
Enemy ThisRobot=&AllEnemys[EnemyNum];
// We do some definitions to save us some more typing later...
WpList = CurLevel->AllWaypoints;
nextwp = ThisRobot->nextwaypoint;
// determine the remaining way until the target point is reached
Restweg.x = WpList[nextwp].x - ThisRobot->pos.x;
Restweg.y = WpList[nextwp].y - ThisRobot->pos.y;
//--------------------
// Now we can see if we are perhaps already there?
// then it might be time to set a new waypoint.
//
if ((Restweg.x == 0) && (Restweg.y == 0))
{
ThisRobot->lastwaypoint = ThisRobot->nextwaypoint;
ThisRobot->warten = MyRandom (ENEMYMAXWAIT);
/* suche moegliche Verbindung von hier */
DebugPrintf (2, "/* suche moegliche Verbindung von hier */\n");
num_con = WpList[nextwp].num_connections;
if ( num_con > 0)
ThisRobot->nextwaypoint = WpList[nextwp].connections[MyRandom(num_con-1)];
} /* if */
return;
} // void MoveThisRobotClassical ( int Enemynum );
/* ----------------------------------------------------------------------
* This function checks if the connection between two points is free of
* droids.
*
* MAP TILES ARE NOT TAKEN INTO CONSIDERATION, ONLY DROIDS!!!
*
----------------------------------------------------------------------*/
int
CheckIfWayIsFreeOfDroids ( float x1 , float y1 , float x2 , float y2 , int OurLevel , int ExceptedDroid )
{
float LargerDistance;
int Steps;
int i, j;
finepoint step;
finepoint CheckPosition;
DebugPrintf( 2, "\nint CheckIfWayIsFreeOfDroids (...) : Checking from %d-%d to %d-%d.", (int) x1, (int) y1 , (int) x2, (int) y2 );
fflush(stdout);
if ( abs((int)(x1-x2)) > abs ((int)(y1-y2)) ) LargerDistance=fabsf(x1-x2);
else LargerDistance=fabsf(y1-y2);
Steps=LargerDistance * 4 ; // We check four times on each map tile...
if ( Steps == 0 ) return TRUE;
// We determine the step size when walking from (x1,y1) to (x2,y2) in Steps number of steps
step.x = (x2 - x1) / Steps;
step.y = (y2 - y1) / Steps;
DebugPrintf( 2 , "\nint CheckIfWayIsFreeOfDroids (...) : step.x=%f step.y=%f." , step.x , step.y );
// We start from position (x1, y1)
CheckPosition.x = x1;
CheckPosition.y = y1;
for ( i = 0 ; i < Steps ; i++ )
{
for ( j = 0 ; j < NumEnemys ; j ++ )
{
if ( AllEnemys[j].levelnum != OurLevel ) continue;
if ( AllEnemys[j].status == OUT ) continue;
if ( AllEnemys[j].energy <= 0 ) continue;
if ( j == ExceptedDroid ) continue;
// so it seems that we need to test this one!!
if ( ( fabsf(AllEnemys[j].pos.x - CheckPosition.x ) < 2*Droid_Radius ) &&
( fabsf(AllEnemys[j].pos.y - CheckPosition.y ) < 2*Droid_Radius ) )
{
DebugPrintf( 2, "\nCheckIfWayIsFreeOfDroids (...) : Connection analysis revealed : TRAFFIC-BLOCKED !");
return FALSE;
}
}
if ( ( fabsf( Me.pos.x - CheckPosition.x ) < 2*Droid_Radius ) &&
( fabsf( Me.pos.y - CheckPosition.y ) < 2*Droid_Radius ) )
{
DebugPrintf( 2 , "\nCheckIfWayIsFreeOfDroids (...) : Connection analysis revealed : TRAFFIC-BLOCKED-INFLUENCER !");
return FALSE;
}
CheckPosition.x += step.x;
CheckPosition.y += step.y;
}
DebugPrintf( 2 , "\nCheckIfWayIsFreeOfDroids (...) : Connection analysis revealed : FREE!");
return TRUE;
}; // CheckIfWayIsFreeOfDroids ( float x1 , float y1 , float x2 , float y2 , int OurLevel , int ExceptedDroid )
/* ----------------------------------------------------------------------
* This function moves one robot thowards his next waypoint. If already
* there, the function does nothing more.
*
----------------------------------------------------------------------*/
void
MoveThisRobotThowardsHisWaypoint ( int EnemyNum )
{
finepoint Restweg;
Waypoint WpList; /* Pointer to waypoint-liste */
int nextwp;
finepoint nextwp_pos;
float maxspeed, steplen, dist;
Enemy ThisRobot=&AllEnemys[ EnemyNum ];
DebugPrintf( 2 , "\n void MoveThisRobotThowardsHisWaypoint ( int EnemyNum ) : real function call confirmed. ");
// We do some definitions to save us some more typing later...
WpList = CurLevel->AllWaypoints;
nextwp = ThisRobot->nextwaypoint;
maxspeed = Druidmap[ ThisRobot->type ].maxspeed;
nextwp_pos.x = WpList[nextwp].x;
nextwp_pos.y = WpList[nextwp].y;
// determine the remaining way until the target point is reached
Restweg.x = nextwp_pos.x - ThisRobot->pos.x;
Restweg.y = nextwp_pos.y - ThisRobot->pos.y;
steplen = Frame_Time() * maxspeed;
// --------------------
// As long a the distance from the current position of the enemy
// to its next wp is large, movement is rather simple:
dist = sqrt(Restweg.x*Restweg.x + Restweg.y*Restweg.y);
if ( dist > steplen )
{
ThisRobot->speed.x = (Restweg.x/dist) * maxspeed;
ThisRobot->speed.y = (Restweg.y/dist) * maxspeed;
ThisRobot->pos.x += ThisRobot->speed.x * Frame_Time ();
ThisRobot->pos.y += ThisRobot->speed.y * Frame_Time ();
}
else
{
// --------------------
// If this enemy is just one step ahead of his target, we just put him there now
ThisRobot->pos.x = nextwp_pos.x;
ThisRobot->pos.y = nextwp_pos.y;
ThisRobot->speed.x = 0;
ThisRobot->speed.y = 0;
}
return;
}; // void MoveThisRobotThowardsHisWaypoint ( int EnemyNum )
/* ----------------------------------------------------------------------
* This function moves one robot in an advanced way, that hasn't been
* present within the classical paradroid game.
*
----------------------------------------------------------------------*/
void
SelectNextWaypointAdvanced ( int EnemyNum )
{
int i,j;
finepoint Restweg;
Waypoint WpList; /* Pointer to waypoint-liste */
int nextwp;
finepoint nextwp_pos;
int trywp = 0;
Enemy ThisRobot=&AllEnemys[ EnemyNum ];
int FreeWays[ MAX_WP_CONNECTIONS ];
int SolutionFound;
int TestConnection;
DebugPrintf( 2 , "\n void MoveThisRobotAdvanced ( int EnemyNum ) : real function call confirmed. ");
// We do some definitions to save us some more typing later...
WpList = CurLevel->AllWaypoints;
nextwp = ThisRobot->nextwaypoint;
nextwp_pos.x = WpList[nextwp].x;
nextwp_pos.y = WpList[nextwp].y;
// determine the remaining way until the target point is reached
Restweg.x = nextwp_pos.x - ThisRobot->pos.x;
Restweg.y = nextwp_pos.y - ThisRobot->pos.y;
//--------------------
// Now we can see if we are perhaps already there?
// then it might be time to set a new waypoint.
//
if ((Restweg.x == 0) && (Restweg.y == 0))
{
ThisRobot->lastwaypoint = ThisRobot->nextwaypoint;
ThisRobot->warten = MyRandom (ENEMYMAXWAIT);
// This statement should make hostile droids with aggresssion
// wait, if they see the influencer and are at their waypoint now.
// Then they (in some other function) open fire and should do
// that, until the influencer vanishes out of sight, which should cause them
// to go into a hunting mode. (to be implemented later).
//
if ( Druidmap[ThisRobot->type].aggression && IsVisible ( &(ThisRobot->pos)))
{
// But now that the enemy is are almost ready to fire, it just
// might also say something. But of course it will not repeat this
// sentence every frame. Therefore we may only say something when
// this happens the first time i.e. when the robot was not waiting
// already.
//
if ( ThisRobot->warten == 0)
{
AddStandingAndAimingText( EnemyNum );
}
ThisRobot->warten=2;
return;
}
// search for possible connections from here...
DebugPrintf (2, "\nMoveRobotAdvanced: searching for possible connections...");
// search for the first connection, that doesn't exist any more, so
// that we know, which connections surely do exist
for ( j=0; j<MAX_WP_CONNECTIONS; j++ )
if ( WpList[nextwp].connections[j] == -1 )
break;
// Of course, only if such connections exist at all, we do the
// following change of target waypoint procedure
if ( j < MAX_WP_CONNECTIONS )
{
// At this point, we should check, if there is another waypoint
// and also if the way there is free of other droids
for ( i = 0; i < j ; i++ )
{
FreeWays[i] = CheckIfWayIsFreeOfDroids ( WpList[ThisRobot->lastwaypoint].x , WpList[ThisRobot->lastwaypoint].y , WpList[WpList[ThisRobot->lastwaypoint].connections[i]].x , WpList[WpList[ThisRobot->lastwaypoint].connections[i]].y , ThisRobot->levelnum , EnemyNum );
}
// Now see whether any way point at all is free in that sense
// otherwise we set this robot to waiting and return;
for ( i = 0 ; i < j ; i++ )
{
if ( FreeWays[i] ) break;
}
if ( i == j )
{
DebugPrintf( 2 , "\n Sorry, there seems no free way out. I'll wait then... , j was : %d ." , j);
ThisRobot->warten = 1;
return;
}
// Now that we know, there is some way out of this, we can test around
// and around randomly until we finally find some solution.
SolutionFound=FALSE;
while ( !SolutionFound )
{
TestConnection = MyRandom (MAX_WP_CONNECTIONS - 1);
if ( WpList[nextwp].connections[ TestConnection ] == (-1) ) continue;
if ( !FreeWays[TestConnection] ) continue;
trywp = WpList[nextwp].connections[ TestConnection ];
SolutionFound = TRUE;
}
}
else
{
DebugPrintf (1, "\nWeird waypoint %d has no connections!\n", nextwp);
// Terminate(ERR);
}
/* setze neuen Waypoint */
ThisRobot->nextwaypoint = trywp;
} /* if */
} // void MoveThisRobotAdvanced ( int EnemyNum )
void
MoveThisEnemy( int EnemyNum )
{
Enemy ThisRobot=&AllEnemys[ EnemyNum ];
// Now check if the robot is still alive
// if the robot just got killed, initiate the
// explosion and all that...
if ( ThisRobot->energy <= 0 && (ThisRobot->status != TERMINATED) )
{
ThisRobot->status = TERMINATED;
RealScore += Druidmap[ ThisRobot->type ].score;
DeathCount += ThisRobot->type * ThisRobot->type; // quadratic "importance", max=529
StartBlast ( ThisRobot->pos.x, ThisRobot->pos.y, DRUIDBLAST);
if (LevelEmpty ())
{
RealScore += DECKCOMPLETEBONUS;
CurLevel->empty = TRUE;
CurLevel->timer = WAIT_LEVELEMPTY;
set_time_factor ( SLOWMO_FACTOR ); // watch final explosion in slow-motion
}
return; // this one's down, so we can move on to the next
}
// robots that still have to wait also do not need to
// be processed for movement
if ( ThisRobot->warten > 0)
return;
// Now check for collisions of this enemy with his colleagues
CheckEnemyEnemyCollision ( EnemyNum );
//--------------------
// Now comes the real movement part
MoveThisRobotThowardsHisWaypoint( EnemyNum );
SelectNextWaypointClassical( EnemyNum );
} // void MoveThisEnemy ( int EnemyNum )
/*@Function============================================================
@Desc: This is the function, that move each of the enemys according to
their orders and their program
@Ret: none
* $Function----------------------------------------------------------*/
void
MoveEnemys (void)
{
int i;
PermanentHealRobots (); // enemy robots heal as time passes...
AnimateEnemys (); // move the "phase" of the rotation of enemys
for (i = 0; i < NumEnemys; i++)
{
if ( (AllEnemys[i].status == OUT) || (AllEnemys[i].status == TERMINATED) ||
(AllEnemys[i].levelnum != CurLevel->levelnum) )
continue;
MoveThisEnemy(i);
// If its a combat droid, then if might attack...
if (Druidmap[AllEnemys[i].type].aggression)
AttackInfluence (i);
} /* for NumEnemys */
} /* MoveEnemys() */
/*@Function============================================================
@Desc: AttackInfluence(): This function sometimes fires a bullet from
enemy number enemynum directly into the direction of the influencer,
but of course only if the odds are good i.e. requirements are met.
@Ret: void
* $Function----------------------------------------------------------*/
void
AttackInfluence (int enemynum)
{
int j;
Bullet CurBullet;
int guntype;
float xdist, ydist;
float dist2;
Enemy ThisRobot=&AllEnemys[ enemynum ];
//--------------------
// At first, we check for a lot of cases in which we do not
// need to move anything for this reason or for that
//
// ignore robots on other levels
if ( ThisRobot->levelnum != CurLevel->levelnum) return;
// ignore dead robots as well...
if ( ThisRobot->status == OUT ) return;
xdist = Me.pos.x - ThisRobot->pos.x;
ydist = Me.pos.y - ThisRobot->pos.y;
// Add some security against division by zero
if (xdist == 0) xdist = 0.01;
if (ydist == 0) ydist = 0.01;
// if odds are good, make a shot at your target
guntype = Druidmap[ThisRobot->type].gun;
dist2 = sqrt(xdist * xdist + ydist * ydist);
//--------------------
//
// From here on, it's classical Paradroid robot behaviour concerning fireing....
//
if ( dist2 >= FIREDIST2 ) return; // distance limitation only for MS mechs
if ( ThisRobot->firewait ) return;
if ( !IsVisible (&ThisRobot->pos))
return;
if ( ( MyRandom (AGGRESSIONMAX) >= Druidmap[ThisRobot->type].aggression ))
{
ThisRobot->firewait += 1.0*MyRandom(1000)* ROBOT_MAX_WAIT_BETWEEN_SHOTS / 1000.0;
return;
}
Fire_Bullet_Sound ( guntype );
// find a bullet entry, that isn't currently used...
for (j = 0; j < MAXBULLETS; j++)
{
if (AllBullets[j].type == OUT)
break;
}
if (j == MAXBULLETS)
{
DebugPrintf (2, "\nvoid AttackInfluencer(void): no free bullets... giving up\n");
return ;
}
CurBullet = &AllBullets[j];
// determine the direction of the shot, so that it will go into the direction of
// the target
if (fabsf (xdist) > fabsf (ydist))
{
CurBullet->speed.x = Bulletmap[guntype].speed;
CurBullet->speed.y = ydist * CurBullet->speed.x / xdist;
if (xdist < 0)
{
CurBullet->speed.x = -CurBullet->speed.x;
CurBullet->speed.y = -CurBullet->speed.y;
}
}
if (fabsf (xdist) < fabsf (ydist))
{
CurBullet->speed.y = Bulletmap[guntype].speed;
CurBullet->speed.x = xdist * CurBullet->speed.y / ydist;
if (ydist < 0)
{
CurBullet->speed.x = -CurBullet->speed.x;
CurBullet->speed.y = -CurBullet->speed.y;
}
}
CurBullet->angle = - ( 90 + 180 * atan2 ( CurBullet->speed.y, CurBullet->speed.x ) / M_PI );
/* Bullets im Zentrum des Schuetzen starten */
CurBullet->pos.x = ThisRobot->pos.x;
CurBullet->pos.y = ThisRobot->pos.y;
/* Bullets so abfeuern, dass sie nicht den Schuetzen treffen */
CurBullet->pos.x +=
(CurBullet->speed.x) / fabsf (Bulletmap[guntype].speed) * 0.5;
CurBullet->pos.y +=
(CurBullet->speed.y) / fabsf (Bulletmap[guntype].speed) * 0.5;
/* Dem Bullettype entsprechend lange warten vor naechstem Schuss */
ThisRobot->firewait = Bulletmap[Druidmap[ThisRobot->type].gun].recharging_time ;
/* Bullettype gemaess dem ueblichen guntype fuer den robottyp setzen */
CurBullet->type = guntype;
CurBullet->time_in_frames = 0;
CurBullet->time_in_seconds = 0;
//} /* if */
} /* AttackInfluence */
/*@Function============================================================
@Desc: CheckEnemyEnemyCollision()
@Ret: TRUE: if enemy Nr. Enemynum collided with another enemy
@Int:
* $Function----------------------------------------------------------*/
int
CheckEnemyEnemyCollision (int enemynum)
{
int i;
int curlev = CurLevel->levelnum;
float check_x, check_y;
int swap;
float xdist, ydist;
float dist;
float speed_x, speed_y;
check_x = AllEnemys[enemynum].pos.x;
check_y = AllEnemys[enemynum].pos.y;
for (i = 0; i < NumEnemys ; i++)
{
// check only collisions of LIVING enemys on this level
if ( (AllEnemys[i].status == OUT) || (AllEnemys[i].status == TERMINATED)
|| AllEnemys[i].levelnum != curlev)
continue;
// dont check yourself...
if (i == enemynum)
continue;
/* get distance between enemy i and enemynum */
xdist = check_x - AllEnemys[i].pos.x;
ydist = check_y - AllEnemys[i].pos.y;
dist = sqrt(xdist * xdist + ydist * ydist);
// Is there a Collision?
if ( dist <= 2*Droid_Radius)
{
// am I waiting already? If so, keep waiting...
if (AllEnemys[enemynum].warten)
{
/* weiter warten */
AllEnemys[enemynum].warten = MyRandom(2*WAIT_COLLISION);
continue;
}
/* Sonst: Feind stoppen und selbst umdrehen */
AllEnemys[i].warten = MyRandom(2*WAIT_COLLISION);
/* gestoppten gegner ein wenig zurueckstossen */
if (xdist)
AllEnemys[i].pos.x -= xdist / fabsf (xdist) * Frame_Time();
if (ydist)
AllEnemys[i].pos.y -= ydist / fabsf (ydist) * Frame_Time();
swap = AllEnemys[enemynum].nextwaypoint;
AllEnemys[enemynum].nextwaypoint =
AllEnemys[enemynum].lastwaypoint;
AllEnemys[enemynum].lastwaypoint = swap;
/* Etwas aus Gegner herausbewegen !! */
speed_x = AllEnemys[enemynum].speed.x;
speed_y = AllEnemys[enemynum].speed.y;
if (speed_x)
AllEnemys[enemynum].pos.x -=
Frame_Time() * COL_SPEED * (speed_x) / fabsf (speed_x);
if (speed_y)
AllEnemys[enemynum].pos.y -=
Frame_Time() * COL_SPEED * (speed_y) / fabsf (speed_y);
return TRUE;
} /* if dist zu klein */
} /* for */
return FALSE;
} // int CheckEnemyEnemyCollision
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
AnimateEnemys (void)
{
int i;
for (i = 0; i < NumEnemys; i++)
{
// if (AllEnemys[i].type == DRUID598)
// {
// AllEnemys[i].feindrehcode,
// Druidmap[AllEnemys[i].type].maxenergy,
// AllEnemys[i].energy,
// AllEnemys[i].phase);
//}
/* ignore enemys that are dead or on other levels or dummys */
if (AllEnemys[i].levelnum != CurLevel->levelnum)
continue;
if (AllEnemys[i].status == OUT)
continue;
// AllEnemys[i].feindrehcode+=AllEnemys[i].energy;
AllEnemys[i].phase +=
(AllEnemys[i].energy / Druidmap[AllEnemys[i].type].maxenergy) *
Frame_Time () * ENEMYPHASES * 2.5;
if (AllEnemys[i].phase >= ENEMYPHASES)
{
AllEnemys[i].phase = 0;
}
}
} // void AnimateEnemys(void)
/*@Function============================================================
@Desc: ClassOfDruid(druidtype): liefert die Classe des Druidtypes type
@Ret: int class: 0-9
@Int:
* $Function----------------------------------------------------------*/
int
ClassOfDruid (int druidtype)
{
char classnumber[2];
classnumber[0] = Druidmap[druidtype].druidname[0]; /* first digit is class */
classnumber[1] = '\0';
return (atoi (classnumber));
} /* ClassOfDruid */
#undef _enemy_c