Heal a gunshot hediff through code?

Started by mipen, June 20, 2015, 04:44:34 AM

Previous topic - Next topic

mipen

I am having trouble finding a way of healing or removing a gunshot hediff from a pawn. They require treatment first to be curable, but treating it then using the direct heal ends in an exception. Anyone know of a way to do this? Tynan? Please help? :)

isistoy

#1
Interesting... I have the same thing to think of now.
EDIT: code that deals with it is in JobDriver_HealPatient, which then uses Toils_Heal.ApplyMedicine->Medicine.ApplyOnPawn. Then, I am not sure if you can get rid of Hediff directly from there, or if it's only tagging Hediff as treated and the game deals with it on its own.
<Stay on the scene like a State machine>

skullywag

Pretty sure i did this in my omnimedical/autoheal mod but i based that of haplos mai code. Would be worth checking there.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Haplo

I don't know if it is of some help, but here is the part that gives MAI an enhanced healing.
It should work.. But I'm not sure, if I've tested it in a11..


        private void DoHealDamagedBodyPart(bool enhancedAI)
        {
            IEnumerable<Hediff_Injury> injuredBodyParts = health.hediffSet.GetInjuriesLocalTreatable();
            if (injuredBodyParts.Count() == 0)
            {
                // Nothing to heal, check for missing body parts next
                DoHealMissingBodyPart(enhancedAI);
                return;
            }

            Hediff_Injury injuredBodyPart = injuredBodyParts.RandomElement();

            HediffComp_Treatable hediffCompTreatable = injuredBodyPart.TryGetComp<HediffComp_Treatable>();
            if (hediffCompTreatable == null)
            {
                ThingDef med = DefDatabase<ThingDef>.GetNamed("Medicine");
                hediffCompTreatable.NewlyTreated(enhancedAI ? 1.0f : Rand.Value, enhancedAI ? med : Rand.Value > 0.6f ? med : null);
            }

            // No additional healing while in bed (it heals by itself here)
            if (IsInBed())
                return;

            injuredBodyPart.DirectHeal(Rand.RangeInclusive(0, enhancedAI ? 2 : 1));
        }

mipen

Quote from: Haplo on June 20, 2015, 05:03:41 PM
I don't know if it is of some help, but here is the part that gives MAI an enhanced healing.
It should work.. But I'm not sure, if I've tested it in a11..


        private void DoHealDamagedBodyPart(bool enhancedAI)
        {
            IEnumerable<Hediff_Injury> injuredBodyParts = health.hediffSet.GetInjuriesLocalTreatable();
            if (injuredBodyParts.Count() == 0)
            {
                // Nothing to heal, check for missing body parts next
                DoHealMissingBodyPart(enhancedAI);
                return;
            }

            Hediff_Injury injuredBodyPart = injuredBodyParts.RandomElement();

            HediffComp_Treatable hediffCompTreatable = injuredBodyPart.TryGetComp<HediffComp_Treatable>();
            if (hediffCompTreatable == null)
            {
                ThingDef med = DefDatabase<ThingDef>.GetNamed("Medicine");
                hediffCompTreatable.NewlyTreated(enhancedAI ? 1.0f : Rand.Value, enhancedAI ? med : Rand.Value > 0.6f ? med : null);
            }

            // No additional healing while in bed (it heals by itself here)
            if (IsInBed())
                return;

            injuredBodyPart.DirectHeal(Rand.RangeInclusive(0, enhancedAI ? 2 : 1));
        }

That's pretty much the same as what I have, and it works for anything that doesn't require treatment to fully heal. But once the injury has been treated, calling direct heal causes a null ref exception, though I have no idea how because all it does is decrease the severity :/

mrofa

All i do is clutter all around.

mipen

After almost being reduced to tears, I have finally figured a way to do this. Instead of using Hediff.DirectHeal(), which kept causing exceptions for some reason, I am using this.TakeDamage() and passing the HealGlobal DamageDef, which heals, and the amount and hediff to be healed. And voila, no exceptions! :D This has been bothering me for so long...

So Haplo, if your method throws an exception when trying to heal things, try using TakeDamage with a Heal DamageDef. Your method looks nearly identical to what mine did, but you might be in luck and have it work :P