public int FeatureFunction(GameObject pacMan) { float distance = Mathf.Infinity; //Get all the neighboring nodes NodePacMan pacManCurrentNode = pacMan.GetComponent <PacMan>().GetNodeAtPosition(pacMan.transform.position); if (pacManCurrentNode) { NodePacMan[] neighbors = pacManCurrentNode.neighbors; for (int i = 0; i < neighbors.Length; i++) { float shortestDistance = Vector3.Distance(pacManCurrentNode.transform.position, neighbors[i].transform.position); if (shortestDistance < distance) { distance = shortestDistance; } } } if (distance < 7.0f) { return(1); } else { return(0); } }
private Move GetMove() { //Get all the neighboring nodes NodePacMan pacManCurrentNode = pacMan.GetComponent <PacMan>().GetNodeAtPosition(pacMan.transform.position); if (pacManCurrentNode) { NodePacMan[] neighbors = pacManCurrentNode.neighbors; double reward = 0; for (int i = 0; i < neighbors.Length; i++) { int pacManCurrentNodeIndex = nodes.IndexOf(pacManCurrentNode); int neighborIndex = nodes.IndexOf(neighbors[i]); double rewardFromNeighbor = qMatrix[pacManCurrentNodeIndex, neighborIndex]; if (rewardFromNeighbor > reward) { reward = rewardFromNeighbor; bestDirection = pacManCurrentNode.validDirections[i]; } } } return(GetDirection(bestDirection)); }
private void CheckIsInGhostHouse() { if (currentMode == Mode.Consumed) { GameObject tile = GetTileAtPosition(transform.position); if (tile != null) { if (tile.GetComponent <Tile>() != null) { if (tile.transform.GetComponent <Tile>().isGhostHouse) { moveSpeed = normalMoveSpeed; NodePacMan node = GetNodeAtPosition(transform.position); if (node != null) { currentNode = node; direction = Vector2.up; targetNode = currentNode.neighbors[0]; previousNode = currentNode; currentMode = Mode.Chase; UpdateAnimatorController(); } } } } } }
// Use this for initialization void Start() { audioSource = transform.GetComponent <AudioSource>(); NodePacMan node = GetNodeAtPosition(transform.localPosition); startingPosition = node; if (node != null) { currentNode = node; //Debug.Log(currentNode); } direction = Vector2.left; orientation = Vector2.left; ChangePosition(direction); if (GameBoard.isPlayerOneUp) { SetDifficultyForLevel(GameBoard.playerOneLevel); } else { SetDifficultyForLevel(GameBoard.playerTwoLevel); } }
// Use this for initialization void Start() { if (GameBoard.isPlayerOneUp) { SetDifficultyForLevel(GameBoard.playerOneLevel); } else { SetDifficultyForLevel(GameBoard.playerTwoLevel); } backgroundAudio = GameObject.Find("Game").transform.GetComponent <AudioSource>(); pacMan = GameObject.FindGameObjectWithTag("PacMan"); NodePacMan node = GetNodeAtPosition(transform.position); if (node != null) { currentNode = node; } if (isInGhostHouse) { direction = Vector2.up; targetNode = currentNode.neighbors[0]; } else { direction = Vector2.left; targetNode = ChooseNextNode(); } previousNode = currentNode; UpdateAnimatorController(); }
private void Move() { if (targetNode != currentNode && targetNode != null && !isInGhostHouse) { if (OverShotTarget()) { currentNode = targetNode; transform.localPosition = currentNode.transform.position; GameObject otherPortal = GetPortal(currentNode.transform.position); if (otherPortal != null) { transform.localPosition = otherPortal.transform.position; currentNode = otherPortal.GetComponent <NodePacMan>(); } targetNode = ChooseNextNode(); previousNode = currentNode; currentNode = null; UpdateAnimatorController(); } else { transform.localPosition += (Vector3)direction * moveSpeed * Time.deltaTime; } } }
public int FeatureFunction(NodePacMan node) { GameObject[] ghosts = GameObject.FindGameObjectsWithTag("Ghost"); float closestGhostDistance = float.MaxValue; foreach (GameObject ghost in ghosts) { if (ghost.GetComponent <Ghost>().currentMode == Ghost.Mode.Frightened) { float shortestDistance = Vector3.Distance(node.transform.position, ghost.transform.position); if (shortestDistance < closestGhostDistance) { closestGhostDistance = shortestDistance; } } } if (closestGhostDistance < 9.0f) { return(1); } else { return(0); } }
public void Restart() { canMove = true; currentMode = Mode.Scatter; moveSpeed = normalMoveSpeed; previousMoveSpeed = 0; transform.position = startingPosition.transform.position; ghostReleaseTimer = 0; modeChangeIteration = 1; modeChangeTimer = 0; if (transform.name != "Ghost_Blinky") { isInGhostHouse = true; } currentNode = startingPosition; if (isInGhostHouse) { direction = Vector2.up; targetNode = currentNode.neighbors[0]; } else { direction = Vector2.left; targetNode = ChooseNextNode(); } previousNode = currentNode; UpdateAnimatorController(); }
private void MoveToNode(Vector2 dir) { NodePacMan moveToNode = CanMove(dir); if (moveToNode != null) { transform.localPosition = moveToNode.transform.position; currentNode = moveToNode; } }
private void Move() { if (targetNode != currentNode && targetNode != null) { if (nextDirection == direction * -1) { direction *= -1; NodePacMan tempNode = targetNode; targetNode = previousNode; previousNode = tempNode; } if (OverShotTarget()) { currentNode = targetNode; transform.localPosition = currentNode.transform.position; GameObject otherPortal = GetPortal(currentNode.transform.position); if (otherPortal != null) { transform.localPosition = otherPortal.transform.position; currentNode = otherPortal.GetComponent <NodePacMan>(); } NodePacMan moveToNode = CanMove(nextDirection); if (moveToNode != null) { direction = nextDirection; } if (moveToNode == null) { moveToNode = CanMove(direction); } if (moveToNode != null) { targetNode = moveToNode; previousNode = currentNode; currentNode = null; } else { direction = Vector2.zero; } } else { transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime; } } }
public void Restart() { canMove = true; currentNode = startingPosition; nextDirection = Vector2.left; //Chomp animation transform.GetComponent <Animator>().Play("PacManAnimation"); transform.GetComponent <Animator>().enabled = true; ChangePosition(direction); }
private NodePacMan CanMove(Vector2 dir) { NodePacMan moveToNode = null; for (int i = 0; i < currentNode.neighbors.Length; i++) { if (currentNode.validDirections[i] == dir) { moveToNode = currentNode.neighbors[i]; break; } } return(moveToNode); }
private void CalculateQValue(NodePacMan node) { //Assign the new Q value NodePacMan pacManCurrentNode = pacMan.GetComponent <PacMan>().GetNodeAtPosition(pacMan.transform.position); if (pacManCurrentNode) { NodePacMan[] neighbors = pacManCurrentNode.neighbors; for (int i = 0; i < neighbors.Length; i++) { FeatureGoAfterPill goafterPill = new FeatureGoAfterPill(); FeatureGoAfterPowerPill goafterPowerPill = new FeatureGoAfterPowerPill(); FeatureGoAfterEdibleGhost goafterEdibleGhost = new FeatureGoAfterEdibleGhost(); FeatureGhostIsClose ghostIsClose = new FeatureGhostIsClose(); FeatureDangerousDirection dangerousDirection = new FeatureDangerousDirection(); features.Add(goafterPill.FeatureFunction(pacMan)); features.Add(goafterPowerPill.FeatureFunction(pacMan)); features.Add(goafterEdibleGhost.FeatureFunction(neighbors[i])); features.Add(ghostIsClose.FeatureFunction(neighbors[i])); features.Add(dangerousDirection.FeatureFunction(pacManCurrentNode)); weights.Add(goafterPill.weight); weights.Add(goafterPowerPill.weight); weights.Add(goafterEdibleGhost.weight); weights.Add(ghostIsClose.weight); weights.Add(dangerousDirection.weight); double qValue = 0.0; for (int j = 0; j < numberOfFeatures; j++) { qValue += features[j] * weights[j]; } int pacManCurrentNodeIndex = nodes.IndexOf(pacManCurrentNode); int neighborIndex = nodes.IndexOf(neighbors[i]); double newQValue = learningRate * (rMatrix[pacManCurrentNodeIndex, neighborIndex] + discountFactor * qValue); qMatrix[pacManCurrentNodeIndex, neighborIndex] = newQValue; features.Clear(); weights.Clear(); } } }
private void ChangePosition(Vector2 dir) { if (dir != direction) { nextDirection = dir; } if (currentNode != null) { NodePacMan moveToNode = CanMove(dir); if (moveToNode != null) { direction = dir; targetNode = moveToNode; previousNode = currentNode; currentNode = null; } } }
private void Update() { if (!GameBoard.didStartDeath && GameMenu.isAIPlayerGame) { NodePacMan pacManCurrentNode = pacMan.GetComponent <PacMan>().GetNodeAtPosition(pacMan.transform.position); if (pacManCurrentNode != null) { if (Vector3.Distance(pacMan.transform.position, pacManCurrentNode.transform.position) < 0.01f) { fillRewardMatrix(); CalculateQValue(pacMan.GetComponent <PacMan>().GetNodeAtPosition(pacMan.transform.position)); //Get the best move bestMove = GetMove(); } } } }
private void fillRewardMatrix() { for (int i = 0; i < 66; i++) { for (int j = 0; j < 66; j++) { NodePacMan node = nodeDictionary[i]; NodePacMan otherNeighbor = nodeDictionary[j]; if (i == j) { rMatrix[i, j] = 0.0; } else { foreach (NodePacMan neighbour in node.neighbors) { if (neighbour.name == otherNeighbor.name) { if (neighbour.GetComponent <Tile>().isPellet&& !neighbour.GetComponent <Tile>().didConsumePlayerOne) { rMatrix[i, j] = 25.0; } else if (neighbour.GetComponent <Tile>().isPowerPellet&& !neighbour.GetComponent <Tile>().didConsumePlayerOne) { rMatrix[i, j] = 50.0; } else if (neighbour.GetComponent <Tile>().isPortal) { rMatrix[i, j] = 10.0; } else { rMatrix[i, j] = 0.0; } } } } } } }
NodePacMan ChooseNextNode() { Vector2 targetTile = Vector2.zero; if (currentMode == Mode.Chase) { targetTile = GetTargetTile(); } else if (currentMode == Mode.Scatter) { targetTile = homeNode.transform.position; } else if (currentMode == Mode.Frightened) { targetTile = GetRandomTile(); } else if (currentMode == Mode.Consumed) { targetTile = ghostHouse.transform.position; } NodePacMan moveToNode = null; List <NodePacMan> foundNodes = new List <NodePacMan>(); List <Vector2> foundNodesDirection = new List <Vector2>(); for (int i = 0; i < currentNode.neighbors.Length; i++) { if (currentNode.validDirections[i] != direction * -1) { if (currentMode != Mode.Consumed) { GameObject tile = GetTileAtPosition(currentNode.transform.position); if (tile.transform.GetComponent <Tile>().isGhostHouseEntrance) { //Found ghost house, no movement if (currentNode.validDirections[i] != Vector2.down) { foundNodes.Add(currentNode.neighbors[i]); foundNodesDirection.Add(currentNode.validDirections[i]); } } else { foundNodes.Add(currentNode.neighbors[i]); foundNodesDirection.Add(currentNode.validDirections[i]); } } else { foundNodes.Add(currentNode.neighbors[i]); foundNodesDirection.Add(currentNode.validDirections[i]); } } } if (foundNodes.Count == 1) { moveToNode = foundNodes[0]; direction = foundNodesDirection[0]; } if (foundNodes.Count > 1) { float leastDistance = Mathf.Infinity; for (int i = 0; i < foundNodes.Count; i++) { if (foundNodesDirection[i] != Vector2.zero) { float distanceToNeighbor = GetDistance(currentNode.transform.position, foundNodes[i].transform.position); float distance = distanceToNeighbor + GetDistance(foundNodes[i].transform.position, targetTile); if (distance < leastDistance) { leastDistance = distance; moveToNode = foundNodes[i]; direction = foundNodesDirection[i]; } } } } return(moveToNode); }