1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "map.h"
4 
5 
MAP()6 MAP::MAP()
7 {
8    Init();
9 }
10 
11 
12 
~MAP()13 MAP::~MAP()
14 {
15 
16 }
17 
18 
19 
Init()20 int MAP::Init()
21 {
22    int status;
23    int index;
24 
25    // init stuff ahead of time
26    for (index = 0; index < MAX_THERMALS; index++)
27        thermals[index] = 0;
28 
29    status = Create_Thermal_Layers(MAX_THERMALS);
30    return status;
31 }
32 
33 
34 
35 // This function creates thermal layers spaced (more or less)
36 // eveningly. The function returns the number of thermals created.
Create_Thermal_Layers(int how_many)37 int MAP::Create_Thermal_Layers(int how_many)
38 {
39    int base_range;
40    int current_range;
41    int index;
42 
43    base_range = MAX_THERMAL_DEPTH / how_many;
44    current_range = 0;
45    index = 0;
46    while ( (index < MAX_THERMALS) && (index < how_many) )
47    {
48        current_range += base_range;
49        thermals[index] = current_range + ( rand() % 150 ) - 75;
50        index++;
51    }
52 
53    return index;
54 }
55 
56 
57 // At this point the ocean depth is the same everywhere, so
58 // we ignore the passed coordinaes and just return MAX_DEPTH
Get_Ocean_Depth(int x,int y)59 int MAP::Get_Ocean_Depth(int x, int y)
60 {
61    return MAX_DEPTH;
62 }
63 
64 
65 
66 // This function returns the number of thermal layers
67 // between depth1 and depth2
68 // If no thermals exist then the function returns zero.
Thermals_Between(int depth_1,int depth_2)69 int MAP::Thermals_Between(int depth_1, int depth_2)
70 {
71    int start_point, end_point;
72    int index, thermal_count;
73 
74    // if depths are the same, just return
75    if (depth_1 == depth_2)
76        return 0;
77 
78    // we need to make sure start_point is the higher depth
79    if (depth_1 <= depth_2)
80    {
81       start_point = depth_1;
82       end_point = depth_2;
83    }
84    else
85    {
86         start_point = depth_2;
87         end_point = depth_1;
88    }
89 
90    // move through each thermal layer and see if it lies between
91    // the two depths.
92    index = thermal_count = 0;
93    while (index < MAX_THERMALS)
94    {
95       if ( (start_point < thermals[index]) && (thermals[index] < end_point) )
96           thermal_count++;
97       index++;
98    }
99 
100    return thermal_count;
101 }
102 
103 
104 /*
105 This function finds the next themral above the current depth. If
106 no thermal lies above this position then we return "from_depth".
107 */
Next_Up(int from_depth)108 int MAP::Next_Up(int from_depth)
109 {
110    int index = MAX_THERMALS - 1;
111    int found = FALSE;
112 
113    while ( (!found) && (index >= 0) )
114    {
115       if ( (thermals[index] > 0) && (thermals[index] < from_depth) )
116          found = TRUE;
117       else
118          index--;
119    }
120    if (found)
121       return thermals[index];
122    else
123       return from_depth;
124 }
125 
126 
127 /*
128 This function finds the next themral below the current depth. If
129 no thermal lies below this position then we return "from_depth".
130 */
Next_Down(int from_depth)131 int MAP::Next_Down(int from_depth)
132 {
133    int index = 0;
134    int found = FALSE;
135 
136    while ( (!found) && (index < MAX_THERMALS) )
137    {
138         if ( (thermals[index] > 0) && (thermals[index] > from_depth) )
139             found = TRUE;
140         else
141            index++;
142    }
143    if (found)
144        return thermals[index];
145    else
146       return from_depth;
147 }
148 
149 
150 
151 
152 
153 
154 
155 #ifdef DEBUGMAP
Test_Map()156 void MAP::Test_Map()
157 {
158    int index;
159 
160    for (index = 0; index < MAX_THERMALS; index++)
161      printf("Map termal %d: %d\n", index, thermals[index]);
162    index = Thermals_Between(MIN_DEPTH, MAX_DEPTH);
163    printf("Thermals between %d and %d: %d\n", MIN_DEPTH, MAX_DEPTH, index);
164    index = Thermals_Between(MAX_DEPTH, MIN_DEPTH);
165    printf("Thermals between %d and %d: %d\n", MAX_DEPTH, MIN_DEPTH, index);
166 }
167 
168 #endif
169