Destroying aliens at close range can have serious consequences
When we destroy an alien, two things happen: an explosion, and turbulence. Let's look at how these are implemented.
Alien explosions are handled by the ExplodeAlien routine, which is called from part 5 of the main loop on every iteration around the loop. When we kill an alien, the hitTimer variable is used as a timer that counts down for the duration of the explosion, so the first thing the explosion routine does is to confirm that this timer is non-zero.
Assuming we do indeed have an exploding alien on our hands, the next step is to make the sound of an alien exploding, and then we move onto the process of splitting the alien's lines apart on-screen.
There are four different 3D objects used for the various stages of alien. Object 30 contains dormant aliens, objects 31 and 32 are feeding aliens, and object 33 is the attacking alien. These alien objects consist of the following object points:
- Object 30 consists of points 178 to 180
- Object 31 consists of points 183 to 186
- Object 32 consists of points 188 to 191
- Object 33 consists of points 193 to 200
These point ranges are stored in the explodeFrom and explodeTo tables. The point IDs are stored in explicit tables as there's no way to extract the points in an object from objectPoints given the object ID. See the deep dive on 3D objects for more on this.
To explode one of these alien objects, we can alter the coordinates for the relevant points, and this will make the object change on-screen. Note that we don't change the object definitions in the (xObjectPoint, yObjectPoint, zObjectPoint) table, as that would mean we couldn't spawn new objects based on that design; instead we change the projected points in the (xPoint, yPoint, zPoint) table for this specific object.
The first step is to extract the alien's feeding state, so we know how much to move the points (as aliens in later feeding stages are bigger). Once we have this scale factor, we generate a random number for each axis, scaled according to the alien's size, and randomly add or subtract it to the point's coordinate in that axis. Working our way through all the points, we end up randomising all the lines that make up the alien.
This process is repeated by the ExplodeAlien routine on every iteration of the main loop, for the duration of the explosion. As mentioned above, the explosion is tracked via the hitTimer variable, so the last step is to decrement hitTimer to progress the explosion. If it hits zero, then we call the ScoreHitPoints routine to award the correct number of points, depending on the alien's feeding stage (and the next time we call ExplodeAlien, it won't do anything).
Near or far
If this is the first stage of the explosion, there is one more task to do. We talk about applying turbulence to the plane in the next section, and the closer we are to the exploding alien, the more powerful the turbulence. We therefore need to calculate the distance to the explosion, and we do this at the end of the ExplodeAlien routine, on the first call of a new explosion.
The distance is calculated by taking the z-coordinate of the last point that we applied a random movement to. These coordinates have already been rotated into the plane's frame of reference, so they are relative to the plane, so this gives us the distance between that point of the exploding alien and the plane, in the z-axis (with the z-axis pointing out of the plane's nose, as we are in the plane's frame of reference).
To put it another way, this measures how far the explosion is in front of the plane - we ignore how far to the side the explosion is. If the alien is flying rather than sitting in a field, we subtract 8 from this value to make the turbulence even greater, and then we store the result in distanceFromHit, for use in the next section.
The closer we are to an exploding alien, the more intense the turbulence from the explosion. Turbulence is applied to the plane as part of the flight model calculations, in part 3 of the ApplyFlightModel routine.
There are two parts to the calculation. First we set the level of turbulence in the range 0 to 1920, to be inversely proportional to the distance from the explosion in distanceFromHit, which gets set in the explosion routine described above. This means the level of turbulence is set to 0 if we are far away, or 1920 if we are very close.
We then randomly add or subtract this amount from the angular forces from the plane's controls, one axis at a time, with the sign of each axis calculation being individually randomised. This gives the following:
xControlsSc = xControlsSc +/- turbulence / 2 yControlsSc = yControlsSc +/- turbulence / 2 zControlsSc = zControlsSc +/- turbulence / 2
These variables contain the following angular forces, after they have been scaled as part of the flight model calculations:
- xControlsSc: Pitching moment due to elevator deflection
- yControlsSc: Yawing moment due to rudder demand
- zControlsSc: Rolling moment due to aileron deflection
In terms of the flight model, then, turbulence is applied to the plane as if someone has grabbed the stick and rudder pedals and randomly thrown them around. This goes on for the duration of the explosion, so if we're close to the ground when we take out an alien, things can get very hairy indeed...