Debian Codegraph @

src/helicopter.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/src/helicopter.cpp`/include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <limits.h>
5 #include "helicopter.h"cxx . . $ `<file>/src/helicopter.h`/
6 #include "files.h"cxx . . $ `<file>/src/files.h`/
7
8 Helicoptercxx . . $ Helicopter#::Helicoptercxx . . $ Helicopter#Helicopter(49f6e7a06ebc5aa8).()
9 {
10 Initcxx . . $ Helicopter#Init(b126dc7c1de90089).();
11 }
12
13
14 Helicoptercxx . . $ Helicopter#::~cxx . . $ Helicopter#`~Helicopter`(ced63f7c635d850d).Helicoptercxx . . $ Helicopter#()
15 {
16 Cleanupcxx . . $ Helicopter#Cleanup(49f6e7a06ebc5aa8).();
17 }
18
19
20
21 int Helicoptercxx . . $ Helicopter#::Initcxx . . $ Helicopter#Init(b126dc7c1de90089).()
22 {
23 MaxSpeedcxx . . $ Helicopter#MaxSpeed. = 0;
24 DesiredHeadingcxx . . $ Helicopter#DesiredHeading. = 0;
25 Speedcxx . . $ Coord#Speed. = 0;
26 Headingcxx . . $ Coord#Heading. = 0;
27 current_actioncxx . . $ Helicopter#current_action. = ACTION_HOVERcxx . . $ `src/helicopter.h:15:9`!;
28 moodcxx . . $ Helicopter#mood. = MOOD_PASSIVEcxx . . $ `src/submarine.h:128:9`!;
29 Friendcxx . . $ Helicopter#Friend. = FRIENDcxx . . $ `src/submarine.h:42:9`!;
30 destination_xcxx . . $ Helicopter#destination_x. = destination_ycxx . . $ Helicopter#destination_y. = -1;
31 ShipTypecxx . . $ Helicopter#ShipType. = TYPE_HELICOPTERcxx . . $ `src/submarine.h:40:9`!;
32 ShipClasscxx . . $ Helicopter#ShipClass. = CLASS_HELICOPTERcxx . . $ `src/submarine.h:65:9`!;
33 MaxDepthcxx . . $ Helicopter#MaxDepth. = LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!;
34 Ruddercxx . . $ Helicopter#Rudder. = HELICOPTER_RUBBERcxx . . $ `src/helicopter.h:23:9`!;
35 hull_strengthcxx . . $ Helicopter#hull_strength. = 1;
36 has_sonarcxx . . $ Helicopter#has_sonar. = TRUEcxx . . $ `src/map.h:10:9`!;
37 PSCScxx . . $ Helicopter#PSCS. = 360.0;
38 firing_countdowncxx . . $ Helicopter#firing_countdown. = 0;
39 nextcxx . . $ Helicopter#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
40 return TRUEcxx . . $ `src/map.h:10:9`!;
41 }
42
43
44
45 void Helicoptercxx . . $ Helicopter#::Cleanupcxx . . $ Helicopter#Cleanup(49f6e7a06ebc5aa8).()
46 {
47 // nothing to clean yet
48 }
49
50
51 // This function should be called right after the helicopter is created. It
52 // will load the ship's class specific information from the appropriate
53 // data file.
54 // The data file's format will be
55 // maxspeed maxdepth rudder torpedoes-carried hull_strength has_sonar, P
56 // SCS class-name class-type
57 int Helicoptercxx . . $ Helicopter#::Load_Classcxx . . $ Helicopter#Load_Class(1ef8e6ec083aabb9).(char *from_file)
58 {
59 FILE *my_file;
60 char line[256];
61 char *status;
62
63 if (! from_file)
64 return FALSEcxx . . $ `src/map.h:13:9`!;
65 my_file = fopen(from_file, "r");
66 if (! my_file)
67 return FALSEcxx . . $ `src/map.h:13:9`!;
68
69 status = fgets(line, 256, my_file);
70 if (status)
71 {
72 sscanf(line, "%d %d %d %d %d %d %f %s %s",
73 &MaxSpeedcxx . . $ Helicopter#MaxSpeed., &MaxDepthcxx . . $ Helicopter#MaxDepth., &Ruddercxx . . $ Helicopter#Rudder., &TorpedoesOnBoardcxx . . $ Helicopter#TorpedoesOnBoard.,
74 &hull_strengthcxx . . $ Helicopter#hull_strength., &has_sonarcxx . . $ Helicopter#has_sonar., &PSCScxx . . $ Helicopter#PSCS., ClassNamecxx . . $ Helicopter#ClassName., ClassTypecxx . . $ Helicopter#ClassType.);
75
76 }
77 fclose(my_file);
78 return TRUEcxx . . $ `src/map.h:10:9`!;
79 }
80
81
82 // Helicopters do not receive mission objectives
83 // yet, but they may have an entry in the mission file
84 // for formatting. This function simply
85 // reads one line from the file and returns
86 int Helicoptercxx . . $ Helicopter#::Load_Missioncxx . . $ Helicopter#Load_Mission(89bc1efcdac0ca22).(FILE *from_file)
87 {
88 char line[256];
89 char *status;
90 if (! from_file)
91 return FALSEcxx . . $ `src/map.h:13:9`!;
92 status = fgets(line, 256, from_file);
93 if (! status)
94 return FALSEcxx . . $ `src/map.h:13:9`!;
95 return TRUEcxx . . $ `src/map.h:10:9`!;
96 }
97
98
99
100 // This function lets us know if the helicopter is okay
101 // if it something has happened to make it die.
102 // Returns TRUE if the helicopter can still fly or
103 // FALSE if it has died.
104 // We might add checks here later for running out
105 // of fuel....
106 int Helicoptercxx . . $ Helicopter#::Check_Statuscxx . . $ Helicopter#Check_Status(b126dc7c1de90089).()
107 {
108 if (hull_strengthcxx . . $ Helicopter#hull_strength. > 0)
109 return TRUEcxx . . $ `src/map.h:10:9`!;
110 else
111 return FALSEcxx . . $ `src/map.h:13:9`!;
112
113 }
114
115
116
117 // This function will determine what the helicopeter is doing.
118 // Ideally we want to patrol more or less randomly, looking
119 // for enemy submarines. When we hear an enemy we
120 // will move close, confirm we can still hear it and fire.
121 // When we are out of torpedoes we should probably
122 // continue to patrol, and radio sightings
123 Submarinecxx . . $ Submarine# *Helicoptercxx . . $ Helicopter#::Helicopter_AIcxx . . $ Helicopter#Helicopter_AI(d62bab4205bed956).(Submarinecxx . . $ Submarine# *all_ships, Submarinecxx . . $ Submarine# *all_torpedoes)
124 {
125 double distance_to_target;
126 int range, new_direction;
127 Submarinecxx . . $ Submarine# *ship = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, *target = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
128 int do_what = DO_NOTHINGcxx . . $ `src/helicopter.h:26:9`!;
129 int new_heading;
130 Submarinecxx . . $ Submarine# *torpedo, *my_torpedoes;
131 int found;
132
133 if (firing_countdowncxx . . $ Helicopter#firing_countdown.)
134 firing_countdowncxx . . $ Helicopter#firing_countdown.--;
135 #ifdef DEBUG_HELICOPTER
136 printf("Current action: %d\n", current_action);
137 #endif
138 switch (current_actioncxx . . $ Helicopter#current_action.)
139 {
140 case ACTION_HOVERcxx . . $ `src/helicopter.h:15:9`!:
141 destination_xcxx . . $ Helicopter#destination_x. = -1;
142 destination_ycxx . . $ Helicopter#destination_y. = -1;
143 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = 0;
144 current_actioncxx . . $ Helicopter#current_action. = ACTION_MOVINGcxx . . $ `src/helicopter.h:16:9`!;
145 break;
146 case ACTION_MOVINGcxx . . $ `src/helicopter.h:16:9`!:
147 // no destination, set one
148 if ( (destination_xcxx . . $ Helicopter#destination_x. < 0) || (destination_ycxx . . $ Helicopter#destination_y. < 0) )
149 {
150 // destination_x = Lat_TotalYards + ((rand() % 4000) - 2000);
151 // destination_y = Lon_TotalYards + ((rand() % 4000) - 2000);
152 // pick direction 0-7
153 new_direction = randcxx . . $ rand(9efc7249d839d30e).() % 8;
154 range = MILES_TO_YARDScxx . . $ `src/submarine.h:114:9`! * 2;
155 switch (new_direction)
156 {
157 case 0: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. + range;
158 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
159 break; // west
160 case 1: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. + range;
161 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - range;
162 break; // sw
163 case 2: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
164 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - range;
165 break; // south
166 case 3: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
167 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. + range;
168 break; // north
169 case 4: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - range;
170 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - range;
171 break; // se
172 case 5: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - range;
173 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
174 break; // east
175 case 6: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. + range;
176 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. + range;
177 break; // nw
178 case 7: destination_xcxx . . $ Helicopter#destination_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - range;
179 destination_ycxx . . $ Helicopter#destination_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. + range;
180 break; // ne
181 } // end of pick new destination
182 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!;
183 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed.;
184 }
185 // figure out heading here
186 new_heading = Bearing_To_Destinationcxx . . $ Helicopter#Bearing_To_Destination(b126dc7c1de90089).();
187 if (new_heading != -1)
188 DesiredHeadingcxx . . $ Helicopter#DesiredHeading. = new_heading;
189 distance_to_target = Distance_To_Destinationcxx . . $ Helicopter#Distance_To_Destination(31fa7155c10fdd13).();
190 #ifdef DEBUG_HELICOPTER
191 printf("DesiredHeading: %d\n", DesiredHeading);
192 printf("Distance: %lf\n", distance_to_target);
193 #endif
194 // near destination, stop then switch to listening
195 if (distance_to_target < MILES_TO_YARDScxx . . $ `src/submarine.h:114:9`!)
196 {
197 #ifdef DEBUG_HELICOPTER
198 printf("Close to destination, slow down.\n");
199 #endif
200 current_actioncxx . . $ Helicopter#current_action. = ACTION_LISTENcxx . . $ `src/helicopter.h:17:9`!;
201 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = 0;
202 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!;
203 }
204 /*
205 // to far away from target, pick new destination
206 else if (distance_to_target > (MAX_DISTANCE * MILES_TO_YARDS) )
207 {
208 destination_x = -1;
209 destination_y = -1;
210 }
211 */
212 // close to target, slow down
213 else if (distance_to_target < (2 * MILES_TO_YARDScxx . . $ `src/submarine.h:114:9`!) )
214 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed. / 4;
215 break;
216
217 case ACTION_LISTENcxx . . $ `src/helicopter.h:17:9`!:
218 if (destination_xcxx . . $ Helicopter#destination_x. >= 0)
219 destination_xcxx . . $ Helicopter#destination_x. = -1;
220 if (destination_ycxx . . $ Helicopter#destination_y. >= 0)
221 destination_ycxx . . $ Helicopter#destination_y. = -1;
222 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = 0;
223 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!;
224 DesiredHeadingcxx . . $ Helicopter#DesiredHeading. = Headingcxx . . $ Coord#Heading.;
225 if ( (Depthcxx . . $ Helicopter#Depth. == LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!) && (Speedcxx . . $ Coord#Speed. < 1.0) )
226 {
227 // check all ships subs to find if we can hear a foe
228 target = Find_Targetcxx . . $ Helicopter#Find_Target(9c4f5e11a658b20d).(all_ships);
229 if (target)
230 {
231 #ifdef DEBUG_HELICOPTER
232 printf("We can hear something, checking range.\n");
233 #endif
234 range = Distance_To_Targetcxx . . $ Helicopter#Distance_To_Target(2de0d0e522051339).(target);
235 // we hear a foe and it is close, fire
236 if (range < (TORPEDO_RANGE_PASSIVEcxx . . $ `src/submarine.h:122:9`! * MILES_TO_YARDScxx . . $ `src/submarine.h:114:9`!) )
237 {
238 #ifdef DEBUG_HELICOPTER
239 printf("We can shoot at a sub.\n");
240 #endif
241 do_what = DO_SHOOTcxx . . $ `src/helicopter.h:27:9`!;
242 // do shooting here
243 if ( (TorpedoesOnBoardcxx . . $ Helicopter#TorpedoesOnBoard. > 0) && (! firing_countdowncxx . . $ Helicopter#firing_countdown.) )
244 {
245 char *ship_file, filename[] = "ships/class5.shp";
246 ship_file = Find_Data_Filecxx . . $ Find_Data_File(7a9722d75b371431).(filename);
247 torpedo = Fire_Torpedocxx . . $ Helicopter#Fire_Torpedo(7e9c5f383c561fca).(target, ship_file);
248 if ( (ship_file) && (ship_file != filename) )
249 freecxx . . $ free(bd21765a0afc8e3c).(ship_file);
250 if (torpedo)
251 {
252 #ifdef DEBUG_HELICOPTER
253 printf("We fired a torpedo.\n");
254 #endif
255 if (! all_torpedoes)
256 all_torpedoes = torpedo;
257 else
258 {
259 my_torpedoes = all_torpedoes;
260 found = FALSEcxx . . $ `src/map.h:13:9`!;
261 while ( (! found) && (my_torpedoes) )
262 {
263 if (my_torpedoes->nextcxx . . $ Submarine#next.)
264 my_torpedoes = my_torpedoes->nextcxx . . $ Submarine#next.;
265 else
266 {
267 my_torpedoes->nextcxx . . $ Submarine#next. = torpedo;
268 found = TRUEcxx . . $ `src/map.h:10:9`!;
269 }
270 }
271 }
272 TorpedoesOnBoardcxx . . $ Helicopter#TorpedoesOnBoard.--;
273 firing_countdowncxx . . $ Helicopter#firing_countdown. = FIRING_WAITcxx . . $ `src/helicopter.h:24:9`!;
274 }
275 } // end of torpedoes on board
276 destination_xcxx . . $ Helicopter#destination_x. = target->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
277 destination_ycxx . . $ Helicopter#destination_y. = target->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
278 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!;
279 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed.;
280 current_actioncxx . . $ Helicopter#current_action. = ACTION_MOVINGcxx . . $ `src/helicopter.h:16:9`!;
281 return all_torpedoes;
282 }
283 // we hear a foe and it is far away, move
284 else
285 {
286 #ifdef DEBUG_HELICOPTER
287 printf("We can hear a sub, we need to move closer.\n");
288 #endif
289 do_what = DO_MOVEcxx . . $ `src/helicopter.h:28:9`!;
290 destination_xcxx . . $ Helicopter#destination_x. = target->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
291 destination_ycxx . . $ Helicopter#destination_y. = target->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
292 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!;
293 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed.;
294 current_actioncxx . . $ Helicopter#current_action. = ACTION_MOVINGcxx . . $ `src/helicopter.h:16:9`!;
295 return all_torpedoes;
296 }
297
298 } // end of we found a target
299
300 // No target? then we should find a friendy ship
301 // to get close to
302 ship = Find_Closest_Friendcxx . . $ Helicopter#Find_Closest_Friend(9c4f5e11a658b20d).(all_ships);
303 if (ship)
304 {
305 do_what = DO_MOVEcxx . . $ `src/helicopter.h:28:9`!;
306 destination_xcxx . . $ Helicopter#destination_x. = ship->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
307 destination_ycxx . . $ Helicopter#destination_y. = ship->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
308 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!;
309 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed. / 2;
310 }
311
312 // we do not hear anything, move randomly and listen again
313 // when in doubt, we are hovering
314 if (! do_what)
315 current_actioncxx . . $ Helicopter#current_action. = ACTION_HOVERcxx . . $ `src/helicopter.h:15:9`!;
316 } // end of we are listening
317 break;
318 default:
319 current_actioncxx . . $ Helicopter#current_action. = ACTION_HOVERcxx . . $ `src/helicopter.h:15:9`!;
320
321 } // end of switch actions
322
323 return all_torpedoes;
324 }
325
326
327
328 // This function handles moving the helicopter. It will
329 // adjust the craft up/down, turn, and adjust speed.
330 int Helicoptercxx . . $ Helicopter#::Handlecxx . . $ Helicopter#Handle(b126dc7c1de90089).()
331 {
332 float AmountOfChange;
333 float delta_heading;
334
335 // first, handle height
336 if (DesiredDepthcxx . . $ Helicopter#DesiredDepth. > LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!)
337 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = LISTEN_HEIGHTcxx . . $ `src/helicopter.h:21:9`!;
338 else if (DesiredDepthcxx . . $ Helicopter#DesiredDepth. < MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!)
339 DesiredDepthcxx . . $ Helicopter#DesiredDepth. = MOVE_HEIGHTcxx . . $ `src/helicopter.h:22:9`!;
340
341 if (DesiredDepthcxx . . $ Helicopter#DesiredDepth. > Depthcxx . . $ Helicopter#Depth.)
342 Depthcxx . . $ Helicopter#Depth.++;
343 else if (DesiredDepthcxx . . $ Helicopter#DesiredDepth. < Depthcxx . . $ Helicopter#Depth.)
344 Depthcxx . . $ Helicopter#Depth.--;
345
346 AmountOfChange = Ruddercxx . . $ Helicopter#Rudder. / 10;
347 // turn toward desired heading
348 if (Headingcxx . . $ Coord#Heading. > DesiredHeadingcxx . . $ Helicopter#DesiredHeading.){
349 if ((Headingcxx . . $ Coord#Heading. - DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) < 180){
350 Headingcxx . . $ Coord#Heading. = Headingcxx . . $ Coord#Heading. - AmountOfChange;
351 if ((Headingcxx . . $ Coord#Heading. < DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) &&
352 ((DesiredHeadingcxx . . $ Helicopter#DesiredHeading. - Headingcxx . . $ Coord#Heading.) < AmountOfChange)){
353 Headingcxx . . $ Coord#Heading. = (float)DesiredHeadingcxx . . $ Helicopter#DesiredHeading.;
354 }
355 }
356 else{
357 Headingcxx . . $ Coord#Heading. = Headingcxx . . $ Coord#Heading. + AmountOfChange;
358 if ((Headingcxx . . $ Coord#Heading. > DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) &&
359 ((Headingcxx . . $ Coord#Heading. - DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) < AmountOfChange)){
360 Headingcxx . . $ Coord#Heading. = (float)DesiredHeadingcxx . . $ Helicopter#DesiredHeading.;
361 }
362 }
363 }
364 else{
365 if (Headingcxx . . $ Coord#Heading. < DesiredHeadingcxx . . $ Helicopter#DesiredHeading.){
366 if ((DesiredHeadingcxx . . $ Helicopter#DesiredHeading. - Headingcxx . . $ Coord#Heading.) < 180){
367 Headingcxx . . $ Coord#Heading. += AmountOfChange;
368 if ((Headingcxx . . $ Coord#Heading. > DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) &&
369 ((Headingcxx . . $ Coord#Heading. - DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) < AmountOfChange)){
370 Headingcxx . . $ Coord#Heading. = (float)DesiredHeadingcxx . . $ Helicopter#DesiredHeading.;
371 }
372 }
373 else{
374 Headingcxx . . $ Coord#Heading. = Headingcxx . . $ Coord#Heading. - AmountOfChange;
375 if ((Headingcxx . . $ Coord#Heading. < DesiredHeadingcxx . . $ Helicopter#DesiredHeading.) &&
376 ((DesiredHeadingcxx . . $ Helicopter#DesiredHeading. - Headingcxx . . $ Coord#Heading.) < AmountOfChange)){
377 Headingcxx . . $ Coord#Heading. = (float)DesiredHeadingcxx . . $ Helicopter#DesiredHeading.;
378 }
379 }
380 }
381 }
382
383 // sanity check on course
384 if (Headingcxx . . $ Coord#Heading. < 0)
385 Headingcxx . . $ Coord#Heading. += 360;
386 else if (Headingcxx . . $ Coord#Heading. > 359)
387 Headingcxx . . $ Coord#Heading. -= 360;
388
389 // sanity check on speed, if we need to turn a lot to
390 // get to our destination, slow to half speed
391 delta_heading = Headingcxx . . $ Coord#Heading. - DesiredHeadingcxx . . $ Helicopter#DesiredHeading.;
392 if ( (delta_heading < -45) || (delta_heading > 45) )
393 DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed. / 2;
394
395 // adjust speed
396 if (DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. > Speedcxx . . $ Coord#Speed.)
397 Speedcxx . . $ Coord#Speed. += 1.0;
398 else if (DesiredSpeedcxx . . $ Helicopter#DesiredSpeed. < Speedcxx . . $ Coord#Speed.)
399 Speedcxx . . $ Coord#Speed. -= 1.0;
400 if (Speedcxx . . $ Coord#Speed. < 0.0)
401 Speedcxx . . $ Coord#Speed. = 0.0;
402 else if (Speedcxx . . $ Coord#Speed. > MaxSpeedcxx . . $ Helicopter#MaxSpeed.)
403 Speedcxx . . $ Coord#Speed. = MaxSpeedcxx . . $ Helicopter#MaxSpeed.;
404
405 return TRUEcxx . . $ `src/map.h:10:9`!;
406 }
407
408
409
410 // This function returns the bearing (in degrees) to
411 // our destination. If no destination is set, then
412 // we return zero.
413 int Helicoptercxx . . $ Helicopter#::Bearing_To_Destinationcxx . . $ Helicopter#Bearing_To_Destination(b126dc7c1de90089).()
414 {
415 double delta_x = 0, delta_y = 0;
416 double bearing = 0;
417
418 if ( (destination_xcxx . . $ Helicopter#destination_x. < 0) || (destination_ycxx . . $ Helicopter#destination_y. < 0) )
419 return -1;
420
421 if (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. > destination_xcxx . . $ Helicopter#destination_x.){
422 delta_x = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - destination_xcxx . . $ Helicopter#destination_x.;
423 }
424 else{
425 delta_x = destination_xcxx . . $ Helicopter#destination_x. - Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
426 }
427
428 if (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. > destination_ycxx . . $ Helicopter#destination_y.){
429 delta_y = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - destination_ycxx . . $ Helicopter#destination_y.;
430 }
431 else{
432 delta_y = destination_ycxx . . $ Helicopter#destination_y. - Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
433 }
434
435 if ( (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. < destination_ycxx . . $ Helicopter#destination_y.) &&
436 (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. < destination_xcxx . . $ Helicopter#destination_x.) )
437 bearing = (360 - ((atancxx . . $ atan(fee4f1d22bc8e617).(delta_x / delta_y) * 360) / RADIAN_RATIOcxx . . $ `src/coord.h:31:9`!));
438 else if ( (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. < destination_ycxx . . $ Helicopter#destination_y.) &&
439 (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. > destination_xcxx . . $ Helicopter#destination_x.) )
440 bearing = (0 + ((atancxx . . $ atan(fee4f1d22bc8e617).(delta_x / delta_y) * 360) / RADIAN_RATIOcxx . . $ `src/coord.h:31:9`!));
441 else if ( (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. > destination_ycxx . . $ Helicopter#destination_y.) &&
442 (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. < destination_xcxx . . $ Helicopter#destination_x.) )
443 bearing = (180 + ((atancxx . . $ atan(fee4f1d22bc8e617).(delta_x / delta_y) * 360) / RADIAN_RATIOcxx . . $ `src/coord.h:31:9`!) );
444 else if ( (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. > destination_ycxx . . $ Helicopter#destination_y.) &&
445 (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. > destination_xcxx . . $ Helicopter#destination_x.) )
446 bearing = (180 - ((atancxx . . $ atan(fee4f1d22bc8e617).(delta_x / delta_y) * 360) / RADIAN_RATIOcxx . . $ `src/coord.h:31:9`!) );
447 #ifdef DEBUG_HELICOPTER
448 printf("Bearing to destination as double: %lf\n", bearing);
449 #endif
450 if (delta_y == 0)
451 {
452 if (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. > destination_xcxx . . $ Helicopter#destination_x.)
453 bearing = 90;
454 else
455 bearing = 270;
456 }
457 if (delta_x == 0)
458 {
459 if (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. > destination_ycxx . . $ Helicopter#destination_y.)
460 bearing = 180;
461 else
462 bearing = 0;
463 }
464 #ifdef DEBUG_HELICOPTER
465 printf("Bearing to destination as double: %lf\n", bearing);
466 #endif
467 if ( (bearing > 360.0) || (bearing < 0.0) )
468 {
469 // something has gone wrong. wipe destination
470 destination_xcxx . . $ Helicopter#destination_x. = -1;
471 destination_ycxx . . $ Helicopter#destination_y. = -1;
472 current_actioncxx . . $ Helicopter#current_action. = ACTION_MOVINGcxx . . $ `src/helicopter.h:16:9`!;
473 bearing = -1;
474 }
475
476 return (int) bearing;
477 }
478
479
480
481 // This function lets us know how far away
482 // we are (in yards) from out destination.
483 // If no destination is set, we return zero.
484 double Helicoptercxx . . $ Helicopter#::Distance_To_Destinationcxx . . $ Helicopter#Distance_To_Destination(31fa7155c10fdd13).()
485 {
486 int delta_x, delta_y;
487 if ( (destination_xcxx . . $ Helicopter#destination_x. < 0) || (destination_ycxx . . $ Helicopter#destination_y. < 0) )
488 return 0;
489
490 delta_x = destination_xcxx . . $ Helicopter#destination_x. - Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
491 if (delta_x < 0)
492 delta_x = -delta_x;
493 delta_y = destination_ycxx . . $ Helicopter#destination_y. - Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
494 if (delta_y < 0)
495 delta_y = -delta_y;
496
497 return sqrtcxx . . $ sqrt(7759b11ec49fe502).( (delta_x * delta_x) + (delta_y * delta_y) );
498 }
499
500
501
502 // This function finds out how far away a target is
503 // from our helicopter in yards.
504 double Helicoptercxx . . $ Helicopter#::Distance_To_Targetcxx . . $ Helicopter#Distance_To_Target(2de0d0e522051339).(Submarinecxx . . $ Submarine# *Target)
505 {
506 double latdif = 0, londif = 0; //sqrt needs doubles
507 //LatLonDifference( observer, target, &latdif, &londif );
508
509 if (! Target)
510 return 0;
511
512 if (Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. > Target->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.){
513 latdif = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - Target->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
514 }
515 else{
516 latdif = Target->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. - Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
517 }
518
519 if (Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. > Target->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.){
520 londif = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - Target->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
521 }
522 else{
523 londif = Target->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. - Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
524 }
525 return sqrtcxx . . $ sqrt(fee4f1d22bc8e617).((latdif * latdif) + (londif * londif));
526 }
527
528
529
530
531 // This function will go through the list of existing
532 // subs. It checks to see if we can hear
533 // any foes. If so, this function returns the closest
534 // enemy. If nothing is found, the function returns
535 // NULL.
536 Submarinecxx . . $ Submarine# *Helicoptercxx . . $ Helicopter#::Find_Targetcxx . . $ Helicopter#Find_Target(9c4f5e11a658b20d).(Submarinecxx . . $ Submarine# *all_ships)
537 {
538 Submarinecxx . . $ Submarine# *ship;
539 Submarinecxx . . $ Submarine# *best_target = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
540 double min_range = INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!;
541 double current_range;
542 int status;
543
544 ship = all_ships;
545 while (ship)
546 {
547 // check to see if it is a sub
548 if (ship->ShipTypecxx . . $ Submarine#ShipType. == TYPE_SUBcxx . . $ `src/submarine.h:35:9`!)
549 {
550 // check to see if it is a foe
551 if ( ((ship->Friendcxx . . $ Submarine#Friend. == FOEcxx . . $ `src/submarine.h:43:9`!) && (Friendcxx . . $ Helicopter#Friend. == FRIENDcxx . . $ `src/submarine.h:42:9`!)) ||
552 ( (ship->Friendcxx . . $ Submarine#Friend. == FRIENDcxx . . $ `src/submarine.h:42:9`!) && (Friendcxx . . $ Helicopter#Friend. == FOEcxx . . $ `src/submarine.h:43:9`!)) )
553 {
554 // check to see if it is close
555 current_range = Distance_To_Targetcxx . . $ Helicopter#Distance_To_Target(2de0d0e522051339).(ship);
556 if (current_range < min_range)
557 {
558 // check to see if we can hear it
559 status = Can_Hearcxx . . $ Helicopter#Can_Hear(e898ed6f8282e8).(ship);
560 if (status)
561 {
562 min_range = current_range;
563 best_target = ship;
564 } // end of we can hear this one
565 } // end of closer
566 } // endo f is enemy
567 } // end of proper type
568 ship = ship->nextcxx . . $ Submarine#next.;
569 }
570 return best_target;
571 }
572
573
574
575 // This function will let us know if we can hear a given ship/sub
576 // The function returns TRUE if we can hear the target or FALSE
577 // if we cannot.
578 int Helicoptercxx . . $ Helicopter#::Can_Hearcxx . . $ Helicopter#Can_Hear(e898ed6f8282e8).(Submarinecxx . . $ Submarine# *target)
579 {
580 float Range = Distance_To_Targetcxx . . $ Helicopter#Distance_To_Target(2de0d0e522051339).(target);
581 float NauticalMiles = (float)Range / 2000.0;
582 float HisPassiveSonarCrosssection = target->PSCScxx . . $ Submarine#PSCS.;
583 float EffectiveTargetSpeed;
584 float AmbientNoise;
585 float OwnShipNoise;
586 float TotalNoise;
587 float TargetNoise;
588 float Gb;
589 float Lbp;
590 float NoiseFromSpeed;
591 float BasisNoiseLevel;
592 float value;
593 float SeaState = 3.0; // Anyone want to model the weather.
594 float minimum_sound = -45.0;
595 int thermal_layers = 0;
596
597 // sanity check
598 if (! target)
599 return FALSEcxx . . $ `src/map.h:13:9`!;
600
601 if (target->Speedcxx . . $ Coord#Speed. <= 5.0){
602 EffectiveTargetSpeed = 0.0;
603 }else{
604 EffectiveTargetSpeed = target->Speedcxx . . $ Coord#Speed. - 5.0;
605 }
606
607 if (target->Speedcxx . . $ Coord#Speed. < 20.0){
608 NoiseFromSpeed = 1.30;
609 BasisNoiseLevel = 0.0;
610 }else{
611 NoiseFromSpeed = 0.65;
612 BasisNoiseLevel = 9.75;
613 }
614 // check just in case we didn't set the map variable
615 if (mapcxx . . $ Helicopter#map.)
616 {
617 thermal_layers = mapcxx . . $ Helicopter#map.->Thermals_Betweencxx . . $ MAP#Thermals_Between(9b79fb6aee4c0440).(Depthcxx . . $ Helicopter#Depth., target->Depthcxx . . $ Submarine#Depth.);
618 #ifdef DEBUGMAP
619 printf("%d thermals between ship and target\n", thermal_layers);
620 #endif
621 }
622 AmbientNoise = 89.0 + (5.0 * SeaState);
623 // OwnShipNoise = RadiatedNoise();
624 OwnShipNoise = 0.0;
625 TotalNoise = 10.0 * log10cxx . . $ log10(fee4f1d22bc8e617).(powcxx . . $ pow(b71928d39fde4295).(10.0,OwnShipNoise/10.0) + powcxx . . $ pow(b71928d39fde4295).(10.0,AmbientNoise/10.0));
626 Gb = (TotalNoise - AmbientNoise) / 2.9;
627 Lbp = AmbientNoise + Gb;
628 TargetNoise = HisPassiveSonarCrosssection +
629 ((NoiseFromSpeed * EffectiveTargetSpeed) + BasisNoiseLevel);
630 if (target->pingingcxx . . $ Submarine#pinging.)
631 TargetNoise += PING_NOISEcxx . . $ `src/submarine.h:50:9`!;
632 if (thermal_layers)
633 TargetNoise -= TargetNoise * (thermal_layers * THERMAL_FILTERcxx . . $ `src/map.h:20:9`!);
634 value = TargetNoise - (20.0 * log10cxx . . $ log10(fbcf3211dfe1bfd1).(NauticalMiles) + 1.1 * NauticalMiles) - Lbp;
635 // if (!observer)
636 // SonarStation.flowandambientnoise = (Lbp - 34);
637 if (value > minimum_sound){
638 return TRUEcxx . . $ `src/map.h:10:9`!;
639 }else{
640 return FALSEcxx . . $ `src/map.h:13:9`!;
641 }
642 }
643
644
645
646
647
648
649 // This function launches a torpedo or noise maker. If the
650 // passed target is NULL, the new torp/noisemaker is given a
651 // random course and no target, leaving it to run in a striaght line.
652 // On success the function passes back a pointer to the new torpedo
653 // and on failure NULL is returned.
654 Submarinecxx . . $ Submarine# *Helicoptercxx . . $ Helicopter#::Fire_Torpedocxx . . $ Helicopter#Fire_Torpedo(7e9c5f383c561fca).(Submarinecxx . . $ Submarine# *target, char *ship_file)
655 {
656 Submarinecxx . . $ Submarine# *my_torp;
657 my_torp = new Submarinecxx . . $ Submarine#();
658 if (! my_torp)
659 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
660
661 my_torp->Load_Classcxx . . $ Submarine#Load_Class(1ef8e6ec083aabb9).(ship_file);
662 if (target)
663 {
664 my_torp->targetcxx . . $ Submarine#target. = target;
665 // set heading and desired depth
666 my_torp->DesiredHeadingcxx . . $ Submarine#DesiredHeading. = my_torp->Headingcxx . . $ Coord#Heading. = my_torp->BearingToTargetcxx . . $ Submarine#BearingToTarget(2de0d0e522051339).(target);
667 my_torp->DesiredDepthcxx . . $ Submarine#DesiredDepth. = target->Depthcxx . . $ Submarine#Depth.;
668 my_torp->ShipTypecxx . . $ Submarine#ShipType. = TYPE_TORPEDOcxx . . $ `src/submarine.h:38:9`!;
669 }
670 else // no target, noisemaker
671 {
672 my_torp->targetcxx . . $ Submarine#target. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
673 // set random heading
674 my_torp->Headingcxx . . $ Coord#Heading. = randcxx . . $ rand(9efc7249d839d30e).() % 360;
675 my_torp->DesiredHeadingcxx . . $ Submarine#DesiredHeading. = my_torp->Headingcxx . . $ Coord#Heading.;
676 my_torp->ShipTypecxx . . $ Submarine#ShipType. = TYPE_NOISEMAKERcxx . . $ `src/submarine.h:39:9`!;
677 }
678
679 // set current position
680 my_torp->Lat_TotalYardscxx . . $ Coord#Lat_TotalYards. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
681 my_torp->Lon_TotalYardscxx . . $ Coord#Lon_TotalYards. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
682 // set fuel
683 my_torp->fuel_remainingcxx . . $ Submarine#fuel_remaining. = TORPEDO_FUELcxx . . $ `src/submarine.h:94:9`!;
684 // set depth
685 my_torp->Depthcxx . . $ Submarine#Depth. = Depthcxx . . $ Helicopter#Depth.;
686 // set speed and desired speed
687 my_torp->Speedcxx . . $ Coord#Speed. = Speedcxx . . $ Coord#Speed.;
688 my_torp->DesiredSpeedcxx . . $ Submarine#DesiredSpeed. = my_torp->MaxSpeedcxx . . $ Submarine#MaxSpeed.;
689 // my_torp->ShipType = TYPE_TORPEDO;
690 // my_torp->Friend = FOE;
691 my_torp->Friendcxx . . $ Submarine#Friend. = Friendcxx . . $ Helicopter#Friend.;
692 my_torp->origin_xcxx . . $ Submarine#origin_x. = Lat_TotalYardscxx . . $ Coord#Lat_TotalYards.;
693 my_torp->origin_ycxx . . $ Submarine#origin_y. = Lon_TotalYardscxx . . $ Coord#Lon_TotalYards.;
694 return my_torp;
695 }
696
697
698
699 // This function tries to locate the nearest friendly surface ship.
700 // If we find a friendly surface vessel that ship's pointer is
701 // returned. Otherwise we return NULL.
702 Submarinecxx . . $ Submarine# *Helicoptercxx . . $ Helicopter#::Find_Closest_Friendcxx . . $ Helicopter#Find_Closest_Friend(9c4f5e11a658b20d).(Submarinecxx . . $ Submarine# *ships)
703 {
704 double nearest = INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!;
705 double range;
706 Submarinecxx . . $ Submarine# *best_fit = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
707 Submarinecxx . . $ Submarine# *current;
708
709 current = ships;
710 while (current)
711 {
712 if ( (current->ShipTypecxx . . $ Submarine#ShipType. == TYPE_SHIPcxx . . $ `src/submarine.h:36:9`!) && (current->Friendcxx . . $ Submarine#Friend. == Friendcxx . . $ Helicopter#Friend.) )
713 {
714 range = Distance_To_Targetcxx . . $ Helicopter#Distance_To_Target(2de0d0e522051339).(current);
715 if (range < nearest)
716 {
717 best_fit = current;
718 nearest = range;
719 }
720 }
721 current = current->nextcxx . . $ Submarine#next.;
722 }
723 return best_fit;
724 }
725