示例#1
0
 public void NotifyPoweredRequiredPowerChangeHandlers(TileEntityPowered te, int oldValue, int newValue)
 {
     if (OnPoweredRequiredPowerChanged != null)
     {
         OnPoweredRequiredPowerChanged(te, oldValue, newValue);
     }
 }
示例#2
0
        /*
         * Power Source Notifiers
         */

        public void NotifyPoweredIsPoweredChangeHandlers(TileEntityPowered te, bool oldValue, bool newValue)
        {
            if (OnPoweredIsPoweredChanged != null)
            {
                OnPoweredIsPoweredChanged(te, oldValue, newValue);
            }
        }
        public void OnTileEntityChanged(TileEntity _te, int _dataObject)
        {
            Log.Out(_te.GetTileEntityType().ToString() + " - " + _dataObject);
            TileEntityPowered powered = _te as TileEntityPowered;

            if (powered.IsPowered != this.isPowered)
            {
                bool oldIsPowered = this.isPowered;
                this.isPowered = powered.IsPowered;
                //notify handlers
                API.Events.NotifyPoweredIsPoweredChangeHandlers(powered, oldIsPowered, this.isPowered);
            }

            if (powered.PowerUsed != this.powerUsed)
            {
                int oldPowerUsed = this.powerUsed;
                this.powerUsed = powered.PowerUsed;
                //notify handlers
                API.Events.NotifyPoweredPowerUsedChangeHandlers(powered, oldPowerUsed, this.powerUsed);
            }

            if (powered.RequiredPower != this.requiredPower)
            {
                int oldRequiredPower = this.requiredPower;
                this.requiredPower = powered.RequiredPower;
                //notify handlers
                API.Events.NotifyPoweredRequiredPowerChangeHandlers(powered, oldRequiredPower, this.requiredPower);
            }
        }
示例#4
0
    public static bool IsBlockPoweredUp(Vector3i _blockPos, int _clrIdx)
    {
        WorldBase world = GameManager.Instance.World;

        if (world.IsRemote())
        {
            //Use HasActivePower power instead since directly powering blocks doesnt work on servers.
            return(BlockHydroponicFarmPower.HasActivePower(world, _clrIdx, _blockPos));
        }
        TileEntityPowered tileEntityPowered = (TileEntityPowered)GameManager.Instance.World.GetTileEntity(_clrIdx, _blockPos);

        if (tileEntityPowered != null)
        {
            if (tileEntityPowered.IsPowered)
            {
                DebugMsg("Block Power Is On");
                return(true);
            }
        }
        if (BlockHydroponicFarmPower.IsSpRemotePowerAllowed(_blockPos))
        {
            DebugMsg("No direct power found, checking for remote power");
            return(BlockHydroponicFarmPower.HasActivePower(world, _clrIdx, _blockPos));
        }
        DebugMsg("Block Power Is Off");
        return(false);
    }
        public TEPoweredChangedHandler(TileEntity _te)
        {
            TileEntityPowered powered = _te as TileEntityPowered;

            isPowered     = powered.IsPowered;
            powerUsed     = powered.PowerUsed;
            requiredPower = powered.RequiredPower;
        }
示例#6
0
    /**
     * Checks if block is powered. It is required to be next to a TileEntityPowered type of block in any cardinal direction.
     */

    public bool IsPowered()
    {
        if (!this.CanTick())
        {
            return(this.hasPower);
        }

        if (!this.requiresPower)
        {
            return(this.hasPower = true);
        }

        if (this.poweredBlockCoords.Count == 0)
        {
            return(this.hasPower = false);
        }

        World world = GameManager.Instance.World;
        Dictionary <Vector3i, TileEntity> tileEntitiesNearby = CoordinateHelper.GetTileEntitiesInCoordinatesWithType(world, this.poweredBlockCoords, TileEntityType.Powered);

        if (tileEntitiesNearby.Count == 0)
        {
            return(this.hasPower = false);
        }

        foreach (KeyValuePair <Vector3i, TileEntity> entry in tileEntitiesNearby)
        {
            TileEntityPowered otherTileEntity    = entry.Value as TileEntityPowered;
            Vector3i          otherTileEntityPos = entry.Key;

            foreach (string powerSource in this.powerSources)
            {
                string name = CoordinateHelper.GetBlockNameAtCoordinate(world, otherTileEntityPos);
                if (!CoordinateHelper.BlockAtCoordinateIs(world, otherTileEntityPos, powerSource))
                {
                    continue;
                }

                if (otherTileEntity.IsPowered)
                {
                    return(this.hasPower = true);
                }
            }
        }
        return(this.hasPower = false);
    }
示例#7
0
    /**
     * Checks if block is powered. It is required to be next to an electric wire relay.
     */

    private bool IsPowered()
    {
        if (!this.requiresPower)
        {
            return(true);
        }

        World           world         = GameManager.Instance.World;
        Vector3i        tileEntityPos = this.ToWorldPos();
        List <Vector3i> nearby        = CoordinateHelper.GetCoOrdinatesAround(tileEntityPos, true, 1, 1, 1);

        if (nearby.Count == 0)
        {
            return(false);
        }

        Dictionary <Vector3i, TileEntity> tileEntitiesNearby = CoordinateHelper.GetTileEntitiesInCoordinates(world, nearby, TileEntityType.Powered);

        if (tileEntitiesNearby.Count == 0)
        {
            return(false);
        }

        foreach (KeyValuePair <Vector3i, TileEntity> entry in tileEntitiesNearby)
        {
            TileEntityPowered otherTileEntity    = entry.Value as TileEntityPowered;
            Vector3i          otherTileEntityPos = entry.Key;

            foreach (string powerSource in this.powerSources)
            {
                string name = CoordinateHelper.GetBlockNameAtCoordinate(world, otherTileEntityPos);
                if (!CoordinateHelper.BlockAtCoordinateIs(world, otherTileEntityPos, powerSource))
                {
                    continue;
                }

                if (otherTileEntity.IsPowered)
                {
                    return(true);
                }
            }
        }
        return(false);
    }
示例#8
0
 //Used for severs, block will be NOT be powered directly. Also used in SP if AllowRemotePower is true in the xml.
 public static bool HasActivePower(WorldBase _world, int _cIdx, Vector3i _blockPos)
 {
     Vector3i[] locations = PowerInputLocations(_blockPos);
     foreach (Vector3i vector in locations)
     {
         BlockValue inputBlockValue = _world.GetBlock(vector);
         Type       inputBlockType  = Block.list[inputBlockValue.type].GetType();
         if (inputBlockType == typeof(BlockPowered))
         {
             TileEntityPowered tileEntityPowered = (TileEntityPowered)_world.GetTileEntity(_cIdx, vector);
             if (tileEntityPowered != null)
             {
                 if (tileEntityPowered.IsPowered)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#9
0
    public override void OnBlockEntityTransformBeforeActivated(WorldBase _world, Vector3i _blockPos, int _cIdx, BlockValue _blockValue, BlockEntityData _ebcd)
    {
        this.shape.OnBlockEntityTransformBeforeActivated(_world, _blockPos, _cIdx, _blockValue, _ebcd);
        DebugMsg("OnBlockEntityTransformBeforeActivated");
        try
        {
            if (_ebcd != null && _ebcd.bHasTransform)
            {
                GameObject        gameObject = _ebcd.transform.gameObject;
                HealingPodControl healingPodControlScript = gameObject.GetComponent <HealingPodControl>();
                if (healingPodControlScript == null)
                {
                    healingPodControlScript = gameObject.AddComponent <HealingPodControl>();
                }
                healingPodControlScript.enabled  = true;
                healingPodControlScript.cIdx     = _cIdx;
                healingPodControlScript.blockPos = _blockPos;
            }
            else
            {
                DebugMsg("ERROR: _ebcd null (OnBlockEntityTransformBeforeActivated)");
            }
        }
        catch (Exception ex)
        {
            DebugMsg("Error Message: " + ex.ToString());
        }

        // Offset wire so that it's at the base of the Healing Pod
        TileEntityPowered tileEntityPowered = (TileEntityPowered)_world.GetTileEntity(_cIdx, _blockPos);

        if (tileEntityPowered != null)
        {
            tileEntityPowered.WireOffset = new Vector3(0, -0.5f, 0);
        }
    }
示例#10
0
 public static void SetBool3(this TileEntityPowered obj, bool value)
 {
     fi_TileEntityPowered_bool_3.SetValue(obj, value);
 }
示例#11
0
 public static void SetWireParent(this TileEntityPowered obj, Vector3i pos)
 {
     fi_TileEntityPowered_wireParent.SetValue(obj, pos);
 }
示例#12
0
 public static List <Vector3i> GetWireChildren(this TileEntityPowered obj)
 {
     return((List <Vector3i>)fi_TileEntityPowered_wireChildren.GetValue(obj));
 }
示例#13
0
        private static void LoadTileEntityPowered(BinaryReader _br, TileEntityPowered tileEntity, Vector3i posDelta)
        {
            const int expectedReadVersion = 3; // See CurrentSaveVersion.CurrentSaveVersion
            Vector3i  noParent            = new Vector3i(-9999, -9999, -9999);

            // Doing everything here that the TileEntityPowered classes do in read(..) methods, but modifying the locations of itself,
            // parents, and children in the process. Intentionally not using ELSE because of TileEntityPowered inheritence,
            // see: https://abload.de/img/tileentity-hierarchyanxo7.png

            if (tileEntity is TileEntity) // always
            {
                var te          = (TileEntity)tileEntity;
                var readVersion = _br.ReadUInt16();
                if (readVersion != expectedReadVersion)
                {
                    Log.Error($"Could not load tile entity during import: te's readVersion is {readVersion}, but we expected version {expectedReadVersion} in the file.");
                    throw new FriendlyMessageException("The import file contains incompatible data; maybe it was written with a too new game version. Check if an update for the mod is available!");
                }

                te.SetReadVersion(readVersion);
                var localChunkPos = NetworkUtils.ReadVector3i(_br);
                localChunkPos    = World.toBlock(te.GetChunk().ToWorldPos(localChunkPos) + posDelta); // Adjusting to new position
                te.localChunkPos = localChunkPos;
                // te.localChunkPos = World.toBlock(NetworkUtils.ReadVector3i(_br));
                te.entityId = _br.ReadInt32();
                //if (te.GetReadVersion() <= 1)
                //    return;
                te.SetNextHeatMapEvent(_br.ReadUInt64());
            }

            if (tileEntity is TileEntityPowered) // always
            {
                // ReSharper disable once RedundantCast
                var te   = (TileEntityPowered)tileEntity;
                int num1 = _br.ReadInt32();
                te.IsPlayerPlaced = _br.ReadBoolean();
                te.PowerItemType  = (PowerItem.PowerItemTypes)_br.ReadByte();
                te.InitializePowerData();
                te.SetBool1(true);
                int wireChildrenSize = _br.ReadByte();
                te.GetWireChildren().Clear(); // list_1
                for (int index = 0; index < wireChildrenSize; ++index)
                {
                    te.GetWireChildren().Add(NetworkUtils.ReadVector3i(_br) + posDelta); // Adjusting to new position
                }
                te.CheckForNewWires();
                var wireParent = NetworkUtils.ReadVector3i(_br);
                if (wireParent != noParent)
                {
                    wireParent += posDelta;                                              // Adjusting to new position
                }
                te.SetWireParent(wireParent);
                //if (_eStreamMode == TileEntity.StreamModeRead.FromServer)
                //    te.bool_2 = _br.ReadBoolean();
                te.SetBool3(true);
                te.MarkWireDirty();
                if (num1 <= 0)
                {
                    return;
                }
                //if (_eStreamMode == TileEntity.StreamModeRead.FromServer)
                //{
                //    bool flag = false;
                //    if (LocalPlayerUI.GetUIForPrimaryPlayer().windowManager.Contains(XUiC_PowerCameraWindowGroup.string_0))
                //    {
                //        XUiC_PowerCameraWindowGroup controller = (XUiC_PowerCameraWindowGroup)((XUiWindowGroup)LocalPlayerUI.GetUIForPrimaryPlayer().windowManager.GetWindow(XUiC_PowerCameraWindowGroup.string_0)).Controller;
                //        flag = te.IsUserAccessing() && controller != null && controller.TileEntity == te;
                //    }
                //    if (!flag)
                //    {
                //        te.CenteredPitch = _br.ReadSingle();
                //        te.CenteredYaw = _br.ReadSingle();
                //    }
                //    else
                //    {
                //        double num3 = (double)_br.ReadSingle();
                //        double num4 = (double)_br.ReadSingle();
                //    }
                //}
                //else
                //{
                te.CenteredPitch = _br.ReadSingle();
                te.CenteredYaw   = _br.ReadSingle();
                //}
            }

            if (tileEntity is TileEntityPoweredBlock)
            {
                // Nothing to do here
            }

            if (tileEntity is TileEntityPoweredRangedTrap)
            {
                var te = (TileEntityPoweredRangedTrap)tileEntity;
                if (te.PowerItemType == PowerItem.PowerItemTypes.RangedTrap)
                {
                    te.SetOwner(_br.ReadString());
                }
            }

            if (tileEntity is TileEntityPoweredTrigger)
            {
                var te = (TileEntityPoweredTrigger)tileEntity;
                te.TriggerType = (PowerTrigger.TriggerTypes)_br.ReadByte();
                if (te.TriggerType == PowerTrigger.TriggerTypes.Motion)
                {
                    te.SetOwner(_br.ReadString());
                }
            }

            if (tileEntity is TileEntityPowerSource)
            {
                var te = (TileEntityPowerSource)tileEntity;
                if (te.ClientData == null)
                {
                    te.ClientData = new TileEntityPowerSource.ClientPowerData();
                }
            }
        }
示例#14
0
 public BCMTileEntityPowered(Vector3i pos, TileEntityPowered te) : base(pos, te)
 {
     //todo
 }
        /// <summary>
        /// Returns false if the given tile entity has an invalid PowerItem attached; true otherwise
        /// </summary>
        public static bool IsValidTileEntityPowered([NotNull] TileEntityPowered te)
        {
            var teType = te.GetType();
            var pi     = te.GetPowerItem();

            // Can't check what's not there. That's ok, some powered blocks (e.g. lamps) don't have a power item until connected.
            if (pi == null)
            {
                return(true);
            }

            var piType = pi.GetType();

            if (te is TileEntityPoweredTrigger teTrigger)
            {
                // Trigger must be handled differently, because there are multiple possible power items for one TileEntityPoweredTriger,
                // and the PowerItemType is sometimes just (incorrectly) "PowerSource" when the TriggerType determines the *real* power type.

                // CHECK 1: Power item should be of type PowerTrigger if this is a TileEntityPoweredTrigger
                if (!(pi is PowerTrigger piTrigger))
                {
                    Log.Debug($"[{te.ToWorldPos()}] {teType} should have power item \"PowerTrigger\" or some descendant of it, but has power item \"{piType}\".");
                    return(false);
                }

                // CHECK 2: PowerItemType should match the actual power item's object type, or be at least "PowerSource",
                // because TileEntityPoweredTrigger sometimes has the (incorrect) default PowerItemType "PowerSource" value
                // and only TriggerType is reliable. It "smells" but we have to accept it.
                if (te.PowerItemType != pi.PowerItemType && te.PowerItemType != PowerItem.PowerItemTypes.Consumer)
                {
                    Log.Debug($"[{te.ToWorldPos()}] {teType}.PowerItemType=\"{te.PowerItemType}\" doesn't match with {piType}.PowerItemType=\"{pi.PowerItemType}\" " +
                              $"and is also not the default \"{PowerItem.PowerItemTypes.Consumer}\".");
                    return(false);
                }

                // CHECK 3: TriggerType and actual power item type should be compatible
                var expectedClass = ValidTriggerTypes.GetValue(teTrigger.TriggerType);
                if (expectedClass == null)
                {
                    Log.Warning($"Unknown enum value PowerTrigger.TriggerTypes.{teTrigger.TriggerType} found.");
                }
                else if (piType != expectedClass)
                {
                    Log.Debug($"[{te.ToWorldPos()}] {teType}.TriggerType=\"{teTrigger.TriggerType}\" doesn't fit together with power item \"{piType}\". " +
                              $"A {expectedClass} was expected.");
                    return(false);
                }

                // CHECK 4: Tile entity's TriggerType and power items's TriggerType should match
                if (teTrigger.TriggerType != piTrigger.TriggerType)
                {
                    Log.Debug($"[{te.ToWorldPos()}] {teType}.TriggerType=\"{teTrigger.TriggerType}\" doesn't match with {piType}.PowerItemType=\"{piTrigger.TriggerType}\".");
                    return(false);
                }
            }
            else
            {
                // CHECK 5: For all non-trigger tile entities, the power item type must match with the actual object
                if (te.PowerItemType != pi.PowerItemType)
                {
                    Log.Debug($"[{te.ToWorldPos()}] {teType}.PowerItemType=\"{te.PowerItemType}\" doesn't match with {piType}.PowerItemType=\"{pi.PowerItemType}\".");
                    return(false);
                }
            }

            return(true);
        }