~ok2/retro-language/arduino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( interactive fiction interpreter core                                        )
( copyright [c] 2011-2012, Charles Childers                                   )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

needs array'
needs struct'
needs enum'
needs linkedList'

with struct'

chain: fiction'

( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( At a fundamental level, everything derives from a single generic "object"   )
( structure. This contains a lot of fields, most of which aren't relevant to  )
( all of the objects. So there's some waste here, but it simplfies things     )
( overall.                                                                    )
(                                                                             )
( The objects have a .type field. We use the enum' library to create a series )
( of enumerated data types. Each object gets tagged with a type, so we can    )
( enumerate things later. And functions can use this to prevent execution on  )
( objects that they do not support.                                           )
(                                                                             )
( When you create an object, you should provide a short name, and a detailed  )
( description.                                                                )
(                                                                             )
( So:                                                                         )
(                                                                             )
(   object foo                                                                )
(   "Foo" nameOf foo                                                          )
(   "This is a small brass key." describes foo                                )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

1 ^enum'enum| ROOM ITEM |

{ 1 field .description
  1 field .shortDescription
  1 field .type
  1 field .onLook
  1 field .onEntry
  1 field .location
  1 field .visited
  1 field .toNorth
  1 field .toSouth
  1 field .toEast
  1 field .toWest
  1 field .onNorth
  1 field .onSouth
  1 field .onEast
  1 field .onWest
  1 field .postDescription
  1 field .preDescription
  1 field .onRead
} object

: describes ( $"- )
  keepString ' .description ! ;

: nameOf  ( $"- )
  keepString ' .shortDescription ! ;


( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Creation of Rooms                                                           )
(                                                                             )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

: room ( "- )
  object ROOM @last @d->xt .type ! ;

: invertDirection ( a$-a )
  [ "north" compare ] [ .toSouth ] whend
  [ "south" compare ] [ .toNorth ] whend
  [ "east"  compare ] [ .toWest  ] whend
  [ "west"  compare ] [ .toEast  ] whend
  drop ;

: is ( a""""- )
  getToken invertDirection getToken drop ' swap ! ;



( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Creation of items                                                           )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

^linkedList'new: items

: item ( "- )
  object ITEM @last @d->xt .type !
  @last @d->xt items ^linkedList'add ;

: contains ( a"- )
  ' .location ! ;

: has? ( a-af )
  dup .location @ 0 = ;

: displayItem ( a- )
  dup .shortDescription @ swap xt->d d->name "(%s) - %s\n" puts ;

: inventory ( - )
  "\nYou are carrying:\n" puts
  items [ ^linkedList'.value @ has? [ displayItem ] [ drop ] if ] ^types'LIST each@ ;


( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Game Play Loop                                                              )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

variable at

: invalidDirection ( - )
  "You can not go in that direction.\n" puts ;

: moveNorth ( - )
  @at .onNorth @
  [ @at .onNorth @ do @at .toNorth @ 0; !at ]
  [ @at .toNorth @ 0 <>
    [ @at .toNorth @ !at ]
    [ invalidDirection   ] if ] if ;

: moveSouth ( - )
  @at .onSouth @
  [ @at .onSouth @ do @at .toSouth @ 0; !at ]
  [ @at .toSouth @ 0 <>
    [ @at .toSouth @ !at ]
    [ invalidDirection   ] if ] if ;

: moveEast ( - )
  @at .onEast @
  [ @at .onEast @ do @at .toEast @ 0; !at ]
  [ @at .toEast @ 0 <>
    [ @at .toEast @ !at ]
    [ invalidDirection   ] if ] if ;

: moveWest ( - )
  @at .onWest @
  [ @at .onWest @ do @at .toWest @ 0; !at ]
  [ @at .toWest @ 0 <>
    [ @at .toWest @ !at ]
    [ invalidDirection   ] if ] if ;

: describeCurrentRoom ( - )
  @at .shortDescription @ puts 2cr
  [ @at .preDescription @ 0; do ] do
  @at .visited @
      [ @at .description @ puts 2cr -1 @at .visited ! ] ifFalse
  [ @at .postDescription @ 0; do ] do ;

: play ( - )
  clear
  repeat
  describeCurrentRoom
  "> " puts getToken
  [ [ "n" compare ] [ moveNorth ] whend
    [ "s" compare ] [ moveSouth ] whend
    [ "e" compare ] [ moveEast  ] whend
    [ "w" compare ] [ moveWest  ] whend
    [ "l" compare ] [ @at .description @ puts ] whend
    [ "i" compare ] [ inventory ] whend
    [ "drop" compare ] [ getToken find [ @d->xt .location @at swap ! ] [ drop ] if ] whend
    [ "take" compare ] [ getToken find [ @d->xt .location 0 swap ! ]
                                            [ "\nI don't see that here.\n" puts ] if ] whend
    [ "x" compare ]
      [ getToken find
        [ @d->xt dup .location @ [ @at = ] [ 0 = ] bi or
          [ .description @ puts cr ] [ drop "\nI don't see that here.\n" puts ] if
      ] [ "\nI don't see that here\n" puts ] if ] whend

    [ "read" compare ] [ getToken find
        [ @d->xt dup .location @ [ @at = ] [ 0 = ] bi or
          [ .onRead @ dup [ do ] [ "You can't read this" puts ] if ] [ drop "\nI don't see that here.\n" puts ] if
      ] [ "\nI don't see that here\n" puts ] if ] whend
    [ "quit" compare ] [ "\n\nThanks for playing!\n\n" puts bye ] whend
  ] do
  2cr
  again ;
;chain

without

doc{
========
fiction'
========

--------
Overview
--------

I have always enjoyed playing interactive fiction games. This library provides a
framework for building simple games of this type. It's been used for an implemention
of *Cloak of Darkness*, as well as various small puzzle games for my children.


---------
Functions
---------

+-------------------+-----------+------------------------------------------------+
| Name              | Stack     | Usage                                          |
+===================+===========+================================================+
| ROOM              | -n        | Item type for rooms                            |
+-------------------+-----------+------------------------------------------------+
| ITEM              | -n        | Item type for items                            |
+-------------------+-----------+------------------------------------------------+
| .description      | a-a       | Object field. Describe the item                |
+-------------------+-----------+------------------------------------------------+
| .shortDescription | a-a       | Object field. Short name for object            |
+-------------------+-----------+------------------------------------------------+
| .type             | a-a       | Object field. Type. ROOM or ITEM               |
+-------------------+-----------+------------------------------------------------+
| .onLook           | a-a       | Object field. Custom handler for "look"        |
+-------------------+-----------+------------------------------------------------+
| .onEntry          | a-a       | Object field. Custom handler for entry event   |
+-------------------+-----------+------------------------------------------------+
| .location         | a-a       | Object field. Where is this object?            |
+-------------------+-----------+------------------------------------------------+
| .visited          | a-a       | Object field. Have we been here before?        |
+-------------------+-----------+------------------------------------------------+
| .toNorth          | a-a       | Object field. Pointer to location to the north |
+-------------------+-----------+------------------------------------------------+
| .toSouth          | a-a       | Object field. Pointer to location to the south |
+-------------------+-----------+------------------------------------------------+
| .toEast           | a-a       | Object field. Pointer to location to the east  |
+-------------------+-----------+------------------------------------------------+
| .toWest           | a-a       | Object field. Pointer to location to the west  |
+-------------------+-----------+------------------------------------------------+
| .onNorth          | a-a       | Object field. Custom handler for "north"       |
+-------------------+-----------+------------------------------------------------+
| .onSouth          | a-a       | Object field. Custom handler for "south"       |
+-------------------+-----------+------------------------------------------------+
| .onEast           | a-a       | Object field. Custom handler for "east"        |
+-------------------+-----------+------------------------------------------------+
| .onWest           | a-a       | Object field. Custom handler for "west"        |
+-------------------+-----------+------------------------------------------------+
| .postDescription  | a-a       | Object field. Custom handler run after showing |
|                   |           |               a description                    |
+-------------------+-----------+------------------------------------------------+
| .preDescription   | a-a       | Object field. Custom handler run before showing|
|                   |           |               a desription                     |
+-------------------+-----------+------------------------------------------------+
| .onRead           | a-a       | Object field. custom handler for "read"        |
+-------------------+-----------+------------------------------------------------+
| describes         | $"-       | Add a description to an object                 |
+-------------------+-----------+------------------------------------------------+
| nameOf            | $"-       | Specify a short name for an object             |
+-------------------+-----------+------------------------------------------------+
| room              | "-        | Create a new ROOM object                       |
+-------------------+-----------+------------------------------------------------+
| invertDirection   | a$-a      |                                                |
+-------------------+-----------+------------------------------------------------+
| is                | a""""-    |                                                |
+-------------------+-----------+------------------------------------------------+
| items             | -L        | Linked list of all known items                 |
+-------------------+-----------+------------------------------------------------+
| item              | "-        | Create a new ITEM object                       |
+-------------------+-----------+------------------------------------------------+
| contains          | a"-       | Mark that a room contains an item              |
+-------------------+-----------+------------------------------------------------+
| has?              | a-af      | Does the player have an item?                  |
+-------------------+-----------+------------------------------------------------+
| displayItem       | a-        | Display the name of an item                    |
+-------------------+-----------+------------------------------------------------+
| inventory         | ``-``     | Display all items the player has               |
+-------------------+-----------+------------------------------------------------+

}doc