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 * Special Weapons And Tactics.
49 struct univ_object universe[MAX_UNIV_OBJECTS];
50 int ship_count[NO_OF_SHIPS + 1]; /* many */
53 int initial_flags[NO_OF_SHIPS + 1] =
56 FLG_TARGET, // missile
58 FLG_SLOW | FLG_FLY_TO_PLANET, // escape
59 FLG_INACTIVE | FLG_TARGET, // alloy
60 FLG_INACTIVE | FLG_TARGET, // cargo
61 FLG_INACTIVE | FLG_TARGET, // boulder
62 FLG_INACTIVE | FLG_TARGET, // asteroid
63 FLG_INACTIVE | FLG_TARGET, // rock
64 FLG_FLY_TO_PLANET | FLG_SLOW, // shuttle
65 FLG_FLY_TO_PLANET | FLG_SLOW, // transporter
71 FLG_BOLD | FLG_POLICE, // viper
72 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // sidewinder
73 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // mamba
74 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // krait
75 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // adder
76 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // gecko
77 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // cobra1
78 FLG_SLOW | FLG_ANGRY | FLG_TARGET, // worm
79 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // cobra3
80 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // asp2
81 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // python
82 FLG_POLICE, // fer_de_lance
83 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // moray
84 FLG_BOLD | FLG_ANGRY | FLG_TARGET, // thargoid
85 FLG_ANGRY | FLG_TARGET, // thargon
86 FLG_ANGRY, // constrictor
87 FLG_POLICE | FLG_CLOAKED, // cougar
94 void clear_universe (void)
98 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
101 for (i = 0; i <= NO_OF_SHIPS; i++)
108 int add_new_ship (int ship_type, int x, int y, int z, struct vector *rotmat, int rotx, int rotz)
112 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
114 if (universe[i].type == 0)
116 universe[i].type = ship_type;
117 universe[i].location.x = x;
118 universe[i].location.y = y;
119 universe[i].location.z = z;
121 universe[i].distance = sqrt(x*x + y*y + z*z);
123 universe[i].rotmat[0] = rotmat[0];
124 universe[i].rotmat[1] = rotmat[1];
125 universe[i].rotmat[2] = rotmat[2];
127 universe[i].rotx = rotx;
128 universe[i].rotz = rotz;
130 universe[i].velocity = 0;
131 universe[i].acceleration = 0;
132 universe[i].bravery = 0;
133 universe[i].target = 0;
135 universe[i].flags = initial_flags[ship_type];
137 if ((ship_type != SHIP_PLANET) && (ship_type != SHIP_SUN))
139 universe[i].energy = ship_list[ship_type]->energy;
140 universe[i].missiles = ship_list[ship_type]->missiles;
141 ship_count[ship_type]++;
154 void check_missiles (int un)
158 if (missile_target == un)
160 missile_target = MISSILE_UNARMED;
161 info_message ("Target Lost");
164 for (i = 0; i < MAX_UNIV_OBJECTS; i++)
166 if ((universe[i].type == SHIP_MISSILE) && (universe[i].target == un))
167 universe[i].flags |= FLG_DEAD;
172 void remove_ship (int un)
178 type = universe[un].type;
186 universe[un].type = 0;
190 if ((type == SHIP_CORIOLIS) || (type == SHIP_DODEC))
192 set_init_matrix (rotmat);
193 px = universe[un].location.x;
194 py = universe[un].location.y;
195 pz = universe[un].location.z;
200 add_new_ship (SHIP_SUN, px, py, pz, rotmat, 0, 0);
205 void add_new_station (double sx, double sy, double sz, Matrix rotmat)
209 station = (current_planet_data.techlevel >= 10) ? SHIP_DODEC : SHIP_CORIOLIS;
210 universe[1].type = 0;
211 add_new_ship (station, sx, sy, sz, rotmat, 0, -127);
217 void reset_weapons (void)
223 missile_target = MISSILE_UNARMED;
227 void launch_enemy (int un, int type, int flags, int bravery)
230 struct univ_object *ns;
232 newship = add_new_ship (type, universe[un].location.x, universe[un].location.y,
233 universe[un].location.z, universe[un].rotmat,
234 universe[un].rotx, universe[un].rotz);
241 ns = &universe[newship];
243 if ((universe[un].type == SHIP_CORIOLIS) || (universe[un].type == SHIP_DODEC))
246 ns->location.x += ns->rotmat[2].x * 2;
247 ns->location.y += ns->rotmat[2].y * 2;
248 ns->location.z += ns->rotmat[2].z * 2;
254 ns->bravery = bravery;
256 if ((type == SHIP_CARGO) || (type == SHIP_ALLOY) || (type == SHIP_ROCK))
258 ns->rotz = ((rand255() * 2) & 255) - 128;
259 ns->rotx = ((rand255() * 2) & 255) - 128;
260 ns->velocity = rand255() & 15;
265 void launch_loot (int un, int loot)
269 if (loot == SHIP_ROCK)
279 cnt &= ship_list[universe[un].type]->max_loot;
283 for (i = 0; i < cnt; i++)
285 launch_enemy (un, loot, 0,0);
292 int in_target (int type, double x, double y, double z)
299 size = ship_list[type]->size;
301 return ((x*x + y*y) <= size);
306 void make_angry (int un)
311 type = universe[un].type;
312 flags = universe[un].flags;
314 if (flags & FLG_INACTIVE) {
315 universe[un].flags |= FLG_TACTICAL;
319 if ((type == SHIP_CORIOLIS) || (type == SHIP_DODEC))
321 universe[un].flags |= FLG_ANGRY;
325 if (type > SHIP_ROCK)
327 universe[un].rotx = 4;
328 universe[un].acceleration = 2;
329 universe[un].flags |= FLG_ANGRY | FLG_TACTICAL;
334 void explode_object (int un)
339 if ((cmdr.score & 255) == 0)
340 info_message ("Right On Commander!");
342 snd_play_sample (SND_EXPLODE);
343 universe[un].flags |= FLG_DEAD;
345 if (universe[un].type == SHIP_CONSTRICTOR)
350 void check_target (int un, struct univ_object *flip)
352 struct univ_object *univ;
354 univ = &universe[un];
356 if (in_target (univ->type, flip->location.x, flip->location.y, flip->location.z))
358 if ((missile_target == MISSILE_ARMED) && (univ->type >= 0))
361 info_message ("Target Locked");
362 snd_play_sample (SND_BEEP);
367 snd_play_sample (SND_HIT_ENEMY);
369 if ((univ->type != SHIP_CORIOLIS) && (univ->type != SHIP_DODEC))
371 if ((univ->type == SHIP_CONSTRICTOR) || (univ->type == SHIP_COUGAR))
373 if (laser == (MILITARY_LASER & 127))
374 univ->energy -= laser / 4;
378 univ->energy -= laser;
382 if (univ->energy <= 0)
386 if (univ->type == SHIP_ASTEROID)
388 if (laser == (MINING_LASER & 127))
389 launch_loot (un, SHIP_ROCK);
393 launch_loot (un, SHIP_ALLOY);
394 launch_loot (un, SHIP_CARGO);
405 void activate_ecm (int ours)
411 snd_play_sample (SND_ECM);
422 decrease_energy (-1);
427 void arm_missile (void)
429 if ((cmdr.missiles != 0) && (missile_target == MISSILE_UNARMED))
430 missile_target = MISSILE_ARMED;
434 void unarm_missile (void)
436 missile_target = MISSILE_UNARMED;
437 snd_play_sample (SND_BOOP);
440 void fire_missile (void)
443 struct univ_object *ns;
446 if (missile_target < 0)
449 set_init_matrix (rotmat);
453 newship = add_new_ship (SHIP_MISSILE, 0, -28, 14, rotmat, 0, 0);
457 info_message ("Missile Jammed");
461 ns = &universe[newship];
463 ns->velocity = flight_speed * 2;
464 ns->flags |= FLG_TACTICAL;
465 ns->target = missile_target;
467 if (universe[missile_target].type > SHIP_ROCK)
468 universe[missile_target].flags |= FLG_ANGRY;
471 missile_target = MISSILE_UNARMED;
473 snd_play_sample (SND_MISSILE);
478 void track_object (struct univ_object *ship, double direction, Vector nvec)
487 dir = vector_dot_product (&nvec, &ship->rotmat[1]);
489 if (direction < -0.861)
491 ship->rotx = (dir < 0) ? 7 : -7;
498 if ((fabs(dir) * 2) >= rat2)
500 ship->rotx = (dir < 0) ? rat : -rat;
503 if (abs(ship->rotz) < 16)
505 dir = vector_dot_product (&nvec, &ship->rotmat[0]);
509 if ((fabs(dir) * 2) > rat2)
511 ship->rotz = (dir < 0) ? rat : -rat;
514 ship->rotz = -ship->rotz;
521 void missile_tactics (int un)
523 struct univ_object *missile;
524 struct univ_object *target;
530 missile = &universe[un];
534 snd_play_sample (SND_EXPLODE);
535 missile->flags |= FLG_DEAD;
539 if (missile->target == 0)
541 if (missile->distance < 256)
543 missile->flags |= FLG_DEAD;
544 snd_play_sample (SND_EXPLODE);
545 damage_ship (250, missile->location.z >= 0.0);
549 vec.x = missile->location.x;
550 vec.y = missile->location.y;
551 vec.z = missile->location.z;
555 target = &universe[missile->target];
557 vec.x = missile->location.x - target->location.x;
558 vec.y = missile->location.y - target->location.y;
559 vec.z = missile->location.z - target->location.z;
561 if ((fabs(vec.x) < 256) && (fabs(vec.y) < 256) && (fabs(vec.z) < 256))
563 missile->flags |= FLG_DEAD;
565 if ((target->type != SHIP_CORIOLIS) && (target->type != SHIP_DODEC))
566 explode_object (missile->target);
568 snd_play_sample (SND_EXPLODE);
573 if ((rand255() < 16) && (target->flags & FLG_HAS_ECM))
580 if ((rand255() < 16) &&
581 (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])) {
586 nvec = unit_vector(&vec);
587 direction = vector_dot_product (&nvec, &missile->rotmat[2]);
591 direction = -direction;
593 track_object (missile, direction, nvec);
595 if (direction <= -0.167)
597 missile->acceleration = -2;
601 if (direction >= cnt2)
603 missile->acceleration = 3;
607 if (missile->velocity < 6)
608 missile->acceleration = 3;
610 if (rand255() >= 200)
611 missile->acceleration = -2;
617 void launch_shuttle (void)
621 if ((ship_count[SHIP_TRANSPORTER] != 0) ||
622 (ship_count[SHIP_SHUTTLE] != 0) ||
623 (rand255() < 253) || (auto_pilot))
626 type = rand255() & 1 ? SHIP_SHUTTLE : SHIP_TRANSPORTER;
627 launch_enemy (1, type, FLG_HAS_ECM | FLG_FLY_TO_PLANET, 113);
631 void tactics (int un)
637 struct univ_object *ship;
643 ship = &universe[un];
647 if ((type == SHIP_PLANET) || (type == SHIP_SUN))
650 if (flags & FLG_DEAD)
653 if (flags & FLG_INACTIVE)
656 if (type == SHIP_MISSILE)
658 if (flags & FLG_ANGRY)
659 missile_tactics (un);
663 if (((un ^ mcount) & 7) != 0)
666 if ((type == SHIP_CORIOLIS) || (type == SHIP_DODEC))
668 if (flags & FLG_ANGRY)
670 if ((rand() & 255) < 240)
673 if (ship_count[SHIP_VIPER] >= 4)
676 launch_enemy (un, SHIP_VIPER, FLG_ANGRY | FLG_HAS_ECM, 113);
684 if (type == SHIP_HERMIT)
688 launch_enemy (un, SHIP_SIDEWINDER + (rand255() & 3), FLG_ANGRY | FLG_HAS_ECM, 113);
689 ship->flags |= FLG_INACTIVE | FLG_TARGET;
696 if (ship->energy < ship_list[type]->energy)
699 if ((type == SHIP_THARGLET) && (ship_count[SHIP_THARGOID] == 0))
701 ship->flags &= FLG_TARGET | FLG_TACTICAL;
706 if (flags & FLG_SLOW)
712 if (flags & FLG_POLICE)
714 if (cmdr.legal_status >= 64)
721 if ((flags & FLG_ANGRY) == 0)
723 if ((flags & FLG_FLY_TO_PLANET) || (flags & FLG_FLY_TO_STATION))
725 auto_pilot_ship (&universe[un]);
732 /* If we get to here then the ship is angry so start attacking... */
734 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
736 if ((flags & FLG_BOLD) == 0)
741 if (type == SHIP_ANACONDA)
745 launch_enemy (un, rand255() > 100 ? SHIP_WORM : SHIP_SIDEWINDER,
746 FLG_ANGRY | FLG_HAS_ECM, 113);
752 if (rand255() >= 250)
754 ship->rotz = rand255() | 0x68;
755 if (ship->rotz > 127)
756 ship->rotz = -(ship->rotz & 127);
759 maxeng = ship_list[type]->energy;
760 energy = ship->energy;
762 if (energy < (maxeng / 2))
764 if ((energy < (maxeng / 8)) && (rand255() > 230) && (type != SHIP_THARGOID))
766 ship->flags &= ~FLG_ANGRY;
767 ship->flags |= FLG_INACTIVE;
768 launch_enemy (un, SHIP_ESCAPE_CAPSULE, 0, 126);
772 if ((ship->missiles != 0) && (ecm_active == 0) &&
773 (ship->missiles >= (rand255() & 31)))
776 ship->flags |= FLG_TACTICAL;
777 if (type == SHIP_THARGOID)
778 launch_enemy (un, SHIP_THARGLET, FLG_ANGRY, ship->bravery);
781 launch_enemy (un, SHIP_MISSILE, FLG_ANGRY, 126);
782 ship->flags |= FLG_HOSTILE;
783 info_message ("INCOMING MISSILE");
789 nvec = unit_vector(&universe[un].location);
790 direction = vector_dot_product (&nvec, &ship->rotmat[2]);
792 if ((ship->distance < 8192) && (direction <= -0.833) &&
793 (ship_list[type]->laser_strength != 0))
795 if (direction <= -0.917)
796 ship->flags |= FLG_FIRING | FLG_HOSTILE | FLG_TACTICAL;
798 if (direction <= -0.972)
800 damage_ship (ship_list[type]->laser_strength, ship->location.z >= 0.0);
801 ship->acceleration--;
802 if (((ship->location.z >= 0.0) && (front_shield == 0)) ||
803 ((ship->location.z < 0.0) && (aft_shield == 0)))
804 snd_play_sample (SND_INCOMMING_FIRE_2);
806 snd_play_sample (SND_INCOMMING_FIRE_1);
813 direction = -direction;
814 track_object (&universe[un], direction, nvec);
817 // if ((fabs(ship->location.z) < 768) && (ship->bravery <= ((rand255() & 127) | 64)))
818 if (fabs(ship->location.z) < 768)
820 ship->rotx = rand255() & 0x87;
821 if (ship->rotx > 127)
822 ship->rotx = -(ship->rotx & 127);
824 ship->acceleration = 3;
828 if (ship->distance < 8192)
829 ship->acceleration = -1;
831 ship->acceleration = 3;
837 if ((fabs(ship->location.z) >= 768) ||
838 (fabs(ship->location.x) >= 512) ||
839 (fabs(ship->location.y) >= 512))
841 if (ship->bravery > (rand255() & 127))
847 direction = -direction;
851 track_object (&universe[un], direction, nvec);
853 if ((attacking == 1) && (ship->distance < 2048))
855 if (direction >= cnt2)
857 ship->acceleration = -1;
861 if (ship->velocity < 6)
862 ship->acceleration = 3;
864 if (rand255() >= 200)
865 ship->acceleration = -1;
869 if (direction <= -0.167)
871 ship->acceleration = -1;
875 if (direction >= cnt2)
877 ship->acceleration = 3;
881 if (ship->velocity < 6)
882 ship->acceleration = 3;
884 if (rand255() >= 200)
885 ship->acceleration = -1;
889 void draw_laser_lines (void)
893 gfx_draw_colour_line (32 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
894 gfx_draw_colour_line (48 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
895 gfx_draw_colour_line (208 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
896 gfx_draw_colour_line (224 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
900 gfx_draw_triangle (32 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, 48 * GFX_SCALE, GFX_VIEW_BY, GFX_COL_RED);
901 gfx_draw_triangle (208 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, 224 * GFX_SCALE, GFX_VIEW_BY, GFX_COL_RED);
906 int fire_laser (void)
908 if ((laser_counter == 0) && (laser_temp < 242))
910 switch (current_screen)
913 laser = cmdr.front_laser;
917 laser = cmdr.rear_laser;
921 laser = cmdr.right_laser;
925 laser = cmdr.left_laser;
934 laser_counter = (laser > 127) ? 0 : (laser & 0xFA);
938 snd_play_sample (SND_PULSE);
943 laser_x = ((rand() & 3) + 128 - 2) * GFX_SCALE;
944 laser_y = ((rand() & 3) + 96 - 2) * GFX_SCALE;
954 void cool_laser (void)
961 if (laser_counter > 0)
964 if (laser_counter > 0)
969 int create_other_ship (int type)
975 set_init_matrix (rotmat);
978 x = 1000 + (randint() & 8191);
979 y = 1000 + (randint() & 8191);
986 newship = add_new_ship (type, x, y, z, rotmat, 0, 0);
992 void create_thargoid (void)
996 newship = create_other_ship (SHIP_THARGOID);
999 universe[newship].flags = FLG_ANGRY | FLG_HAS_ECM | FLG_TARGET;
1000 universe[newship].bravery = 113;
1003 launch_enemy (newship, SHIP_THARGLET, FLG_ANGRY | FLG_HAS_ECM,
1011 void create_cougar (void)
1015 if (ship_count[SHIP_COUGAR] != 0)
1018 newship = create_other_ship (SHIP_COUGAR);
1021 universe[newship].flags = FLG_HAS_ECM; // | FLG_CLOAKED;
1022 universe[newship].bravery = 121;
1023 universe[newship].velocity = 18;
1029 void create_trader (void)
1035 type = SHIP_COBRA3 + (rand255() & 3);
1037 newship = create_other_ship (type);
1041 universe[newship].rotmat[2].z = -1.0;
1042 universe[newship].rotz = rand255() & 7;
1045 universe[newship].velocity = (rnd & 31) | 16;
1046 universe[newship].bravery = rnd / 2;
1049 universe[newship].flags |= FLG_HAS_ECM;
1052 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1057 void create_lone_hunter (void)
1063 if ((cmdr.mission == 1) && (cmdr.galaxy_number == 1) &&
1064 (docked_planet.d == 144) && (docked_planet.b == 33) &&
1065 (ship_count[SHIP_CONSTRICTOR] == 0))
1067 type = SHIP_CONSTRICTOR;
1072 type = SHIP_COBRA3_LONE + (rnd & 3) + (rnd > 127);
1075 newship = create_other_ship (type);
1079 // universe[newship].flags = FLG_ANGRY;
1080 if ((rand255() > 200) || (type == SHIP_CONSTRICTOR))
1081 universe[newship].flags |= FLG_HAS_ECM;
1083 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1084 if (type == SHIP_FER_DE_LANCE) {
1085 if (rand255() > 160)
1086 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1088 universe[newship].rotmat[2].z = -1.0;
1089 universe[newship].rotz = rand255() & 7;
1091 universe[newship].velocity = (rnd & 31) | 16;
1095 if (universe[newship].flags & FLG_ANGRY)
1102 /* Check for a random asteroid encounter... */
1104 void check_for_asteroids (void)
1109 if ((rand255() >= 35) || (ship_count[SHIP_ASTEROID] >= 3))
1112 if (rand255() > 253)
1115 type = SHIP_ASTEROID;
1117 newship = create_other_ship (type);
1121 // universe[newship].velocity = (rand255() & 31) | 16;
1122 universe[newship].velocity = 8;
1123 universe[newship].rotz = rand255() > 127 ? -127 : 127;
1124 universe[newship].rotx = 16;
1130 /* If we've been a bad boy then send the cops after us... */
1132 void check_for_cops (void)
1137 offense = carrying_contraband() * 2;
1138 if (ship_count[SHIP_VIPER] == 0)
1139 offense |= cmdr.legal_status;
1141 if (rand255() >= offense)
1144 newship = create_other_ship (SHIP_VIPER);
1148 universe[newship].flags |= FLG_ANGRY;
1149 if (rand255() > 245)
1150 universe[newship].flags |= FLG_HAS_ECM;
1152 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1157 void check_for_others (void)
1167 gov = current_planet_data.government;
1170 if ((gov != 0) && ((rnd >= 90) || ((rnd & 7) < gov)))
1173 if (rand255() < 100)
1175 create_lone_hunter();
1179 /* Pack hunters... */
1181 set_init_matrix (rotmat);
1184 x = 1000 + (randint() & 8191);
1185 y = 1000 + (randint() & 8191);
1187 if (rand255() > 127)
1189 if (rand255() > 127)
1192 rnd = rand255() & 3;
1194 for (i = 0; i <= rnd; i++)
1196 type = SHIP_SIDEWINDER + (rand255() & rand255() & 7);
1197 newship = add_new_ship (type, x, y, z, rotmat, 0, 0);
1200 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1201 if (rand255() > 245)
1202 universe[newship].flags |= FLG_HAS_ECM;
1204 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1212 void random_encounter (void)
1214 if ((ship_count[SHIP_CORIOLIS] != 0) || (ship_count[SHIP_DODEC] != 0))
1217 if (rand255() == 136)
1219 if (((int)(universe[0].location.z) & 0x3e) != 0)
1227 if ((rand255() & 7) == 0)
1233 check_for_asteroids();
1237 if (ship_count[SHIP_VIPER] != 0)
1243 if ((cmdr.mission == 5) && (rand255() >= 200))
1250 void abandon_ship (void)
1254 cmdr.escape_pod = 0;
1255 cmdr.legal_status = 0;
1256 cmdr.fuel = myship.max_fuel;
1258 for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
1259 cmdr.current_cargo[i] = 0;
1261 snd_play_sample (SND_DOCK);
1263 current_screen = SCR_BREAK_PATTERN;