-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbullet.c
538 lines (441 loc) · 15 KB
/
bullet.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
/*
*
* 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 Bullet AND Blast - related functions.
*
*
*----------------------------------------------------------------------*/
#define _bullet_c
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"
/* Distances for hitting a druid */
//NORMALISATION #define MORE 4
#define MORE (4/64.0)
//NORMALISATION #define DRUIDHITDIST2 (Druid_Radius_X+MORE)*(DRUIDRADIUSY+MORE)
// #define DRUIDHITDIST2 0
// #define DRUIDHITDIST2 (Druid_Radius_X+MORE)*(DRUIDRADIUSY+MORE)
#define DRUIDHITDIST2 (0.3+MORE)*(Droid_Radius+MORE)
// #define DRUIDHITDIST2 0
// local prototypes
int GetDirection (point robo, point bul);
/*@Function============================================================
@Desc: this function moves all the bullets according to their speeds.
NEW: this function also takes into accoung the current framerate.
@Ret: keiner
@Int: keiner
* $Function----------------------------------------------------------*/
void
MoveBullets (void)
{
/* lokale Variablen der Funktion: */
int i;
Bullet CurBullet;
/* Bewegung der Bullets */
for (i = 0; i < MAXBULLETS; i++)
{
CurBullet = &AllBullets[i];
if ( CurBullet->type == OUT )
continue;
CurBullet->prev_pos.x = CurBullet->pos.x;
CurBullet->prev_pos.y = CurBullet->pos.y;
CurBullet->pos.x += CurBullet->speed.x * Frame_Time ();
CurBullet->pos.y += CurBullet->speed.y * Frame_Time ();
CurBullet->time_in_frames++;
CurBullet->time_in_seconds += Frame_Time();
} /* for */
return;
} // void MoveBullets(void)
/*@Function============================================================
@Desc: delete bullet of given number, set it type=OUT, put it at x/y=-1/-1
and create a Bullet-blast if with_blast==TRUE
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
DeleteBullet (int Bulletnumber)
{
Bullet CurBullet = &AllBullets[Bulletnumber];
int i;
if (CurBullet->type == OUT) // ignore dead bullets
return;
//--------------------
// At first we generate the blast at the collision spot of the bullet,
// cause later, after the bullet is deleted, it will be hard to know
// the correct location ;)
// RP (18/11/02): nay, we do that manually before DeleteBullet() now,
// --> not all bullets should create Blasts (i.e. not if droid was hit)
// StartBlast (CurBullet->pos.x, CurBullet->pos.y, BULLETBLAST);
//--------------------
// maybe, the bullet had several SDL_Surfaces attached to it. Then we need to
// free the SDL_Surfaces again as well...
//
// if ( ( CurBullet->type != FLASH) && ( CurBullet->type != OUT ) )
if ( CurBullet->Surfaces_were_generated )
{
DebugPrintf( 1 , "\nvoid DeleteBullet(...): freeing this bullets attached surfaces...");
for ( i=0 ; i < Bulletmap[ CurBullet->type ].phases ; i++ )
{
SDL_FreeSurface( CurBullet->SurfacePointer[i] );
CurBullet->SurfacePointer[i] = NULL;
}
CurBullet->Surfaces_were_generated = FALSE;
}
//--------------------
// Now that the memory has been freed again, we can finally delete this bullet entry.
// Hope, that this does not give us a SEGFAULT, but it should not do so.
//
CurBullet->type = OUT;
CurBullet->time_in_seconds = 0;
CurBullet->time_in_frames = 0;
CurBullet->mine = FALSE;
CurBullet->phase = 0;
CurBullet->pos.x = -1;
CurBullet->pos.y = -1;
CurBullet->angle = 0;
return;
}; // void DeleteBullet(int Bulletnumber)
/*@Function============================================================
@Desc: StartBlast(): erzeugt einen Blast type an x/y
@Ret: void
@Int:
* $Function----------------------------------------------------------*/
void
StartBlast (float x, float y, int type)
{
int i;
Blast NewBlast;
/* Position des naechsten freien Blasts herausfinden */
for (i = 0; i < MAXBLASTS; i++)
if (AllBlasts[i].type == OUT)
break;
/* keinen gefunden: nimm den ersten */
if (i >= MAXBLASTS)
i = 0;
/* Get Pointer to it: more comfortable */
NewBlast = &(AllBlasts[i]);
if (type == REJECTBLAST)
{
NewBlast->mine = TRUE;
type = DRUIDBLAST; // not really a different type, just avoid damaging influencer
}
else
NewBlast->mine = FALSE;
/* Einen Blast an x/y erzeugen */
NewBlast->PX = x;
NewBlast->PY = y;
NewBlast->type = type;
NewBlast->phase = 0;
NewBlast->MessageWasDone = 0;
if (type == DRUIDBLAST)
{
DruidBlastSound ();
}
} /* StartBlast */
/*@Function============================================================
@Desc: Diese Funktion zaehlt die Phasen aller Explosionen weiter
@Ret: keiner
@Int: keiner
* $Function----------------------------------------------------------*/
void
ExplodeBlasts (void)
{
int i;
Blast CurBlast = AllBlasts;
for (i = 0; i < MAXBLASTS; i++, CurBlast++)
if (CurBlast->type != OUT)
{
/* Druidblasts sind gefaehrlich !! */
if (CurBlast->type == DRUIDBLAST)
CheckBlastCollisions (i);
// CurBlast->phase++;
CurBlast->phase += Frame_Time () * Blastmap[ CurBlast->type ].phases / Blastmap[ CurBlast->type ].total_animation_time;
if (((int) floorf (CurBlast->phase)) >=
Blastmap[CurBlast->type].phases)
DeleteBlast (i);
} /* if */
} /* ExplodeBlasts */
/*@Function============================================================
@Desc: Einen eizelnen Blast ausloeschen
@Ret: keiner
@Int:
* $Function----------------------------------------------------------*/
void
DeleteBlast (int Blastnummer)
{
AllBlasts[Blastnummer].type = OUT;
}
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
int
GetDirection (point robo, point bul)
{
if ((robo.x < bul.x) && (robo.y > bul.y))
return 0;
if ((robo.x < bul.x) && (robo.y < bul.y))
return 1;
if ((robo.x > bul.x) && (robo.y < bul.y))
return 2;
if ((robo.x > bul.x) && (robo.y > bul.y))
return 3;
if ((robo.x == bul.x) && (robo.y == bul.y))
{
DebugPrintf (2, " Center hit directy!");
getchar ();
}
return 0;
}
/*@Function============================================================
@Desc: CheckBulletCollisions(int num)
checkt Collisions des Bullets Num mit Hintergrund && Druids
@Ret: void
@Int:
* $Function----------------------------------------------------------*/
void
CheckBulletCollisions (int num)
{
int levelnum = CurLevel->levelnum;
float xdist, ydist;
Bullet CurBullet = &AllBullets[num];
static int FBTZaehler = 0;
finepoint step;
int num_check_steps, stepnum;
int i;
switch (CurBullet->type)
{
// --------------------
// Never do any collision checking if the bullet is OUT already...
case OUT:
return;
break;
// --------------------
// Next we handle the case that the bullet is of type FLASH
case FLASH:
// if the flash is over, just delete it and return
if (CurBullet->time_in_seconds >= FLASH_DURATION)
{
CurBullet->time_in_frames = 0;
CurBullet->time_in_seconds = 0;
CurBullet->type = OUT;
CurBullet->mine = FALSE;
return;
}
// if the flash is not yet over, do some checking for who gets
// hurt by it.
// Two different methode for doing this are available:
// The first but less elegant Method is just to check for
// flash immunity, for distance and visiblity.
// The second and more elegant method is to recursively fill
// out the room where the flash-maker is in and to hurt all
// robots in there except of course for those immune.
if ( CurBullet->time_in_frames != 1 )
break; // we only do the damage once and thats at frame nr. 1 of the flash
for (i = 0; i < NumEnemys; i++)
{
// !! dont't forget: Only droids on our level are harmed!! (bugfix)
if (AllEnemys[i].levelnum != levelnum)
continue;
if ( IsVisible (&AllEnemys[i].pos) & (!Druidmap[AllEnemys[i].type].flashimmune) )
{
AllEnemys[i].energy -= Bulletmap[FLASH].damage;
// Since the enemy just got hit, it might as well say so :)
EnemyHitByBulletText( i );
}
}
// droids with flash are always flash-immune!
// -> we don't get hurt by our own flashes!
if (!InvincibleMode && !Druidmap[Me.type].flashimmune)
Me.energy -= Bulletmap[FLASH].damage ;
return;
break;
// --------------------
// If its a "normal" Bullet, several checks have to be
// done, one for collisions with background,
// one for collision with influencer
// some for collisions with enemys
// and some for collisions with other bullets
// and some for collisions with blast
//
default:
// first check for collision with background
step.x = CurBullet->pos.x - CurBullet->prev_pos.x;
step.y = CurBullet->pos.y - CurBullet->prev_pos.y;
num_check_steps = (int)( sqrt(step.x*step.x + step.y*step.y)/ COLLISION_STEPSIZE);
if (num_check_steps == 0) num_check_steps = 1;
step.x /= 1.0* num_check_steps;
step.y /= 1.0* num_check_steps;
CurBullet->pos.x = CurBullet->prev_pos.x;
CurBullet->pos.y = CurBullet->prev_pos.y;
for (stepnum=0; stepnum < num_check_steps; stepnum++)
{
CurBullet->pos.x += step.x;
CurBullet->pos.y += step.y;
if (IsPassable (CurBullet->pos.x, CurBullet->pos.y, CENTER) != CENTER)
{
StartBlast (CurBullet->pos.x, CurBullet->pos.y, BULLETBLAST);
DeleteBullet (num);
return;
}
// check for collision with influencer
if (!CurBullet->mine)
{
xdist = Me.pos.x - CurBullet->pos.x;
ydist = Me.pos.y - CurBullet->pos.y;
if ((xdist * xdist + ydist * ydist) < DRUIDHITDIST2) // FIXME: don't use DRUIDHITDIST2!!
{
GotHitSound ();
if (!InvincibleMode)
Me.energy -= Bulletmap[CurBullet->type].damage; /* Energie verlieren */
DeleteBullet( num );
return; /* Bullet ist hin */
}
} // if Bullet!=mine
// check for collision with enemys
for (i = 0; i < NumEnemys; i++)
{
if (AllEnemys[i].status == OUT || AllEnemys[i].status == TERMINATED ||
AllEnemys[i].levelnum != levelnum)
continue;
xdist = CurBullet->pos.x - AllEnemys[i].pos.x;
ydist = CurBullet->pos.y - AllEnemys[i].pos.y;
if ((xdist * xdist + ydist * ydist) < DRUIDHITDIST2) // FIXME
{
// The enemy who was hit, loses some energy, depending on the bullet
AllEnemys[i].energy -= Bulletmap[CurBullet->type].damage;
DeleteBullet( num );
GotHitSound ();
if (!CurBullet->mine)
{
FBTZaehler++;
}
CurBullet->type = OUT;
CurBullet->mine = FALSE;
// break; /* Schleife beenden */
return;
} /* if getroffen */
} /* for AllEnemys */
// check for collisions with other bullets
for (i = 0; i < MAXBULLETS; i++)
{
if (i == num) continue; // never check for collision with youself.. ;)
if (AllBullets[i].type == OUT) continue; // never check for collisions with dead bullets..
if (AllBullets[i].type == FLASH) continue; // never check for collisions with flashes bullets..
xdist = AllBullets[i].pos.x-CurBullet->pos.x;
ydist = AllBullets[i].pos.y-CurBullet->pos.y;
if ( xdist*xdist + ydist*ydist > BULLET_COLL_DIST2 ) continue;
// it seems like we have a collision of two bullets!
// both will be deleted and replaced by blasts..
DebugPrintf (1, "\nBullet-Bullet-Collision detected...");
StartBlast(CurBullet->pos.x, CurBullet->pos.y, DRUIDBLAST);
// ??why start two blasts?
// StartBlast(AllBullets[num].pos.x, AllBullets[num].pos.y, DRUIDBLAST);
DeleteBullet (num);
DeleteBullet (i);
}
} // for numsteps < num_check_steps
break;
} // switch ( Bullet-Type )
} /* CheckBulletCollisions */
/*@Function============================================================
@Desc: CheckBlastCollsions(int num)
checkt Collisionen des Blasts num mit Bullets und Druids
UND reagiert darauf
@Ret: void
@Int:
* $Function----------------------------------------------------------*/
void
CheckBlastCollisions (int num)
{
int i;
int levelnum = CurLevel->levelnum;
Blast CurBlast = &(AllBlasts[num]);
Bullet CurBullet;
float dist;
vect vdist;
/* check Blast-Bullet Collisions and kill hit Bullets */
for (i = 0; i < MAXBULLETS; i++)
{
CurBullet = &AllBullets[i];
if (CurBullet->type == OUT)
continue;
vdist.x = CurBullet->pos.x - CurBlast->PX;
vdist.y = CurBullet->pos.y - CurBlast->PY;
dist = sqrt(vdist.x*vdist.x + vdist.y*vdist.y);
if (dist < Blast_Radius)
{
StartBlast (CurBullet->pos.x, CurBullet->pos.y, BULLETBLAST);
DeleteBullet( i );
}
} /* for */
/* Check Blast-Enemy Collisions and smash energy of hit enemy */
for (i = 0; i < NumEnemys; i++)
{
if ((AllEnemys[i].status == OUT)
|| (AllEnemys[i].levelnum != levelnum))
continue;
vdist.x = AllEnemys[i].pos.x - CurBlast->PX;
vdist.y = AllEnemys[i].pos.y - CurBlast->PY;
dist = sqrt(vdist.x*vdist.x + vdist.y*vdist.y);
if (dist < Blast_Radius+Droid_Radius)
{
/* drag energy of enemy */
AllEnemys[i].energy -= Blast_Damage_Per_Second * Frame_Time ();
}
if (AllEnemys[i].energy < 0)
AllEnemys[i].energy = 0;
} /* for */
/* Check influence-Blast collisions */
vdist.x = Me.pos.x - CurBlast->PX;
vdist.y = Me.pos.y - CurBlast->PY;
dist = sqrt(vdist.x*vdist.x + vdist.y*vdist.y);
if ( (Me.status != OUT) && (!CurBlast->mine) && (dist < Blast_Radius+Droid_Radius) )
{
if (!InvincibleMode)
{
Me.energy -= Blast_Damage_Per_Second * Frame_Time ();
// So the influencer got some damage from the hot blast
// Now most likely, he then will also say so :)
if ( !CurBlast->MessageWasDone )
{
AddInfluBurntText();
CurBlast->MessageWasDone=TRUE;
}
}
// In order to avoid a new sound EVERY frame we check for how long the previous blast
// lies back in time. LastBlastHit is a float, that counts SECONDS real-time !!
if (LastGotIntoBlastSound > 1.2)
{
GotIntoBlastSound ();
LastGotIntoBlastSound = 0;
}
}
} /* CheckBlastCollisions */
#undef _bullet_c