| 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.
| 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. | |
"Money for Nothing"
Section 1 - Prices and Bargaining
Price is a kind of value. $10.99 specifies a price with parts dollars and cents (optional, preamble optional).
A person has a price called wealth. The wealth of the player is $15.
A thing has a price called minimum value. The minimum value of a thing is usually $0.50.
A thing has a price called desired value. The desired value of a thing is usually $5.00.
Offering it for is an action applying to one price and one visible thing.
Understand "offer [price] for [something]" as offering it for.
After taking inventory, say "You have [the wealth of the player]."
Check offering it for:
if the price understood is greater than the wealth of the player, say "You don't have that kind of cash." instead;
if the second noun is not carried by someone, say "There's no one in a position to sell you [the second noun]." instead;
if the second noun is carried by the player, say "[The second noun] is already yours." instead;
if the minimum value of the second noun is greater than the price understood, say "[The holder of the second noun] cackles disdainfully. 'If yer just here to insult me you can take your business elsewhere!' he says." instead;
if the desired value of the second noun is greater than the price understood:
let difference be the desired value of the second noun minus the price understood;
let difference be difference divided by two;
decrease the desired value of the second noun by difference;
now the last object offered is the second noun;
say "'How about [desired value of the second noun]?' suggests [the holder of the second noun]." instead;
otherwise:
unless the desired value of the second noun is the price understood:
say "From the avaricious gleam in the eye of [the holder of the second noun], you guess you could've gotten this purchase for less..."
Carry out offering it for:
increase the wealth of the holder of the second noun by the price understood;
decrease the wealth of the player by the price understood;
move the second noun to the player.
Report offering it for:
say "You spend [the price understood], and now you possess [the second noun]."
When play begins: now right hand status line is "Your funds: [wealth of the player]".
Now, since the man does make counter-offers, it would be reasonable to let the player accept or reject those, as well:
The last object offered is a thing that varies.
Instead of saying yes when the last object offered is carried by a person (called seller) who is not the player:
if the seller is not visible:
continue the action;
otherwise:
now the price understood is the desired value of the last object offered;
try offering the desired value of the last object offered for the last object offered.
Instead of saying no when the last object offered is carried by a person (called seller) who is not the player:
if the seller is not visible:
continue the action;
otherwise:
now the last object offered is the player;
say "You reject the offer firmly."
And we borrow just a line or two from a later chapter to take care of some alternate syntax the player might try:
Understand "offer [price] to [someone]" as a mistake ("You'll need to specify what you want to buy -- try OFFER $1000.00 FOR BROOKLYN BRIDGE."). Understand "offer [someone] [price]" as a mistake ("You'll need to specify what you want to buy -- try OFFER $1000.00 FOR BROOKLYN BRIDGE.").
Understand "buy [something]" as a mistake ("You'll have to name your price: try OFFER $1000.00 FOR BROOKLYN BRIDGE.").
Section 2 - The Scenario
The Flea Market is a room. The crotchety man is a man in the Market. "A crotchety man here is selling [the list of things carried by the crotchety man]." The crotchety man carries a broken television set, a Victorian rhinestone brooch, and a cracked shaving mug.
The minimum value of the brooch is $2.50.
Test me with "offer $0.50 for mug / offer $0.50 to man / offer $6.00 for mug / offer $50.00 for brooch / offer $1.50 for brooch / offer $4.50 for brooch / no / offer $4.50 for brooch / yes".
|