diff 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
line wrap: on
line diff
--- a/src/sim.tp	Sun Jul 15 14:25:05 2012 -0700
+++ b/src/sim.tp	Sun Jul 15 14:27:21 2012 -0700
@@ -14,6 +14,10 @@
 	debugLog <- :str {
 		os write: 2 str
 	}
+	
+	abs <- :val {
+		if: val < 0 { 0 - val } else: { val }
+	}
 
 	makeCellTypes <- {
 		typedict <- dict linear
@@ -114,7 +118,7 @@
 	state <- #{
 		new <- :in_grid in_width in_height { 
 			nextGrid <- #[]
-			robot <- null
+			_robot <- null
 			endreached <- false
 			ret <- #{
 				grid <- in_grid
@@ -129,6 +133,47 @@
 				getCell <- :x y {
 					grid get: (calcIndex: x y)
 				}
+				validDest?:from <- :index :fromIndex {
+					cell <- (grid get: index)
+					if: (cell navigable) {true} else: {
+						if: (cell eq: (cellTypes rock)) {
+							diff <- index - fromIndex
+							//make sure movement was horizontal
+							if: (abs: diff) = 1 {
+								rockdest <- index + diff
+								if: ((grid get: rockdest) eq: (cellTypes empty)) {
+									//make sure rock destination doesn't wrap
+									(calcY: rockdest) = (calcY: index)
+								}
+							}
+						}
+					}
+				}
+				validMoves <- :x y {
+					
+					amove <- :idx name {#{
+						index <- idx
+						cmd <- name
+						string <- {
+							name . "(" . idx . ")"
+						} 
+					}}
+					here <- calcIndex: x y
+					//TODO: Add wait move when rocks are in motion
+					//(amove: here "W") 
+					cur <- #[(amove: here "A")]
+					up <- amove: (calcIndex: x y + 1) "U"
+					down <- amove: (calcIndex: x y - 1) "D"
+					left <- amove: (calcIndex: x - 1 y) "L"
+					right <- amove: (calcIndex: x + 1 y) "R"
+					foreach: #[up down left right] :idx el {
+						if: (validDest?: (el index) from: here) {
+							cur append: el
+						}
+					}
+					cur
+				}
+				getRobot <- { _robot }
 				updatePos <- :obj Index {
 					obj x!: (calcX: Index)
 					obj y!: (calcY: Index)
@@ -145,7 +190,7 @@
 				advance <- :roboCmd {
 					endreached <- roboCmd = "A"
 					if: (not: endreached) {
-						robot doCmd: roboCmd
+						_robot doCmd: roboCmd
 						moves <- moves + 1
 						doUpdate:
 					}
@@ -165,13 +210,29 @@
 						}
 					}
 				}
+				clone <- {
+					cgrid <- #[]
+					foreach: grid :idx el {
+						if: (el isrobot) {
+							cgrid append: (cellTypes robot)
+						} else: {
+							cgrid append: el
+						}
+					}
+					myclone <- state new: cgrid width height
+					myclone water!: water
+					myclone flooding!: flooding
+					myclone waterproof!: waterproof
+					myclone moves!: moves
+					myclone
+				}
 			}
 			foreach: in_grid :index el{
 				nextGrid append: el
 				if: (el isrobot) {
-					robot <- el
-					robot mine!: ret
-					ret updatePos: robot index
+					_robot <- el
+					_robot mine!: ret
+					ret updatePos: _robot index
 				}