-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulator.py
394 lines (345 loc) · 12.8 KB
/
simulator.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
import os, yaml, util
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, ClearColor, Rectangle
from kivy.clock import Clock
from datamgr import variables, datastr
from variable import Variable
class Gauge:
def __init__(self, pos):
self.pos = pos
class Valve:
def __init__(self, pos):
self.pos = pos
self.modifier = 1
def rotate_modifier(self):
if self.modifier == -1:
self.modifier = 1
elif self.modifier == 1:
self.modifier = -1
class Pump:
def __init__(self, section):
self.section = section
self.power = 5.0
self.modifier = 0
def rotate_modifier(self):
if self.modifier == 0:
self.modifier = 1
elif self.modifier == 1:
self.modifier = -1
elif self.modifier == -1:
self.modifier = 0
class Section:
def __init__(self, pos, pattern):
self.pos = pos
self.pattern = pattern
self.content = 0.0
self.capacity = 10.0
self.neighbors = []
class PipeNetwork:
def __init__(self, cells, cols, rows):
self.cells = cells
self.rows = rows
self.cols = cols
self.sections = []
self.pumps = []
self.gauges = []
self.valves = []
self.build()
def find_section(self, pos, pattern):
for section in self.sections:
if section.pos == pos and section.pattern == pattern:
return section
return None
def join_vertical(self, pos, pos_top):
section = self.find_section(pos, '1000')
section_top = self.find_section(pos_top, '0010')
if section != None and section_top != None:
section.neighbors.append(section_top)
section_top.neighbors.append(section)
def join_horizontal(self, pos, pos_right):
section = self.find_section(pos, '0100')
section_right = self.find_section(pos_right, '0001')
if section != None and section_right != None:
section.neighbors.append(section_right)
section_right.neighbors.append(section)
def build(self):
i_row = 0
i_col = 0
for i_row in range(self.rows):
for i_col in range(self.cols):
cell = self.cells[i_row][i_col]
if cell.pattern == None:
continue
cell_sections = []
if cell.pattern[0] == '1':
cell_sections.append(Section([i_row, i_col], '1000'))
self.sections.append(cell_sections[-1])
if cell.pattern[1] == '1':
cell_sections.append(Section([i_row, i_col], '0100'))
self.sections.append(cell_sections[-1])
if cell.pattern[2] == '1':
cell_sections.append(Section([i_row, i_col], '0010'))
self.sections.append(cell_sections[-1])
if cell.pattern[3] == '1':
cell_sections.append(Section([i_row, i_col], '0001'))
self.sections.append(cell_sections[-1])
if cell.type == 'pump':
self.pumps.append(Pump(cell_sections[-1]))
elif cell.addon == 'valve':
self.valves.append(Valve([i_row, i_col]))
elif cell.addon == 'gauge':
self.gauges.append(Gauge([i_row, i_col]))
for section in cell_sections:
for neighbor in cell_sections:
if neighbor != section:
section.neighbors.append(neighbor)
for i_row in range(self.rows):
for i_col in range(self.cols):
#cell = self.cells[i_row][i_col]
if i_row < (self.rows - 1):
#cell_top = self.cells[i_row + 1][i_col]
self.join_horizontal([i_row, i_col], [i_row + 1, i_col])
if i_col < (self.cols - 1):
#cell_right = self.cells[i_row][i_col + 1]
self.join_vertical([i_row, i_col], [i_row, i_col + 1])
def search_section(self, section, searched):
if section in searched:
return
else: searched.append(section)
if section.content < section.capacity:
return section, searched
else:
for neighbor in section.neighbors:
if not neighbor in searched:
return self.search_section(neighbor, searched)
def valve_open(self, pos):
for valve in self.valves:
if valve.pos == pos:
return valve.modifier == 1
return True #no valve found to obstruct
def search_fill(self, previous, current, searched, fluid):
if fluid == 0: return
if current in searched:
return
else: searched.append(current)
if current.content < current.capacity:
if previous == None:
can_pass = True
elif previous.pos != current.pos:
can_pass = True
else: can_pass = self.valve_open(current.pos)
if can_pass:
diff = current.capacity - current.content
if fluid > diff:
current.content = current.capacity
fluid -= diff
else:
current.content += fluid
fluid = 0
else:
for neighbor in current.neighbors:
if not neighbor in searched:
return self.search_fill(current, neighbor, searched, fluid)
def fill_section(self, section, fluid):
diff = section.capacity - section.content
if fluid > diff:
section.content = section.capacity
return fluid - diff
else:
section.content += fluid
return 0
def run_gauges(self):
n_gauges = len(self.gauges)
i = 0
while i < n_gauges:
name = 'GAUGE_' + str(i).zfill(2)
if name in variables:
variable = variables[name]
else:
variable = Variable(name, 'word', name)
variable.meta['min'] = 0
variable.meta['max'] = 20
variables[name] = variable
sections = self.find_sections(self.gauges[i].pos)
variable.value = 0
for section in sections:
variable.value += section.content
datastr.write(variable)
i += 1
def find_sections(self, pos):
for section in self.sections:
if section.pos == pos:
yield section
def run_pumps(self):
for pump in self.pumps:
if pump.modifier > 0:
self.search_fill(None, pump.section, [], pump.power)
#fluid = pump.power
# while fluid > 0:
# section, searched = self.search_section(pump.section, [])
# print section.pos, [x.pos for x in searched]
# if section is None:
# print 'No section found'
# return
# fluid = self.fill_section(section, fluid)
elif pump.modifier < 0:
if pump.power > pump.section.content:
pump.section.content = 0
else: pump.section.content -= pump.power
def propagation(self):
for section in self.sections:
for neighbor in section.neighbors:
if neighbor.pos != section.pos:
can_pass = True
else: can_pass = self.valve_open(section.pos)
if can_pass:
diff = section.content - neighbor.content
if abs(diff) > 0.1: #TODO viscosity
section.content -= diff / 2
neighbor.content += diff / 2
class Cell():
def __init__(self):
self.pattern = None
self.type = None
self.addon = None
class SimulGrid(FloatLayout):
def __init__(self, **kwargs):
super(SimulGrid, self).__init__(**kwargs)
self.rows = None
self.cols = None
self.cell_width = None
self.cell_height = None
self.network = None
self.cells = self.load_data()
with self.canvas:
Color(.2, .2, .2)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_gfx)
self.bind(size=self.update_gfx)
def build_network(self):
self.network = PipeNetwork(list(self.cells), self.rows, self.cols)
def on_touch_down(self, touch):
if not self.collide_point(touch.x, touch.y):
return
i_row = int( (touch.x - self.pos[0]) / self.cell_width )
i_col = int( (touch.y - self.pos[1]) / self.cell_height )
cell = self.cells[i_row][i_col]
if cell.type == 'pump':
for pump in self.network.pumps:
if pump.section.pos == [i_row, i_col]:
pump.rotate_modifier()
if cell.addon == 'valve':
for valve in self.network.valves:
if valve.pos == [i_row, i_col]:
valve.rotate_modifier()
def set_dimenstion(self, rows, cols):
self.rows = rows
self.cols = cols
if self.cells is None:
self.cells = [[Cell() for x in range(rows)] for y in xrange(cols)]
self.update_gfx()
def update_gfx(self, *args):
self.cell_width = self.size[0] / self.cols
self.cell_height = self.size[1] / self.rows
self.draw_tiles()
def load_data(self):
if os.path.isfile('data/pipeline-grid.yaml'):
stream = file('data/pipeline-grid.yaml', 'r')
cells = yaml.load(stream)
return list(cells)
else: return None
def draw_tiles(self, *args):
self.canvas.clear()
with self.canvas:
Color(1,1,1)
# ground and pipes
for i_row in range(self.rows):
for i_col in range(self.cols):
cell_x = self.pos[0] + i_row * self.cell_width
cell_y = self.pos[1] + i_col * self.cell_height
Rectangle(source=os.path.join(util.dir_img, 'ground.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
cell = self.cells[i_row][i_col]
if not cell.pattern is None:
Rectangle(source=os.path.join(util.dir_img, cell.type + '-' + cell.pattern + '.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
#fluid
if self.network != None:
Color(.1,.5,1.)
for section in self.network.sections:
cell_x = self.pos[0] + section.pos[0] * self.cell_width
cell_y = self.pos[1] + section.pos[1] * self.cell_height
fill = section.content / section.capacity
if section.pattern == '1000':
pos = [cell_x + self.cell_width / 2, cell_y + self.cell_height / 2]
size = [self.cell_width * 0.1 * fill, self.cell_height / 2]
pos = [pos[0] - size[0] / 2, pos[1]]
if section.pattern == '0100':
pos = [cell_x + self.cell_width / 2, cell_y + self.cell_height / 2]
size = [self.cell_width / 2, self.cell_height * 0.1 * fill]
pos = [pos[0], pos[1] - size[1] / 2]
if section.pattern == '0010':
pos = [cell_x + self.cell_width / 2, cell_y]
size = [self.cell_width * 0.1 * fill, self.cell_height / 2]
pos = [pos[0] - size[0] / 2, pos[1]]
if section.pattern == '0001':
pos = [cell_x, cell_y + self.cell_height / 2]
size = [self.cell_width / 2, self.cell_height * 0.1 * fill]
pos = [pos[0], pos[1] - size[1] / 2]
Rectangle(pos=pos, size=size)
Color(1,1,1)
# pumps
for pump in self.network.pumps:
cell_x = self.pos[0] + pump.section.pos[0] * self.cell_width
cell_y = self.pos[1] + pump.section.pos[1] * self.cell_height
if pump.modifier > 0:
Rectangle(source=os.path.join(util.dir_img, 'indicator-plus.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
elif pump.modifier < 0:
Rectangle(source=os.path.join(util.dir_img, 'indicator-minus.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
# valves
for valve in self.network.valves:
cell_x = self.pos[0] + valve.pos[0] * self.cell_width
cell_y = self.pos[1] + valve.pos[1] * self.cell_height
if valve.modifier > 0:
Rectangle(source=os.path.join(util.dir_img, 'indicator-plus.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
elif valve.modifier < 0:
Rectangle(source=os.path.join(util.dir_img, 'indicator-minus.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
# addons
for i_row in range(self.rows):
for i_col in range(self.cols):
cell_x = self.pos[0] + i_row * self.cell_width
cell_y = self.pos[1] + i_col * self.cell_height
cell = self.cells[i_row][i_col]
if not cell.addon is None:
Rectangle(source=os.path.join(util.dir_img, cell.addon + '.png'),
pos=[cell_x, cell_y], size=[self.cell_width, self.cell_height])
class Simulator:
running = False
grid = None
@staticmethod
def set_grid(simulgrid):
Simulator.grid = simulgrid
@staticmethod
def process(dt):
if Simulator.grid != None:
Simulator.grid.network.run_pumps()
Simulator.grid.network.propagation()
Simulator.grid.draw_tiles()
return Simulator.running
@staticmethod
def measure(dt):
if Simulator.grid != None:
Simulator.grid.network.run_gauges()
return Simulator.running
@staticmethod
def start(instance):
Clock.schedule_interval(Simulator.process, 0.2)
Clock.schedule_interval(Simulator.measure, 1)
Simulator.running = True
@staticmethod
def stop(instance):
Simulator.running = False