changeset 66:9cc019c98335

Added chaser ghost AI
author Michael Pavone <pavone@retrodev.com>
date Sun, 27 Jul 2014 22:18:12 -0700
parents 2a5d7308e1df
children 76570158a6e9
files code/ghost0.gq
diffstat 1 files changed, 140 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/code/ghost0.gq	Sun Jul 27 22:18:12 2014 -0700
@@ -0,0 +1,140 @@
+#{
+	up <- 0
+	right <- 1
+	down <- 2
+	left <- 3
+	
+	getDirX <- :dir startX {
+		if: dir = right {
+			startX <- startX + 1
+		} else: {
+			if: dir = left {
+				startX <- startX - 1 
+			} else: {}
+		}
+		startX
+	}
+	
+	getDirY <- :dir startY {
+		if: dir = up {
+			startY <- startY - 1
+		} else: {
+			if: dir = down {
+				startY <- startY + 1 
+			} else: {}
+		}
+		startY
+	}
+	
+	opDir <- :dir {
+		if: dir < 2 {
+			dir <- dir + 2
+		} else: {
+			dir <- dir - 2
+		}
+		dir
+	}
+
+	goTowardsX:Y <- :targetX targetY {
+		myIdx <- (me: )
+		myX <- ghostPos: myIdx
+		myY <- yCoord
+		myVit <- ghostStatus: myIdx
+		myDir <- direction
+		
+		
+		firstChoice <- 0
+		secondChoice <- 0
+		
+		if: myX > targetX {
+			//ghost is to the right of target
+			if: myY > targetY {
+				//ghost is below target
+				if: (myX - targetX) > (myY - targetY) {
+					//target is more left than up
+					firstChoice <- left
+					secondChoice <- up
+				} else: {
+					firstChoice <- up
+					secondChoice <- left
+				}
+			} else: {
+				//ghost is above or directly to the right of target
+				if: (myX - targetX) > (targetY - myY) {
+					//target is more left than down
+					firstChoice <- left
+					secondChoice <- down
+				} else: {
+					//target is more down than left
+					firstChoice <- down
+					secondChoice <- left
+				}
+			}
+		} else: {
+			//ghost is to the left of or directly above/below target
+			if: myY > targetY {
+				//ghost is below target
+				if: (targetX - myX) > (myY - targetY) {
+					//target is more right than up
+					firstChoice <- right
+					secondChoice <- up
+				} else: {
+					firstChoice <- up
+					secondChoice <- right
+				}
+			} else: {
+				//ghost is above or directly to the left of target
+				if: (targetX - myX) > (targetY - myY) {
+					//target is more right than down
+					firstChoice <- right
+					secondChoice <- down
+				} else: {
+					//target is more down than right
+					firstChoice <- down
+					secondChoice <- right
+				}
+			}
+		}
+		if: myVit = 1 {
+			//currently in fright mode, try to run away
+			firstChoice <- opDir: firstChoice
+			secondChoice <- opDir: secondChoice
+		} else: {}
+		
+		
+		
+		tmp <- 0
+		i <- 0
+		while: { i < 2} do: {
+			targetX <- getDirX: firstChoice myX
+			targetY <- getDirY: firstChoice myY
+		
+			if: (mapContentsAt: targetX targetY) - 1 > 4 {
+				//first choice is a wall or ghost start pos
+				tmp <- firstChoice
+				firstChoice <- secondChoice
+				secondChoice <- opDir: firstChoice
+				i <- i + 1
+			} else: {
+				if: firstChoice = (opDir: myDir) {
+					//first choice is backwards
+					tmp <- firstChoice
+					firstChoice <- secondChoice
+					secondChoice <- opDir: firstChoice
+					i <- i + 1
+				} else: {
+					i <- 2
+				}
+			}
+		}
+		direction!: firstChoice
+
+		0
+	}
+
+	//chases lambda man
+	main <- {
+		lambdamanPos:
+		goTowardsX: xCoord Y: yCoord
+	}
+}
\ No newline at end of file