Version 2/121101 of Currency by Zorkfire games begins here. "Uses the code from the Example 'Nickel and dime' to implement an intricate system for buying things" Section 1 - Currency Price is a kind of value. $10.99 specifies a price with parts dollars and cents. A thing has a price. The price of a thing is usually $0.00. Money is a kind of thing. Coin is a kind of money. 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 ten-dollar bill is a kind of money. The price of a ten-dollar bill is $10.00. Understand "Ten" or "Ten dollar" as a ten-dollar bill. A twenty-dollar bill is a kind of money. The price of a twenty-dollar bill is $20.00. Understand "Twenty" or "Twenty dollar" as a twenty-dollar bill. 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:] Test bills with "x hundred dollar bill / x five dollar bill / x hundred / x five / x dollar / x dollar bill". A quarter is a kind of coin. The price of a quarter is $0.25. The description of a quarter is "One of the old-fashioned variety, not a state quarter." A dime is a kind of coin. The price of a dime is $0.10. The description of a dime is "Franklin Roosevelt, trying not to look too annoyed that his coin is so small and thin." A nickel is a kind of coin. The price of a nickel is $0.05. The description of a nickel is "A chubby coin, but you've always liked Thomas Jefferson, and the Monticello on the back is a nice touch." A penny is a kind of coin. The price of a penny is $0.01. The description of the penny is "A profile of Abe Lincoln. Sometime soon they'll stop minting these, you're sure of it." Section 2 - Ownership Ownership relates one person (called the owner) to various things. The verb to own (he owns, they own, he owned, he is owned) implies the ownership relation. Definition: a thing is owned if the player owns it. Instead of buying something which is owned by the player: say "You already own [the noun]." 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. After taking inventory: say "Altogether, you've got [the player's cash] on your person." To decide what price is the player's cash: let sum be the total price of money enclosed by the player; decide on sum. To decide what price is the sum in (item - a container): let sum be the total price of the money in the item; decide on sum. When play begins: now every thing carried by the player is owned by the player. Section 3 - Purchasing and Sales Definition: a thing is worthless if the price of it is $0.00. Definition: a thing is valuable if it is not worthless. A thing can be for sale. Rule for printing room description details of something (called target) which is for sale (this is the disclose prices in room description rule): say " ([price of the target])". Before listing contents: group money together giving articles. Instead of examining a for sale thing (this is the describe things by price rule): say "[The noun] costs [the price of the noun], payable to [the owner of the noun]." [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. The block buying rule is not listed in the check buying rules. 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]"; if the sum paid is greater than the price of the noun, let change be 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. Money can be spent or fresh. Report buying something: if the player owns the noun, say "Your transaction is now complete, leaving you with [the player's cash]." [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:] Definition: money is costly if its price is $2.50 or more. Definition: money is cheap if its price is $0.99 or less. Functional relation is a kind of value. The functional relations are overpayment, underpayment, or irrelevant. Money has a functional relation. 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; otherwise: 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; otherwise: 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.] Instead of giving money to someone: say "Best to keep the transaction simple by buying whatever you want." Currency ends here. ---- DOCUMENTATION ---- Section: Introduction Currency implements the code from the Recipe book example called "nickel and dime", by creating two kinds: Money (A kind of thing), and coin (a kind of money). It also extends Inform's "buy" command, by checking if the player has money or not. Currency also creates a value for things called 'price'. Section: Using Currency To use currency in a work of Interactive Fiction, simply include it like any extension, like so: Include Currency by Zorkfire games. Creating money is as easy as creating any thing in Inform: A quarter is in the wallet or a dollar bill is on the desk. Section: Buying Buying is handled automatically by the extension. The player can buy whatever he/she wants, as long as he/she has enough money, and as long as the item in question is on sale. We can make something become "on sale" like this: The deck of cards is on sale. We can also make some rules that prohibit someone buying something, for an example of this see Following the law. Section: Ownership The extension also implements a system for 'ownership'. We can control who owns something in our source text. Here is an example, using the 'deck of cards' scenario from earlier: The deck of cards is owned by the shopkeeper. For another example of ownership, see Following the law. The extension handles stolen goods automatically, and will print a message if the player tries to get away with the goods in the presence of the owner. Example: * Following the law - preventing the player from buying something illegal *: "Following the law" by Zorkfire Games Include Currency by Zorkfire games. The street corner is a room. "This is a busy street corner, bustling with lunch hour traffic." The suspicious-looking man is here. "A suspicious-looking individual stands here, carrying a bag of marajuana." The description of the suspicious-looking man is "He smells of marajuana." A bag of marajuana is an openable container. The bag of marajuana is closed. The bag of marajuana is carried by the suspicious-looking man. The suspicious-looking man owns the bag of marajuana. The description of the bag of marajuana is "You want nothing to do with it." The bag of marajuana has a price of $7.00. The player carries a ten-dollar bill. A pound of marajuana is a thing. It is in the bag of marajuana. The description of the pound of marajuana is "You shouldn't see this message unless the no drugs rule didn[']t work, so that is a bug." [Now that we have the setting, we need a rule to stop the player from buying the drugs.] Instead of buying the bag of marajuana (this is the no drugs rule): say "Do you really want to do that? No! You are a law-abiding citizen. You haven[']t started doing drugs now! When will you start doing them? Never! That[']s when!" Test me with "l / x man / x bag / buy bag / x pound".