-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.lua
More file actions
254 lines (200 loc) · 6.01 KB
/
object.lua
File metadata and controls
254 lines (200 loc) · 6.01 KB
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
Object = {
pronoun = "the";
};
function Object:new(x)
x = x or {};
local mt = {__index = self};
setmetatable(x, mt);
return x;
end
function Object:isA(testtype)
if(testtype == Object) then return true; end
if(type(testtype) == "string") then
if(string.lower(testtype) == "object") then return true; end
end
return false;
end
function Object:parent()
local mt = getmetatable(self);
if(mt == nil) then return nil; end
return mt.__index;
end
function Object:match(tokens)
if(self.name == nil) then return false; end
local found;
--TODO: this is O(n^2) can we do better?
for j,t in ipairs(tokens) do
found = false;
if(self.pronoun == t) then found = true; end
if(not found) then
for i,n in ipairs(self.name) do
if(n == t) then found=true; break; end
end
end
if(not found) then
for i,n in ipairs(junkWords) do
if(n == t) then found=true; break; end
end
end
if(found == false) then return false; end
end
return true;
end
function Object:placeIn(obj)
obj:liberate();
if(self.contains == nil) then self.contains = {}; end
self.contains[#self.contains+1] = obj;
obj.container = self;
end
function Object:placeOn(obj)
obj:liberate();
if(self.supports == nil) then self.supports = {}; end
self.supports[#self.supports+1] = obj;
obj.supporter = self;
end
function Object:removeFrom(obj)
if(obj.supporter == self) then
if(self.supports == nil) then error("Tried to remove an object that isn't supported"); end
obj.supporter = nil;
local found = false;
for i,v in ipairs(self.supports) do
if(v == obj) then
table.remove(self.supports, i);
found = true;
break;
end
end
if(not found) then error("Object wasn't in the list"); end
else if(obj.container == self) then
if(self.contains == nil) then error("Tried to remove an object that is not contianed"); end
obj.container = nil;
local found = false
for i,v in ipairs(self.contains) do
if(v == obj) then
table.remove(self.contains, i);
found = true;
break;
end
end
if(not found) then error("Object wasn't in the list"); end
end
end
end
function Object:liberate()
if(self.container) then
self.container:removeFrom(self);
end
if(self.supporter) then
self.supporter:removeFrom(self);
end
end
function Object:details()
local res = {};
if(self.desc) then tableappend(res, self.desc); end
return table.concat(res);
end
function Object:phrase(pronoun, cap)
local res = {};
if(cap) then cap = capitalize else cap = function(x) return x; end end
if(self.short) then
if(pronoun) then
tableappend(res, cap(self.pronoun), " ", self.short);
else
tableappend(res, cap(self.short));
end
end
return table.concat(res);
end
function Object:describe(contentsLevel, supportsLevel, curlevel)
contentsLevel = contentsLevel or 1;
supportsLevel = supportsLevel or 2;
if(curlevel) then curlevel = curlevel.." "; else curlevel = ""; end
io.write(curlevel, self:phrase(true, true), "\n\n")
io.write(curlevel, self:details(), "\n");
self:describeChildren(contentsLevel, supportsLevel, curlevel);
end
function Object:describeChildren(contentsLevel, supportsLevel, curlevel)
if(contentsLevel > 0
and (self == current.room or self.open or self.transparent)
) then
if(type(self.contains) == "table" and #self.contains > 0) then
if(self.short) then
io.write("\n", curlevel, "In ", self.pronoun, " ", self.short, " is:\n");
else
io.write("\n", curlevel, "Inside is:\n");
end
for i,x in ipairs(self.contains) do
x:shortDescribe(true, contentsLevel-1, supportsLevel-1, curlevel);
end
else
if(self.short) then
io.write("\n", curlevel, self.pronoun, " ", self.short, " is empty\n");
else
io.write("\n", curlevel, "It is empty\n");
end
end
end
if(supportsLevel > 0 and type(self.supports) == "table" and #self.supports > 0) then
if(self.short) then
io.write("\n", curlevel, "On, ", self.pronoun, " ", self.short, " is:\n");
else
io.write("\n", curlevel, "On it is:\n");
end
for i,x in ipairs(self.supports) do
x:shortDescribe(true, contentsLevel-1, supportsLevel-1, curlevel);
end
end
end
function Object:shortDescribe(pronoun, contentsLevel, supportsLevel, curlevel)
contentsLevel = contentsLevel or 1;
supportsLevel = supportsLevel or 2;
if(curlevel) then curlevel = curlevel.." "; else curlevel = ""; end
pronoun = pronoun or true;
io.write(curlevel, self:phrase(pronoun, true), "\n");
self:describeChildren(contentsLevel, supportsLevel, curlevel);
end
function Object:before(what, ...)
return self:hook("before", what, ...);
end
function Object:after(what, ...)
return self:hook("after", what, ...);
end
function Object:alert(what, ...)
return self:hook("alert", what, ...);
end
function Object:hook(when, what, ...)
if(type(what) ~= "string" or what == "") then
error("hook expects the name of the event.");
end
what = when.."_"..what;
if(type(self[what]) == "function") then
return self[what](self, ...);
end
end
----------------------------
Thing = Object:new();
function Thing:isA(testtype)
if(testtype == Thing) then return true; end
if(type(testtype) == "string") then
if(string.lower(testtype) == "thing") then return true; end
end
return Thing:parent().isA(self, testtype);
end
-----------------------------
Person = Object:new();
function Person:isA(testtype)
if(testtype == Person) then return true; end
if(type(testtype) == "string") then
if(string.lower(testtype) == "person") then return true; end
end
return Person:parent().isA(self, testtype);
end
------------------------
Room = Object:new();
function Room:isA(testtype)
if(testtype == Room) then return true; end
if(type(testtype) == "string") then
if(string.lower(testtype) == "room") then return true; end
end
return Room:parent().isA(self, testtype);
end