using RailEnterprise.Core.Simulation; using RailEnterprise.Core.Simulation.Systems; namespace RailEnterprise.Core.Persistence; /// /// Eventi del mondo in corso (corpo del salvataggio dalla versione 5, ADR 0017): tipo e bene per id /// testuale, bersaglio, giorni d'inizio e fine, intensità. Le versioni precedenti si caricano senza /// eventi attivi. Entra nell'hash giornaliero con lo stato dinamico. /// public static class EventSerializer { public static void Write(World w, SaveWriter s) { var state = w.WorldEvents; s.Write(state.NextId); s.Write(state.Active.Count); foreach (var e in state.Active) { s.Write(e.Id); s.Write(e.Def.Id); s.Write((byte)e.TargetKind); s.Write(e.Target); s.Write(e.Good >= 0 ? w.Data.Goods[e.Good].Id : ""); s.Write(e.StartDay); s.Write(e.EndDay); s.Write(e.Intensity); } } public static void Read(World w, SaveReader r) { var state = w.WorldEvents; state.Clear(); state.NextId = r.ReadInt32(); int n = r.ReadInt32(); for (int i = 0; i < n; i++) { int id = r.ReadInt32(); string type = r.ReadRequiredString(); var kind = (EntityKind)r.ReadByte(); int target = r.ReadInt32(); string goodId = r.ReadRequiredString(); long start = r.ReadInt64(); long end = r.ReadInt64(); double intensity = r.ReadDouble(); // un tipo o un bene tolti dai dati fanno finire l'evento al caricamento var def = w.Data.EventType(type); if (def is null) continue; int good = -1; if (goodId.Length > 0 && !w.Data.TryGoodIndex(goodId, out good)) continue; var (center, radius2) = EventSystem.Zone(w, def, kind, target); state.Add(new ActiveWorldEvent { Id = id, Def = def, TargetKind = kind, Target = target, Good = good, StartDay = start, EndDay = end, Intensity = intensity, Center = center, RadiusSquaredM = radius2, }); } } }