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 * Allegro version of the main game handler.
55 int old_cross_x, old_cross_y;
61 char message_string[80];
73 * Initialise the game parameters.
76 void initialise_game(void)
78 set_rand_seed (time(NULL));
79 current_screen = SCR_INTRO_ONE;
81 restore_saved_commander();
107 myship.max_speed = 40; /* 0.27 Light Mach */
108 myship.max_roll = 31;
109 myship.max_climb = 8; /* CF 8 */
110 myship.max_fuel = 70; /* 7.0 Light Years */
114 void finish_game (void)
127 * Move the planet chart cross hairs to specified position.
131 void move_cross (int dx, int dy)
135 if (current_screen == SCR_SHORT_RANGE)
142 if (current_screen == SCR_GALACTIC_CHART)
163 * Draw the cross hairs at the specified position.
166 void draw_cross (int cx, int cy)
168 if (current_screen == SCR_SHORT_RANGE)
170 gfx_set_clip_region (1, 37, 510, 339);
172 gfx_draw_colour_line (cx - 16, cy, cx + 16, cy, GFX_COL_RED);
173 gfx_draw_colour_line (cx, cy - 16, cx, cy + 16, GFX_COL_RED);
175 gfx_set_clip_region (1, 1, 510, 383);
179 if (current_screen == SCR_GALACTIC_CHART)
181 gfx_set_clip_region (1, 37, 510, 293);
183 gfx_draw_colour_line (cx - 8, cy, cx + 8, cy, GFX_COL_RED);
184 gfx_draw_colour_line (cx, cy - 8, cx, cy + 8, GFX_COL_RED);
186 gfx_set_clip_region (1, 1, 510, 383);
192 void draw_laser_sights(void)
197 switch (current_screen)
200 gfx_display_centre_text (32, "Front View", 120, GFX_COL_WHITE);
201 laser = cmdr.front_laser;
205 gfx_display_centre_text (32, "Rear View", 120, GFX_COL_WHITE);
206 laser = cmdr.rear_laser;
210 gfx_display_centre_text (32, "Left View", 120, GFX_COL_WHITE);
211 laser = cmdr.left_laser;
215 gfx_display_centre_text (32, "Right View", 120, GFX_COL_WHITE);
216 laser = cmdr.right_laser;
223 x1 = 128 * GFX_SCALE;
224 y1 = (96-8) * GFX_SCALE;
225 y2 = (96-16) * GFX_SCALE;
227 gfx_draw_colour_line (x1-1, y1, x1-1, y2, GFX_COL_GREY_1);
228 gfx_draw_colour_line (x1, y1, x1, y2, GFX_COL_WHITE);
229 gfx_draw_colour_line (x1+1, y1, x1+1, y2, GFX_COL_GREY_1);
231 y1 = (96+8) * GFX_SCALE;
232 y2 = (96+16) * GFX_SCALE;
234 gfx_draw_colour_line (x1-1, y1, x1-1, y2, GFX_COL_GREY_1);
235 gfx_draw_colour_line (x1, y1, x1, y2, GFX_COL_WHITE);
236 gfx_draw_colour_line (x1+1, y1, x1+1, y2, GFX_COL_GREY_1);
238 x1 = (128-8) * GFX_SCALE;
240 x2 = (128-16) * GFX_SCALE;
242 gfx_draw_colour_line (x1, y1-1, x2, y1-1, GFX_COL_GREY_1);
243 gfx_draw_colour_line (x1, y1, x2, y1, GFX_COL_WHITE);
244 gfx_draw_colour_line (x1, y1+1, x2, y1+1, GFX_COL_GREY_1);
246 x1 = (128+8) * GFX_SCALE;
247 x2 = (128+16) * GFX_SCALE;
249 gfx_draw_colour_line (x1, y1-1, x2, y1-1, GFX_COL_GREY_1);
250 gfx_draw_colour_line (x1, y1, x2, y1, GFX_COL_WHITE);
251 gfx_draw_colour_line (x1, y1+1, x2, y1+1, GFX_COL_GREY_1);
256 void arrow_right (void)
258 switch (current_screen)
260 case SCR_MARKET_PRICES:
265 select_right_setting();
268 case SCR_SHORT_RANGE:
269 case SCR_GALACTIC_CHART:
281 decrease_flight_roll();
282 decrease_flight_roll();
290 void arrow_left (void)
292 switch (current_screen)
294 case SCR_MARKET_PRICES:
299 select_left_setting();
302 case SCR_SHORT_RANGE:
303 case SCR_GALACTIC_CHART:
315 increase_flight_roll();
316 increase_flight_roll();
326 switch (current_screen)
328 case SCR_MARKET_PRICES:
329 select_previous_stock();
333 select_previous_equip();
337 select_previous_option();
344 case SCR_SHORT_RANGE:
345 case SCR_GALACTIC_CHART:
353 if (flight_climb > 0)
357 decrease_flight_climb();
366 void arrow_down (void)
368 switch (current_screen)
370 case SCR_MARKET_PRICES:
379 select_next_option();
383 select_down_setting();
386 case SCR_SHORT_RANGE:
387 case SCR_GALACTIC_CHART:
395 if (flight_climb < 0)
399 increase_flight_climb();
408 void return_pressed (void)
410 switch (current_screen)
427 void y_pressed (void)
429 switch (current_screen)
438 void n_pressed (void)
440 switch (current_screen)
444 display_commander_status();
446 current_screen = SCR_FRONT_VIEW;
452 void d_pressed (void)
454 switch (current_screen)
456 case SCR_GALACTIC_CHART:
457 case SCR_SHORT_RANGE:
458 show_distance_to_planet();
466 disengage_auto_pilot();
472 void f_pressed (void)
474 if ((current_screen == SCR_GALACTIC_CHART) ||
475 (current_screen == SCR_SHORT_RANGE))
479 gfx_clear_text_area();
480 gfx_display_text (16, 340, "Planet Name?");
485 void add_find_char (int letter)
489 if (strlen (find_name) == 16)
492 str[0] = toupper (letter);
494 strcat (find_name, str);
496 sprintf (str, "Planet Name? %s", find_name);
497 gfx_clear_text_area ();
498 gfx_display_text(16, 340, str);
502 void delete_find_char (void)
507 len = strlen (find_name);
511 find_name[len - 1] = '\0';
513 sprintf (str, "Planet Name? %s", find_name);
514 gfx_clear_text_area();
515 gfx_display_text(16, 340, str);
520 switch (current_screen)
522 case SCR_GALACTIC_CHART:
523 case SCR_SHORT_RANGE:
524 move_cursor_to_origin();
530 void auto_dock (void)
532 struct univ_object ship;
538 set_init_matrix (ship.rotmat);
539 ship.rotmat[2].z = 1;
540 ship.rotmat[0].x = -1;
542 ship.velocity = flight_speed;
543 ship.acceleration = 0;
548 auto_pilot_ship (&ship);
550 if (ship.velocity > 22)
553 flight_speed = ship.velocity;
555 if (ship.acceleration > 0)
558 if (flight_speed > 22)
562 if (ship.acceleration < 0)
565 if (flight_speed < 1)
574 increase_flight_climb();
577 increase_flight_climb();
582 decrease_flight_climb();
585 decrease_flight_climb();
588 if (ship.rotz == 127)
597 increase_flight_roll();
600 increase_flight_roll();
605 decrease_flight_roll();
608 decrease_flight_roll();
614 void run_escape_sequence (void)
620 current_screen = SCR_ESCAPE_POD;
626 set_init_matrix (rotmat);
629 newship = add_new_ship (SHIP_COBRA3, 0, 0, 200, rotmat, -127, -127);
630 universe[newship].velocity = 7;
631 snd_play_sample (SND_LAUNCH);
633 for (i = 0; i < 90; i++)
637 universe[newship].flags |= FLG_DEAD;
638 snd_play_sample (SND_EXPLODE);
641 gfx_set_clip_region (1, 1, 510, 383);
646 universe[newship].location.x = 0;
647 universe[newship].location.y = 0;
648 universe[newship].location.z += 2;
650 gfx_display_centre_text (358, "Escape pod launched - Ship auto-destuct initiated.", 120, GFX_COL_WHITE);
657 while ((ship_count[SHIP_CORIOLIS] == 0) &&
658 (ship_count[SHIP_DODEC] == 0))
662 if ((abs(flight_roll) < 3) && (abs(flight_climb) < 3))
664 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
666 if (universe[i].type != 0)
667 universe[i].location.z -= 1500;
673 gfx_set_clip_region (1, 1, 510, 383);
685 void handle_flight_keys (void)
690 ((current_screen == SCR_MARKET_PRICES) ||
691 (current_screen == SCR_OPTIONS) ||
692 (current_screen == SCR_SETTINGS) ||
693 (current_screen == SCR_EQUIP_SHIP)))
702 if (joy[0].stick[0].axis[1].d1)
705 if (joy[0].stick[0].axis[1].d2)
708 if (joy[0].stick[0].axis[0].d1)
711 if (joy[0].stick[0].axis[0].d2)
714 if (joy[0].button[0].b)
715 kbd_fire_pressed = 1;
717 if (joy[0].button[1].b)
718 kbd_inc_speed_pressed = 1;
720 if (joy[0].button[2].b)
721 kbd_dec_speed_pressed = 1;
727 if (kbd_resume_pressed)
740 if (current_screen != SCR_FRONT_VIEW)
742 current_screen = SCR_FRONT_VIEW;
754 if (current_screen != SCR_REAR_VIEW)
756 current_screen = SCR_REAR_VIEW;
768 if (current_screen != SCR_LEFT_VIEW)
770 current_screen = SCR_LEFT_VIEW;
784 if (current_screen != SCR_RIGHT_VIEW)
786 current_screen = SCR_RIGHT_VIEW;
797 display_galactic_chart();
804 display_short_range_chart();
810 display_data_on_planet();
813 if (kbd_F8_pressed && (!witchspace))
816 display_market_prices();
822 display_commander_status();
839 keyasc = kbd_read_key();
841 if (kbd_enter_pressed)
844 find_planet_by_name (find_name);
848 if (kbd_backspace_pressed)
855 add_find_char (keyasc);
867 if (kbd_fire_pressed)
869 if ((!docked) && (draw_lasers == 0))
870 draw_lasers = fire_laser();
873 if (kbd_dock_pressed)
875 if (!docked && cmdr.docking_computer)
878 engage_docking_computer();
889 if (!docked && cmdr.ecm)
893 if (kbd_find_pressed)
896 if (kbd_hyperspace_pressed && (!docked))
898 if (kbd_ctrl_pressed)
899 start_galactic_hyperspace();
904 if (kbd_jump_pressed && (!docked) && (!witchspace))
909 if (kbd_fire_missile_pressed)
915 if (kbd_origin_pressed)
918 if (kbd_pause_pressed)
921 if (kbd_target_missile_pressed)
927 if (kbd_unarm_missile_pressed)
933 if (kbd_inc_speed_pressed)
937 if (flight_speed < myship.max_speed)
942 if (kbd_dec_speed_pressed)
946 if (flight_speed > 1)
954 if (kbd_down_pressed)
957 if (kbd_left_pressed)
960 if (kbd_right_pressed)
963 if (kbd_enter_pressed)
966 if (kbd_energy_bomb_pressed)
968 if ((!docked) && (cmdr.energy_bomb))
971 cmdr.energy_bomb = 0;
975 if (kbd_escape_pressed)
977 if ((!docked) && (cmdr.escape_pod) && (!witchspace))
978 run_escape_sequence();
984 void set_commander_name (char *path)
989 fname = get_filename (path);
992 for (i = 0; i < 31; i++)
994 if (!isalnum(*fname))
997 *cname++ = toupper(*fname++);
1004 void save_commander_screen (void)
1010 current_screen = SCR_SAVE_CMDR;
1012 gfx_clear_display();
1013 gfx_display_centre_text (10, "SAVE COMMANDER", 140, GFX_COL_GOLD);
1014 gfx_draw_line (0, 36, 511, 36);
1015 gfx_update_screen();
1017 strcpy (path, cmdr.name);
1018 strcat (path, ".nkc");
1020 okay = gfx_request_file ("Save Commander", path, "nkc");
1028 rv = save_commander_file (path);
1032 gfx_display_centre_text (175, "Error Saving Commander!", 140, GFX_COL_GOLD);
1036 gfx_display_centre_text (175, "Commander Saved.", 140, GFX_COL_GOLD);
1038 set_commander_name (path);
1040 saved_cmdr.ship_x = docked_planet.d;
1041 saved_cmdr.ship_y = docked_planet.b;
1045 void load_commander_screen (void)
1050 gfx_clear_display();
1051 gfx_display_centre_text (10, "LOAD COMMANDER", 140, GFX_COL_GOLD);
1052 gfx_draw_line (0, 36, 511, 36);
1053 gfx_update_screen();
1056 strcpy (path, "jameson.nkc");
1058 rv = gfx_request_file ("Load Commander", path, "nkc");
1063 rv = load_commander_file (path);
1068 gfx_display_centre_text (175, "Error Loading Commander!", 140, GFX_COL_GOLD);
1069 gfx_display_centre_text (200, "Press any key to continue.", 140, GFX_COL_GOLD);
1070 gfx_update_screen();
1075 restore_saved_commander();
1076 set_commander_name (path);
1083 void run_first_intro_screen (void)
1085 current_screen = SCR_INTRO_ONE;
1087 snd_play_midi (SND_ELITE_THEME, TRUE);
1089 initialise_intro1();
1095 gfx_update_screen();
1097 kbd_poll_keyboard();
1102 load_commander_screen();
1117 void run_second_intro_screen (void)
1119 current_screen = SCR_INTRO_TWO;
1121 snd_play_midi (SND_BLUE_DANUBE, TRUE);
1123 initialise_intro2();
1133 gfx_update_screen();
1135 kbd_poll_keyboard();
1137 if (kbd_space_pressed)
1147 * Draw the game over sequence.
1150 void run_game_over_screen()
1157 current_screen = SCR_GAME_OVER;
1158 gfx_set_clip_region (1, 1, 510, 383);
1165 set_init_matrix (rotmat);
1167 newship = add_new_ship (SHIP_COBRA3, 0, 0, -400, rotmat, 0, 0);
1168 universe[newship].flags |= FLG_DEAD;
1170 for (i = 0; i < 5; i++)
1172 type = (rand255() & 1) ? SHIP_CARGO : SHIP_ALLOY;
1173 newship = add_new_ship (type, (rand255() & 63) - 32,
1174 (rand255() & 63) - 32, -400, rotmat, 0, 0);
1175 universe[newship].rotz = ((rand255() * 2) & 255) - 128;
1176 universe[newship].rotx = ((rand255() * 2) & 255) - 128;
1177 universe[newship].velocity = rand255() & 15;
1181 for (i = 0; i < 100; i++)
1183 gfx_clear_display();
1186 gfx_display_centre_text (190, "GAME OVER", 140, GFX_COL_GOLD);
1187 gfx_update_screen();
1195 * Draw a break pattern (for launching, docking and hyperspacing).
1196 * Just draw a very simple one for the moment.
1199 void display_break_pattern (void)
1203 gfx_set_clip_region (1, 1, 510, 383);
1204 gfx_clear_display();
1206 for (i = 0; i < 20; i++)
1208 gfx_draw_circle (256, 192, 30 + i * 15, GFX_COL_WHITE);
1209 gfx_update_screen();
1215 check_mission_brief();
1216 display_commander_status();
1220 current_screen = SCR_FRONT_VIEW;
1224 void info_message (char *message)
1226 strcpy (message_string, message);
1228 // snd_play_sample (SND_BEEP);
1236 void initialise_allegro (void)
1245 if (install_joystick(JOY_TYPE_AUTODETECT) == 0)
1247 have_joystick = (num_joysticks > 0);
1255 initialise_allegro();
1258 if (gfx_graphics_startup() == 1)
1263 /* Start the sound system... */
1264 snd_sound_startup();
1266 /* Do any setup necessary for the keyboard... */
1267 kbd_keyboard_startup();
1280 current_screen = SCR_FRONT_VIEW;
1281 run_first_intro_screen();
1282 run_second_intro_screen();
1288 display_commander_status ();
1293 gfx_update_screen();
1294 gfx_set_clip_region (1, 1, 510, 383);
1299 handle_flight_keys ();
1304 if (message_count > 0)
1309 if (flight_roll > 0)
1310 decrease_flight_roll();
1312 if (flight_roll < 0)
1313 increase_flight_roll();
1318 if (flight_climb > 0)
1319 decrease_flight_climb();
1321 if (flight_climb < 0)
1322 increase_flight_climb();
1328 gfx_acquire_screen();
1330 if ((current_screen == SCR_FRONT_VIEW) || (current_screen == SCR_REAR_VIEW) ||
1331 (current_screen == SCR_LEFT_VIEW) || (current_screen == SCR_RIGHT_VIEW) ||
1332 (current_screen == SCR_INTRO_ONE) || (current_screen == SCR_INTRO_TWO) ||
1333 (current_screen == SCR_GAME_OVER))
1335 gfx_clear_display();
1342 if ((mcount & 127) == 0)
1343 info_message ("Docking Computers On");
1351 gfx_release_screen();
1355 if ((current_screen == SCR_FRONT_VIEW) || (current_screen == SCR_REAR_VIEW) ||
1356 (current_screen == SCR_LEFT_VIEW) || (current_screen == SCR_RIGHT_VIEW))
1364 draw_laser_sights();
1367 if (message_count > 0)
1368 gfx_display_centre_text (358, message_string, 120, GFX_COL_WHITE);
1372 display_hyper_status();
1373 if ((mcount & 3) == 0)
1375 countdown_hyperspace();
1379 gfx_release_screen();
1385 if ((mcount & 7) == 0)
1386 regenerate_shields();
1388 if ((mcount & 31) == 10)
1392 info_message ("ENERGY LOW");
1393 snd_play_sample (SND_BEEP);
1399 if ((mcount & 31) == 20)
1400 update_cabin_temp();
1402 if ((mcount == 0) && (!witchspace))
1411 if (current_screen == SCR_BREAK_PATTERN)
1412 display_break_pattern();
1414 if (cross_timer > 0)
1417 if (cross_timer == 0)
1419 show_distance_to_planet();
1423 if ((cross_x != old_cross_x) ||
1424 (cross_y != old_cross_y))
1426 if (old_cross_x != -1)
1427 draw_cross (old_cross_x, old_cross_y);
1429 old_cross_x = cross_x;
1430 old_cross_y = cross_y;
1432 draw_cross (old_cross_x, old_cross_y);
1437 run_game_over_screen();
1440 snd_sound_shutdown();
1442 gfx_graphics_shutdown ();