-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.jang
360 lines (308 loc) · 9.81 KB
/
framework.jang
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
var Game = function(start_room) {
// Map from room names to all rooms, visible or not.
var rooms = {||}
// Map from item names to all items, visible or not
var items = {||}
// Set of held items
var held_items = {||}
// Locations of non-held items
var item_locations = {||}
var current_room = start_room;
this.addRoom = function(room) {
rooms[room.getId()] = room
}
this.addItem = function(item, room_name) {
var item_name = item.getId()
if (item_name in items) {
error("Item " + item_name + " already exists.")
}
items[item_name] = item
item_locations[item_name] = room_name
}
this.getRoomIdByAlias = function(alias) {
var real_rooms = rooms.ownItems().map(function(e) {
return rooms[e]
})
var i = 0
while (i < real_rooms.length()) {
if (real_rooms[i].getAlias() == alias) {
return real_rooms[i].getId()
}
i += 1
}
error("No room with that alias.")
}
this.getRoom = function(room_name) {
return rooms[room_name]
}
this.getItem = function(item_name) {
return items[item_name]
}
this.userHasItem = function(item_name) {
return item_name in held_items
}
this.isItemVisible = function(item_name) {
return items[item_name].isVisible()
}
this.give = function(item_name) {
if (item_name in held_items) {
return false
} else {
held_items[item_name] = true
del item_locations[item_name]
}
}
this.take = function(item_name) {
if (!(item_name in held_items)) {
return false
}
del held_items[item_name]
item_locations[item_name] = current_room
return true
}
this.show = function(item_name) {
if (!(item_name in items)) {
error("Attempted to show non-existent item")
}
items[item_name].show()
}
this.hide = function(item_name) {
if (!(item_name in items)) {
error("Attempted to hide non-existent item")
}
items[item_name].hide()
}
this.moveItem = function(item_name, room_name) {
del held_items[item_name]
item_locations[item_name] = room_name
}
this.move = function(room_name) {
current_room = room_name
rooms[current_room].onEnter()
}
this.cmdGoDir = function(dir) {
rooms[current_room].onTryDir(dir)
}
this.cmdDropItem = function(item_name) {
if (!(item_name in held_items && items[item_name].isVisible())) {
tell("You don't have that item!")
return;
} else {
items[item_name].onDrop()
}
}
this.cmdLookItem = function(item_name) {
if (!(item_name in item_locations) && !(item_name in held_items)) {
tell("There is no such item.")
} else if (items[item_name].isHidden()) {
tell("There is no such item.")
} else if (item_locations[item_name] != current_room) {
tell("There is no such item.")
} else {
items[item_name].onLook()
}
}
this.cmdExamineItem = function(item_name) {
if (((!(item_name in item_locations)) || (item_locations[item_name] !=
current_room)) && !(item_name in held_items)) {
tell("There is no such item")
} else {
items[item_name].onExamine()
}
}
this.cmdTakeItem = function(item_name) {
if (item_name in held_items && items[item_name].isVisible()) {
tell("You already have that item!")
} else if (!(item_name in item_locations) || item_locations[item_name]
!= current_room || items[item_name].isHidden()) {
tell("There is no such item")
} else {
items[item_name].onTake()
}
}
this.cmdUseItem = function(item_name1, item_name2) {
if (!(item_name1 in held_items && items[item_name1].isVisible())) {
tell("You don't have that item.")
} else if (!(item_name2 in items)) {
tell("That object doesn't exist.")
} else {
items[item_name1].onUse(item_name2)
}
}
this.cmdSpeakItem = function(item_name) {
if (((!(item_name in item_locations)) || (item_locations[item_name] !=
current_room)) && !(item_name in held_items)) {
print item_name in item_locations
print item_locations[item_name]
print current_room
print item_name in held_items
tell("There is no such item")
} else {
items[item_name].onSpeak()
}
}
this.userItems = function() {
return user_items.clone()
}
this.currentRoom = function() {
return current_room
}
}
const var game = new Game();
var Room = function(options) {
var required_fields = ['id', 'alias', 'predescription', 'description']
// Check that all required fields are included in options
if (!required_fields.all(function(e) {e in options})) {
error("Error: Room constructor called without all required arguments.")
}
if (!('exits' in options)) {
options.exits = {||}
}
this.getId = function() {
return options.id
}
this.getAlias = function() {
return options.alias
}
this.setExit = function(dir, action) {
options.exits[dir] = action
}
this.delExit = function(dir) {
del options.exits[dir]
}
this.onInit = function() {
if (isinstance(options.init, Function)) {
var init = options.init()
init()
} else {
error("Invalid init attribute type for Room")
}
}
this.items = function() {
}
this.onEnter = function() {
if (options.predescription[-1] == ":") {
tell(options.predescription[:-1] + " " + options.alias)
} else {
tell(options.predescription)
}
tell(options.description)
if (isinstance(options.enter, Function)) {
// pattern to avoid 'this' binding to options
var enter = options.enter
enter()
} else if (isinstance(options.enter, String)) {
tell(options.enter)
} else if (options.enter == undefined) {
} else {
error("Invalid type of enter attribute for Room")
}
};
this.onTryDir = function(dir) {
var exits = options.exits
var action = exits[dir]
if (isinstance(action, String)) {
game.move(action)
this.onExit()
} else if (isinstance(action, Function)) {
action()
} else if (action == undefined) {
tell("You can't go that way!")
}
}
this.onExit = function() {
if (isinstance(options.exit, Function)) {
var exit = options.exit
exit()
} else if (isinstance(options.exit, String)) {
tell(options.exit)
} else if (options.exit == undefined) {
} else {
error("Invalid type of exit attribute for Room")
}
};
}
var Item = function(options) {
var required_fields = ['id', 'alias', 'look', 'examine']
var this_item = this
if (!required_fields.all(function(e) {e in options})) {
error("Error: Item constructor called without all required arguments.")
}
if (! ('article' in options)) {
options.article = 'it'
}
if (! ('visible' in options)) {
options.visible = true
}
if (! ('prefix' in options)) {
options.prefix = 'a'
}
if (! ('uses' in options)) {
options.uses = {||}
}
if (!('gender_object' in options)) {
options.gender_object = 'it'
}
if (!('gender_subject' in options)) {
options.gender_subject = 'it'
}
this.getId = function() {
return options.id
}
this.isVisible = function() {
return options.visible
}
this.isHidden = function() {
return !options.visible
}
this.show = function() {
options.visible = true
}
this.hide = function() {
options.visible = false
}
var makeHandler = function(attr_name, default_action, string_action) {
return function() {
var action = options[attr_name]
if (isinstance(action, String)) {
tell(action)
if (string_action) {
string_action()
}
} else if (isinstance(action, Function)) {
action()
} else if (action == undefined) {
default_action()
} else {
error("Invalid " + attr_name + " attribute type for Item")
}
}
}
this.onLook = makeHandler('look', function() {
tell("There's nothing to see there")
});
this.onExamine = makeHandler('examine', function() {
tell("There's nothing of interest.")
});
this.onSpeak = makeHandler('speak', function() {
tell(options.pronoun.capitalize() + " doesn't respond.")
});
this.onTake = makeHandler('take', function() {
game.give(this_item)
tell("You take " + options.article + options.alias)
}, function() {game.give(this_item.getId())});
this.onDrop = makeHandler('drop', function() {
game.take(options.id)
tell("You drop " + options.article + options.alias)
})
this.onUse = function(other_item_name) {
var action = options.uses[other_item_name]
if (isinstance(action, String)) {
tell(action)
} else if (isinstance(action, Function)) {
action()
} else if (action == undefined) {
tell("You can't do anything with that on its own.")
}
}
}