CSBwin/DSA/Lesson 6

From DmWiki
Revision as of 12:28, 16 August 2008 by Gilles (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

DSA Lesson 6 - Building a Torch

We want the player to be able to make a Torch out of a Stick and some Ashes. The player will see a Grate in the wall. If he places both a Stick and some Ashes in the grate, a Torch will appear in his hand. If he attempts to place two Sticks in the Grate, the second one will not be accepted. Likewise for two Ashes. The player can make an infinite number of Torches so long as he keeps supplying Sticks and Ashes. We are going to accomplish this with a DSA.

First of all we draw a state diagram. This is an essential step for any complex machine. If you have a state-brain, then you might skip this step for a simple case like this. But the diagram makes the whole process easier because it makes the labels easy to remember and the flow-control more obvious. Here is our diagram : DSALesson006.gif

Each of the machine's states is represented by a circle with the number of the state enclosed. We are going to have three states in our machine: 0, 1, and 2. We could have added some words to indicate what each of these states represents, but it is pretty obvious : *0 - Nothing has been put into the Grate

  • 1 - Some Ashes have been added to the Grate
  • 2 - A stick has been placed in the Grate

Each state has arrows emanating from it to show how that state reacts to messages that it receives. For example, state 0 responds to two different messages, namely (Set, North) and (Clear, North). In shorthand these are called S0 and C0.

The actions we perform are represented by rectangles with people-readable words that give some indication of what should happen. Each of these rectangles is labeled with the state and row in the DSA which will accomplish the action.

And, very importantly, the arrows point to the new state of the machine after each type of message is processed.

It should be pretty obvious how this all works. We start at state 0. If the player puts in a Stick, the machine moves to state 2. At state 2, the machine refuses any additional Sticks and we stay in state 2, but if the player adds a Stick, then a Torch is placed in his hand and the machine returns to state 0 awaiting the player to build another Torch.

The only problem is that we don't have any idea how to do the things we say (in the rectangles) that we want to do! How do we put a Torch in his hand? How do we give back the Stick when he attempts to put two Sticks in the Grate? Hmmmmm. Well, we study whatever documentation we can find and come upon this : http://www.dianneandpaul.net/CSBwin/documentation/DSA_Language.html

On that page is the description of the word '&ADD' and the words 'To place object in cursor use location = -1'. That sounds very much like something we could use. We want to place an object in the 'cursor' (the player's hand) in place of the Stick or Ashes which he put into the Grate. We click on 'Detailed Description' and see these words : *Use the &ADD operation to duplicate an object and place it in the dungeon. You can place the object in one of the cells of the dungeon itself, on a character (in the backpack, as an item of clothing, or in a hand, etc.), on the cursor, or as a monster's possession. The location parameter determines where the object will be placed:

This is beginning to sound promising. It sounds like what we need to do is to locate an object of the correct type somewhere in the dungeon (so that '&ADD' can copy it, and then do the '&ADD' itself with parameters indicating that we want the copy to be placed in the cursor. The parameters are '~PosMask', 'Location', and 'ObjectID'. It says that the '~PosMask' will be ignored in the case that we put the (copy of the) object in the cursor. So we will use zero. It says that 'Location' should be negative one to put the (copy of the) object in the cursor. So those two parameters are easy. It will look like this :

L0 L1 &NEG

Now all we need is an object to copy. How do we do that? We need three different objects: Ashes, Stick, and Torch. What we will do is put one of each of these things in a dungeon cell that cannot be reached by the player. We will put the Ashes in position 0 (North), the Stick in position 1 (East), and the Torch in position 2 (South). We will put these things in a solid wall and the DSA can 'Fetch' one of them to serve as the third parameter for the '&ADD'. But now we are stumped again, because we do not know how to 'Fetch' an object. Lesson 7] will make it clear how this is done. For now we will say that we simly fetch an object from the cell of the dungeon specified by the DSA's parameter A and at the proper position. Here is how it will look :

The Ashes --- L0 LA L1 L1024 F
The Stick --- L0 LA L2 L32 F
The Torch --- L0 LA L4 L32 F

So, all together, the operation will look like this (for the Ashes, Stick, and Torch) :

L0 L1 &NEG L0 LA L1 L1024 F &ADD
L0 L1 &NEG L0 LA L2 L32 F &ADD
L0 L1 &NEG L0 LA L4 L32 F &ADD

To review--The '&ADD' requires three parameters. The three parameters that we provide are:

  1. L0
  2. L1 &NEG
  3. L0 LA L1 L1024 F

Go get a working version at http://dianneandpaul.net/CSBwin/documentation/DSAwiki/Lesson006.zip

FAQ

I can't think of any questions. This stuff is much too easy! Maybe you have a question? Hmmmmm.