-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.coffee
228 lines (180 loc) · 5.81 KB
/
util.coffee
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
chars =
positive: '+'
negative: '-'
nothing: ' '
thinsolid: 'x'
shuttle: 'S'
thinshuttle: 's'
bridge: 'b'
parseXY = exports.parseXY = (k) ->
[x,y] = k.split ','
{x:x|0, y:y|0}
numKeys = exports.numKeys = (obj) ->
num = 0
num++ for k of obj
num
# Flood fill through the grid from a cell
exports.fill = (initial_square, f) ->
visited = {}
visited["#{initial_square.x},#{initial_square.y}"] = true
to_explore = [initial_square]
hmm = (x,y) ->
k = "#{x},#{y}"
if not visited[k]
visited[k] = true
to_explore.push {x,y}
while n = to_explore.shift()
ok = f n.x, n.y, hmm
if ok
hmm n.x+1, n.y
hmm n.x-1, n.y
hmm n.x, n.y+1
hmm n.x, n.y-1
return
# Find the intersection of list1 and list2. Return null if the intersection is
# empty.
intersectListOrNull = (list1, list2) ->
intersection = null
for s,i in list1 when s and list2[i]
intersection ||= []
intersection[i] = true
intersection
# Flood fill through the graph of connected regions. DFS for now - assuming
# there's no path between regions thats bigger than the node stack.
exports.fillRegions = (regions, initialRid, f) ->
expand = (rid, trace) =>
region = regions[rid]
shouldExpand = f rid, trace
return unless shouldExpand
for k,{rid:nextRid,sid,inStates} of region.connections
continue if nextRid in trace.path
#console.log "connection from #{rid} to #{nextRid} when #{sid} in states #{inStates}"
prevStates = trace.shuttleStates[sid]
if !prevStates
trace.shuttleStates[sid] = inStates
else
intersect = intersectListOrNull prevStates, inStates
# Mutually exclusive states - skip.
continue if !intersect
trace.shuttleStates[sid] = intersect
trace.path.push nextRid
expand nextRid, trace
trace.path.pop()
if !prevStates
delete trace.shuttleStates[sid]
else
trace.shuttleStates[sid] = prevStates
expand initialRid, {path:[initialRid], shuttleStates:{}}
exports.gridExtents = (grid) ->
# calculate the extents
top = left = bottom = right = null
for k, v of grid
{x,y} = parseXY k
left = x if left is null || x < left
right = x if right is null || x > right
top = y if top is null || y < top
bottom = y if bottom is null || y > bottom
{top, left, bottom, right}
exports.printCustomGrid = printCustomGrid = ({top, left, bottom, right}, getFn, stream = process.stdout) ->
for y in [top-1..bottom+1]
stream.write ''
for x in [left-1..right+1]
v = getFn(x, y)
stream.write chars[v] || (if v? then ("#{v}")[0] else ';')
stream.write '\n'
exports.printGrid = (extents, grid, stream = process.stdout) ->
printCustomGrid extents, ((x, y) -> grid[[x,y]]), stream
exports.printPoint = (extents, grid, px, py) ->
get = (x, y) -> if x == px and y == py then '%' else grid[[x,y]]
printCustomGrid extents, get
exports.printEdges = ({top, left, bottom, right}, grid, edgeGrid, stream = process.stdout) ->
edgeChar = (x, y, isTop) ->
e = edgeGrid["#{x},#{y},#{isTop}"]
if e?
e % 10
else
' '
for y in [top..bottom+1]
# tops
for x in [left..right]
stream.write " #{edgeChar x, y, true}"
stream.write '\n'
# lefts
if y <= bottom
for x in [left..right+1]
stream.write "#{edgeChar x, y, false}"
if x <= right
stream.write chars[grid[[x, y]]] || ';'
stream.write '\n'
stream.write '\n'
exports.drawRegionGraph = (parserData, filename) ->
{shuttles, regions} = parserData
#console.log '\n\n\n'
g = require('graphviz').graph 'regions'
g.set 'layout', 'fdp'
g.set 'K', '1.1'
drawnRegions = {}
addRegion = (rid) ->
r = regions[rid]
return if drawnRegions[rid]
drawnRegions[rid] = true
color = undefined
label = "r#{rid}"
if r.pressure > 0
label += "(+#{r.pressure})"
color = 'green'
else if r.pressure < 0
label += "(#{r.pressure})"
color = 'red'
node = g.addNode "r#{rid}", {shape:'box', color}
node.set 'label', label
addRegion rid for r,rid in regions when numKeys r.connections
for s,sid in shuttles
node = g.addNode "s#{sid}", {shape:'oval', style:'filled', fillcolor:'plum1'}
node.set 'label', "S#{sid} #{s.type}"
edges = {}
addEdge = ({rid, mx, my}) ->
edge = edges[rid]
return edge if edge
addRegion rid
pressure = (mx||0) + (my||0)
color = if pressure < 0 then 'red' else if pressure > 0 then 'green' else 'black'
edges[rid] = edge = g.addEdge "r#{rid}", "s#{sid}"
edge.set 'color', color
edge.set 'penwidth', 2
edge.set "dir", "forward"
edge
addEdge p for p in s.pushedBy
for state, stateid in s.states
edge = addEdge p for p in state.pushedBy
#edge.set 'penwidth', 1
for r,rid in regions when numKeys r.connections
for k,c of r.connections when c.rid > rid
edge = g.addEdge "r#{rid}", "r#{c.rid}"
allowedStates = []
allowedStates.push stateid for v, stateid in c.inStates when v
stateList = if allowedStates.length <= 3
" (#{allowedStates.join(',')})"
else
"x#{allowedStates.length}"
edge.set 'label', "S#{c.sid}#{stateList}"
#edge = g.addEdge "s#{c.sid}", "r#{rid}"
#edge.set 'dir', 'forward'
#console.log g.to_dot()
g.output filename.split('.')[1], filename
console.log "generated #{filename}"
exports.moveShuttle = (grid, shuttles, sid, from, to) ->
return if from == to
# First remove the shuttle from the grid
s = shuttles[sid]
{dx,dy} = s.states[from]
for k,v of s.points
{x,y} = parseXY k
k = "#{x+dx},#{y+dy}"
throw 'Shuttle not in state' if grid[k] isnt v
grid[k] = 'nothing'
{dx,dy} = s.states[to]
for k,v of s.points
{x,y} = parseXY k
k = "#{x+dx},#{y+dy}"
grid[k] = v