/// <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> /// 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); }
/// <summary> /// Takes a Snapshot of the current state and starts to write all Snapshots that are left /// </summary> /// <param name="simData">Current SimulationData to take a Snapshot of</param> public void TakeSnapshot(SimulationData simData, long tick) { int cellCount = simData.Cells.Count(x => x != null); CellSnapshot[] cells = new CellSnapshot[cellCount]; int pos = 0; int i = 0; foreach (PopulationCell cell in simData.Cells) // Not parallel... { if (cell != null) cells[i++] = CellSnapshot.InitializeFromRuntime(cell, pos); ++pos; } TickSnapshot snap = TickSnapshot.IntitializeFromRuntime(tick, cells, simData.Deaths.NotNullIterator().ToArray()); simData.ClearDeaths(); _snapshots.Enqueue(snap); TookSnapshot(this, null); }