Debian Codegraph @

src/esm.cpp

raw symbols SCIP
1 /***************************************************************************
2 esm.cpp - description
3 -------------------
4 begin : Wed August 15 2001
5 copyright : (C) 2001 by Rick McDaniel
6 email : [email protected]
7 $Id: esm.cpp,v 1.5 2003/04/14 05:51:03 mbridak Exp $
8 ***************************************************************************/
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License. *
15 * . *
16 * *
17 ***************************************************************************/
18 #include <string>
19 #include "SDL/SDL.h"
20 #include "SDL/SDL_image.h"
21 #include "SDL/SDL_thread.h"
22 #include "dfont.h"
23 #include "files.h"
24 #include "submarine.h"
25
26 #include "esm.h"
27 #include "dstack.h"
28 #include <cmath>
29 #include <cstdlib>
30
31 // rdm windows
32 #include <iostream>
33 using namespace std;
34
35 DStack EsmStack;
36
37
38 Esm::Esm(Submarine *temp):Subs(temp)
39 {
40 // Default values: mast down, power off
41 MastHeight = 68; // Max height of esm mast
42 Mast = false;
43 }
44 Esm::~Esm(){
45 }
46
47 void Esm::InitGraphics(SDL_Surface *temp, SDL_Surface *tempesmscreen)
48 {
49 screen=temp;
50 esmscreen=tempesmscreen;
51
52 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, 420, 490, 16,
53 screen->format->Rmask,
54 screen->format->Gmask,
55 screen->format->Bmask,
56 screen->format->Amask);
57 tempscreen=SDL_DisplayFormat(temp);
58 SDL_FreeSurface(temp);
59
60 LoadWidgets();
61 DisplayWidgets();
62 orange = SDL_MapRGB(screen->format, 238, 118, 0);
63 black = SDL_MapRGB(screen->format, 0, 0, 0);
64 }
65
66 void Esm::LoadWidgets(){
67
68 SDL_Surface *temp;
69
70 temp = Load_Image("images/mastdownoff.png");
71 if(temp != NULL) mastdownoff = SDL_DisplayFormat(temp);
72 if ( mastdownoff == NULL ) {
73 cerr<<"Function LoadWidgets()" << endl
74 << SDL_GetError() << endl;
75 SDL_Quit();
76 exit(0);
77 }
78 SDL_FreeSurface(temp);
79
80
81 temp = Load_Image("images/mastdownon.png");
82 if(temp != NULL) mastdownon = SDL_DisplayFormat(temp);
83 if ( mastdownon == NULL ) {
84 cerr<<"Function LoadWidgets()" << endl
85 << SDL_GetError() << endl;
86 SDL_Quit();
87 exit(0);
88 }
89 SDL_FreeSurface(temp);
90
91 temp = Load_Image("images/mastupoff.png");
92 if(temp != NULL) mastupoff = SDL_DisplayFormat(temp);
93 if ( mastupoff== NULL ) {
94 cerr<<"Function LoadWidgets()" << endl
95 << SDL_GetError() << endl;
96 SDL_Quit();
97 exit(0);
98 }
99 SDL_FreeSurface(temp);
100
101
102 temp = Load_Image("images/mastupon.png");
103 if(temp != NULL) mastupon = SDL_DisplayFormat(temp);
104 if ( mastupon == NULL ) {
105 cerr<<"Function LoadWidgets()" << endl
106 << SDL_GetError() << endl;
107 SDL_Quit();
108 exit(0);
109 }
110 SDL_FreeSurface(temp);
111
112 temp = Load_Image("images/styllus.png");
113 if(temp != NULL) styllus = SDL_DisplayFormat(temp);
114 if ( styllus == NULL ) {
115 cerr<<"Function LoadWidgets()" << endl
116 << SDL_GetError() << endl;
117 SDL_Quit();
118 exit(0);
119 }
120 SDL_FreeSurface(temp);
121
122 }
123
124 void Esm::DisplayWidgets(){
125
126 // Center of ESM screen at (316,325)
127 // dest.x = 316;
128 // dest.y = 325;
129 // dest.h = styllus->h;
130 // dest.w = styllus->w;
131 // SDL_BlitSurface(styllus, NULL, screen, &dest);
132 // SDL_UpdateRects(screen, 1, &dest);
133
134 if(Mast)
135 {
136 dest.x = 845;
137 dest.y = 187;
138 dest.h = mastupon->h;
139 dest.w = mastupon->w;
140 SDL_BlitSurface(mastupon, NULL, screen, &dest);
141 SDL_UpdateRects(screen, 1, &dest);
142
143 dest.x = 740;
144 dest.y = 187;
145 dest.h = mastdownoff->h;
146 dest.w = mastdownoff->w;
147 SDL_BlitSurface(mastdownoff, NULL, screen, &dest);
148 SDL_UpdateRects(screen, 1, &dest);
149 }
150 else
151 {
152 dest.x = 845;
153 dest.y = 187;
154 dest.h = mastupoff->h;
155 dest.w = mastupoff->w;
156 SDL_BlitSurface(mastupoff, NULL, screen, &dest);
157 SDL_UpdateRects(screen, 1, &dest);
158
159 dest.x = 740;
160 dest.y = 187;
161 dest.h = mastdownon->h;
162 dest.w = mastdownon->w;
163 SDL_BlitSurface(mastdownon, NULL, screen, &dest);
164 SDL_UpdateRects(screen, 1, &dest);
165 }
166
167 // Need this for the time compression??
168 // SDL_UpdateRect(screen,0, 0, 0, 0);
169
170 }
171
172 void Esm::ClearScreen(){
173 // Clear the screen
174 SDL_Surface *temp;
175 temp = Load_Image("images/ClearEsm.png");
176 if(temp != NULL) ClearEsm = SDL_DisplayFormat(temp);
177 if ( ClearEsm == NULL ) {
178 cerr<<"Function LoadWidgets()" << endl
179 << SDL_GetError() << endl;
180 SDL_Quit();
181 exit(0);
182 }
183
184 SDL_FreeSurface(temp);
185
186 temp = Load_Image("images/ClearEsm2.png");
187 if(temp != NULL) ClearEsm2 = SDL_DisplayFormat(temp);
188 if ( ClearEsm2 == NULL ) {
189 cerr<<"Function LoadWidgets()" << endl
190 << SDL_GetError() << endl;
191 SDL_Quit();
192 exit(0);
193 }
194
195 SDL_FreeSurface(temp);
196
197 src.x = 0;
198 src.y = 0;
199 src.w = ClearEsm->w;
200 src.h = ClearEsm->h;
201
202 dest.x = 105; // Blit destination x&y to the upper left
203 dest.y = 169;
204 dest.w = ClearEsm->w; // Height and width equal to the
205 dest.h = ClearEsm->h; // source images....
206 SDL_BlitSurface(ClearEsm, NULL, screen, &dest); // Do the actual Blit
207 SDL_UpdateRects(screen, 1, &dest); //Show the screen...
208 SDL_FreeSurface(ClearEsm); //Free up the surface memory.
209
210 src.x = 0;
211 src.y = 0;
212 src.w = ClearEsm2->w;
213 src.h = ClearEsm2->h;
214
215 dest.x = 693; // Blit destination x&y to the upper left
216 dest.y = 308;
217 dest.w = ClearEsm2->w; // Height and width equal to the
218 dest.h = ClearEsm2->h; // source images....
219 SDL_BlitSurface(ClearEsm2, NULL, screen, &dest); // Do the actual Blit
220 SDL_UpdateRects(screen, 1, &dest); //Show the screen...
221 SDL_FreeSurface(ClearEsm2); //Free up the surface memory.
222
223 }
224
225 void Esm::LowerMast()
226 {
227 Mast = false;
228 }
229
230 void Esm::RaiseMast()
231 {
232 Mast = true;
233 }
234
235 void Esm::ToggleMast()
236 {
237 Mast = !Mast;
238 }
239
240 bool Esm::getMastStatus()const
241 {
242 return Mast;
243 }
244
245 int Esm::getAntennaHeight(int Depth,int SeaState) const
246 {
247 // Effective Height of esm mast above water.
248 switch (SeaState)
249 {
250 case 0: // case 0 and case 1 are same
251 case 1: return(MastHeight - Depth);
252 case 2: return(MastHeight - Depth - 1);
253 case 3: return(MastHeight - Depth - 3);
254 case 4: return(MastHeight - Depth - 5);
255 case 5: return(MastHeight - Depth - 8);
256 default: return(MastHeight - Depth - 67);
257 }
258 }
259 bool Esm::isTargetVisible(Submarine *target, int TargetRange, int ObserverDepth,
260 int TargetHeight, bool TargetRadar, int SeaState)
261 {
262 // Determines if observer's esm can detect target radar.
263 // Returns a 1 if true or a 0 if false.
264
265 // TargetRange is the true range to the target.
266
267 // TargetHeight is the height of the tallest mast on a surface ship
268 // (usually 50 ft - 175 ft), height of conning tower for a submarine
269 // (usually 40-50 ft), or altitude for airplane.
270
271 /* To see Target, Range < EsmHorizon, Targets radar must be on.
272
273 Esm horizon is the earliest posssible target detection due to
274 the Earth-Curvature. Esm horizon = 3600(sqrt(ha)+ sqrt(ht))
275 ha = antenna height(ft), ht = target height(ft), EsmHorizon(yds)
276
277 Note: ESM horizon > Radar Horizon since can detect targets radar
278 long before he can detect esm mast.
279 */
280
281 bool boolean = false;
282 // we cannot detect subs on esm
283 if (target->Depth > 0.0)
284 return false;
285
286 CurrentAntennaHeight = getAntennaHeight(ObserverDepth,SeaState);
287 if(CurrentAntennaHeight <= 0) return boolean; // Mast is below water!
288
289 if(TargetRadar == 1 && getMastStatus() == 1
290 && TargetRange <= getEsmHorizon(ObserverDepth, TargetHeight, SeaState))
291 boolean = true;
292 return boolean;
293 }
294 int Esm::getBearing(int TargetBearing, int ObserverDepth,
295 int TargetHeight, int SeaState)
296 {
297 // Returns the bearing to the target based on accuracy of
298 // esm mast.
299
300 // The greater the EsmHorizon, the greater the error.
301
302 EsmHorizon = getEsmHorizon(ObserverDepth, TargetHeight, SeaState);
303
304 if (EsmHorizon > 30000)
305 bearing = TargetBearing - 5 + rand()%10;
306 else if (EsmHorizon > 20000 && EsmHorizon < 30000)
307 bearing = TargetBearing - 3 + rand()%6;
308 else if (EsmHorizon > 15000 && EsmHorizon < 20000)
309 bearing = TargetBearing - 2 + rand()%4;
310 else
311 bearing = TargetBearing - 1 + rand()%2;
312
313 return bearing;
314 }
315 int Esm::getSignalStrength(Submarine *Target, int TargetRange, int ObserverDepth,
316 int TargetHeight, bool TargetRadar,int SeaState)
317 {
318 // Returns the signal strength of the target radar.
319 // The signal strength is determined by the Detection Probability.
320
321 // The detection probability is a function of Target Range, Antenna
322 // Height, and TargetHeight
323
324
325 if(isTargetVisible(Target, TargetRange, ObserverDepth, TargetHeight,
326 TargetRadar, SeaState) == 0)
327 {
328 SignalStrength = 0;
329 return SignalStrength;
330 }
331 else
332 {
333 DetectProb = TargetRange - 600*getAntennaHeight(ObserverDepth,SeaState)
334 - 20*TargetHeight;
335 if (DetectProb > 35000)
336 SignalStrength = 1;
337 else if (DetectProb > 25000 && DetectProb < 35000)
338 SignalStrength = 2;
339 else if (DetectProb > 18000 && DetectProb < 25000)
340 SignalStrength = 3;
341 else if (DetectProb > 14000 && DetectProb < 18000)
342 SignalStrength = 4;
343 else
344 SignalStrength = 5;
345 }
346 return SignalStrength;
347 }
348
349 int Esm::getEsmHorizon(int ObserverDepth, int TargetHeight, int SeaState)
350 {
351 CurrentAntennaHeight = getAntennaHeight(ObserverDepth,SeaState);
352 if(CurrentAntennaHeight <= 0) return 0; // Mast is below water!
353
354 EsmHorizon = 3600*(int)(sqrt(float(CurrentAntennaHeight)) +
355 sqrt(float(TargetHeight)));
356
357 return EsmHorizon;
358 }
359
360 void Esm::DisplayContacts(){
361 static char text[120];
362 char file1[] = "images/font.png";
363 char file2[] = "images/font.dat";
364 DFont fnt(file1, file2);
365 // static DFont largeFont(filename.c_str(), filename2.c_str());
366 float radians;
367 float radians_old;
368 int bearing, range, depth;
369 Submarine *target;
370 int count;
371
372 // Note: Center of radar screen at (x,y) = (323,330)
373 // set dx = 144 dy = 144
374
375 src.x=695;
376 src.y=417; //define a rectangle on the screen and make it black
377 src.h=50;
378 src.w=174;
379
380 /*
381 if (!EsmStack.empty()) // Is there is data on the stack?
382 {
383 // TODO: FIX THIS
384 for(count=1; count<8;count++)
385 {
386 radians_old[count] = EsmStack.pop();
387
388 // Clear Old Data from Heading Compass
389 x = int(326.0 + (460.0 - 326.0)*cos(1.57-radians_old[count]));
390 y = int(383.0 - (460.0 - 326.0)*sin(1.57-radians_old[count]));
391 DLine(screen, 326, 383, x, y, black);
392 }
393 }
394 */
395 while (! EsmStack.empty() )
396 {
397 radians_old = EsmStack.pop();
398 x = int(326.0 + (460.0 - 326.0)*cos(1.57-radians_old));
399 y = int(383.0 - (460.0 - 326.0)*sin(1.57-radians_old));
400 DLine(screen, 326, 383, x, y, black);
401 }
402
403 target = Subs->next;
404 count = 0;
405 while (target)
406 {
407 bearing = (int)Subs->BearingToTarget(target);
408 range = (int)Subs->DistanceToTarget(target);
409 depth = (int)Subs->Depth;
410
411 // bearing = getBearing((int)Subs[0].BearingToTarget(Subs[target]), depth, 100, 3);
412
413 radians = float(bearing) *(3.14/180.0); // degrees to radians
414
415 if(isTargetVisible(target, range, depth,100, true, 3))
416 {
417 count++;
418 // Plot a line at the correct bearing
419 x = int(326.0 + (460.0 - 326.0)*cos(1.57-radians));
420 y = int(383.0 - (460.0 - 326.0)*sin(1.57-radians));
421 DLine(screen, 326, 383, x, y, orange);
422
423 sprintf(text, "BEARING %i SS %i", bearing,
424 getSignalStrength(target, range,60,100,true,3));
425 fnt.PutString(screen, 698, 300 + 11*count, text);
426 // Push the data on the stack
427 EsmStack.push(radians);
428
429 }
430
431 target = target->next;
432 }
433 SDL_UpdateRect(screen,0, 0, 0, 0);
434 }
435
436 void Esm::DLine(SDL_Surface *screen, int X1, int Y1, int X2, int Y2, Uint32 Color)
437 {
438
439 int dx,dy,sdx,sdy,py,px,x,y;
440 dx = X2 - X1;
441 dy = Y2 - Y1;
442 if (dx < 0) sdx = -1;
443 else sdx = 1;
444 if (dy < 0) sdy = -1;
445 else sdy = 1;
446 dx = sdx * dx + 1;
447 dy = sdy * dy + 1;
448 x = 0;
449 y = 0;
450 px = X1;
451 py = Y1;
452 if (dx >= dy){
453 for (int x = 0; x < dx; x++){
454 DPixel(screen, px, py, Color);
455 y = y + dy;
456 if (y >= dx){
457 y = y - dx;
458 py = py + sdy;
459 }
460 px = px + sdx;
461 }
462 }else{
463 for (int y = 0; y < dy; y++){
464 DPixel(screen, px, py, Color);
465 x = x + dx;
466 if (x >= dy){
467 x = x - dy;
468 px = px + sdx;
469 }
470 py = py + sdy;
471 }
472 }
473 }
474
475 void Esm::DPixel(SDL_Surface *screen, int x, int y, Uint32 color)
476 {
477 //this only works for 16bpp screens
478 //are we outside the screen?????
479 //If we are bail out now before it's too late!
480
481 //cerr << ".";
482 if (x > 1023 || x < 0 || y > 759 || y < 0) {
483 return;
484 }
485
486 //place the pixel on the screen
487 Uint16 *pixel_location;
488 pixel_location = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
489 *pixel_location = color;
490 }