2 * Elite - The New Kind.
4 * Reverse engineered from the BBC disk version of Elite.
5 * Additional material by C.J.Pinder.
7 * The original Elite code is (C) I.Bell & D.Braben 1984.
8 * This version re-engineered in C by C.J.Pinder 1999-2001.
10 * email: <christian@newkind.co.uk>
18 * This module handles all the flight system and management of the space universe.
48 extern int flight_climb;
49 extern int flight_roll;
50 extern int flight_speed;
52 struct galaxy_seed destination_planet;
64 void rotate_x_first (double *a, double *b, int direction)
73 *a = fx - (fx / 512) + (ux / 19);
74 *b = ux - (ux / 512) - (fx / 19);
78 *a = fx - (fx / 512) - (ux / 19);
79 *b = ux - (ux / 512) + (fx / 19);
84 void rotate_vec (struct vector *vec, double alpha, double beta)
104 * Update an objects location in the universe.
107 void move_univ_object (struct univ_object *obj)
116 alpha = flight_roll / 256.0;
117 beta = flight_climb / 256.0;
123 if (!(obj->flags & FLG_DEAD))
125 if (obj->velocity != 0)
127 speed = obj->velocity;
129 x += obj->rotmat[2].x * speed;
130 y += obj->rotmat[2].y * speed;
131 z += obj->rotmat[2].z * speed;
134 if (obj->acceleration != 0)
136 obj->velocity += obj->acceleration;
137 obj->acceleration = 0;
138 if (obj->velocity > ship_list[obj->type]->velocity)
139 obj->velocity = ship_list[obj->type]->velocity;
141 if (obj->velocity <= 0)
151 z = z - flight_speed;
157 obj->distance = sqrt (x*x + y*y + z*z);
159 if (obj->type == SHIP_PLANET)
162 rotate_vec (&obj->rotmat[2], alpha, beta);
163 rotate_vec (&obj->rotmat[1], alpha, beta);
164 rotate_vec (&obj->rotmat[0], alpha, beta);
166 if (obj->flags & FLG_DEAD)
173 /* If necessary rotate the object around the X axis... */
177 rotate_x_first (&obj->rotmat[2].x, &obj->rotmat[1].x, rotx);
178 rotate_x_first (&obj->rotmat[2].y, &obj->rotmat[1].y, rotx);
179 rotate_x_first (&obj->rotmat[2].z, &obj->rotmat[1].z, rotx);
181 if ((rotx != 127) && (rotx != -127))
182 obj->rotx -= (rotx < 0) ? -1 : 1;
186 /* If necessary rotate the object around the Z axis... */
190 rotate_x_first (&obj->rotmat[0].x, &obj->rotmat[1].x, rotz);
191 rotate_x_first (&obj->rotmat[0].y, &obj->rotmat[1].y, rotz);
192 rotate_x_first (&obj->rotmat[0].z, &obj->rotmat[1].z, rotz);
194 if ((rotz != 127) && (rotz != -127))
195 obj->rotz -= (rotz < 0) ? -1 : 1;
199 /* Orthonormalize the rotation matrix... */
201 tidy_matrix (obj->rotmat);
206 * Dock the player into the space station.
209 void dock_player (void)
211 disengage_auto_pilot();
219 myship.altitude = 255;
226 * Check if we are correctly aligned to dock.
229 int is_docking (int sn)
235 if (universe[sn].flags & FLG_ANGRY)
238 if (auto_pilot) // Don't want it to kill anyone!
241 fz = universe[sn].rotmat[2].z;
246 vec = unit_vector (&universe[sn].location);
251 ux = universe[sn].rotmat[1].x;
266 void do_game_over (void)
268 snd_play_sample (SND_GAMEOVER);
273 void update_altitude (void)
278 myship.altitude = 255;
283 x = fabs(universe[0].location.x);
284 y = fabs(universe[0].location.y);
285 z = fabs(universe[0].location.z);
287 if ((x > 65535) || (y > 65535) || (z > 65535))
294 dist = (x * x) + (y * y) + (z * z);
315 myship.altitude = dist;
319 void update_cabin_temp (void)
329 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
332 x = abs((int)universe[1].location.x);
333 y = abs((int)universe[1].location.y);
334 z = abs((int)universe[1].location.z);
336 if ((x > 65535) || (y > 65535) || (z > 65535))
343 dist = ((x * x) + (y * y) + (z * z)) / 256;
350 myship.cabtemp = dist + 30;
352 if (myship.cabtemp > 255)
354 myship.cabtemp = 255;
359 if ((myship.cabtemp < 224) || (cmdr.fuel_scoop == 0))
362 cmdr.fuel += flight_speed / 2;
363 if (cmdr.fuel > myship.max_fuel)
364 cmdr.fuel = myship.max_fuel;
366 info_message ("Fuel Scoop On");
372 * Regenerate the shields and the energy banks.
375 void regenerate_shields (void)
379 if (front_shield < 255)
385 if (aft_shield < 255)
393 energy += cmdr.energy_unit;
399 void decrease_energy (int amount)
409 * Deplete the shields. Drain the energy banks if the shields fail.
412 void damage_ship (int damage, int front)
416 if (damage <= 0) /* sanity check */
419 shield = front ? front_shield : aft_shield;
424 decrease_energy (shield);
429 front_shield = shield;
437 void make_station_appear (void)
444 px = universe[0].location.x;
445 py = universe[0].location.y;
446 pz = universe[0].location.z;
448 vec.x = (rand() & 32767) - 16384;
449 vec.y = (rand() & 32767) - 16384;
450 vec.z = rand() & 32767;
452 vec = unit_vector (&vec);
454 sx = px - vec.x * 65792;
455 sy = py - vec.y * 65792;
456 sz = pz - vec.z * 65792;
458 // set_init_matrix (rotmat);
466 rotmat[1].z = -vec.y;
472 tidy_matrix (rotmat);
474 add_new_station (sx, sy, sz, rotmat);
479 void check_docking (int i)
483 snd_play_sample (SND_DOCK);
485 current_screen = SCR_BREAK_PATTERN;
489 if (flight_speed >= 5)
496 damage_ship (5, universe[i].location.z > 0);
497 snd_play_sample (SND_CRASH);
501 void switch_to_view (struct univ_object *flip)
505 if ((current_screen == SCR_REAR_VIEW) ||
506 (current_screen == SCR_GAME_OVER))
508 flip->location.x = -flip->location.x;
509 flip->location.z = -flip->location.z;
511 flip->rotmat[0].x = -flip->rotmat[0].x;
512 flip->rotmat[0].z = -flip->rotmat[0].z;
514 flip->rotmat[1].x = -flip->rotmat[1].x;
515 flip->rotmat[1].z = -flip->rotmat[1].z;
517 flip->rotmat[2].x = -flip->rotmat[2].x;
518 flip->rotmat[2].z = -flip->rotmat[2].z;
523 if (current_screen == SCR_LEFT_VIEW)
525 tmp = flip->location.x;
526 flip->location.x = flip->location.z;
527 flip->location.z = -tmp;
532 tmp = flip->rotmat[0].x;
533 flip->rotmat[0].x = flip->rotmat[0].z;
534 flip->rotmat[0].z = -tmp;
536 tmp = flip->rotmat[1].x;
537 flip->rotmat[1].x = flip->rotmat[1].z;
538 flip->rotmat[1].z = -tmp;
540 tmp = flip->rotmat[2].x;
541 flip->rotmat[2].x = flip->rotmat[2].z;
542 flip->rotmat[2].z = -tmp;
546 if (current_screen == SCR_RIGHT_VIEW)
548 tmp = flip->location.x;
549 flip->location.x = -flip->location.z;
550 flip->location.z = tmp;
555 tmp = flip->rotmat[0].x;
556 flip->rotmat[0].x = -flip->rotmat[0].z;
557 flip->rotmat[0].z = tmp;
559 tmp = flip->rotmat[1].x;
560 flip->rotmat[1].x = -flip->rotmat[1].z;
561 flip->rotmat[1].z = tmp;
563 tmp = flip->rotmat[2].x;
564 flip->rotmat[2].x = -flip->rotmat[2].z;
565 flip->rotmat[2].z = tmp;
572 * Update all the objects in the universe and render them.
575 void update_universe (void)
581 struct univ_object flip;
586 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
588 type = universe[i].type;
592 if (universe[i].flags & FLG_REMOVE)
594 if (!(universe[i].flags & FLG_TARGET))
595 cmdr.legal_status |= 64;
597 bounty = ship_list[type]->bounty;
599 if ((bounty != 0) && (!witchspace))
601 cmdr.credits += bounty;
602 sprintf (str, "Bounty: %d.%d CR", bounty / 10, bounty % 10);
610 if ((detonate_bomb) && ((universe[i].flags & FLG_DEAD) == 0) &&
611 (type != SHIP_PLANET) && (type != SHIP_SUN) &&
612 (type != SHIP_CONSTRICTOR) && (type != SHIP_COUGAR) &&
613 (type != SHIP_CORIOLIS) && (type != SHIP_DODEC))
615 snd_play_sample (SND_EXPLODE);
616 universe[i].flags |= FLG_DEAD;
619 if ((current_screen != SCR_INTRO_ONE) &&
620 (current_screen != SCR_INTRO_TWO) &&
621 (current_screen != SCR_GAME_OVER) &&
622 (current_screen != SCR_ESCAPE_POD))
627 move_univ_object (&universe[i]);
630 switch_to_view (&flip);
632 if (type == SHIP_PLANET)
634 if ((ship_count[SHIP_CORIOLIS] == 0) &&
635 (ship_count[SHIP_DODEC] == 0) &&
636 (universe[i].distance < 65792)) // was 49152
638 make_station_appear();
645 if (type == SHIP_SUN)
652 if (universe[i].distance < 170)
654 if ((type == SHIP_CORIOLIS) || (type == SHIP_DODEC))
662 if (universe[i].distance > 57344)
670 universe[i].flags = flip.flags;
671 universe[i].exp_seed = flip.exp_seed;
672 universe[i].exp_delta = flip.exp_delta;
674 universe[i].flags &= ~FLG_FIRING;
676 if (universe[i].flags & FLG_DEAD)
679 check_target (i, &flip);
691 * Update the scanner and draw all the lollipops.
694 void update_scanner (void)
701 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
703 if ((universe[i].type <= 0) ||
704 (universe[i].flags & FLG_DEAD) ||
705 (universe[i].flags & FLG_CLOAKED))
708 x = universe[i].location.x * scanner_zoom / 256;
709 y = universe[i].location.y * scanner_zoom / 256;
710 z = universe[i].location.z * scanner_zoom / 256;
716 if ((y2 < -28) || (y2 > 28) ||
717 (x1 < -50) || (x1 > 50))
724 colour = (universe[i].flags & FLG_HOSTILE) ? GFX_COL_YELLOW_5 : GFX_COL_WHITE;
726 switch (universe[i].type)
734 colour = GFX_COL_GREEN_1;
742 gfx_draw_colour_line (x1+2, y2, x1-3, y2, colour);
743 gfx_draw_colour_line (x1+2, y2+1, x1-3, y2+1, colour);
744 gfx_draw_colour_line (x1+2, y2+2, x1-3, y2+2, colour);
745 gfx_draw_colour_line (x1+2, y2+3, x1-3, y2+3, colour);
748 gfx_draw_colour_line (x1, y1, x1, y2, colour);
749 gfx_draw_colour_line (x1+1, y1, x1+1, y2, colour);
750 gfx_draw_colour_line (x1+2, y1, x1+2, y2, colour);
756 * Update the compass which tracks the space station / planet.
759 void update_compass (void)
769 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
772 dest = unit_vector (&universe[un].location);
774 compass_x = compass_centre_x + (dest.x * 16);
775 compass_y = compass_centre_y + (dest.y * -16);
779 gfx_draw_sprite (IMG_RED_DOT, compass_x, compass_y);
783 gfx_draw_sprite (IMG_GREEN_DOT, compass_x, compass_y);
790 * Display the speed bar.
793 void display_speed (void)
803 len = ((flight_speed * 64) / myship.max_speed) - 1;
805 colour = (flight_speed > (myship.max_speed * 2 / 3)) ? GFX_COL_DARK_RED : GFX_COL_GOLD;
807 for (i = 0; i < 6; i++)
809 gfx_draw_colour_line (sx, sy + i, sx + len, sy + i, colour);
815 * Draw an indicator bar.
816 * Used for shields and energy banks.
819 void display_dial_bar (int len, int x, int y)
823 gfx_draw_colour_line (x, y + 384, x + len, y + 384, GFX_COL_GOLD);
825 gfx_draw_colour_line (x, y + i + 384, x + len, y + i + 384, GFX_COL_GOLD);
827 for (i = 2; i < 7; i++)
828 gfx_draw_colour_line (x, y + i + 384, x + len, y + i + 384, GFX_COL_YELLOW_1);
830 gfx_draw_colour_line (x, y + i + 384, x + len, y + i + 384, GFX_COL_DARK_RED);
835 * Display the current shield strengths.
838 void display_shields (void)
840 if (front_shield > 3)
841 display_dial_bar (front_shield / 4, 31, 7);
844 display_dial_bar (aft_shield / 4, 31, 23);
848 void display_altitude (void)
850 if (myship.altitude > 3)
851 display_dial_bar (myship.altitude / 4, 31, 92);
854 void display_cabin_temp (void)
856 if (myship.cabtemp > 3)
857 display_dial_bar (myship.cabtemp / 4, 31, 60);
861 void display_laser_temp (void)
864 display_dial_bar (laser_temp / 4, 31, 76);
869 * Display the energy banks.
872 void display_energy (void)
876 e1 = energy > 64 ? 64 : energy;
877 e2 = energy > 128 ? 64 : energy - 64;
878 e3 = energy > 192 ? 64 : energy - 128;
882 display_dial_bar (e4, 416, 61);
885 display_dial_bar (e3, 416, 79);
888 display_dial_bar (e2, 416, 97);
891 display_dial_bar (e1, 416, 115);
896 void display_flight_roll (void)
905 pos = sx - ((flight_roll * 28) / myship.max_roll);
908 for (i = 0; i < 4; i++)
910 gfx_draw_colour_line (pos + i, sy, pos + i, sy + 7, GFX_COL_GOLD);
914 void display_flight_climb (void)
921 sy = 384 + 9 + 14 + 16;
923 pos = sx + ((flight_climb * 28) / myship.max_climb);
926 for (i = 0; i < 4; i++)
928 gfx_draw_colour_line (pos + i, sy, pos + i, sy + 7, GFX_COL_GOLD);
933 void display_fuel (void)
936 display_dial_bar ((cmdr.fuel * 64) / myship.max_fuel, 31, 44);
940 void display_missiles (void)
945 if (cmdr.missiles == 0)
948 nomiss = cmdr.missiles > 4 ? 4 : cmdr.missiles;
950 x = (4 - nomiss) * 16 + 35;
953 if (missile_target != MISSILE_UNARMED)
955 gfx_draw_sprite ((missile_target < 0) ? IMG_MISSILE_YELLOW :
956 IMG_MISSILE_RED, x, y);
961 for (; nomiss > 0; nomiss--)
963 gfx_draw_sprite (IMG_MISSILE_GREEN, x, y);
970 void display_condition(void)
972 static const int cc[] = { GFX_COL_BLACK, GFX_COL_GREEN_1, GFX_COL_YELLOW_1,
973 GFX_COL_RED, GFX_COL_RED };
974 int c = cc[condition];
975 if (condition == COND_ALERT && (mcount & 4))
977 gfx_draw_filled_circle(condition_x, condition_y, condition_r, c);
980 void update_console (void)
985 display_flight_climb();
986 display_flight_roll();
990 display_cabin_temp();
991 display_laser_temp();
997 gfx_set_clip_region (0, 0, 512, 512);
1003 display_condition();
1007 sprintf(buf, "x%d", scanner_zoom);
1008 gfx_display_text(zoom_x, zoom_y, buf);
1011 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
1012 gfx_draw_sprite (IMG_BIG_S, 387, 490);
1015 gfx_draw_sprite (IMG_BIG_E, 115, 490);
1017 gfx_set_clip_region (0, 0, 512, 512);
1020 void increase_flight_roll (void)
1022 if (flight_roll < myship.max_roll)
1027 void decrease_flight_roll (void)
1029 if (flight_roll > -myship.max_roll)
1034 void increase_flight_climb (void)
1036 if (flight_climb < myship.max_climb)
1040 void decrease_flight_climb (void)
1042 if (flight_climb > -myship.max_climb)
1047 void start_hyperspace (void)
1052 hyper_distance = calc_distance_to_planet (docked_planet, hyperspace_planet);
1054 if ((docked_planet.a == hyperspace_planet.a &&
1055 docked_planet.b == hyperspace_planet.b &&
1056 docked_planet.c == hyperspace_planet.c &&
1057 docked_planet.d == hyperspace_planet.d &&
1058 docked_planet.e == hyperspace_planet.e &&
1059 docked_planet.f == hyperspace_planet.f) ||
1060 (hyper_distance > cmdr.fuel))
1063 destination_planet = hyperspace_planet;
1064 name_planet (hyper_name, destination_planet);
1065 capitalise_name (hyper_name);
1068 hyper_countdown = 15;
1071 disengage_auto_pilot();
1074 void start_galactic_hyperspace (void)
1079 if (cmdr.galactic_hyperdrive == 0)
1083 hyper_countdown = 2;
1085 disengage_auto_pilot();
1090 void display_hyper_status (void)
1094 sprintf (str, "%d", hyper_countdown);
1096 if ((current_screen == SCR_FRONT_VIEW) || (current_screen == SCR_REAR_VIEW) ||
1097 (current_screen == SCR_LEFT_VIEW) || (current_screen == SCR_RIGHT_VIEW))
1099 gfx_display_text (5, 5, str);
1102 gfx_display_centre_text (358, "Galactic Hyperspace", 120, GFX_COL_WHITE);
1106 sprintf (str, "Hyperspace - %s", hyper_name);
1107 gfx_display_centre_text (358, str, 120, GFX_COL_WHITE);
1112 gfx_clear_area (5, 5, 25, 34);
1113 gfx_display_text (5, 5, str);
1118 int rotate_byte_left (int x)
1120 return ((x << 1) | (x >> 7)) & 255;
1123 void enter_next_galaxy (void)
1125 cmdr.galaxy_number++;
1126 cmdr.galaxy_number &= 7;
1128 cmdr.galaxy.a = rotate_byte_left (cmdr.galaxy.a);
1129 cmdr.galaxy.b = rotate_byte_left (cmdr.galaxy.b);
1130 cmdr.galaxy.c = rotate_byte_left (cmdr.galaxy.c);
1131 cmdr.galaxy.d = rotate_byte_left (cmdr.galaxy.d);
1132 cmdr.galaxy.e = rotate_byte_left (cmdr.galaxy.e);
1133 cmdr.galaxy.f = rotate_byte_left (cmdr.galaxy.f);
1135 docked_planet = find_planet (0x60, 0x60);
1136 hyperspace_planet = docked_planet;
1143 void enter_witchspace (void)
1149 docked_planet.b ^= 31;
1158 nthg = (randint() & 3) + 1;
1160 for (i = 0; i < nthg; i++)
1163 current_screen = SCR_BREAK_PATTERN;
1164 snd_play_sample (SND_HYPERSPACE);
1168 void complete_hyperspace (void)
1178 cmdr.galactic_hyperdrive = 0;
1179 enter_next_galaxy();
1180 cmdr.legal_status = 0;
1184 cmdr.fuel -= hyper_distance;
1185 cmdr.legal_status /= 2;
1187 if ((rand255() > 253) || (flight_climb == myship.max_climb))
1193 docked_planet = destination_planet;
1196 cmdr.market_rnd = rand255();
1197 generate_planet_data (¤t_planet_data, docked_planet);
1198 generate_stock_market ();
1206 generate_landscape(docked_planet.a * 251 + docked_planet.b);
1207 set_init_matrix (rotmat);
1209 pz = (((docked_planet.b) & 7) + 7) / 2;
1217 if ((docked_planet.b & 1) == 0)
1223 add_new_ship (SHIP_PLANET, px, py, pz, rotmat, 0, 0);
1226 pz = -(((docked_planet.d & 7) | 1) << 16);
1227 px = ((docked_planet.f & 3) << 16) | ((docked_planet.f & 3) << 8);
1229 add_new_ship (SHIP_SUN, px, py, pz, rotmat, 0, 0);
1231 current_screen = SCR_BREAK_PATTERN;
1232 snd_play_sample (SND_HYPERSPACE);
1236 void countdown_hyperspace (void)
1238 if (hyper_countdown == 0)
1240 complete_hyperspace();
1249 void jump_warp (void)
1255 if (!kbd_ctrl_pressed) {
1256 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
1258 type = universe[i].type;
1260 if ((type > 0) && (type != SHIP_ASTEROID) && (type != SHIP_CARGO) &&
1261 (type != SHIP_ALLOY) && (type != SHIP_ROCK) &&
1262 (type != SHIP_BOULDER) && (type != SHIP_ESCAPE_CAPSULE))
1264 info_message ("Mass Locked");
1268 if ((universe[0].distance < 75001) || (universe[1].distance < 75001))
1270 info_message ("Mass Locked");
1277 if (universe[0].distance < universe[1].distance)
1278 jump = universe[0].distance - 75000;
1280 jump = universe[1].distance - 75000;
1282 if (jump > 1024 || kbd_ctrl_pressed)
1285 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
1287 if (universe[i].type != 0)
1288 universe[i].location.z -= jump;
1292 if (!kbd_ctrl_pressed) {
1299 void launch_player (void)
1307 cmdr.legal_status |= carrying_contraband();
1310 generate_landscape(docked_planet.a * 251 + docked_planet.b);
1311 set_init_matrix (rotmat);
1312 add_new_ship (SHIP_PLANET, 0, 0, 65536, rotmat, 0, 0);
1318 rotmat[2].x = -rotmat[2].x;
1319 rotmat[2].y = -rotmat[2].y;
1320 rotmat[2].z = -rotmat[2].z;
1321 add_new_station (0, 0, -256, rotmat);
1323 current_screen = SCR_BREAK_PATTERN;
1324 snd_play_sample (SND_LAUNCH);
1330 * Engage the docking computer.
1331 * For the moment we just do an instant dock if we are in the safe zone.
1334 void engage_docking_computer (void)
1336 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
1338 snd_play_sample (SND_DOCK);
1340 current_screen = SCR_BREAK_PATTERN;
1345 void update_condition(void)
1348 condition = COND_DOCKED;
1349 else if (energy < 50 || myship.altitude < 32 || myship.cabtemp > 224)
1350 condition = COND_ALERT;
1351 else if (energy < 128 || myship.altitude < 64 || myship.cabtemp > 192)
1352 condition = COND_RED;
1355 condition = COND_GREEN;
1356 if (myship.altitude < 128 || myship.cabtemp >= 128)
1357 condition = COND_YELLOW;
1358 for (i = 0; i < MAX_UNIV_OBJECTS; i++) {
1359 struct univ_object *un = &universe[i];
1362 if (un->flags & FLG_HOSTILE) {
1363 condition = COND_RED;
1366 if (condition == COND_GREEN &&
1367 un->type != SHIP_ASTEROID && un->type != SHIP_CARGO &&
1368 un->type != SHIP_ALLOY && un->type != SHIP_ROCK &&
1369 un->type != SHIP_BOULDER && un->type != SHIP_ESCAPE_CAPSULE &&
1370 un->type != SHIP_CORIOLIS && un->type != SHIP_DODEC)
1371 condition = COND_YELLOW;