forked from greyblue9/text-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Executive.class.inc
390 lines (294 loc) · 9 KB
/
Executive.class.inc
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
360
<?php
require_once('Area.class.inc');
require_once('Game.class.inc');
require_once('Player.class.inc');
function incrementArrayKey($pKey, &$pArray, $pIncrementBy = 1)
{
if (!isset($pArray[$pKey])) $pArray[$pKey] = 0;
$pArray[$pKey] += $pIncrementBy;
}
class Executive
{
const DOOR = 'door';
const NORTH = 'north';
const SOUTH = 'south';
const EAST = 'east';
const WEST = 'west';
const DOWN = 'down';
const UP = 'up';
const LEFT = 'left';
const RIGHT = 'right';
private $game;
private $player;
private $area;
private $tags; // current tags (for outlet matching)
public function __construct()
{
$this->game = Game::get();
$this->player = Player::get();
$this->area = $this->game->getArea($this->player->location);
$this->clearTags();
}
public static function get()
{
if (!isset($_SESSION['executive']))
{
$_SESSION['executive'] = new Executive();
}
return $_SESSION['executive'];
}
private function clearTags()
{
$this->tags = array();
}
/**
Input: @north
Candidates
area votes tags
--------------------------------------
doorway 1 @north @west @down
hall 1 @north @west @up
cinema 0 @east
Tied Candidates
area common unique
------------------------------------------
doorway @north @west @down
hall @north @west @up
Which way north do you want to go-- up or down?
Input: @door
Candidates
area votes tags
--------------------------------------
doorway 1 @door @left
hall 1 @door @right
cinema 0 @down
Tied Candidates
area common unique
------------------------------------------
doorway @door @left
hall @door @right
Which door do you want to go through-- left or right?
Input: @up
Candidates
area votes tags
--------------------------------------
doorway 1 @up @door
hall 1 @west @up
cinema 0 @door @down
Tied Candidates
area common unique
------------------------------------------
doorway @up @door
hall @up @west
Which way up do you want to go up-- through the door or west?
*/
public function look()
{
return $this->areaDescription();
}
public function go($pDirection)
{
$area = $this->game->getArea($this->player->location);
// determine which outlet best matches input direction
$votesByOutletIndex = array();
for ($i=0; $i<count($area->outlets); $i++)
{
$outlet = $area->outlets[$i];
foreach ($outlet->tags as $tag)
{
debug('Tag: '.$tag.', Direction: '.$pDirection);
if ($tag == '@'.$pDirection)
{
incrementArrayKey($i, $votesByOutletIndex);
}
}
}
debug('Area outlets:');
debug($area->outlets);
krsort($votesByOutletIndex);
debug('Votes by outlet index:');
debug($votesByOutletIndex);
foreach ($votesByOutletIndex as $index => $votes)
{
// Only process first match (highest)
// TODO: Ask for disambiguation if top two or more
// outlets have same # of votes
$outlet = $area->outlets[$index];
$this->player->location = $outlet->destArea;
return $this->areaDescription();
}
// No matches
return 'Not sure how to go that way.';
}
/*
private static function findIntendedObject($pObjWords, $pObjNames)
{
$cmdNoun = array_pop($pObjWords);
$cmdAdjs = $pObjWords;
$votesByObjectName = array();
foreach ($pObjNames as $objName)
{
$object = $this->game->getObject($objName);
debug('Object "'.$objName.'"');
debug($object);
foreach ($object->nouns as $objNoun)
{
if ($cmdNoun == $objNoun)
{
// Noun match is weighted much higher than any adjective
incrementArrayKey($objName, $votesByObjectName, 10);
}
}
foreach ($object->adjs as $objAdj)
{
foreach ($cmdAdjs as $cmdAdj)
{
if ($cmdAdj == $objAdj)
{
// Any adjective match is weighted 1
incrementArrayKey($objName, $votesByObjectName, 1);
}
}
}
}
krsort($votesByObjectName);
foreach ($votesByObjectName as $objName => $votes)
{
// Only process first match (highest)
// TODO: Ask for disambiguation if top two or more
// objects have same # of votes
$object = $this->game->getObject($objName);
if ($object->canCarry)
{
// Add to inventory
$this->player->addToInventory($objName);
// Remove from area
$area->objects = array_values(
array_diff($objects, array($objName))
);
return $object->carryStr?
$object->carryStr:
('You pick up the '.$object->title.'.');
}
else
{
return $object->carryStr?
$object->carryStr:
('You cannot carry the '.$object->title.'.');
}
}
}*/
public function pickUp($pObjWords)
{
$cmdNoun = array_pop($pObjWords);
$cmdAdjs = $pObjWords;
$area = $this->game->getArea($this->player->location);
$objects = $area->objects;
debug('Objects:');
debug($objects);
$votesByObjectName = array();
foreach ($objects as $objName)
{
$object = $this->game->getObject($objName);
debug('Object "'.$objName.'"');
debug($object);
foreach ($object->nouns as $objNoun)
{
if ($cmdNoun == $objNoun)
{
// Noun match is weighted much higher than any adjective
incrementArrayKey($objName, $votesByObjectName, 10);
}
}
foreach ($object->adjs as $objAdj)
{
foreach ($cmdAdjs as $cmdAdj)
{
if ($cmdAdj == $objAdj)
{
// Any adjective match is weighted 1
incrementArrayKey($objName, $votesByObjectName, 1);
}
}
}
}
krsort($votesByObjectName);
foreach ($votesByObjectName as $objName => $votes)
{
// Only process first match (highest)
// TODO: Ask for disambiguation if top two or more
// objects have same # of votes
$object = $this->game->getObject($objName);
if ($object->canCarry)
{
// Add to inventory
$this->player->addToInventory($objName);
// Remove from area
$area->objects = array_values(
array_diff($objects, array($objName))
);
return $object->carryStr?
$object->carryStr:
('You pick up the '.$object->title.'.');
}
else
{
return $object->carryStr?
$object->carryStr:
('You cannot carry the '.$object->title.'.');
}
}
// No matches
return 'I don\'t see any '.$cmdNoun.' here.';
}
public function drop()
{
$invObjNames = $this->player->getInventory();
if (!count($invObjNames))
{
return 'You\'re not carrying anything.';
}
return 'I haven\'t implemented that yet!';
}
public function showInventory()
{
$invObjNames = $this->player->getInventory();
if (count($invObjNames))
{
$Output = "You are carrying: \n";
foreach ($invObjNames as $objName)
{
$object = $this->game->getObject($objName);
$Output .= "- ".ucfirst($object->title)." \n";
}
return trim($Output);
}
else
{
return 'You are empty-handed.';
}
}
private function areaDescription()
{
debug('Location:');
debug($this->player->location);
$this->area = $this->game->getArea($this->player->location);
$output = $this->area->description;
debug('Area:');
debug($this->area);
$objectOutput = '';
foreach ($this->area->objects as $objectName)
{
$object = $this->game->getObject($objectName);
if ($object->tagline)
{
$objectOutput .= $object->tagline."\n";
}
}
if ($objectOutput)
{
$output .= "\n\n".trim($objectOutput);
}
return $output;
}
}