private static void LoadPowerItem(BinaryReader _br, [NotNull] PowerItem powerItem, Vector3i posDelta) { // Doing everything here that the PowerItem classes do in read(..) methods, but only for itself, not parents or childs. // Intentionally not using ELSE to because of PowerItem inheritence, see: https://abload.de/img/poweritem-hierarchyzvumv.png if (powerItem is PowerItem) // always { // ReSharper disable once RedundantCast var pi = (PowerItem)powerItem; // No need to set block data; it's already set from tile import //this.BlockID = _br.ReadUInt16(); //this.SetValuesFromBlock(); //this.Position = NetworkUtils.ReadVector3i(_br); if (_br.ReadBoolean()) // has parent { // Do NOT use this, because it removes the connection from powerItems previous power item, which belongs to the old prefab location //PowerManager.Instance.SetParent(this, PowerManager.Instance.GetPowerItemByWorldPos(NetworkUtils.ReadVector3i(_br))); var parentPos = NetworkUtils.ReadVector3i(_br); parentPos += posDelta; // Adjusting to new position pi.Parent = PowerManager.Instance.GetPowerItemByWorldPos(parentPos); if (pi.Parent != null) // Could happen when parent was not included in prefab area { // Add reciprocal connection back from parent if (!pi.Parent.Children.Contains(pi)) { pi.Parent.Children.Add(pi); } // When power item receives a parent, it is not a root power item anymore and must be removed from the list var rootPowerItems = PowerManager.Instance.GetRootPowerItems(); if (rootPowerItems.Contains(pi)) { rootPowerItems.Remove(pi); } } } pi.SendHasLocalChangesToRoot(); // No need to read in children; the needed ones are added by tile import //int num = (int)_br.ReadByte(); //this.Children.Clear(); //for (int index = 0; index < num; ++index) //{ // PowerItem node = PowerItem.CreateItem((PowerItem.PowerItemTypes)_br.ReadByte()); // node.read(_br, _version); // PowerManager.Instance.AddPowerNode(node, this); //} } if (powerItem is PowerConsumer) { // nothing to read } if (powerItem is PowerConsumerToggle) { var pi = (PowerConsumerToggle)powerItem; pi.SetIsToggled(_br.ReadBoolean()); } if (powerItem is PowerElectricWireRelay) { // nothing to read } if (powerItem is PowerRangedTrap) { var pi = (PowerRangedTrap)powerItem; pi.SetIsLocked(_br.ReadBoolean()); pi.SetSlots(GameUtils.ReadItemStack(_br)); pi.TargetType = (PowerRangedTrap.TargetTypes)_br.ReadInt32(); } if (powerItem is PowerTrigger) { var pi = (PowerTrigger)powerItem; pi.TriggerType = (PowerTrigger.TriggerTypes)_br.ReadByte(); if (pi.TriggerType == PowerTrigger.TriggerTypes.Switch) { pi.SetIsTriggered(_br.ReadBoolean()); } else { pi.SetIsActive(_br.ReadBoolean()); } if (pi.TriggerType != PowerTrigger.TriggerTypes.Switch) { pi.TriggerPowerDelay = (PowerTrigger.TriggerPowerDelayTypes)_br.ReadByte(); pi.TriggerPowerDuration = (PowerTrigger.TriggerPowerDurationTypes)_br.ReadByte(); pi.SetDelayStartTime(_br.ReadSingle()); pi.SetPowerTime(_br.ReadSingle()); } if (pi.TriggerType != PowerTrigger.TriggerTypes.Motion) { return; } pi.TargetType = (PowerTrigger.TargetTypes)_br.ReadInt32(); } if (powerItem is PowerPressurePlate) { // nothing to read } if (powerItem is PowerTimerRelay) { var pi = (PowerTimerRelay)powerItem; pi.StartTime = _br.ReadByte(); pi.EndTime = _br.ReadByte(); } if (powerItem is PowerTripWireRelay) { // nothing to read } if (powerItem is PowerConsumerSingle) { // nothing to read } if (powerItem is PowerSource) { var pi = (PowerSource)powerItem; pi.LastCurrentPower = pi.CurrentPower = _br.ReadUInt16(); pi.IsOn = _br.ReadBoolean(); pi.SetSlots(GameUtils.ReadItemStack(_br)); pi.SetHasChangesLocal(true); } if (powerItem is PowerBatteryBank) { // nothing to read } if (powerItem is PowerGenerator) { var pi = (PowerGenerator)powerItem; pi.CurrentFuel = _br.ReadUInt16(); } if (powerItem is PowerSolarPanel) { // nothing to read } Log.Debug($"Loaded power item {powerItem.ToStringBetter()}."); }
private static void SavePowerItem(BinaryWriter writer, [NotNull] PowerItem powerItem) { // Doing everything here that the PowerItem classes do in write(..) methods, but only for itself, not parents or childs. // Intentionally not using ELSE because of PowerItem inheritence, see: https://abload.de/img/poweritem-hierarchyzvumv.png if (powerItem is PowerItem) { // ReSharper disable once RedundantCast var pi = (PowerItem)powerItem; // None of this is needed for import //writer.Write(pi.BlockID); //NetworkUtils.Write(writer, pi.Position); writer.Write(pi.Parent != null); if (pi.Parent != null) { NetworkUtils.Write(writer, pi.Parent.Position); } //writer.Write((byte)pi.Children.Count); //for (int index = 0; index < pi.Children.Count; ++index) //{ // writer.Write((byte)pi.Children[index].PowerItemType); // pi.Children[index].write(writer); //} } if (powerItem is PowerConsumer) { // nothing to write } if (powerItem is PowerConsumerToggle) { var pi = (PowerConsumerToggle)powerItem; writer.Write(pi.GetIsToggled()); } if (powerItem is PowerElectricWireRelay) { // nothing to write } if (powerItem is PowerRangedTrap) { var pi = (PowerRangedTrap)powerItem; writer.Write(pi.GetIsLocked()); GameUtils.WriteItemStack(writer, pi.Stacks); writer.Write((int)pi.TargetType); } if (powerItem is PowerTrigger) { var pi = (PowerTrigger)powerItem; writer.Write((byte)pi.TriggerType); // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression if (pi.TriggerType == PowerTrigger.TriggerTypes.Switch) { writer.Write(pi.GetIsTriggered()); } else { writer.Write(pi.GetIsActive()); } if (pi.TriggerType != PowerTrigger.TriggerTypes.Switch) { writer.Write((byte)pi.TriggerPowerDelay); writer.Write((byte)pi.TriggerPowerDuration); writer.Write(pi.GetDelayStartTime()); writer.Write(pi.GetPowerTime()); } if (pi.TriggerType != PowerTrigger.TriggerTypes.Motion) { return; } writer.Write((int)pi.TargetType); } if (powerItem is PowerPressurePlate) { // nothing to write } if (powerItem is PowerTimerRelay) { var pi = (PowerTimerRelay)powerItem; writer.Write(pi.StartTime); writer.Write(pi.EndTime); } if (powerItem is PowerTripWireRelay) { // nothing to write } if (powerItem is PowerConsumerSingle) { // nothing to write } if (powerItem is PowerSource) { var pi = (PowerSource)powerItem; writer.Write(pi.CurrentPower); writer.Write(pi.IsOn); GameUtils.WriteItemStack(writer, pi.Stacks); } if (powerItem is PowerBatteryBank) { // nothing to write } if (powerItem is PowerGenerator) { var pi = (PowerGenerator)powerItem; writer.Write(pi.CurrentFuel); } if (powerItem is PowerSolarPanel) { // nothing to write } Log.Debug($"Exported power item {powerItem.ToStringBetter()}."); }