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];
75 * Initialise the game parameters.
78 void initialise_game(void)
80 set_rand_seed (time(NULL
));
81 current_screen
= SCR_INTRO_ONE
;
83 restore_saved_commander();
109 myship
.max_speed
= 40; /* 0.27 Light Mach */
110 myship
.max_roll
= 31;
111 myship
.max_climb
= 8; /* CF 8 */
112 myship
.max_fuel
= 70; /* 7.0 Light Years */
116 void finish_game (void)
129 * Move the planet chart cross hairs to specified position.
133 void move_cross (int dx
, int dy
)
137 if (kbd_ctrl_pressed
) {
142 if (current_screen
== SCR_SHORT_RANGE
)
149 if (current_screen
== SCR_GALACTIC_CHART
)
170 * Draw the cross hairs at the specified position.
173 void draw_cross (int cx
, int cy
)
175 if (current_screen
== SCR_SHORT_RANGE
)
177 gfx_set_clip_region (1, 37, 510, 339);
179 gfx_draw_colour_line (cx
- 16, cy
, cx
+ 16, cy
, GFX_COL_RED
);
180 gfx_draw_colour_line (cx
, cy
- 16, cx
, cy
+ 16, GFX_COL_RED
);
182 gfx_set_clip_region (1, 1, 510, 383);
186 if (current_screen
== SCR_GALACTIC_CHART
)
188 gfx_set_clip_region (1, 37, 510, 293);
190 gfx_draw_colour_line (cx
- 8, cy
, cx
+ 8, cy
, GFX_COL_RED
);
191 gfx_draw_colour_line (cx
, cy
- 8, cx
, cy
+ 8, GFX_COL_RED
);
193 gfx_set_clip_region (1, 1, 510, 383);
199 void draw_laser_sights(void)
204 switch (current_screen
)
207 gfx_display_centre_text (32, "Front View", 120, GFX_COL_WHITE
);
208 laser
= cmdr
.front_laser
;
212 gfx_display_centre_text (32, "Rear View", 120, GFX_COL_WHITE
);
213 laser
= cmdr
.rear_laser
;
217 gfx_display_centre_text (32, "Left View", 120, GFX_COL_WHITE
);
218 laser
= cmdr
.left_laser
;
222 gfx_display_centre_text (32, "Right View", 120, GFX_COL_WHITE
);
223 laser
= cmdr
.right_laser
;
230 x1
= 128 * GFX_SCALE
;
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 y1
= (96+8) * GFX_SCALE
;
239 y2
= (96+16) * GFX_SCALE
;
241 gfx_draw_colour_line (x1
-1, y1
, x1
-1, y2
, GFX_COL_GREY_1
);
242 gfx_draw_colour_line (x1
, y1
, x1
, y2
, GFX_COL_WHITE
);
243 gfx_draw_colour_line (x1
+1, y1
, x1
+1, y2
, GFX_COL_GREY_1
);
245 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
);
253 x1
= (128+8) * GFX_SCALE
;
254 x2
= (128+16) * GFX_SCALE
;
256 gfx_draw_colour_line (x1
, y1
-1, x2
, y1
-1, GFX_COL_GREY_1
);
257 gfx_draw_colour_line (x1
, y1
, x2
, y1
, GFX_COL_WHITE
);
258 gfx_draw_colour_line (x1
, y1
+1, x2
, y1
+1, GFX_COL_GREY_1
);
263 void arrow_right (void)
265 switch (current_screen
)
267 case SCR_MARKET_PRICES
:
272 select_right_setting();
275 case SCR_SHORT_RANGE
:
276 case SCR_GALACTIC_CHART
:
288 decrease_flight_roll();
289 decrease_flight_roll();
297 void arrow_left (void)
299 switch (current_screen
)
301 case SCR_MARKET_PRICES
:
306 select_left_setting();
309 case SCR_SHORT_RANGE
:
310 case SCR_GALACTIC_CHART
:
322 increase_flight_roll();
323 increase_flight_roll();
333 switch (current_screen
)
335 case SCR_MARKET_PRICES
:
336 select_previous_stock();
340 select_previous_equip();
344 select_previous_option();
351 case SCR_SHORT_RANGE
:
352 case SCR_GALACTIC_CHART
:
360 if (flight_climb
> 0)
364 decrease_flight_climb();
373 void arrow_down (void)
375 switch (current_screen
)
377 case SCR_MARKET_PRICES
:
386 select_next_option();
390 select_down_setting();
393 case SCR_SHORT_RANGE
:
394 case SCR_GALACTIC_CHART
:
402 if (flight_climb
< 0)
406 increase_flight_climb();
415 void return_pressed (void)
417 switch (current_screen
)
434 void y_pressed (void)
436 switch (current_screen
)
445 void n_pressed (void)
447 switch (current_screen
)
451 display_commander_status();
453 current_screen
= SCR_FRONT_VIEW
;
459 void d_pressed (void)
461 switch (current_screen
)
463 case SCR_GALACTIC_CHART
:
464 case SCR_SHORT_RANGE
:
465 show_distance_to_planet();
473 disengage_auto_pilot();
479 void f_pressed (void)
481 if ((current_screen
== SCR_GALACTIC_CHART
) ||
482 (current_screen
== SCR_SHORT_RANGE
))
486 gfx_clear_text_area();
487 gfx_display_text (16, 340, "Planet Name?");
492 void add_find_char (int letter
)
496 if (strlen (find_name
) == 16)
499 str
[0] = toupper (letter
);
501 strcat (find_name
, str
);
503 sprintf (str
, "Planet Name? %s", find_name
);
504 gfx_clear_text_area ();
505 gfx_display_text(16, 340, str
);
509 void delete_find_char (void)
514 len
= strlen (find_name
);
518 find_name
[len
- 1] = '\0';
520 sprintf (str
, "Planet Name? %s", find_name
);
521 gfx_clear_text_area();
522 gfx_display_text(16, 340, str
);
527 switch (current_screen
)
529 case SCR_GALACTIC_CHART
:
530 case SCR_SHORT_RANGE
:
531 move_cursor_to_origin();
537 void auto_dock (void)
539 struct univ_object ship
;
545 set_init_matrix (ship
.rotmat
);
546 ship
.rotmat
[2].z
= 1;
547 ship
.rotmat
[0].x
= -1;
549 ship
.velocity
= flight_speed
;
550 ship
.acceleration
= 0;
555 auto_pilot_ship (&ship
);
557 if (ship
.velocity
> 22)
560 flight_speed
= ship
.velocity
;
562 if (ship
.acceleration
> 0)
565 if (flight_speed
> 22)
569 if (ship
.acceleration
< 0)
572 if (flight_speed
< 1)
581 increase_flight_climb();
584 increase_flight_climb();
589 decrease_flight_climb();
592 decrease_flight_climb();
595 if (ship
.rotz
== 127)
604 increase_flight_roll();
607 increase_flight_roll();
612 decrease_flight_roll();
615 decrease_flight_roll();
621 void run_escape_sequence (void)
627 current_screen
= SCR_ESCAPE_POD
;
633 set_init_matrix (rotmat
);
636 newship
= add_new_ship (SHIP_COBRA3
, 0, 0, 200, rotmat
, -127, -127);
637 universe
[newship
].velocity
= 7;
638 snd_play_sample (SND_LAUNCH
);
640 for (i
= 0; i
< 90; i
++)
644 universe
[newship
].flags
|= FLG_DEAD
;
645 snd_play_sample (SND_EXPLODE
);
648 gfx_set_clip_region (1, 1, 510, 383);
653 universe
[newship
].location
.x
= 0;
654 universe
[newship
].location
.y
= 0;
655 universe
[newship
].location
.z
+= 2;
657 gfx_display_centre_text (358, "Escape pod launched - Ship auto-destuct initiated.", 120, GFX_COL_WHITE
);
664 while ((ship_count
[SHIP_CORIOLIS
] == 0) &&
665 (ship_count
[SHIP_DODEC
] == 0))
669 if ((abs(flight_roll
) < 3) && (abs(flight_climb
) < 3))
671 for (i
= 0; i
< MAX_UNIV_OBJECTS
; i
++)
673 if (universe
[i
].type
!= 0)
674 universe
[i
].location
.z
-= 1500;
680 gfx_set_clip_region (1, 1, 510, 383);
692 void handle_flight_keys (void)
697 ((current_screen
== SCR_MARKET_PRICES
) ||
698 (current_screen
== SCR_OPTIONS
) ||
699 (current_screen
== SCR_SETTINGS
) ||
700 (current_screen
== SCR_EQUIP_SHIP
)))
709 if (joy
[0].stick
[0].axis
[1].d1
)
712 if (joy
[0].stick
[0].axis
[1].d2
)
715 if (joy
[0].stick
[0].axis
[0].d1
)
718 if (joy
[0].stick
[0].axis
[0].d2
)
721 if (joy
[0].button
[0].b
)
722 kbd_fire_pressed
= 1;
724 if (joy
[0].button
[1].b
)
725 kbd_inc_speed_pressed
= 1;
727 if (joy
[0].button
[2].b
)
728 kbd_dec_speed_pressed
= 1;
734 if (kbd_resume_pressed
)
747 if (current_screen
!= SCR_FRONT_VIEW
)
749 current_screen
= SCR_FRONT_VIEW
;
761 if (current_screen
!= SCR_REAR_VIEW
)
763 current_screen
= SCR_REAR_VIEW
;
775 if (current_screen
!= SCR_LEFT_VIEW
)
777 current_screen
= SCR_LEFT_VIEW
;
791 if (current_screen
!= SCR_RIGHT_VIEW
)
793 current_screen
= SCR_RIGHT_VIEW
;
804 display_galactic_chart();
811 display_short_range_chart();
817 display_data_on_planet();
820 if (kbd_F8_pressed
&& (!witchspace
))
823 display_market_prices();
829 display_commander_status();
846 keyasc
= kbd_read_key();
848 if (kbd_enter_pressed
)
851 find_planet_by_name (find_name
);
855 if (kbd_backspace_pressed
)
862 add_find_char (keyasc
);
873 if (kbd_i_pressed
== 1)
874 identify
= !identify
;
875 if (kbd_zoom_pressed
== 1)
878 if (kbd_fire_pressed
)
880 if ((!docked
) && (draw_lasers
== 0))
881 draw_lasers
= fire_laser();
884 if (kbd_dock_pressed
)
886 if (!docked
&& cmdr
.docking_computer
)
889 engage_docking_computer();
900 if (!docked
&& cmdr
.ecm
)
904 if (kbd_find_pressed
)
907 if (kbd_hyperspace_pressed
&& (!docked
))
909 if (kbd_ctrl_pressed
)
910 start_galactic_hyperspace();
915 if (kbd_jump_pressed
&& (!docked
) && (!witchspace
))
920 if (kbd_fire_missile_pressed
)
926 if (kbd_origin_pressed
)
929 if (kbd_pause_pressed
)
932 if (kbd_target_missile_pressed
)
938 if (kbd_unarm_missile_pressed
)
944 if (kbd_inc_speed_pressed
)
948 if (flight_speed
< myship
.max_speed
)
953 if (kbd_dec_speed_pressed
)
957 if (flight_speed
> 1)
965 if (kbd_down_pressed
)
968 if (kbd_left_pressed
)
971 if (kbd_right_pressed
)
974 if (kbd_enter_pressed
)
977 if (kbd_energy_bomb_pressed
)
979 if ((!docked
) && (cmdr
.energy_bomb
))
982 cmdr
.energy_bomb
= 0;
986 if (kbd_escape_pressed
)
988 if ((!docked
) && (cmdr
.escape_pod
) && (!witchspace
))
989 run_escape_sequence();
995 void set_commander_name (char *path
)
1000 fname
= get_filename (path
);
1003 for (i
= 0; i
< 31; i
++)
1005 if (!isalnum(*fname
))
1008 *cname
++ = toupper(*fname
++);
1015 void save_commander_screen (void)
1021 current_screen
= SCR_SAVE_CMDR
;
1023 gfx_clear_display();
1024 gfx_display_centre_text (10, "SAVE COMMANDER", 140, GFX_COL_GOLD
);
1025 gfx_draw_line (0, 36, 511, 36);
1026 gfx_update_screen();
1028 strcpy (path
, cmdr
.name
);
1029 strcat (path
, ".nkc");
1031 okay
= gfx_request_file ("Save Commander", path
, "nkc");
1039 rv
= save_commander_file (path
);
1043 gfx_display_centre_text (175, "Error Saving Commander!", 140, GFX_COL_GOLD
);
1047 gfx_display_centre_text (175, "Commander Saved.", 140, GFX_COL_GOLD
);
1049 set_commander_name (path
);
1051 saved_cmdr
.ship_x
= docked_planet
.d
;
1052 saved_cmdr
.ship_y
= docked_planet
.b
;
1056 void load_commander_screen (void)
1061 gfx_clear_display();
1062 gfx_display_centre_text (10, "LOAD COMMANDER", 140, GFX_COL_GOLD
);
1063 gfx_draw_line (0, 36, 511, 36);
1064 gfx_update_screen();
1067 strcpy (path
, "jameson.nkc");
1069 rv
= gfx_request_file ("Load Commander", path
, "nkc");
1074 rv
= load_commander_file (path
);
1079 gfx_display_centre_text (175, "Error Loading Commander!", 140, GFX_COL_GOLD
);
1080 gfx_display_centre_text (200, "Press any key to continue.", 140, GFX_COL_GOLD
);
1081 gfx_update_screen();
1086 restore_saved_commander();
1087 set_commander_name (path
);
1094 void run_first_intro_screen (void)
1096 current_screen
= SCR_INTRO_ONE
;
1098 snd_play_midi (SND_ELITE_THEME
, TRUE
);
1100 initialise_intro1();
1107 gfx_update_screen();
1109 kbd_poll_keyboard();
1114 load_commander_screen();
1129 void run_second_intro_screen (void)
1131 current_screen
= SCR_INTRO_TWO
;
1133 snd_play_midi (SND_BLUE_DANUBE
, TRUE
);
1136 initialise_intro2();
1146 gfx_update_screen();
1148 kbd_poll_keyboard();
1150 if (kbd_space_pressed
)
1160 * Draw the game over sequence.
1163 void run_game_over_screen()
1170 current_screen
= SCR_GAME_OVER
;
1171 gfx_set_clip_region (1, 1, 510, 383);
1179 set_init_matrix (rotmat
);
1181 newship
= add_new_ship (SHIP_COBRA3
, 0, 0, -400, rotmat
, 0, 0);
1182 universe
[newship
].flags
|= FLG_DEAD
;
1184 for (i
= 0; i
< 5; i
++)
1186 type
= (rand255() & 1) ? SHIP_CARGO
: SHIP_ALLOY
;
1187 newship
= add_new_ship (type
, (rand255() & 63) - 32,
1188 (rand255() & 63) - 32, -400, rotmat
, 0, 0);
1189 universe
[newship
].rotz
= ((rand255() * 2) & 255) - 128;
1190 universe
[newship
].rotx
= ((rand255() * 2) & 255) - 128;
1191 universe
[newship
].velocity
= rand255() & 15;
1195 for (i
= 0; i
< 100; i
++)
1197 gfx_clear_display();
1200 gfx_display_centre_text (190, "GAME OVER", 140, GFX_COL_GOLD
);
1201 gfx_update_screen();
1209 * Draw a break pattern (for launching, docking and hyperspacing).
1210 * Just draw a very simple one for the moment.
1213 void display_break_pattern (void)
1217 gfx_set_clip_region (1, 1, 510, 383);
1218 gfx_clear_display();
1220 for (i
= 0; i
< 20; i
++)
1222 gfx_draw_circle (256, 192, 30 + i
* 15, GFX_COL_WHITE
);
1223 gfx_update_screen();
1229 check_mission_brief();
1230 display_commander_status();
1234 current_screen
= SCR_FRONT_VIEW
;
1238 void info_message (char *message
)
1240 strcpy (message_string
, message
);
1242 // snd_play_sample (SND_BEEP);
1250 void initialise_allegro (void)
1259 if (install_joystick(JOY_TYPE_AUTODETECT
) == 0)
1261 have_joystick
= (num_joysticks
> 0);
1269 initialise_allegro();
1272 if (gfx_graphics_startup() == 1)
1277 /* Start the sound system... */
1278 snd_sound_startup();
1280 /* Do any setup necessary for the keyboard... */
1281 kbd_keyboard_startup();
1294 current_screen
= SCR_FRONT_VIEW
;
1295 run_first_intro_screen();
1296 run_second_intro_screen();
1302 display_commander_status ();
1307 gfx_update_screen();
1308 gfx_set_clip_region (1, 1, 510, 383);
1313 handle_flight_keys ();
1318 if (message_count
> 0)
1323 if (flight_roll
> 0)
1324 decrease_flight_roll();
1326 if (flight_roll
< 0)
1327 increase_flight_roll();
1332 if (flight_climb
> 0)
1333 decrease_flight_climb();
1335 if (flight_climb
< 0)
1336 increase_flight_climb();
1342 gfx_acquire_screen();
1344 if ((current_screen
== SCR_FRONT_VIEW
) || (current_screen
== SCR_REAR_VIEW
) ||
1345 (current_screen
== SCR_LEFT_VIEW
) || (current_screen
== SCR_RIGHT_VIEW
) ||
1346 (current_screen
== SCR_INTRO_ONE
) || (current_screen
== SCR_INTRO_TWO
) ||
1347 (current_screen
== SCR_GAME_OVER
))
1349 gfx_clear_display();
1356 if ((mcount
& 127) == 0)
1357 info_message ("Docking Computers On");
1365 gfx_release_screen();
1369 if ((current_screen
== SCR_FRONT_VIEW
) || (current_screen
== SCR_REAR_VIEW
) ||
1370 (current_screen
== SCR_LEFT_VIEW
) || (current_screen
== SCR_RIGHT_VIEW
))
1378 draw_laser_sights();
1381 if (message_count
> 0)
1382 gfx_display_centre_text (358, message_string
, 120, GFX_COL_WHITE
);
1386 display_hyper_status();
1387 if ((mcount
& 3) == 0)
1389 countdown_hyperspace();
1393 gfx_release_screen();
1399 if ((mcount
& 7) == 0)
1400 regenerate_shields();
1402 if ((mcount
& 31) == 10)
1406 info_message ("ENERGY LOW");
1407 snd_play_sample (SND_BEEP
);
1413 if ((mcount
& 31) == 20)
1414 update_cabin_temp();
1416 if ((mcount
== 0) && (!witchspace
))
1425 if (current_screen
== SCR_BREAK_PATTERN
)
1426 display_break_pattern();
1428 if (cross_timer
> 0)
1431 if (cross_timer
== 0)
1433 show_distance_to_planet();
1437 if ((cross_x
!= old_cross_x
) ||
1438 (cross_y
!= old_cross_y
))
1440 if (old_cross_x
!= -1)
1441 draw_cross (old_cross_x
, old_cross_y
);
1443 old_cross_x
= cross_x
;
1444 old_cross_y
= cross_y
;
1446 draw_cross (old_cross_x
, old_cross_y
);
1451 run_game_over_screen();
1454 snd_sound_shutdown();
1456 gfx_graphics_shutdown ();