Debian Codegraph @

src/destruct.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 /* File notes:
21 * Two players duke it out in a Scorched Earth style game.
22 * Most of the variables referring to the players are global as
23 * they are often edited and that's how the original was written.
24 *
25 * Currently this file is at its final stage for vanilla destruct.
26 * Almost all of the left/right code duplications is gone. Most of the
27 * functions have been examined and tightened up, none of the enums
28 * start with '1', and the various large functions have been divided into
29 * smaller chunks.
30 *
31 * Destruct also supports some 'hidden' configuration that's just too awesome
32 * to not have available. Destruct has no configuration options in game, but
33 * that doesn't stop us from changing various limiting vars and letting
34 * people remap the keyboard. AIs may also be introduced here; fighting a
35 * stateless AI isn't really challenging after all.
36 *
37 * This hidden config also allows for a hidden game mode! Though as a custom
38 * game mode wouldn't show up in the data files it forces us to distinguish
39 * between the constant DESTRUCT_MODES (5) and MAX_MODES (6). DESTRUCT_MODES
40 * is only used with loaded data.
41 *
42 * Things I wanted to do but can't: Remove references to VGAScreen. For
43 * a multitude of reasons this just isn't feasible. It would have been nice
44 * to increase the playing field though...
45 */
46
47 /*** Headers ***/
48 #include "destruct.h"
49
50 #include "config.h"
51 #include "config_file.h"
52 #include "fonthand.h"
53 #include "helptext.h"
54 #include "keyboard.h"
55 #include "loudness.h"
56 #include "mtrand.h"
57 #include "nortsong.h"
58 #include "opentyr.h"
59 #include "palette.h"
60 #include "picload.h"
61 #include "sprite.h"
62 #include "varz.h"
63 #include "vga256d.h"
64 #include "video.h"
65
66 #include <assert.h>
67
68 /*** Defines ***/
69 #define UNIT_HEIGHT 12
70 #define MAX_KEY_OPTIONS 4
71
72 /*** Enums ***/
73 enum de_state_t
74 {
75 STATE_INIT,
76 STATE_RELOAD,
77 STATE_CONTINUE
78 };
79
80 enum de_player_t
81 {
82 PLAYER_LEFT = 0,
83 PLAYER_RIGHT = 1,
84 MAX_PLAYERS = 2
85 };
86
87 enum de_team_t
88 {
89 TEAM_LEFT = 0,
90 TEAM_RIGHT = 1,
91 MAX_TEAMS = 2
92 };
93
94 enum de_mode_t
95 {
96 MODE_5CARDWAR = 0,
97 MODE_TRADITIONAL,
98 MODE_HELIASSAULT,
99 MODE_HELIDEFENSE,
100 MODE_OUTGUNNED,
101 MODE_CUSTOM,
102 MODE_FIRST = MODE_5CARDWAR,
103 MODE_LAST = MODE_CUSTOM,
104 MAX_MODES = 6,
105 MODE_NONE = -1
106 };
107
108 enum de_unit_t
109 {
110 UNIT_TANK = 0,
111 UNIT_NUKE,
112 UNIT_DIRT,
113 UNIT_SATELLITE,
114 UNIT_MAGNET,
115 UNIT_LASER,
116 UNIT_JUMPER,
117 UNIT_HELI,
118 UNIT_FIRST = UNIT_TANK,
119 UNIT_LAST = UNIT_HELI,
120 MAX_UNITS = 8,
121 UNIT_NONE = -1
122 };
123
124 enum de_shot_t
125 {
126 SHOT_TRACER = 0,
127 SHOT_SMALL,
128 SHOT_LARGE,
129 SHOT_MICRO,
130 SHOT_SUPER,
131 SHOT_DEMO,
132 SHOT_SMALLNUKE,
133 SHOT_LARGENUKE,
134 SHOT_SMALLDIRT,
135 SHOT_LARGEDIRT,
136 SHOT_MAGNET,
137 SHOT_MINILASER,
138 SHOT_MEGALASER,
139 SHOT_LASERTRACER,
140 SHOT_MEGABLAST,
141 SHOT_MINI,
142 SHOT_BOMB,
143 SHOT_FIRST = SHOT_TRACER,
144 SHOT_LAST = SHOT_BOMB,
145 MAX_SHOT_TYPES = 17,
146 SHOT_INVALID = -1
147 };
148
149 enum de_expl_t
150 {
151 EXPL_NONE,
152 EXPL_MAGNET,
153 EXPL_DIRT,
154 EXPL_NORMAL
155 }; /* this needs a better name */
156
157 enum de_trails_t
158 {
159 TRAILS_NONE,
160 TRAILS_NORMAL,
161 TRAILS_FULL
162 };
163
164 enum de_pixel_t
165 {
166 PIXEL_BLACK = 0,
167 PIXEL_DIRT = 25
168 };
169
170 enum de_mapflags_t
171 {
172 MAP_NORMAL = 0x00,
173 MAP_WALLS = 0x01,
174 MAP_RINGS = 0x02,
175 MAP_HOLES = 0x04,
176 MAP_FUZZY = 0x08,
177 MAP_TALL = 0x10
178 };
179
180 /* keys and moves should line up. */
181 enum de_keys_t
182 {
183 KEY_LEFT = 0,
184 KEY_RIGHT,
185 KEY_UP,
186 KEY_DOWN,
187 KEY_CHANGE,
188 KEY_FIRE,
189 KEY_CYUP,
190 KEY_CYDN,
191 MAX_KEY = 8
192 };
193
194 enum de_move_t
195 {
196 MOVE_LEFT = 0,
197 MOVE_RIGHT,
198 MOVE_UP,
199 MOVE_DOWN,
200 MOVE_CHANGE,
201 MOVE_FIRE,
202 MOVE_CYUP,
203 MOVE_CYDN,
204 MAX_MOVE = 8
205 };
206
207 /* The tracerlaser is dummied out. It works but (probably due to the low
208 * MAX_SHOTS) is not assigned to anything. The bomb does not work.
209 */
210
211 /*** Structs ***/
212 struct destruct_config_s
213 {
214 unsigned int max_shots;
215 unsigned int min_walls;
216 unsigned int max_walls;
217 unsigned int max_explosions;
218 unsigned int max_installations;
219 bool allow_custom;
220 bool alwaysalias;
221 bool jumper_straight[2];
222 bool ai[2];
223 };
224
225 struct destruct_unit_s
226 {
227 /* Positioning/movement */
228 unsigned int unitX; /* yep, one's an int and the other is a real */
229 float unitY;
230 float unitYMov;
231 bool isYInAir;
232
233 /* What it is and what it fires */
234 enum de_unit_t unitType;
235 enum de_shot_t shotType;
236
237 /* What it's pointed */
238 float angle;
239 float power;
240
241 /* Misc */
242 int lastMove;
243 unsigned int ani_frame;
244 int health;
245 };
246
247 struct destruct_shot_s
248 {
249 bool isAvailable;
250
251 float x;
252 float y;
253 float xmov;
254 float ymov;
255 bool gravity;
256 unsigned int shottype;
257 //int shotdur; /* This looks to be unused */
258 unsigned int trailx[4], traily[4], trailc[4];
259 };
260
261 struct destruct_explo_s
262 {
263 bool isAvailable;
264
265 unsigned int x, y;
266 unsigned int explowidth;
267 unsigned int explomax;
268 unsigned int explofill;
269 enum de_expl_t exploType;
270 };
271
272 struct destruct_moves_s
273 {
274 bool actions[MAX_MOVE];
275 };
276
277 struct destruct_keys_s
278 {
279 SDL_Scancode Config[MAX_KEY][MAX_KEY_OPTIONS];
280 };
281
282 struct destruct_ai_s
283 {
284 int c_Angle, c_Power, c_Fire;
285 unsigned int c_noDown;
286 };
287
288 struct destruct_player_s
289 {
290 bool is_cpu;
291 struct destruct_ai_s aiMemory;
292
293 struct destruct_unit_s * unit;
294 struct destruct_moves_s moves;
295 struct destruct_keys_s keys;
296
297 enum de_team_t team;
298 unsigned int unitsRemaining;
299 unsigned int unitSelected;
300 unsigned int shotDelay;
301 unsigned int score;
302 };
303
304 struct destruct_wall_s
305 {
306 bool wallExist;
307 unsigned int wallX, wallY;
308 };
309
310 struct destruct_world_s
311 {
312 /* Map data & screen pointer */
313 unsigned int baseMap[320];
314 SDL_Surface * VGAScreen;
315 struct destruct_wall_s * mapWalls;
316
317 /* Map configuration */
318 enum de_mode_t destructMode;
319 unsigned int mapFlags;
320 };
321
322 /*** Function decs ***/
323 //Prep functions
324 static void JE_destructMain(void);
325 static void JE_introScreen(void);
326 static enum de_mode_t JE_modeSelect(void);
327 static void JE_helpScreen(void);
328 static void JE_pauseScreen(void);
329
330 //level generating functions
331 static void JE_generateTerrain(void);
332 static void DE_generateBaseTerrain(unsigned int, unsigned int *);
333 static void DE_drawBaseTerrain(unsigned int *);
334 static void DE_generateUnits(unsigned int *);
335 static void DE_generateWalls(struct destruct_world_s *);
336 static void DE_generateRings(SDL_Surface *, Uint8);
337 static void DE_ResetLevel(void);
338 static unsigned int JE_placementPosition(unsigned int, unsigned int, unsigned int *);
339
340 //drawing functions
341 static void JE_aliasDirt(SDL_Surface *);
342 static void DE_RunTickDrawCrosshairs(void);
343 static void DE_RunTickDrawHUD(void);
344 static void DE_GravityDrawUnit(enum de_player_t, struct destruct_unit_s *);
345 static void DE_RunTickAnimate(void);
346 static void DE_RunTickDrawWalls(void);
347 static void DE_DrawTrails(struct destruct_shot_s *, unsigned int, unsigned int, unsigned int);
348 static void JE_tempScreenChecking(void);
349 static void JE_superPixel(unsigned int, unsigned int);
350 static void JE_pixCool(unsigned int, unsigned int, Uint8);
351
352 //player functions
353 static void DE_RunTickGetInput(void);
354 static void DE_ProcessInput(void);
355 static void DE_ResetPlayers(void);
356 static void DE_ResetAI(void);
357 static void DE_ResetActions(void);
358 static void DE_RunTickAI(void);
359
360 //unit functions
361 static void DE_RaiseAngle(struct destruct_unit_s *);
362 static void DE_LowerAngle(struct destruct_unit_s *);
363 static void DE_RaisePower(struct destruct_unit_s *);
364 static void DE_LowerPower(struct destruct_unit_s *);
365 static void DE_CycleWeaponUp(struct destruct_unit_s *);
366 static void DE_CycleWeaponDown(struct destruct_unit_s *);
367 static void DE_RunMagnet(enum de_player_t, struct destruct_unit_s *);
368 static void DE_GravityFlyUnit(struct destruct_unit_s *);
369 static void DE_GravityLowerUnit(struct destruct_unit_s *);
370 static void DE_DestroyUnit(enum de_player_t, struct destruct_unit_s *);
371 static void DE_ResetUnits(void);
372 static inline bool DE_isValidUnit(struct destruct_unit_s *);
373
374 //weapon functions
375 static void DE_ResetWeapons(void);
376 static void DE_RunTickShots(void);
377 static void DE_RunTickExplosions(void);
378 static void DE_TestExplosionCollision(unsigned int, unsigned int);
379 static void JE_makeExplosion(unsigned int, unsigned int, enum de_shot_t);
380 static void DE_MakeShot(enum de_player_t, const struct destruct_unit_s *, int);
381
382 //gameplay functions
383 static enum de_state_t DE_RunTick(void);
384 static void DE_RunTickCycleDeadUnits(void);
385 static void DE_RunTickGravity(void);
386 static bool DE_RunTickCheckEndgame(void);
387 static bool JE_stabilityCheck(unsigned int, unsigned int);
388
389 //sound
390 static void DE_RunTickPlaySounds(void);
391 static void JE_eSound(unsigned int);
392
393 /*** Weapon configurations ***/
394
395 /* Part of me wants to leave these as bytes to save space. */
396 static const bool demolish[MAX_SHOT_TYPES] = {false, false, false, false, false, true, true, true, false, false, false, false, true, false, true, false, true};
397 //static const int shotGr[MAX_SHOT_TYPES] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101};
398 static const int shotTrail[MAX_SHOT_TYPES] = {TRAILS_NONE, TRAILS_NONE, TRAILS_NONE, TRAILS_NORMAL, TRAILS_NORMAL, TRAILS_NORMAL, TRAILS_FULL, TRAILS_FULL, TRAILS_NONE, TRAILS_NONE, TRAILS_NONE, TRAILS_NORMAL, TRAILS_FULL, TRAILS_NORMAL, TRAILS_FULL, TRAILS_NORMAL, TRAILS_NONE};
399 //static const int shotFuse[MAX_SHOT_TYPES] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0};
400 static const int shotDelay[MAX_SHOT_TYPES] = {10, 30, 80, 20, 60, 100, 140, 200, 20, 60, 5, 15, 50, 5, 80, 16, 0};
401 static const int shotSound[MAX_SHOT_TYPES] = {S_SELECT, S_WEAPON_2, S_WEAPON_1, S_WEAPON_7, S_WEAPON_7, S_EXPLOSION_9, S_EXPLOSION_22, S_EXPLOSION_22, S_WEAPON_5, S_WEAPON_13, S_WEAPON_10, S_WEAPON_15, S_WEAPON_15, S_WEAPON_26, S_WEAPON_14, S_WEAPON_7, S_WEAPON_7};
402 static const int exploSize[MAX_SHOT_TYPES] = {4, 20, 30, 14, 22, 16, 40, 60, 10, 30, 0, 5, 10, 3, 15, 7, 0};
403 static const bool shotBounce[MAX_SHOT_TYPES] = {false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, false, true};
404 static const int exploDensity[MAX_SHOT_TYPES] = { 2, 5, 10, 15, 20, 15, 25, 30, 40, 80, 0, 30, 30, 4, 30, 5, 0};
405 static const int shotDirt[MAX_SHOT_TYPES] = {EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_DIRT, EXPL_DIRT, EXPL_MAGNET, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NORMAL, EXPL_NONE};
406 static const int shotColor[MAX_SHOT_TYPES] = {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 10, 10, 10, 10, 16, 0};
407
408 static const int defaultWeapon[MAX_UNITS] = {SHOT_SMALL, SHOT_MICRO, SHOT_SMALLDIRT, SHOT_INVALID, SHOT_MAGNET, SHOT_MINILASER, SHOT_MICRO, SHOT_MINI};
409 static const int defaultCpuWeapon[MAX_UNITS] = {SHOT_SMALL, SHOT_MICRO, SHOT_DEMO, SHOT_INVALID, SHOT_MAGNET, SHOT_MINILASER, SHOT_MICRO, SHOT_MINI};
410 static const int defaultCpuWeaponB[MAX_UNITS] = {SHOT_DEMO, SHOT_SMALLNUKE, SHOT_DEMO, SHOT_INVALID, SHOT_MAGNET, SHOT_MEGALASER, SHOT_MICRO, SHOT_MINI};
411 static const int systemAngle[MAX_UNITS] = {true, true, true, false, false, true, false, false};
412 static const int baseDamage[MAX_UNITS] = {200, 120, 400, 300, 80, 150, 600, 40};
413 static const int systemAni[MAX_UNITS] = {false, false, false, true, false, false, false, true};
414
415 static bool weaponSystems[MAX_UNITS][MAX_SHOT_TYPES] =
416 {
417 {1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // normal
418 {0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // nuke
419 {0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // dirt
420 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // worthless
421 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // magnet
422 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, // laser
423 {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // jumper
424 {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} // helicopter
425 };
426
427 /* More constant configuration settings. */
428 /* Music that destruct will play. You can check out musmast.c to see what is what. */
429 static const JE_byte goodsel[14] /*[1..14]*/ = {1, 2, 6, 12, 13, 14, 17, 23, 24, 26, 28, 29, 32, 33};
430
431 /* Unit creation. Need to move this later: Doesn't belong here */
432 static JE_byte basetypes[10][11] /*[1..8, 1..11]*/ = /* [0] is amount of units*/
433 {
434 {5, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_DIRT, UNIT_DIRT, UNIT_SATELLITE, UNIT_MAGNET, UNIT_LASER, UNIT_JUMPER, UNIT_HELI}, /*Normal*/
435 {1, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK}, /*Traditional*/
436 {4, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI}, /*Weak Heli attack fleet*/
437 {8, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_NUKE, UNIT_NUKE, UNIT_DIRT, UNIT_MAGNET, UNIT_LASER, UNIT_JUMPER}, /*Strong Heli defense fleet*/
438 {8, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI, UNIT_HELI}, /*Strong Heli attack fleet*/
439 {4, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_NUKE, UNIT_DIRT, UNIT_MAGNET, UNIT_JUMPER, UNIT_JUMPER}, /*Weak Heli defense fleet*/
440 {8, UNIT_TANK, UNIT_NUKE, UNIT_DIRT, UNIT_SATELLITE, UNIT_MAGNET, UNIT_LASER, UNIT_JUMPER, UNIT_HELI, UNIT_TANK, UNIT_NUKE}, /*Overpowering fleet*/
441 {4, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_DIRT, UNIT_TANK, UNIT_LASER, UNIT_JUMPER, UNIT_HELI, UNIT_NUKE, UNIT_JUMPER}, /*Weak fleet*/
442 {5, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_DIRT, UNIT_DIRT, UNIT_SATELLITE, UNIT_MAGNET, UNIT_LASER, UNIT_JUMPER, UNIT_HELI}, /*Left custom*/
443 {5, UNIT_TANK, UNIT_TANK, UNIT_NUKE, UNIT_DIRT, UNIT_DIRT, UNIT_SATELLITE, UNIT_MAGNET, UNIT_LASER, UNIT_JUMPER, UNIT_HELI}, /*Right custom*/
444 };
445 static const unsigned int baseLookup[MAX_PLAYERS][MAX_MODES] =
446 {
447 {0, 1, 3, 4, 6, 8},
448 {0, 1, 2, 5, 7, 9}
449 };
450
451 static const JE_byte GraphicBase[MAX_PLAYERS][MAX_UNITS] =
452 {
453 { 1, 6, 11, 58, 63, 68, 96, 153},
454 { 20, 25, 30, 77, 82, 87, 115, 172}
455 };
456
457 static const JE_byte ModeScore[MAX_PLAYERS][MAX_MODES] =
458 {
459 {1, 0, 0, 5, 0, 1},
460 {1, 0, 5, 0, 1, 1}
461 };
462
463 static SDL_Scancode defaultKeyConfig[MAX_PLAYERS][MAX_KEY][MAX_KEY_OPTIONS] =
464 {
465 {
466 {SDL_SCANCODE_C},
467 {SDL_SCANCODE_V},
468 {SDL_SCANCODE_A},
469 {SDL_SCANCODE_Z},
470 {SDL_SCANCODE_LALT},
471 {SDL_SCANCODE_X, SDL_SCANCODE_LSHIFT},
472 {SDL_SCANCODE_LCTRL},
473 {SDL_SCANCODE_SPACE}
474 },
475 {
476 {SDL_SCANCODE_LEFT, SDL_SCANCODE_KP_4},
477 {SDL_SCANCODE_RIGHT, SDL_SCANCODE_KP_6},
478 {SDL_SCANCODE_UP, SDL_SCANCODE_KP_8},
479 {SDL_SCANCODE_DOWN, SDL_SCANCODE_KP_2},
480 {SDL_SCANCODE_BACKSLASH, SDL_SCANCODE_KP_5},
481 {SDL_SCANCODE_INSERT, SDL_SCANCODE_RETURN, SDL_SCANCODE_KP_0, SDL_SCANCODE_KP_ENTER},
482 {SDL_SCANCODE_PAGEUP, SDL_SCANCODE_KP_9},
483 {SDL_SCANCODE_PAGEDOWN, SDL_SCANCODE_KP_3}
484 }
485 };
486
487 /*** Globals ***/
488 static SDL_Surface *destructTempScreen;
489 static JE_boolean destructFirstTime;
490
491 static struct destruct_config_s config = { 40, 20, 20, 40, 10, false, false, {true, false}, {true, false} };
492 static struct destruct_player_s destruct_player[MAX_PLAYERS];
493 static struct destruct_world_s world;
494 static struct destruct_shot_s * shotRec;
495 static struct destruct_explo_s * exploRec;
496
497 static const char *const player_names[] =
498 {
499 "left",
500 "right",
501 };
502
503 static const char *const key_names[] =
504 {
505 "left",
506 "right",
507 "up",
508 "down",
509 "change",
510 "fire",
511 "previous weapon",
512 "next weapon",
513 };
514
515 static const char *const unit_names[] =
516 {
517 "tank",
518 "nuke",
519 "dirt",
520 "satellite",
521 "magnet",
522 "laser",
523 "jumper",
524 "heli",
525 };
526
527 static enum de_unit_t get_unit_by_name(const char *unit_name)
528 {
529 for (enum de_unit_t unit = UNIT_FIRST; unit < MAX_UNITS; ++unit)
530 if (strcmp(unit_name, unit_names[unit]) == 0)
531 return unit;
532
533 return UNIT_NONE;
534 }
535
536 static void load_destruct_config(Config *config_)
537 {
538 ConfigSection *section;
539
540 section = config_find_or_add_section(config_, "destruct", NULL);
541 if (section == NULL)
542 exit(EXIT_FAILURE); // out of memory
543
544 config.alwaysalias = config_get_or_set_bool_option(section, "antialias craters", false, NO_YES);
545
546 weaponSystems[UNIT_LASER][SHOT_LASERTRACER] = config_get_or_set_bool_option(section, "tracer laser", false, OFF_ON);
547
548 config.max_shots = config_get_or_set_int_option(section, "max shots", 40);
549 config.max_explosions = config_get_or_set_int_option(section, "max explosions", 40);
550 config.min_walls = config_get_or_set_int_option(section, "min walls", 20);
551 config.max_walls = config_get_or_set_int_option(section, "max walls", 20);
552
553 config.ai[0] = config_get_or_set_bool_option(section, "left ai", true, NO_YES);
554 config.jumper_straight[0] = config_get_or_set_bool_option(section, "left jumper fires straight", true, NO_YES);
555 config.ai[1] = config_get_or_set_bool_option(section, "right ai", false, NO_YES);
556 config.jumper_straight[1] = config_get_or_set_bool_option(section, "right jumper fires straight", false, NO_YES);
557
558 // keyboard controls
559
560 for (int p = 0; p < MAX_PLAYERS; ++p)
561 {
562 section = config_find_section(config_, "destruct keyboard", player_names[p]);
563 if (section == NULL)
564 if ((section = config_add_section(config_, "destruct keyboard", player_names[p])) == NULL)
565 exit(-1);
566
567 ConfigOption *option;
568
569 for (int k = 0; k < MAX_KEY; ++k)
570 {
571 if ((option = config_get_or_set_option(section, key_names[k], NULL)) == NULL)
572 exit(-1);
573
574 foreach_option_i_value(i, value, option)
575 {
576 SDL_Scancode key = SDL_GetScancodeFromName(value);
577 if (key != SDL_SCANCODE_UNKNOWN && i < COUNTOF(defaultKeyConfig[p][k]))
578 {
579 defaultKeyConfig[p][k][i] = key;
580 }
581 else // invalid or excess
582 {
583 foreach_remove_option_value();
584 continue;
585 }
586 }
587
588 if (config_get_value_count(option) > 0)
589 {
590 // unset remaining defaults
591 for (unsigned int i = config_get_value_count(option); i < COUNTOF(defaultKeyConfig[p][k]); ++i)
592 defaultKeyConfig[p][k][i] = SDL_SCANCODE_UNKNOWN;
593 }
594 else
595 {
596 // set defaults
597 for (unsigned int i = 0; i < COUNTOF(defaultKeyConfig[p][k]); ++i)
598 if (defaultKeyConfig[p][k][i] != SDL_SCANCODE_UNKNOWN)
599 config_add_value(option, SDL_GetScancodeName(defaultKeyConfig[p][k][i]));
600 }
601 }
602 }
603
604 // custom destruct mode
605
606 section = config_find_section(config_, "destruct custom", NULL);
607 if (section == NULL)
608 if ((section = config_add_section(config_, "destruct custom", NULL)) == NULL)
609 exit(-1);
610
611 config.allow_custom = config_get_or_set_bool_option(section, "enable", false, NO_YES);
612
613 char buffer[15 + 1];
614
615 for (int p = 0; p < MAX_PLAYERS; ++p)
616 {
617 snprintf(buffer, sizeof(buffer), "%s num units", player_names[p]);
618 basetypes[8 + p][0] = config_get_or_set_int_option(section, buffer, basetypes[8 + p][0]);
619
620 ConfigOption *option;
621
622 snprintf(buffer, sizeof(buffer), "%s unit", player_names[p]);
623 if ((option = config_get_or_set_option(section, buffer, NULL)) == NULL)
624 exit(-1);
625
626 foreach_option_i_value(i, value, option)
627 {
628 enum de_unit_t unit = get_unit_by_name(value);
629 if (unit != UNIT_NONE && 1 + i < COUNTOF(basetypes[8 + p]))
630 {
631 basetypes[8 + p][1 + i] = unit;
632 }
633 else // invalid or excess
634 {
635 foreach_remove_option_value();
636 continue;
637 }
638 }
639
640 if (config_get_value_count(option) > 0)
641 {
642 // set remaining units to tank
643 for (unsigned int i = config_get_value_count(option); 1 + i < COUNTOF(basetypes[8 + p]); ++i)
644 {
645 basetypes[8 + p][1 + i] = UNIT_TANK;
646 config_add_value(option, unit_names[UNIT_TANK]);
647 }
648 }
649 else
650 {
651 // set defaults
652 for (unsigned int i = 0; 1 + i < COUNTOF(basetypes[8 + p]); ++i)
653 config_add_value(option, unit_names[basetypes[8 + p][1 + i]]);
654 }
655 }
656 }
657
658 /*** Startup ***/
659
660 void JE_destructGame(void)
661 {
662 unsigned int i;
663
664 /* This is the entry function. Any one-time actions we need to
665 * perform can go in here. */
666 JE_clr256(VGAScreen);
667 JE_showVGA();
668
669 load_destruct_config(&opentyrian_config);
670
671 //malloc things that have customizable sizes
672 shotRec = malloc(sizeof(struct destruct_shot_s) * config.max_shots);
673 exploRec = malloc(sizeof(struct destruct_explo_s) * config.max_explosions);
674 world.mapWalls = malloc(sizeof(struct destruct_wall_s) * config.max_walls);
675
676 //Malloc enough structures to cover all of this session's possible needs.
677 for (i = 0; i < 10; i++)
678 config.max_installations = MAX(config.max_installations, basetypes[i][0]);
679 destruct_player[PLAYER_LEFT ].unit = malloc(sizeof(struct destruct_unit_s) * config.max_installations);
680 destruct_player[PLAYER_RIGHT].unit = malloc(sizeof(struct destruct_unit_s) * config.max_installations);
681
682 destructTempScreen = game_screen;
683 world.VGAScreen = VGAScreen;
684
685 JE_loadCompShapes(&destructSpriteSheet, '~');
686
687 fade_black(1);
688
689 JE_destructMain();
690
691 free_sprite2s(&destructSpriteSheet);
692
693 //and of course exit actions go here.
694 free(shotRec);
695 free(exploRec);
696 free(world.mapWalls);
697 free(destruct_player[PLAYER_LEFT ].unit);
698 free(destruct_player[PLAYER_RIGHT].unit);
699 }
700
701 static void JE_destructMain(void)
702 {
703 enum de_state_t curState;
704
705 JE_loadPic(VGAScreen, 11, false);
706 JE_introScreen();
707
708 DE_ResetPlayers();
709
710 destruct_player[PLAYER_LEFT ].is_cpu = config.ai[PLAYER_LEFT];
711 destruct_player[PLAYER_RIGHT].is_cpu = config.ai[PLAYER_RIGHT];
712
713 while (true)
714 {
715 world.destructMode = JE_modeSelect();
716
717 if (world.destructMode == MODE_NONE)
718 break; /* User is quitting */
719
720 do
721 {
722
723 destructFirstTime = true;
724 JE_loadPic(VGAScreen, 11, false);
725
726 DE_ResetUnits();
727 DE_ResetLevel();
728 do
729 {
730 curState = DE_RunTick();
731 } while (curState == STATE_CONTINUE);
732
733 fade_black(25);
734 } while (curState == STATE_RELOAD);
735 }
736 }
737
738 static void JE_introScreen(void)
739 {
740 memcpy(VGAScreen2->pixels, VGAScreen->pixels, VGAScreen2->h * VGAScreen2->pitch);
741 JE_outText(VGAScreen, JE_fontCenter(specialName[7], TINY_FONT), 90, specialName[7], 12, 5);
742 JE_outText(VGAScreen, JE_fontCenter(miscText[64], TINY_FONT), 180, miscText[64], 15, 2);
743 JE_outText(VGAScreen, JE_fontCenter(miscText[65], TINY_FONT), 190, miscText[65], 15, 2);
744 JE_showVGA();
745 fade_palette(colors, 15, 0, 255);
746
747 newkey = false;
748 while (!newkey)
749 {
750 service_SDL_events(false);
751 SDL_Delay(16);
752 }
753
754 fade_black(15);
755 memcpy(VGAScreen->pixels, VGAScreen2->pixels, VGAScreen->h * VGAScreen->pitch);
756 JE_showVGA();
757 }
758
759 /* JE_modeSelect
760 *
761 * This function prints the DESTRUCT mode selection menu.
762 * The return value is the selected mode, or -1 (MODE_NONE)
763 * if the user quits.
764 */
765 static void DrawModeSelectMenu(enum de_mode_t mode)
766 {
767 int i;
768
769 /* Helper function of JE_modeSelect. Do not use elsewhere. */
770 for (i = 0; i < DESTRUCT_MODES; i++)
771 JE_textShade(VGAScreen, JE_fontCenter(destructModeName[i], TINY_FONT), 82 + i * 12, destructModeName[i], 12, (i == mode) * 4, FULL_SHADE);
772 if (config.allow_custom == true)
773 JE_textShade(VGAScreen, JE_fontCenter("Custom", TINY_FONT), 82 + i * 12, "Custom", 12, (i == mode) * 4, FULL_SHADE);
774 }
775
776 static enum de_mode_t JE_modeSelect(void)
777 {
778 enum de_mode_t mode;
779
780 memcpy(VGAScreen2->pixels, VGAScreen->pixels, VGAScreen2->h * VGAScreen2->pitch);
781 mode = MODE_5CARDWAR;
782
783 // Draw the menu and fade us in
784 DrawModeSelectMenu(mode);
785
786 JE_showVGA();
787 fade_palette(colors, 15, 0, 255);
788
789 /* Get input in a loop. */
790 while (true)
791 {
792 /* Re-draw the menu every iteration */
793 DrawModeSelectMenu(mode);
794 JE_showVGA();
795
796 /* Grab keys */
797 newkey = false;
798 do
799 {
800 service_SDL_events(false);
801 SDL_Delay(16);
802 } while (!newkey);
803
804 /* See what was pressed */
805 if (keysactive[SDL_SCANCODE_ESCAPE])
806 {
807 mode = MODE_NONE; /* User is quitting, return failure */
808 break;
809 }
810 if (keysactive[SDL_SCANCODE_RETURN])
811 {
812 break; /* User has selected, return choice */
813 }
814 if (keysactive[SDL_SCANCODE_UP])
815 {
816 if (mode == MODE_FIRST)
817 {
818 if (config.allow_custom == true)
819 mode = MODE_LAST;
820 else
821 mode = MODE_LAST-1;
822 }
823 else
824 {
825 mode--;
826 }
827 }
828 if (keysactive[SDL_SCANCODE_DOWN])
829 {
830 if (mode >= MODE_LAST-1)
831 {
832 if (config.allow_custom == true && mode == MODE_LAST-1)
833 mode++;
834 else
835 mode = MODE_FIRST;
836 }
837 else
838 {
839 mode++;
840 }
841 }
842 }
843
844 fade_black(15);
845 memcpy(VGAScreen->pixels, VGAScreen2->pixels, VGAScreen->h * VGAScreen->pitch);
846 JE_showVGA();
847 return mode;
848 }
849
850 static void JE_generateTerrain(void)
851 {
852 /* The unique modifiers:
853 Altered generation (really tall)
854 Fuzzy hills
855 Rings of dirt
856
857 The non-unique ones;:
858 Rings of not dirt (holes)
859 Walls
860 */
861
862 world.mapFlags = MAP_NORMAL;
863
864 if (mt_rand() % 2 == 0)
865 world.mapFlags |= MAP_WALLS;
866 if (mt_rand() % 4 == 0)
867 world.mapFlags |= MAP_HOLES;
868 switch (mt_rand() % 4)
869 {
870 case 0:
871 world.mapFlags |= MAP_FUZZY;
872 break;
873
874 case 1:
875 world.mapFlags |= MAP_TALL;
876 break;
877
878 case 2:
879 world.mapFlags |= MAP_RINGS;
880 break;
881 }
882
883 play_song(goodsel[mt_rand() % 14] - 1);
884
885 DE_generateBaseTerrain(world.mapFlags, world.baseMap);
886 DE_generateUnits(world.baseMap);
887 DE_generateWalls(&world);
888 DE_drawBaseTerrain(world.baseMap);
889
890 if (world.mapFlags & MAP_RINGS)
891 DE_generateRings(world.VGAScreen, PIXEL_DIRT);
892 if (world.mapFlags & MAP_HOLES)
893 DE_generateRings(world.VGAScreen, PIXEL_BLACK);
894
895 JE_aliasDirt(world.VGAScreen);
896 JE_showVGA();
897
898 memcpy(destructTempScreen->pixels, VGAScreen->pixels, destructTempScreen->pitch * destructTempScreen->h);
899 }
900
901 static void DE_generateBaseTerrain(unsigned int mapFlags, unsigned int * baseWorld)
902 {
903 unsigned int i;
904 unsigned int newheight, HeightMul;
905 float sinewave, sinewave2, cosinewave, cosinewave2;
906
907 /* The 'terrain' is actually the video buffer :). If it's brown, flu... er,
908 * brown pixels are what we check for collisions with. */
909
910 /* The ranges here are between .01 and roughly 0.07283...*/
911 sinewave = mt_rand_lt1() * M_PI / 50 + 0.01f;
912 sinewave2 = mt_rand_lt1() * M_PI / 50 + 0.01f;
913 cosinewave = mt_rand_lt1() * M_PI / 50 + 0.01f;
914 cosinewave2 = mt_rand_lt1() * M_PI / 50 + 0.01f;
915 HeightMul = 20;
916
917 /* This block just exists to mix things up. */
918 if (mapFlags & MAP_FUZZY)
919 {
920 sinewave = M_PI - mt_rand_lt1() * 0.3f;
921 sinewave2 = M_PI - mt_rand_lt1() * 0.3f;
922 }
923 if (mapFlags & MAP_TALL)
924 {
925 HeightMul = 100;
926 }
927
928 /* Now compute a height for each of our lines. */
929 for (i = 1; i <= 318; i++)
930 {
931 newheight = roundf(sinf(sinewave * i) * HeightMul + sinf(sinewave2 * i) * 15 +
932 cosf(cosinewave * i) * 10 + sinf(cosinewave2 * i) * 15) + 130;
933
934 /* Bind it; we have mins and maxs */
935 if (newheight < 40)
936 newheight = 40;
937 else if (newheight > 195)
938 newheight = 195;
939 baseWorld[i] = newheight;
940 }
941 /* The base world has been created. */
942 }
943
944 static void DE_drawBaseTerrain(unsigned int * baseWorld)
945 {
946 unsigned int i;
947
948 for (i = 1; i <= 318; i++)
949 {
950 JE_rectangle(VGAScreen, i, baseWorld[i], i, 199, PIXEL_DIRT);
951 }
952 }
953
954 static void DE_generateUnits(unsigned int * baseWorld)
955 {
956 unsigned int i, j, numSatellites;
957
958 for (i = 0; i < MAX_PLAYERS; i++)
959 {
960 numSatellites = 0;
961 destruct_player[i].unitsRemaining = 0;
962
963 for (j = 0; j < basetypes[baseLookup[i][world.destructMode]][0]; j++)
964 {
965 /* Not everything is the same between players */
966 if (i == PLAYER_LEFT)
967 {
968 destruct_player[i].unit[j].unitX = (mt_rand() % 120) + 10;
969 }
970 else
971 {
972 destruct_player[i].unit[j].unitX = 320 - ((mt_rand() % 120) + 22);
973 }
974
975 destruct_player[i].unit[j].unitY = JE_placementPosition(destruct_player[i].unit[j].unitX - 1, 14, baseWorld);
976 destruct_player[i].unit[j].unitType = basetypes[baseLookup[i][world.destructMode]][(mt_rand() % 10) + 1];
977
978 /* Sats are special cases since they are useless. They don't count
979 * as active units and we can't have a team of all sats */
980 if (destruct_player[i].unit[j].unitType == UNIT_SATELLITE)
981 {
982 if (numSatellites == basetypes[baseLookup[i][world.destructMode]][0])
983 {
984 destruct_player[i].unit[j].unitType = UNIT_TANK;
985 destruct_player[i].unitsRemaining++;
986 }
987 else
988 {
989 /* Place the satellite. Note: Earlier we cleared
990 * space with JE_placementPosition. Now we are randomly
991 * placing the sat's Y. It can be generated in hills
992 * and there is a clearing underneath it. This CAN
993 * be fixed but won't be for classic.
994 */
995 destruct_player[i].unit[j].unitY = 30 + (mt_rand() % 40);
996 numSatellites++;
997 }
998 }
999 else
1000 {
1001 destruct_player[i].unitsRemaining++;
1002 }
1003
1004 /* Now just fill in the rest of the unit's values. */
1005 destruct_player[i].unit[j].lastMove = 0;
1006 destruct_player[i].unit[j].unitYMov = 0;
1007 destruct_player[i].unit[j].isYInAir = false;
1008 destruct_player[i].unit[j].angle = 0;
1009 destruct_player[i].unit[j].power = (destruct_player[i].unit[j].unitType == UNIT_LASER) ? 6 : 3;
1010 destruct_player[i].unit[j].shotType = defaultWeapon[destruct_player[i].unit[j].unitType];
1011 destruct_player[i].unit[j].health = baseDamage[destruct_player[i].unit[j].unitType];
1012 destruct_player[i].unit[j].ani_frame = 0;
1013 }
1014 }
1015 }
1016
1017 static void DE_generateWalls(struct destruct_world_s * gameWorld)
1018 {
1019 unsigned int i, j, wallX;
1020 unsigned int wallHeight, remainWalls;
1021 unsigned int tries;
1022 bool isGood;
1023
1024 if ((world.mapFlags & MAP_WALLS) == false)
1025 {
1026 /* Just clear them out */
1027 for (i = 0; i < config.max_walls; i++)
1028 {
1029 gameWorld->mapWalls[i].wallExist = false;
1030 }
1031 return;
1032 }
1033
1034 remainWalls = (rand() % (config.max_walls - config.min_walls + 1)) + config.min_walls;
1035
1036 do
1037 {
1038 /* Create a wall. Decide how tall the wall will be */
1039 wallHeight = (mt_rand() % 5) + 1;
1040 if (wallHeight > remainWalls)
1041 {
1042 wallHeight = remainWalls;
1043 }
1044
1045 /* Now find a good place to put the wall. */
1046 tries = 0;
1047 do
1048 {
1049 isGood = true;
1050 wallX = (mt_rand() % 300) + 10;
1051
1052 /* Is this X already occupied? In the original Tyrian we only
1053 * checked to make sure four units on each side were unobscured.
1054 * That's not very scalable; instead I will check every unit,
1055 * but I'll only try plotting an unobstructed X four times.
1056 * After that we'll cover up what may; having a few units
1057 * stuck behind walls makes things mildly interesting.
1058 */
1059 for (i = 0; i < MAX_PLAYERS; i++)
1060 {
1061 for (j = 0; j < config.max_installations; j++)
1062 {
1063 if ((wallX > destruct_player[i].unit[j].unitX - 12) &&
1064 (wallX < destruct_player[i].unit[j].unitX + 13))
1065 {
1066 isGood = false;
1067 goto label_outer_break; /* I do feel that outer breaking is a legitimate goto use. */
1068 }
1069 }
1070 }
1071
1072 label_outer_break:
1073 tries++;
1074
1075 } while (isGood == false && tries < 5);
1076
1077 /* We now have a valid X. Create the wall. */
1078 for (i = 1; i <= wallHeight; i++)
1079 {
1080 gameWorld->mapWalls[remainWalls - i].wallExist = true;
1081 gameWorld->mapWalls[remainWalls - i].wallX = wallX;
1082 gameWorld->mapWalls[remainWalls - i].wallY = JE_placementPosition(wallX, 12, gameWorld->baseMap) - 14 * i;
1083 }
1084
1085 remainWalls -= wallHeight;
1086
1087 } while (remainWalls != 0);
1088 }
1089
1090 static void DE_generateRings(SDL_Surface * screen, Uint8 pixel)
1091 {
1092 unsigned int i, j, tempSize, rings;
1093 int tempPosX1, tempPosY1, tempPosX2, tempPosY2;
1094 float tempRadian;
1095
1096 rings = mt_rand() % 6 + 1;
1097 for (i = 1; i <= rings; i++)
1098 {
1099 tempPosX1 = (mt_rand() % 320);
1100 tempPosY1 = (mt_rand() % 160) + 20;
1101 tempSize = (mt_rand() % 40) + 10; /*Size*/
1102
1103 for (j = 1; j <= tempSize * tempSize * 2; j++)
1104 {
1105 tempRadian = mt_rand_lt1() * (2 * M_PI);
1106 tempPosY2 = tempPosY1 + roundf(cosf(tempRadian) * (mt_rand_lt1() * 0.1f + 0.9f) * tempSize);
1107 tempPosX2 = tempPosX1 + roundf(sinf(tempRadian) * (mt_rand_lt1() * 0.1f + 0.9f) * tempSize);
1108 if ((tempPosY2 > 12) && (tempPosY2 < 200) &&
1109 (tempPosX2 > 0) && (tempPosX2 < 319))
1110 {
1111 ((Uint8 *)screen->pixels)[tempPosX2 + tempPosY2 * screen->pitch] = pixel;
1112 }
1113 }
1114 }
1115 }
1116
1117 static unsigned int aliasDirtPixel(const SDL_Surface * screen, unsigned int x, unsigned int y, const Uint8 * s)
1118 {
1119 //A helper function used when aliasing dirt. That's a messy process;
1120 //let's contain the mess here.
1121 unsigned int newColor = PIXEL_BLACK;
1122
1123 if ((y > 0) && (*(s - screen->pitch) == PIXEL_DIRT)) // look up
1124 newColor += 1;
1125 if ((y < screen->h - 1u) && (*(s + screen->pitch) == PIXEL_DIRT)) // look down
1126 newColor += 3;
1127 if ((x > 0) && (*(s - 1) == PIXEL_DIRT)) // look left
1128 newColor += 2;
1129 if ((x < screen->pitch - 1u) && (*(s + 1) == PIXEL_DIRT)) // look right
1130 newColor += 2;
1131 if (newColor != PIXEL_BLACK)
1132 return newColor + 16; // 16 must be the start of the brown pixels.
1133
1134 return PIXEL_BLACK;
1135 }
1136
1137 static void JE_aliasDirt(SDL_Surface * screen)
1138 {
1139 /* This complicated looking function goes through the whole screen
1140 * looking for brown pixels which just happen to be next to non-brown
1141 * pixels. It's an aliaser, just like it says. */
1142 unsigned int x, y;
1143
1144 /* This is a pointer to a screen. If you don't like pointer arithmetic,
1145 * you won't like this function. */
1146 Uint8 *s = screen->pixels;
1147 s += 12 * screen->pitch;
1148
1149 for (y = 12; y < (unsigned int)screen->h; y++)
1150 {
1151 for (x = 0; x < (unsigned int)screen->pitch; x++)
1152 {
1153 if (*s == PIXEL_BLACK)
1154 *s = aliasDirtPixel(screen, x, y, s);
1155
1156 s++;
1157 }
1158 }
1159 }
1160
1161 static unsigned int JE_placementPosition(unsigned int passed_x, unsigned int width, unsigned int * world)
1162 {
1163 unsigned int i, new_y;
1164
1165 /* This is the function responsible for carving out chunks of land.
1166 * There's a bug here, but it's a pretty major gameplay altering one:
1167 * areas can be carved out for units that are aerial or in mountains.
1168 * This can result in huge caverns. Ergo, it's a feature :)
1169 *
1170 * I wondered if it might be better to not carve out land at all.
1171 * On testing I determined that was distracting and added nothing. */
1172 new_y = 0;
1173 for (i = passed_x; i <= passed_x + width - 1; i++)
1174 {
1175 if (new_y < world[i])
1176 new_y = world[i];
1177 }
1178
1179 for (i = passed_x; i <= passed_x + width - 1; i++)
1180 {
1181 world[i] = new_y;
1182 }
1183
1184 return new_y;
1185 }
1186
1187 static bool JE_stabilityCheck(unsigned int x, unsigned int y)
1188 {
1189 unsigned int i, numDirtPixels;
1190 Uint8 * s;
1191
1192 numDirtPixels = 0;
1193 s = destructTempScreen->pixels;
1194 s += x + (y * destructTempScreen->pitch) - 1;
1195
1196 /* Check the 12 pixels on the bottom border of our object */
1197 for (i = 0; i < 12; i++)
1198 {
1199 if (*s == PIXEL_DIRT)
1200 numDirtPixels++;
1201
1202 s++;
1203 }
1204
1205 /* If there are fewer than 10 brown pixels we don't consider it a solid base */
1206 return (numDirtPixels < 10);
1207 }
1208
1209 static void JE_tempScreenChecking(void) /*and copy to vgascreen*/
1210 {
1211 Uint8 *s = VGAScreen->pixels;
1212 s += 12 * VGAScreen->pitch;
1213
1214 Uint8 *temps = destructTempScreen->pixels;
1215 temps += 12 * destructTempScreen->pitch;
1216
1217 for (int y = 12; y < VGAScreen->h; y++)
1218 {
1219 for (int x = 0; x < VGAScreen->pitch; x++)
1220 {
1221 // This block is what fades out explosions. The palette from 241
1222 // to 255 fades from a very dark red to a very bright yellow.
1223 if (*temps >= 241)
1224 {
1225 if (*temps == 241)
1226 *temps = PIXEL_BLACK;
1227 else
1228 (*temps)--;
1229 }
1230
1231 // This block is for aliasing dirt. Computers are fast these days,
1232 // and it's fun.
1233 if (config.alwaysalias == true && *temps == PIXEL_BLACK)
1234 *temps = aliasDirtPixel(VGAScreen, x, y, temps);
1235
1236 /* This is copying from our temp screen to VGAScreen */
1237 *s = *temps;
1238
1239 s++;
1240 temps++;
1241 }
1242 }
1243 }
1244
1245 static void JE_makeExplosion(unsigned int tempPosX, unsigned int tempPosY, enum de_shot_t shottype)
1246 {
1247 unsigned int i, tempExploSize;
1248
1249 /* First find an open explosion. If we can't find one, return.*/
1250 for (i = 0; i < config.max_explosions; i++)
1251 if (exploRec[i].isAvailable == true)
1252 break;
1253 if (i == config.max_explosions) /* No empty slots */
1254 return;
1255
1256 exploRec[i].isAvailable = false;
1257 exploRec[i].x = tempPosX;
1258 exploRec[i].y = tempPosY;
1259 exploRec[i].explowidth = 2;
1260
1261 if (shottype != SHOT_INVALID)
1262 {
1263 tempExploSize = exploSize[shottype];
1264 if (tempExploSize < 5)
1265 JE_eSound(3);
1266 else if (tempExploSize < 15)
1267 JE_eSound(4);
1268 else if (tempExploSize < 20)
1269 JE_eSound(12);
1270 else if (tempExploSize < 40)
1271 JE_eSound(11);
1272 else
1273 {
1274 JE_eSound(12);
1275 JE_eSound(11);
1276 }
1277
1278 exploRec[i].explomax = tempExploSize;
1279 exploRec[i].explofill = exploDensity[shottype];
1280 exploRec[i].exploType = shotDirt[shottype];
1281 }
1282 else
1283 {
1284 JE_eSound(4);
1285 exploRec[i].explomax = (mt_rand() % 40) + 10;
1286 exploRec[i].explofill = (mt_rand() % 60) + 20;
1287 exploRec[i].exploType = EXPL_NORMAL;
1288 }
1289 }
1290
1291 static void JE_eSound(unsigned int sound)
1292 {
1293 static int exploSoundChannel = 0;
1294
1295 if (++exploSoundChannel > 5)
1296 exploSoundChannel = 1;
1297
1298 soundQueue[exploSoundChannel] = sound;
1299 }
1300
1301 static void JE_superPixel(unsigned int tempPosX, unsigned int tempPosY)
1302 {
1303 const unsigned int starPattern[5][5] =
1304 {
1305 { 0, 0, 246, 0, 0 },
1306 { 0, 247, 249, 247, 0 },
1307 { 246, 249, 252, 249, 246 },
1308 { 0, 247, 249, 247, 0 },
1309 { 0, 0, 246, 0, 0 }
1310 };
1311 const unsigned int starIntensity[5][5] =
1312 {
1313 { 0, 0, 1, 0, 0 },
1314 { 0, 1, 2, 1, 0 },
1315 { 1, 2, 4, 2, 1 },
1316 { 0, 1, 2, 1, 0 },
1317 { 0, 0, 1, 0, 0 }
1318 };
1319
1320 int x, y, maxX, maxY;
1321 unsigned int rowLen;
1322 Uint8 *s;
1323
1324 maxX = destructTempScreen->pitch;
1325 maxY = destructTempScreen->h;
1326
1327 rowLen = destructTempScreen->pitch;
1328 s = destructTempScreen->pixels;
1329 s += (rowLen * (tempPosY - 2)) + (tempPosX - 2);
1330
1331 for (y = 0; y < 5; y++, s += rowLen - 5)
1332 {
1333 if ((signed)tempPosY + y - 2 < 0 || /* would be out of bounds */
1334 (signed)tempPosY + y - 2 >= maxY)
1335 {
1336 continue;
1337 }
1338
1339 for (x = 0; x < 5; x++, s++)
1340 {
1341 if ((signed)tempPosX + x - 2 < 0 ||
1342 (signed)tempPosX + x - 2 >= maxX)
1343 {
1344 continue;
1345 }
1346
1347 if (starPattern[y][x] == 0)
1348 continue; /* this is just to speed it up */
1349
1350 /* at this point *s is our pixel. Our constant arrays tell us what
1351 * to do with it. */
1352 if (*s < starPattern[y][x])
1353 *s = starPattern[y][x];
1354 else if (*s + starIntensity[y][x] > 255)
1355 *s = 255;
1356 else
1357 *s += starIntensity[y][x];
1358 }
1359 }
1360 }
1361
1362 static void JE_helpScreen(void)
1363 {
1364 unsigned int i, j;
1365
1366 //JE_getVGA(); didn't do anything anyway?
1367 fade_black(15);
1368 memcpy(VGAScreen2->pixels, VGAScreen->pixels, VGAScreen2->h * VGAScreen2->pitch);
1369 JE_clr256(VGAScreen);
1370
1371 for (i = 0; i < 2; i++)
1372 {
1373 JE_outText(VGAScreen, 100, 5 + i * 90, destructHelp[i * 12 + 0], 2, 4);
1374 JE_outText(VGAScreen, 100, 15 + i * 90, destructHelp[i * 12 + 1], 2, 1);
1375 for (j = 3; j <= 12; j++)
1376 JE_outText(VGAScreen, ((j - 1) % 2) * 160 + 10, 15 + ((j - 1) / 2) * 12 + i * 90, destructHelp[i * 12 + j-1], 1, 3);
1377 }
1378 JE_outText(VGAScreen, 30, 190, destructHelp[24], 3, 4);
1379 JE_showVGA();
1380 fade_palette(colors, 15, 0, 255);
1381
1382 do /* wait until user hits a key */
1383 {
1384 service_SDL_events(true);
1385 SDL_Delay(16);
1386 } while (!newkey);
1387
1388 fade_black(15);
1389 memcpy(VGAScreen->pixels, VGAScreen2->pixels, VGAScreen->h * VGAScreen->pitch);
1390 JE_showVGA();
1391 fade_palette(colors, 15, 0, 255);
1392 }
1393
1394 static void JE_pauseScreen(void)
1395 {
1396 set_volume(tyrMusicVolume / 2, fxVolume);
1397
1398 /* Save our current screen/game world. We don't want to screw it up while paused. */
1399 memcpy(VGAScreen2->pixels, VGAScreen->pixels, VGAScreen2->h * VGAScreen2->pitch);
1400 JE_outText(VGAScreen, JE_fontCenter(miscText[22], TINY_FONT), 90, miscText[22], 12, 5);
1401 JE_showVGA();
1402
1403 do /* wait until user hits a key */
1404 {
1405 service_SDL_events(true);
1406 SDL_Delay(16);
1407 } while (!newkey);
1408
1409 /* Restore current screen & volume*/
1410 memcpy(VGAScreen->pixels, VGAScreen2->pixels, VGAScreen->h * VGAScreen->pitch);
1411 JE_showVGA();
1412
1413 set_volume(tyrMusicVolume, fxVolume);
1414 }
1415
1416 /* DE_ResetX
1417 *
1418 * The reset functions clear the state of whatever they are assigned to.
1419 */
1420 static void DE_ResetUnits(void)
1421 {
1422 unsigned int p, u;
1423
1424 for (p = 0; p < MAX_PLAYERS; ++p)
1425 for (u = 0; u < config.max_installations; ++u)
1426 destruct_player[p].unit[u].health = 0;
1427 }
1428
1429 static void DE_ResetPlayers(void)
1430 {
1431 unsigned int i;
1432
1433 for (i = 0; i < MAX_PLAYERS; ++i)
1434 {
1435 destruct_player[i].is_cpu = false;
1436 destruct_player[i].unitSelected = 0;
1437 destruct_player[i].shotDelay = 0;
1438 destruct_player[i].score = 0;
1439 destruct_player[i].aiMemory.c_Angle = 0;
1440 destruct_player[i].aiMemory.c_Power = 0;
1441 destruct_player[i].aiMemory.c_Fire = 0;
1442 destruct_player[i].aiMemory.c_noDown = 0;
1443 memcpy(destruct_player[i].keys.Config, defaultKeyConfig[i], sizeof(destruct_player[i].keys.Config));
1444 }
1445 }
1446
1447 static void DE_ResetWeapons(void)
1448 {
1449 unsigned int i;
1450
1451 for (i = 0; i < config.max_shots; i++)
1452 shotRec[i].isAvailable = true;
1453
1454 for (i = 0; i < config.max_explosions; i++)
1455 exploRec[i].isAvailable = true;
1456 }
1457
1458 static void DE_ResetLevel(void)
1459 {
1460 /* Okay, let's prep the arena */
1461
1462 DE_ResetWeapons();
1463
1464 JE_generateTerrain();
1465 DE_ResetAI();
1466 }
1467
1468 static void DE_ResetAI(void)
1469 {
1470 unsigned int i, j;
1471 struct destruct_unit_s * ptr;
1472
1473 for (i = PLAYER_LEFT; i < MAX_PLAYERS; i++)
1474 {
1475 if (destruct_player[i].is_cpu == false)
1476 continue;
1477 ptr = destruct_player[i].unit;
1478
1479 for (j = 0; j < config.max_installations; j++, ptr++)
1480 {
1481 if (DE_isValidUnit(ptr) == false)
1482 continue;
1483
1484 if (systemAngle[ptr->unitType] || ptr->unitType == UNIT_HELI)
1485 ptr->angle = M_PI_4;
1486 else
1487 ptr->angle = 0;
1488
1489 ptr->power = (ptr->unitType == UNIT_LASER) ? 6 : 4;
1490
1491 if (world.mapFlags & MAP_WALLS)
1492 ptr->shotType = defaultCpuWeaponB[ptr->unitType];
1493 else
1494 ptr->shotType = defaultCpuWeapon[ptr->unitType];
1495 }
1496 }
1497 }
1498
1499 static void DE_ResetActions(void)
1500 {
1501 unsigned int i;
1502
1503 for (i = 0; i < MAX_PLAYERS; i++)
1504 { /* Zero it all. A memset would do the trick */
1505 memset(&(destruct_player[i].moves), 0, sizeof(destruct_player[i].moves));
1506 }
1507 }
1508
1509 /* DE_RunTick
1510 *
1511 * Runs one tick. One tick involves handling physics, drawing crap,
1512 * moving projectiles and explosions, and getting input.
1513 * Returns true while the game is running or false if the game is
1514 * to be terminated.
1515 */
1516 static enum de_state_t DE_RunTick(void)
1517 {
1518 static unsigned int endDelay;
1519
1520 setDelay(1);
1521
1522 memset(soundQueue, 0, sizeof(soundQueue));
1523 JE_tempScreenChecking();
1524
1525 DE_ResetActions();
1526 DE_RunTickCycleDeadUnits();
1527
1528 DE_RunTickGravity();
1529 DE_RunTickAnimate();
1530 DE_RunTickDrawWalls();
1531 DE_RunTickExplosions();
1532 DE_RunTickShots();
1533 DE_RunTickAI();
1534 DE_RunTickDrawCrosshairs();
1535 DE_RunTickDrawHUD();
1536 JE_showVGA();
1537
1538 if (destructFirstTime)
1539 {
1540 fade_palette(colors, 25, 0, 255);
1541 destructFirstTime = false;
1542 endDelay = 0;
1543 }
1544
1545 DE_RunTickGetInput();
1546 DE_ProcessInput();
1547
1548 if (endDelay > 0)
1549 {
1550 if (--endDelay == 0)
1551 return STATE_RELOAD;
1552 }
1553 else if (DE_RunTickCheckEndgame() == true)
1554 {
1555 endDelay = 80;
1556 }
1557
1558 DE_RunTickPlaySounds();
1559
1560 /* The rest of this cruft needs to be put in appropriate sections */
1561 if (keysactive[SDL_SCANCODE_F10])
1562 {
1563 destruct_player[PLAYER_LEFT].is_cpu = !destruct_player[PLAYER_LEFT].is_cpu;
1564 keysactive[SDL_SCANCODE_F10] = false;
1565 }
1566 if (keysactive[SDL_SCANCODE_F11])
1567 {
1568 destruct_player[PLAYER_RIGHT].is_cpu = !destruct_player[PLAYER_RIGHT].is_cpu;
1569 keysactive[SDL_SCANCODE_F11] = false;
1570 }
1571 if (keysactive[SDL_SCANCODE_P])
1572 {
1573 JE_pauseScreen();
1574 keysactive[lastkey_scan] = false;
1575 }
1576
1577 if (keysactive[SDL_SCANCODE_F1])
1578 {
1579 JE_helpScreen();
1580 keysactive[lastkey_scan] = false;
1581 }
1582
1583 wait_delay();
1584
1585 if (keysactive[SDL_SCANCODE_ESCAPE])
1586 {
1587 keysactive[SDL_SCANCODE_ESCAPE] = false;
1588 return STATE_INIT; /* STATE_INIT drops us to the mode select */
1589 }
1590
1591 if (keysactive[SDL_SCANCODE_BACKSPACE])
1592 {
1593 keysactive[SDL_SCANCODE_BACKSPACE] = false;
1594 return STATE_RELOAD; /* STATE_RELOAD creates a new map */
1595 }
1596
1597 return STATE_CONTINUE;
1598 }
1599
1600 /* DE_RunTickX
1601 *
1602 * Handles something that we do once per tick, such as
1603 * track ammo and move explosions.
1604 */
1605 static void DE_RunTickCycleDeadUnits(void)
1606 {
1607 unsigned int i;
1608 struct destruct_unit_s * unit;
1609
1610 /* This code automatically switches the active unit if it is destroyed
1611 * and skips over the useless satellite */
1612 for (i = 0; i < MAX_PLAYERS; i++)
1613 {
1614 if (destruct_player[i].unitsRemaining == 0)
1615 continue;
1616
1617 unit = &(destruct_player[i].unit[destruct_player[i].unitSelected]);
1618 while (DE_isValidUnit(unit) == false ||
1619 unit->shotType == SHOT_INVALID)
1620 {
1621 destruct_player[i].unitSelected++;
1622 unit++;
1623 if (destruct_player[i].unitSelected >= config.max_installations)
1624 {
1625 destruct_player[i].unitSelected = 0;
1626 unit = destruct_player[i].unit;
1627 }
1628 }
1629 }
1630 }
1631
1632 static void DE_RunTickGravity(void)
1633 {
1634 unsigned int i, j;
1635 struct destruct_unit_s * unit;
1636
1637 for (i = 0; i < MAX_PLAYERS; i++)
1638 {
1639 unit = destruct_player[i].unit;
1640 for (j = 0; j < config.max_installations; j++, unit++)
1641 {
1642 if (DE_isValidUnit(unit) == false) /* invalid unit */
1643 continue;
1644
1645 switch (unit->unitType)
1646 {
1647 case UNIT_SATELLITE: /* satellites don't fall down */
1648 break;
1649
1650 case UNIT_HELI:
1651 case UNIT_JUMPER:
1652 if (unit->isYInAir == true) /* unit is falling down, at least in theory */
1653 {
1654 DE_GravityFlyUnit(unit);
1655 break;
1656 }
1657 /* else treat as a normal unit */
1658 /* fall through */
1659 default:
1660 DE_GravityLowerUnit(unit);
1661 }
1662
1663 /* Draw the unit. */
1664 DE_GravityDrawUnit(i, unit);
1665 }
1666 }
1667 }
1668
1669 static void DE_GravityDrawUnit(enum de_player_t team, struct destruct_unit_s * unit)
1670 {
1671 unsigned int anim_index;
1672
1673 anim_index = GraphicBase[team][unit->unitType] + unit->ani_frame;
1674 if (unit->unitType == UNIT_HELI)
1675 {
1676 /* Adjust animation index if we are traveling right or left. */
1677 if (unit->lastMove < -2)
1678 anim_index += 5;
1679 else if (unit->lastMove > 2)
1680 anim_index += 10;
1681 }
1682 else /* This handles our cannons and the like */
1683 {
1684 anim_index += floorf(unit->angle * 9.99f / M_PI);
1685 }
1686
1687 blit_sprite2(VGAScreen, unit->unitX, roundf(unit->unitY) - 13, destructSpriteSheet, anim_index);
1688 }
1689
1690 static void DE_GravityLowerUnit(struct destruct_unit_s * unit)
1691 {
1692 /* units fall at a constant speed. The heli is an odd case though;
1693 * we simply give it a downward velocity, but due to a buggy implementation
1694 * the chopper didn't lower until you tried to fly it up. Tyrian 2000 fixes
1695 * this by not making the chopper a special case. I've decided to actually
1696 * mix both; the chopper is given a slight downward acceleration (simulating
1697 * a 'rocky' takeoff), and it is lowered like a regular unit, but not as
1698 * quickly.
1699 */
1700 if (unit->unitY < 199) /* checking takes time, don't check if it's at the bottom */
1701 {
1702 if (JE_stabilityCheck(unit->unitX, roundf(unit->unitY)))
1703 {
1704 switch (unit->unitType)
1705 {
1706 case UNIT_HELI:
1707 unit->unitYMov = 1.5f;
1708 unit->unitY += 0.2f;
1709 break;
1710
1711 default:
1712 unit->unitY += 1;
1713 }
1714
1715 if (unit->unitY > 199) /* could be possible */
1716 unit->unitY = 199;
1717 }
1718 }
1719 }
1720
1721 static void DE_GravityFlyUnit(struct destruct_unit_s * unit)
1722 {
1723 if (unit->unitY + unit->unitYMov > 199) /* would hit bottom of screen */
1724 {
1725 unit->unitY = 199;
1726 unit->unitYMov = 0;
1727 unit->isYInAir = false;
1728 return;
1729 }
1730
1731 /* move the unit and alter acceleration */
1732 unit->unitY += unit->unitYMov;
1733 if (unit->unitY < 24) /* This stops units from going above the screen */
1734 {
1735 unit->unitYMov = 0;
1736 unit->unitY = 24;
1737 }
1738
1739 if (unit->unitType == UNIT_HELI) /* helicopters fall more slowly */
1740 unit->unitYMov += 0.0001f;
1741 else
1742 unit->unitYMov += 0.03f;
1743
1744 if (!JE_stabilityCheck(unit->unitX, roundf(unit->unitY)))
1745 {
1746 unit->unitYMov = 0;
1747 unit->isYInAir = false;
1748 }
1749 }
1750
1751 static void DE_RunTickAnimate(void)
1752 {
1753 unsigned int p, u;
1754 struct destruct_unit_s * ptr;
1755
1756 for (p = 0; p < MAX_PLAYERS; ++p)
1757 {
1758 ptr = destruct_player[p].unit;
1759 for (u = 0; u < config.max_installations; ++u, ++ptr)
1760 {
1761 /* Don't mess with any unit that is unallocated
1762 * or doesn't animate and is set to frame 0 */
1763 if (DE_isValidUnit(ptr) == false)
1764 continue;
1765 if (systemAni[ptr->unitType] == false && ptr->ani_frame == 0)
1766 continue;
1767
1768 if (++(ptr->ani_frame) > 3)
1769 ptr->ani_frame = 0;
1770 }
1771 }
1772 }
1773
1774 static void DE_RunTickDrawWalls(void)
1775 {
1776 unsigned int i;
1777
1778 for (i = 0; i < config.max_walls; i++)
1779 if (world.mapWalls[i].wallExist)
1780 blit_sprite2(VGAScreen, world.mapWalls[i].wallX, world.mapWalls[i].wallY, destructSpriteSheet, 42);
1781 }
1782
1783 static void DE_RunTickExplosions(void)
1784 {
1785 unsigned int i, j;
1786 int tempPosX, tempPosY;
1787 float tempRadian;
1788
1789 /* Run through all open explosions. They are not sorted in any way */
1790 for (i = 0; i < config.max_explosions; i++)
1791 {
1792 if (exploRec[i].isAvailable == true)
1793 continue; /* Nothing to do */
1794
1795 for (j = 0; j < exploRec[i].explofill; j++)
1796 {
1797 /* An explosion is comprised of multiple 'flares' that fan out.
1798 Calculate where this 'flare' will end up */
1799 tempRadian = mt_rand_lt1() * (2 * M_PI);
1800 tempPosY = exploRec[i].y + roundf(cosf(tempRadian) * mt_rand_lt1() * exploRec[i].explowidth);
1801 tempPosX = exploRec[i].x + roundf(sinf(tempRadian) * mt_rand_lt1() * exploRec[i].explowidth);
1802
1803 /* Our game allows explosions to wrap around. This looks to have
1804 * originally been a bug that was left in as being fun, but we are
1805 * going to replicate it w/o risking out of bound arrays. */
1806
1807 while (tempPosX < 0)
1808 tempPosX += 320;
1809 while (tempPosX > 320)
1810 tempPosX -= 320;
1811
1812 /* We don't draw our explosion if it's out of bounds vertically */
1813 if (tempPosY >= 200 || tempPosY <= 15)
1814 continue;
1815
1816 /* And now the drawing. There are only two types of explosions
1817 * right now; dirt and flares. Dirt simply draws a brown pixel;
1818 * flares explode and have a star formation. */
1819 switch (exploRec[i].exploType)
1820 {
1821 case EXPL_DIRT:
1822 ((Uint8 *)destructTempScreen->pixels)[tempPosX + tempPosY * destructTempScreen->pitch] = PIXEL_DIRT;
1823 break;
1824
1825 case EXPL_NORMAL:
1826 JE_superPixel(tempPosX, tempPosY);
1827 DE_TestExplosionCollision(tempPosX, tempPosY);
1828 break;
1829
1830 default:
1831 assert(false);
1832 break;
1833 }
1834 }
1835
1836 /* Widen the explosion and delete it if necessary. */
1837 exploRec[i].explowidth++;
1838 if (exploRec[i].explowidth == exploRec[i].explomax)
1839 {
1840 exploRec[i].isAvailable = true;
1841 }
1842 }
1843 }
1844
1845 static void DE_TestExplosionCollision(unsigned int PosX, unsigned int PosY)
1846 {
1847 unsigned int i, j;
1848 struct destruct_unit_s * unit;
1849
1850 for (i = PLAYER_LEFT; i < MAX_PLAYERS; i++)
1851 {
1852 unit = destruct_player[i].unit;
1853 for (j = 0; j < config.max_installations; j++, unit++)
1854 {
1855 if (DE_isValidUnit(unit) == true &&
1856 PosX > unit->unitX && PosX < unit->unitX + 11 &&
1857 PosY < unit->unitY && PosY > unit->unitY - 11)
1858 {
1859 unit->health--;
1860 if (unit->health <= 0)
1861 DE_DestroyUnit(i, unit);
1862 }
1863 }
1864 }
1865 }
1866
1867 static void DE_DestroyUnit(enum de_player_t playerID, struct destruct_unit_s * unit)
1868 {
1869 /* This function call was an evil evil piece of brilliance before. Go on.
1870 * Look at the older revisions. It passed the result of a comparison.
1871 * MULTIPLIED. This is at least a little clearer... */
1872 JE_makeExplosion(unit->unitX + 5, roundf(unit->unitY) - 5, (unit->unitType == UNIT_HELI) ? SHOT_SMALL : SHOT_INVALID); /* Helicopters explode like small shots do. Invalids are their own special case. */
1873
1874 if (unit->unitType != UNIT_SATELLITE) /* increment score */
1875 { /* todo: change when teams are created. Hacky kludge for now.*/
1876 destruct_player[playerID].unitsRemaining--;
1877 destruct_player[((playerID == PLAYER_LEFT) ? PLAYER_RIGHT : PLAYER_LEFT)].score++;
1878 }
1879 }
1880
1881 static void DE_RunTickShots(void)
1882 {
1883 unsigned int i, j, k;
1884 unsigned int tempTrails;
1885 unsigned int tempPosX, tempPosY;
1886 struct destruct_unit_s * unit;
1887
1888 for (i = 0; i < config.max_shots; i++)
1889 {
1890 if (shotRec[i].isAvailable == true)
1891 continue; /* Nothing to do */
1892
1893 /* Move the shot. Simple displacement */
1894 shotRec[i].x += shotRec[i].xmov;
1895 shotRec[i].y += shotRec[i].ymov;
1896
1897 /* If the shot can bounce off the map, bounce it */
1898 if (shotBounce[shotRec[i].shottype])
1899 {
1900 if (shotRec[i].y > 199 || shotRec[i].y < 14)
1901 {
1902 shotRec[i].y -= shotRec[i].ymov;
1903 shotRec[i].ymov = -shotRec[i].ymov;
1904 }
1905 if (shotRec[i].x < 1 || shotRec[i].x > 318)
1906 {
1907 shotRec[i].x -= shotRec[i].xmov;
1908 shotRec[i].xmov = -shotRec[i].xmov;
1909 }
1910 }
1911 else /* If it cannot, apply normal physics */
1912 {
1913 shotRec[i].ymov += 0.05f; /* add gravity */
1914
1915 if (shotRec[i].y > 199) /* We hit the floor */
1916 {
1917 shotRec[i].y -= shotRec[i].ymov;
1918 shotRec[i].ymov = -shotRec[i].ymov * 0.8f; /* bounce at reduced velocity */
1919
1920 /* Don't allow a bouncing shot to bounce straight up and down */
1921 if (shotRec[i].xmov == 0)
1922 shotRec[i].xmov += mt_rand_lt1() - 0.5f;
1923 }
1924 }
1925
1926 /* Shot has gone out of bounds. Eliminate it. */
1927 if (shotRec[i].x > 318 || shotRec[i].x < 1)
1928 {
1929 shotRec[i].isAvailable = true;
1930 continue;
1931 }
1932
1933 /* Now check for collisions. */
1934
1935 /* Don't bother checking for collisions above the map :) */
1936 if (shotRec[i].y <= 14)
1937 continue;
1938
1939 tempPosX = roundf(shotRec[i].x);
1940 tempPosY = roundf(shotRec[i].y);
1941
1942 /*Check building hits*/
1943 for (j = 0; j < MAX_PLAYERS; j++)
1944 {
1945 unit = destruct_player[j].unit;
1946 for (k = 0; k < config.max_installations; k++, unit++)
1947 {
1948 if (DE_isValidUnit(unit) == false)
1949 continue;
1950
1951 if (tempPosX > unit->unitX && tempPosX < unit->unitX + 11 &&
1952 tempPosY < unit->unitY && tempPosY > unit->unitY - 13)
1953 {
1954 shotRec[i].isAvailable = true;
1955 JE_makeExplosion(tempPosX, tempPosY, shotRec[i].shottype);
1956 }
1957 }
1958 }
1959
1960 tempTrails = (shotColor[shotRec[i].shottype] << 4) - 3;
1961 JE_pixCool(tempPosX, tempPosY, tempTrails);
1962
1963 /*Draw the shot trail (if applicable) */
1964 switch (shotTrail[shotRec[i].shottype])
1965 {
1966 case TRAILS_NONE:
1967 break;
1968 case TRAILS_NORMAL:
1969 DE_DrawTrails(&(shotRec[i]), 2, 4, tempTrails - 3);
1970 break;
1971 case TRAILS_FULL:
1972 DE_DrawTrails(&(shotRec[i]), 4, 3, tempTrails - 1);
1973 break;
1974 }
1975
1976 /* Bounce off of or destroy walls */
1977 for (j = 0; j < config.max_walls; j++)
1978 {
1979 if (world.mapWalls[j].wallExist == true &&
1980 tempPosX >= world.mapWalls[j].wallX && tempPosX <= world.mapWalls[j].wallX + 11 &&
1981 tempPosY >= world.mapWalls[j].wallY && tempPosY <= world.mapWalls[j].wallY + 14)
1982 {
1983 if (demolish[shotRec[i].shottype])
1984 {
1985 /* Blow up the wall and remove the shot. */
1986 world.mapWalls[j].wallExist = false;
1987 shotRec[i].isAvailable = true;
1988 JE_makeExplosion(tempPosX, tempPosY, shotRec[i].shottype);
1989 continue;
1990 }
1991 else
1992 {
1993 /* Otherwise, bounce. */
1994 if (shotRec[i].x - shotRec[i].xmov < world.mapWalls[j].wallX ||
1995 shotRec[i].x - shotRec[i].xmov > world.mapWalls[j].wallX + 11)
1996 {
1997 shotRec[i].xmov = -shotRec[i].xmov;
1998 }
1999 if (shotRec[i].y - shotRec[i].ymov < world.mapWalls[j].wallY ||
2000 shotRec[i].y - shotRec[i].ymov > world.mapWalls[j].wallY + 14)
2001 {
2002 if (shotRec[i].ymov < 0)
2003 shotRec[i].ymov = -shotRec[i].ymov;
2004 else
2005 shotRec[i].ymov = -shotRec[i].ymov * 0.8f;
2006 }
2007
2008 tempPosX = roundf(shotRec[i].x);
2009 tempPosY = roundf(shotRec[i].y);
2010 }
2011 }
2012 }
2013
2014 /* Our last collision check, at least for now. We hit dirt. */
2015 if ((((Uint8 *)destructTempScreen->pixels)[tempPosX + tempPosY * destructTempScreen->pitch]) == PIXEL_DIRT)
2016 {
2017 shotRec[i].isAvailable = true;
2018 JE_makeExplosion(tempPosX, tempPosY, shotRec[i].shottype);
2019 continue;
2020 }
2021 }
2022 }
2023
2024 static void DE_DrawTrails(struct destruct_shot_s * shot, unsigned int count, unsigned int decay, unsigned int startColor)
2025 {
2026 int i;
2027
2028 for (i = count-1; i >= 0; i--) /* going in reverse is important as it affects how we draw */
2029 {
2030 if (shot->trailc[i] > 0 && shot->traily[i] > 12) /* If it exists and if it's not out of bounds, draw it. */
2031 {
2032 JE_pixCool(shot->trailx[i], shot->traily[i], shot->trailc[i]);
2033 }
2034
2035 if (i == 0) /* The first trail we create. */
2036 {
2037 shot->trailx[i] = roundf(shot->x);
2038 shot->traily[i] = roundf(shot->y);
2039 shot->trailc[i] = startColor;
2040 }
2041 else /* The newer trails decay into the older trails.*/
2042 {
2043 shot->trailx[i] = shot->trailx[i-1];
2044 shot->traily[i] = shot->traily[i-1];
2045 if (shot->trailc[i-1] > 0)
2046 {
2047 shot->trailc[i] = shot->trailc[i-1] - decay;
2048 }
2049 }
2050 }
2051 }
2052
2053 static void DE_RunTickAI(void)
2054 {
2055 unsigned int i, j;
2056 struct destruct_player_s * ptrPlayer, * ptrTarget;
2057 struct destruct_unit_s * ptrUnit, * ptrCurUnit;
2058
2059 for (i = 0; i < MAX_PLAYERS; i++)
2060 {
2061 ptrPlayer = &(destruct_player[i]);
2062 if (ptrPlayer->is_cpu == false)
2063 continue;
2064
2065 /* I've been thinking, purely hypothetically, about what it would take
2066 * to have multiple computer opponents. The answer? A lot of crap
2067 * and a 'target' variable in the destruct_player struct. */
2068 j = i + 1;
2069 if (j >= MAX_PLAYERS)
2070 j = 0;
2071
2072 ptrTarget = &(destruct_player[j]);
2073 ptrCurUnit = &(ptrPlayer->unit[ptrPlayer->unitSelected]);
2074
2075 /* This is the start of the original AI. Heh. AI. */
2076
2077 if (ptrPlayer->aiMemory.c_noDown > 0)
2078 ptrPlayer->aiMemory.c_noDown--;
2079
2080 /* Until all structs are properly divvied up this must only apply to player1 */
2081 if (mt_rand() % 100 > 80)
2082 {
2083 ptrPlayer->aiMemory.c_Angle += (mt_rand() % 3) - 1;
2084
2085 if (ptrPlayer->aiMemory.c_Angle > 1)
2086 ptrPlayer->aiMemory.c_Angle = 1;
2087 else
2088 if (ptrPlayer->aiMemory.c_Angle < -1)
2089 ptrPlayer->aiMemory.c_Angle = -1;
2090 }
2091 if (mt_rand() % 100 > 90)
2092 {
2093 if (ptrPlayer->aiMemory.c_Angle > 0 && ptrCurUnit->angle > (M_PI_2) - (M_PI / 9))
2094 ptrPlayer->aiMemory.c_Angle = 0;
2095 else
2096 if (ptrPlayer->aiMemory.c_Angle < 0 && ptrCurUnit->angle < M_PI / 8)
2097 ptrPlayer->aiMemory.c_Angle = 0;
2098 }
2099
2100 if (mt_rand() % 100 > 93)
2101 {
2102 ptrPlayer->aiMemory.c_Power += (mt_rand() % 3) - 1;
2103
2104 if (ptrPlayer->aiMemory.c_Power > 1)
2105 ptrPlayer->aiMemory.c_Power = 1;
2106 else
2107 if (ptrPlayer->aiMemory.c_Power < -1)
2108 ptrPlayer->aiMemory.c_Power = -1;
2109 }
2110 if (mt_rand() % 100 > 90)
2111 {
2112 if (ptrPlayer->aiMemory.c_Power > 0 && ptrCurUnit->power > 4)
2113 ptrPlayer->aiMemory.c_Power = 0;
2114 else
2115 if (ptrPlayer->aiMemory.c_Power < 0 && ptrCurUnit->power < 3)
2116 ptrPlayer->aiMemory.c_Power = 0;
2117 else
2118 if (ptrCurUnit->power < 2)
2119 ptrPlayer->aiMemory.c_Power = 1;
2120 }
2121
2122 // prefer helicopter
2123 ptrUnit = ptrPlayer->unit;
2124 for (j = 0; j < config.max_installations; j++, ptrUnit++)
2125 {
2126 if (DE_isValidUnit(ptrUnit) && ptrUnit->unitType == UNIT_HELI)
2127 {
2128 ptrPlayer->unitSelected = j;
2129 break;
2130 }
2131 }
2132
2133 if (ptrCurUnit->unitType == UNIT_HELI)
2134 {
2135 if (ptrCurUnit->isYInAir == false)
2136 {
2137 ptrPlayer->aiMemory.c_Power = 1;
2138 }
2139 if (mt_rand() % ptrCurUnit->unitX > 100)
2140 {
2141 ptrPlayer->aiMemory.c_Power = 1;
2142 }
2143 if (mt_rand() % 240 > ptrCurUnit->unitX)
2144 {
2145 ptrPlayer->moves.actions[MOVE_RIGHT] = true;
2146 }
2147 else if ((mt_rand() % 20) + 300 < ptrCurUnit->unitX)
2148 {
2149 ptrPlayer->moves.actions[MOVE_LEFT] = true;
2150 }
2151 else if (mt_rand() % 30 == 1)
2152 {
2153 ptrPlayer->aiMemory.c_Angle = (mt_rand() % 3) - 1;
2154 }
2155 if (ptrCurUnit->unitX > 295 && ptrCurUnit->lastMove > 1)
2156 {
2157 ptrPlayer->moves.actions[MOVE_LEFT] = true;
2158 ptrPlayer->moves.actions[MOVE_RIGHT] = false;
2159 }
2160 if (ptrCurUnit->unitType != UNIT_HELI || ptrCurUnit->lastMove > 3 || (ptrCurUnit->unitX > 160 && ptrCurUnit->lastMove > -3))
2161 {
2162 if (mt_rand() % (int)roundf(ptrCurUnit->unitY) < 150 && ptrCurUnit->unitYMov < 0.01f && (ptrCurUnit->unitX < 160 || ptrCurUnit->lastMove < 2))
2163 ptrPlayer->moves.actions[MOVE_FIRE] = true;
2164 ptrPlayer->aiMemory.c_noDown = (5 - abs(ptrCurUnit->lastMove)) * (5 - abs(ptrCurUnit->lastMove)) + 3;
2165 ptrPlayer->aiMemory.c_Power = 1;
2166 }
2167 else
2168 {
2169 ptrPlayer->moves.actions[MOVE_FIRE] = false;
2170 }
2171
2172 ptrUnit = ptrTarget->unit;
2173 for (j = 0; j < config.max_installations; j++, ptrUnit++)
2174 {
2175 if (abs((int)ptrUnit->unitX - (int)ptrCurUnit->unitX) < 8)
2176 {
2177 /* I get it. This makes helicopters hover over
2178 * their enemies. */
2179 if (ptrUnit->unitType == UNIT_SATELLITE)
2180 {
2181 ptrPlayer->moves.actions[MOVE_FIRE] = false;
2182 }
2183 else
2184 {
2185 ptrPlayer->moves.actions[MOVE_LEFT] = false;
2186 ptrPlayer->moves.actions[MOVE_RIGHT] = false;
2187 if (ptrCurUnit->lastMove < -1)
2188 ptrCurUnit->lastMove++;
2189 else if (ptrCurUnit->lastMove > 1)
2190 ptrCurUnit->lastMove--;
2191 }
2192 }
2193 }
2194 }
2195 else
2196 {
2197 ptrPlayer->moves.actions[MOVE_FIRE] = 1;
2198 }
2199
2200 if (mt_rand() % 200 > 198)
2201 {
2202 ptrPlayer->moves.actions[MOVE_CHANGE] = true;
2203 ptrPlayer->aiMemory.c_Angle = 0;
2204 ptrPlayer->aiMemory.c_Power = 0;
2205 ptrPlayer->aiMemory.c_Fire = 0;
2206 }
2207
2208 if (mt_rand() % 100 > 98 || ptrCurUnit->shotType == SHOT_TRACER)
2209 {
2210 ptrPlayer->moves.actions[MOVE_CYDN] = true;
2211 }
2212 if (ptrPlayer->aiMemory.c_Angle > 0)
2213 {
2214 ptrPlayer->moves.actions[MOVE_LEFT] = true;
2215 }
2216 if (ptrPlayer->aiMemory.c_Angle < 0)
2217 {
2218 ptrPlayer->moves.actions[MOVE_RIGHT] = true;
2219 }
2220 if (ptrPlayer->aiMemory.c_Power > 0)
2221 {
2222 ptrPlayer->moves.actions[MOVE_UP] = true;
2223 }
2224 if (ptrPlayer->aiMemory.c_Power < 0 && ptrPlayer->aiMemory.c_noDown == 0)
2225 {
2226 ptrPlayer->moves.actions[MOVE_DOWN] = true;
2227 }
2228 if (ptrPlayer->aiMemory.c_Fire > 0)
2229 {
2230 ptrPlayer->moves.actions[MOVE_FIRE] = true;
2231 }
2232
2233 if (ptrCurUnit->unitYMov < -0.1f && ptrCurUnit->unitType == UNIT_HELI)
2234 {
2235 ptrPlayer->moves.actions[MOVE_FIRE] = false;
2236 }
2237
2238 /* This last hack was down in the processing section.
2239 * What exactly it was doing there I do not know */
2240 if (ptrCurUnit->unitType == UNIT_LASER || ptrCurUnit->isYInAir == true)
2241 ptrPlayer->aiMemory.c_Power = 0;
2242 }
2243 }
2244
2245 static void DE_RunTickDrawCrosshairs(void)
2246 {
2247 unsigned int i;
2248 int tempPosX, tempPosY;
2249 int direction;
2250 struct destruct_unit_s * curUnit;
2251
2252 /* Draw the crosshairs. Most vehicles aim left or right. Helis can aim
2253 * either way and this must be accounted for.
2254 */
2255 for (i = 0; i < MAX_PLAYERS; i++)
2256 {
2257 direction = (i == PLAYER_LEFT) ? -1 : 1;
2258 curUnit = &(destruct_player[i].unit[destruct_player[i].unitSelected]);
2259
2260 if (curUnit->unitType == UNIT_HELI)
2261 {
2262 tempPosX = curUnit->unitX + roundf(0.1f * curUnit->lastMove * curUnit->lastMove * curUnit->lastMove) + 5;
2263 tempPosY = roundf(curUnit->unitY) + 1;
2264 }
2265 else
2266 {
2267 tempPosX = roundf(curUnit->unitX + 6 - cosf(curUnit->angle) * (curUnit->power * 8 + 7) * direction);
2268 tempPosY = roundf(curUnit->unitY - 7 - sinf(curUnit->angle) * (curUnit->power * 8 + 7));
2269 }
2270
2271 /* Draw it. Clip away from the HUD though. */
2272 if (tempPosY > 9)
2273 {
2274 if (tempPosY > 11)
2275 {
2276 if (tempPosY > 13)
2277 {
2278 /* Top pixel */
2279 JE_pix(VGAScreen, tempPosX, tempPosY - 2, 3);
2280 }
2281 /* Middle three pixels */
2282 JE_pix(VGAScreen, tempPosX + 3, tempPosY, 3);
2283 JE_pix(VGAScreen, tempPosX, tempPosY, 14);
2284 JE_pix(VGAScreen, tempPosX - 3, tempPosY, 3);
2285 }
2286 /* Bottom pixel */
2287 JE_pix(VGAScreen, tempPosX, tempPosY + 2, 3);
2288 }
2289 }
2290 }
2291
2292 static void DE_RunTickDrawHUD(void)
2293 {
2294 unsigned int i;
2295 unsigned int startX;
2296 char tempstr[16]; /* Max size needed: 16 assuming 10 digit int max. */
2297 struct destruct_unit_s * curUnit;
2298
2299 for (i = 0; i < MAX_PLAYERS; i++)
2300 {
2301 curUnit = &(destruct_player[i].unit[destruct_player[i].unitSelected]);
2302 startX = ((i == PLAYER_LEFT) ? 0 : 320 - 150);
2303
2304 fill_rectangle_xy(VGAScreen, startX + 5, 3, startX + 14, 8, 241);
2305 JE_rectangle(VGAScreen, startX + 4, 2, startX + 15, 9, 242);
2306 JE_rectangle(VGAScreen, startX + 3, 1, startX + 16, 10, 240);
2307 fill_rectangle_xy(VGAScreen, startX + 18, 3, startX + 140, 8, 241);
2308 JE_rectangle(VGAScreen, startX + 17, 2, startX + 143, 9, 242);
2309 JE_rectangle(VGAScreen, startX + 16, 1, startX + 144, 10, 240);
2310
2311 blit_sprite2(VGAScreen, startX + 4, 0, destructSpriteSheet, 191 + curUnit->shotType);
2312
2313 JE_outText (VGAScreen, startX + 20, 3, weaponNames[curUnit->shotType], 15, 2);
2314 sprintf (tempstr, "dmg~%d~", curUnit->health);
2315 JE_outText (VGAScreen, startX + 75, 3, tempstr, 15, 0);
2316 sprintf (tempstr, "pts~%d~", destruct_player[i].score);
2317 JE_outText (VGAScreen, startX + 110, 3, tempstr, 15, 0);
2318 }
2319 }
2320
2321 static void DE_RunTickGetInput(void)
2322 {
2323 unsigned int player_index, key_index, slot_index;
2324 SDL_Scancode key;
2325
2326 /* destruct_player.keys holds our key config. Players will eventually be
2327 * allowed to can change their key mappings. destruct_player.moves and
2328 * destruct_player.keys line up; rather than manually checking left and
2329 * right we can just loop through the indexes and set the actions as
2330 * needed. */
2331 service_SDL_events(true);
2332
2333 for (player_index = 0; player_index < MAX_PLAYERS; player_index++)
2334 {
2335 for (key_index = 0; key_index < MAX_KEY; key_index++)
2336 {
2337 for (slot_index = 0; slot_index < MAX_KEY_OPTIONS; slot_index++)
2338 {
2339 key = destruct_player[player_index].keys.Config[key_index][slot_index];
2340 if (key == SDL_SCANCODE_UNKNOWN)
2341 break;
2342 if (keysactive[key] == true)
2343 {
2344 /* The right key was clearly pressed */
2345 destruct_player[player_index].moves.actions[key_index] = true;
2346
2347 /* Some keys we want to toggle afterwards */
2348 if (key_index == KEY_CHANGE ||
2349 key_index == KEY_CYUP ||
2350 key_index == KEY_CYDN)
2351 {
2352 keysactive[key] = false;
2353 }
2354 break;
2355 }
2356 }
2357 }
2358 }
2359 }
2360
2361 static void DE_ProcessInput(void)
2362 {
2363 int direction;
2364
2365 unsigned int player_index;
2366 struct destruct_unit_s * curUnit;
2367
2368 for (player_index = 0; player_index < MAX_PLAYERS; player_index++)
2369 {
2370 if (destruct_player[player_index].unitsRemaining <= 0)
2371 continue;
2372
2373 direction = (player_index == PLAYER_LEFT) ? -1 : 1;
2374 curUnit = &(destruct_player[player_index].unit[destruct_player[player_index].unitSelected]);
2375
2376 if (systemAngle[curUnit->unitType] == true) /* selected unit may change shot angle */
2377 {
2378 if (destruct_player[player_index].moves.actions[MOVE_LEFT] == true)
2379 {
2380 if (player_index == PLAYER_LEFT)
2381 DE_RaiseAngle(curUnit);
2382 else
2383 DE_LowerAngle(curUnit);
2384 }
2385 if (destruct_player[player_index].moves.actions[MOVE_RIGHT] == true)
2386 {
2387 if (player_index == PLAYER_LEFT)
2388 DE_LowerAngle(curUnit);
2389 else
2390 DE_RaiseAngle(curUnit);
2391 }
2392 }
2393 else if (curUnit->unitType == UNIT_HELI)
2394 {
2395 if (destruct_player[player_index].moves.actions[MOVE_LEFT] == true && curUnit->unitX > 5)
2396 {
2397 if (JE_stabilityCheck(curUnit->unitX - 5, roundf(curUnit->unitY)))
2398 {
2399 if (curUnit->lastMove > -5)
2400 curUnit->lastMove--;
2401 curUnit->unitX--;
2402 if (JE_stabilityCheck(curUnit->unitX, roundf(curUnit->unitY)))
2403 curUnit->isYInAir = true;
2404 }
2405 }
2406 if (destruct_player[player_index].moves.actions[MOVE_RIGHT] == true && curUnit->unitX < 305)
2407 {
2408 if (JE_stabilityCheck(curUnit->unitX + 5, roundf(curUnit->unitY)))
2409 {
2410 if (curUnit->lastMove < 5)
2411 curUnit->lastMove++;
2412 curUnit->unitX++;
2413 if (JE_stabilityCheck(curUnit->unitX, roundf(curUnit->unitY)))
2414 curUnit->isYInAir = true;
2415 }
2416 }
2417 }
2418
2419 if (curUnit->unitType != UNIT_LASER)
2420 { /*increasepower*/
2421 if (destruct_player[player_index].moves.actions[MOVE_UP] == true)
2422 {
2423 if (curUnit->unitType == UNIT_HELI)
2424 {
2425 curUnit->isYInAir = true;
2426 curUnit->unitYMov -= 0.1f;
2427 }
2428 else if (curUnit->unitType == UNIT_JUMPER &&
2429 curUnit->isYInAir == false)
2430 {
2431 curUnit->unitYMov = -3;
2432 curUnit->isYInAir = true;
2433 }
2434 else
2435 {
2436 DE_RaisePower(curUnit);
2437 }
2438 }
2439 /*decreasepower*/
2440 if (destruct_player[player_index].moves.actions[MOVE_DOWN] == true)
2441 {
2442 if (curUnit->unitType == UNIT_HELI && curUnit->isYInAir == true)
2443 {
2444 curUnit->unitYMov += 0.1f;
2445 }
2446 else
2447 {
2448 DE_LowerPower(curUnit);
2449 }
2450 }
2451 }
2452
2453 /*up/down weapon. These just cycle until a valid weapon is found */
2454 if (destruct_player[player_index].moves.actions[MOVE_CYUP] == true)
2455 DE_CycleWeaponUp(curUnit);
2456 if (destruct_player[player_index].moves.actions[MOVE_CYDN] == true)
2457 DE_CycleWeaponDown(curUnit);
2458
2459 /* Change. Since change would change out curUnit pointer, let's just do it last.
2460 * Validity checking is performed at the beginning of the tick. */
2461 if (destruct_player[player_index].moves.actions[MOVE_CHANGE] == true)
2462 {
2463 destruct_player[player_index].unitSelected++;
2464 if (destruct_player[player_index].unitSelected >= config.max_installations)
2465 destruct_player[player_index].unitSelected = 0;
2466 }
2467
2468 /*Newshot*/
2469 if (destruct_player[player_index].shotDelay > 0)
2470 destruct_player[player_index].shotDelay--;
2471 if (destruct_player[player_index].moves.actions[MOVE_FIRE] == true &&
2472 destruct_player[player_index].shotDelay == 0)
2473 {
2474 destruct_player[player_index].shotDelay = shotDelay[curUnit->shotType];
2475
2476 switch (shotDirt[curUnit->shotType])
2477 {
2478 case EXPL_NONE:
2479 break;
2480
2481 case EXPL_MAGNET:
2482 DE_RunMagnet(player_index, curUnit);
2483 break;
2484
2485 case EXPL_DIRT:
2486 case EXPL_NORMAL:
2487 DE_MakeShot(player_index, curUnit, direction);
2488 break;
2489
2490 default:
2491 assert(false);
2492 }
2493 }
2494 }
2495 }
2496
2497 static void DE_CycleWeaponUp(struct destruct_unit_s * unit)
2498 {
2499 do
2500 {
2501 unit->shotType++;
2502 if (unit->shotType > SHOT_LAST)
2503 unit->shotType = SHOT_FIRST;
2504 } while (weaponSystems[unit->unitType][unit->shotType] == 0);
2505 }
2506
2507 static void DE_CycleWeaponDown(struct destruct_unit_s * unit)
2508 {
2509 do
2510 {
2511 unit->shotType--;
2512 if (unit->shotType < SHOT_FIRST)
2513 unit->shotType = SHOT_LAST;
2514 } while (weaponSystems[unit->unitType][unit->shotType] == 0);
2515 }
2516
2517 static void DE_MakeShot(enum de_player_t curPlayer, const struct destruct_unit_s * curUnit, int direction)
2518 {
2519 unsigned int i;
2520 unsigned int shotIndex;
2521
2522 /* First, find an empty shot struct we can use */
2523 for (i = 0; ; i++)
2524 {
2525 if (i >= config.max_shots)
2526 return; /* no empty slots. Do nothing. */
2527
2528 if (shotRec[i].isAvailable)
2529 {
2530 shotIndex = i;
2531 break;
2532 }
2533 }
2534
2535 /* Helis can't fire when they are on the ground. */
2536 if (curUnit->unitType == UNIT_HELI && curUnit->isYInAir == false)
2537 return;
2538
2539 /* Play the firing sound */
2540 soundQueue[curPlayer] = shotSound[curUnit->shotType];
2541
2542 /* Create our shot. Some units have differing logic here */
2543 switch (curUnit->unitType)
2544 {
2545 case UNIT_HELI:
2546
2547 shotRec[shotIndex].x = curUnit->unitX + curUnit->lastMove * 2 + 5;
2548 shotRec[shotIndex].xmov = 0.02f * curUnit->lastMove * curUnit->lastMove * curUnit->lastMove;
2549
2550 /* If we are trying in vain to move up off the screen, act differently.*/
2551 if (destruct_player[curPlayer].moves.actions[MOVE_UP] && curUnit->unitY < 30)
2552 {
2553 shotRec[shotIndex].y = curUnit->unitY;
2554 shotRec[shotIndex].ymov = 0.1f;
2555
2556 if (shotRec[shotIndex].xmov < 0)
2557 shotRec[shotIndex].xmov += 0.1f;
2558 else if (shotRec[shotIndex].xmov > 0)
2559 shotRec[shotIndex].xmov -= 0.1f;
2560 }
2561 else
2562 {
2563 shotRec[shotIndex].y = curUnit->unitY + 1;
2564 shotRec[shotIndex].ymov = 0.5f + curUnit->unitYMov * 0.1f;
2565 }
2566 break;
2567
2568 case UNIT_JUMPER: /* Jumpers are normally only special for the left hand player. Bug? Or feature? */
2569
2570 if (config.jumper_straight[curPlayer])
2571 {
2572 /* This is identical to the default case.
2573 * I considered letting the switch fall through
2574 * but that's more confusing to people who aren't used
2575 * to that quirk of switch. */
2576
2577 shotRec[shotIndex].x = curUnit->unitX + 6 - cosf(curUnit->angle) * 10 * direction;
2578 shotRec[shotIndex].y = curUnit->unitY - 7 - sinf(curUnit->angle) * 10;
2579 shotRec[shotIndex].xmov = -cosf(curUnit->angle) * curUnit->power * direction;
2580 shotRec[shotIndex].ymov = -sinf(curUnit->angle) * curUnit->power;
2581 }
2582 else
2583 {
2584 /* This is not identical to the default case. */
2585
2586 shotRec[shotIndex].x = curUnit->unitX + 2;
2587 shotRec[shotIndex].xmov = -cosf(curUnit->angle) * curUnit->power * direction;
2588
2589 if (curUnit->isYInAir == true)
2590 {
2591 shotRec[shotIndex].ymov = 1;
2592 shotRec[shotIndex].y = curUnit->unitY + 2;
2593 }
2594 else
2595 {
2596 shotRec[shotIndex].ymov = -2;
2597 shotRec[shotIndex].y = curUnit->unitY - 12;
2598 }
2599 }
2600 break;
2601
2602 default:
2603
2604 shotRec[shotIndex].x = curUnit->unitX + 6 - cosf(curUnit->angle) * 10 * direction;
2605 shotRec[shotIndex].y = curUnit->unitY - 7 - sinf(curUnit->angle) * 10;
2606 shotRec[shotIndex].xmov = -cosf(curUnit->angle) * curUnit->power * direction;
2607 shotRec[shotIndex].ymov = -sinf(curUnit->angle) * curUnit->power;
2608 break;
2609 }
2610
2611 /* Now set/clear out a few last details. */
2612 shotRec[shotIndex].isAvailable = false;
2613
2614 shotRec[shotIndex].shottype = curUnit->shotType;
2615 //shotRec[shotIndex].shotdur = shotFuse[shotRec[shotIndex].shottype];
2616
2617 shotRec[shotIndex].trailc[0] = 0;
2618 shotRec[shotIndex].trailc[1] = 0;
2619 shotRec[shotIndex].trailc[2] = 0;
2620 shotRec[shotIndex].trailc[3] = 0;
2621 }
2622
2623 static void DE_RunMagnet(enum de_player_t curPlayer, struct destruct_unit_s * magnet)
2624 {
2625 unsigned int i;
2626 enum de_player_t curEnemy;
2627 int direction;
2628 struct destruct_unit_s * enemyUnit;
2629
2630 curEnemy = (curPlayer == PLAYER_LEFT) ? PLAYER_RIGHT : PLAYER_LEFT;
2631 direction = (curPlayer == PLAYER_LEFT) ? -1 : 1;
2632
2633 /* Push all shots that are in front of the magnet */
2634 for (i = 0; i < config.max_shots; i++)
2635 {
2636 if (shotRec[i].isAvailable == false)
2637 {
2638 if ((curPlayer == PLAYER_LEFT && shotRec[i].x > magnet->unitX) ||
2639 (curPlayer == PLAYER_RIGHT && shotRec[i].x < magnet->unitX))
2640 {
2641 shotRec[i].xmov += magnet->power * 0.1f * -direction;
2642 }
2643 }
2644 }
2645
2646 enemyUnit = destruct_player[curEnemy].unit;
2647 for (i = 0; i < config.max_installations; i++, enemyUnit++) /* magnets push coptors */
2648 {
2649 if (DE_isValidUnit(enemyUnit) &&
2650 enemyUnit->unitType == UNIT_HELI &&
2651 enemyUnit->isYInAir == true)
2652 {
2653 if ((curEnemy == PLAYER_RIGHT && destruct_player[curEnemy].unit[i].unitX + 11 < 318) ||
2654 (curEnemy == PLAYER_LEFT && destruct_player[curEnemy].unit[i].unitX > 1))
2655 {
2656 enemyUnit->unitX -= 2 * direction;
2657 }
2658 }
2659 }
2660 magnet->ani_frame = 1;
2661 }
2662
2663 static void DE_RaiseAngle(struct destruct_unit_s * unit)
2664 {
2665 unit->angle += 0.01f;
2666 if (unit->angle > M_PI_2 - 0.01f)
2667 unit->angle = M_PI_2 - 0.01f;
2668 }
2669
2670 static void DE_LowerAngle(struct destruct_unit_s * unit)
2671 {
2672 unit->angle -= 0.01f;
2673 if (unit->angle < 0)
2674 unit->angle = 0;
2675 }
2676
2677 static void DE_RaisePower(struct destruct_unit_s * unit)
2678 {
2679 unit->power += 0.05f;
2680 if (unit->power > 5)
2681 unit->power = 5;
2682 }
2683
2684 static void DE_LowerPower(struct destruct_unit_s * unit)
2685 {
2686 unit->power -= 0.05f;
2687 if (unit->power < 1)
2688 unit->power = 1;
2689 }
2690
2691 /* DE_isValidUnit
2692 *
2693 * Returns true if the unit's health is above 0 and false
2694 * otherwise. This mainly exists because the 'health' var
2695 * serves two roles and that can get confusing.
2696 */
2697 static inline bool DE_isValidUnit(struct destruct_unit_s * unit)
2698 {
2699 return unit->health > 0;
2700 }
2701
2702 static bool DE_RunTickCheckEndgame(void)
2703 {
2704 if (destruct_player[PLAYER_LEFT].unitsRemaining == 0)
2705 {
2706 destruct_player[PLAYER_RIGHT].score += ModeScore[PLAYER_LEFT][world.destructMode];
2707 soundQueue[7] = V_CLEARED_PLATFORM;
2708 return true;
2709 }
2710 if (destruct_player[PLAYER_RIGHT].unitsRemaining == 0)
2711 {
2712 destruct_player[PLAYER_LEFT].score += ModeScore[PLAYER_RIGHT][world.destructMode];
2713 soundQueue[7] = V_CLEARED_PLATFORM;
2714 return true;
2715 }
2716 return false;
2717 }
2718
2719 static void DE_RunTickPlaySounds(void)
2720 {
2721 unsigned int i, tempSampleIndex, tempVolume;
2722
2723 for (i = 0; i < COUNTOF(soundQueue); i++)
2724 {
2725 if (soundQueue[i] != S_NONE)
2726 {
2727 tempSampleIndex = soundQueue[i];
2728 if (i == 7)
2729 tempVolume = fxPlayVol;
2730 else
2731 tempVolume = fxPlayVol / 2;
2732
2733 multiSamplePlay(soundSamples[tempSampleIndex-1], soundSampleCount[tempSampleIndex-1], i, tempVolume);
2734 soundQueue[i] = S_NONE;
2735 }
2736 }
2737 }
2738
2739 static void JE_pixCool(unsigned int x, unsigned int y, Uint8 c)
2740 {
2741 JE_pix(VGAScreen, x, y, c);
2742 JE_pix(VGAScreen, x - 1, y, c - 2);
2743 JE_pix(VGAScreen, x + 1, y, c - 2);
2744 JE_pix(VGAScreen, x, y - 1, c - 2);
2745 JE_pix(VGAScreen, x, y + 1, c - 2);
2746 }