/// <summary> /// Determine the cells that will be transferred by a battle. /// </summary> /// /// <param name="winner"> /// The winner of the battle. /// </param> /// <param name="loser"> /// The loser of the battle. /// </param> /// /// <returns> /// The bordering cells between the two civs. /// </returns> private Cell[] GetOccupiedTerritory(Civilization winner, Civilization loser) { HashSet <Cell> cells = new HashSet <Cell>(); foreach (Cell cell in winner.Neighbours) { if (cell.Owner == loser) { cells.Add(cell); } } return(cells.ToArray()); }
/// <summary> /// Start a new war between two civs. /// </summary> /// <param name="attacker">The initiator of the war.</param> /// <param name="defender">The defending civ.</param> /// <param name="seed">The seed used for random outcomes.</param> public War(Civilization attacker, Civilization defender, int seed) { _random = new Random(seed); _battleBalance = 0; _duration = 1; Attacker = attacker; Defender = defender; // Notify the participants that the war has properly started. Attacker.StartWar(this); Defender.StartWar(this); // Create the logger and log the start of the war. _logger = Logger.GetInstance(); _logger.AddWithGameTime(string.Format(WAR_START, Attacker.Name, Defender.Name), Simulator.Date, "war"); }
/// <summary> /// Simulate all cells of a civ /// </summary> /// <param name="civilization"> /// The civilization to simulate all cells for /// </param> /// <returns> /// A list of cells changed during the simulation /// </returns> private Cell[] SimulateCells(Civilization civilization) { List <Cell> changed = new List <Cell>(); // Use a parralel loop to speed up territory simulation of large civs. Parallel.ForEach(civilization.Territory, cell => { if (cell.Simulate(rand)) { removeOwner.TryAdd(cell, cell.Owner); } else if (WentOverPopulationThreshold(cell)) { changed.Add(cell); } }); return(changed.ToArray()); }
/// <summary> /// Perform actions chosen by the civs /// </summary> /// <returns> /// A list of cells changed during the simulation /// </returns> private Cell[] PerformCivilizationActions() { List <Cell> changed = new List <Cell>(); // Loop through all the actions from the civ while (actionQueue.Count > 0) { // Try to dequeue the next action if (actionQueue.TryDequeue(out SimulationAction action)) { if (action.Action == CivDecision.EXPAND) { // Get the cell to claim Cell cell = (Cell)action.Params[0]; // Claim the cell if (action.Civilization.ClaimCell(cell)) { // On succes, add to changed list changed.Add(cell); } } else if (action.Action == CivDecision.EXTERMINATE) { // Get the civ to declare war on Civilization defender = (Civilization)action.Params[0]; // Ensure that two countries aren't at war twice at the same time. if (ongoingWars.Find(w => (w.Defender == action.Civilization && w.Attacker == defender)) != null) { continue; } // Create a new war War war = new War(action.Civilization, defender, Scene.Seed); // Add to the ongoing war list ongoingWars.Add(war); } } } return(changed.ToArray()); }
/// <summary> /// The task to run for a civilization /// </summary> /// <param name="civilization"> /// The civilization to run the task for /// </param> /// <returns> /// A list of cells changed during the simulation /// </returns> private Cell[] CivilizationTask(Civilization civilization) { Cell[] changedCells = SimulateCells(civilization); return(changedCells); }
public SimulationAction(Civilization civilization, CivDecision action, object[] @params) { Civilization = civilization; Action = action; Params = @params; }