Inform 7 Home Page / Documentation
This chapter began by mentioning arithmetic, and then went on a long diversion to create scientific units, everyday weights and measures, and other notational conveniences. Putting all of that together, it's time now to calculate something with all of these numerical quantities.
Suppose we invent the idea of weight, and give everything a weight of its own. Most items will have a nominal weight of 1kg, but people will be heavier. Going on actuarial tables, we might say:
A weight is a kind of value. 10kg specifies a weight. Everything has a weight. A thing usually has weight 1kg. A man usually has weight 80kg. A woman usually has weight 67kg.
Definition: A thing is light if its weight is 3kg or less.
Definition: A thing is heavy if its weight is 10kg or more.
and this provides us with "lighter", "lightest", "heavier" and "heaviest" as before. Now we could say "if Peter is heavier than Paul", or even "if Peter is heavier than 75kg", and so forth. We need one more tool:
total (arithmetic values valued property) of (description of values) ... value
This phrase produces the total of some property held by all of the values matching the description. A problem message is produced if the values in question can't have that property ("the total carrying capacity of scenes"), or if it holds a kind of value which can't meaningfully be ad ed up ("the total description of open doors"). Example:
That gives us everything we need for a working balance platform:
The balance platform is a supporter in the Weighbridge. "The balance platform is currently weighing [the list of things on the platform]. The scale alongside reads: [total weight of things on the platform]."
Note that this only works because we said that "everything has a weight": otherwise it would make no sense to add up the weights of things.
This enables us to get the average weight of a group of things, too:
the total weight of things on the platform divided by the number of things on the platform
But we should be careful that this does not accidentally divide by zero, which it will if the platform has nothing on it! As well as the average, we could find the maximum and minimum weights:
the weight of the heaviest thing on the platform
the weight of the lightest thing on the platform
We should remember that "the heaviest thing on the platform" may be ambiguous, because there may be several equally heavy things there. That means
if the lead pig is the heaviest thing on the platform
will only reliably work if there is no possibility of a tie. A safer bet is:
if the lead pig is the weight of the heaviest thing on the platform
|Start of Chapter 15: Numbers and Equations|
|Back to §15.16. Understanding specified numbers|
|Onward to §15.18. Equations|
Nickel and Dimed
Typically games which keep track of the player's wealth need only do so as an abstract number, but occasionally it becomes useful to represent money as physical coins and bills. Here is an example that does exactly that:
A dollar bill is a kind of money. The price of a dollar bill is $1.00. The printed name of a dollar bill is "dollar bill". Rule for printing the plural name of a dollar bill: say "dollar bills". The description of a dollar bill is "It has George Washington's head on the front, which with a bit of creative folding can be scrunched to look like a mushroom. All important things really are learned in kindergarten."
A five-dollar bill is a kind of money. The price of a five-dollar bill is $5.00. Understand "five" or "five dollar" as the five-dollar bill. The description of a five-dollar bill is "Abraham Lincoln. He looks slightly less dignified here than he does on the penny."
A hundred-dollar bill is a kind of money. The price of the hundred-dollar bill is $100.00. Understand "hundred" or "hundred dollar" as the hundred-dollar bill. Understand "dollar" as the dollar bill. The description of a hundred-dollar bill is "It's got Benjamin Franklin, who always gets shafted: a denomination too large for anyone to carry conveniently, and a lot of local fame in Philadelphia."
Our choice of understand rules guarantees that "five dollar" will be understood as the five, but "dollar" alone as the single. We will learn more about "understand" in later chapters, but here is a test to check the functionality:
Instead of going somewhere when the player encloses something (called the stolen goods) which is not owned by the player:
if the owner of the stolen goods is not a person:
now the player owns the stolen goods;
continue the action;
if the owner of the stolen goods can see the player,
say "'Hey there buddy, not so fast,' says [the owner of the stolen goods]. 'You going to buy [the stolen goods] first, or am I gonna call the cops?'";
otherwise continue the action.
The cashbox is a theoretical construct, not something the player will ever encounter in the course of the game. It contains all the money that is available for non-player characters to use in making change. If we wanted, we could give each character his own stash of change, but this would increase the likelihood that any given person would run out of cash to make change with. (And in this example there is only one vendor anyway.)
The cashbox is a container. The cashbox contains 10 pennies. The cashbox contains 10 nickels. The cashbox contains 10 dimes. The cashbox contains 10 quarters. The cashbox contains 10 dollar bills. The cashbox contains 10 five-dollar bills.
Check buying something:
if the noun is not for sale, say "[The owner of the noun] does not want to sell you [the noun]." instead;
if the player's cash is less than the price of the noun, say "You can't afford the asking price of [the price of the noun] for [the noun]." instead.
Carry out buying something:
let sum paid be $0.00;
while sum paid is less than the price of the noun:
let current target be the price of the noun minus the sum paid;
let bill offered be the best money from the player for the current target;
if the bill offered is money:
move the bill offered to the owner of the noun;
now the bill offered is spent;
increase the sum paid by the price of the bill offered;
let current target be the price of the noun minus the sum paid;
say "You hand [the owner of the noun] [a list of spent money]. [run paragraph on]";
let change be $0.00;
if the sum paid is greater than the price of the noun:
now the change is the sum paid minus the price of the noun;
if change is greater than the sum in the cashbox:
now the player carries every spent money;
now every spent thing is fresh;
say "'Whoa,' says [the owner of the noun], handing the cash back to you. 'I can't make change for that, man, sorry.'" instead;
now every spent thing is in the cashbox;
now every spent thing is fresh;
while change is greater than $0.00:
let change bill be the best money from the cashbox for change;
decrease change by the price of the change bill;
now change bill is spent;
move change bill to player;
if money is spent, say "[The owner of the noun] makes change with [a list of spent money]. [run paragraph on]";
now every spent thing is fresh;
if the noun is not enclosed by the player and the owner of the noun can touch the noun:
say "'Here ya go,' says [the owner of the noun], handing [the noun] to you. [run paragraph on]";
move the noun to the player;
now the player owns the noun.
We've skipped over defining what makes a denomination the best for a given transaction, so we'd better do that now. Our goal is to avoid ever having the player gratuitously overpay -- he should always offer the smallest amount of money that will meet the price of what he's buying.
We also assume that all money "enclosed by the buyer" -- that is, somewhere in the buyer's possession -- is available for use. This might not be true in a game where the player could pick up, say, a sealed lucite container with a ten-dollar bill inside; in that case we would have to define our terms more rigorously, perhaps by requiring that the bills be both enclosed and touchable by the buyer. The touchability check adds an extra layer of calculation, however, and since it is not necessary in this example (and probably not in most other cases either), we'll leave it out:
To decide what money is the best money from (buyer - a thing) for (cost - a price):
repeat with bill offered running through money:
if the bill offered is enclosed by the buyer:
if the price of the bill offered is the cost, decide on the bill offered;
if the price of the bill offered is greater than the cost, now the functional relation of bill offered is overpayment;
otherwise now the functional relation of the bill offered is underpayment;
now the functional relation of the bill offered is irrelevant;
[say "underpayment: [a list of underpayment money]
overpayment: [a list of overpayment money]";]
if the total price of underpayment money is less than the cost:
decide on the cheapest money which is overpayment;
decide on the costliest money which is underpayment.
Notice the "say underpayment/overpayment section..." noted out, above. This is for debugging purposes: when writing complex code, it is sometimes useful to put in lines that will say explicitly what is going on. We can enclose them in brackets and Inform will ignore them as though they were comments; if we run into any problems with the code later, we can erase the brackets and see the diagnostic printed to the screen as we play.
The Bitterly Cold Street is north of the Subway Station. "Even though there is no actual snow or ice, the street is about as cold as you can stand, for which reason walking the twenty blocks uptown is not an acceptable option." The Bitterly Cold Street contains a dollar bill.
The newspaper man is a man in the Subway Station. "A newspaper man in a knit cap and fingerless gloves is hopping up and down behind his stand[if the turn count is 1]. Cold weather, caffeine overdose, or mental illness? You may never know. Welcome to New York[end if]." The description is "Eye contact with strangers is something to avoid around here."
We could have done all that by hand, but the initialization requires a little less work.
The ticket machine is a container in the Subway Station. It is fixed in place. The description of the ticket machine is "An LED screen on the front instructs you to insert [remaining ticket total] to complete your purchase. You also notice that the NO CHANGE light is lit up." The light is part of the ticket machine. The printed name of the light is "no change light". Understand "no change" or "no change light" as the light.
The description of the light is "In the whole of your recollection, the ticket machine has actually had change a total of twice. Usually, as now, the no-change light gleams angrily, daring you to put in more than you owe." A cash return button is part of the ticket machine. Instead of pushing the cash return button: say "The ticket machine regurgitates [the list of things in the ticket machine]."; now every thing in the ticket machine is carried by the player. Instead of taking something which is in the ticket machine: say "The ticket machine has swallowed your money, but it can be retrieved (you hope) with the cash return button."
To decide what price is the remaining ticket total:
let absolute cost be $2.25;
let remaining cost be absolute cost minus the total price of things in the ticket machine;
if remaining cost is less than $0.00, decide on $0.00;
decide on remaining cost.
A subway pass is a kind of thing. 15 subway passes are in the cashbox. The description of a subway pass is usually "A rectangle of thick lavender paper with a black magnetic stripe running up the back side. It is good for one trip on the subway."
After inserting something into the ticket machine:
if the remaining ticket total is $0.00:
let purchased ticket be a random subway pass in the cashbox;
if purchased ticket is not a subway pass, say "The ticket machine grunts disobligingly and then the unwelcome word MALFUNCTION parades across the LED screen, three letters at a time." instead;
repeat with item running through things in the machine:
now the item is nowhere;
move purchased ticket to player;
say "The ticket machine beeps obligingly and disgorges a single subway pass.";
say "The ticket machine beeps obligingly and adjusts its price down to [remaining ticket total]."
And because even though the ticket machine is a container, we don't want to say (empty) after it in the room description:
After all that, we should probably give the player a chance to win, as well:
The turnstile is south of the Subway Station. "A turnstile is all that separates you from the subway platform stairs." The turnstile is north of the Platform. The turnstile is a door. Before going down in the presence of the turnstile, try going south instead. The turnstile is openable. The turnstile is open.
Instead of going through the turnstile when the player carries a subway pass: say "You enter the turnstile and begin your journey uptown..."; end the story finally saying "At last". Instead of going through the turnstile: say "You can't go through the turnstile without a subway pass. They're very strict about this."
In fairness to the Metropolitan Transit Authority, we should admit that most of the ticketing machines in the real New York subway are better than this, and will accept, say, a debit card. But that would be so much less exciting to implement.