-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2321 lines (2183 loc) · 81.3 KB
/
main.cpp
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
/***************************************************************************
main.cpp - description
-------------------
begin : Tue Sep 19 17:16:48 PDT 2000
copyright : (C) 2000 by Michael Bridak
email : michael.bridak@verizon.net
$Id: main.cpp,v 1.28 2003/07/18 03:50:00 mbridak Exp $
***************************************************************************/
/***************************************************************************
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; either version 2 of the License.
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "main.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <unistd.h>
#include "dfont.h"
#include "draw.h"
#include "menu.h"
#include "mission.h"
#include "files.h"
#include "sound.h"
#include "winfunctions.h"
//#######GRAPHIC PRIMITIVES#############
void SetupScreen(bool full_screen)
{
// Initialize the screen and some default colors
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) {
std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << std::endl;
exit(1);
}
atexit(SDL_Quit);
SDL_InitSubSystem(SDL_INIT_TIMER);
if (full_screen) {
// Create main screen surface
screen = SDL_SetVideoMode(1024, 768, 32, SDL_FULLSCREEN);
} else {
screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE);
}
if (screen == NULL) {
std::cerr << "Couldn't set video mode..." << std::endl
<< "OpenSSN requires 1024x768 @ 16 bpp" << std::endl
<< SDL_GetError() << std::endl;
exit(1);
}
SDL_WM_SetCaption("OpenSSN http://openssn.sf.net", "OpenSSN");
// define some standard colors
black = SDL_MapRGB(screen->format, 0, 0, 0);
white = SDL_MapRGB(screen->format, 255, 255, 255);
red = SDL_MapRGB(screen->format, 255, 99, 20);
dark_red = SDL_MapRGB(screen->format, 140, 80, 10);
green = SDL_MapRGB(screen->format, 0, 255, 0);
dark_green = SDL_MapRGB(screen->format, 0, 120, 0);
yellow = SDL_MapRGB(screen->format, 216, 255, 0);
brown = SDL_MapRGB(screen->format, 120, 140, 0);
grey = SDL_MapRGB(screen->format, 180, 180, 180);
dark_grey = SDL_MapRGB(screen->format, 100, 100, 100);
mapcolor = SDL_MapRGB(screen->format, 10, 10, 100);
}
// Let all stations initialize their graphics
void InitGraphics()
{
SonarStation.InitGraphics(screen);
NavigationStation.InitGraphics(screen);
WeaponsStation.InitGraphics(screen);
TmaStation.InitGraphics(screen);
ControlStation.InitGraphics(screen);
EsmStation.InitGraphics(screen);
RadarStation.InitGraphics(screen);
}
void LoadWidgets()
{
titlescreen.load("images/tittle.png");
successscreen.load("images/sub_surfacing.png");
failurescreen.load("images/sub_rising.png");
menuscreen.load("images/sub_menu.png");
sonarbuttonup.load("images/sonarup.png");
sonarbuttondown.load("images/sonardown.png");
navbuttonup.load("images/mapup.png");
navbuttondown.load("images/mapdown.png");
tmabuttonup.load("images/tmaup.png");
tmabuttondown.load("images/tmadown.png");
shipcontrolbuttonup.load("images/shipcontrolup.png");
shipcontrolbuttondown.load("images/shipcontroldown.png");
esmbuttonup.load("images/esmup.png");
esmbuttondown.load("images/esmdown.png");
radarbuttonup.load("images/radarup.png");
radarbuttondown.load("images/radardown.png");
quitbuttonup.load("images/quitbuttonup.png");
quitbuttondown.load("images/quitbuttondown.png");
plusbuttonup.load("images/plusbuttonup.png");
plusbuttondown.load("images/plusbuttondown.png");
minusbuttonup.load("images/minusbuttonup.png");
minusbuttondown.load("images/minusbuttondown.png");
SonarStation.LoadWidgets();
NavigationStation.LoadWidgets();
WeaponsStation.LoadWidgets();
TmaStation.LoadWidgets();
ControlStation.LoadWidgets();
EsmStation.LoadWidgets();
RadarStation.LoadWidgets();
}
void UnLoadWidgets()
{
// free the surfaces
titlescreen.unload();
successscreen.unload();
failurescreen.unload();
menuscreen.unload();
sonarbuttonup.unload();
sonarbuttondown.unload();
navbuttonup.unload();
navbuttondown.unload();
tmabuttonup.unload();
tmabuttondown.unload();
shipcontrolbuttonup.unload();
shipcontrolbuttondown.unload();
esmbuttonup.unload();
esmbuttondown.unload();
radarbuttonup.unload();
radarbuttondown.unload();
quitbuttonup.unload();
quitbuttondown.unload();
plusbuttonup.unload();
plusbuttondown.unload();
minusbuttonup.unload();
minusbuttondown.unload();
SonarStation.UnLoadWidgets();
NavigationStation.UnLoadWidgets();
WeaponsStation.UnLoadWidgets();
TmaStation.UnLoadWidgets();
ControlStation.UnLoadWidgets();
EsmStation.UnLoadWidgets();
RadarStation.UnLoadWidgets();
}
void DisplayScreen(int item)
{
switch (item) {
case SCREEN_TITLE:
titlescreen.draw(screen, 0, 0);
break;
case SCREEN_SUCCESS:
successscreen.draw(screen, 0, 0);
break;
case SCREEN_FAILURE:
failurescreen.draw(screen, 0, 0);
break;
case SCREEN_MENU:
menuscreen.draw(screen, 0, 0);
break;
default:
std::cerr << "Unknown screen item" << std::endl;
break;
}
}
void DisplayStationWidgets()
{
station == STN_SONAR ? sonarbuttondown.draw(screen, 180, 710) : sonarbuttonup.draw(screen, 180, 710);
station == STN_NAV ? navbuttondown.draw(screen, 280, 710) : navbuttonup.draw(screen, 280, 710);
station == STN_WEAPONS ? tmabuttondown.draw(screen, 380, 710) : tmabuttonup.draw(screen, 380, 710);
station == STN_SHIPCONTROL ? shipcontrolbuttondown.draw(screen, 480, 710) : shipcontrolbuttonup.draw(screen, 480, 710);
station == STN_ESM ? esmbuttondown.draw(screen, 580, 710) : esmbuttonup.draw(screen, 580, 710);
station == STN_RADAR ? radarbuttondown.draw(screen, 680, 710) : radarbuttonup.draw(screen, 680, 710);
quit ? quitbuttondown.draw(screen, 780, 710) : quitbuttonup.draw(screen, 780, 710);
}
// This function adds a torpedo to the linked-list of torpedoes
// The returned value is the new linked-list pointer, in case
// the new_torpedo is the first in the list.
Submarine *Add_Ship(Submarine *all_torp, Submarine *new_torp)
{
Submarine *my_torp;
// we do not have a torpedo to add
if (! new_torp)
return all_torp;
// empty list
if (!all_torp)
return new_torp;
// add one to an existing list
my_torp = all_torp;
while (my_torp->next)
my_torp = my_torp->next;
// add to end of list
my_torp->next = new_torp;
return all_torp;
}
// This function removes a torpedo from the list of all
// torpedoes. It returns the first item in the torpedo linked-list.
Submarine *Remove_Ship(Submarine *all_torpedoes, Submarine *old_torpedo)
{
Submarine *my_torp, *previous;
if (!all_torpedoes)
return NULL;
if (!old_torpedo)
return all_torpedoes;
my_torp = all_torpedoes;
previous = NULL;
while (my_torp) {
// found match and it is the first in the list
if ((my_torp == old_torpedo) && (!previous)) {
previous = my_torp->next;
delete my_torp;
return previous;
}
// found a match and it isn't the first in the list
else if (my_torp == old_torpedo) {
previous->next = my_torp->next;
delete my_torp;
return all_torpedoes;
}
// this one does not match, go to next node
else {
previous = my_torp;
my_torp = my_torp->next;
}
}
// item wasn't found, just return the complete list
return all_torpedoes;
}
Helicopter *Add_Helicopter(Helicopter *all_helicopters, Helicopter *new_helicopter)
{
Helicopter *current;
if (!new_helicopter)
return all_helicopters;
if (!all_helicopters)
return new_helicopter;
current = all_helicopters;
while (current->next)
current = current->next;
current->next = new_helicopter;
return all_helicopters;
}
Helicopter *Remove_Helicopter(Helicopter *all_helicopters, Helicopter *old_helicopter)
{
Helicopter *current, *previous = NULL;
if (!all_helicopters)
return NULL;
if (!old_helicopter)
return all_helicopters;
current = all_helicopters;
while (current) {
// first in list
if ((current == old_helicopter) && (!previous)) {
previous = current->next;
delete current;
return previous;
}
// not first in list
else if (current == old_helicopter) {
previous->next = current->next;
delete current;
return all_helicopters;
}
// this is not our node
else {
previous = current;
current = current->next;
}
}
return all_helicopters;
}
//######################################
void ShipHandeling()
{
Submarine *my_torp, *temp_torp;
int status;
Submarine *ship;
Helicopter *helicopter;
// see if we can use radar, esm, etc
if (player->Depth > PERISCOPE_DEPTH) {
EsmStation.LowerMast();
RadarStation.LowerMast();
player->using_radar = FALSE;
}
// if array is out, limit speed
if (TB16.GetLength() > 0) {
int max_speed = Subs->MaxSpeed / 2;
if (Subs->Speed > max_speed)
Subs->Speed = max_speed;
}
ship = Subs;
while (ship) {
ship->UpdateLatLon(); // Move all the ships
// see what the AI wants to do
if (ship != Subs) {
/*
if (ship->ShipType == TYPE_SUB)
torpedoes = ship->Sub_AI(Subs, torpedoes);
else if (ship->ShipType == TYPE_SHIP)
torpedoes = ship->Ship_AI(Subs, torpedoes);
*/
torpedoes = ship->Sub_AI(Subs, torpedoes, helicopters);
}
ship->Handeling(); // Steer, Change Depth etc...
// we were pinging, but it fades quickly
if (ship->pinging > 0)
ship->pinging--;
ship = ship->next;
}
helicopter = helicopters;
while (helicopter) {
helicopter->UpdateLatLon();
torpedoes = helicopter->Helicopter_AI(Subs, torpedoes);
#ifdef DEBUG_HELICOPTER
printf("Course: %d\nSpeed: %d\nHeight: %d\n",
(int) helicopter->Heading, (int) helicopter->Speed,
(int) helicopter->Depth);
printf("DX: %d DY: %d\nX: %d Y: %d\n",
helicopter->destination_x, helicopter->destination_y,
(int) helicopter->Lat_TotalYards,
(int) helicopter->Lon_TotalYards);
#endif
helicopter->Handle();
helicopter = helicopter->next;
}
for (int x = 0; x < MAX_SUBS; x++) {
Contacts[x].UpdateContact();
}
// move some torps
my_torp = torpedoes;
while (my_torp) {
// check to see if this torpedo is chasing the player
if ((my_torp->target == player) &&
(my_torp->fuel_remaining == (TORPEDO_FUEL - 30))) {
Message.post_message("Torpedo coming our way!");
}
my_torp->UpdateLatLon();
my_torp->Torpedo_AI(Subs); // see where we should be going
my_torp->Handeling(); // change heading and depth
status = my_torp->Check_Status(); // see if we ran into something
// or we are out of fuel
if (status == OUT_OF_FUEL) {
if (my_torp->ShipType == TYPE_TORPEDO)
Message.post_message("A torpedo ran out of fuel.");
else
Message.post_message("Noisemaker stopped running.");
temp_torp = my_torp->next;
if (current_target == my_torp)
current_target = NULL;
Subs->Cancel_Target(my_torp);
torpedoes = Remove_Ship(torpedoes, my_torp);
my_torp = temp_torp;
} else if (status == HIT_TARGET) {
int target_status = DAMAGE_OK;
// damage target
// we should always have a target, but just in case...
if (my_torp->target) {
if (my_torp->target == player)
Message.post_message("We have been hit!");
else
Message.post_message("A torpedo hit its target!");
// radio warning to others
if ((my_torp->target->ShipType == TYPE_SHIP) &&
(my_torp->target->mood == MOOD_CONVOY)) {
my_torp->target->Radio_Signal(Subs, RADIO_UNDER_ATTACK);
}
target_status = my_torp->target->Take_Damage();
if (target_status == DAMAGE_SINK) {
if (my_torp->target == player)
player = NULL;
if (my_torp->target->mission_status == MISSION_ALIVE)
my_mission_status = MISSION_FAILED;
Remove_Inactive_Ship(my_torp->target);
}
}
if (current_target == my_torp)
current_target = NULL;
Subs->Cancel_Target(my_torp);
temp_torp = my_torp->next;
torpedoes = Remove_Ship(torpedoes, my_torp);
my_torp = temp_torp;
// Message.post_message("A torpedo hit its target!");
if (target_status == DAMAGE_SINK)
Message.post_message("Target is sinking!");
} else
my_torp = my_torp->next;
} // end of all torpedoes
// see if we can still detect the current target
if (current_target) {
status = player->Can_Detect(current_target);
if (status < CONTACT_WEAK)
current_target = NULL;
}
}
// This function may get a little messy, we need to do a few things
// to remove a ship completely from the scenario.
// 1. Any torpedoes shooting at this target have to have their
// target variable set to NULL
// 2. Any ships/subs who were tracking this ship need to stop
void Remove_Inactive_Ship(Submarine *victim)
{
Submarine *torpedo;
Submarine *ship;
if (!victim) // couldn't find victim, this should never happen, quit
return;
// 1. Cancel torpedoes targetting this ship
torpedo = torpedoes;
while (torpedo) {
if (torpedo->target == victim)
torpedo->target = NULL;
torpedo = torpedo->next;
}
// 2. Ships who were tracking this ship need to stop
ship = Subs;
while (ship) {
ship->Cancel_Target(victim);
ship = ship->next;
}
Subs->last_target = NULL;
Subs = Remove_Ship(Subs, victim);
if (victim == current_target)
current_target = NULL;
}
double RelativeBearing(Submarine *observer, Submarine *target)
{
// Returns the relative bearing of target to observer. This bearing is
// measured clockwise from on ships heading to target bearing.
double relative_bearing;
int observer_heading, bearing_to_target;
bearing_to_target = (int) observer->BearingToTarget(target);
observer_heading = (int) observer->Heading;
if (observer_heading > bearing_to_target) bearing_to_target += 360;
relative_bearing = bearing_to_target - observer_heading;
return relative_bearing;
}
int ReciprocalBearing(int bearing)
{
// returns the 180 degree opposite of the bearing given to it..
int recipbearing;
if (bearing >= 180) {
recipbearing = bearing - 180;
} else {
recipbearing = bearing + 180;
}
return recipbearing;
}
double AngleOnBow(Submarine *observer, Submarine *target)
{
// Returns the Angle on the Bow (AOB) of target relative to observer.
// Note: -180 < Angle on Bow < 180
// Positive AOB = Port AOB, Negative AOB = Starboard AOB
// Examples:
// target AOB port 30 -> +30
// target AOB starboard 30 -> -30
// target pointing at observer -> 0
// target pointing away from observer -> -180
double aob = 0;
aob = target->Heading - observer->Heading
- RelativeBearing(observer, target) - 180;
if (aob > 180) aob = 360 - aob;
if (aob < -180) aob = 360 + aob;
return aob;
}
int minimize360(int compass_bearing)
{
// If course is bigger than 360 degrees, then subtract 360 degrees from it
// and keep doing so until it is less than 360 degrees.
while (compass_bearing > 360) {
compass_bearing -= 360;
}
return compass_bearing;
}
int maximize360(int compass_bearing)
{
// If course is negative then, then add 360 degrees to it
// and keep doing so until it is positive.
while (compass_bearing < 0) {
compass_bearing += 360;
}
return compass_bearing;
}
double CalculateRange(Submarine *observer, Submarine *target)
{
// Calculates the distance in yards from the observer, to the target.
double latdif = 0, londif = 0; // sqrt needs doubles
LatLonDifference(observer, target, &latdif, &londif);
return sqrt((latdif * latdif) + (londif * londif));
}
void LatLonDifference(Submarine *x, Submarine *y, double *platdif, double *plondif) // CHANGE TO REFERENCES
{
// Returns in yards the difference between object x and object y
if (x->Lat_TotalYards > y->Lat_TotalYards) {
*platdif = x->Lat_TotalYards - y->Lat_TotalYards;
} else {
*platdif = y->Lat_TotalYards - x->Lat_TotalYards;
}
if (x->Lon_TotalYards > y->Lon_TotalYards) {
*plondif = x->Lon_TotalYards - y->Lon_TotalYards;
} else {
*plondif = y->Lon_TotalYards - x->Lon_TotalYards;
}
}
void CreateShips(int mission_number, MAP *map)
{
char *ship_file, *mission_name;
char filename[128];
char line[256], *status;
// int i;
FILE *my_file, *mission_file;
Submarine *new_ship;
Helicopter *new_helicopter;
int ship_type, ship_class;
#ifndef WIN32
snprintf(filename, 128, "data/ships%d.dat", mission_number);
#else
sprintf(filename, "data/ships%d.dat", mission_number);
#endif
ship_file = Find_Data_File(filename);
my_file = fopen(ship_file, "r");
free(ship_file);
// idiot check
if (!my_file) {
printf("Create ships: file missing\n");
exit(1);
}
#ifndef WIN32
snprintf(filename, 128, "data/mission%d.dat", mission_number);
#else
sprintf(filename, "data/mission%d.dat", mission_number);
#endif
mission_name = Find_Data_File(filename);
mission_file = fopen(mission_name, "r");
free(mission_name);
// Read in the data from the ship file
// Format of ship file: 1 row of data per ship
// Active flag, Ship Type, Friend flag, Speed, DesiredSpeed, Depth,
// Desired depth, Heading, Desired
// Heading, Rudder, Lat_TotalYards, Lon_TotalYards, PSCS
// Notes: Ship Type: Sub=0, Surface=1, Aircraft=2
// Friend: Foe=0, Friend=1, Unknown=2, Neutral=3
// i = 0;
status = fgets(line, 256, my_file);
while (status) {
sscanf(line, "%d %d", &ship_type, &ship_class);
if (ship_class == CLASS_HELICOPTER) {
#ifdef DEBUG
printf("Creating new helicopter.\n");
#endif
new_helicopter = new Helicopter();
if (new_helicopter) {
new_helicopter->map = map;
sscanf(line, "%d %d %d %d %d %d %f %f %d",
&(new_helicopter->ShipType),
&(new_helicopter->ShipClass),
&(new_helicopter->Friend),
&(new_helicopter->DesiredSpeed),
&(new_helicopter->DesiredDepth),
&(new_helicopter->DesiredHeading),
&(new_helicopter->Lat_TotalYards),
&(new_helicopter->Lon_TotalYards),
&(new_helicopter->has_sonar));
// check mood
#ifndef WIN32
if (strcasestr(line, "convoy"))
new_helicopter->mood = MOOD_CONVOY;
else if (strcasestr(line, "passive"))
new_helicopter->mood = MOOD_PASSIVE;
else if (strcasestr(line, "attack"))
new_helicopter->mood = MOOD_ATTACK;
#else
if (my_strcasestr(line, "convoy"))
new_helicopter->mood = MOOD_CONVOY;
else if (my_strcasestr(line, "passive"))
new_helicopter->mood = MOOD_PASSIVE;
else if (my_strcasestr(line, "attack"))
new_helicopter->mood = MOOD_ATTACK;
#endif
new_helicopter->Speed = new_helicopter->DesiredSpeed;
new_helicopter->Heading = new_helicopter->DesiredHeading;
new_helicopter->Depth = new_helicopter->DesiredDepth;
// load mission
new_helicopter->Load_Mission(mission_file);
// load class
sprintf(filename, "ships/class%d.shp", new_helicopter->ShipClass);
ship_file = Find_Data_File(filename);
new_helicopter->Load_Class(ship_file);
free(ship_file);
// add helicopter to list
helicopters = Add_Helicopter(helicopters, new_helicopter);
} // end of created a helicopter
} // end of helicopter
else { // all other classes
#ifdef DEBUG
printf("Creating new ship\n");
#endif
new_ship = new Submarine();
if (new_ship) {
#ifdef DEBUG
printf("Added new ship to list of ships.\n");
#endif
new_ship->Init();
new_ship->map = map;
#ifdef DEBUG
printf("Loaded: %s", line);
#endif
sscanf(line, "%d %d %d %d %d %d %f %f",
&(new_ship->ShipType),
&(new_ship->ShipClass), &(new_ship->Friend),
&(new_ship->DesiredSpeed), &(new_ship->DesiredDepth),
&(new_ship->DesiredHeading), &(new_ship->Lat_TotalYards),
&(new_ship->Lon_TotalYards)); // , &(new_ship->PSCS) );
// check mood
#ifndef WIN32
if (strcasestr(line, "convoy"))
new_ship->mood = MOOD_CONVOY;
else if (strcasestr(line, "passive"))
new_ship->mood = MOOD_PASSIVE;
else if (strcasestr(line, "attack"))
new_ship->mood = MOOD_ATTACK;
#else
if (my_strcasestr(line, "convoy"))
new_ship->mood = MOOD_CONVOY;
else if (my_strcasestr(line, "passive"))
new_ship->mood = MOOD_PASSIVE;
else if (my_strcasestr(line, "attack"))
new_ship->mood = MOOD_ATTACK;
#endif
#ifdef DEBUG
printf("%d %d %d %d %d %d %f %f %d\n",
(new_ship->ShipType),
(new_ship->ShipClass), (new_ship->Friend),
(new_ship->DesiredSpeed), (new_ship->DesiredDepth),
(new_ship->DesiredHeading), (new_ship->Lat_TotalYards),
(new_ship->Lon_TotalYards), new_ship->mood);
// , (new_ship->PSCS) );
#endif
new_ship->Speed = new_ship->DesiredSpeed;
new_ship->Depth = new_ship->DesiredDepth;
new_ship->Heading = new_ship->DesiredHeading;
#ifdef DEBUG
printf("Set S: %f H: %f D: %f\n", new_ship->Speed,
new_ship->Heading, new_ship->Depth);
#endif
new_ship->Load_Mission(mission_file);
#ifdef DEBUG
printf("Ship has mission flag: %d\n", new_ship->mission_status);
#endif
sprintf(filename, "ships/class%d.shp", new_ship->ShipClass);
ship_file = Find_Data_File(filename);
new_ship->Load_Class(ship_file);
Subs = Add_Ship(Subs, new_ship);
free(ship_file);
// i+=1;
} // end of successfully created new ship
} // end of other classes
status = fgets(line, 256, my_file);
}
// inClientFile.close();
if (my_file)
fclose(my_file);
if (mission_file)
fclose(mission_file);
// ships = i - 1;
// ships = i;
// rdm 5/15/01 testing to be sure correct number of ships being read
// std::cout << " Number of ships = " << i-1 << std::endl;
if (Subs) {
SonarStation.setSubs(Subs);
NavigationStation.setShips(Subs, torpedoes, helicopters);
NavigationStation.setDepthMap(map);
TmaStation.setSubs(Subs);
WeaponsStation.setSubs(Subs);
ControlStation.setSubs(Subs);
EsmStation.setSubs(Subs);
RadarStation.setSubs(Subs);
player = Subs;
}
}
void UpdateSensors()
{
// Added code to place our boats position into a FIFO so we
// can calculate where the sonar array is at any time.
// the cable on the TB16 is 2600ft. wich is 866yds. The FIFO is
// 100 elements holding 2 double precision floats. So 866/100
// Is 8.66, Which is the resolution of our placement of the array.
#ifdef DEBUG
printf("Updating sensors and stuff\n");
#endif
static float distance_traveled;
distance_traveled += Subs->Speed * 0.555;
if (distance_traveled >= 8.66) { // if we've gone 8.66yds record our loc in the FIFO
TB16.RecordPos(Subs->Lat_TotalYards, Subs->Lon_TotalYards);
distance_traveled = distance_traveled - 8.66;
}
TB16.OperateWinch(); // extend/retract the array as needed.
SoundEnvironment(); // lets give a listen...
SonarStation.Sonar(SonarStation.GetNorthCenterState());
SonarStation.TowedSonar(SonarStation.GetNorthCenterState());
}
// calls the other detection functions to see if there is
// any way we can pick up the target
float Any_Detection(double Range, Submarine *observer, Submarine *target)
{
float status;
#ifdef DEBUG
printf("Attempting all forms of detection.\n");
#endif
status = Radar_Detection(Range, observer, target);
if (status) {
// printf("I can see on radar target %d\n", target);
return 2.0;
}
status = Esm_Detection(Range, observer, target);
if (status) {
// printf("I can pick up on esm target %d\n", target);
return 1.0;
}
// this one takes the most math, so we do it last
// status = Sonar_Detection(Range, observer, target);
status = Sonar_Detection_New(Range, observer, target);
// if (status)
// printf("I can hear target %d\n", target);
return status;
}
// Can I see you on radar?
float Radar_Detection(double Range, Submarine *observer, Submarine *target)
{
int depth, range;
bool result;
depth = (int) observer->Depth;
range = (int) observer->DistanceToTarget(target);
result = RadarStation.isTargetVisible(target, range, depth,
SHIP_HEIGHT, DEFAULT_SEA_STATE);
if (result)
return 1.0;
else
return 0.0;
}
// Can I detect your radar?
float Esm_Detection(double Range, Submarine *observer, Submarine *target)
{
int depth, range;
bool result;
depth = (int) observer->Depth;
range = (int) observer->DistanceToTarget(target);
result = EsmStation.isTargetVisible(target, range, depth,
SHIP_HEIGHT, TRUE, DEFAULT_SEA_STATE);
if (result)
return 1.0;
else
return 0.0;
}
// Can the observer hear the target?
float Sonar_Detection_New(double Range, Submarine *observer, Submarine *target)
{
float NauticalMiles = (float) Range / 2000.0;
float HisPassiveSonarCrosssection = target->PSCS;
float EffectiveTargetSpeed;
float AmbientNoise;
float OwnShipNoise;
float TotalNoise;
float TargetNoise;
float Gb;
float Lbp;
float NoiseFromSpeed;
float BasisNoiseLevel;
float value;
float SeaState = 3.0; // Anyone want to model the weather?
int thermal_layers = 0;
if (target->Speed <= 5.0) {
EffectiveTargetSpeed = 0.0;
} else {
EffectiveTargetSpeed = target->Speed - 5.0;
}
if (target->Speed < 20.0) {
NoiseFromSpeed = 1.30;
BasisNoiseLevel = 0.0;
} else {
NoiseFromSpeed = 0.65;
BasisNoiseLevel = 9.75;
}
thermal_layers = my_map.Thermals_Between(observer->Depth, target->Depth);
#ifdef DEBUGMAP
printf("There are %d thermal layers between us.\n", thermal_layers);
#endif
AmbientNoise = 89.0 + (5.0 * SeaState);
OwnShipNoise = observer->RadiatedNoise();
TotalNoise = 10.0 * log10(pow(10.0, OwnShipNoise / 10.0) + pow(10.0, AmbientNoise / 10.0));
Gb = (TotalNoise - AmbientNoise) / 2.9;
Lbp = AmbientNoise + Gb;
TargetNoise = HisPassiveSonarCrosssection +
((NoiseFromSpeed * EffectiveTargetSpeed) + BasisNoiseLevel);
#ifdef DEBUGMAP
printf("Old target noise: %f\n", TargetNoise);
#endif
if (thermal_layers)
TargetNoise -= TargetNoise * (thermal_layers * THERMAL_FILTER);
#ifdef DEBUGMAP
printf("New target noise: %f\n", TargetNoise);
#endif
value = TargetNoise - (20.0 * log10(NauticalMiles) + 1.1 * NauticalMiles) - Lbp;
if (!observer)
SonarStation.setFlowandambientnoise(Lbp - 34);
if (value > -45.0) {
return (value - -45.0) + 1.0;
} else {
return 0.0;
}
}
void SoundEnvironment()
{
//*********************************************************************
// This will gather sound information from all platforms and place
// messages into each platforms sonar queue.
//*********************************************************************
int bearing;
double Range;
Submarine *target;
int line = FALSE;
// loop through each ship and let them listen
// for the other ships...
#ifdef DEBUG
printf("Advancing sonar.\n");
#endif
Subs->AdvanceSonarHistory(); // advance sonar queue by 1/5th of a second
TB16.AdvanceSonarHistory(); // advance sonar queue by 1/5th of a second
// for (target = Subs->next; target; target = target->next) {
target = Subs->next;
while (target) {
#ifdef DEBUG
printf("Checking if we can hear other ships.\n");
#endif
if (!player->InBaffles(target, 1, &TB16)) { // I'm not deaf?
Range = CalculateRange(target, Subs);
float signal;
signal = Any_Detection(Range, Subs, target);
if (signal) { // Are we audible?
#ifdef DEBUG
printf("Heard you, adding to list.\n");
#endif
bearing = (int) Subs->BearingToTarget(target); // Change me to float for better tma
// this weird check prevents us from
// erasing sonar data with radar/esm data
if ((signal == 1.0) || (signal == 2.0)) {
float temp_signal;
temp_signal = Sonar_Detection_New(Range, Subs, target);
Subs->RegisterEvent(bearing, temp_signal, target->ShipType);
} else
Subs->RegisterEvent(bearing, signal, target->ShipType);
// printf("Adding target %d to list.\n", target);
Subs->Add_Target(target, signal);
} else {
// printf("Lost target %d from list.\n", target);
Subs->Remove_Target(target);
}
} else if (!player->InBaffles(target, 2, &TB16) && TB16.GetLength() > 240) { // do the same for towed array
Range = TB16.RangeToTarget(target->Lat_TotalYards, target->Lon_TotalYards);
float signal;
// signal = Sonar_Detection(Range, 0, target);
signal = Sonar_Detection_New(Range, Subs, target);
if (signal) { // Are we audible?
bearing = (int) TB16.BearingToTarget(target->Lat_TotalYards, target->Lon_TotalYards); // Change me to float for better tma
TB16.RegisterEvent(bearing, signal, target->ShipType);
// printf("Adding target %d to list.\n", target);
Subs->Add_Target(target, signal);
} else {
// printf("Lost target %d from list.\n", target);
Subs->Remove_Target(target);
}
} else // can't hear target with either sonar device
Subs->Remove_Target(target);
target = target->next;
if ((!target) && (!line)) {
target = torpedoes;
line = TRUE;
}
}
}
inline int RandInt(int TO) // Returns a random integer... TO is upper limit
{
return (rand() % TO);
}
inline int Clamp(int sample) // holds a value to be between 0 and 255
{
if (sample < 0) {
sample = 0;
}
if (sample > 255) {
sample = 255;
}
return sample;
}
inline double Clamp(double sample) // Overloaded for floats
{
if (sample < 0) {
sample = 0.0;
}
if (sample > 255) {
sample = 255.0;
}
return sample;
}
// Display the status of the sub: speed / ordered speed, heading / ordered
// heading, depth / ordered depth, and time
void DisplaySubStatus()
{
int hours, minutes, seconds;
SDL_Rect rectangle;
static DFont fnt("images/font.png", "data/font.dat");
static char text[120];
rectangle.x = 16;
rectangle.y = 14; // define a rectangle on the screen and make it black