Debian Codegraph @

src/helicopter.cpp

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <limits.h>
5 #include "helicopter.h"
6 #include "files.h"
7
8 Helicopter::Helicopter()
9 {
10 Init();
11 }
12
13
14 Helicopter::~Helicopter()
15 {
16 Cleanup();
17 }
18
19
20
21 int Helicopter::Init()
22 {
23 MaxSpeed = 0;
24 DesiredHeading = 0;
25 Speed = 0;
26 Heading = 0;
27 current_action = ACTION_HOVER;
28 mood = MOOD_PASSIVE;
29 Friend = FRIEND;
30 destination_x = destination_y = -1;
31 ShipType = TYPE_HELICOPTER;
32 ShipClass = CLASS_HELICOPTER;
33 MaxDepth = LISTEN_HEIGHT;
34 Rudder = HELICOPTER_RUBBER;
35 hull_strength = 1;
36 has_sonar = TRUE;
37 PSCS = 360.0;
38 firing_countdown = 0;
39 next = NULL;
40 return TRUE;
41 }
42
43
44
45 void Helicopter::Cleanup()
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 Helicopter::Load_Class(char *from_file)
58 {
59 FILE *my_file;
60 char line[256];
61 char *status;
62
63 if (! from_file)
64 return FALSE;
65 my_file = fopen(from_file, "r");
66 if (! my_file)
67 return FALSE;
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 &MaxSpeed, &MaxDepth, &Rudder, &TorpedoesOnBoard,
74 &hull_strength, &has_sonar, &PSCS, ClassName, ClassType);
75
76 }
77 fclose(my_file);
78 return TRUE;
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 Helicopter::Load_Mission(FILE *from_file)
87 {
88 char line[256];
89 char *status;
90 if (! from_file)
91 return FALSE;
92 status = fgets(line, 256, from_file);
93 if (! status)
94 return FALSE;
95 return TRUE;
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 Helicopter::Check_Status()
107 {
108 if (hull_strength > 0)
109 return TRUE;
110 else
111 return FALSE;
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 Submarine *Helicopter::Helicopter_AI(Submarine *all_ships, Submarine *all_torpedoes)
124 {
125 double distance_to_target;
126 int range, new_direction;
127 Submarine *ship = NULL, *target = NULL;
128 int do_what = DO_NOTHING;
129 int new_heading;
130 Submarine *torpedo, *my_torpedoes;
131 int found;
132
133 if (firing_countdown)
134 firing_countdown--;
135 #ifdef DEBUG_HELICOPTER
136 printf("Current action: %d\n", current_action);
137 #endif
138 switch (current_action)
139 {
140 case ACTION_HOVER:
141 destination_x = -1;
142 destination_y = -1;
143 DesiredSpeed = 0;
144 current_action = ACTION_MOVING;
145 break;
146 case ACTION_MOVING:
147 // no destination, set one
148 if ( (destination_x < 0) || (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 = rand() % 8;
154 range = MILES_TO_YARDS * 2;
155 switch (new_direction)
156 {
157 case 0: destination_x = Lat_TotalYards + range;
158 destination_y = Lon_TotalYards;
159 break; // west
160 case 1: destination_x = Lat_TotalYards + range;
161 destination_y = Lon_TotalYards - range;
162 break; // sw
163 case 2: destination_x = Lat_TotalYards;
164 destination_y = Lon_TotalYards - range;
165 break; // south
166 case 3: destination_x = Lat_TotalYards;
167 destination_y = Lon_TotalYards + range;
168 break; // north
169 case 4: destination_x = Lat_TotalYards - range;
170 destination_y = Lon_TotalYards - range;
171 break; // se
172 case 5: destination_x = Lat_TotalYards - range;
173 destination_y = Lon_TotalYards;
174 break; // east
175 case 6: destination_x = Lat_TotalYards + range;
176 destination_y = Lon_TotalYards + range;
177 break; // nw
178 case 7: destination_x = Lat_TotalYards - range;
179 destination_y = Lon_TotalYards + range;
180 break; // ne
181 } // end of pick new destination
182 DesiredDepth = MOVE_HEIGHT;
183 DesiredSpeed = MaxSpeed;
184 }
185 // figure out heading here
186 new_heading = Bearing_To_Destination();
187 if (new_heading != -1)
188 DesiredHeading = new_heading;
189 distance_to_target = Distance_To_Destination();
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_YARDS)
196 {
197 #ifdef DEBUG_HELICOPTER
198 printf("Close to destination, slow down.\n");
199 #endif
200 current_action = ACTION_LISTEN;
201 DesiredSpeed = 0;
202 DesiredDepth = LISTEN_HEIGHT;
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_YARDS) )
214 DesiredSpeed = MaxSpeed / 4;
215 break;
216
217 case ACTION_LISTEN:
218 if (destination_x >= 0)
219 destination_x = -1;
220 if (destination_y >= 0)
221 destination_y = -1;
222 DesiredSpeed = 0;
223 DesiredDepth = LISTEN_HEIGHT;
224 DesiredHeading = Heading;
225 if ( (Depth == LISTEN_HEIGHT) && (Speed < 1.0) )
226 {
227 // check all ships subs to find if we can hear a foe
228 target = Find_Target(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_Target(target);
235 // we hear a foe and it is close, fire
236 if (range < (TORPEDO_RANGE_PASSIVE * MILES_TO_YARDS) )
237 {
238 #ifdef DEBUG_HELICOPTER
239 printf("We can shoot at a sub.\n");
240 #endif
241 do_what = DO_SHOOT;
242 // do shooting here
243 if ( (TorpedoesOnBoard > 0) && (! firing_countdown) )
244 {
245 char *ship_file, filename[] = "ships/class5.shp";
246 ship_file = Find_Data_File(filename);
247 torpedo = Fire_Torpedo(target, ship_file);
248 if ( (ship_file) && (ship_file != filename) )
249 free(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 = FALSE;
261 while ( (! found) && (my_torpedoes) )
262 {
263 if (my_torpedoes->next)
264 my_torpedoes = my_torpedoes->next;
265 else
266 {
267 my_torpedoes->next = torpedo;
268 found = TRUE;
269 }
270 }
271 }
272 TorpedoesOnBoard--;
273 firing_countdown = FIRING_WAIT;
274 }
275 } // end of torpedoes on board
276 destination_x = target->Lat_TotalYards;
277 destination_y = target->Lon_TotalYards;
278 DesiredDepth = MOVE_HEIGHT;
279 DesiredSpeed = MaxSpeed;
280 current_action = ACTION_MOVING;
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_MOVE;
290 destination_x = target->Lat_TotalYards;
291 destination_y = target->Lon_TotalYards;
292 DesiredDepth = MOVE_HEIGHT;
293 DesiredSpeed = MaxSpeed;
294 current_action = ACTION_MOVING;
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_Friend(all_ships);
303 if (ship)
304 {
305 do_what = DO_MOVE;
306 destination_x = ship->Lat_TotalYards;
307 destination_y = ship->Lon_TotalYards;
308 DesiredDepth = MOVE_HEIGHT;
309 DesiredSpeed = 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_action = ACTION_HOVER;
316 } // end of we are listening
317 break;
318 default:
319 current_action = ACTION_HOVER;
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 Helicopter::Handle()
331 {
332 float AmountOfChange;
333 float delta_heading;
334
335 // first, handle height
336 if (DesiredDepth > LISTEN_HEIGHT)
337 DesiredDepth = LISTEN_HEIGHT;
338 else if (DesiredDepth < MOVE_HEIGHT)
339 DesiredDepth = MOVE_HEIGHT;
340
341 if (DesiredDepth > Depth)
342 Depth++;
343 else if (DesiredDepth < Depth)
344 Depth--;
345
346 AmountOfChange = Rudder / 10;
347 // turn toward desired heading
348 if (Heading > DesiredHeading){
349 if ((Heading - DesiredHeading) < 180){
350 Heading = Heading - AmountOfChange;
351 if ((Heading < DesiredHeading) &&
352 ((DesiredHeading - Heading) < AmountOfChange)){
353 Heading = (float)DesiredHeading;
354 }
355 }
356 else{
357 Heading = Heading + AmountOfChange;
358 if ((Heading > DesiredHeading) &&
359 ((Heading - DesiredHeading) < AmountOfChange)){
360 Heading = (float)DesiredHeading;
361 }
362 }
363 }
364 else{
365 if (Heading < DesiredHeading){
366 if ((DesiredHeading - Heading) < 180){
367 Heading += AmountOfChange;
368 if ((Heading > DesiredHeading) &&
369 ((Heading - DesiredHeading) < AmountOfChange)){
370 Heading = (float)DesiredHeading;
371 }
372 }
373 else{
374 Heading = Heading - AmountOfChange;
375 if ((Heading < DesiredHeading) &&
376 ((DesiredHeading - Heading) < AmountOfChange)){
377 Heading = (float)DesiredHeading;
378 }
379 }
380 }
381 }
382
383 // sanity check on course
384 if (Heading < 0)
385 Heading += 360;
386 else if (Heading > 359)
387 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 = Heading - DesiredHeading;
392 if ( (delta_heading < -45) || (delta_heading > 45) )
393 DesiredSpeed = MaxSpeed / 2;
394
395 // adjust speed
396 if (DesiredSpeed > Speed)
397 Speed += 1.0;
398 else if (DesiredSpeed < Speed)
399 Speed -= 1.0;
400 if (Speed < 0.0)
401 Speed = 0.0;
402 else if (Speed > MaxSpeed)
403 Speed = MaxSpeed;
404
405 return TRUE;
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 Helicopter::Bearing_To_Destination()
414 {
415 double delta_x = 0, delta_y = 0;
416 double bearing = 0;
417
418 if ( (destination_x < 0) || (destination_y < 0) )
419 return -1;
420
421 if (Lat_TotalYards > destination_x){
422 delta_x = Lat_TotalYards - destination_x;
423 }
424 else{
425 delta_x = destination_x - Lat_TotalYards;
426 }
427
428 if (Lon_TotalYards > destination_y){
429 delta_y = Lon_TotalYards - destination_y;
430 }
431 else{
432 delta_y = destination_y - Lon_TotalYards;
433 }
434
435 if ( (Lon_TotalYards < destination_y) &&
436 (Lat_TotalYards < destination_x) )
437 bearing = (360 - ((atan(delta_x / delta_y) * 360) / RADIAN_RATIO));
438 else if ( (Lon_TotalYards < destination_y) &&
439 (Lat_TotalYards > destination_x) )
440 bearing = (0 + ((atan(delta_x / delta_y) * 360) / RADIAN_RATIO));
441 else if ( (Lon_TotalYards > destination_y) &&
442 (Lat_TotalYards < destination_x) )
443 bearing = (180 + ((atan(delta_x / delta_y) * 360) / RADIAN_RATIO) );
444 else if ( (Lon_TotalYards > destination_y) &&
445 (Lat_TotalYards > destination_x) )
446 bearing = (180 - ((atan(delta_x / delta_y) * 360) / RADIAN_RATIO) );
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_TotalYards > destination_x)
453 bearing = 90;
454 else
455 bearing = 270;
456 }
457 if (delta_x == 0)
458 {
459 if (Lon_TotalYards > 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_x = -1;
471 destination_y = -1;
472 current_action = ACTION_MOVING;
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 Helicopter::Distance_To_Destination()
485 {
486 int delta_x, delta_y;
487 if ( (destination_x < 0) || (destination_y < 0) )
488 return 0;
489
490 delta_x = destination_x - Lat_TotalYards;
491 if (delta_x < 0)
492 delta_x = -delta_x;
493 delta_y = destination_y - Lon_TotalYards;
494 if (delta_y < 0)
495 delta_y = -delta_y;
496
497 return sqrt( (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 Helicopter::Distance_To_Target(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_TotalYards > Target->Lat_TotalYards){
513 latdif = Lat_TotalYards - Target->Lat_TotalYards;
514 }
515 else{
516 latdif = Target->Lat_TotalYards - Lat_TotalYards;
517 }
518
519 if (Lon_TotalYards > Target->Lon_TotalYards){
520 londif = Lon_TotalYards - Target->Lon_TotalYards;
521 }
522 else{
523 londif = Target->Lon_TotalYards - Lon_TotalYards;
524 }
525 return sqrt((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 Submarine *Helicopter::Find_Target(Submarine *all_ships)
537 {
538 Submarine *ship;
539 Submarine *best_target = NULL;
540 double min_range = INT_MAX;
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->ShipType == TYPE_SUB)
549 {
550 // check to see if it is a foe
551 if ( ((ship->Friend == FOE) && (Friend == FRIEND)) ||
552 ( (ship->Friend == FRIEND) && (Friend == FOE)) )
553 {
554 // check to see if it is close
555 current_range = Distance_To_Target(ship);
556 if (current_range < min_range)
557 {
558 // check to see if we can hear it
559 status = Can_Hear(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->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 Helicopter::Can_Hear(Submarine *target)
579 {
580 float Range = Distance_To_Target(target);
581 float NauticalMiles = (float)Range / 2000.0;
582 float HisPassiveSonarCrosssection = target->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 FALSE;
600
601 if (target->Speed <= 5.0){
602 EffectiveTargetSpeed = 0.0;
603 }else{
604 EffectiveTargetSpeed = target->Speed - 5.0;
605 }
606
607 if (target->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 (map)
616 {
617 thermal_layers = map->Thermals_Between(Depth, target->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 * log10(pow(10.0,OwnShipNoise/10.0) + pow(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->pinging)
631 TargetNoise += PING_NOISE;
632 if (thermal_layers)
633 TargetNoise -= TargetNoise * (thermal_layers * THERMAL_FILTER);
634 value = TargetNoise - (20.0 * log10(NauticalMiles) + 1.1 * NauticalMiles) - Lbp;
635 // if (!observer)
636 // SonarStation.flowandambientnoise = (Lbp - 34);
637 if (value > minimum_sound){
638 return TRUE;
639 }else{
640 return FALSE;
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 Submarine *Helicopter::Fire_Torpedo(Submarine *target, char *ship_file)
655 {
656 Submarine *my_torp;
657 my_torp = new Submarine();
658 if (! my_torp)
659 return NULL;
660
661 my_torp->Load_Class(ship_file);
662 if (target)
663 {
664 my_torp->target = target;
665 // set heading and desired depth
666 my_torp->DesiredHeading = my_torp->Heading = my_torp->BearingToTarget(target);
667 my_torp->DesiredDepth = target->Depth;
668 my_torp->ShipType = TYPE_TORPEDO;
669 }
670 else // no target, noisemaker
671 {
672 my_torp->target = NULL;
673 // set random heading
674 my_torp->Heading = rand() % 360;
675 my_torp->DesiredHeading = my_torp->Heading;
676 my_torp->ShipType = TYPE_NOISEMAKER;
677 }
678
679 // set current position
680 my_torp->Lat_TotalYards = Lat_TotalYards;
681 my_torp->Lon_TotalYards = Lon_TotalYards;
682 // set fuel
683 my_torp->fuel_remaining = TORPEDO_FUEL;
684 // set depth
685 my_torp->Depth = Depth;
686 // set speed and desired speed
687 my_torp->Speed = Speed;
688 my_torp->DesiredSpeed = my_torp->MaxSpeed;
689 // my_torp->ShipType = TYPE_TORPEDO;
690 // my_torp->Friend = FOE;
691 my_torp->Friend = Friend;
692 my_torp->origin_x = Lat_TotalYards;
693 my_torp->origin_y = 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 Submarine *Helicopter::Find_Closest_Friend(Submarine *ships)
703 {
704 double nearest = INT_MAX;
705 double range;
706 Submarine *best_fit = NULL;
707 Submarine *current;
708
709 current = ships;
710 while (current)
711 {
712 if ( (current->ShipType == TYPE_SHIP) && (current->Friend == Friend) )
713 {
714 range = Distance_To_Target(current);
715 if (range < nearest)
716 {
717 best_fit = current;
718 nearest = range;
719 }
720 }
721 current = current->next;
722 }
723 return best_fit;
724 }
725