|
| 1 | +using UnityEngine; |
| 2 | +using Verse; |
| 3 | +using Verse.Sound; |
| 4 | +using RimWorld; |
| 5 | + |
| 6 | +namespace Autarky |
| 7 | +{ |
| 8 | + public class CompTempExplosive : ThingComp |
| 9 | + { |
| 10 | + // |
| 11 | + // Static Fields |
| 12 | + // |
| 13 | + public const string UnsafeTempSignal = "Autarky_UnsafeStorageTemperature"; |
| 14 | + |
| 15 | + // |
| 16 | + // Fields |
| 17 | + // |
| 18 | + public bool wickStarted; |
| 19 | + |
| 20 | + protected Sustainer wickSoundSustainer; |
| 21 | + |
| 22 | + public bool detonated; |
| 23 | + |
| 24 | + private Thing instigator; |
| 25 | + |
| 26 | + protected int wickTicksLeft; |
| 27 | + |
| 28 | + // |
| 29 | + // Properties |
| 30 | + // |
| 31 | + private bool CanEverExplodeFromDamage { |
| 32 | + get { |
| 33 | + if (this.Props.chanceNeverExplodeFromDamage < 1E-05f) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + Rand.PushState (); |
| 37 | + Rand.Seed = this.parent.thingIDNumber.GetHashCode() + (Find.TickManager.TicksGame % 7919); |
| 38 | + bool result = Rand.Value < this.Props.chanceNeverExplodeFromDamage; |
| 39 | + Rand.PopState (); |
| 40 | + return result; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public CompProperties_TempExplosive Props { |
| 45 | + get { |
| 46 | + return (CompProperties_TempExplosive)this.props; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected int StartWickThreshold { |
| 51 | + get { |
| 52 | + return Mathf.RoundToInt (this.Props.startWickHitPointsPercent * (float)this.parent.MaxHitPoints); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // |
| 57 | + // Methods |
| 58 | + // |
| 59 | + public override void CompTick () |
| 60 | + { |
| 61 | + if (this.wickStarted) |
| 62 | + { |
| 63 | + if (this.wickSoundSustainer == null) |
| 64 | + { |
| 65 | + this.StartWickSustainer(); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + this.wickSoundSustainer.Maintain(); |
| 70 | + } |
| 71 | + this.wickTicksLeft--; |
| 72 | + if (this.wickTicksLeft <= 0) |
| 73 | + { |
| 74 | + this.Detonate(this.parent.MapHeld); |
| 75 | + } |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + float ambientTemperature = this.parent.AmbientTemperature; |
| 80 | + float maxSafeTemperature = this.Props.maxSafeTemperature; |
| 81 | + if (ambientTemperature > maxSafeTemperature) |
| 82 | + { |
| 83 | + Rand.PushState(); |
| 84 | + Rand.Seed = this.parent.thingIDNumber.GetHashCode() + (Find.TickManager.TicksGame % 7919); |
| 85 | + bool selfIgnite = Rand.Value < Mathf.Pow((ambientTemperature - maxSafeTemperature), 2.71828f) * |
| 86 | + this.Props.maxSafeTempFactor; |
| 87 | + Rand.PopState(); |
| 88 | + if (selfIgnite) |
| 89 | + { |
| 90 | + this.StartWick(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public override string CompInspectStringExtra() |
| 97 | + { |
| 98 | + if (this.parent.AmbientTemperature > this.Props.maxSafeTemperature) |
| 99 | + { |
| 100 | + return UnsafeTempSignal.Translate(); |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + protected void Detonate (Map map) |
| 106 | + { |
| 107 | + if (this.detonated) { |
| 108 | + return; |
| 109 | + } |
| 110 | + this.detonated = true; |
| 111 | + if (!this.parent.SpawnedOrAnyParentSpawned) { |
| 112 | + return; |
| 113 | + } |
| 114 | + if (map == null) { |
| 115 | + Log.Warning ("Tried to detonate CompExplosive in a null map."); |
| 116 | + return; |
| 117 | + } |
| 118 | + CompProperties_TempExplosive props = this.Props; |
| 119 | + float num = props.explosiveRadius; |
| 120 | + if (this.parent.stackCount > 1 && props.explosiveExpandPerStackcount > 0f) { |
| 121 | + num += Mathf.Sqrt ((float)(this.parent.stackCount - 1) * props.explosiveExpandPerStackcount); |
| 122 | + } |
| 123 | + if (props.explosionEffect != null) { |
| 124 | + Effecter effecter = props.explosionEffect.Spawn (); |
| 125 | + effecter.Trigger (new TargetInfo (this.parent.PositionHeld, map, false), |
| 126 | + new TargetInfo (this.parent.PositionHeld, map, false)); |
| 127 | + effecter.Cleanup (); |
| 128 | + } |
| 129 | + ThingDef postExplosionSpawnThingDef = props.postExplosionSpawnThingDef; |
| 130 | + float postExplosionSpawnChance = props.postExplosionSpawnChance; |
| 131 | + int postExplosionSpawnThingCount = props.postExplosionSpawnThingCount; |
| 132 | + GenExplosion.DoExplosion (this.parent.PositionHeld, map, num, props.explosiveDamageType, |
| 133 | + this.instigator ?? this.parent, null, null, null, postExplosionSpawnThingDef, |
| 134 | + postExplosionSpawnChance, postExplosionSpawnThingCount, |
| 135 | + props.applyDamageToExplosionCellsNeighbors, props.preExplosionSpawnThingDef, |
| 136 | + props.preExplosionSpawnChance, props.preExplosionSpawnThingCount); |
| 137 | + if (!this.parent.Destroyed) |
| 138 | + { |
| 139 | + this.parent.Kill(null); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public override void PostDraw () |
| 144 | + { |
| 145 | + if (this.wickStarted) { |
| 146 | + this.parent.Map.overlayDrawer.DrawOverlay (this.parent, OverlayTypes.BurningWick); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public override void PostExposeData () |
| 151 | + { |
| 152 | + base.PostExposeData (); |
| 153 | + Scribe_References.Look<Thing> (ref this.instigator, "instigator", false); |
| 154 | + Scribe_Values.Look<bool> (ref this.wickStarted, "wickStarted", false, false); |
| 155 | + Scribe_Values.Look<int> (ref this.wickTicksLeft, "wickTicksLeft", 0, false); |
| 156 | + Scribe_Values.Look<bool> (ref this.detonated, "detonated", false, false); |
| 157 | + } |
| 158 | + |
| 159 | + public override void PostPostApplyDamage (DamageInfo dinfo, float totalDamageDealt) |
| 160 | + { |
| 161 | + if (!this.CanEverExplodeFromDamage) { |
| 162 | + return; |
| 163 | + } |
| 164 | + if (!this.parent.Destroyed) { |
| 165 | + if (this.wickStarted && dinfo.Def == DamageDefOf.Extinguish) { |
| 166 | + this.StopWick (); |
| 167 | + } |
| 168 | + else if (!this.wickStarted && this.parent.HitPoints <= this.StartWickThreshold) { |
| 169 | + this.StartWick (dinfo.Instigator); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public override void PostPreApplyDamage (DamageInfo dinfo, out bool absorbed) |
| 175 | + { |
| 176 | + absorbed = false; |
| 177 | + if (this.CanEverExplodeFromDamage) { |
| 178 | + if (dinfo.Def.externalViolence && dinfo.Amount >= this.parent.HitPoints) { |
| 179 | + if (this.parent.MapHeld != null) { |
| 180 | + this.Detonate (this.parent.MapHeld); |
| 181 | + absorbed = true; |
| 182 | + } |
| 183 | + } |
| 184 | + else if (!this.wickStarted && this.Props.startWickOnDamageTaken != null && |
| 185 | + dinfo.Def == this.Props.startWickOnDamageTaken) { |
| 186 | + this.StartWick (dinfo.Instigator); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public void StartWick (Thing instigator = null) |
| 192 | + { |
| 193 | + if (this.wickStarted) { |
| 194 | + return; |
| 195 | + } |
| 196 | + this.instigator = instigator; |
| 197 | + this.wickStarted = true; |
| 198 | + this.wickTicksLeft = this.Props.wickTicks.RandomInRange; |
| 199 | + this.StartWickSustainer (); |
| 200 | + GenExplosion.NotifyNearbyPawnsOfDangerousExplosive (this.parent, this.Props.explosiveDamageType, null); |
| 201 | + } |
| 202 | + |
| 203 | + private void StartWickSustainer () |
| 204 | + { |
| 205 | + SoundDefOf.MetalHitImportant.PlayOneShot (new TargetInfo (this.parent.Position, this.parent.Map, false)); |
| 206 | + SoundInfo info = SoundInfo.InMap (this.parent, MaintenanceType.PerTick); |
| 207 | + this.wickSoundSustainer = SoundDefOf.HissSmall.TrySpawnSustainer (info); |
| 208 | + } |
| 209 | + |
| 210 | + public void StopWick () |
| 211 | + { |
| 212 | + this.wickStarted = false; |
| 213 | + this.instigator = null; |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments