14.14. Arithmetic with units

| Chapter 14: Numbers and Equations |

| 14.14. Arithmetic with units

The example equations in the previous section carried out quite a lot of arithmetic, but they may have given the impression that Inform always allows arithmetic - which is not true.

This is actually a good thing, because it keeps us from error. For instance, Inform will not allow:

> Equation - Newton's Totally Bogus Law
> F = m^2
> where F is a force, m is a mass.

because whatever you get when you square a mass, you don't get a force - in the same way that a length times another length makes an area, not another length. Physicists call this "dimensional analysis", and it often provides clues about which equations are right. Just after the Second World War, someone correctly worked out the explosive power of an atomic bomb without any classified information simply by guessing what values would appear in the formula, and then finding the simplest equation they could appear in.

In general, Inform will not allow numerical kinds of value to be multiplied or divided by each other (or square or cube rooted) unless we give it instructions that this would make sense.

Of course, there's plenty we can still do without any need for such instructions. For instance, going back to weight,

> The Weighbridge is a room. "A sign declares that the maximum load is [100kg multiplied by 3]."

...will produce the text "A sign declares that the maximum load is 300kg." Here Inform knows that it makes sense to multiply a weight by 3, and that the result will be a weight. Similarly, Inform allows us to add and subtract weights, and several different forms of division are allowed:

> The blackboard is in the Weighbridge. "A blackboard propped against one wall reads: '122 / 10 is [122 divided by 10] remainder [remainder after dividing 122 by 10]; but 122kg / 10kg is [122kg divided by 10kg] remainder [remainder after dividing 122kg by 10kg]; and 122kg / 10 is [122kg divided by 10] remainder [remainder after dividing 122kg by 10].'"

When we visit the Weighbridge, we find:

> A blackboard propped against one wall reads: "122 / 10 is 12 remainder 2; but 122kg / 10kg is 12 remainder 2kg; and 122kg / 10 is 12kg remainder 2kg."

Whereas we are not allowed to divide 122 by 10kg: that would make no sense, since 122 is a number and not made up of kilograms. Inform will produce a problem message if we try. Similarly, Inform won't normally allow us to multiply two weights together - but see the next section.

253
| Example Frozen Assets A treatment of money which keeps track of how much the player has on him, and a BUY command which lets him go shopping. |
RB

254
| Example Money for Nothing An OFFER price FOR command, allowing the player to bargain with a flexible seller. |
RB

255
| Example 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. |
RB

256
| Example Savannah Using the liquid implementation demonstrated in Lemonade for putting out fires. |
RB

Here we build very slightly on the existing liquid implementation to add a puzzle where the player puts out a fire with a bucket of water. Most of the liquid implementation remains the same as before, but we are now in a position to understand the line at the end of section 1, where we understand the names of containers according to the liquids they contain. The new material, pertaining to extinguishing fires, is at the bottom in section 2. "Savannah" Section 1 - Essentials of Liquid A volume is a kind of value. 15.9 fl oz specifies a volume with parts ounces and tenths (optional, preamble optional). A fluid container is a kind of container. A fluid container has a volume called a fluid capacity. A fluid container has a volume called current volume. The fluid capacity of a fluid container is usually 12.0 fl oz. The current volume of a fluid container is usually 0.0 fl oz. Liquid is a kind of value. A fluid container has a liquid. Instead of examining a fluid container: if the noun is empty, say "You catch just a hint of [the liquid of the noun] at the bottom."; otherwise say "[The noun] contains [current volume of the noun in rough terms] of [liquid of the noun]." To say (amount - a volume) in rough terms: if the amount is less than 0.5 fl oz: say "a swallow or two"; otherwise if tenths part of amount is greater than 3 and tenths part of amount is less than 7: let estimate be ounces part of amount; say "[estimate in words] or [estimate plus 1 in words] fluid ounces"; otherwise: if tenths part of amount is greater than 6, increase amount by 1.0 fl oz; say "about [ounces part of amount in words] fluid ounce[s]". Before printing the name of a fluid container (called the target) while not drinking: if the target is empty: say "empty "; otherwise: do nothing. After printing the name of a fluid container (called the target) while not examining: unless the target is empty: say " of [liquid of the target]"; omit contents in listing. Instead of inserting something into a fluid container: say "[The second noun] has too narrow a mouth to accept anything but liquids." Definition: a fluid container is empty if the current volume of it is 0.0 fl oz. Definition: a fluid container is full if the current volume of it is the fluid capacity of it. Understand "drink from [fluid container]" as drinking. Instead of drinking a fluid container: if the noun is empty: say "There is no more [liquid of the noun] within." instead; otherwise: decrease the current volume of the noun by 0.2 fl oz; if the current volume of the noun is less than 0.0 fl oz, now the current volume of the noun is 0.0 fl oz; say "You take a sip of [the liquid of the noun][if the noun is empty], leaving [the noun] empty[end if]." Understand the command "fill" as something new. Understand "pour [fluid container] in/into/on/onto [fluid container]" as pouring it into. Understand "empty [fluid container] into [fluid container]" as pouring it into. Understand "fill [fluid container] with/from [fluid container]" as pouring it into (with nouns reversed). Understand "pour [something] in/into/on/onto [something]" as pouring it into. Understand "empty [something] into [something]" as pouring it into. Understand "fill [something] with/from [something]" as pouring it into (with nouns reversed). Pouring it into is an action applying to two things. Check pouring it into: if the noun is not a fluid container, say "You can't pour [the noun]." instead; if the second noun is not a fluid container, say "You can't pour liquids into [the second noun]." instead; if the noun is the second noun, say "You can hardly pour [the noun] into itself." instead; if the liquid of the noun is not the liquid of the second noun: if the second noun is empty, now the liquid of the second noun is the liquid of the noun; otherwise say "Mixing [the liquid of the noun] with [the liquid of the second noun] would give unsavory results." instead; if the noun is empty, say "No more [liquid of the noun] remains in [the noun]." instead; if the second noun is full, say "[The second noun] cannot contain any more than it already holds." instead. Carry out pouring it into: let available capacity be the fluid capacity of the second noun minus the current volume of the second noun; if the available capacity is greater than the current volume of the noun, now the available capacity is the current volume of the noun; increase the current volume of the second noun by available capacity; decrease the current volume of the noun by available capacity. Report pouring it into: say "[if the noun is empty][The noun] is now empty;[otherwise][The noun] now contains [current volume of the noun in rough terms] of [liquid of the noun]; [end if]"; say "[the second noun] contains [current volume of the second noun in rough terms] of [liquid of the second noun][if the second noun is full], and is now full[end if]." Understand the liquid property as describing a fluid container. Understand "of" as a fluid container. Section 2 - Putting Out Fires The Beach is a room. "The Atlantic stretches east to the horizon, though it is at low tide at the moment. It is dawn: time to pack up and go home." We will skip implementing the Pacific ocean itself, though the example Lakeside Living shows how to incorporate large bodies of water into our liquid simulation. The liquids are seawater. [We could include others, but for the moment...] Instead of drinking a fluid container when the liquid of the noun is seawater: say "Blech!" The bucket is a fluid container carried by the player. The liquid of the bucket is seawater. The current volume of the bucket is 64.0 fl oz. The fire is a fixed in place thing in the beach. "A low fire crackles here, left over from an attempt at s'mores much earlier in the evening." Instead of touching or rubbing or taking the fire, say "You're not such a glutton for punishment." Instead of pouring something into the fire: remove the fire from play; now the current volume of the noun is 0.0 fl oz; say "[The second noun] goes out in a great hiss." Test me with "drink seawater / pour seawater on fire / x bucket / i". This is still a specific implementation: if we wanted to weave liquids together with a full-scale burning model (as in "In Fire or in Flood"), where pretty much any object in the game can be flaming (currently on fire) or damp (extinguished), we might generalize our rule to Instead of pouring something into a flaming thing: now the second noun is damp; now the current volume of the noun is 0.0 fl oz; say "[The second noun] goes out in a great hiss." Of course, the merging of fire and liquids also raises the possibility of gasoline and explosives, of heating and boiling liquids, etc.: as always, it's wise to incorporate a simulation that is only as detailed as the game's interactions really justify.

Previous | Contents | Next