Mark Sachs (ksleet) wrote,
Mark Sachs
ksleet

  • Mood:
  • Music:

And when the instructions called for a hammer, I just used my forehead.

Why am I trying to implement a flight simulator in Inform?!


Because it's easier than implementing a flight simulator in C++ and DirectX, maybe?

At one point in Star City I've decided to put the player in a glider and require them to safely land it. Rather than choreograph the whole trip, I'm implementing a bare-bones simulation: the player can adjust the glider's pitch, yaw, and rudder to manage the descent, and "solves the puzzle" by putting the glider on the ground in level flight at under 80 knots. (Just to add interest it's also possible to stall the glider or even put it into a spin if it is flown irresponsibly, or I feel like screwing with you.)

There are measures that can be taken to simplify this problem. For example, there are no meaningful ground features, so I don't need to keep track of how far the glider moves horizontally at all. However, I can't get out of needing to keep track of how fast the glider descends every turn. This in turn depends on what I'm calling the "attitude" although that isn't strictly correct terminology: the angle at which the aircraft is descending. In ordinary programming, this would be a fairly trivial little trigonometry problem involving taking the sine of the attitude and multiplying it by the airspeed, yielding the vertical component of the speed. This works because in a triangle the sine of an angle is equal to the length of the opposite side divided by the length of the hypoteneuse. In this case the opposite side is the descent rate, the hypoteneuse is the airspeed, and the angle is the attitude, so we can rearrange this and make it possible to solve for the descent rate.

But Inform 7 is not a normal programming language. It is almost aggressively anti-mathematical, featuring no trig functions, no floating point values and actually restricting integer values to between -32,768 and 32,767. How is it possible to do any useful math under these conditions?

Table of Sine Approximations
angle      value
0          0
[...]
45         71

To decide what number is descent rate:
	if the attitude is greater than 45, let theta be 90 minus the attitude;
	otherwise, let theta be the attitude;
	let x be the airspeed times the value corresponding to an angle of theta
           in the Table of Sine Approximations;
	decide on x divided by 100.


We use a table of sine values (a seriously old-school technique, to be sure), but multiplied by 100: for example, the sine of 45 degrees is about .7107, but we return a value of 71. We can use the table to perform the operation, then divide by 100 again to spit out a result which, while rounded off, is still close enough for my purposes. Assuming I never try to get cute and supply an attitude value less than 0 or greater than 90, this should work. Now, does this work...? Well, I'm not sure yet. But it looks plausible, at any rate.

More generally the idea of using Inform 7 to simulate unexpected game genres fascinates me. To be honest, when it comes to making my own games I've always been far more interested in the story than in the code, and if it's possible to make these stories come to life so much more easily in Inform I'm willing to play a few rounds of Stump the Compiler to make it happen.
Tags: if
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 6 comments