-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilot.c
3956 lines (3343 loc) · 106 KB
/
pilot.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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file pilot.c
*
* @brief Handles the pilot stuff.
*/
#include "pilot.h"
#include "naev.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include "nxml.h"
#include "log.h"
#include "weapon.h"
#include "ndata.h"
#include "spfx.h"
#include "rng.h"
#include "hook.h"
#include "map.h"
#include "explosion.h"
#include "escort.h"
#include "music.h"
#include "player.h"
#include "gui.h"
#include "board.h"
#include "debris.h"
#include "ntime.h"
#include "ai_extra.h"
#include "faction.h"
#include "font.h"
#include "land.h"
#include "land_outfits.h"
#include "land_shipyard.h"
#include "array.h"
#define PILOT_CHUNK_MIN 128 /**< Maximum chunks to increment pilot_stack by */
#define PILOT_CHUNK_MAX 2048 /**< Minimum chunks to increment pilot_stack by */
#define CHUNK_SIZE 32 /**< Size to allocate memory by. */
/* ID Generators. */
static unsigned int pilot_id = PLAYER_ID; /**< Stack of pilot ids to assure uniqueness */
static unsigned int mission_cargo_id = 0; /**< ID generator for special mission cargo.
Not guaranteed to be absolutely unique,
only unique for each pilot. */
/* stack of pilot_nstack */
Pilot** pilot_stack = NULL; /**< Not static, used in player.c, weapon.c, pause.c, space.c and ai.c */
int pilot_nstack = 0; /**< same */
static int pilot_mstack = 0; /**< Memory allocated for pilot_stack. */
/* misc */
static double sensor_curRange = 0.; /**< Current base sensor range, used to calculate
what is in range and what isn't. */
static double pilot_commTimeout = 15.; /**< Time for text above pilot to time out. */
static double pilot_commFade = 5.; /**< Time for text above pilot to fade out. */
static PilotHook *pilot_globalHooks = NULL; /**< Global hooks that affect all pilots. */
/*
* prototyes
*/
/* update. */
static int pilot_shootWeapon( Pilot* p, PilotOutfitSlot* w );
static void pilot_hyperspace( Pilot* pilot, double dt );
static void pilot_refuel( Pilot *p, double dt );
/* cargo. */
static int pilot_rmCargoRaw( Pilot* pilot, Commodity* cargo, int quantity, int cleanup );
static void pilot_calcCargo( Pilot* pilot );
static int pilot_addCargoRaw( Pilot* pilot, Commodity* cargo,
int quantity, unsigned int id );
/* clean up. */
void pilot_free( Pilot* p ); /* externed in player.c */
static void pilot_dead( Pilot* p, unsigned int killer );
/* misc */
static void pilot_setCommMsg( Pilot *p, const char *s );
static int pilot_getStackPos( const unsigned int id );
static void pilot_updateMass( Pilot *pilot );
extern int landtarget; /* From player.c */
/**
* @brief Gets the pilot's position in the stack.
*
* @param id ID of the pilot to get.
* @return Position of pilot in stack or -1 if not found.
*/
static int pilot_getStackPos( const unsigned int id )
{
/* binary search */
int l,m,h;
l = 0;
h = pilot_nstack-1;
while (l <= h) {
m = (l+h) >> 1; /* for impossible overflow returning neg value */
if (pilot_stack[m]->id > id) h = m-1;
else if (pilot_stack[m]->id < id) l = m+1;
else return m;
}
/* Not found. */
return -1;
}
/**
* @brief Gets the next pilot based on id.
*
* @param id ID of current pilot.
* @param mode Method to use when cycling. 0 is normal, 1 is hostiles.
* @return ID of next pilot or PLAYER_ID if no next pilot.
*/
unsigned int pilot_getNextID( const unsigned int id, int mode )
{
int m, p;
/* Player must exist. */
if (player.p == NULL)
return PLAYER_ID;
/* Get the pilot. */
m = pilot_getStackPos(id);
/* Unselect. */
if ((m == (pilot_nstack-1)) || (m == -1))
return PLAYER_ID;
/* Get first one in range. */
p = m+1;
if (mode == 0) {
while (p < pilot_nstack) {
if (((pilot_stack[p]->faction != FACTION_PLAYER) ||
pilot_isDisabled(pilot_stack[p])) &&
!pilot_isFlag( pilot_stack[p], PILOT_INVISIBLE ) &&
pilot_inRangePilot( player.p, pilot_stack[p] ))
return pilot_stack[p]->id;
p++;
}
}
/* Get first hostile in range. */
if (mode == 1) {
while (p < pilot_nstack) {
if ((pilot_stack[p]->faction != FACTION_PLAYER) &&
!pilot_isFlag( pilot_stack[p], PILOT_INVISIBLE ) &&
pilot_inRangePilot( player.p, pilot_stack[p] ) &&
(pilot_isFlag(pilot_stack[p],PILOT_HOSTILE) ||
areEnemies( FACTION_PLAYER, pilot_stack[p]->faction)))
return pilot_stack[p]->id;
p++;
}
}
/* None found. */
return PLAYER_ID;
}
/**
* @brief Gets the previous pilot based on ID.
*
* @param id ID of the current pilot.
* @return ID of previous pilot or PLAYER_ID if no previous pilot.
*/
unsigned int pilot_getPrevID( const unsigned int id, int mode )
{
int m, p;
/* Player must exist. */
if (player.p == NULL)
return PLAYER_ID;
/* Get the pilot. */
m = pilot_getStackPos(id);
/* Check to see what position to try. */
if (m == -1)
return PLAYER_ID;
else if (m == 0)
p = pilot_nstack-1;
else
p = m-1;
/* Get first one in range. */
if (mode == 0) {
while (p >= 0) {
if (((pilot_stack[p]->faction != FACTION_PLAYER) ||
(pilot_isDisabled(pilot_stack[p]))) &&
!pilot_isFlag( pilot_stack[p], PILOT_INVISIBLE ) &&
pilot_inRangePilot( player.p, pilot_stack[p] ))
return pilot_stack[p]->id;
p--;
}
}
/* Get first hostile in range. */
else if (mode == 1) {
while (p >= 0) {
if ((pilot_stack[p]->faction != FACTION_PLAYER) &&
!pilot_isFlag( pilot_stack[p], PILOT_INVISIBLE ) &&
pilot_inRangePilot( player.p, pilot_stack[p] ) &&
(pilot_isFlag(pilot_stack[p],PILOT_HOSTILE) ||
areEnemies( FACTION_PLAYER, pilot_stack[p]->faction)))
return pilot_stack[p]->id;
p--;
}
}
/* None found. */
return PLAYER_ID;
}
/**
* @brief Gets the nearest enemy to the pilot.
*
* @param p Pilot to get his nearest enemy.
* @return ID of his nearest enemy.
*/
unsigned int pilot_getNearestEnemy( const Pilot* p )
{
unsigned int tp;
int i;
double d, td;
tp = 0;
d = 0.;
for (i=0; i<pilot_nstack; i++) {
/* Must not be dead. */
if (pilot_isFlag( pilot_stack[i], PILOT_DELETE ) ||
pilot_isFlag( pilot_stack[i], PILOT_DEAD))
continue;
/* Must not be bribed. */
if ((pilot_stack[i]->faction == FACTION_PLAYER) && pilot_isFlag(p,PILOT_BRIBED))
continue;
/* Must not be invisible. */
if (pilot_isFlag( pilot_stack[i], PILOT_INVISIBLE ))
continue;
/* Should either be hostile by faction or by player. */
if (!(areEnemies( p->faction, pilot_stack[i]->faction) ||
((pilot_stack[i]->id == PLAYER_ID) &&
pilot_isFlag(p,PILOT_HOSTILE))))
continue;
/* Shouldn't be disabled. */
if (pilot_isDisabled(pilot_stack[i]))
continue;
/* Must be in range. */
if (!pilot_inRangePilot( p, pilot_stack[i] ))
continue;
/* Check distance. */
td = vect_dist2(&pilot_stack[i]->solid->pos, &p->solid->pos);
if (!tp || (td < d)) {
d = td;
tp = pilot_stack[i]->id;
}
}
return tp;
}
/**
* @brief Get the nearest pilot to a pilot.
*
* @param p Pilot to get his nearest pilot.
* @return The nearest pilot.
*/
unsigned int pilot_getNearestPilot( const Pilot* p )
{
unsigned int tp;
int i;
double d, td;
tp=PLAYER_ID;
d=0;
for (i=0; i<pilot_nstack; i++) {
if (pilot_stack[i] == p)
continue;
/* Player doesn't select escorts. */
if ((p->faction == FACTION_PLAYER) &&
(pilot_stack[i]->faction == FACTION_PLAYER))
continue;
/* Shouldn't be disabled. */
if (pilot_isDisabled(pilot_stack[i]))
continue;
/* Shouldn't be invisible. */
if (pilot_isFlag( pilot_stack[i], PILOT_INVISIBLE ))
continue;
/* Must be in range. */
if (!pilot_inRangePilot( p, pilot_stack[i] ))
continue;
td = vect_dist2(&pilot_stack[i]->solid->pos, &player.p->solid->pos);
if (((tp==PLAYER_ID) || (td < d))) {
d = td;
tp = pilot_stack[i]->id;
}
}
return tp;
}
/**
* @brief Pulls a pilot out of the pilot_stack based on ID.
*
* It's a binary search ( O(logn) ) therefore it's pretty fast and can be
* abused all the time. Maximum iterations is 32 on a platfom with 32 bit
* unsigned ints.
*
* @param id ID of the pilot to get.
* @return The actual pilot who has matching ID or NULL if not found.
*/
Pilot* pilot_get( const unsigned int id )
{
int m;
if (id==PLAYER_ID)
return player.p; /* special case player.p */
m = pilot_getStackPos(id);
if ((m==-1) || (pilot_isFlag(pilot_stack[m], PILOT_DELETE)))
return NULL;
else
return pilot_stack[m];
}
/**
* @brief Sets the pilot's thrust.
*/
void pilot_setThrust( Pilot *p, double thrust )
{
p->solid->force_x = p->thrust * thrust;
}
/**
* @brief Sets the pilot's turn.
*/
void pilot_setTurn( Pilot *p, double turn )
{
p->solid->dir_vel = p->turn * turn;
}
/**
* @brief Checks to see if pilot is hostile to the player.
*
* @param p Player to see if is hostile.
* @return 1 if pilot is hostile to the player.
*/
int pilot_isHostile( const Pilot *p )
{
if (pilot_isFlag(p, PILOT_FRIENDLY))
return 0;
if (pilot_isFlag(p, PILOT_HOSTILE) ||
areEnemies(FACTION_PLAYER,p->faction))
return 1;
return 0;
}
/**
* @brief Checks to see if pilot is neutral to the player.
*
* @param p Player to see if is neutral.
* @return 1 if pilot is neutral to the player.
*/
int pilot_isNeutral( const Pilot *p )
{
if (!pilot_isHostile(p) && !pilot_isFriendly(p))
return 1;
return 0;
}
/**
* @brief Checks to see if pilot is friendly to the player.
*
* @param p Player to see if is friendly.
* @return 1 if pilot is friendly to the player.
*/
int pilot_isFriendly( const Pilot *p )
{
if (pilot_isFlag(p, PILOT_HOSTILE))
return 0;
if (pilot_isFlag(p, PILOT_FRIENDLY) ||
areAllies(FACTION_PLAYER,p->faction))
return 1;
return 0;
}
/**
* @brief Tries to turn the pilot to face dir.
*
* Sets the direction velocity property of the pilot's solid, does not
* directly manipulate the direction.
*
* @param p Pilot to turn.
* @param dir Direction to attempt to face.
* @return The distance left to turn to match dir.
*/
double pilot_face( Pilot* p, const double dir )
{
double diff, turn;
diff = angle_diff( p->solid->dir, dir );
turn = CLAMP( -1., 1., -10.*diff );
pilot_setTurn( p, -turn );
return diff;
}
/**
* @brief Marks pilot as hostile to player.
*
* @param p Pilot to mark as hostile to player.
*/
void pilot_setHostile( Pilot* p )
{
if (!pilot_isFlag(p, PILOT_HOSTILE)) {
/* Time to play combat music. */
if (player.enemies == 0)
music_choose("combat");
player.enemies++;
pilot_setFlag(p, PILOT_HOSTILE);
}
}
/**
* @brief Check to see if a position is in range of the pilot.
*
* @param p Pilot to check to see if position is in his sensor range.
* @param x X position to check.
* @param y Y position to check.
* @return 1 if the position is in range, 0 if it isn't.
*/
int pilot_inRange( const Pilot *p, double x, double y )
{
double d;
/* Get distance. */
d = pow2(x-p->solid->pos.x) + pow2(y-p->solid->pos.y);
if (d < sensor_curRange)
return 1;
return 0;
}
/**
* @brief Check to see if a pilot is in sensor range of another.
*
* @param p Pilot who is trying to check to see if other is in sensor range.
* @param target Target of p to check to see if is in sensor range.
* @return 1 if they are in range, 0 if they aren't and -1 if they are detected fuzzily.
*/
int pilot_inRangePilot( const Pilot *p, const Pilot *target )
{
double d, sense;
/* Get distance. */
d = vect_dist2( &p->solid->pos, &target->solid->pos );
sense = sensor_curRange * p->ew_detect;
if (d * target->ew_evasion < sense)
return 1;
else if (d * target->ew_hide < sense)
return -1;
return 0;
}
/**
* @brief Check to see if a planet is in sensor range of the pilot.
*
* @param p Pilot who is trying to check to see if the planet is in sensor range.
* @param target Planet to see if is in sensor range.
* @return 1 if they are in range, 0 if they aren't.
*/
int pilot_inRangePlanet( const Pilot *p, int target )
{
(void)p;
(void)target;
/* Always consider planets in range. */
return 1;
#if 0
double d;
Planet *pnt;
if (cur_system->interference == 0.)
return 1;
/* Get the planet. */
pnt = cur_system->planets[target];
/* Get distance. */
d = vect_dist2( &p->solid->pos, &pnt->pos );
if (d < sensor_curRange)
return 1;
return 0;
#endif
}
/**
* @brief Gets the faction colour char, works like faction_getColourChar but for a pilot.
*
* @sa faction_getColourChar
*/
char pilot_getFactionColourChar( const Pilot *p )
{
if (pilot_isDisabled(p))
return 'I';
else if (pilot_isFlag(p, PILOT_BRIBED))
return 'N';
else if (pilot_isFlag(p, PILOT_HOSTILE))
return 'H';
return faction_getColourChar(p->faction);
}
/**
* @brief Sets the overhead communication message of the pilot.
*/
static void pilot_setCommMsg( Pilot *p, const char *s )
{
/* Free previous message. */
if (p->comm_msg != NULL)
free(p->comm_msg);
/* Duplicate the message. */
p->comm_msg = strdup(s);
p->comm_msgWidth = gl_printWidthRaw( NULL, s );
p->comm_msgTimer = pilot_commTimeout;
}
/**
* @brief Have pilot send a message to another.
*
* @param p Pilot sending message.
* @param target Target of the message.
* @param msg The message.
* @param ignore_int Whether or not should ignore interference.
*/
void pilot_message( Pilot *p, unsigned int target, const char *msg, int ignore_int )
{
Pilot *t;
char c;
/* Makes no sense with no player.p atm. */
if (player.p==NULL)
return;
/* Get the target. */
t = pilot_get(target);
if (t == NULL)
return;
/* Must be in range. */
if (!ignore_int && !pilot_inRangePilot( player.p, p ))
return;
/* Only really affects player.p atm. */
if (target == PLAYER_ID) {
c = pilot_getFactionColourChar( p );
player_message( "\e%cComm %s>\e0 \"%s\"", c, p->name, msg );
/* Set comm message. */
pilot_setCommMsg( p, msg );
}
}
/**
* @brief Has the pilot broadcast a message.
*
* @param p Pilot to broadcast the message.
* @param msg Message to broadcast.
* @param ignore_int Whether or not should ignore interference.
*/
void pilot_broadcast( Pilot *p, const char *msg, int ignore_int )
{
char c;
/* Only display if player.p exists and is in range. */
if (player.p==NULL)
return;
/* Check if should ignore interference. */
if (!ignore_int && !pilot_inRangePilot( player.p, p ))
return;
c = pilot_getFactionColourChar( p );
player_message( "\e%cBroadcast %s>\e0 \"%s\"", c, p->name, msg );
/* Set comm message. */
pilot_setCommMsg( p, msg );
}
/**
* @brief Has the pilot broadcast a distress signal.
*
* Can do a faction hit on the player.
*
* @param p Pilot to send distress signal.
* @param msg Message in distress signal.
* @param ignore_int Whether or not should ignore interference.
*/
void pilot_distress( Pilot *p, const char *msg, int ignore_int )
{
int i, r;
Pilot *t;
/* Broadcast the message. */
if (msg[0] != '\0')
pilot_broadcast( p, msg, ignore_int );
/* Get the target to see if it's the player. */
t = pilot_get(p->target);
/* Now proceed to see if player.p should incur faction loss because
* of the broadcast signal. */
/* Consider not in range at first. */
r = 0;
/* Check if planet is in range. */
for (i=0; i<cur_system->nplanets; i++) {
if (planet_hasService(cur_system->planets[i], PLANET_SERVICE_INHABITED) &&
(!ignore_int && pilot_inRangePlanet(p, i)) &&
!areEnemies(p->faction, cur_system->planets[i]->faction)) {
r = 1;
break;
}
}
/* Now we must check to see if a pilot is in range. */
for (i=0; i<pilot_nstack; i++) {
if ((pilot_stack[i]->id != p->id) &&
(!ignore_int && pilot_inRangePilot(p, pilot_stack[i]))) {
/* Send AI the distress signal. */
if (pilot_stack[i]->ai != NULL)
ai_getDistress( pilot_stack[i], p );
/* Check if should take faction hit. */
if (!areEnemies(p->faction, pilot_stack[i]->faction))
r = 1;
}
}
/* Player only gets one faction hit per pilot. */
if (!pilot_isFlag(p, PILOT_DISTRESSED)) {
/* Modify faction, about 1 for a llama, 4.2 for a hawking */
if ((t != NULL) && (t->faction == FACTION_PLAYER) && r)
faction_modPlayer( p->faction, -(pow(p->ship->mass, 0.2) - 1.) );
/* Set flag to avoid a second faction hit. */
pilot_setFlag(p, PILOT_DISTRESSED);
}
}
/**
* @brief Unmarks a pilot as hostile to player.
*
* @param p Pilot to mark as hostile to player.
*/
void pilot_rmHostile( Pilot* p )
{
if (pilot_isFlag(p, PILOT_HOSTILE)) {
if (!pilot_isDisabled(p))
player.enemies--;
pilot_rmFlag(p, PILOT_HOSTILE);
/* Change music back to ambient if no more enemies. */
if (player.enemies <= 0) {
music_choose("ambient");
player.enemies = 0;
}
}
}
/**
* @brief Marks pilot as friendly to player.
*
* @param p Pilot to mark as friendly to player.
*/
void pilot_setFriendly( Pilot* p )
{
pilot_rmHostile(p);
pilot_setFlag(p, PILOT_FRIENDLY);
}
/**
* @brief Unmarks a pilot as friendly to player.
*
* @param p Pilot to mark as friendly to player.
*/
void pilot_rmFriendly( Pilot* p )
{
pilot_rmFlag(p, PILOT_FRIENDLY);
}
/**
* @brief Gets the amount of jumps the pilot has left.
*
* @param p Pilot to get the jumps left.
* @return Number of jumps the pilot has left.
*/
int pilot_getJumps( const Pilot* p )
{
return (int)(p->fuel) / HYPERSPACE_FUEL;
}
/**
* @brief Gets the quantity of a pilot outfit.
*
* @param p Pilot to which the outfit belongs.
* @param w Outfit to check quantity of.
* @return The amount of the outfit the pilot has.
*/
int pilot_oquantity( Pilot* p, PilotOutfitSlot* w )
{
return (outfit_isAmmo(w->outfit) && p->secondary) ?
p->secondary->quantity : w->quantity ;
}
/**
* @brief Makes the pilot shoot.
*
* @param p The pilot which is shooting.
* @param type Indicate what shoot group to use.
* 0 = all
* 1 = turrets
* 2 = forward
* @return The number of shots fired.
*/
int pilot_shoot( Pilot* p, int group )
{
int i, ret;
Outfit* o;
if (!p->outfits) return 0; /* no outfits */
ret = 0;
for (i=0; i<p->outfit_nweapon; i++) { /* cycles through outfits to find primary weapons */
o = p->outfit_weapon[i].outfit;
if (o==NULL)
continue;
if (!outfit_isProp(o,OUTFIT_PROP_WEAP_SECONDARY) &&
(outfit_isBolt(o) || outfit_isBeam(o) || outfit_isLauncher(o))) {
/* Choose what to shoot dependent on type. */
if ((group == 0) ||
((group == 1) && outfit_isTurret(o)) ||
((group == 2) && !outfit_isTurret(o))) {
ret += pilot_shootWeapon( p, &p->outfit_weapon[i] );
}
}
}
return ret;
}
/**
* @brief Makes the pilot shoot it's currently selected secondary weapon.
*
* @param p The pilot which is to shoot.
* @return The number of shots fired.
*/
int pilot_shootSecondary( Pilot* p )
{
int i, ret;
/* No secondary weapon. */
if (p->secondary == NULL)
return 0;
/* Fire all secondary weapon of same type. */
ret = 0;
for (i=0; i<p->outfit_nweapon; i++) {
if (p->outfit_weapon[i].outfit == p->secondary->outfit)
ret += pilot_shootWeapon( p, &p->outfit_weapon[i] );
}
return ret;
}
/**
* @brief Have pilot stop shooting his weapon.
*
* Only really deals with beam weapons.
*
* @param p Pilot that was shooting.
* @param secondary If weapon is secondary.
*/
void pilot_shootStop( Pilot* p, const int secondary )
{
int i;
Outfit* o;
/* Non beam secondaries don't matter. */
if (secondary && ((p->secondary == NULL) ||
(p->secondary->outfit == NULL) ||
!outfit_isBeam(p->secondary->outfit)))
return;
/* Iterate over them all. */
for (i=0; i<p->outfit_nweapon; i++) {
o = p->outfit_weapon[i].outfit;
/* Must have outfit. */
if (o==NULL)
continue;
/* Must be beam. */
if (!outfit_isBeam(o))
continue;
if (secondary) {
if (o == p->secondary->outfit) {
if (p->outfit_weapon[i].u.beamid > 0) {
beam_end( p->id, p->outfit_weapon[i].u.beamid );
p->outfit_weapon[i].u.beamid = 0;
}
}
}
else {
if (!outfit_isProp(o, OUTFIT_PROP_WEAP_SECONDARY)) {
if (p->outfit_weapon[i].u.beamid > 0) {
beam_end( p->id, p->outfit_weapon[i].u.beamid );
p->outfit_weapon[i].u.beamid = 0;
}
}
}
}
}
/**
* @brief Gets the mount position of a pilot.
*
* Position is relative to the pilot.
*
* @param p Pilot to get mount position of.
* @param id ID of the mount.
* @param[out] v Position of the mount.
* @return 0 on success.
*/
int pilot_getMount( const Pilot *p, const PilotOutfitSlot *w, Vector2d *v )
{
double a, x, y;
double cm, sm;
const ShipMount *m;
/* Calculate the sprite angle. */
a = (double)(p->tsy * p->ship->gfx_space->sx + p->tsx);
a *= p->ship->mangle;
/* 2d rotation matrix
* [ x' ] [ cos sin ] [ x ]
* [ y' ] = [ -sin cos ] * [ y ]
*
* dir is inverted so that rotation is counter-clockwise.
*/
m = &w->mount;
cm = cos(-a);
sm = sin(-a);
x = m->x * cm + m->y * sm;
y = m->x *-sm + m->y * cm;
/* Correction for ortho perspective. */
y *= M_SQRT1_2;
/* Don't forget to add height. */
y += m->h;
/* Get the mount and add the player.p offset. */
vect_cset( v, x, y );
return 0;
}
/**
* @brief Actually handles the shooting, how often the player.p can shoot and such.
*
* @param p Pilot that is shooting.
* @param w Pilot's outfit to shoot.
* @return 0 if nothing was shot and 1 if something was shot.
*/
static int pilot_shootWeapon( Pilot* p, PilotOutfitSlot* w )
{
Vector2d vp, vv;
int i;
PilotOutfitSlot *slot;
int minp;
double q, mint;
int is_launcher;
double rate_mod, energy_mod;
/* check to see if weapon is ready */
if (w->timer > 0.)
return 0;
/* See if is launcher. */
is_launcher = outfit_isLauncher(w->outfit);
/* Calculate rate modifier. */
switch (w->outfit->type) {
case OUTFIT_TYPE_BOLT:
rate_mod = 2. - p->stats.firerate_forward; /* invert. */
energy_mod = p->stats.energy_forward;
break;
case OUTFIT_TYPE_TURRET_BOLT:
rate_mod = 2. - p->stats.firerate_turret; /* invert. */
energy_mod = p->stats.energy_turret;
break;
default:
rate_mod = 1.;
energy_mod = 1.;
break;
}
/* Count the outfits and current one - only affects non-beam. */
if (!outfit_isBeam(w->outfit)) {
/* Calculate last time weapon was fired. */
q = 0.;
minp = -1;
for (i=0; i<p->outfit_nweapon; i++) {
slot = &p->outfit_weapon[i];
/* No outfit. */
if (slot->outfit == NULL)
continue;
/* Not what we are looking for. */
if (outfit_delay(slot->outfit) != outfit_delay(w->outfit))
continue;
/* Launcher only counts with ammo. */
if (is_launcher && ((w->u.ammo.outfit == NULL) || (w->u.ammo.quantity <= 0)))
continue;
/* Save some stuff. */
if ((minp < 0) || (slot->timer > mint)) {
minp = i;
mint = slot->timer;
}
q++;
}