-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellular.py
More file actions
538 lines (455 loc) · 14.6 KB
/
Copy pathcellular.py
File metadata and controls
538 lines (455 loc) · 14.6 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
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
import random
import sys
neighbourSynonyms=('neighbours','neighbors','neighbour','neighbor')
class Cell:
def __getattr__(self,key):
if key in neighbourSynonyms:
pts=[self.world.getPointInDirection(self.x,self.y,dir) for dir in range(self.world.directions)]
ns=tuple([self.world.grid[y][x] for (x,y) in pts])
for n in neighbourSynonyms:
self.__dict__[n]=ns
return ns
raise AttributeError,key
class Agent:
def __setattr__(self,key,val):
if key=='cell':
old=self.__dict__.get(key,None)
if old!=None: old.agents.remove(self)
if val!=None: val.agents.append(self)
self.__dict__[key]=val
def __getattr__(self,key):
if key=='leftCell':
return self.getCellOnLeft()
elif key=='rightCell':
return self.getCellOnRight()
elif key=='aheadCell':
return self.getCellAhead()
raise AttributeError,key
def turn(self,amount):
self.dir=(self.dir+amount)%self.world.directions
def turnLeft(self):
self.turn(-1)
def turnRight(self):
self.turn(1)
def turnAround(self):
self.turn(self.world.directions/2)
def goInDirection(self,dir):
target=self.cell.neighbour[dir]
if getattr(target,'wall',False):
return
self.cell=target
def goForward(self):
self.goInDirection(self.dir)
def goBackward(self):
self.turnAround()
self.goForward()
self.turnAround()
def getCellAhead(self):
return self.cell.neighbour[self.dir]
def getCellOnLeft(self):
return self.cell.neighbour[(self.dir-1)%self.world.directions]
def getCellOnRight(self):
return self.cell.neighbour[(self.dir+1)%self.world.directions]
def goTowards(self,target):
if self.cell==target: return
best=None
for n in self.cell.neighbours:
if n==target:
best=target
break
dist=(n.x-target.x)**2+(n.y-target.y)**2
if best==None or bestDist>dist:
best=n
bestDist=dist
if best!=None:
if getattr(best,'wall',False): return
self.cell=best
class World:
def __init__(self,cell=None,width=None,height=None,directions=8,filename=None):
if cell==None: cell=Cell
self.Cell=cell
self.display=makeDisplay(self)
self.directions=directions
if filename!=None:
data=file(filename).readlines()
if height==None: height=len(data)
if width==None: width=max([len(x.rstrip()) for x in data])
if width==None: width=20
if height==None: height=20
self.width=width
self.height=height
self.image=None
self.reset()
if filename!=None:
self.load(filename)
def getCell(self,x,y):
return self.grid[y][x]
def reset(self):
self.grid=[[self.makeCell(i,j) for i in range(self.width)] for j in range(self.height)]
self.dictBackup=[[{} for i in range(self.width)] for j in range(self.height)]
self.agents=[]
self.age=0
def makeCell(self,x,y):
c=self.Cell()
c.x=x
c.y=y
c.world=self
c.agents=[]
return c
def randomize(self):
if not hasattr(self.Cell,'randomize'): return
for row in self.grid:
for cell in row:
cell.randomize()
def save(self,f=None):
if not hasattr(self.Cell,'save'): return
if type(f)==type(''):
f=file(f,'w')
total=''
for j in range(self.height):
line=''
for i in range(self.width):
line+=self.grid[j][i].save()
total+='%s\n'%line
if f!=None:
f.write(total)
f.close()
else:
return total
def load(self,f):
if not hasattr(self.Cell,'load'): return
if type(f)==type(''):
f=file(f)
lines=f.readlines()
lines=[x.rstrip() for x in lines]
fh=len(lines)
fw=max([len(x) for x in lines])
if fh>self.height:
fh=self.height
starty=0
else:
starty=(self.height-fh)/2
if fw>self.width:
fw=self.width
startx=0
else:
startx=(self.width-fw)/2
self.reset()
for j in range(fh):
line=lines[j]
for i in range(min(fw,len(line))):
self.grid[starty+j][startx+i].load(line[i])
def update(self):
if hasattr(self.Cell,'update'):
for j,row in enumerate(self.grid):
for i,c in enumerate(row):
self.dictBackup[j][i].update(c.__dict__)
c.update()
c.__dict__,self.dictBackup[j][i]=self.dictBackup[j][i],c.__dict__
for j,row in enumerate(self.grid):
for i,c in enumerate(row):
c.__dict__,self.dictBackup[j][i]=self.dictBackup[j][i],c.__dict__
for a in self.agents:
a.update()
self.display.redraw()
else:
for a in self.agents:
oldCell=a.cell
a.update()
if oldCell!=a.cell:
self.display.redrawCell(oldCell.x,oldCell.y)
self.display.redrawCell(a.cell.x,a.cell.y)
self.display.update()
self.age+=1
def getPointInDirection(self,x,y,dir):
if self.directions==8:
dx,dy=[(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1)][dir]
elif self.directions==4:
dx,dy=[(0,-1),(1,0),(0,1),(-1,0)][dir]
elif self.directions==6:
if y%2==0:
dx,dy=[(1,0),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)][dir]
else:
dx,dy=[(1,0),(1,1),(0,1),(-1,0),(0,-1),(1,-1)][dir]
x2=x+dx
y2=y+dy
if x2<0: x2+=self.width
if y2<0: y2+=self.height
if x2>=self.width: x2-=self.width
if y2>=self.height: y2-=self.height
return (x2,y2)
def addAgent(self,agent,x=None,y=None,cell=None,dir=None):
self.agents.append(agent)
if cell!=None:
x=cell.x
y=cell.y
if x==None: x=random.randrange(self.width)
if y==None: y=random.randrange(self.height)
if dir==None: dir=random.randrange(self.directions)
agent.cell=self.grid[y][x]
agent.dir=dir
agent.world=self
def makeDisplay(world):
d=Display()
d.world=world
return d
class DummyDisplay:
def activate(self,size=4):
pass
def redraw(self):
pass
def redrawCell(self,x,y):
pass
def update(self):
pass
def setTitle(self,title):
pass
class TkinterDisplay:
activated=False
paused=False
title=''
updateEvery=1
root=None
def activate(self,size=4):
self.bg=None
self.size=size
if TkinterDisplay.root==None:
TkinterDisplay.root=Tkinter.Tk()
for c in self.root.winfo_children():
c.destroy()
self.activated=True
self.imageLabel=Tkinter.Label(self.root)
self.imageLabel.pack(side=Tkinter.LEFT,fill=Tkinter.BOTH,expand=1)
self.frameWidth,self.frameHeight=self.world.width*size,self.world.height*size
self.root.geometry('%dx%d'%(self.world.width*size,self.world.height*size))
self.root.update()
self.redraw()
self.root.bind('<Configure>',self.onConfigure)
self.root.bind('<Prior>',self.onPageUp)
self.root.bind('<Next>',self.onPageDown)
self.root.bind('<space>',self.pause)
def update(self):
if not self.activated: return
if self.world.age%self.updateEvery!=0 and not self.paused: return
self.setTitle(self.title)
self.imageLabel.update()
def setTitle(self,title):
if not self.activated: return
self.title=title
title+=' %s'%makeTitle(self.world)
if self.root.title()!=title:
self.root.title(title)
def onConfigure(self,event):
if event.width!=self.frameWidth or event.height!=self.frameHeight:
oldSize=self.size
scalew=event.width/self.world.width
scaleh=event.height/self.world.height
self.size=min(scalew,scaleh)
if self.size<1: self.size=1
if oldSize<self.size:
self.imageCache.clear()
if oldSize!=self.size:
self.redraw()
self.frameWidth=event.width
self.frameHeight=event.height
def onPageDown(self,event):
self.updateEvery/=2
if self.updateEvery<1: self.updateEvery=1
def onPageUp(self,event):
self.updateEvery*=2
def pause(self,event=None):
self.paused=not self.paused
while self.paused:
self.update()
def getBackground(self):
if self.bg==None:
r,g,b=self.imageLabel.winfo_rgb(self.root['background'])
self.bg='%c%c%c'%(r>>8,g>>8,b>>8)
return self.bg
def redraw(self):
if not self.activated: return
hexgrid=self.world.directions==6
iw=self.world.width*self.size
ih=self.world.height*self.size
if hexgrid: iw+=self.size/2
f=file('temp.ppm','wb')
f.write('P6\n%d %d\n255\n'%(iw,ih))
odd=False
for row in self.world.grid:
line=cStringIO.StringIO()
if hexgrid and odd: line.write(self.getBackground()*(self.size/2))
for cell in row:
if len(cell.agents)>0:
c=self.getDataColour(cell.agents[0])
else:
c=self.getDataColour(cell)
line.write(c*self.size)
if hexgrid and not odd: line.write(self.getBackground()*(self.size/2))
odd=not odd
f.write(line.getvalue()*self.size)
f.close()
self.image=Tkinter.PhotoImage(file='temp.ppm')
self.imageLabel.config(image=self.image)
imageCache={}
def redrawCell(self,x,y):
if not self.activated: return
sx=x*self.size
sy=y*self.size
if y%2==1 and self.world.directions==6: sx+=self.size/2
cell=self.world.grid[y][x]
if len(cell.agents)>0:
c=self.getTextColour(cell.agents[0])
else:
c=self.getTextColour(cell)
sub=self.imageCache.get(c,None)
if sub==None:
sub=Tkinter.PhotoImage(width=1,height=1)
sub.put(c,to=(0,0))
sub=sub.zoom(self.size)
self.imageCache[c]=sub
self.image.tk.call(self.image,'copy',sub,'-from',0,0,self.size,self.size,'-to',sx,sy)
def getTextColour(self,obj):
c=getattr(obj,'colour',None)
if c==None: c=getattr(obj,'color','white')
if callable(c): c=c()
if type(c)==type(()):
if type(c[0])==type(0.0):
c=(int(c[0]*255),int(c[1]*255),int(c[2]*255))
return '#%02x%02x%02x'%c
return c
dataCache={}
def getDataColour(self,obj):
c=getattr(obj,'colour',None)
if c==None: c=getattr(obj,'color','white')
if callable(c): c=c()
if type(c)==type(()):
if type(c[0])==type(0.0):
c=(int(c[0]*255),int(c[1]*255),int(c[2]*255))
return '%c%c%c'%c
else:
val=self.dataCache.get(c,None)
if val==None:
r,g,b=self.imageLabel.winfo_rgb(c)
val='%c%c%c'%(r>>8,g>>8,b>>8)
self.dataCache[c]=val
return val
class PygameDisplay:
activated=False
paused=False
title=''
updateEvery=1
screen=None
def activate(self,size=4):
self.size=size
pygame.init()
w=self.world.width*size
h=self.world.height*size
if self.world.directions==6: w+=size/2
if PygameDisplay.screen==None or PygameDisplay.screen.get_width()!=w or PygameDisplay.screen.get_height()!=h:
PygameDisplay.screen=pygame.display.set_mode((w,h),pygame.RESIZABLE,32)
self.activated=True
self.defaultColour=self.getColour(self.world.grid[0][0].__class__())
self.redraw()
def redraw(self):
if not self.activated: return
self.screen.fill(self.defaultColour)
hexgrid=self.world.directions==6
self.offsety=(self.screen.get_height()-self.world.height*self.size)/2
self.offsetx=(self.screen.get_width()-self.world.width*self.size)/2
sy=self.offsety
odd=False
for row in self.world.grid:
sx=self.offsetx
if hexgrid and odd: sx+=self.size/2
for cell in row:
if len(cell.agents)>0:
c=self.getColour(cell.agents[0])
else:
c=self.getColour(cell)
if c!=self.defaultColour:
try:
self.screen.fill(c,(sx,sy,self.size,self.size))
except TypeError:
print 'Error: invalid colour:',c
sx+=self.size
odd=not odd
sy+=self.size
def redrawCell(self,x,y):
if not self.activated: return
sx=x*self.size+self.offsetx
sy=y*self.size+self.offsety
if y%2==1 and self.world.directions==6: sx+=self.size/2
cell=self.world.grid[y][x]
if len(cell.agents)>0:
c=self.getColour(cell.agents[0])
else:
c=self.getColour(cell)
self.screen.fill(c,(sx,sy,self.size,self.size))
def update(self):
if not self.activated: return
if self.world.age%self.updateEvery!=0 and not self.paused: return
self.setTitle(self.title)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
self.onResize(event)
elif event.type == pygame.KEYDOWN and event.key==pygame.K_PAGEUP:
self.updateEvery*=2
elif event.type == pygame.KEYDOWN and event.key==pygame.K_PAGEDOWN:
self.updateEvery/=2
if self.updateEvery<1: self.updateEvery=1
elif event.type == pygame.KEYDOWN and event.key==pygame.K_SPACE:
self.pause()
pygame.display.flip()
def setTitle(self,title):
if not self.activated: return
self.title=title
title+=' %s'%makeTitle(self.world)
if pygame.display.get_caption()[0]!=title:
pygame.display.set_caption(title)
def pause(self,event=None):
self.paused=not self.paused
while self.paused:
self.update()
def onResize(self,event):
if not self.activated: return
pygame.display.set_mode(event.size,pygame.RESIZABLE,32)
oldSize=self.size
scalew=event.size[0]/self.world.width
scaleh=event.size[1]/self.world.height
self.size=min(scalew,scaleh)
if self.size<1: self.size=1
self.redraw()
def getColour(self,obj):
c=getattr(obj,'colour',None)
if c==None: c=getattr(obj,'color','white')
if callable(c): c=c()
if type(c)==type(()):
if type(c[0])==type(0.0):
c=(int(c[0]*255),int(c[1]*255),int(c[2]*255))
return c
return pygame.color.Color(c)
def saveImage(self,filename=None):
if filename==None: filename='%05d.bmp'%self.world.age
pygame.image.save(self.screen,filename)
def makeTitle(world):
text='age: %d'%world.age
extra=[]
if world.display.paused: extra.append('paused')
if world.display.updateEvery!=1: extra.append('skip=%d'%world.display.updateEvery)
if len(extra)>0:
text+=' [%s]'%', '.join(extra)
return text
try:
import pygame
Display=PygameDisplay
except:
try:
import Tkinter
import cStringIO
Display=TkinterDisplay
except:
Display=DummyDisplay