10. Physics: Substances, Ropes, Energy and Weight

Recipe Book

RB §10.1 Gases

Inform normally assumes that everything is solid. It has no built-in support for gases or liquids, because they have so many different behaviours. For instance, is the important thing about gas that it diffuses, or that we breathe it, or that it mixes with other gases to react, or that it sometimes obscures vision? The answer depends on what we are trying to write, and for this reason Inform leaves it up to us.

Gases are easier to deal with than liquids, because they tend to be everywhere in their location at once (unlike a liquid which might form a pool on the floor) and because they diffuse and mix by themselves (rather than being carried around or brought into contact with each other by the player). On the other hand, unlike liquids, gases are compressible: they can be present at low or high pressures, that is, in low or high concentrations.

The simplest approach is the one in Only You… ★★★, where rooms are either filled with smoke or else smoke-free. Smoke gradually fills through the map, obscuring vision: no attempt is made to conserve the total quantity of smoke, as we assume that some fire is churning it out continuously to replace what diffuses away.

Lethal Concentration ★★ and Lethal Concentration 2 ★★★ simulate a gas diffusing through a three-dimensional maze of rooms, and becoming dangerous above certain concentrations. There is just one possible gas, and it is modelled by giving each room a number which represents the concentration (in parts per million). This enables us to conserve the total amount of gas, or to have it released or captured by sources and sinks of given capacity.

This could be extended by giving each room similar concentration levels for other gases, and providing for the diffusion rule to notice when different gases come into contact; or by giving a concentration (and also, for realism, a volume) to each closed container, applying rules for capturing and releasing gases as containers are opened and closed.

Examples

A poisonous gas that spreads from room to room, incapacitating or killing the player when it reaches sufficient levels.
74.
Only You... ★★★
Smoke which spreads through the rooms of the map, but only every other turn.
Poisonous gas again, only this time it sinks.

RB §10.2 Liquids

Liquids are notoriously difficult to simulate well. A fully thorough approach consumes endless storage and can be very finicky to write and keep realistic. It is essential to decide what aspect of a liquid's behaviour is actually needed in a given story, and to simulate only that. For instance, if we only need a little chemistry, where a player can add (say) water to salt and make a solution, we do not want to fool around with calculating quantities or concentrations: what's important is that "some water" (amount unspecified) combines with "some salt" to produce "some salty water". We should no more calculate precisely here than we would work out where all the furniture is to the nearest inch. Good advice for handling liquids is to simulate the least amount of realism possible, but no less.

Sometimes all we want is a down-in-one drink: we needn't simulate the actual liquid, just the bottle it comes in, and all we need is to handle the "drinking" action. See Beverage Service , and also 3 AM ★★★, where carbonated drinks can be shaken – again simulating the vessel, not the liquid.

Some elementary biochemistry in Xylan ★★ is done simply by… well, the point is that two different liquids are represented by single things each, and a chemical reaction simply switches one for the other.

In Frizz ★★★, we allow any container to be filled with water (only) and we simulate what happens to any solid objects also inside: some waterproof, some not. Flotation provides a well (always full of water), with rules to determine whether things dropped into it should sink or float.

Next we move up to quantitative approaches, where we remember not just whether a liquid is present, but how much of it. In its simplest form, we could have a drinking vessel from which we draw in sips, so that it can be full, half-empty or empty: see Thirst .

The example with the best compromise between simulation quality and complexity is Lemonade ★★★. Here we provide a kind of container called a "fluid container", not just a single cup, and each such vessel has a given "fluid capacity". Each holds only a single liquid at a time (so no mixtures) and can be empty or full to any level (rounded off to the nearest 0.1 fl oz). We can fill one vessel from another (unless it would make a mixture). But liquids leaving these vessels must be consumed – drunk or poured away without trace: we cannot make pools on the floor, or carry liquids in our cupped hands. There is no object representing "lemonade": there are only fluid containers, but which can be called LEMONADE if that is what they now contain.

Savannah ★★★ is a light elaboration of Lemonade, showing how liquids might be poured on other objects, as for instance to extinguish a fire.

Noisy Cricket ★★★ extends Lemonade ★★★ to allow for mixing, though then the number of different possible mixtures is so large that complexity increases greatly. Lakeside Living ★★★ extends Lemonade ★★★ differently to add a "liquid source" kind, a form of fluid container which has infinite fluid capacity and is scenery – ideal for a lake, river or spring.

See Also

Bags, Bottles, Boxes and Safes for stoppered bottles which could also be used for carrying liquids around in.
Heat for keeping liquids warm in insulated containers.

Examples

117.
Thirst
A waterskin that is depleted as the player drinks from it.
A potion that the player can drink.
402.
Objects that can sink or float in a well, depending on their own properties and the state of the surrounding environment.
288.
Xylan ★★
Creating a new command that does require an object to be named; and some comments about the choice of vocabulary, in general.
192.
Frizz ★★★
Liquid flows within containers and soaks objects that are not waterproof; any contact with a wet object can dampen our gloves.
193.
3 AM ★★★
A shake command which agitates soda and makes items thump around in boxes.
266.
Lemonade ★★★
Containers for liquid which keep track of how much liquid they are holding and of what kind, and allow quantities to be moved from one container to another.
267.
Savannah ★★★
Using the liquid implementation demonstrated in Lemonade for putting out fires.
276.
Noisy Cricket ★★★
Implementing liquids that can be mixed, and the components automatically recognized as matching one recipe or another.
333.
Lakeside Living ★★★
Similar to "Lemonade", but with bodies of liquid that can never be depleted, and some adjustments to the "fill" command so that it will automatically attempt to fill from a large liquid source if possible.

RB §10.3 Dispensers and Supplies of Small Objects

A slightly tricky situation arises in IF when we want to offer the player a simulation of a near-infinite supply of something: a napkin dispenser from which he can keep taking more napkins, or an infinite selection of pebbles on a beach, or something of that nature.

One approach is simply to limit the number of items the player is allowed to pick up at a time, while maintaining the fiction that there are more of these items in existence than the player is allowed to interact with. Extra Supplies ★★ demonstrates this.

The task becomes harder if we do want to let the player have as many napkins as he wants. In some languages, it is possible to generate new objects on the fly after the story has begun (called "dynamic object creation"), and something like this is possible if we are compiling for Glulx. (See the Inform extensions site for examples.) Usually, though, it is less complicated and almost as effective simply to have a very large supply of existing objects, which are moved in and out of play as the player needs them. Pizza Prince demonstrates how to do this with slices of pizza.

See Also

Ropes for an example involving divisible pieces of string, which relies on similar techniques.

Examples

378.
Providing a pizza buffet from which the player can take as many pieces as he wants.
124.
A supply of red pens from which the player can take another pen only if he doesn't already have one somewhere in the game world.

RB §10.4 Glass and Other Damage-Prone Substances

Just as Inform normally assumes everything is solid, it also assumes that these solid objects will not buckle, crack, break or deform under pressure, and cannot be fragmented. But breakability adds realism, and breakage need not be negative: sometimes we want the player to break her way in to something.

In IF the word DROP is more often used to mean "put down" or "leave behind" than "let go from a height": so it is perhaps unfair that in Ming Vase something fragile, when dropped, shatters (into nothing). In Spring Cleaning , fragile objects must be explicitly attacked by the player in order to break, and although they leave no tangible debris behind, their loss is at least remembered. Kyoto ★★ provides a general-purpose model for things being thrown at other things, with consequences including things moving (even between rooms) as well as breaking each other.

Debris from breakages is to be avoided if possible because it means keeping track of increasing numbers of objects. But we can increase realism by allowing something to have a visibly "broken" state, which it changes to when it breaks, rather than simply vanishing. Terracottissima ★★ provides for broken and unbroken flowerpots in this way.

Since "part of" allows us to have two objects joined together into what the player sees as one, it also gives us a natural seam which allows the whole to be broken back down into its component parts, and this is the neatest way of providing a breakage into pieces. Paddington ★★ demonstrates a cutting action which allows component parts to be cut away from their holders but will only make small surface gashes in any individual thing: so the player can cut something up, but only into the pieces we specifically choose to allow. Cutting also forces an opening into containers.

See Also

Combat and Death for a robot that breaks into its component limbs when shot with a blaster.
Goal-Seeking Characters for a character who eats donuts, leaving crumbs on the floor.
Volume, Height, Weight for containers breaking under the weight of their contents.
Ropes for cutting up string into up to 35 different pieces of different lengths - a limit the player is unlikely to find out about, but a limit all the same, and an expensive solution since we need 35 different things for the "debris" when string is "broken".

Examples

97.
ATTACK or DROP break and remove fragile items from play.
A character who sulks over objects that the player has broken (and which are now off-stage).
195.
Paddington ★★
A CUT [something] WITH [something] command which acts differently on different types of objects.
314.
The flowerpots once again, but this time arranged so that after the first breakage all undamaged pots are said to be "unbroken", to distinguish them from the others.
403.
Kyoto ★★
Expanding the effects of the THROW something AT something command so that objects do make contact with one another.

RB §10.5 Volume, Height, Weight

What should fit into what? Inform has basically three sizes: small, person-sized, and room-sized. The difference between "small" and "person-sized" doesn't appear much, but it's the difference between an ordinary container and an enterable container; the fact that a person cannot get inside an ordinary container is one of the few size-related rules built into Inform. It will not object to, say, a fishing rod being put inside a matchbox.

Inform does have one built-in measure of the size of a container: its "carrying capacity". This is a maximum number of contents:

The carrying capacity of the rucksack is 3.

This of course allows three anvils, while forbidding four postage stamps. To do better, we need units of measurement, and Dimensions ★★ demonstrates setting these up. The Speed of Thought ★★, meanwhile, ventures into the area of unit conversion: having multiple types of unit and being able to express them to the player, or parse these in the player's input.

To be fully realistic in what will fit into what, we need sophisticated three-dimensional models of shapes, both of the items being carried and of the free space remaining inside containers. Depth elegantly simplifies this by approximating items as cuboids, with a given width, length and height: these multiply to give a volume. To fit in a container, a new item's volume must not exceed the volume remaining inside the container, and in addition its three dimensions must also fit in one of the possible arrangements at right angles to the sides. (So this system would indeed prevent a 1x1x100 fishing rod from being put inside a 5x2x1 matchbox, but would also prevent a 12x1x1 pencil from being put into a 10x10x1 box, because it would need to be turned diagonally to fit.)

Lead Cuts Paper ★★★ provides a different constraint: here we do not let light-weight containers hold heavy objects.

Weight comes in a different way into Swerve left? Swerve right? Or think about it and die? , which exploits up/down map connections to work out which way gravity would take a rolling marble.

See Also

Liquids for containers with liquid capacity.

Examples

Building a marble chute track in which a dropped marble will automatically roll downhill.
268.
Depth
Receptacles that calculate internal volume and the amount of room available, and cannot be overfilled.
258.
Dimensions ★★
This example draws together the previous snippets into a working implementation of the weighbridge.
Describing scientifically-measured objects in units more familiar to the casual audience.
259.
Lead Cuts Paper ★★★
To give every container a breaking strain, that is, a maximum weight of contents which it can bear – so that to put the lead pig into a paper bag invites disaster.

RB §10.6 Ropes

Ropes, chains and similar long, thin, bendable items present three problems: they are like a liquid in that (unless unbreakable) they can be divided arbitrarily into smaller and smaller portions of themselves, they can be in two or more places at once (even in two or more rooms at once), and they can be tied down at either or both ends, allowing them to occupy an uneasy state in between being "portable" and "fixed in place". Even when all this is simulated, they allow us to pull on one end and so to exert force at the other – allowing action-at-a-distance which Inform's realism rules would ordinarily forbid. Ropes are hard. And it is very difficult to imagine everything a player might try when given a fully practical rope with which to solve puzzles.

Snip ★★★ solves the divisibility question, allowing string to be cut or retied into lengths of any size, with all the consequences for describing and understanding that this entails.

Otranto ★★★ provides a lengthy but straightforward approach to the other rope-related issues, subject to the simplifying assumptions that a rope is indivisible, has about the length of the distance between two adjacent rooms, and cannot be tied to another rope.

Examples

228.
Otranto ★★★
A kind of rope which can be tied to objects and used to anchor the player or drag items from room to room.
261.
Snip ★★★
A string which can be cut into arbitrary lengths, and then tied back together.

RB §10.7 Electricity and Magnetism

Electrons are so tiny, and move so fast, that we will never want to simulate them in ordinary IF. So we simply regard electricity and magnetism as behaviours which are either present or not present, and which have instantaneous effects.

In Witnessed ★★★, batteries provide electricity to enable a "device" to work. Even if switched on, a device with no battery will be ineffective.

Larger voltages are exposed in Electrified , which makes certain items untouchable, and ensures that an experienced electrician will not even try.

Rules of Attraction provides for a magnet which attracts metallic items just strongly enough to stick together until pulled apart for any reason.

Examples

A magnet which picks up nearby metal objects, and describes itself appropriately in room descriptions and inventory listings, but otherwise goes by its ordinary name.
407.
Adding a rule before the basic accessibility rule that will prevent the player from touching electrified objects under the wrong circumstances.
139.
Witnessed 1 ★★★
A kind of battery which can be put into different devices, and which will lose power after extended use.

RB §10.8 Fire

Fire exhibits some of the properties of a gas: it is only vaguely located and tends to spread out, though it passes by touch rather than on the air. It is hazardous to life, through direct contact, heat, and smoke. Better governed, it provides light and warmth. Worse governed, it consumes almost anything it comes into contact with. Here the problem with "debris" is not so much that we need potentially hundreds of new objects to represent broken items: instead, fire could sweep through a work of IF destroying so much that no play is possible any longer. Setting up a problem in which the player must defeat a fully-capable fire is difficult to balance.

As with liquids, it is best to simulate the least amount of fire that the design will allow. Bruneseau's Journey ★★ provides a single candle which can be lit, or blown out, but where fire can never transfer from the candle's end to anything else – or vice versa: the player's source of fire, with which to light the candle, is discreetly neglected.

In the more realistic Thirst 2 , a campfire is lit using a tinderbox, so that fire does transfer from one thing (tinder) to another (the campfire): but it is always confined to just these two items, and can be used only for light and warmth.

The Cow Exonerated ★★ provides a class of matches that can light any flammable object, but assumes that burning objects requires only one turn; lighting one thing does not burn another.

In Fire or in Flood ★★★ provides a complete simulation of what we might call "wild-fire": combustion which spreads through arbitrary objects and rooms, destroying all in its path.

See Also

Examining for a way to describe objects as charred once they have been partly burnt.
Heat for one consequence of fire having touched something.
Gases for an implementation of smoke without fire, if this can exist.
Liquids for water being used to extinguish a simple fire.
Lighting for other uses of candles and torches as light sources.

Examples

118.
A campfire added to the camp site, which can be lit using tinder.
A candle which reacts to lighting and blowing actions differently depending on whether it has already been lit once.
Creating a class of matches that burn for a time and then go out, with elegant reporting when several matches go out at once.
400.
A BURN command; flammable objects which light other items in their vicinity and can burn for different periods of time; the possibility of having parts or contents of a flaming item which survive being burnt.

RB §10.9 Heat

Since we prefer not to simulate burnt skin, and it is unsporting to kill a player outright merely for touching a hot object, heat is mostly used as a reason why something cannot be picked up at a given moment. This very basic puzzle is demonstrated in Grilling .

With the naked eye, it is not always easy to detect what is too hot to touch – a point made in both Masochism Deli , where the only solution is to keep picking up potatoes until one doesn't burn, and in Hot Glass Looks Like Cold Glass ★★★, where infrared goggles turn the scales.

If a hot object is not to be touched, will it stay hot forever? It might well, if it is a steak on an electric grill, but not if it is a recently-baked apple pie sitting on a window-sill. Entropy simulates the gradual return of temperature to equilibrium.

See Also

Electricity and Magnetism for items which shouldn't be touched because they are hot in a different way.

Examples

84.
A grill, from which the player is not allowed to take anything lest he burn himself.
182.
All objects in the game have a heat, but if not kept insulated they will tend toward room temperature (and at a somewhat exaggerated rate).
Multiple potatoes, with rules to make the player drop the hot potato first and pick it up last.
Responding to references to a property that the player isn't yet allowed to mention (or when not to use "understand as a mistake").

RB §10.10 Magic (Breaking the Laws of Physics)

Every previous section of this chapter has been about adding further realism constraints to Inform, to give it a more advanced understanding of physics (and chemistry). But Inform has plenty of physical laws built into it already, even if they are more mundane: inanimate objects do not spontaneously move or change, one solid object cannot pass through another, there are opaque barriers through which light cannot pass, and so on. These rules stop many attempted actions. (GO EAST – "The oak door is closed." GET EMERALD – "You can't, since the glass display case is in the way.")

In the real world, physics is not negotiable. ("Gravity: it's not just a good idea, it's the law.") But in stories, magic can sometimes happen, and in these examples some of the rules built into Inform are waived in special circumstances, which in effect cancel certain physical laws. Very many other magical tricks could be achieved: if you want to make a given command work despite realism constraints, try typing ACTIONS – a testing command which reveals what is happening behind the scenes – and then typing the command you want. This should reveal which named rule is stopping it: you can then try suspending that rule, and seeing if the effect now works.

See Also

Magic Words for ways to create new single-word spell commands.

Examples

Kitty Pryde of the X-Men is able to reach through solid objects, so we might implement her with special powers that the player does not have…
A wand which, when waved, reveals the concealed items carried by people the player can see.
A machine that turns objects into other, similar objects.
The Pointy Hat of Liminal Transgression allows its wearer to walk clean through closed doors.

RB §10.11 Mathematics

It is uncommon, but not absolutely unheard-of, to need to do detailed mathematical calculations in interactive fiction.

The Fibonacci Sequence demonstrates the calculation of a series of numbers; Sieve of Eratosthenes shows off how to calculate the prime numbers within a certain range.

Number Study demonstrates conditional relations between numbers.

Examples

240.
The parity and joint magnitude relations explored.
The modest Leonardo Fibonacci of Pisa will be only too happy to construct his sequence on request, using an array.
The haughty Eratosthenes of Cyrene will nevertheless consent to sieve prime numbers on request.