A Shop


A demonstration

A shop generally works as follows:

A shop consists of:
  1. One or more areas of the dungeon which can be isolated by doors, pits, teleporters, or whatever.  These barriers are needed to prevent the player from exiting the shop without paying.
  2. Zero or more 'Display Windows'.  These are alcoves built in solid walls that display the items for sale.  The player can pick up the items, examine them, put them back or pay for them and keep them.
  3. Zero or more 'Garbage Disposals'.  These are used to dispose of articles that have a trade-in value but which are not put on display for resale.  For example, you might want to accept Screamer Slices as payment but you don't want an infinite number of them to pile up because it would eventually crash the system.
  4. Zero or more 'Cash Slots'.  Where the player can put money to pay for articles purchased.
  5. Exactly one 'Lost and Found' alcove.  When the player leaves the shop any articles left behind that the shop does not handle will be placed here.  Also, at the same time, a tally is made of the total value of all items remaining in the shop and all cash accepted.  If this total is too small, then the player is trying to steal something and the barrier to exit is put in place.  Any articles places in the Lost and Found by the player will stay there until the player retrieves them and have no value, even if the shop buys and sells such articles.
  6. One or more 'Shop Exit' pressure pads.  These should be placed such that the player cannot see any of the 'Display Windows' or 'Lost and Found'.  They should also be placed such that the player cannot somehow remove objects from the shop without activating the pressure pad.  If, for example, the player could throw objects through the exit door then it might be impossible to continue the game if he did not have enough money to pay for the object.  This pressure pad also re-initializes the shop and sorts articles into their proper 'Display Windows'.
Setting things up.  Phase one.  Placing actuators.

The Shop.  Create one or more areas of the dungeon to be the shops.'Display Windows'.  As you prceed to place the alcoves, DSAs, and other objects you should write on a piece of paper the type and location (including position within the cell) of everything you place.  This will make Phase Two of the setup much easier.

'Display Windows'.  In the walls of the shop area, place alcoves.  Each alcove can handle zero or more of only one type of object.  'Garbage Disposals'.  These are placed where the player cannot see them.  Probably in some solid wall somewhere. 
'Cash Slots'.  Probably placed one step prior to the 'Exit Pad'
'Lost and Found'.  You need EXACTLY one of these per 'Virtual Shop'.  It does all the work of counting money, etc.  It serves as the 'Shopkeeper'.
'Entrance/Exit' Pad.  You need one of these inside the entrance/exit of each physical shopping area.  It should be placed where it cannot see any of the 'Display Windows', the 'Lost and Found' alcove, and should not be in a place where articles could be thrown out of the shopping area prior to stepping on this pad.  The player must step on this pad before leaving the shopping area.  If the exit is blocked by a door then the door must be far enough from this pad to prevent the player from running through the door as it is closing.
Setting things up.  Phase Two.  Linking the shop together.

The shop works by passing messages around a 'ring' of DSAs.  All the 'Display Window', 'Garbage Disposal', 'Cash Slots', and 'Lost and Found' DSAs are linked together using Parameter B of each to point to the next.  The 'Lost and Found' receives all messages initially and acts as the 'Shop Keeper'.  It processes any messages and, if necessary, sends a message to the next DSA in the ring.  Eventually, the message will go around the entire ring and come back to the 'Lost and Found' where it may or may not initiate another message to the rest of the ring.

Go through the list of DSAs that you created in Phase One:
Link the 'Lost and Found' back to your door/pit/teleporter or other method of keeping the player from leaving without paying.  Parameter D of the 'Lost and Found' should be set to the location that is to receive the open and close messages. (This is Parameter B of the 'More Parameters' DSA that is at the same location of the 'Lost and Found' DSA.)

Now we need to provide a global variable for all the shop DSAs to share.  Having this global variable makes the whole business (pun intended) easier to program and more efficient at runtime.  Multiple shops can share the same global variable.  By default this is variable number 14.  If this variable number conflicts with a global variable already in use then you can change it by editing each of the DSA types and changing the value in the S0 and C0 lines of state number 15.  For now we will assume it is 14.

How things work.  Messages

How things work.  DSAs

All the DSAa in a shop are linked in a ring.  Each is responsible for maintaining and reporting the value of items in its possession.  Each is responsible for passing along messages and any items it does not wish to keep track of.  The 'Inventory' messages, and unsorted items, are passed around the ring to get all the items put into their proper places.  Then a 'Payment' message is passed around the ring and every DSA is expected to add the value of its items to the Global Variable.
    1. Sort all the items.  It first protects all items currently in the 'Lost and Found' by sending them to the other side of the cell where they are temporarily hidden.  This is done to avoid the possibility of having to move hundreds of items around the ring ( efficiency ) and to prevent items placed in the 'Lost and Found' by the player from being added to the store's inventory.  Then two inventory messages are passed around the ring to push all items into their proper locations.  Then the temporarily hidden items are moved back into view.
    2. The Global variable is cleared and a 'Payment' message is passed around the ring to determine the total value of all items in the store plus the total value of all cash received plus the total value of all items accepted for value and disposed of.
    3. If the final value from step 2 is less than the value determined when the party first entered the shop then the exit is closed.  Otherwise the exit is opened.
All of the DSAs have only one state - State 0.  They all share a common global variable. By default, variable 14.  But they all use state 15 as a 'configuration state' and the global variable number is defined in only that one state to make it easier to change in case of conflicts with other software.  All the DSAs use parameter B to locate the next DSA in the Shop's ring.
Here are some diagrams I drew before writing the DSAs.  They are very helpful in sorting out how the code fits together.