DEV website instance!Verb-specific codeAll that is left now is to define actions associated with individual verbs. The major directive ACTION is used to define code associated with a particular vocabulary word. Let's start with INVENTORY. action inventory local optr # Local variable which will be used as a pointer to objects local count # Counts listed objects, automatically initialised to zero itobj optr, inhand # Loop through objects in player's inventory say optr # Show object's description add count, 1 # Increment count of listed objects fin ifeq count, 0 # If nothing listed, ... say "You are not carrying anything." # ... say empty-handed fin quit # Command fully handled, so terminate the REPEAT loopWe have already met local declarations and comments should make the that code quite self-explanatory. Having tackled INVENTORY, we'll proceed with TAKE, which normally applies only to objects, but it is traditional to code also for TAKE INVENTORY. That's not a problem, since A-code makes no fundamental distinction between verbs and nouns in handling player commands. So INVENTORY can be used as either. action take inventory call inventory # Execute code associated with the INVENTORY command The ACTION directive takes one or two arguments. The first is the vocabulary word with which subsequent code is to be associated. The second argument is optional. If it is present, then the subsequent code will be executed only if the specified word also features in the player's command. (NB, I've dealt with INVENTORY before TAKE INVENTORY but that was just for clarity of exposition.) Just like INIT and REPEAT (and also AT and PROC) there can be multiple ACTION sections for a given verb. The only object that can be picked up in this game is the cloak, so there is no need for generic ACTION TAKE – any other attempt to use TAKE will be handled by the last REPEAT section as an error. action take cloak ifhere cloak # If cloak is at this location get cloak set cloak, 1 # The cloak is now carried set bar, 0 # and the bar is now dark quip "You [/pick/take] the cloak[/ up/ off the hook].", cloak fin The IFHERE conditional return true if the nominated object is at the same location as the player (which also means not in player's possession!). If the cloak is absent, the error handling REPEAT section will handle it. The response to success could have been just the generic "Taken" or similar, But I cannot resist showing of the feature of A-code which really caught my eye when I first encountered the language. The cloak has three states: 0 if it is carried/worn, 1 if it is lying on the floor and 2 if it is hanging on the hook. The QUIP directive will only be executed if the state is 1 or 2 and will automatically construct the appropriate response. The DROP action is very similar: action drop cloak ifhave cloak # True only if the player has the cloak ifat cloakroom # True only if player is at cloakroom drop cloak # Move the cloak from player's inventory to cloakroom set cloak, 1 # It is now lying on the floor set bar, 1 # The bar is now lit quip "You drop the cloak." fin quip "This is not a good place to leave your cloak." fin That handles all cases where the cloak is carried or worn by the player. Other possibilities are already covered by the general error handling in the last REPEAT section.
|