comparison src/sim.tp @ 39:9bccdb3ac979

Add priority queue implementation to lifter. Add methods for cloning playfield and determining valid moves.
author Mike Pavone <pavone@retrodev.com>
date Sun, 15 Jul 2012 14:27:21 -0700
parents 814e3d482ad3
children 20327ae2120b
comparison
equal deleted inserted replaced
38:cd3ec7d99330 39:9bccdb3ac979
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 ret <- #{ 123 ret <- #{
120 grid <- in_grid 124 grid <- in_grid
121 width <- in_width 125 width <- in_width
122 height <- in_height 126 height <- in_height
127 grid set: (calcIndex: x y) val 131 grid set: (calcIndex: x y) val
128 } 132 }
129 getCell <- :x y { 133 getCell <- :x y {
130 grid get: (calcIndex: x y) 134 grid get: (calcIndex: x y)
131 } 135 }
136 validDest?:from <- :index :fromIndex {
137 cell <- (grid get: index)
138 if: (cell navigable) {true} else: {
139 if: (cell eq: (cellTypes rock)) {
140 diff <- index - fromIndex
141 //make sure movement was horizontal
142 if: (abs: diff) = 1 {
143 rockdest <- index + diff
144 if: ((grid get: rockdest) eq: (cellTypes empty)) {
145 //make sure rock destination doesn't wrap
146 (calcY: rockdest) = (calcY: index)
147 }
148 }
149 }
150 }
151 }
152 validMoves <- :x y {
153
154 amove <- :idx name {#{
155 index <- idx
156 cmd <- name
157 string <- {
158 name . "(" . idx . ")"
159 }
160 }}
161 here <- calcIndex: x y
162 //TODO: Add wait move when rocks are in motion
163 //(amove: here "W")
164 cur <- #[(amove: here "A")]
165 up <- amove: (calcIndex: x y + 1) "U"
166 down <- amove: (calcIndex: x y - 1) "D"
167 left <- amove: (calcIndex: x - 1 y) "L"
168 right <- amove: (calcIndex: x + 1 y) "R"
169 foreach: #[up down left right] :idx el {
170 if: (validDest?: (el index) from: here) {
171 cur append: el
172 }
173 }
174 cur
175 }
176 getRobot <- { _robot }
132 updatePos <- :obj Index { 177 updatePos <- :obj Index {
133 obj x!: (calcX: Index) 178 obj x!: (calcX: Index)
134 obj y!: (calcY: Index) 179 obj y!: (calcY: Index)
135 } 180 }
136 water <- 0 181 water <- 0
143 true 188 true
144 } 189 }
145 advance <- :roboCmd { 190 advance <- :roboCmd {
146 endreached <- roboCmd = "A" 191 endreached <- roboCmd = "A"
147 if: (not: endreached) { 192 if: (not: endreached) {
148 robot doCmd: roboCmd 193 _robot doCmd: roboCmd
149 moves <- moves + 1 194 moves <- moves + 1
150 doUpdate: 195 doUpdate:
151 } 196 }
152 self 197 self
153 } 198 }
163 cur <- cur - (width + width) 208 cur <- cur - (width + width)
164 os write: 2 "\n" 209 os write: 2 "\n"
165 } 210 }
166 } 211 }
167 } 212 }
213 clone <- {
214 cgrid <- #[]
215 foreach: grid :idx el {
216 if: (el isrobot) {
217 cgrid append: (cellTypes robot)
218 } else: {
219 cgrid append: el
220 }
221 }
222 myclone <- state new: cgrid width height
223 myclone water!: water
224 myclone flooding!: flooding
225 myclone waterproof!: waterproof
226 myclone moves!: moves
227 myclone
228 }
168 } 229 }
169 foreach: in_grid :index el{ 230 foreach: in_grid :index el{
170 nextGrid append: el 231 nextGrid append: el
171 if: (el isrobot) { 232 if: (el isrobot) {
172 robot <- el 233 _robot <- el
173 robot mine!: ret 234 _robot mine!: ret
174 ret updatePos: robot index 235 ret updatePos: _robot index
175 } 236 }
176 237
177 238
178 // adding a 'new' method to robot and doing this instead 239 // adding a 'new' method to robot and doing this instead
179 // wolud allow me to treat robots and other cellTypes equaly 240 // wolud allow me to treat robots and other cellTypes equaly