private void ChargeFromOther(CompPowerBattery battery, float charge)
        {
            battery.DrawPower(charge);
            var efficiency = Props.efficiency;

            Props.efficiency = 1.0f;
            AddEnergy(charge);
            Props.efficiency = efficiency;
        }
Пример #2
0
        // Token: 0x06000015 RID: 21 RVA: 0x00002480 File Offset: 0x00000680
        public bool UseResources()
        {
            if (!this.HasEnoughEnergy())
            {
                return(false);
            }
            float val = activePower;

            for (int i = 0; i < powerTrader.PowerNet.batteryComps.Count; i++)
            {
                CompPowerBattery compPowerBattery = powerTrader.PowerNet.batteryComps[i];
                float            num = Math.Min(val, compPowerBattery.StoredEnergy);
                val -= num;
                compPowerBattery.DrawPower(num);
            }
            return(true);
        }
        public bool UseResources()
        {
            if (!this.HasEnoughEnergy())
            {
                return(false);
            }

            float _EnergyLeftToDrain = this.m_RequiredEnergy;

            for (int i = 0; i < this.m_PowerComp.PowerNet.batteryComps.Count; i++)
            {
                CompPowerBattery compPowerBattery = this.m_PowerComp.PowerNet.batteryComps[i];
                float            _DrainThisTime   = Math.Min(_EnergyLeftToDrain, compPowerBattery.StoredEnergy);

                _EnergyLeftToDrain -= _DrainThisTime;
                compPowerBattery.DrawPower(_DrainThisTime);
            }

            return(true);
        }
 private void ChargeFromOther(CompPowerBattery battery, float charge)
 {
     battery.DrawPower(charge);
     AddEnergy(charge);
 }
Пример #5
0
        public override void Tick()
        {
            base.Tick();

            if (this.StoredEnergy > 0)
            {
                this.StoredEnergy -= 0.01f;
            }
            else
            {
                if (!this.Downed)
                {
                    Hediff hediff = HediffMaker.MakeHediff(ProfSchmilvsPokemon.DefOfs.HediffDefOf.OutOfPower, this);
                    this.health.AddHediff(hediff);
                }
            }

            if (closestCompPower == null && this.StoredEnergy >= this.storedEnergyMax * 0.21f && this.Faction != null)
            {
                if (this.repairJob == null)
                {
                    List <Thing> toRepair = base.Map.listerBuildingsRepairable.RepairableBuildings(this.Faction);

                    if (!toRepair.NullOrEmpty())
                    {
                        this.repairJob = toRepair[0];
                        this.jobs.ClearQueuedJobs();
                        this.pather.StartPath(new LocalTargetInfo(this.repairJob.Position), Verse.AI.PathEndMode.Touch);
                    }
                }
                else
                {
                    if (this.repairJob.MaxHitPoints == this.repairJob.HitPoints)
                    {
                        base.Map.listerBuildingsRepairable.Notify_BuildingRepaired((Building)this.repairJob);
                        this.repairJob = null;
                        this.jobs.ClearQueuedJobs();
                    }
                    else
                    {
                        if (this.Position.AdjacentTo8WayOrInside(this.repairJob.Position))
                        {
                            this.repairing();
                        }
                        else if (this.pather.Destination != new LocalTargetInfo(this.repairJob.Position))
                        {
                            this.pather.StartPath(new LocalTargetInfo(this.repairJob.Position), Verse.AI.PathEndMode.Touch);
                        }
                    }
                }
            }

            if (closestCompPower != null && this.StoredEnergy >= this.storedEnergyMax)
            {
                closestCompPower = null;
                this.jobs.ClearQueuedJobs();
            }

            if (this.Downed)
            {
                if (this.StoredEnergy < this.storedEnergyMax * 0.2f)
                {
                    List <CompPower> dummylist = Spawner.spawnerPokemon.PowerComps;

                    if (!dummylist.NullOrEmpty())
                    {
                        float distance = 999999f;

                        foreach (CompPower cp in dummylist)
                        {
                            float compareDistance = this.Position.DistanceTo(cp.parent.Position);

                            if (compareDistance < distance)
                            {
                                distance         = compareDistance;
                                closestCompPower = cp;
                            }
                        }
                    }
                }

                if (this.Position.AdjacentTo8WayOrInside(this.closestCompPower.parent.Position))
                {
                    PowerNet pn = this.closestCompPower.PowerNet;
                    if (pn != null)
                    {
                        float currentEnergy = pn.CurrentStoredEnergy();
                        if (currentEnergy >= 0.5f)
                        {
                            List <CompPowerBattery> cpb = pn.batteryComps;
                            CompPowerBattery        bat = null;
                            foreach (CompPowerBattery c in cpb)
                            {
                                if (c.StoredEnergy >= 0.5f)
                                {
                                    bat = c;
                                    break;
                                }
                            }
                            if (bat != null)
                            {
                                this.StoredEnergy += 0.5f;
                                bat.DrawPower(0.5f);
                            }
                        }
                    }
                }

                if (this.StoredEnergy > this.storedEnergyMax * 0.2f)
                {
                    List <Hediff> heds = this.health.hediffSet.hediffs;
                    if (!heds.NullOrEmpty())
                    {
                        Hediff oop = null;
                        foreach (Hediff h in heds)
                        {
                            if (h.def.Equals(ProfSchmilvsPokemon.DefOfs.HediffDefOf.OutOfPower))
                            {
                                oop = h;
                            }
                        }
                        if (oop != null)
                        {
                            this.health.RemoveHediff(oop);
                        }
                    }
                }
            }
            else
            {
                if (closestCompPower == null)
                {
                    if (this.StoredEnergy > 0 && this.StoredEnergy < this.storedEnergyMax * 0.2f)
                    {
                        List <CompPower> dummylist = Spawner.spawnerPokemon.PowerComps;

                        if (!dummylist.NullOrEmpty())
                        {
                            float distance = 999999f;

                            foreach (CompPower cp in dummylist)
                            {
                                float compareDistance = this.Position.DistanceTo(cp.parent.Position);

                                if (compareDistance < distance)
                                {
                                    distance         = compareDistance;
                                    closestCompPower = cp;
                                }
                            }

                            this.pather.StartPath(new LocalTargetInfo(this.closestCompPower.parent.Position), Verse.AI.PathEndMode.OnCell);
                        }
                    }
                }
                else
                {
                    if (this.Position.AdjacentTo8WayOrInside(this.closestCompPower.parent.Position))
                    {
                        PowerNet pn = this.closestCompPower.PowerNet;
                        if (pn != null)
                        {
                            float currentEnergy = pn.CurrentStoredEnergy();
                            if (currentEnergy >= 0.5f)
                            {
                                List <CompPowerBattery> cpb = pn.batteryComps;
                                CompPowerBattery        bat = null;
                                foreach (CompPowerBattery c in cpb)
                                {
                                    if (c.StoredEnergy >= 0.5f)
                                    {
                                        bat = c;
                                        break;
                                    }
                                }
                                if (bat != null)
                                {
                                    this.StoredEnergy += 0.5f;
                                    bat.DrawPower(0.5f);

                                    this.evolve();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (this.pather.Destination != new LocalTargetInfo(this.closestCompPower.parent.Position))
                        {
                            this.jobs.ClearQueuedJobs();
                            this.pather.StartPath(new LocalTargetInfo(this.closestCompPower.parent.Position), Verse.AI.PathEndMode.Touch);
                        }
                    }
                }
            }
        }
        protected override void Impact(Thing hitThing)
        {
            Map map = base.Map;

            base.Impact(hitThing);
            ThingDef def = this.def;

            Pawn caster = this.launcher as Pawn;

            if (!this.initialized)
            {
                ModOptions.SettingsRef settingsRef = new ModOptions.SettingsRef();
                CompAbilityUserMagic   comp        = caster.GetComp <CompAbilityUserMagic>();

                pwrVal = comp.MagicData.MagicPowerSkill_Sabotage.FirstOrDefault((MagicPowerSkill x) => x.label == "TM_Sabotage_pwr").level;
                verVal = comp.MagicData.MagicPowerSkill_Sabotage.FirstOrDefault((MagicPowerSkill x) => x.label == "TM_Sabotage_ver").level;
                if (caster.story.traits.HasTrait(TorannMagicDefOf.Faceless))
                {
                    MightPowerSkill mpwr = caster.GetComp <CompAbilityUserMight>().MightData.MightPowerSkill_Mimic.FirstOrDefault((MightPowerSkill x) => x.label == "TM_Mimic_pwr");
                    MightPowerSkill mver = caster.GetComp <CompAbilityUserMight>().MightData.MightPowerSkill_Mimic.FirstOrDefault((MightPowerSkill x) => x.label == "TM_Mimic_ver");
                    pwrVal = mpwr.level;
                    verVal = mver.level;
                }
                this.arcaneDmg = comp.arcaneDmg;
                if (settingsRef.AIHardMode && !caster.IsColonist)
                {
                    pwrVal = 3;
                    verVal = 3;
                }
                SoundInfo info = SoundInfo.InMap(new TargetInfo(base.Position, this.Map, false), MaintenanceType.None);
                info.pitchFactor  = .5f;
                info.volumeFactor = .8f;
                SoundDefOf.PsychicPulseGlobal.PlayOneShot(info);
                Effecter SabotageEffect = TorannMagicDefOf.TM_SabotageExplosion.Spawn();
                SabotageEffect.Trigger(new TargetInfo(base.Position, this.Map, false), new TargetInfo(base.Position, this.Map, false));
                SabotageEffect.Cleanup();
                targetCells       = GenRadial.RadialCellsAround(base.Position, this.def.projectile.explosionRadius, true).ToList();
                this.targetThings = new List <SabotageThing>();
                this.targetThings.Clear();
                this.initialized = true;

                Pawn     targetPawn     = null;
                Building targetBuilding = null;

                for (int i = 0; i < this.targetCells.Count; i++)
                {
                    var targetCell = this.targetCells[i];
                    if (Rand.Chance((.5f + (.1f * verVal)) * this.arcaneDmg))
                    {
                        float rnd = Rand.Range(0, 1f);
                        targetPawn = targetCell.GetFirstPawn(this.Map);
                        if (targetPawn != null)
                        {
                            if (TM_Calc.IsRobotPawn(targetPawn))
                            {
                                TM_Action.DoAction_SabotagePawn(targetPawn, caster, rnd, pwrVal, this.arcaneDmg, this.launcher);
                                this.age = this.duration;
                            }
                            else
                            {
                                targetPawn = null;
                                //Log.Message("pawn not a robot, mechanoid, or android");
                            }
                        }

                        targetBuilding = targetCell.GetFirstBuilding(this.Map);
                        if (targetPawn == null && targetBuilding != null)
                        {
                            CompPower       compP = targetBuilding.GetComp <CompPower>();
                            CompPowerTrader cpt   = targetBuilding.GetComp <CompPowerTrader>();
                            if (compP != null && compP.Props.basePowerConsumption != 0 && cpt != null && cpt.powerOutputInt != 0)
                            {
                                if (true)
                                {
                                    //stun/electrical explosion
                                    GenExplosion.DoExplosion(targetBuilding.Position, base.Map, 2 + pwrVal + Mathf.RoundToInt(cpt.powerOutputInt / 400), DamageDefOf.Stun, null);
                                    GenExplosion.DoExplosion(targetBuilding.Position, base.Map, 1 + pwrVal + Mathf.RoundToInt(cpt.powerOutputInt / 600), TMDamageDefOf.DamageDefOf.TM_ElectricalBurn, null);
                                }
                                else
                                {
                                }
                            }

                            Building_Battery targetBattery = targetBuilding as Building_Battery;
                            if (targetBattery != null && targetBattery.def.thingClass.ToString() == "RimWorld.Building_Battery")
                            {
                                CompPowerBattery compB = targetBattery.GetComp <CompPowerBattery>();
                                if (rnd <= .5f)
                                {
                                    Traverse.Create(root: targetBattery).Field(name: "ticksToExplode").SetValue(Rand.Range(40, 130) - (5 * pwrVal));
                                    compB.SetStoredEnergyPct(.81f);
                                }
                                else
                                {
                                    GenExplosion.DoExplosion(targetBattery.Position, base.Map, 2 + pwrVal + Mathf.RoundToInt(compB.StoredEnergy / 200), DamageDefOf.EMP, null);
                                    compB.DrawPower(compB.StoredEnergy);
                                }
                            }

                            Building_TurretGun targetTurret = targetBuilding as Building_TurretGun;
                            if (targetTurret != null && targetTurret.gun != null)
                            {
                                if (rnd <= .5f)
                                {
                                    targetTurret.SetFaction(Faction.OfAncientsHostile, null);
                                }
                                else
                                {
                                    GenExplosion.DoExplosion(targetTurret.Position, base.Map, 2 + pwrVal, TMDamageDefOf.DamageDefOf.TM_ElectricalBurn, null); //20 default damage
                                }
                            }
                        }
                        else
                        {
                            //Log.Message("no thing to sabotage");
                        }
                        targetPawn     = null;
                        targetBuilding = null;
                    }
                }
            }
            else if (this.targetThings.Count > 0)
            {
                this.age = this.duration;
            }
            else
            {
                this.age = this.duration;
            }
        }
        public float GetEnergyStored(float currentEnergyOtherSide)
        {
            if (thisBattery.PowerNet == null)
            {
                //Logger.MessageFormat(this, "No PowerNet found.");
                reloadedEnergyCalculated = true;

                oldDuplicateEnergy = duplicateEnergy = 1.0f;
                return(0.0f);
            }

            List <CompPowerBattery> networkBatteries = this.AvailableBatteries();

            oldNetworkEnergy   = networkEnergy;
            oldDuplicateEnergy = duplicateEnergy;
            duplicateEnergy    = currentEnergyOtherSide;
            float duplicateEnergyChange = duplicateEnergy - oldDuplicateEnergy;

            networkEnergy = networkBatteries.Select(x => x.StoredEnergy).DefaultIfEmpty(0.0f).Sum();
            float networkMaxEnergy = networkBatteries.Select(x => x.Props.storedEnergyMax).DefaultIfEmpty(0.0f).Sum();

            energyGained   = networkEnergy - oldNetworkEnergy;
            energyReturned = duplicateEnergyChange;

            //Logger.MessageFormat(this, "{0}, Old duplicate energy: {1}, new duplicate energy: {2}, Duplicate energy change: {3}, network: {4}/{5}", this.parent.GetType().ToString(), oldDuplicateEnergy, duplicateEnergy, duplicateEnergyChange, networkEnergy, networkMaxEnergy);

            if (reloadedEnergyCalculated && duplicateEnergyChange != 0.0f)
            {
                float networkEnergyPlusDuplicate = networkEnergy + duplicateEnergyChange;

                // If more energy lost in duplicate battery than available in duplicated network
                if (networkEnergyPlusDuplicate < 0.0f)
                {
                    if (networkEnergy > 0.0f)
                    {
                        // Drain the whole thing
                        foreach (CompPowerBattery battery in networkBatteries)
                        {
                            battery.SetStoredEnergyPct(0.0f);
                        }

                        networkEnergy = 0.0f;

                        //Logger.MessageFormat(this, "Network drained");
                    }
                }
                // If more energy gained in duplicate battery than the network can hold
                else if (networkEnergyPlusDuplicate > networkMaxEnergy)
                {
                    if (networkEnergy < networkMaxEnergy)
                    {
                        // Max the whole thing
                        foreach (CompPowerBattery battery in networkBatteries)
                        {
                            battery.SetStoredEnergyPct(1.0f);
                        }

                        networkEnergy = networkMaxEnergy;

                        //Logger.MessageFormat(this, "Network filled");
                    }
                }
                else if (oldDuplicateEnergy > 0.0f)
                {
                    if (duplicateEnergyChange > 0.0f)
                    {
                        List <CompPowerBattery> shuffledBatteries = new List <CompPowerBattery>();
                        shuffledBatteries.AddRange(networkBatteries);
                        float totalEnergyToGive = duplicateEnergyChange;
                        int   i = 0;
                        while (totalEnergyToGive > 0.0f && i < shuffledBatteries.Count)
                        {
                            CompPowerBattery compPowerBattery = shuffledBatteries[i];
                            float            energyCanRecieve = (compPowerBattery.Props.storedEnergyMax - compPowerBattery.StoredEnergy);
                            float            energyToGive     = Mathf.Min(totalEnergyToGive, energyCanRecieve);

                            compPowerBattery.AddEnergy(energyToGive / compPowerBattery.Props.efficiency);

                            totalEnergyToGive -= energyToGive;
                            ++i;
                        }
                    }
                    else if (duplicateEnergyChange < 0.0f)
                    {
                        List <CompPowerBattery> shuffledBatteries = new List <CompPowerBattery>();
                        shuffledBatteries.AddRange(networkBatteries);
                        float totalEnergyToGive = -duplicateEnergyChange;
                        int   i = 0;
                        while (totalEnergyToGive > 0.0f && i < shuffledBatteries.Count)
                        {
                            CompPowerBattery compPowerBattery = shuffledBatteries[i];
                            float            energyCanRemove  = compPowerBattery.StoredEnergy;
                            float            energyToRemove   = Mathf.Min(totalEnergyToGive, energyCanRemove);

                            compPowerBattery.DrawPower(energyToRemove);

                            totalEnergyToGive -= energyToRemove;
                            ++i;
                        }
                    }

                    //Logger.MessageFormat(this, "Duplicate energy change: {0}, energy returned to network: {1}", energyGained, energyReturned);
                }
            }

            reloadedEnergyCalculated = true;

            // Add in any extra energy stored in the sharer
            if (currentEnergyOtherSide > networkMaxEnergy)
            {
                networkEnergy += (currentEnergyOtherSide - networkMaxEnergy);
            }

            duplicateEnergy = networkEnergy;
            return(networkEnergy);
        }
Пример #8
0
        protected override IEnumerable <Toil> MakeNewToils()
        {
            this.FailOnDestroyedNullOrForbidden(PowerDestIndex);
            AddFailCondition(() => energyNeed == null);

            yield return(Toils_Reserve.Reserve(PowerDestIndex));

            if (!TargetB.IsValid)
            {
                yield return(Toils_Goto.GotoThing(PowerDestIndex, PathEndMode.ClosestTouch));
            }
            else
            {
                yield return(Toils_Reserve.Reserve(AlternateDestIndex));

                yield return(Toils_Goto.GotoThing(AlternateDestIndex, PathEndMode.OnCell));
            }

            Toil rechargeToil = new Toil();

            rechargeToil.tickAction = delegate
            {
                //Drain the powernet.
                CompPowerBattery compBattery = powerBuilding.PowerComp?.PowerNet?.batteryComps?.FirstOrDefault(battery => battery.StoredEnergy > PowerNetEnergyDrainedPerTick);

                if (compBattery != null)
                {
                    compBattery.DrawPower(PowerNetEnergyDrainedPerTick);

                    //Add to our energy.
                    energyNeed.CurLevel += energyNeed.MaxLevel / MaxTicksSpentCharging;
                }

                ticksSpentCharging++;
            };
            rechargeToil.AddEndCondition(delegate
            {
                if (powerBuilding == null || powerBuilding.PowerComp == null /*|| temporaryTrader == null*/)
                {
                    return(JobCondition.Incompletable);
                }

                if (powerBuilding.PowerComp?.PowerNet.CurrentStoredEnergy() < PowerNetEnergyDrainedPerTick)
                {
                    return(JobCondition.Incompletable);
                }

                if (energyNeed.CurLevelPercentage >= 0.99f)
                {
                    return(JobCondition.Succeeded);
                }

                if (ticksSpentCharging > MaxTicksSpentCharging)
                {
                    return(JobCondition.Incompletable);
                }

                return(JobCondition.Ongoing);
            });

            if (!TargetB.IsValid)
            {
                rechargeToil.FailOnCannotTouch(PowerDestIndex, PathEndMode.ClosestTouch);
            }
            else
            {
                rechargeToil.FailOnCannotTouch(AlternateDestIndex, PathEndMode.OnCell);
            }

            rechargeToil.WithProgressBar(TargetIndex.A, () => energyNeed.CurLevelPercentage, false);
            rechargeToil.defaultCompleteMode = ToilCompleteMode.Never;
            yield return(rechargeToil);
        }