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

Liquids, and all substances that can be mixed or broken off in partial amounts, pose a challenge to model in interactive fiction. The following example is a simple one, but adequate for many scenarios. We start by assuming that all liquids in the game will always appear in containers. The player can pour liquids from one container to another, and the containers keep track of how full they are and describe themselves appropriately. The player can also refer to containers by content. Mixture, however, is not allowed, nor is it possible to put liquids on other objects, pour them out on the ground, etc. These ideas would require a more complicated set-up. "Lemonade" 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. The liquids are water, milk, lemonade, and iced tea. 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]." We have allowed all liquids to be drunk, but it would be possible also to add checking, if we had a game where some liquids were beverages and others were, say, motor oil. 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]." This is probably a drier description than we would actually want in our story, but it does allow us to see that the mechanics of the system are working, so we'll stick with this for the example. Now we need a trick from a later chapter, which allows something to be described in terms of a property it has. This way, the story will understand not only "pitcher" and "glass" but also "pitcher of lemonade" and "glass of milk" -- and, indeed, "glass of lemonade", if we empty the glass and refill it with another substance: Understand the liquid property as describing a fluid container. Understand "of" as a fluid container. And now the scenario itself: The Porch is a room. The porch swing is an enterable supporter in the Porch. "An inviting swing hangs here at the end of the porch, allowing you to enjoy the summer with a cool beverage, and watch your neighbor Ted mowing his lawn with the very last manual powerless lawnmower on the block." The glass is a fluid container carried by the player. The liquid of the glass is milk. The current volume of the glass is 0.8 fl oz. The pitcher is a fluid container in the Porch. The fluid capacity of the pitcher is 32.0 fl oz. The current volume of the pitcher is 20.0 fl oz. The liquid of the pitcher is lemonade. Ted's Lawn is outside from the Porch. Ted is a man in Ted's Lawn. "Ted has taken off his shirt, but still seems a bit oppressed by the sun." The description of Ted is "He looks hot. In all senses." After deciding the scope of the player: place Ted in scope. Instead of doing something to Ted when the player is in the Porch: say "You can't really interact with Ted from this distance, except in the sense of eyeing him surreptitiously." Instead of giving an empty fluid container to Ted: say "Yes, taunt the poor man, why don't you?" Instead of giving a fluid container to Ted when the liquid of the noun is milk: say "Ted looks ruefully at the milk. 'Thanks, but I'm lactose-intolerant,' he says." The block giving rule is not listed in the check giving it to rules. Every turn: if Ted is in the location: if Ted carries a fluid container (called refreshment): try Ted drinking the refreshment; otherwise if a random chance of 1 in 3 succeeds: say "Ted pushes the ineffective mower over some dandelions." Instead of someone drinking a fluid container: if the noun is empty: try the person asked giving the noun to the player; otherwise: decrease the current volume of the noun by 2.0 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 "[The person asked] gulps down some [liquid of the noun]." After someone giving something to the player: say "'Here,' says [the person asked], handing [the noun] back to you. 'Thanks, I owe you one.'"; end the story finally. Test me with "x milk / x lemonade / drink lemonade / drink milk / pour lemonade into glass / drink milk / x milk / drink milk / g / i / fill glass with lemonade / drink lemonade / drop glass / drink lemonade / pitcher". Test Ted with "out / give milk to ted / drink milk / g / g / g / give glass to ted / in / fill glass with lemonade / out / give lemonade to ted / wait / z / z / z ".

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

Previous | Contents | Next