<commit_before>AggressiveMode.prototype = new Mode();
AggressiveMode.prototype.constructor = AggressiveMode;

// if an enemy unit is in sight, attack the enemy.
// Move towards the enemy position
// Build units to reinforce the army
function AggressiveMode(battle) {
  this.battle = battle;
  this.enemyHQPos = this._getEnemyHQPos();
  this.usedPositionsThisTurn = [];
  this.currentSelectedUnit = null;
  this.buildPhase = false;
};

// AggressiveMode.prototype.execute = function() {
//   var nextMode = "aggressive";
//   this._moveEachUnit();
//   this._buildPhase();
//   return nextMode;
// };

// AggressiveMode.prototype._moveEachUnit = function() {
//   var that = this;
//   // this.battle.players[1].army.units.forEach(function(unit) {
//   //   that._moveUnit(unit);
//   // });
//   // var unit = this.battle.players[1].army.units[0];
//   // this._moveUnit(unit);
//   // unit = this.battle.players[1].army.units[0];
//   // this._moveUnit(unit);
//   var units = this.battle.players[1].army.units;
//   this._moveAllUnits(units);
//   sleep(500);
//   window.setTimeout(this._endTurn, 1500);
// };


// AggressiveMode.prototype._moveAllUnits = function(unitsArray) {
//   // Recursive Move function
//   // Base Case
//   if (unitsArray.length === 0) {
//     return;
//   } else {
//     this._moveUnit(unitsArray.pop(), function() {
//       this._moveAllUnits(unitsArray);
//     });
//   };
// }

// AggressiveMode.prototype._moveUnit = function(unit, callback) {
//   // var mousePos = unit.pos
//   // this.battle.currentSelectedTile = battle.map.getTileAtPos(mousePos);
//   this.battle.currentSelectedUnit = unit;
//   var possibleMoves = unit.getPossibleMoves(unit.pos, this.battle.map, this.battle.enemyPositions())
//   possibleMoves = this._filterPossibleMoves(possibleMoves)
//   this.battle.currentSelectedMovement = possibleMoves;
//   this.battle.turnState = "selectingMove";
//   this.battle.renderMoveHighlights();
//   // // mousePos = this._getClosestMove(possibleMoves);
//   var squareToMoveTo = this._getClosestMove(possibleMoves);
//   // this.usedPositionsThisTurn.push(squareToMoveTo);
//   this.battle.turnState = "animatingMovement";
//   unit.walkPath = squareToMoveTo.getPath();
// };

// AggressiveMode.prototype._keepAnimatingUnit = function(unit) {
//   if (unit.walkPath.length === 0) {
//     return false;
//   } else {
//     return true;
//   }
// };

AggressiveMode.prototype._endTurn = function() {
  battle.players[1].endTurn();
  battle.currentPlayer = 1;
  var prevGold = battle.getCurrentPlayer().gold;
  battle.addGold();
  if (prevGold !== battle.getCurrentPlayer().gold) {
      game.add.audio('coin').play();
  };
  battle.getCurrentPlayer().onTurnStart(battle.map, battle.currentPlayer);
  var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
  var text = game.add.text(game.world.centerX , game.world.centerY - 300, "Player 1 Turn", style);
  text.anchor.set(0.5);
  text.alpha = 1;
  var tween = game.add.tween(text).to( { alpha: 0 }, 2000, "Linear", true);
  battle.turn ++;
  turnCount.setText("Turn: " + battle.turn);
  currentPlayerText.setText("Player " + this.battle.currentPlayer);
  currentPlayerGold.setText("Gold: " + this.battle.players[battle.currentPlayer - 1].gold);
};

AggressiveMode.prototype._getEnemyHQPos = function() {
  pos = this.battle.players[0].hqPos;
  return pos;
};

AggressiveMode.prototype._getDistanceBetween = function(pos1, pos2) {
  // Currently using pythagorean theorem to find distance, but can use some distance function which takes account terrain.
  return Math.sqrt(Math.pow(Math.abs(pos1.x - pos2.x), 2) + Math.pow(Math.abs(pos1.y - pos2.y), 2));
};

AggressiveMode.prototype.arrayIncludesPosition = function(array, pos) {
  var included = false;
  array.forEach(function(item) {
    if(pos.equals(item)) {
      included = true;
      return;
    }
  });
  return included;
}

AggressiveMode.prototype.handleComputerMove = function() {
  // Returns mousePos to position computer desires to click.
  // Sets battle.computerCanClick to true once a selection is made.
  var mousePos;
  if (this.buildPhase === true) {
    if (this.battle.turnState !== "buildUnit") {
      mousePos = this._runBuildPhase();
    }
    else if (this.battle.turnState === "buildUnit") {

    }
    // move to ._runBuildPhase;
    this.buildPhase = false;
    this._endTurn();
  }
  else if(this.battle.turnState === "selectingUnit") {
    mousePos = this._selectNextUnit();
  }
  else if(this.battle.turnState === "selectingMove") {
    mousePos = this._selectNextMove();
  }
  else if(this.battle.turnState === "selectingAttack") {
    mousePos = this._selectNextAttack();
  }
  else if (this.battle.turnState === "capturePrompt") {
    mousePos = this._selectNextCapture();
  } 
  else {
    mousePos = new Pos (1,1);
  }
  return mousePos;
};

AggressiveMode.prototype._selectNextUnit = function() {
  var nextUnitPos = null;
  var that = this;
  this.battle.players[1].army.units.forEach(function(unit) {
    if (!nextUnitPos && !unit.movedThisTurn) {
      that.currentSelectedUnit = unit;
      nextUnitPos = unit.pos;
    };
  });
  // Logic for when to end turn
  if (nextUnitPos === null) {
    this.buildPhase = true;
  }
  return nextUnitPos
}

AggressiveMode.prototype._selectNextMove = function() {
  var that = this;
  var nextMovePos = null;
  var possibleMoves = this.currentSelectedUnit.getPossibleMoves(this.currentSelectedUnit.pos, this.battle.map, this.battle.enemyPositions());
  var filteredPossibleMoves = this._filterPossibleMoves(possibleMoves);
  if (this.currentSelectedUnit instanceof UnitArtillery) {
    nextMovePos = that._handleArtilleryMovement(filteredPossibleMoves);
  } else {
    // nextMovePos = this._getClosestMoveToEnemyHQ(filteredPossibleMoves);
    // nextMovePos = this._getClosestMoveToEnemyUnit(filteredPossibleMoves);
    nextMovePos = this._randomizeUnitMovement(filteredPossibleMoves);
  }
  return nextMovePos;
}

AggressiveMode.prototype._selectNextAttack = function() {
  var nextAttackPos = null;
  var possibleAttackMoves = this.currentSelectedUnit.getPossibleAttacks(this.battle.map);
  var filteredAttackMoves = this._filterPossibleAttackMoves(possibleAttackMoves);
  return filteredAttackMoves[0];
}

AggressiveMode.prototype._selectNextCapture = function() {

};


// Battle.prototype.clickOnBarracks = function(mousePos) {
//   if (this.currentSelectedTile) {
//     if (this.currentSelectedTile.name === "barracks" && this.currentSelectedTile.owner === this.currentPlayer) {
//       this.turnState = "buildUnit";
//       this.buildScreen = new BuildScreen(this.getCurrentPlayer().army.armyList, this, mousePos);
//     }
//   }
// }

AggressiveMode.prototype._runBuildPhase = function() {
  // fix so won't reset
    var unusedBarracks = this._getAllBarracks();
    this._selectNextBarracks(unusedBarracks.pop());
}

AggressiveMode.prototype._selectNextBarracks = function(barracks) {
  var mousePos = this._getBarracksPosition(barracks)
  this.battle.currentSelectedTile = barracks[0];
  this.battle.clickOnBarracks(mousePos);
  
};

AggressiveMode.prototype._getAllBarracks = function() {
  var barracks = [];
  this.battle.map.getAllBuildings(true).forEach(function(buildingArray) {
    console.log(buildingArray[0].name === "barracks" && buildingArray[0].owner === this.battle.currentPlayer);
    if (buildingArray[0].name === "barracks" && parseInt(buildingArray[0].owner) === this.battle.currentPlayer) {
      barracks.push(buildingArray);
    }
  });
  return barracks;
};

AggressiveMode.prototype._getBarracksPosition = function(barracks) {
  var barracksTileCoordinates = [];
  barracksTileCoordinates.push(barracks[1]);
  barracksTileCoordinates.push(barracks[2]);
  return barracksTileCoordinates;
};

AggressiveMode.prototype._chooseUnit = function() {
  var armyList = this.battle.getCurrentPlayer().army.armyList;
  var chosenUnit = this._evaluateUnitsCost;
  return chosenUnit;
}

AggressiveMode.prototype._evaluateUnitsCost = function (armyList) {
  var unitsToBuy = {};
  armyList.forEach(function(unitClass, index) {
    unit = new unitClass(new Pos(5, 3 + index), this.battle.currentPlayer);
    unitsToBuy.unitClass = unit.cost;
  });
  var mostExpensiveUnit = this._findMostExpensiveUnit();
}

AggressiveMode.prototype._findMostExpensiveUnit = function(unitsToBuy) {
  var mostExpensiveUnit = 0;
  for (var unit in unitsToBuy) {
    if (unitsToBuy.unit > mostExpensiveUnit) {
      mostExpensiveUnit = unitsToBuy.unit;
    }
  }
  return mostExpensiveUnit;
}

AggressiveMode.prototype._findUnitMousePos = function(unit) {
  
}

AggressiveMode.prototype._filterPossibleMoves = function(possibleMoves) {
  // Takes in list of possible moves and filters out positions in which friendly units already exist
  // for each possible move
    // Call unitAtPos and check if null
  var that = this;
  result = [];
  possibleMoves.forEach(function(movePos) {
    var unitAtPos = that.battle.getUnitAtPos(movePos);
    if (!unitAtPos) {
      result.push(movePos);<commit_msg>Add click handler logic for AI build units<commit_after>AggressiveMode.prototype = new Mode();
AggressiveMode.prototype.constructor = AggressiveMode;

// if an enemy unit is in sight, attack the enemy.
// Move towards the enemy position
// Build units to reinforce the army
function AggressiveMode(battle) {
  this.battle = battle;
  this.enemyHQPos = this._getEnemyHQPos();
  this.usedPositionsThisTurn = [];
  this.currentSelectedUnit = null;
  this.buildPhase = false;
  this.visitedBarracks = [];
};

// AggressiveMode.prototype.execute = function() {
//   var nextMode = "aggressive";
//   this._moveEachUnit();
//   this._buildPhase();
//   return nextMode;
// };

// AggressiveMode.prototype._moveEachUnit = function() {
//   var that = this;
//   // this.battle.players[1].army.units.forEach(function(unit) {
//   //   that._moveUnit(unit);
//   // });
//   // var unit = this.battle.players[1].army.units[0];
//   // this._moveUnit(unit);
//   // unit = this.battle.players[1].army.units[0];
//   // this._moveUnit(unit);
//   var units = this.battle.players[1].army.units;
//   this._moveAllUnits(units);
//   sleep(500);
//   window.setTimeout(this._endTurn, 1500);
// };


// AggressiveMode.prototype._moveAllUnits = function(unitsArray) {
//   // Recursive Move function
//   // Base Case
//   if (unitsArray.length === 0) {
//     return;
//   } else {
//     this._moveUnit(unitsArray.pop(), function() {
//       this._moveAllUnits(unitsArray);
//     });
//   };
// }

// AggressiveMode.prototype._moveUnit = function(unit, callback) {
//   // var mousePos = unit.pos
//   // this.battle.currentSelectedTile = battle.map.getTileAtPos(mousePos);
//   this.battle.currentSelectedUnit = unit;
//   var possibleMoves = unit.getPossibleMoves(unit.pos, this.battle.map, this.battle.enemyPositions())
//   possibleMoves = this._filterPossibleMoves(possibleMoves)
//   this.battle.currentSelectedMovement = possibleMoves;
//   this.battle.turnState = "selectingMove";
//   this.battle.renderMoveHighlights();
//   // // mousePos = this._getClosestMove(possibleMoves);
//   var squareToMoveTo = this._getClosestMove(possibleMoves);
//   // this.usedPositionsThisTurn.push(squareToMoveTo);
//   this.battle.turnState = "animatingMovement";
//   unit.walkPath = squareToMoveTo.getPath();
// };

// AggressiveMode.prototype._keepAnimatingUnit = function(unit) {
//   if (unit.walkPath.length === 0) {
//     return false;
//   } else {
//     return true;
//   }
// };

AggressiveMode.prototype._endTurn = function() {
  battle.players[1].endTurn();
  battle.currentPlayer = 1;
  var prevGold = battle.getCurrentPlayer().gold;
  battle.addGold();
  if (prevGold !== battle.getCurrentPlayer().gold) {
      game.add.audio('coin').play();
  };
  battle.getCurrentPlayer().onTurnStart(battle.map, battle.currentPlayer);
  var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
  var text = game.add.text(game.world.centerX , game.world.centerY - 300, "Player 1 Turn", style);
  text.anchor.set(0.5);
  text.alpha = 1;
  var tween = game.add.tween(text).to( { alpha: 0 }, 2000, "Linear", true);
  battle.turn ++;
  turnCount.setText("Turn: " + battle.turn);
  currentPlayerText.setText("Player " + this.battle.currentPlayer);
  currentPlayerGold.setText("Gold: " + this.battle.players[battle.currentPlayer - 1].gold);
};

AggressiveMode.prototype._getEnemyHQPos = function() {
  pos = this.battle.players[0].hqPos;
  return pos;
};

AggressiveMode.prototype._getDistanceBetween = function(pos1, pos2) {
  // Currently using pythagorean theorem to find distance, but can use some distance function which takes account terrain.
  return Math.sqrt(Math.pow(Math.abs(pos1.x - pos2.x), 2) + Math.pow(Math.abs(pos1.y - pos2.y), 2));
};

AggressiveMode.prototype.arrayIncludesPosition = function(array, pos) {
  var included = false;
  array.forEach(function(item) {
    if(pos.equals(item)) {
      included = true;
      return;
    }
  });
  return included;
}

AggressiveMode.prototype.handleComputerMove = function() {
  // Returns mousePos to position computer desires to click.
  // Sets battle.computerCanClick to true once a selection is made.
  var mousePos;
  if (this.buildPhase === true) {
    if (this.battle.turnState !== "buildUnit") {
      if (this._selectNextBarracks) {
          mousePos = this._selectNextBarracks;
      }
      else {
        this.buildPhase = false;
        this._endTurn();
      }
    }
    else if (this.battle.turnState === "buildUnit") {
      // find the most expensive affordable unit
      // get the mousePos for that unit
    }

  }
  else if(this.battle.turnState === "selectingUnit") {
    mousePos = this._selectNextUnit();
  }
  else if(this.battle.turnState === "selectingMove") {
    mousePos = this._selectNextMove();
  }
  else if(this.battle.turnState === "selectingAttack") {
    mousePos = this._selectNextAttack();
  }
  else if (this.battle.turnState === "capturePrompt") {
    mousePos = this._selectNextCapture();
  } 
  else {
    mousePos = new Pos (1,1);
  }
  return mousePos;
};

// Battle.prototype.clickOnBarracks = function(mousePos) {
//   if (this.currentSelectedTile) {
//     if (this.currentSelectedTile.name === "barracks" && this.currentSelectedTile.owner === this.currentPlayer) {
//       this.turnState = "buildUnit";
//       this.buildScreen = new BuildScreen(this.getCurrentPlayer().army.armyList, this, mousePos);
//     }
//   }
// }

// AggressiveMode.prototype._runBuildPhase = function() {
//   // fix so won't reset
//     var unusedBarracks = this._getAllBarracks();
//     this._selectNextBarracks(unusedBarracks.pop());
// }

AggressiveMode.prototype._selectNextBarracks = function(barracks) {
  var allBarracks = this._getAllBarracks();
  if (allBarracks.length !== 0) {
    var mousePos = this._getBarracksPosition(allBarracks[0]);
    this.visitedBarracks.push(allBarracks.shift());
    return mousePos;
  }
  else {
    return null;
  }
};

AggressiveMode.prototype._getAllBarracks = function() {
  var barracks = [];
  this.battle.map.getAllBuildings(true).forEach(function(buildingArray) {
    if (buildingArray[0].name === "barracks" && parseInt(buildingArray[0].owner) === this.battle.currentPlayer && this.visitedBarracks.includes(buildingArray) === false) {
      barracks.push(buildingArray);
    }
  });
  return barracks;
};

AggressiveMode.prototype._getBarracksPosition = function(barracks) {
  var barracksTileCoordinates = [];
  barracksTileCoordinates.push(barracks[1]);
  barracksTileCoordinates.push(barracks[2]);
  return barracksTileCoordinates;
};

AggressiveMode.prototype._getMostExpensiveAffordableUnit = function (armyList) {
  var mostExpensiveAffordableUnit = null;
  var greatestAffordableUnitCost = 0;
  for (var unitCost in UNITS) {
    if (UNITS.unitCost > greatestAffordableUnitCost && UNITS.unit <= this.battle.getCurrentPlayer().gold) {
      greatestAffordableUnitCost = UNITS.unit;
      mostExpensiveAffordableUnit = unit;
    }
  }
  return mostExpensiveAffordableUnit;
}

AggressiveMode.prototype._findUnitMousePos = function(unit) {
  
}

AggressiveMode.prototype._selectNextUnit = function() {
  var nextUnitPos = null;
  var that = this;
  this.battle.players[1].army.units.forEach(function(unit) {
    if (!nextUnitPos && !unit.movedThisTurn) {
      that.currentSelectedUnit = unit;
      nextUnitPos = unit.pos;
    };
  });
  // Logic for when to end turn
  if (nextUnitPos === null) {
    this.buildPhase = true;
  }
  return nextUnitPos
}

AggressiveMode.prototype._selectNextMove = function() {
  var that = this;
  var nextMovePos = null;
  var possibleMoves = this.currentSelectedUnit.getPossibleMoves(this.currentSelectedUnit.pos, this.battle.map, this.battle.enemyPositions());
  var filteredPossibleMoves = this._filterPossibleMoves(possibleMoves);
  if (this.currentSelectedUnit instanceof UnitArtillery) {
    nextMovePos = that._handleArtilleryMovement(filteredPossibleMoves);
  } else {
    // nextMovePos = this._getClosestMoveToEnemyHQ(filteredPossibleMoves);
    // nextMovePos = this._getClosestMoveToEnemyUnit(filteredPossibleMoves);
    nextMovePos = this._randomizeUnitMovement(filteredPossibleMoves);
  }
  return nextMovePos;
}

AggressiveMode.prototype._selectNextAttack = function() {
  var nextAttackPos = null;
  var possibleAttackMoves = this.currentSelectedUnit.getPossibleAttacks(this.battle.map);
  var filteredAttackMoves = this._filterPossibleAttackMoves(possibleAttackMoves);
  return filteredAttackMoves[0];
}

AggressiveMode.prototype._selectNextCapture = function() {

};



AggressiveMode.prototype._filterPossibleMoves = function(possibleMoves) {
  // Takes in list of possible moves and filters out positions in which friendly units already exist
  // for each possible move
    // Call unitAtPos and check if null
  var that = this;
  result = [];
  possibleMoves.forEach(function(movePos) {
    var unitAtPos = that.battle.getUnitAtPos(movePos);
    if (!unitAtPos) {
      result.push(movePos);
{'fraction_non_alphanumeric': 0.13995122491963197, 'fraction_numerical': 0.0063740161844584855, 'mean_word_length': 3.830789825970549, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '665660', 'n_tokens_mistral': 6047, 'n_tokens_neox': 5845, 'n_words': 1025}