CSBwin/DSA/Lesson 2

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

State Machines

A DSA was originally designed primarily as a [state machine|State Machine] and that is still how it works to a large extent. Therefore, you must understand what a State Machine is before you can use the DSA to your best advantage.

A State machine is a machine with one or more states. More than one state is generally more useful than one state. But DSAs very commonly have but one state. A Door in the dungeon has about three states - namely, Closed, Open, or Bashed. A door is a fine example of a state machine. It has these three states and it receives 'messages' that cause it to change from one state to another. So let us describe this state machine using state machine theory. I start with a table and I will tell you what it means in a moment.

A Door State Machine
Open Message Close Message Toggle Message Chop
Closed Open Closed Open Bashed
Open Open Closed Closed Open
Bashed Bashed Bashed Bashed Bashed

Along the left side are listed the possible states of the door - Closed, Open, or Bashed. Along the top are the four possible messages that can be sent to the door. The interior cells of the table tell how the door reacts (that is, changes its state) to each of the four possible messages. Notice that it reacts differently depending on its 'STATE'. This is the essence of a state machine. It remembers something about its past by changing state in response to messages and reacts differently to a message depending on what has happened to it in the past. See if the table makes sense to you.

A Dungeon Master Counter is also a state machine with this table:

Counter State Machine
Increment Decrement
0 0 0
1 2 0
2 3 1
3 4 2
4 5 3
5 6 4
6 7 5
7 8 6
8 9 7
9 10 8
10 11 9
11 12 10
12 13 11
13 14 12
14 15 13
15 15 14

You will see that there are some special cases. State 15 cannot be incremented to state 16. In fact there is no state 16. State 0 cannot be decremented to state -1 because there is no state -1. Stranger yet is the fact that state 0 cannot be incremented to state 1!!!! When you get to state 0, you are stuck there. But our point in drawing this table is to demonstrate that a counter is a 'State Machine'. A DSA is a state machine, too.

The DSA State machine table.

DSA
S0
Set North
C0
Clear North
T0
Toggle North
S1
Set East
C1
Clear East
T1
Toggle East
S2
Set South
C2
Clear South
T2
Toggle South
S3
Set West
C3
Clear West
T3
Toggle West
0
1
2
Etc

It looks a bit empty. But YOU can write whatever you like in the empty spaces by putting a number into the corresponding cells in the DSA editor. You can make this state machine do what you want it to do. If you want a counter very much like the standard counter but which can be incremented from the Zero state, then you simply write the proper number into the cells.

You see that the DSA can receive twelve different messages (Actually, there is a provision for receiving an unlimited number of different message but it has never been used and is untested). The twelve messages are S0, C0, ... T3. And their longhand names are also listed. The message names may seem strange. We had to call them something. There is a reason for the names. We won't try to explain them until we get to the lesson on sending/receiving messages.

Let us make a DSA that will detect when the player has pressed three buttons in a particular sequence. There are puzzles in Dungeon Master that require particular buttons to be pressed but not in a particular sequence. The exit from Dungeon Master's "Choose Your Fate" level requires you to open several doors in a particular sequence, but that can hardly be classified as a 'Sequential Puzzle'. We are going to require that the player presses buttons A, B, and C in that order and that any mistakes will require him to start over again with button A. Here is the table :

Puzzle
Button A Button B Button C
0 1 0 0
1 0 2 0
2 0 0 3
3 3 3 3

Examine the table until you understand how it works. Button A gets you to state 1. The only way to get to state 2 is to start in state 1 and press button B. Pressing any incorrect button takes you back to state 0 where you must start over by pressing button A. And so on. If you ever get to state 3 then you are successful and you stay in state 3 no matter what you do. You are done. You may say that getting to state 3 is not exactly what you had in mind. That what you really wanted to do was to open PitE. You may also complain that nobody has told you how we are to send a 'Button A' message to the DSA. First things first. You need to understand the state machine before you can worry about such details. The next lesson will attempt to answer these complaints.