float EqualisedPower(PowerConsumerInterface them)
        {
            var sumCapacity = Capacity + them.GetMaxPower();
            var sumPower    = Power + them.GetMaxPower() - them.GetRemainingPowerCapacity();

            return(Capacity * sumPower / sumCapacity);
        }
예제 #2
0
    protected void findMk4AndMk5Bats()
    {
        VicisMod.log(getPrefix(), "Looking for T4 and T5 batteries");
        long[] coords = new long[3];
        for (int i = 0; i < 3; ++i)
        {
            for (int j = -1; j <= 1; j += 2)
            {
                Array.Clear(coords, 0, 3);
                coords[i] = j;

                long x = mnX + coords[0];
                long y = mnY + coords[1];
                long z = mnZ + coords[2];

                Segment segment = base.AttemptGetSegment(x, y, z);
                // Check if segment was generated (skip this point if it doesn't
                if (segment == null)
                {
                    continue;
                }
                ushort cube = segment.GetCube(x, y, z);
                // If this isn't an entity, skip it
                if (!CubeHelper.HasEntity((int)cube))
                {
                    continue;
                }
                PowerConsumerInterface pci = segment.SearchEntity(x, y, z) as PowerConsumerInterface;
                if (pci == null)
                {
                    continue;
                }
                // We're only looking for T4 and T5 batteries
                if (!(pci is T4_Battery) && !(pci is T5_Battery))
                {
                    continue;
                }
                VicisMod.log(getPrefix(), "Found one: " + pci.ToString());
                // Let's only keep track of PCIs that will accept power from the PowWow
                if (!pci.WantsPowerFromEntity(this))
                {
                    continue;
                }
                for (int l = mk4AndMk5Bats.Count - 1; l >= 0 && pci != null; --l)
                {
                    PowerConsumerInterface pci2 = mk4AndMk5Bats[l];
                    if (pci2 != null && !(pci2 as SegmentEntity).mbDelete && pci2 == pci)
                    {
                        pci = null;
                    }
                }
                VicisMod.log(getPrefix(), "End, checking " + pci);
                if (pci != null)
                {
                    mk4AndMk5Bats.Add(pci);
                }
            }
        }
    }
예제 #3
0
 protected void cullBats()
 {
     for (int i = mk4AndMk5Bats.Count - 1; i >= 0; --i)
     {
         PowerConsumerInterface pci = mk4AndMk5Bats[i];
         if (pci == null || (pci as SegmentEntity).mbDelete)
         {
             mk4AndMk5Bats.RemoveAt(i);
         }
     }
 }
예제 #4
0
 protected void cullPCIs()
 {
     for (int i = pcis.Count - 1; i >= 0; --i)
     {
         PowerConsumerInterface pci = pcis[i];
         if (pci == null || (pci as SegmentEntity).mbDelete || pci == powerSource)
         {
             pcis.RemoveAt(i);
         }
     }
 }
예제 #5
0
    protected float attemptTransferPower()
    {
        VicisMod.log(getPrefix(), "Running LFU");
        lastSearch -= LowFrequencyThread.mrPreviousUpdateTimeStep;
        // Nothing to do
        if (currentPower <= 0)
        {
            return(0);
        }
        lastTransfer1 -= LowFrequencyThread.mrPreviousUpdateTimeStep;
        if (lastTransfer1 > 0)
        {
            return(0);
        }
        lastTransfer1 += transferFrequency;
        VicisMod.log(getPrefix(), "Currently have " + currentPower + " Power");
        cullPCIs();
        if (lastSearch <= 0)
        {
            VicisMod.log(getPrefix(), "Finding PCIs");
            findPCIs();
            lastSearch = scanFrequency;
        }

        VicisMod.log(getPrefix(), "Aware of " + pcis.Count + " PCIs");
        // Now lets charge these guys up!
        float transferedThisTime = 0;

        for (int i = 0; i < pcis.Count; ++i)
        {
            PowerConsumerInterface pci = pcis[i];
            if (pci.WantsPowerFromEntity(this))
            {
                float transfer = transferCap;
                transfer = Math.Min(transfer, currentPower);
                transfer = Math.Min(transfer, pci.GetMaximumDeliveryRate());
                transfer = Math.Min(transfer, pci.GetRemainingPowerCapacity());
                if (transfer > 0 && pci.DeliverPower(transfer))
                {
                    VicisMod.log(getPrefix(), "Adding " + transfer + " to PCI " + pci.ToString());
                    currentPower       -= transfer;
                    totalTransfered    += transfer;
                    transferedThisTime += transfer;
                    // And we're done
                    if (currentPower == 0)
                    {
                        return(transferedThisTime);
                    }
                }
            }
        }
        return(transferedThisTime);
    }
        protected override Byte CalculateOutput(SegmentEntity entity, UInt16 data)
        {
            PowerConsumerInterface powerConsumerInterface = entity as PowerConsumerInterface;

            if (powerConsumerInterface == null)
            {
                return(0);
            }
            Double remainingPower = Math.Round(powerConsumerInterface.GetRemainingPowerCapacity(), MidpointRounding.AwayFromZero);
            Double maxPower       = Math.Round(powerConsumerInterface.GetMaxPower(), MidpointRounding.AwayFromZero);

            return((Byte)((1f - remainingPower / maxPower) * 255f));
        }
        void FeedConsumer(PowerConsumerInterface consumer, float ticks)
        {
            var powerToHoldOn = 0f;

            if (consumer as PowerStorageInterface != null)
            {
                powerToHoldOn = EqualisedPower(consumer);
            }
            if (Power > powerToHoldOn)
            {
                var transferAmount = new [] {
                    OutputRate *ticks,
                    Power - powerToHoldOn,
                    consumer.GetRemainingPowerCapacity(),
                    consumer.GetMaximumDeliveryRate() *ticks,
                }.Min();
                if (consumer.DeliverPower(transferAmount))
                {
                    Power -= transferAmount;
                }
            }
        }
        static bool IsGood(PowerConsumerInterface pci)
        {
            var entity = (SegmentEntity)pci;

            return(entity != null && !entity.mbDelete);
        }
예제 #9
0
 protected void findPCIs()
 {
     for (int i = -1 * radius; i <= radius; ++i)
     {
         for (int j = -1 * radius; j <= radius; ++j)
         {
             for (int k = -1 * radius; k <= radius; ++k)
             {
                 // Skip if we're looking at ourselves
                 if (i == 0 && j == 0 && k == 0)
                 {
                     continue;
                 }
                 // Skip if the point is out of our radius
                 if (i * i + j * j + k * k > radius * radius)
                 {
                     continue;
                 }
                 // Check the segment
                 long    x       = this.mnX + i;
                 long    y       = this.mnY + j;
                 long    z       = this.mnZ + k;
                 Segment segment = base.AttemptGetSegment(x, y, z);
                 // Check if segment was generated (skip this point if it doesn't
                 if (segment == null)
                 {
                     continue;
                 }
                 ushort cube = segment.GetCube(x, y, z);
                 // If this isn't an entity, skip it
                 if (!CubeHelper.HasEntity((int)cube))
                 {
                     continue;
                 }
                 PowerConsumerInterface pci = segment.SearchEntity(x, y, z) as PowerConsumerInterface;
                 if (pci == null)
                 {
                     continue;
                 }
                 // Don't feed power to T4 or T5 batteries
                 if (pci is T4_Battery || pci is T5_Battery)
                 {
                     continue;
                 }
                 // Also, don't feed the power source (infinite loop of power, like a laser pointed at it's PSB)
                 if (pci == powerSource)
                 {
                     continue;
                 }
                 // Let's only keep track of PCIs that will accept power from the PowWow
                 if (!pci.WantsPowerFromEntity(this))
                 {
                     continue;
                 }
                 for (int l = pcis.Count - 1; l >= 0 && pci != null; --l)
                 {
                     PowerConsumerInterface pci2 = pcis[l];
                     if (pci2 != null && !(pci2 as SegmentEntity).mbDelete && pci2 == pci)
                     {
                         pci = null;
                     }
                 }
                 if (pci != null)
                 {
                     pcis.Add(pci);
                 }
             }
         }
     }
 }
 static float NormalizedPower(PowerConsumerInterface powerHolder)
 {
     return(1f - powerHolder.GetRemainingPowerCapacity() / powerHolder.GetMaxPower());
 }