view code/ghost2.gq @ 73:91e7a30a9e27

Unsuccessful attempt at improving ghost performance with a wingman ghost
author Michael Pavone <pavone@retrodev.com>
date Mon, 28 Jul 2014 00:42:45 -0700
parents
children 10a75a37c0fa
line wrap: on
line source

#{
	up <- 0
	right <- 1
	down <- 2
	left <- 3
	myIdx <- (me: )
	lastX <- 0
	lastY <- 0
	myX <- 0
	myY <- 0
	
	getDirX <- :dir startX {
		if: dir = right {
			startX <- startX + 1
		} else: {
			if: dir = left {
				startX <- startX - 1 
			}
		}
		startX
	}
	
	getDirY <- :dir startY {
		if: dir = up {
			startY <- startY - 1
		} else: {
			if: dir = down {
				startY <- startY + 1 
			}
		}
		startY
	}
	
	/*
	opDir <- :dir {
		(dir + 2) and: 3
	}
	*/

	goTowardsX:Y <- :targetX targetY {
		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 <- (firstChoice + 2) and: 3 //opDir: firstChoice
			secondChoice <- (secondChoice + 2) and: 3 //opDir: secondChoice
		}
		
		
		
		tmp <- 0
		i <- 0
		fail <- 0
		while: { i < 3} 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 <- (firstChoice + 2) and: 3 //opDir: firstChoice
				i <- i + 1
			} else: {
				if: firstChoice = ( (myDir + 2) and: 3 /*opDir: myDir*/) {
					//first choice is backwards
					tmp <- firstChoice
					firstChoice <- secondChoice
					secondChoice <- (firstChoice + 2) and: 3 //opDir: firstChoice
					i <- i + 1
				} else: {
					i <- 3
				}
			}
		}
		direction!: firstChoice

		0
	}

	//"wingman" - tries to surround lambda man
	//position two cells ahead of his current position
	main <- {
		lmX <- lambdamanPos:
		lmY <- yCoord
		myX <- ghostPos: myIdx
		myY <- yCoord
		diffX <- 0
		diffY <- 0
		
		if: notFirst? = 1 {
			diffX <- lmX - lastX
			diffY <- lmY - lastY
		}
		lastX <- lmX
		lastY <- lmY
		
		distX <- myX - lmX
		if: distX < 0 {
			distX <- 0 - distX
		}
		distY <- myY - lmY
		if: distY < 0 {
			distY <- 0 - distX
		}
		dist <- distX + distY
		if: dist < 8 {
			diffX <- 0
			diffY <- 0	
		}
		
		lmX <- (lmX + diffY) + diffY
		lmY <- (lmY + diffX) + diffX
		
		goTowardsX: lmX Y: lmY
		
	}
}