changeset 62:c17380c8bac3

ghost movement compiles. fixed code that relied on if being an expression.
author William Morgan <billjunk@mrgn.org>
date Mon, 28 Jul 2014 01:47:07 -0700
parents 0eff4790249f
children 428c1daefca9
files code/gameState.lm
diffstat 1 files changed, 153 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/code/gameState.lm	Mon Jul 28 01:46:08 2014 -0700
+++ b/code/gameState.lm	Mon Jul 28 01:47:07 2014 -0700
@@ -29,6 +29,7 @@
 		gridMaxY
 		gridArea
 		grid:inBounds?
+		grid:get:withDefault
 		calcPos
 	] from: (module: "grid.lm")
 
@@ -37,14 +38,79 @@
 		(a + b) = 2
 	}
 
-	makeTicker <- :mapGrid {
+	poorMod <- :input mod {
+		ret <- input
+		if: input >= mod {
+			ret <- input - mod
+		} else: {
+			ret input 
+		}
+		ret
+	}
+
+	tupGet <- :cell idx size {
+		ret <- 0
+		if: idx = 0 {
+			ret <- cell value
+		} else: { 
+	        if: size = 1 {
+				ret <- cell tail
+			} else: {  // warning, out-of-bounds idx will return a value!
+				ret <- tupGet: (cell tail) (idx - 1) (size - 1)
+			}
+		}
+		ret
+	}
+
+	tileWall <- 0
+	tileEmpty <- 1
+	tilePill <- 2
+	tilePower <- 3
+	tileFruit <- 4
+	tileLm <- 5
+	tileGhost <- 6
+
+	dirUp <- 0
+	dirRight <- 1
+	dirDown <- 2
+	dirLeft <- 3
+	dirFlipped <- :dir {(dir + 2) poorMod: 4}
+
+	makeTicker <- :mapGrid :ghosts{
 		lives <- 3
-		lambdamanPos <- #[5 5]
+		lambdamanStartPos <- #[5 5] // TODO grab during parse or write {grid: mapGrid find: tileLm}
+		lambdamanStartDir <- dirDown
+		lambdamanPos <- lambdamanStartPos
+		lambdamanDir <- lambdamanStartDir
 		win <- 0
-		pillCount <- 50 
+		pillCount <- 50 // TODO count pills during parse 
 
 		isFood <- :tile { (2 <= tile) myAnd: (tile <= 4) }
-		getLambdamanChoice <- {0}
+		getLambdamanChoice <- {0} // TODO ai hookups
+
+		ghostPopulation <- ghosts value
+		ghostChoosers <- #[{0} {0} {0} {0}] // TODO ai hookups
+		ghostChoice <- :ghostIdx{tupGet: ghostChoosers ghostIdx 4}
+		ghostModeStandard <- 0
+		ghostModeFright <- 1
+		ghostModeinvisble <- 2
+		ghostVitality <- :ghost {ghost value}
+		ghostPos <- :ghost {(ghost tail) value}
+		ghostSetPos <- :ghost pos { #[(ghost ghostVitality) pos (ghost ghostDir)] }
+		ghostDir <- :ghost {(ghost tail) tail}
+		ghostSetDir <- :ghost dir { #[(ghost ghostVitality) (ghost ghostPos) dir] }
+		ghostGetLag <- :ghostType vitality{
+			ticksPerMove <- 130 + 2 * ghostType 
+			frightTicksPerMove <- ticksPerMove + 65 + ghostType 
+			ret <- 0
+			if: vitality = ghostModeFright {
+				ret <- ticksPerMove
+			} else: { 
+				ret <- frightTicksPerMove
+			}
+			ret
+		}
+
 
 		makeEventType <- :lagTick isMovement behavior{
 			print: 12
@@ -56,44 +122,80 @@
 		}
 
 		endOfLives <- makeEventType: (127 * (mapGrid gridArea) * 16) 0 :tick events {
+			777 print
 			lives <- 0
-			777 print
 			events
 		}
 
+		/*
+		TODO the rest of the easy action events
+		frightModeDeactivate <- (127 * 20) 0
+
+		// fruit must be a lambda that can be given a time
+		fruit <- :tick { }
+		fruit1Appears <- makeEventType: (127 * 200) 0 :events {
+			fruit <- :tick{80}
+			fruit1Expires | events
+		}
+		fruit2Appears <- (127 * 400) 0
+		fruit1Expires <- (127 * 280) 0
+		fruit2Expires <- (127 * 480) 0
+		*/
+
 		moveLambdaman <- makeEventType: 127 1 :tick events {
 			print: 20
 			move <- getLambdamanChoice: 
 			newPos <- (calcPos: move lambdamanPos)
-			impending <- grid: mapGrid get: newPos
-			print: 21
-			if: (not: (impending = 0)) {
-				lambdamanPos <- impending
+			newTile <- grid: mapGrid get: newPos withDefault: tileWall
+			if: (not: (newTile = tileWall)) {
+				lambdamanPos <- newPos
 			} else: { }
-
-			print: 22
-			if: (impending isFood) {
-				(moveLambdaman: (tick + 10)) | events
-			} else: { 
-				(moveLambdaman: tick) | events
+			
+			ret <- 0
+			if: (newTile isFood) {
+				ret <- (moveLambdaman: (tick + 10)) | events
+			} else: {
+				ret <-(moveLambdaman: tick) | events
 			}
+			ret
 		}
 
-		/*
-		fruit <- 0
-		fruit1Appears <- makeEventType: (127 * 200) 0 :events {
-			fruit <- 80
-			fruit1Expires | events
+		moveGhost <- :lagTick ghostIdx ghostType curTick {
+			print: 21
+			expireTick <- curTick + lagTick
+			#[expireTick 1 :events {
+				nextExpire <- 0
+				tree: ghosts update: ghostIdx with: :ghost {
+					dir <- ghostDir: ghost
+					legalChoice <- dir
+					choice <- ghostChoice: ghostIdx
+					if: (not: (choice = (dir dirFlipped))) {
+						legalChoice <- choice
+					} else: { }
+
+					attempts <- [legalChoice dirUp dirDown dirLeft dirRight]
+					newDir <- 0
+					newPos <- ghost ghostPos
+					newTile <- 0
+					while: {not: (attempts empty?:)} do:{
+						newDir <- attempts value
+						newPos <- (calcPos: newDir (ghost ghostPos))
+						newTile <- grid: mapGrid get: newPos withDefault: tileWall
+						if: (not: (newTile = tileWall)) {
+							ghost ghostSetPos: newPos
+							ghost ghostSetDir: newDir
+							attempts <- []
+						} else: {
+							attempts <- attempts tail
+						}
+					}
+					
+					nextExpire <- ghostGetLag: ghostType (ghost ghostVitality)
+					ghost
+				}
+				(moveGhost: nextExpire ghostIdx ghostType expireTick) | events
+			}]
 		}
-		fruit2Appears <- (127 * 400) 0
-		fruit1Expires <- (127 * 280) 0
-		fruit2Expires <- (127 * 480) 0
-		moveGhost0 <- 1 //(ghostType, ghostId)!?
-		moveGhost1 <- 1 //(ghostType, ghostId)!?
-		moveGhost2 <- 1 //(ghostType, ghostId)!?
-		moveGhost3 <- 1 //(ghostType, ghostId)!?
-		frightModeDeactivate <- (127 * 20) 0
-		*/
 
 		executeEvents <- :tick isMove events {
 			print: #[23 tick isMove events]
@@ -121,8 +223,6 @@
 		}
 
 		executeTick <- :tick events {
-			// TODO: update fruit countdown for AI input
-
 			print: 14
 			// 1.) movement
 			events <- executeEvents: tick 1 events
@@ -132,19 +232,23 @@
 			events <- executeEvents: tick 0 events
 			
 			print: 16
-			// 3.) collide pills powerpills fruit
+			// 3.) TODO collide pills powerpills fruit
+			
 
+			// 4.) TODO ghost collision
+
+			ret <- 0
 			if: pillCount = 0 {
 				win <- 1
-				[]
+				ret <- []
 			} else: {
 				if: lives = 0 {
-					[]
+					ret <- []
 				} else: {
-					events
+					ret <- events
 				}
 			}
-
+			ret
 		}
 	
 		nextTick <- :events {
@@ -155,7 +259,7 @@
 				print: 10
 				if: eventTick < curMin {
 					curMin <- eventTick
-				} else: {}
+				} else: { }
 				curMin
 			}
 		}
@@ -167,6 +271,15 @@
 				endOfLives: 0
 				moveLambdaman: 0
 			]
+			ghostIdx <- 0
+			ghostType <- 0
+			ghostTick <- 0
+			while: {ghostIdx < ghostPopulation} do: {
+				ghostTick <- ghostGetLag: ghostType ghostModeStandard
+				events <- (moveGhost: 0 ghostIdx ghostType ghostTick) | events
+				ghostIdx <- ghostIdx + 1
+				ghostType <- (ghostType + 1) poorMod: 4
+			}
 			print: 5
 			while: {(tick < runUntil) myAnd: (not: (events empty?))} do: {
 				print: 6
@@ -183,6 +296,8 @@
 			makeTree: row 
 		})
 
+		world
+
 		ticker <- makeTicker: grid
 		print: 2
 		ticker: 1000
@@ -190,6 +305,7 @@
 
 		#[0 0]
 	}
+
 /*
 	main <- :initWorld ghostCode{
 		#[0 step]