/// <summary> /// Creates a new Snapshot /// </summary> /// <param name="tick">The DoTick, needed to sort /// them later; only incremented</param> /// <param name="cells">The Cells during the tick</param> /// <param name="deaths">The dead humans since the last tick</param> private TickSnapshot(long tick, CellSnapshot[] cells, HumanSnapshot[] deaths) { Stamp = DateTime.Now; Tick = tick; Name = String.Format("{0:D3}", Tick) + "_[" + Stamp.ToString("HH-mm-ss") + "]"; Cells = cells; Deaths = deaths; }
/// <summary> /// Returns how many Dead Humans had which Profession /// </summary> /// <param name="humans">The Dead Humans to be analyzed</param> /// <returns></returns> public static Dictionary<string, int> ProfessionInformation(HumanSnapshot[] humans) { Dictionary<string, int> profInf = new Dictionary<string, int>(); foreach (EProfession prof in Enum.GetValues(typeof(EProfession))) { profInf.Add(prof.ToString(), humans.Count(x => x.Profession.Equals(prof))); } return profInf; }
public SimulationData(DateTime startTime) : base("SD") { Cells = new PopulationCell[0]; // 10808574 Humans = new Human[0]; // ~82000000 Deaths = new HumanSnapshot[0]; DeathCount = 0; _time = startTime; }
/// <summary> /// Returns, how many Humans died away from Home /// </summary> /// <param name="humans">The Dead Humans to be analyzed</param> /// <returns></returns> public static string HomeCellInformation(HumanSnapshot[] humans) { return humans.Count(x => x.HomeCell != x.DeathCell) + " out of " + humans.Length + " Humans died far away from home..."; }
/// <summary> /// Return information about deathCauses /// </summary> /// <param name="humans">The Dead Humans to be analyzed</param> /// <returns></returns> public static string DeathInformation(HumanSnapshot[] humans) { return humans.Count(x => x.Cause) + " Humans died of the disease, " + humans.Count(x => !x.Cause) + " Humans died of their age..."; }
/// <summary> /// Returns a List of Humans matching the given fields /// </summary> /// <param name="humans">The Original Array of Humans</param> /// <param name="field">The field to be matched</param> /// <returns>A (propably) smaller List of Humans</returns> public static List<HumanSnapshot> GetMatchingHumans(HumanSnapshot[] humans, EStatField field) { List<EStatField> fields = new List<EStatField>(); foreach (EStatField e in Enum.GetValues(typeof(EStatField))) { if ((e & field) == e) fields.Add(e); } List<HumanSnapshot> deceasedHumans = new List<HumanSnapshot>(); bool useCause = (fields.Contains(EStatField.Infected) != fields.Contains(EStatField.Diseased)); foreach (HumanSnapshot human in humans) { EAge age = WrapAge(human.Age); EGender gender = (EGender)human.Gender; bool fits = true; if (useCause) fits = fields.Contains(EStatField.Infected) ? !human.Cause : human.Cause; if (fits) { if (fields.Contains(EStatField.Infected) && !human.Cause) deceasedHumans.Add(human); else if (fields.Contains(EStatField.Diseased) && human.Cause) deceasedHumans.Add(human); //else // this is a much more elegant and shorter way, BUT I'm not sure, whether it works or not... //{ // for (int i = 0; i < 8; ++i) // { // if (fields.Contains((EStatField)(int)Math.Pow(2, i)) & age == (EAge)((i % 4)* 32)) // deceasedHumans.Add(human); // } //} else if (gender == EGender.Male) { if (fields.Contains(EStatField.MaleBaby) & age == EAge.Baby) deceasedHumans.Add(human); else if (fields.Contains(EStatField.MaleChild) & age == EAge.Child) deceasedHumans.Add(human); else if (fields.Contains(EStatField.MaleAdult) & age == EAge.Adult) deceasedHumans.Add(human); else if (fields.Contains(EStatField.MaleSenior) & age == EAge.Senior) deceasedHumans.Add(human); } else { if (fields.Contains(EStatField.FemaleBaby) & age == EAge.Baby) deceasedHumans.Add(human); else if (fields.Contains(EStatField.FemaleChild) & age == EAge.Child) deceasedHumans.Add(human); else if (fields.Contains(EStatField.FemaleAdult) & age == EAge.Adult) deceasedHumans.Add(human); else if (fields.Contains(EStatField.FemaleSenior) & age == EAge.Senior) deceasedHumans.Add(human); } } } return deceasedHumans; }
/// <summary> /// Initializes a new Snapshot from a file /// </summary> /// <param name="bytes">the byte[] from a file</param> /// <returns>A new Snapshot</returns> public static TickSnapshot InitializeFromFile(byte[] bytes) { if (bytes[0] != HEADER) throw new HeaderCorruptException("Header damaged, should " + HEADER); long tick = BitConverter.ToInt64(bytes, 1); int cellCount = BitConverter.ToInt32(bytes, 9); CellSnapshot[] cells = new CellSnapshot[cellCount]; for (int i = 0; i < cellCount; ++i) { byte[] temp = new byte[CellSnapshot.LENGTH]; Array.Copy(bytes, CONSTLENGTH + i * CellSnapshot.LENGTH, temp, 0, CellSnapshot.LENGTH); cells[i] = CellSnapshot.InitializeFromFile(temp); } #if DEBUG Console.WriteLine("Done parsing Snapshots"); #endif int offset = 13 + cellCount * CellSnapshot.LENGTH; int deathCount = BitConverter.ToInt32(bytes, offset); HumanSnapshot[] deaths = new HumanSnapshot[deathCount]; for (int i = 0; i < deathCount; ++i) { byte[] temp = new byte[HumanSnapshot.LENGTH]; Array.Copy(bytes, CONSTLENGTH + (cellCount * CellSnapshot.LENGTH) + i * HumanSnapshot.LENGTH, temp, 0, HumanSnapshot.LENGTH); deaths[i] = HumanSnapshot.InitializeFromFile(temp); } #if DEBUG Console.WriteLine("Done parsing HumanSnapshots!"); #endif return new TickSnapshot(tick, cells, deaths); }
/// <summary> /// Initializes a new Snapshot from Runtime /// </summary> /// <param name ="tick">DoTick</param> /// <param name ="cells">Cells to be saved</param> /// <param name ="deaths">Dead Humans</param> /// <returns></returns> public static TickSnapshot IntitializeFromRuntime(long tick, CellSnapshot[] cells, HumanSnapshot[] deaths) { return new TickSnapshot(tick, cells, deaths); }
private void ExpandDeathArray() { var newArray = new HumanSnapshot[(int) (Deaths.Length <= 1 ? ARRAY_DEATHS_DEFAULT_SIZE : Deaths.Length*ARRAY_DEATHS_EXPAND_FACTOR)]; Deaths.CopyToOtherArray(newArray); Deaths = newArray; }
/// <summary> /// Resets Deaths /// </summary> public void ClearDeaths() { Deaths = new HumanSnapshot[0]; DeathCount = 0; }