-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzombie1_1.py
555 lines (466 loc) · 21 KB
/
zombie1_1.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/usr/bin/env python
# This is the world's first (I think) implementation of the
#
# ZOMBIE-ORIENTED MACHINE BEING INTERFACE ENGINE
#
# You know, like Hex, but with evil beings! Written in Python to offset the evil a bit.
#
# Evil necromancers might want to go here:
# http://www.dangermouse.net/esoteric/zombie.html
# to read the specification, which was made over two years ago, and still nobody
# dared implement it.
#
# I have modified the original language to add more functionallity.
#
import re,sys,thread,time,random
# regexps recognizing syntax elements
comment_re = re.compile("\{.*?\}", re.DOTALL)
declaration_re = re.compile(r'([A-Za-z0-9_\-]*?)\s+is\s+an?\s+(zombie|enslaved undead|' + \
r'ghost|restless undead|vampire|free-willed undead' + \
r'|demon|djinn)', re.I)
task_re = re.compile("task\s+([A-Za-z0-9_-]*)", re.I)
remember_re = re.compile("remember\s+(.*)", re.I)
string_re = re.compile('".*?"')
integer_re = re.compile('[\-0-9\.]+')
kill=False
# error message
def die(msg):
global kill
print "--- Fatal error: %s" % msg
kill=True
sys.exit()
# split line according to whitespace, but keep strings intact
def splitline(string):
cmds = []
# break line up into pieces
prev_whitespace = False
in_string = False
temp = ''
for c in reversed(range(0,len(string))):
if string[c]==' ' and not (prev_whitespace or in_string):
prev_whitespace = True
cmds.insert(0,temp)
temp = ""
elif string[c]!=' ' or in_string:
prev_whitespace = False
if string[c]=='"': in_string = not in_string
temp = string[c]+temp
cmds.insert(0,temp)
return cmds
# Entity objects
class Entity:
environment = None
name = ''
memory = None
tasks = []
def runtasks(self):
self.active=True
# makes a thread; runs the tasks as the specific entity type does.
thread.start_new_thread( self.taskthread, () )
def taskthread(self):
pass; # overloaded by individual entities that have their own way of doing things
def activate(self):
self.active=True
self.runtasks
def banish(self):
self.active=False # task threads check for this and stop
class Undead (Entity):
pass # which is what we make at undead people, of course :-)
class Zombie (Undead):
active = False # zombies require animating
# zombies run their tasks in order
def taskthread(self):
while self.active and (not kill):
for task in [t for t in self.tasks if t.active]:
if not self.active: break
task.run()
task.active=False
time.sleep(.05)
if not [t for t in self.tasks if t.active]: self.active = False
class Ghost (Undead):
active = False # ghosts require desturbing
# ghosts run their tasks in order, but may wait before starting a new task
def taskthread(self):
while self.active and (not kill):
for task in [t for t in self.tasks if t.active]:
time.sleep(int(random.random() * 60))
if not self.active: break
task.run()
task.active=False
if not [t for t in self.tasks if t.active]: self.active = False
class Vampire (Undead):
active = True # vampires are active immediately
# vampires process their tasks in random order
def taskthread(self):
random.shuffle(self.tasks) # tasks in random order
while self.active and (not kill):
for task in [t for t in self.tasks if t.active]:
if not self.active: break
task.run()
task.active=False
time.sleep(.05)
if not [t for t in self.tasks if t.active]: self.active = False
class Demon (Entity):
active = True # demons are always active
# demons process their tasks in random order, and sometimes multiple times
def taskthread(self):
random.shuffle(self.tasks) # tasks in random order
# may run multiple tasks at once (this may cause weird things to happen with
# threading, but I don't care; you shouldn't trust demons anyway)
if bool(int(random.random() * 4)):
thread.start_new_thread( self.taskthread, () )
while self.active and (not kill):
for task in [t for t in self.tasks if t.active]:
if not self.active: break
task.run()
task.active= not bool(int(random.random()*4)) # 1 in 4 chance a task will be repeated
time.sleep(.05)
if not [t for t in self.tasks if t.active]: self.active = False
class Djinn (Entity):
active = True # djinn are always active
def taskthread(self):
random.shuffle(self.tasks) # tasks in random order
# may run multiple tasks at once (this may cause weird things to happen with
# threading, but I don't care; you shouldn't trust demons anyway)
if bool(int(random.random() * 4)):
thread.start_new_thread( self.taskthread, () )
while self.active and (not kill):
for task in [t for t in self.tasks if t.active]:
if not self.active: break
# tasks may not run at all
if task.active: task.active= not bool(int(random.random()*4)) # 1 in 4 chance a task will be repeated
if task.active: task.run()
time.sleep(.05)
if not [t for t in self.tasks if t.active]: self.active = False
# Task objects store tasks and can be run
class Task:
entity = None
lines = []
name = ''
active = False
def __init__(self):
self.commands = \
{'animate': self.c_animate,
'banish': self.c_banish,
'disturb': self.c_disturb,
'forget': self.c_forget,
'invoke': self.c_invoke,
'moan': self.c_moan,
'remember': self.c_remember,
'say': self.c_say,
'command' : self.c_command}
# commands
def c_animate(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
if not isinstance(stack[-1], Zombie):
die("task %s, entity %s: attempt to animate non-zombie entity %s." % \
(self.name, self.entity.name, stack[-1].name))
stack.pop().activate()
else:
if not isinstance(self.entity, Zombie):
die("task %s, entity %s: attempt to animate non-zombie entity %s." % \
(self.name, self.entity.name, self.entity.name))
self.entity.activate()
def c_banish(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
stack.pop().banish()
else:
self.entity.banish()
def c_disturb(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
if not isinstance(stack[-1], Ghost):
die("task %s, entity %s: attempt to disturb non-ghost entity %s." % \
(self.name, self.entity.name, stack[-1].name))
stack.pop().activate()
else:
if not isinstance(self.entity, Ghost):
die("task %s, entity %s: attempt to disturb non-ghost entity %s." % \
(self.name, self.entity.name, self.entity.name))
self.entity.activate()
def c_forget(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
stack.pop().memory=None
else:
self.entity.memory=None
def c_invoke(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
stack.pop().activate()
else:
self.entity.activate()
def c_moan(self,stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
#sys.stdout.write(str(stack[-1].memory))
stack.append(stack.pop().memory)
else:
stack.append(self.entity.memory)
#sys.stdout.write(str(self.entity.memory))
def c_remember(self,stack):
#print "\n Remember :%s: " % str(stack)
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
theEntity = stack[-1]
values = stack[:-1]
else:
theEntity = self.entity
values = stack
total = 0
for value in values:
if isinstance(value, int):
# numbers simply get added
total += value
elif isinstance(value, str):
# strings with numbers in get added, others get ignored
try: total += int(value)
except: pass
elif isinstance(value, Entity):
# entities get their memories added
try: total += value.memory
except: pass
else:
# just try to add it, ignore if not possible
try: total += value
except: pass
for i in range(0,len(values)): stack.pop()
theEntity.memory = total
def c_command(self, stack):
if stack and len(stack)>=1 and isinstance(stack[-1],Entity):
theEntity = stack[-1]
values = stack[:-1]
else:
theEntity = self.entity
values = stack
if isinstance(self.entity, Zombie):
print theEntity.name + ": command me"
theEntity.memory = sys.stdin.readline()
elif isinstance(self.entity, Ghost):
print theEntity.name + ": what is your request mortal?"
theEntity.memory = sys.stdin.readline()
elif isinstance(self.entity, Vampire):
print theEntity.name + ": what does the prey require?"
theEntity.memory = sys.stdin.readline()
elif isinstance(self.entity, Demon):
print theEntity.name + ": speak mortal."
theEntity.memory = sys.stdin.readline()
elif isinstance(self.entity, Djinn):
print theEntity.name + ": what is it that this one is seeking?"
theEntity.memory = sys.stdin.readline()
def c_say(self, stack):
if not stack or (isinstance(stack[0],Entity) and len(stack)==1):
die("task %s, entity %s: argument error for SAY: nothing to say." % \
(self.name,self.entity.name))
if isinstance(stack[0],Entity):
sys.stdout.write(str(stack[1]))
if not isinstance(stack[1], str): sys.stdout.write(" ")
else:
sys.stdout.write(str(stack[0]))
if not isinstance(stack[0], str) or isinstance(stack[0], float): sys.stdout.write(" ")
# do task
def run(self):
try:self._run()
except:die("task %s, entity %s: something weird happened; program terminated to insure safety." \
% (self.name, self.entity.name))
def _run(self):
line_no = 0;
while (line_no < len(self.lines)):
cmds = splitline(self.lines[line_no])
stack = []
#print self.lines[line_no]
for cmd in reversed(cmds):
if cmd.lower() in ['command', 'animate', 'banish', 'disturb', 'forget',
'invoke', 'moan', 'remember', 'say']:
self.commands[cmd.lower()](stack)
elif cmd.lower() == "remembering":
if len(stack)>=1:
if isinstance(stack[-1],Entity):
if len(stack)>=2:
val = int(stack[-1].memory == stack[-2])
stack.pop()
stack.pop()
stack.append(val)
else:
die("task %s, entity %s: argument error for REMEMBERING." % \
(self.name, self.entity.name))
else:
stack.append( int(stack.pop() == self.entity.memory) )
else:
die("task %s, entity %s: argument error for REMEMBERING." % \
(self.name, self.entity.name))
elif cmd.lower() == "stumble":
return;
elif cmd.lower() == "rend":
try:
a=stack.pop()
stack.append(stack.pop() / a)
except:
die("task %s, entity %s: argument error for REND." % (self.name,self.entity.name))
elif cmd.lower() == "turn":
try:
stack.append(-stack.pop())
except:
die("task %s, entity %s: argument error for TURN." % (self.name,self.entity.name))
elif cmd.lower() == "around":
t = 1
try:
while t:
line_no -= 1
if self.lines[line_no].split()[0].lower() in ['around', 'until']: t += 1
if self.lines[line_no].split()[0].lower() == 'shamble': t -= 1
#break
except:
die("task %s, entity %s: unbalanced loop." % (self.name, self.entity.name))
elif cmd.lower() == "until":
if (not stack) or (not stack[0]):
t = 1
try:
while t:
line_no -= 1
if self.lines[line_no].split()[0].lower() == 'shamble': t -= 1
if self.lines[line_no].split()[0].lower() in ['around', 'until']: t += 1
#break
except:
die("task %s, entity %s: unbalanced loop." % (self.name, self.entity.name))
elif cmd.lower() == "taste":
if (not stack) or (not stack[0]):
t = 1
try:
while t:
line_no += 1
if self.lines[line_no].split()[0].lower() == 'taste': t += 1
if self.lines[line_no].split()[0].lower() == 'spit': t -= 1
if self.lines[line_no].split()[0].lower() == 'bad' and t == 1:
break
except:
die("task %s, entity %s: unbalanced taste/spit." %(self.name,self.entity.name))
elif cmd.lower() == "bad":
# this'll only happen if "taste" didn't send it to "bad"; so skip to "spit"
t = 1
try:
while t:
line_no += 1
if self.lines[line_no].split()[0].lower() == 'taste': t += 1
if self.lines[line_no].split()[0].lower() == 'spit': t -= 1
except:
die("task %s, entity %s: unbalanced taste/spit." % (self.name, self.entity.name))
elif cmd.lower() in ['shamble', 'good', 'spit']:
# syntax elements that do nothing themselves when reached
pass;
else:
# it's a value
if string_re.match(cmd):
stack.append(cmd[1:-1].replace('\\n','\n').replace('\\t','\t'))
elif integer_re.match(cmd):
stack.append(float(cmd))
if stack[-1] == int(stack[-1]): stack[-1] = int(stack[-1])
elif str(cmd) in self.entity.environment.entities:
stack.append(self.entity.environment.entities[cmd])
else:
die("task %s, entity %s: '%s' does not exist." % (self.name,self.entity.name,cmd))
line_no += 1
# The environment in which the entities do their tasks.
# Not necessarily a graveyard, one can work around that
# by not using any undead.
class Environment:
entities = {}
def __init__(self, file):
# read file
try:
f = open(file, "r")
code = f.read()
f.close()
except:
die("cannot open file %s." % file)
# parse code
self.parse(code)
def run(self):
global kill
# activate all entities that should be activated
[self.entities[e].runtasks() for e in self.entities if self.entities[e].active]
# keep the main thread running until all entities are done
while (not kill) and [e for e in self.entities if self.entities[e].active]:
time.sleep(1)
if kill: sys.exit()
print "\n"
# Make entities according to the code supplied
def parse(self, code):
currentEntity = None
inEntity = False
currentTask = None
# remove comments from code
code = comment_re.sub(lambda x:'', code)
# split into lines and remove whitespace
lines = [a.strip() for a in code.split("\n") if not a.strip() == '']
line_no = 0
while line_no < len(lines):
# print "LINE : '%s'" % lines[line_no]
if not inEntity:
a = declaration_re.match(lines[line_no])
if a:
if lines[line_no+1].lower() == 'summon':
# we're in an entity declaration block.
# start a new entity, if possible
if a.group(1) in self.entities:
die("line %d: entity '%s' is already defined." % (line_no, a.group(1)))
if a.group(2).lower() in ['zombie', 'enslaved undead']: type=Zombie
elif a.group(2).lower() in ['ghost', 'restless undead']: type=Ghost
elif a.group(2).lower() in ['vampire', 'free-willed undead']: type=Vampire
elif a.group(2).lower() == 'demon': type=Demon
elif a.group(2).lower() == 'djinn': type=Djinn
else:
die("line %d: '%s' is not a valid entity type." % a.group(2))
currentEntityName = a.group(1)
currentEntity = type()
currentEntity.name = currentEntityName
line_no += 1
inEntity = True
else:
die("line %d: entity declaration incomplete, missing SUMMON." % line_no)
else:
die("line %d: only entity declarations may appear outside of entities." % line_no)
else:
a = task_re.match(lines[line_no])
b = remember_re.match(lines[line_no])
if a:
#read the task, store it in the entity
currentTask = Task()
currentTask.name = a.group(1)
while True:
line_no += 1
if lines[line_no].lower() in ['bind', 'animate']:
currentTask.active = lines[line_no].lower() == 'animate'
currentTask.entity = currentEntity
currentEntity.tasks += [currentTask]
break
else:
currentTask.lines += [lines[line_no]]
elif b:
#default value
if b.group(1)[0] == '"' and b.group(1)[-1] == '"':
# string
currentEntity.memory = b.group(1)[1:-1]
else:
try: currentEntity.memory = int(b.group(1))
except ValueError:
die("line %d: REMEMBER outside of task may only use a constant." % line_no)
else:
if not lines[line_no].lower() in ['animate', 'bind', 'disturb']:
die("line %d: not allowed outside of task." % line_no)
else:
inEntity = False
if lines[line_no].lower() != 'bind':
# a state change (to active) is required
if not isinstance(currentEntity, Undead):
die(("line %d: safety check failed: free-willed non-undead *must* be bound " +\
"or all hell will break loose quite literally.") % line_no)
if lines[line_no].lower() == 'animate' and not isinstance(currentEntity,Zombie) \
or lines[line_no].lower() == 'disturb' and not isinstance(currentEntity,Ghost) :
die(("line %d: type error: you are either animating a ghost or disturbing " +\
"a zombie; both aren't a very good idea.") % line_no)
# The fact that we're still here means that the program hasn't been killed.
# That means the safety checks have passed, we can safely activate the entity.
currentEntity.active = True
currentEntity.environment = self
self.entities[currentEntity.name] = currentEntity
line_no += 1
if __name__ == '__main__':
if (len(sys.argv)) != 2: die("argument error.")
env = Environment(sys.argv[1])
env.run()