Mercurial > repos > icfp2012
diff 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 |
line wrap: on
line diff
--- a/src/sim.tp Sun Jul 15 14:35:56 2012 -0700 +++ b/src/sim.tp Sun Jul 15 14:41:28 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 _lambdaCount <- 0 _succeeded <- false @@ -131,6 +135,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) @@ -158,7 +203,7 @@ } } else: { if: (value eq: (cellTypes closedLift)) { - if: (robot collected) = _lambdaCount { + if: (_robot collected) = _lambdaCount { grid set: index (cellTypes openLift) } } @@ -168,7 +213,7 @@ advance <- :roboCmd { endreached <- roboCmd = "A" if: (not: endreached) { - robot doCmd: roboCmd + _robot doCmd: roboCmd moves <- moves + 1 doUpdate: } @@ -188,13 +233,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 } else: { if: (el eq: (cellTypes lambda)) { _lambdaCount <- _lambdaCount + 1