forked from phreeza/cells
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcells.py
executable file
·497 lines (413 loc) · 13.6 KB
/
cells.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
#!/usr/bin/env python
#TODO:
# - Make terrain work
# - Make ScalarView
# - Add more actions: PASS, LIFT, DROP,etc...
# - derive SelfView with more info than the general AgentView
# - render terrain and energy landscapes
# - fractal terrain generation
# - make rendering "smart"(and/or openGL)
# - Split into several files.
# - Messaging system
# - limit frame rate
# - response objects for outcome of action
# - Desynchronize agents
import ConfigParser
import itertools
import math
import random
import sys
import time
import numpy
import pygame, pygame.locals
config = ConfigParser.RawConfigParser()
def get_mind(name):
full_name = 'minds.' + name
__import__(full_name)
return sys.modules[full_name]
bounds = None # HACK
symmetric = None
mind_list = None
def main():
global bounds,symmetric, mind_list
try:
config.read('default.cfg')
bounds = config.getint('terrain', 'bounds')
symmetric = config.getboolean('terrain', 'symmetric')
minds_str = str(config.get('minds', 'minds'))
mind_list = [get_mind(n) for n in minds_str.split(',')]
except Exception as e:
print 'Got error: %s' % e
config.add_section('minds')
config.set('minds', 'minds', 'mind1,mind2')
config.add_section('terrain')
config.set('terrain', 'bounds', '300')
config.set('terrain', 'symmetric', 'true')
with open('default.cfg', 'wb') as configfile:
config.write(configfile)
config.read('default.cfg')
bounds = config.getint('terrain', 'bounds')
symmetric = config.getboolean('terrain', 'symmetric')
# accept command line arguments for the minds over those in the config
try:
if len(sys.argv)>2:
mind_list = [get_mind(n) for n in sys.argv[1:] ]
except (ImportError,IndexError):
pass
try:
import psyco
psyco.full()
except ImportError:
pass
def signum(x):
if x > 0:
return 1
if x < 0:
return -1
return 0
class Game:
def __init__(self):
self.size = self.width,self.height = (bounds,bounds)
self.messages = [MessageQueue() for x in mind_list]
self.disp = Display(self.size,scale=2)
self.time = 0
self.tic = time.time()
self.terr = ScalarMapLayer(self.size)
self.terr.set_random(5)
self.minds = [m.AgentMind for m in mind_list]
self.update_fields = [(x,y) for x in xrange(self.width) for y in xrange(self.height)]
self.energy_map = ScalarMapLayer(self.size)
self.energy_map.set_random(10)
self.plant_map = ObjectMapLayer(self.size,None)
self.plant_population = []
self.agent_map = ObjectMapLayer(self.size,None)
self.agent_population = []
self.winner = False
if symmetric:
self.n_plants = 7
else:
self.n_plants = 14
for x in xrange(self.n_plants):
mx = random.randrange(1,self.width-1)
my = random.randrange(1,self.height-1)
eff = random.randrange(5,11)
p = Plant(mx, my, eff)
self.plant_population.append(p)
if symmetric:
p = Plant(my, mx, eff)
self.plant_population.append(p)
self.plant_map.insert(self.plant_population)
for idx in xrange(len(self.minds)):
(mx,my) = self.plant_population[idx].get_pos()
fuzzed_x = mx + random.randrange(-1,2)
fuzzed_y = my + random.randrange(-1,2)
self.agent_population.append(Agent(fuzzed_x, fuzzed_y, idx, self.minds[idx], None))
self.agent_map.insert(self.agent_population)
def run_plants(self):
for p in self.plant_population:
(x,y) = p.get_pos()
for dx in (-1,0,1):
for dy in (-1,0,1):
if self.energy_map.in_range(x+dx,y+dy):
self.energy_map.change(x+dx,y+dy,p.get_eff())
def add_agent(self,a):
self.agent_population.append(a)
self.agent_map.set(a.x, a.y, a)
def del_agent(self,a):
self.agent_population.remove(a)
self.agent_map.set(a.x, a.y, None)
a.alive = False
def move_agent(self, a, x, y):
if (abs(self.terr.get(x,y)-self.terr.get(a.x,a.y))<=4):
self.agent_map.set(a.x, a.y, None)
self.agent_map.set(x, y, a)
a.x = x
a.y = y
def get_next_move(self,old_x, old_y, x, y):
dx = signum(x - old_x)
dy = signum(y - old_y)
return (old_x + dx, old_y + dy)
def run_agents(self):
views = []
self.update_fields = []
update_fields_append = self.update_fields.append
agent_map_get_small_view_fast = self.agent_map.get_small_view_fast
plant_map_get_small_view_fast = self.plant_map.get_small_view_fast
energy_map = self.energy_map
WV = WorldView
views_append = views.append
for a in self.agent_population:
update_fields_append(a.get_pos())
x = a.x
y = a.y
agent_view = agent_map_get_small_view_fast(x, y)
plant_view = plant_map_get_small_view_fast(x, y)
world_view = WV(a, agent_view, plant_view, energy_map)
views_append((a,world_view))
#get actions
messages = self.messages
actions = [(a, a.act(v, messages[a.team])) for (a,v) in views]
random.shuffle(actions)
#apply agent actions
for (agent,action) in actions:
agent.energy -= 1
# if agent.alive:
if action.type == ACT_MOVE:
act_x, act_y = action.get_data()
(new_x, new_y) = self.get_next_move(agent.x, agent.y, act_x, act_y)
if self.agent_map.in_range(new_x, new_y) and not self.agent_map.get(new_x, new_y):
self.move_agent(agent, new_x, new_y)
elif action.type == ACT_SPAWN:
act_x, act_y = action.get_data()[:2]
(new_x, new_y) = self.get_next_move(agent.x, agent.y, act_x, act_y)
if self.agent_map.in_range(new_x, new_y) and (not self.agent_map.get(new_x, new_y)) and agent.energy >= 50:
a = Agent(new_x, new_y, agent.get_team(),self.minds[agent.get_team()], action.get_data()[2:])
self.add_agent(a)
agent.energy -= 50
elif action.type == ACT_EAT:
intake = self.energy_map.get(agent.x, agent.y)
agent.energy += intake
self.energy_map.change(agent.x, agent.y, -intake)
elif action.type == ACT_ATTACK:
act_x, act_y = act_data = action.get_data()
(new_x, new_y) = next_pos = self.get_next_move(agent.x, agent.y, act_x, act_y)
if self.agent_map.get(act_x, act_y) and (next_pos == act_data) and self.agent_map.get(act_x, act_y).alive:
energy = self.agent_map.get(new_x, new_y).energy + 25
self.energy_map.change(new_x, new_y, energy)
self.del_agent(self.agent_map.get(new_x, new_y))
elif action.type == ACT_LIFT:
if not agent.loaded and self.terr.get(agent.x, agent.y) > 0:
agent.loaded = True
self.terr.change(agent.x, agent.y, -1)
elif action.type == ACT_DROP:
if agent.loaded:
agent.loaded = False
self.terr.change(agent.x, agent.y, 1)
#let agents die if their energy is too low
team = [0 for n in self.minds]
for (agent,action) in actions:
if agent.energy < 0 and agent.alive:
self.energy_map.change(agent.x, agent.y, 25)
self.del_agent(agent)
else :
team[agent.team] += 1
if (team[0] == 0) :
print "Winner is blue in: " + str(self.time)
self.winner = True
if (team[1] == 0) :
print "Winner is red in: " + str(self.time)
self.winner = True
def tick(self):
self.disp.update(self.terr,self.agent_population,self.plant_population,self.update_fields)
self.disp.flip()
# test for spacebar pressed - if yes, restart
for event in pygame.event.get():
if (event.type == pygame.locals.KEYUP and event.key == pygame.locals.K_SPACE):
self.winner = -1
self.run_agents()
self.run_plants()
for msg in self.messages:
msg.update()
self.time += 1
#pygame.time.wait(int(1000*(time.time()-self.tic)))
self.tic = time.time()
class MapLayer:
def __init__(self,size,val=0):
self.size = self.width, self.height = size
self.values = numpy.array([[val for x in xrange(self.width)]
for y in xrange(self.height)], numpy.object_)
def get(self,x,y):
if y >= 0 and x >= 0:
try:
return self.values[x,y]
except IndexError:
return None
return None
def set(self, x, y, val):
self.values[x,y] = val
def in_range(self, x, y):
return (0 <= x < self.width and 0 <= y < self.height)
class ScalarMapLayer(MapLayer):
def set_random(self,range):
self.values = numpy.random.random_integers(0, range-1, (self.width, self.height))
def change(self, x, y, val):
self.values[x, y] += val
class ObjectMapLayer(MapLayer):
# def get_view(self, x, y, r):
# indices = [(j, k) for j in xrange(x-r, x+r+1) for k in xrange(y-r, y+r+1)
# if 0 <= j < self.width and 0 <= k < self.height and (j != x or k != y)]
# a = [j[0] for j in indices]
# b = [j[1] for j in indices]
# slc = self.values[a,b]
# slc = [a.get_view() for a in slc if a is not None]
# # old_way = self.old_get_view(x, y, r)
# # assert all(type(x) == type(y) for (x,y) in itertools.izip_longest(slc, old_way)), '%s; %s' % (slc, self.old_get_view(x,y,r))
# return slc
def get_small_view_fast(self, x, y):
ret = []
get = self.get
append = ret.append
width = self.width
height = self.height
for dx in (-1, 0, 1):
for dy in (-1, 0, 1):
if not (dx or dy):
continue
try:
adj_x = x + dx
if not 0 <= adj_x < width:
continue
adj_y = y + dy
if not 0 <= adj_y < height:
continue
a = self.values[adj_x, adj_y]
if a is not None:
append(a.get_view())
except IndexError:
pass
return ret
def get_view(self, x, y, r):
ret = []
for x_off in xrange(-r,r+1):
for y_off in xrange(-r,r+1):
if x_off == 0 and y_off == 0:
continue
a = self.get(x + x_off, y + y_off)
if a is not None:
ret.append(a.get_view())
return ret
def insert(self,list):
for o in list:
self.set(o.x, o.y, o)
class Agent:
__slots__ = ['x', 'y', 'mind', 'energy', 'alive', 'team', 'loaded', 'color',
'act']
def __init__(self, x, y, team, AgentMind, cargs):
self.x = x
self.y = y
self.mind = AgentMind(cargs)
self.energy = 25
self.alive = True
self.team = team
self.loaded = False
colors = [(255,0,0),(0,0,255),(255,0,255),(255,255,0)]
self.color = colors[team%len(colors)]
self.act = self.mind.act
def get_team(self):
return self.team
def get_pos(self):
return (self.x, self.y)
def set_pos(self, x, y):
self.x = x
self.y = y
def get_team(self):
return self.team
def get_view(self):
return AgentView(self)
#def act(self,view,m):
# return self.mind.act(view,m)
ACT_SPAWN, ACT_MOVE, ACT_EAT, ACT_ATTACK, ACT_LIFT, ACT_DROP = range(6)
class Action:
def __init__(self,type,data=None):
self.type = type
self.data = data
def get_data(self):
return self.data
def get_type(self):
return self.type
class PlantView:
def __init__(self,p):
self.x = p.x
self.y = p.y
self.eff = p.get_eff()
def get_pos(self):
return (self.x, self.y)
def get_eff(self):
return self.eff
class AgentView:
def __init__(self,agent):
(self.x, self.y) = agent.get_pos()
self.team = agent.get_team()
def get_pos(self):
return (self.x, self.y)
def get_team(self):
return self.team
class WorldView:
def __init__(self,me,agent_views,plant_views,energy_map):
self.agent_views = agent_views
self.plant_views = plant_views
self.energy_map = energy_map
self.me = me
def get_me(self):
return self.me
def get_agents(self):
return self.agent_views
def get_plants(self):
return self.plant_views
def get_energy(self):
return self.energy_map
class Display:
black = 0, 0, 0
red = 255, 0, 0
green = 0, 255, 0
yellow = 255,255,0
def __init__(self,size,scale=5):
self.width, self.height = size
self.scale = scale
self.size = (self.width*self.scale,self.height*self.scale)
pygame.init()
self.screen = pygame.display.set_mode(self.size)
def update(self,terr,pop,plants,upfields):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for f in upfields:
(x,y)=f
scaled_x = x * self.scale
scaled_y = y * self.scale
self.screen.fill((min(255,20*terr.get(x,y)),min(255,10*terr.get(x,y)),0),pygame.Rect((scaled_x,scaled_y),(self.scale,self.scale)))
for a in pop:
(x,y)=a.get_pos()
x *= self.scale
y *= self.scale
self.screen.fill(a.color,pygame.Rect((x,y),(self.scale,self.scale)))
for a in plants:
(x,y)=a.get_pos()
x *= self.scale
y *= self.scale
self.screen.fill(self.green,pygame.Rect((x,y),(self.scale,self.scale)))
def flip(self):
pygame.display.flip()
class Plant:
def __init__(self, x, y, eff):
self.x = x
self.y = y
self.eff = eff
def get_pos(self):
return (self.x, self.y)
def get_eff(self):
return self.eff
def get_view(self):
return PlantView(self)
class MessageQueue:
def __init__(self):
self.__inlist = []
self.__outlist = []
def update(self):
self.__outlist = self.__inlist
self.__inlist = []
def send_message(self,m):
self.__inlist.append(m)
def get_messages(self):
return self.__outlist
class Message:
def __init__(self,message):
self.message = message
def get_message(self):
return self.message
if __name__ == "__main__":
main()
while 1:
game = Game()
while not game.winner:
game.tick()