diff src/sim.tp @ 56:ca86c88c2336

merge
author William Morgan <bill@mrgn.org>
date Sun, 15 Jul 2012 22:24:35 -0700
parents b2e9e5ad3ad8 a37ceb0a4f5c
children aa822c683e28
line wrap: on
line diff
--- a/src/sim.tp	Sun Jul 15 22:06:19 2012 -0700
+++ b/src/sim.tp	Sun Jul 15 22:24:35 2012 -0700
@@ -59,6 +59,7 @@
 					x <- 0
 					y <- 0
 					isrobot <- { true }
+					navigable <- { false }
 					eq <- :other { id = (other id) }
 					collected <- 0
 					heldBreath <- 0
@@ -129,6 +130,9 @@
 			_nextGrid <- #[]
 			_robot <- null
 			_ended <- false
+			_maxmoves <- in_width * in_height
+			_heuristicValid <- false
+			_heuristic <- 0
 			getSafe <- :collection :index {
 				if: index >= 0 {
 					if: index < (collection length) {
@@ -196,6 +200,35 @@
 					}
 					cur
 				}
+				distanceFrom:to <- :x y celltype {
+					//print: "calculating distance from " . x . ", " . y . " to " . celltype . "\n"
+					moves <- validMoves: x y
+					curdist <- 0
+					visited <- _nextGrid
+					foreach: grid :idx el {
+						visited set: idx false
+					}
+					notfound <- true
+					while: { if: notfound { (moves length) > 0 } } do: {
+						nextmoves <- #[]
+						curdist <- curdist + 1
+						foreach: moves :idx move {
+							curpos <- move index
+							if: (not: (visited get: curpos)) {
+								if: ((grid get: curpos) eq: celltype) {
+									notfound <- false
+								} else: {
+									visited set: curpos true
+									foreach: (validMoves: (calcX: curpos) (calcY: curpos)) :idx move {
+										nextmoves append: move
+									}
+								}
+							}
+						}
+						moves <- nextmoves
+					}
+					curdist
+				}
 				getRobot <- { _robot }
 				updatePos <- :obj Index {
 					obj x!: (calcX: Index)
@@ -208,6 +241,18 @@
 				moves <- #[]
 				score <- 0
 				maxScore <- { score + (lambdaCount - (_robot collected)) * 25 + lambdaCount * 50 }
+				heuristic <- {
+					if: (not: _heuristicValid) {
+						dest <- if: (_robot collected) = lambdaCount {
+							cellTypes openLift
+						} else: {
+							cellTypes lambda
+						}
+						_heuristic <- score - (distanceFrom: (_robot x) (_robot y) to: dest)
+						_heuristicValid <- true
+					}
+					_heuristic
+				}
 				addPoints <- :points { score <- score + points }
 				ended <- {_ended}
 				succeeded <- {_succeeded}
@@ -266,11 +311,15 @@
 					grid <- _nextGrid
 					_nextGrid <- tmp
 				}
+				abort <- {
+					_ended <- true
+					addPoints: (_robot collected) * 25
+				}
 				advance <- :roboCmd {
+					_heuristicValid <- false
 					if: roboCmd = "A" {
-						_ended <- true
 						moves append: roboCmd
-						addPoints: (_robot collected) * 25
+						abort
 					}
 					
 					if: (not: _ended) {
@@ -280,6 +329,9 @@
 						doUpdate:
 						checkForDeath:
 						swapGrids:
+						if: (moves length) >= _maxmoves {
+							abort
+						}
 					}
 					self
 				}