comparison src/sim.tp @ 41:20327ae2120b

merged.
author William Morgan <bill@mrgn.org>
date Sun, 15 Jul 2012 14:41:28 -0700
parents f38437d22ebd 9bccdb3ac979
children 21b14768ea00 0c09730c173e
comparison
equal deleted inserted replaced
40:f38437d22ebd 41:20327ae2120b
11 } 11 }
12 } 12 }
13 13
14 debugLog <- :str { 14 debugLog <- :str {
15 os write: 2 str 15 os write: 2 str
16 }
17
18 abs <- :val {
19 if: val < 0 { 0 - val } else: { val }
16 } 20 }
17 21
18 makeCellTypes <- { 22 makeCellTypes <- {
19 typedict <- dict linear 23 typedict <- dict linear
20 new <- :idStr { 24 new <- :idStr {
112 cellTypes <- makeCellTypes: 116 cellTypes <- makeCellTypes:
113 117
114 state <- #{ 118 state <- #{
115 new <- :in_grid in_width in_height { 119 new <- :in_grid in_width in_height {
116 nextGrid <- #[] 120 nextGrid <- #[]
117 robot <- null 121 _robot <- null
118 endreached <- false 122 endreached <- false
119 _lambdaCount <- 0 123 _lambdaCount <- 0
120 _succeeded <- false 124 _succeeded <- false
121 ret <- #{ 125 ret <- #{
122 grid <- in_grid 126 grid <- in_grid
129 grid set: (calcIndex: x y) val 133 grid set: (calcIndex: x y) val
130 } 134 }
131 getCell <- :x y { 135 getCell <- :x y {
132 grid get: (calcIndex: x y) 136 grid get: (calcIndex: x y)
133 } 137 }
138 validDest?:from <- :index :fromIndex {
139 cell <- (grid get: index)
140 if: (cell navigable) {true} else: {
141 if: (cell eq: (cellTypes rock)) {
142 diff <- index - fromIndex
143 //make sure movement was horizontal
144 if: (abs: diff) = 1 {
145 rockdest <- index + diff
146 if: ((grid get: rockdest) eq: (cellTypes empty)) {
147 //make sure rock destination doesn't wrap
148 (calcY: rockdest) = (calcY: index)
149 }
150 }
151 }
152 }
153 }
154 validMoves <- :x y {
155
156 amove <- :idx name {#{
157 index <- idx
158 cmd <- name
159 string <- {
160 name . "(" . idx . ")"
161 }
162 }}
163 here <- calcIndex: x y
164 //TODO: Add wait move when rocks are in motion
165 //(amove: here "W")
166 cur <- #[(amove: here "A")]
167 up <- amove: (calcIndex: x y + 1) "U"
168 down <- amove: (calcIndex: x y - 1) "D"
169 left <- amove: (calcIndex: x - 1 y) "L"
170 right <- amove: (calcIndex: x + 1 y) "R"
171 foreach: #[up down left right] :idx el {
172 if: (validDest?: (el index) from: here) {
173 cur append: el
174 }
175 }
176 cur
177 }
178 getRobot <- { _robot }
134 updatePos <- :obj Index { 179 updatePos <- :obj Index {
135 obj x!: (calcX: Index) 180 obj x!: (calcX: Index)
136 obj y!: (calcY: Index) 181 obj y!: (calcY: Index)
137 } 182 }
138 lambdaCount <- {_lambdaCount} 183 lambdaCount <- {_lambdaCount}
156 setCell: x y (cellTypes empty) 201 setCell: x y (cellTypes empty)
157 setCell: x (y - 1) value 202 setCell: x (y - 1) value
158 } 203 }
159 } else: { 204 } else: {
160 if: (value eq: (cellTypes closedLift)) { 205 if: (value eq: (cellTypes closedLift)) {
161 if: (robot collected) = _lambdaCount { 206 if: (_robot collected) = _lambdaCount {
162 grid set: index (cellTypes openLift) 207 grid set: index (cellTypes openLift)
163 } 208 }
164 } 209 }
165 } 210 }
166 } 211 }
167 } 212 }
168 advance <- :roboCmd { 213 advance <- :roboCmd {
169 endreached <- roboCmd = "A" 214 endreached <- roboCmd = "A"
170 if: (not: endreached) { 215 if: (not: endreached) {
171 robot doCmd: roboCmd 216 _robot doCmd: roboCmd
172 moves <- moves + 1 217 moves <- moves + 1
173 doUpdate: 218 doUpdate:
174 } 219 }
175 self 220 self
176 } 221 }
186 cur <- cur - (width + width) 231 cur <- cur - (width + width)
187 os write: 2 "\n" 232 os write: 2 "\n"
188 } 233 }
189 } 234 }
190 } 235 }
236 clone <- {
237 cgrid <- #[]
238 foreach: grid :idx el {
239 if: (el isrobot) {
240 cgrid append: (cellTypes robot)
241 } else: {
242 cgrid append: el
243 }
244 }
245 myclone <- state new: cgrid width height
246 myclone water!: water
247 myclone flooding!: flooding
248 myclone waterproof!: waterproof
249 myclone moves!: moves
250 myclone
251 }
191 } 252 }
192 foreach: in_grid :index el{ 253 foreach: in_grid :index el{
193 nextGrid append: el 254 nextGrid append: el
194 if: (el isrobot) { 255 if: (el isrobot) {
195 robot <- el 256 _robot <- el
196 robot mine!: ret 257 _robot mine!: ret
197 ret updatePos: robot index 258 ret updatePos: _robot index
198 } else: { 259 } else: {
199 if: (el eq: (cellTypes lambda)) { 260 if: (el eq: (cellTypes lambda)) {
200 _lambdaCount <- _lambdaCount + 1 261 _lambdaCount <- _lambdaCount + 1
201 } 262 }
202 } 263 }