File: mission.cpp

package info (click to toggle)
openssn 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,340 kB
  • sloc: cpp: 10,973; makefile: 80
file content (114 lines) | stat: -rw-r--r-- 2,846 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include "mission.h"


// This function checks to see what our missions status should be. It
// returns MISSION_STARTED, MISSION_SUCCESS or MISSION_FAILED
// The old_status should also be one of these, indicating what our
// status was before this function was called.
// For example "my_mission = Mission_Status(Subs, my_mission);"
int Mission_Status(Submarine *all_subs, int old_status)
{
   Submarine *current_sub;
   int found;

   // if the mission already failed it is too late to save it
   if (old_status == MISSION_FAILED)
      return MISSION_FAILED;

   if (! all_subs)
     return MISSION_FAILED;

   // go through each sub and check its status
   current_sub = all_subs;
   found = FALSE;
   while ( (current_sub) && (! found) )
   {
       if (current_sub->mission_status == MISSION_NONE)
       {
          #ifdef DEBUG
          printf("Moving to next ship that might have a mission.\n");
          #endif
          current_sub = current_sub->next;
       }
       else
       {
          #ifdef DEBUG
          printf("Found ship that still has a mission.\n");
          #endif
          found = TRUE;
       }
   }

   // mission not complete
   if (found)
       return MISSION_STARTED;
   return MISSION_SUCCESS;
}



int Check_Find(Submarine *all_subs)
{
  Submarine *player, *current_sub;
  int signal_strength;

  if (! all_subs)
     return FALSE;

  player = all_subs;
  current_sub = all_subs->next;
  while (current_sub)
  {
      // we are supposed to be found?
      if (current_sub->mission_status == MISSION_FIND)
      {
         #ifdef DEBUG
         printf("This ship wants to be found.\n");
         #endif
         signal_strength = player->Can_Detect(current_sub);
         // we can hear you 
         if (signal_strength == CONTACT_SOLID)
         {
           #ifdef DEBUG
           printf("This ship has a contact level of %d\n", signal_strength);
           #endif
           current_sub->mission_status = MISSION_NONE;
         }
      }
      current_sub = current_sub->next;
  }

  return TRUE;
}



// This function checks to see if the mission depends on
// any ships remaining alive for a given amount of time.
// Any ships which have the MISSION_ALIVE flag set will
// have their timer reduced. If the timer hits zero (0)
// then we change the mission flag to be MISSION_NONE.
// This function returns TRUE if everything goes normally
// or FALSE if we encounter an error.
int Check_Alive(Submarine *all_subs)
{
   Submarine *current;

   if (! all_subs)
      return FALSE;

   current = all_subs;
   while (current)
   {
      if (current->mission_status == MISSION_ALIVE)
      {
          current->mission_timer--;
          if (current->mission_timer < 1)
             current->mission_status = MISSION_NONE;
      }
      current = current->next;
   }

   return TRUE;
}