[go: up one dir, main page]

File: fltzview.C

package info (click to toggle)
sabre 0.2.3-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,876 kB
  • ctags: 5,106
  • sloc: cpp: 25,581; ansic: 7,439; sh: 2,063; makefile: 92
file content (417 lines) | stat: -rw-r--r-- 10,579 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
    SABRE Fighter Plane Simulator 
    Copyright (c) 1997 Dan Hammer
    Portions Donated By Antti Barck

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*************************************************
 *           SABRE Fighter Plane Simulator              *
 * Version: 0.1                                  *
 * File   : fltzview.C                           *
 * Date   : March, 1997                          *
 * Author : Dan Hammer                           *
 *************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <limits.h>
#include <values.h>
#include "defs.h"
#include "pc_keys.h"
#include "vga_13.h"
#include "vmath.h"
#include "port_3d.h"
#include "cpoly.h"
#include "obj_3d.h"
#include "copoly.h"
#include "flight.h"
#include "font8x8.h"
#include "weapons.h"
#include "fltzview.h"
#include "sim.h"

extern Font8x8 *the_font;
#define R_TOP 0
#define R_RIGHT 1
#define R_BACK 2
const float shadow_limit = 1500.0;

Flight_ZViewer::Flight_ZViewer(Z_Node_Manager *zv)
  :Z_Viewer(zv)
{
  flt = NULL;
  wil = NULL;
  hurt = 0;
  hitme = 0;
  cpk_flg = 0;
  ground_hits = ground_kills = 0;
  air_hits = air_kills = 0;
  bagged = 0;
}

Flight_ZViewer::~Flight_ZViewer()
{
  if (wil != NULL)
    delete wil;
}

void Flight_ZViewer::set_flight(Flight *f)
{
  flt = f;
  reference_port = &(flt->state.flight_port);
  max_damage = flt->specs->max_damage;
}

extern int rz_which_line;

REAL_TYPE Flight_ZViewer::draw_prep(Port_3D &port)
{
  REAL_TYPE result;
  reference_port = &(flt->state.flight_port);
  result = Z_Viewer::draw_prep(port);
  if (!cpk_flg && visible_flag && wil != NULL && wil->hasExterns())
    wil->draw_prep(&port,&(flt->state.flight_port));
  return (result);
}

void Flight_ZViewer::draw(Port_3D &port)
{
  float ht;
  reference_port = &(flt->state.flight_port);
  int dr = draw_shadow;
  int rz = rz_which_line;
  rz_which_line = 0;
  if (dr)
    {
      ht = reference_port->look_from.z - flt->state.ground_height;
      if (ht * world_scale <= shadow_limit * world_scale)
	draw_shadow = 1;
      else
	draw_shadow = 0;
    }
  if (cpk_flg)
    shapes[cpit].visible = 0;

  /* 
     If there's landing gear shapes, see if they
     should show
     */

  if (z_manager->n_shapes >= lgear3 + 1)
    {
      if (flt->controls.landing_gear)
	{
	  shapes[lgear1].visible = 1;
	  shapes[lgear2].visible = 1;
	  shapes[lgear3].visible = 1;
	}
      else
	{
	  shapes[lgear1].visible = 0;
	  shapes[lgear2].visible = 0;
	  shapes[lgear3].visible = 0;
	}
    }
  Z_Viewer::draw(port);
  if (!cpk_flg && wil != NULL && wil->hasExterns())
    wil->draw(&port);
  draw_shadow = dr;
  rz_which_line = rz;
}

/*****************************************************************
 * Return true if we detect a hit                                *
 *****************************************************************/
int Flight_ZViewer::ouch(const R_3DPoint &p1, const R_3DPoint &p2, int damage, Target *trg)
{

  if (hurt >= max_damage)
    return (0);
  if (damage < threshold_damage)
    return (0);
  int result = 0;
  if (reference_port)
    {
      R_3DPoint r1,r2;
      reference_port->world2port(p1,&r1);
      reference_port->world2port(p2,&r2);
      // It would be nice to simply check if the position of
      // the projectile is in bounds, but ...
      // We have to deal with the fact that during the time frame,
      // the projectile may have travelled a distance greater than
      // the length of our plane ... in other words, it can easily
      // 'skip over' us. So we check for the intersection between
      // the bounding_cube and the line between the start and end of
      // the projectile's movement. This however favors a hit, as we
      // are not taking into account our movement during this time.

      result = calc_hit(r1,r2,bcube);
      if (result)
	{
	  if ( (result = calc_hit(r1,r2,z_manager->shape_info[fuselage].bcube)) != 0 )
	    hit_shape = fuselage;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[tail].bcube)) != 0 )
	    hit_shape = tail;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[left_wing].bcube)) != 0)
	    hit_shape = left_wing;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[right_wing].bcube)) != 0 )
	    hit_shape = right_wing;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[left_hstab].bcube)) != 0 )
	    hit_shape = left_hstab;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[right_hstab].bcube)) != 0)
	    hit_shape = right_hstab;
	  else if ((result = calc_hit(r1,r2,z_manager->shape_info[cpit].bcube)) != 0)
	    hit_shape = cpit;
	  else if (hit_scaler > 1.0)
	    {
	      result = 1;
	      hit_shape = RANDOM(right_wing+1);
	    }
	  if (result)
	    {
	      calc_damage(hit_shape,damage);
	      who_got_me = trg;
	      hitme = 1;
	    }
	}
    }

  if (hurt >= max_damage)
    {
      bagged = 1;
      who_got_me = trg;
    }
  return result;
}

int Flight_ZViewer::ouch(const R_3DPoint &w0, float radius, int damage, Target *tg)
{
  if (isHistory() || damage < threshold_damage)
    return (0);
  R_3DPoint p0;
  int dmg;
  int result = 0;
  reference_port->world2port(w0,&p0);
  if (calc_radial_damage(p0, radius, damage, bcube, dmg))
    {
      if (dmg > threshold_damage)
	{
	  result = 1;
	  hit_shape = 0;
	  hurt += damage;
	  calc_damage(hit_shape,dmg);
	  hitme = 1;
	  who_got_me = tg;
	  if (isHistory())
	    bagged = 1;
	}
    }
  return (result);
}

void Flight_ZViewer::youHit(Target *tg)
{
  switch (tg->getType())
    {
    case TARGET_BASE_T:
    case C_3DOBJECT_T:
    case GROUND_UNIT_T:
      ground_hits++;
      if (tg->isHistory())
	ground_kills++;
      break;

    case FLIGHT_ZVIEW_T:
      air_hits++;
      if (tg->isHistory())
	air_kills++;
      break;
    }
}

// See if the flight is in landing parameters
// if it is on the ground. Return true if plane has
// 'landed' ... for now, that means on the ground
int Flight_ZViewer::landing_report(Landing_Report &lr)
{
  int result = 0;
  if (flt->state.landing_recorded)
    {
      if (lr.landing_attempted)
	{
	  /* If we've already recorded a landing,
	   * but we're airborne again, reset
	   * some flags so we can record one again
	   */
	  if (!flt->state.on_ground)
	    {
	      flt->state.landing_recorded = 0;
	      lr.landing_attempted = 0;
	    }
	  return 0;
	}
      lr.landing_attempted = 1;
      result = 1;
      // Ooops -- forgot landing gear!
      if (!flt->controls.landing_gear)
	{
	  lr.gear_up = 1;
	  lr.score -= (int) (50 + 10 * flt->state.landing_velocity);
	}
      // Did we dig a hole?
      if (flt->state.landing_z <= -flt->specs->l_z)
	{
	  lr.l_z = 1;
	  lr.l_z_value = flt->state.landing_z;
	  lr.score -= (int) (10 * (fabs((flt->state.landing_z - -flt->specs->l_z)) / 10.0));
	}
      // Too fast?
      if (flt->state.landing_velocity > flt->specs->lspeed)
	{
	  lr.v = 1;
	  lr.v_value = flt->state.landing_velocity;
	  lr.score -= (int) ((flt->state.landing_velocity - flt->specs->lspeed) / 10);
	}

      // bad pitch?
      if (flt->state.landing_pitch < _PI2 - 0.8 ||
	  flt->state.landing_pitch > _PI2 + 0.8)
	{
	  lr.phi = 1;
	  lr.phi_value = flt->state.landing_pitch;
	  lr.score -= (int) (fabs(_PI2 - flt->state.landing_pitch) * 20);
	}

      // bad roll?
      if (flt->state.landing_roll > 0.4 &&
	  flt->state.landing_roll < _2PI - 0.4)
	{
	  lr.roll = 1;
	  lr.roll_value = flt->state.landing_roll;
	  lr.score -= (int) (fabs(_PI - flt->state.landing_roll) * 20);
	}

      if (lr.score < 0)
	{
	  calc_damage(fuselage,-lr.score);
	  if (hurt > max_damage)
	    flt->state.crashed = 1;
	}
    }
  return result;
}

void Flight_ZViewer::calc_damage(int hit_shape, int damage)
{
  int choice;
  flt->mods.battle_damage += damage;
  hurt = flt->mods.battle_damage;

  if (flt->mods.battle_damage >= max_damage)
    {
      flt->mods.spin_out = frand(63.0) + 15.0;
      if (RANDOM(2)) 
	flt->mods.spin_out *= -1.0;

      flt->mods.yaw_out = frand(5.0) + 2.0;
      if (RANDOM(2)) 
	flt->mods.yaw_out *= -1.0;

      flt->mods.pitch_out = frand(6.0) + 3.0;
      if (RANDOM(2)) 
	flt->mods.pitch_out *= -1.0;
     
      flt->mods.engine_e = 0;
      flt->mods.elevators_e = 0;
      flt->mods.ailerons_e = 0;
      flt->mods.speed_brakes_e = 0;
      flt->mods.rudder_e = 0;
    }
  else if (flt->mods.battle_damage >= max_damage / 4)
    {
      flt->mods.engine_e = (max_damage - flt->mods.battle_damage) / 100.0;
      float dmg = ((float)damage) / ((float)max_damage);
      switch (hit_shape)
	{
	case fuselage:
	case tail:
	case left_hstab:
	case right_hstab:
	case cpit:
	  choice = RANDOM(4);
	  switch (choice)
	    {
	    case 0:
	      flt->mods.elevators_e -= dmg;
	      break;
	    case 1:
	      flt->mods.h_stab_e -= dmg;
	      break;
	    case 2:
	      flt->mods.speed_brakes_e -= dmg;
	      break;
	    case 3:
	      flt->mods.v_stab_e -= dmg;
	      break;
	    }
	  break;

	case left_wing:
	case right_wing:
	  if (flt->mods.spin_out == 0.0 && flt->mods.battle_damage >= max_damage * 0.75)
	    {
	      flt->mods.spin_out = frand(15.0) + 4.0;
	      if (hit_shape == right_wing)
		flt->mods.spin_out = -flt->mods.spin_out;
	    }
	  if (RANDOM(2))
	    {
	      flt->mods.ailerons_e -= dmg;
	      if (flt->mods.ailerons_e < 0.1)
		flt->mods.ailerons_e = 0.1;
	    }
	  else
	    {
	      flt->mods.wing_e -= dmg;
	    }
	}
    }
}


R_3DPoint *Flight_ZViewer::get_hit_point()
{
  C_ShapeInfo &si = z_manager->shape_info[hit_shape];
  C_PolyInfo &pi = si.polyinfos[RANDOM(si.npolys)];
  R_3DPoint &hp = pi.lpoints[RANDOM(pi.npoints)];
  reference_port->port2world(hp,&hitpoint);
  return (&hitpoint);
}

R_3DPoint *Flight_ZViewer::get_position()
{
  return (&flt->state.flight_port.look_from);
}

Vector_Q *Flight_ZViewer::get_velocity()
{
  if (flt)
    return (& flt->state.velocity);
  else
    return NULL;
}