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 (!(universe[un].flags & FLG_TARGET) &&
326 (universe[1].type == SHIP_CORIOLIS ||
327 universe[1].type == SHIP_DODEC))
328 universe[1].flags |= FLG_ANGRY;
330 if (type > SHIP_ROCK)
332 universe[un].rotx = 4;
333 universe[un].acceleration = 2;
334 universe[un].flags |= FLG_ANGRY | FLG_TACTICAL;
339 void explode_object (int un)
344 if ((cmdr.score & 255) == 0)
345 info_message ("Right On Commander!");
347 snd_play_sample (SND_EXPLODE);
348 universe[un].flags |= FLG_DEAD;
350 if (universe[un].type == SHIP_CONSTRICTOR)
355 void check_target (int un, struct univ_object *flip)
357 struct univ_object *univ;
359 univ = &universe[un];
361 if (in_target (univ->type, flip->location.x, flip->location.y, flip->location.z))
363 if ((missile_target == MISSILE_ARMED) && (univ->type >= 0))
366 info_message ("Target Locked");
367 snd_play_sample (SND_BEEP);
372 snd_play_sample (SND_HIT_ENEMY);
374 if ((univ->type != SHIP_CORIOLIS) && (univ->type != SHIP_DODEC))
376 if ((univ->type == SHIP_CONSTRICTOR) || (univ->type == SHIP_COUGAR))
378 if (laser == (MILITARY_LASER & 127))
379 univ->energy -= laser / 4;
383 univ->energy -= laser;
387 if (univ->energy <= 0)
391 if (univ->type == SHIP_ASTEROID)
393 if (laser == (MINING_LASER & 127))
394 launch_loot (un, SHIP_ROCK);
398 launch_loot (un, SHIP_ALLOY);
399 launch_loot (un, SHIP_CARGO);
410 void activate_ecm (int ours)
416 snd_play_sample (SND_ECM);
427 decrease_energy (-1);
432 void arm_missile (void)
434 if ((cmdr.missiles != 0) && (missile_target == MISSILE_UNARMED))
435 missile_target = MISSILE_ARMED;
439 void unarm_missile (void)
441 missile_target = MISSILE_UNARMED;
442 snd_play_sample (SND_BOOP);
445 void fire_missile (void)
448 struct univ_object *ns;
451 if (missile_target < 0)
454 set_init_matrix (rotmat);
458 newship = add_new_ship (SHIP_MISSILE, 0, -28, 14, rotmat, 0, 0);
462 info_message ("Missile Jammed");
466 ns = &universe[newship];
468 ns->velocity = flight_speed * 2;
469 ns->flags |= FLG_TACTICAL;
470 ns->target = missile_target;
472 if (universe[missile_target].type > SHIP_ROCK)
473 universe[missile_target].flags |= FLG_ANGRY;
476 missile_target = MISSILE_UNARMED;
478 snd_play_sample (SND_MISSILE);
483 void track_object (struct univ_object *ship, double direction, Vector nvec)
492 dir = vector_dot_product (&nvec, &ship->rotmat[1]);
494 if (direction < -0.861)
496 ship->rotx = (dir < 0) ? 7 : -7;
503 if ((fabs(dir) * 2) >= rat2)
505 ship->rotx = (dir < 0) ? rat : -rat;
508 if (abs(ship->rotz) < 16)
510 dir = vector_dot_product (&nvec, &ship->rotmat[0]);
514 if ((fabs(dir) * 2) > rat2)
516 ship->rotz = (dir < 0) ? rat : -rat;
519 ship->rotz = -ship->rotz;
526 void missile_tactics (int un)
528 struct univ_object *missile;
529 struct univ_object *target;
535 missile = &universe[un];
539 snd_play_sample (SND_EXPLODE);
540 missile->flags |= FLG_DEAD;
544 if (missile->target == 0)
546 if (missile->distance < 256)
548 missile->flags |= FLG_DEAD;
549 snd_play_sample (SND_EXPLODE);
550 damage_ship (250, missile->location.z >= 0.0);
554 vec.x = missile->location.x;
555 vec.y = missile->location.y;
556 vec.z = missile->location.z;
560 target = &universe[missile->target];
562 vec.x = missile->location.x - target->location.x;
563 vec.y = missile->location.y - target->location.y;
564 vec.z = missile->location.z - target->location.z;
566 if ((fabs(vec.x) < 256) && (fabs(vec.y) < 256) && (fabs(vec.z) < 256))
568 missile->flags |= FLG_DEAD;
570 if ((target->type != SHIP_CORIOLIS) && (target->type != SHIP_DODEC))
571 explode_object (missile->target);
573 snd_play_sample (SND_EXPLODE);
578 if ((rand255() < 16) && (target->flags & FLG_HAS_ECM))
585 if ((rand255() < 16) &&
586 (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])) {
591 nvec = unit_vector(&vec);
592 direction = vector_dot_product (&nvec, &missile->rotmat[2]);
596 direction = -direction;
598 track_object (missile, direction, nvec);
600 if (direction <= -0.167)
602 missile->acceleration = -2;
606 if (direction >= cnt2)
608 missile->acceleration = 3;
612 if (missile->velocity < 6)
613 missile->acceleration = 3;
615 if (rand255() >= 200)
616 missile->acceleration = -2;
622 void launch_shuttle (void)
626 if ((ship_count[SHIP_TRANSPORTER] != 0) ||
627 (ship_count[SHIP_SHUTTLE] != 0) ||
628 (rand255() < 253) || (auto_pilot))
631 type = rand255() & 1 ? SHIP_SHUTTLE : SHIP_TRANSPORTER;
632 launch_enemy (1, type, FLG_HAS_ECM | FLG_FLY_TO_PLANET, 113);
636 void tactics (int un)
642 struct univ_object *ship;
648 ship = &universe[un];
652 if ((type == SHIP_PLANET) || (type == SHIP_SUN))
655 if (flags & FLG_DEAD)
658 if (flags & FLG_INACTIVE)
661 if (type == SHIP_MISSILE)
663 if (flags & FLG_ANGRY)
664 missile_tactics (un);
668 if (((un ^ mcount) & 7) != 0)
671 if ((type == SHIP_CORIOLIS) || (type == SHIP_DODEC))
673 if (flags & FLG_ANGRY)
675 if ((rand() & 255) < 240)
678 if (ship_count[SHIP_VIPER] >= 4)
681 launch_enemy (un, SHIP_VIPER, FLG_ANGRY | FLG_HAS_ECM, 113);
689 if (type == SHIP_HERMIT)
693 launch_enemy (un, SHIP_SIDEWINDER + (rand255() & 3), FLG_ANGRY | FLG_HAS_ECM, 113);
694 ship->flags |= FLG_INACTIVE | FLG_TARGET;
701 if (ship->energy < ship_list[type]->energy)
704 if ((type == SHIP_THARGLET) && (ship_count[SHIP_THARGOID] == 0))
706 ship->flags &= FLG_TARGET | FLG_TACTICAL;
711 if (flags & FLG_SLOW)
717 if (flags & FLG_POLICE)
719 if (cmdr.legal_status >= 64)
721 flags |= FLG_ANGRY | FLG_TARGET;
726 if ((flags & FLG_ANGRY) == 0)
728 if ((flags & FLG_FLY_TO_PLANET) || (flags & FLG_FLY_TO_STATION))
730 auto_pilot_ship (&universe[un]);
737 /* If we get to here then the ship is angry so start attacking... */
739 if (ship_count[SHIP_CORIOLIS] || ship_count[SHIP_DODEC])
741 if ((flags & FLG_BOLD) == 0)
746 if (type == SHIP_ANACONDA)
750 launch_enemy (un, rand255() > 100 ? SHIP_WORM : SHIP_SIDEWINDER,
751 FLG_ANGRY | FLG_HAS_ECM, 113);
757 if (rand255() >= 250)
759 ship->rotz = rand255() | 0x68;
760 if (ship->rotz > 127)
761 ship->rotz = -(ship->rotz & 127);
764 maxeng = ship_list[type]->energy;
765 energy = ship->energy;
767 if (energy < (maxeng / 2))
769 if ((energy < (maxeng / 8)) && (rand255() > 230) && (type != SHIP_THARGOID))
771 ship->flags &= ~FLG_ANGRY;
772 ship->flags |= FLG_INACTIVE;
773 launch_enemy (un, SHIP_ESCAPE_CAPSULE, 0, 126);
777 if ((ship->missiles != 0) && (ecm_active == 0) &&
778 (ship->missiles >= (rand255() & 31)))
781 ship->flags |= FLG_TACTICAL;
782 if (type == SHIP_THARGOID)
783 launch_enemy (un, SHIP_THARGLET, FLG_ANGRY, ship->bravery);
786 launch_enemy (un, SHIP_MISSILE, FLG_ANGRY, 126);
787 ship->flags |= FLG_HOSTILE;
788 info_message ("INCOMING MISSILE");
794 nvec = unit_vector(&universe[un].location);
795 direction = vector_dot_product (&nvec, &ship->rotmat[2]);
797 if ((ship->distance < 8192) && (direction <= -0.833) &&
798 (ship_list[type]->laser_strength != 0))
800 if (direction <= -0.917)
801 ship->flags |= FLG_FIRING | FLG_HOSTILE | FLG_TACTICAL;
803 if (direction <= -0.972)
805 damage_ship (ship_list[type]->laser_strength, ship->location.z >= 0.0);
806 ship->acceleration--;
807 if (((ship->location.z >= 0.0) && (front_shield == 0)) ||
808 ((ship->location.z < 0.0) && (aft_shield == 0)))
809 snd_play_sample (SND_INCOMMING_FIRE_2);
811 snd_play_sample (SND_INCOMMING_FIRE_1);
818 direction = -direction;
819 track_object (&universe[un], direction, nvec);
822 // if ((fabs(ship->location.z) < 768) && (ship->bravery <= ((rand255() & 127) | 64)))
823 if (fabs(ship->location.z) < 768)
825 ship->rotx = rand255() & 0x87;
826 if (ship->rotx > 127)
827 ship->rotx = -(ship->rotx & 127);
829 ship->acceleration = 3;
833 if (ship->distance < 8192)
834 ship->acceleration = -1;
836 ship->acceleration = 3;
842 if ((fabs(ship->location.z) >= 768) ||
843 (fabs(ship->location.x) >= 512) ||
844 (fabs(ship->location.y) >= 512))
846 if (ship->bravery > (rand255() & 127))
852 direction = -direction;
856 track_object (&universe[un], direction, nvec);
858 if ((attacking == 1) && (ship->distance < 2048))
860 if (direction >= cnt2)
862 ship->acceleration = -1;
866 if (ship->velocity < 6)
867 ship->acceleration = 3;
869 if (rand255() >= 200)
870 ship->acceleration = -1;
874 if (direction <= -0.167)
876 ship->acceleration = -1;
880 if (direction >= cnt2)
882 ship->acceleration = 3;
886 if (ship->velocity < 6)
887 ship->acceleration = 3;
889 if (rand255() >= 200)
890 ship->acceleration = -1;
894 void draw_laser_lines (void)
898 gfx_draw_colour_line (32 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
899 gfx_draw_colour_line (48 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
900 gfx_draw_colour_line (208 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
901 gfx_draw_colour_line (224 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, GFX_COL_WHITE);
905 gfx_draw_triangle (32 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, 48 * GFX_SCALE, GFX_VIEW_BY, GFX_COL_RED);
906 gfx_draw_triangle (208 * GFX_SCALE, GFX_VIEW_BY, laser_x, laser_y, 224 * GFX_SCALE, GFX_VIEW_BY, GFX_COL_RED);
911 int fire_laser (void)
913 if ((laser_counter == 0) && (laser_temp < 242))
915 switch (current_screen)
918 laser = cmdr.front_laser;
922 laser = cmdr.rear_laser;
926 laser = cmdr.right_laser;
930 laser = cmdr.left_laser;
939 laser_counter = (laser > 127) ? 0 : (laser & 0xFA);
943 snd_play_sample (SND_PULSE);
948 laser_x = ((rand() & 3) + 128 - 2) * GFX_SCALE;
949 laser_y = ((rand() & 3) + 96 - 2) * GFX_SCALE;
959 void cool_laser (void)
966 if (laser_counter > 0)
969 if (laser_counter > 0)
974 int create_other_ship (int type)
980 set_init_matrix (rotmat);
983 x = 1000 + (randint() & 8191);
984 y = 1000 + (randint() & 8191);
991 newship = add_new_ship (type, x, y, z, rotmat, 0, 0);
997 void create_thargoid (void)
1001 newship = create_other_ship (SHIP_THARGOID);
1004 universe[newship].flags = FLG_ANGRY | FLG_HAS_ECM | FLG_TARGET;
1005 universe[newship].bravery = 113;
1008 launch_enemy (newship, SHIP_THARGLET, FLG_ANGRY | FLG_HAS_ECM,
1016 void create_cougar (void)
1020 if (ship_count[SHIP_COUGAR] != 0)
1023 newship = create_other_ship (SHIP_COUGAR);
1026 universe[newship].flags = FLG_HAS_ECM; // | FLG_CLOAKED;
1027 universe[newship].bravery = 121;
1028 universe[newship].velocity = 18;
1034 void create_trader (void)
1040 type = SHIP_COBRA3 + (rand255() & 3);
1042 newship = create_other_ship (type);
1046 universe[newship].rotmat[2].z = -1.0;
1047 universe[newship].rotz = rand255() & 7;
1050 universe[newship].velocity = (rnd & 31) | 16;
1051 universe[newship].bravery = rnd / 2;
1054 universe[newship].flags |= FLG_HAS_ECM;
1056 if (rnd > (type == SHIP_ANACONDA ? 250 : 220))
1057 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1062 void create_lone_hunter (void)
1068 if ((cmdr.mission == 1) && (cmdr.galaxy_number == 1) &&
1069 (docked_planet.d == 144) && (docked_planet.b == 33) &&
1070 (ship_count[SHIP_CONSTRICTOR] == 0))
1072 type = SHIP_CONSTRICTOR;
1077 type = SHIP_COBRA3_LONE + (rnd & 3) + (rnd > 127);
1080 newship = create_other_ship (type);
1084 // universe[newship].flags = FLG_ANGRY;
1085 if ((rand255() > 200) || (type == SHIP_CONSTRICTOR))
1086 universe[newship].flags |= FLG_HAS_ECM;
1088 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1089 if (type == SHIP_FER_DE_LANCE) {
1090 if (rand255() > 160)
1091 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1093 universe[newship].rotmat[2].z = -1.0;
1094 universe[newship].rotz = rand255() & 7;
1096 universe[newship].velocity = (rnd & 31) | 16;
1100 if (universe[newship].flags & FLG_ANGRY)
1107 /* Check for a random asteroid encounter... */
1109 void check_for_asteroids (void)
1114 if ((rand255() >= 35) || (ship_count[SHIP_ASTEROID] >= 3))
1117 if (rand255() > 253)
1120 type = SHIP_ASTEROID;
1122 newship = create_other_ship (type);
1126 // universe[newship].velocity = (rand255() & 31) | 16;
1127 universe[newship].velocity = 8;
1128 universe[newship].rotz = rand255() > 127 ? -127 : 127;
1129 universe[newship].rotx = 16;
1135 /* If we've been a bad boy then send the cops after us... */
1137 void check_for_cops (void)
1142 offense = carrying_contraband() * 2;
1143 if (ship_count[SHIP_VIPER] == 0)
1144 offense |= cmdr.legal_status;
1146 if (rand255() >= offense)
1149 newship = create_other_ship (SHIP_VIPER);
1153 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1154 if (rand255() > 245)
1155 universe[newship].flags |= FLG_HAS_ECM;
1157 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1162 void check_for_others (void)
1172 gov = current_planet_data.government;
1175 if ((gov != 0) && ((rnd >= 90) || ((rnd & 7) < gov)))
1178 if (rand255() < 100)
1180 create_lone_hunter();
1184 /* Pack hunters... */
1186 set_init_matrix (rotmat);
1189 x = 1000 + (randint() & 8191);
1190 y = 1000 + (randint() & 8191);
1192 if (rand255() > 127)
1194 if (rand255() > 127)
1197 rnd = rand255() & 3;
1199 for (i = 0; i <= rnd; i++)
1201 type = SHIP_SIDEWINDER + (rand255() & rand255() & 7);
1202 newship = add_new_ship (type, x, y, z, rotmat, 0, 0);
1205 universe[newship].flags |= FLG_ANGRY | FLG_TARGET;
1206 if (rand255() > 245)
1207 universe[newship].flags |= FLG_HAS_ECM;
1209 universe[newship].bravery = ((rand255() * 2) | 64) & 127;
1217 void random_encounter (void)
1219 if ((ship_count[SHIP_CORIOLIS] != 0) || (ship_count[SHIP_DODEC] != 0))
1222 if (rand255() == 136)
1224 if (((int)(universe[0].location.z) & 0x3e) != 0)
1232 if ((rand255() & 7) == 0)
1238 check_for_asteroids();
1242 if (ship_count[SHIP_VIPER] != 0)
1248 if ((cmdr.mission == 5) && (rand255() >= 200))
1255 void abandon_ship (void)
1259 cmdr.escape_pod = 0;
1260 cmdr.legal_status = 0;
1261 cmdr.fuel = myship.max_fuel;
1263 for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
1264 cmdr.current_cargo[i] = 0;
1266 snd_play_sample (SND_DOCK);
1268 current_screen = SCR_BREAK_PATTERN;