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.
47 extern int flight_climb
;
48 extern int flight_roll
;
49 extern int flight_speed
;
51 struct galaxy_seed destination_planet
;
63 void rotate_x_first (double *a
, double *b
, int direction
)
72 *a
= fx
- (fx
/ 512) + (ux
/ 19);
73 *b
= ux
- (ux
/ 512) - (fx
/ 19);
77 *a
= fx
- (fx
/ 512) - (ux
/ 19);
78 *b
= ux
- (ux
/ 512) + (fx
/ 19);
83 void rotate_vec (struct vector
*vec
, double alpha
, double beta
)
103 * Update an objects location in the universe.
106 void move_univ_object (struct univ_object
*obj
)
115 alpha
= flight_roll
/ 256.0;
116 beta
= flight_climb
/ 256.0;
122 if (!(obj
->flags
& FLG_DEAD
))
124 if (obj
->velocity
!= 0)
126 speed
= obj
->velocity
;
128 x
+= obj
->rotmat
[2].x
* speed
;
129 y
+= obj
->rotmat
[2].y
* speed
;
130 z
+= obj
->rotmat
[2].z
* speed
;
133 if (obj
->acceleration
!= 0)
135 obj
->velocity
+= obj
->acceleration
;
136 obj
->acceleration
= 0;
137 if (obj
->velocity
> ship_list
[obj
->type
]->velocity
)
138 obj
->velocity
= ship_list
[obj
->type
]->velocity
;
140 if (obj
->velocity
<= 0)
150 z
= z
- flight_speed
;
156 obj
->distance
= sqrt (x
*x
+ y
*y
+ z
*z
);
158 if (obj
->type
== SHIP_PLANET
)
161 rotate_vec (&obj
->rotmat
[2], alpha
, beta
);
162 rotate_vec (&obj
->rotmat
[1], alpha
, beta
);
163 rotate_vec (&obj
->rotmat
[0], alpha
, beta
);
165 if (obj
->flags
& FLG_DEAD
)
172 /* If necessary rotate the object around the X axis... */
176 rotate_x_first (&obj
->rotmat
[2].x
, &obj
->rotmat
[1].x
, rotx
);
177 rotate_x_first (&obj
->rotmat
[2].y
, &obj
->rotmat
[1].y
, rotx
);
178 rotate_x_first (&obj
->rotmat
[2].z
, &obj
->rotmat
[1].z
, rotx
);
180 if ((rotx
!= 127) && (rotx
!= -127))
181 obj
->rotx
-= (rotx
< 0) ?
-1 : 1;
185 /* If necessary rotate the object around the Z axis... */
189 rotate_x_first (&obj
->rotmat
[0].x
, &obj
->rotmat
[1].x
, rotz
);
190 rotate_x_first (&obj
->rotmat
[0].y
, &obj
->rotmat
[1].y
, rotz
);
191 rotate_x_first (&obj
->rotmat
[0].z
, &obj
->rotmat
[1].z
, rotz
);
193 if ((rotz
!= 127) && (rotz
!= -127))
194 obj
->rotz
-= (rotz
< 0) ?
-1 : 1;
198 /* Orthonormalize the rotation matrix... */
200 tidy_matrix (obj
->rotmat
);
205 * Dock the player into the space station.
208 void dock_player (void)
210 disengage_auto_pilot();
218 myship
.altitude
= 255;
225 * Check if we are correctly aligned to dock.
228 int is_docking (int sn
)
234 if (auto_pilot
) // Don't want it to kill anyone!
237 fz
= universe
[sn
].rotmat
[2].z
;
242 vec
= unit_vector (&universe
[sn
].location
);
247 ux
= universe
[sn
].rotmat
[1].x
;
262 void do_game_over (void)
264 snd_play_sample (SND_GAMEOVER
);
269 void update_altitude (void)
274 myship
.altitude
= 255;
279 x
= fabs(universe
[0].location
.x
);
280 y
= fabs(universe
[0].location
.y
);
281 z
= fabs(universe
[0].location
.z
);
283 if ((x
> 65535) || (y
> 65535) || (z
> 65535))
290 dist
= (x
* x
) + (y
* y
) + (z
* z
);
311 myship
.altitude
= dist
;
315 void update_cabin_temp (void)
325 if (ship_count
[SHIP_CORIOLIS
] || ship_count
[SHIP_DODEC
])
328 x
= abs((int)universe
[1].location
.x
);
329 y
= abs((int)universe
[1].location
.y
);
330 z
= abs((int)universe
[1].location
.z
);
332 if ((x
> 65535) || (y
> 65535) || (z
> 65535))
339 dist
= ((x
* x
) + (y
* y
) + (z
* z
)) / 256;
346 myship
.cabtemp
= dist
+ 30;
348 if (myship
.cabtemp
> 255)
350 myship
.cabtemp
= 255;
355 if ((myship
.cabtemp
< 224) || (cmdr
.fuel_scoop
== 0))
358 cmdr
.fuel
+= flight_speed
/ 2;
359 if (cmdr
.fuel
> myship
.max_fuel
)
360 cmdr
.fuel
= myship
.max_fuel
;
362 info_message ("Fuel Scoop On");
368 * Regenerate the shields and the energy banks.
371 void regenerate_shields (void)
375 if (front_shield
< 255)
381 if (aft_shield
< 255)
389 energy
+= cmdr
.energy_unit
;
395 void decrease_energy (int amount
)
405 * Deplete the shields. Drain the energy banks if the shields fail.
408 void damage_ship (int damage
, int front
)
412 if (damage
<= 0) /* sanity check */
415 shield
= front ? front_shield
: aft_shield
;
420 decrease_energy (shield
);
425 front_shield
= shield
;
433 void make_station_appear (void)
440 px
= universe
[0].location
.x
;
441 py
= universe
[0].location
.y
;
442 pz
= universe
[0].location
.z
;
444 vec
.x
= (rand() & 32767) - 16384;
445 vec
.y
= (rand() & 32767) - 16384;
446 vec
.z
= rand() & 32767;
448 vec
= unit_vector (&vec
);
450 sx
= px
- vec
.x
* 65792;
451 sy
= py
- vec
.y
* 65792;
452 sz
= pz
- vec
.z
* 65792;
454 // set_init_matrix (rotmat);
462 rotmat
[1].z
= -vec
.y
;
468 tidy_matrix (rotmat
);
470 add_new_station (sx
, sy
, sz
, rotmat
);
475 void check_docking (int i
)
479 snd_play_sample (SND_DOCK
);
481 current_screen
= SCR_BREAK_PATTERN
;
485 if (flight_speed
>= 5)
492 damage_ship (5, universe
[i
].location
.z
> 0);
493 snd_play_sample (SND_CRASH
);
497 void switch_to_view (struct univ_object
*flip
)
501 if ((current_screen
== SCR_REAR_VIEW
) ||
502 (current_screen
== SCR_GAME_OVER
))
504 flip
->location
.x
= -flip
->location
.x
;
505 flip
->location
.z
= -flip
->location
.z
;
507 flip
->rotmat
[0].x
= -flip
->rotmat
[0].x
;
508 flip
->rotmat
[0].z
= -flip
->rotmat
[0].z
;
510 flip
->rotmat
[1].x
= -flip
->rotmat
[1].x
;
511 flip
->rotmat
[1].z
= -flip
->rotmat
[1].z
;
513 flip
->rotmat
[2].x
= -flip
->rotmat
[2].x
;
514 flip
->rotmat
[2].z
= -flip
->rotmat
[2].z
;
519 if (current_screen
== SCR_LEFT_VIEW
)
521 tmp
= flip
->location
.x
;
522 flip
->location
.x
= flip
->location
.z
;
523 flip
->location
.z
= -tmp
;
528 tmp
= flip
->rotmat
[0].x
;
529 flip
->rotmat
[0].x
= flip
->rotmat
[0].z
;
530 flip
->rotmat
[0].z
= -tmp
;
532 tmp
= flip
->rotmat
[1].x
;
533 flip
->rotmat
[1].x
= flip
->rotmat
[1].z
;
534 flip
->rotmat
[1].z
= -tmp
;
536 tmp
= flip
->rotmat
[2].x
;
537 flip
->rotmat
[2].x
= flip
->rotmat
[2].z
;
538 flip
->rotmat
[2].z
= -tmp
;
542 if (current_screen
== SCR_RIGHT_VIEW
)
544 tmp
= flip
->location
.x
;
545 flip
->location
.x
= -flip
->location
.z
;
546 flip
->location
.z
= tmp
;
551 tmp
= flip
->rotmat
[0].x
;
552 flip
->rotmat
[0].x
= -flip
->rotmat
[0].z
;
553 flip
->rotmat
[0].z
= tmp
;
555 tmp
= flip
->rotmat
[1].x
;
556 flip
->rotmat
[1].x
= -flip
->rotmat
[1].z
;
557 flip
->rotmat
[1].z
= tmp
;
559 tmp
= flip
->rotmat
[2].x
;
560 flip
->rotmat
[2].x
= -flip
->rotmat
[2].z
;
561 flip
->rotmat
[2].z
= tmp
;
568 * Update all the objects in the universe and render them.
571 void update_universe (void)
577 struct univ_object flip
;
582 for (i
= 0; i
< MAX_UNIV_OBJECTS
; i
++)
584 type
= universe
[i
].type
;
588 if (universe
[i
].flags
& FLG_REMOVE
)
590 if (type
== SHIP_VIPER
)
591 cmdr
.legal_status
|= 64;
593 bounty
= ship_list
[type
]->bounty
;
595 if ((bounty
!= 0) && (!witchspace
))
597 cmdr
.credits
+= bounty
;
598 sprintf (str
, "%d.%d CR", cmdr
.credits
/ 10, cmdr
.credits
% 10);
606 if ((detonate_bomb
) && ((universe
[i
].flags
& FLG_DEAD
) == 0) &&
607 (type
!= SHIP_PLANET
) && (type
!= SHIP_SUN
) &&
608 (type
!= SHIP_CONSTRICTOR
) && (type
!= SHIP_COUGAR
) &&
609 (type
!= SHIP_CORIOLIS
) && (type
!= SHIP_DODEC
))
611 snd_play_sample (SND_EXPLODE
);
612 universe
[i
].flags
|= FLG_DEAD
;
615 if ((current_screen
!= SCR_INTRO_ONE
) &&
616 (current_screen
!= SCR_INTRO_TWO
) &&
617 (current_screen
!= SCR_GAME_OVER
) &&
618 (current_screen
!= SCR_ESCAPE_POD
))
623 move_univ_object (&universe
[i
]);
626 switch_to_view (&flip
);
628 if (type
== SHIP_PLANET
)
630 if ((ship_count
[SHIP_CORIOLIS
] == 0) &&
631 (ship_count
[SHIP_DODEC
] == 0) &&
632 (universe
[i
].distance
< 65792)) // was 49152
634 make_station_appear();
641 if (type
== SHIP_SUN
)
648 if (universe
[i
].distance
< 170)
650 if ((type
== SHIP_CORIOLIS
) || (type
== SHIP_DODEC
))
658 if (universe
[i
].distance
> 57344)
666 universe
[i
].flags
= flip
.flags
;
667 universe
[i
].exp_seed
= flip
.exp_seed
;
668 universe
[i
].exp_delta
= flip
.exp_delta
;
670 universe
[i
].flags
&= ~FLG_FIRING
;
672 if (universe
[i
].flags
& FLG_DEAD
)
675 check_target (i
, &flip
);
687 * Update the scanner and draw all the lollipops.
690 void update_scanner (void)
697 for (i
= 0; i
< MAX_UNIV_OBJECTS
; i
++)
699 if ((universe
[i
].type
<= 0) ||
700 (universe
[i
].flags
& FLG_DEAD
) ||
701 (universe
[i
].flags
& FLG_CLOAKED
))
704 x
= universe
[i
].location
.x
/ 256;
705 y
= universe
[i
].location
.y
/ 256;
706 z
= universe
[i
].location
.z
/ 256;
712 if ((y2
< -28) || (y2
> 28) ||
713 (x1
< -50) || (x1
> 50))
720 colour
= (universe
[i
].flags
& FLG_HOSTILE
) ? GFX_COL_YELLOW_5
: GFX_COL_WHITE
;
722 switch (universe
[i
].type
)
730 colour
= GFX_COL_GREEN_1
;
738 gfx_draw_colour_line (x1
+2, y2
, x1
-3, y2
, colour
);
739 gfx_draw_colour_line (x1
+2, y2
+1, x1
-3, y2
+1, colour
);
740 gfx_draw_colour_line (x1
+2, y2
+2, x1
-3, y2
+2, colour
);
741 gfx_draw_colour_line (x1
+2, y2
+3, x1
-3, y2
+3, colour
);
744 gfx_draw_colour_line (x1
, y1
, x1
, y2
, colour
);
745 gfx_draw_colour_line (x1
+1, y1
, x1
+1, y2
, colour
);
746 gfx_draw_colour_line (x1
+2, y1
, x1
+2, y2
, colour
);
752 * Update the compass which tracks the space station / planet.
755 void update_compass (void)
765 if (ship_count
[SHIP_CORIOLIS
] || ship_count
[SHIP_DODEC
])
768 dest
= unit_vector (&universe
[un
].location
);
770 compass_x
= compass_centre_x
+ (dest
.x
* 16);
771 compass_y
= compass_centre_y
+ (dest
.y
* -16);
775 gfx_draw_sprite (IMG_RED_DOT
, compass_x
, compass_y
);
779 gfx_draw_sprite (IMG_GREEN_DOT
, compass_x
, compass_y
);
786 * Display the speed bar.
789 void display_speed (void)
799 len
= ((flight_speed
* 64) / myship
.max_speed
) - 1;
801 colour
= (flight_speed
> (myship
.max_speed
* 2 / 3)) ? GFX_COL_DARK_RED
: GFX_COL_GOLD
;
803 for (i
= 0; i
< 6; i
++)
805 gfx_draw_colour_line (sx
, sy
+ i
, sx
+ len
, sy
+ i
, colour
);
811 * Draw an indicator bar.
812 * Used for shields and energy banks.
815 void display_dial_bar (int len
, int x
, int y
)
819 gfx_draw_colour_line (x
, y
+ 384, x
+ len
, y
+ 384, GFX_COL_GOLD
);
821 gfx_draw_colour_line (x
, y
+ i
+ 384, x
+ len
, y
+ i
+ 384, GFX_COL_GOLD
);
823 for (i
= 2; i
< 7; i
++)
824 gfx_draw_colour_line (x
, y
+ i
+ 384, x
+ len
, y
+ i
+ 384, GFX_COL_YELLOW_1
);
826 gfx_draw_colour_line (x
, y
+ i
+ 384, x
+ len
, y
+ i
+ 384, GFX_COL_DARK_RED
);
831 * Display the current shield strengths.
834 void display_shields (void)
836 if (front_shield
> 3)
837 display_dial_bar (front_shield
/ 4, 31, 7);
840 display_dial_bar (aft_shield
/ 4, 31, 23);
844 void display_altitude (void)
846 if (myship
.altitude
> 3)
847 display_dial_bar (myship
.altitude
/ 4, 31, 92);
850 void display_cabin_temp (void)
852 if (myship
.cabtemp
> 3)
853 display_dial_bar (myship
.cabtemp
/ 4, 31, 60);
857 void display_laser_temp (void)
860 display_dial_bar (laser_temp
/ 4, 31, 76);
865 * Display the energy banks.
868 void display_energy (void)
872 e1
= energy
> 64 ?
64 : energy
;
873 e2
= energy
> 128 ?
64 : energy
- 64;
874 e3
= energy
> 192 ?
64 : energy
- 128;
878 display_dial_bar (e4
, 416, 61);
881 display_dial_bar (e3
, 416, 79);
884 display_dial_bar (e2
, 416, 97);
887 display_dial_bar (e1
, 416, 115);
892 void display_flight_roll (void)
901 pos
= sx
- ((flight_roll
* 28) / myship
.max_roll
);
904 for (i
= 0; i
< 4; i
++)
906 gfx_draw_colour_line (pos
+ i
, sy
, pos
+ i
, sy
+ 7, GFX_COL_GOLD
);
910 void display_flight_climb (void)
917 sy
= 384 + 9 + 14 + 16;
919 pos
= sx
+ ((flight_climb
* 28) / myship
.max_climb
);
922 for (i
= 0; i
< 4; i
++)
924 gfx_draw_colour_line (pos
+ i
, sy
, pos
+ i
, sy
+ 7, GFX_COL_GOLD
);
929 void display_fuel (void)
932 display_dial_bar ((cmdr
.fuel
* 64) / myship
.max_fuel
, 31, 44);
936 void display_missiles (void)
941 if (cmdr
.missiles
== 0)
944 nomiss
= cmdr
.missiles
> 4 ?
4 : cmdr
.missiles
;
946 x
= (4 - nomiss
) * 16 + 35;
949 if (missile_target
!= MISSILE_UNARMED
)
951 gfx_draw_sprite ((missile_target
< 0) ? IMG_MISSILE_YELLOW
:
952 IMG_MISSILE_RED
, x
, y
);
957 for (; nomiss
> 0; nomiss
--)
959 gfx_draw_sprite (IMG_MISSILE_GREEN
, x
, y
);
965 void update_console (void)
967 gfx_set_clip_region (0, 0, 512, 512);
971 display_flight_climb();
972 display_flight_roll();
976 display_cabin_temp();
977 display_laser_temp();
987 if (ship_count
[SHIP_CORIOLIS
] || ship_count
[SHIP_DODEC
])
988 gfx_draw_sprite (IMG_BIG_S
, 387, 490);
991 gfx_draw_sprite (IMG_BIG_E
, 115, 490);
994 void increase_flight_roll (void)
996 if (flight_roll
< myship
.max_roll
)
1001 void decrease_flight_roll (void)
1003 if (flight_roll
> -myship
.max_roll
)
1008 void increase_flight_climb (void)
1010 if (flight_climb
< myship
.max_climb
)
1014 void decrease_flight_climb (void)
1016 if (flight_climb
> -myship
.max_climb
)
1021 void start_hyperspace (void)
1026 hyper_distance
= calc_distance_to_planet (docked_planet
, hyperspace_planet
);
1028 if ((hyper_distance
== 0) || (hyper_distance
> cmdr
.fuel
))
1031 destination_planet
= hyperspace_planet
;
1032 name_planet (hyper_name
, destination_planet
);
1033 capitalise_name (hyper_name
);
1036 hyper_countdown
= 15;
1039 disengage_auto_pilot();
1042 void start_galactic_hyperspace (void)
1047 if (cmdr
.galactic_hyperdrive
== 0)
1051 hyper_countdown
= 2;
1053 disengage_auto_pilot();
1058 void display_hyper_status (void)
1062 sprintf (str
, "%d", hyper_countdown
);
1064 if ((current_screen
== SCR_FRONT_VIEW
) || (current_screen
== SCR_REAR_VIEW
) ||
1065 (current_screen
== SCR_LEFT_VIEW
) || (current_screen
== SCR_RIGHT_VIEW
))
1067 gfx_display_text (5, 5, str
);
1070 gfx_display_centre_text (358, "Galactic Hyperspace", 120, GFX_COL_WHITE
);
1074 sprintf (str
, "Hyperspace - %s", hyper_name
);
1075 gfx_display_centre_text (358, str
, 120, GFX_COL_WHITE
);
1080 gfx_clear_area (5, 5, 25, 34);
1081 gfx_display_text (5, 5, str
);
1086 int rotate_byte_left (int x
)
1088 return ((x
<< 1) | (x
>> 7)) & 255;
1091 void enter_next_galaxy (void)
1093 cmdr
.galaxy_number
++;
1094 cmdr
.galaxy_number
&= 7;
1096 cmdr
.galaxy
.a
= rotate_byte_left (cmdr
.galaxy
.a
);
1097 cmdr
.galaxy
.b
= rotate_byte_left (cmdr
.galaxy
.b
);
1098 cmdr
.galaxy
.c
= rotate_byte_left (cmdr
.galaxy
.c
);
1099 cmdr
.galaxy
.d
= rotate_byte_left (cmdr
.galaxy
.d
);
1100 cmdr
.galaxy
.e
= rotate_byte_left (cmdr
.galaxy
.e
);
1101 cmdr
.galaxy
.f
= rotate_byte_left (cmdr
.galaxy
.f
);
1103 docked_planet
= find_planet (0x60, 0x60);
1104 hyperspace_planet
= docked_planet
;
1111 void enter_witchspace (void)
1117 docked_planet
.b
^= 31;
1126 nthg
= (randint() & 3) + 1;
1128 for (i
= 0; i
< nthg
; i
++)
1131 current_screen
= SCR_BREAK_PATTERN
;
1132 snd_play_sample (SND_HYPERSPACE
);
1136 void complete_hyperspace (void)
1146 cmdr
.galactic_hyperdrive
= 0;
1147 enter_next_galaxy();
1148 cmdr
.legal_status
= 0;
1152 cmdr
.fuel
-= hyper_distance
;
1153 cmdr
.legal_status
/= 2;
1155 if ((rand255() > 253) || (flight_climb
== myship
.max_climb
))
1161 docked_planet
= destination_planet
;
1164 cmdr
.market_rnd
= rand255();
1165 generate_planet_data (¤t_planet_data
, docked_planet
);
1166 generate_stock_market ();
1174 generate_landscape(docked_planet
.a
* 251 + docked_planet
.b
);
1175 set_init_matrix (rotmat
);
1177 pz
= (((docked_planet
.b
) & 7) + 7) / 2;
1185 if ((docked_planet
.b
& 1) == 0)
1191 add_new_ship (SHIP_PLANET
, px
, py
, pz
, rotmat
, 0, 0);
1194 pz
= -(((docked_planet
.d
& 7) | 1) << 16);
1195 px
= ((docked_planet
.f
& 3) << 16) | ((docked_planet
.f
& 3) << 8);
1197 add_new_ship (SHIP_SUN
, px
, py
, pz
, rotmat
, 0, 0);
1199 current_screen
= SCR_BREAK_PATTERN
;
1200 snd_play_sample (SND_HYPERSPACE
);
1204 void countdown_hyperspace (void)
1206 if (hyper_countdown
== 0)
1208 complete_hyperspace();
1217 void jump_warp (void)
1223 for (i
= 0; i
< MAX_UNIV_OBJECTS
; i
++)
1225 type
= universe
[i
].type
;
1227 if ((type
> 0) && (type
!= SHIP_ASTEROID
) && (type
!= SHIP_CARGO
) &&
1228 (type
!= SHIP_ALLOY
) && (type
!= SHIP_ROCK
) &&
1229 (type
!= SHIP_BOULDER
) && (type
!= SHIP_ESCAPE_CAPSULE
))
1231 info_message ("Mass Locked");
1236 if ((universe
[0].distance
< 75001) || (universe
[1].distance
< 75001))
1238 info_message ("Mass Locked");
1243 if (universe
[0].distance
< universe
[1].distance
)
1244 jump
= universe
[0].distance
- 75000;
1246 jump
= universe
[1].distance
- 75000;
1251 for (i
= 0; i
< MAX_UNIV_OBJECTS
; i
++)
1253 if (universe
[i
].type
!= 0)
1254 universe
[i
].location
.z
-= jump
;
1263 void launch_player (void)
1271 cmdr
.legal_status
|= carrying_contraband();
1274 generate_landscape(docked_planet
.a
* 251 + docked_planet
.b
);
1275 set_init_matrix (rotmat
);
1276 add_new_ship (SHIP_PLANET
, 0, 0, 65536, rotmat
, 0, 0);
1278 rotmat
[2].x
= -rotmat
[2].x
;
1279 rotmat
[2].y
= -rotmat
[2].y
;
1280 rotmat
[2].z
= -rotmat
[2].z
;
1281 add_new_station (0, 0, -256, rotmat
);
1283 current_screen
= SCR_BREAK_PATTERN
;
1284 snd_play_sample (SND_LAUNCH
);
1290 * Engage the docking computer.
1291 * For the moment we just do an instant dock if we are in the safe zone.
1294 void engage_docking_computer (void)
1296 if (ship_count
[SHIP_CORIOLIS
] || ship_count
[SHIP_DODEC
])
1298 snd_play_sample (SND_DOCK
);
1300 current_screen
= SCR_BREAK_PATTERN
;