Item That Heals Below Certain Health and Others

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Item That Heals Below Certain Health and Others

Postby CaptainNurbles » Tue Jan 12, 2021 2:54 pm

So I'm wanting to add an item that accomplishes:

* Constantly heals the player when below 20 HP.
* Administers a morphine shot in the form of PowerProtection when below 30 HP, limited to doing so once every 3 minutes.
* Stays active permanently, even if the player dies and respawns, for the sake of functioning in coop with respawning enabled.

I know how to achieve the last part of this, I think, but I know bugger all for how to achieve the first two. What I've got so far:

Code: Select allExpand view
actor HarnessHeal : PowerRegeneration /// Slow healing powerup, given via script.
{
   Powerup.Duration 0x7FFFFFFF
   Powerup.Strength 1
   +INVENTORY.HUBPOWER
   +INVENTORY.PERSISTENTPOWER
}

actor HarnessMorphine : PowerProtection /// Damage resistance, given via script.
{
   Powerup.Duration -60
   DamageFactor "Normal", 0.7
   +INVENTORY.HUBPOWER
   +INVENTORY.PERSISTENTPOWER
}

actor MedicalHarness : CustomInventory
{
   Inventory.PickupMessage "Picked up a Medical Harness!"
   Tag "MT-12B Medical Harness (Heals below 20 HP, morphine below 30 HP)"
   Inventory.Icon "MDHRB0"
   Inventory.Amount 1
   Inventory.MaxAmount 1
   +AUTOACTIVATE
   +INVENTORY.INVBAR
   States
  {
   Spawn:
     MDHR A -1
     Loop
   Pickup:
     MDHR A 0 A_GiveInventory("HarnessToken",1) /// Token would probably be necessary for the health script to function.
     MDHR A 0 A_GiveInventory("MorphineToken",1) /// Ditto
     Stop
  }
}

actor HarnessToken : Inventory /// Token for healing
{
   Inventory.MaxAmount 1
}

actor MorphineToken : Inventory /// Token for morphine
{
   Inventory.MaxAmount 1
}


A script was written for me for handling a weapon limit system, and I've reverse engineered that to work with similar items that grants permanent damage resistance and increases run speed/jump height, respectively. I started working a similar script based off that, but quickly realized that I have no idea what I am doing here.

Any help would be much appreciated!
CaptainNurbles
 
Joined: 27 Jan 2018
Location: The Deepest Reaches of Space
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia (Modern GZDoom)

Re: Item That Heals Below Certain Health and Others

Postby Player701 » Wed Jan 13, 2021 2:04 am

With DECORATE, this would probably require an ACS script, and once again I have to note that ACS should no longer be used for game logic now that we have the full power of ZScript in our hands.

The following ZScript code should handle most, if not all of your use cases. But please take the following into account:

  1. Item will be lost upon respawning if the "lose entire inventory" dmflag (gameplay option) is enabled. As far as I'm aware, there's no way (and probably no need) to work around this.
  2. Morphine protection will be lost upon respawning unless the "keep powerups" dmflag is enabled. This can be worked around by integrating the protection functionality into the main item. If necessary, I can update the code accordingly.
  3. Combining HUBPOWER and PERSISTENTPOWER is unnecessary, since PERSISTENTPOWER will always take effect, including across hub level transitions.
  4. Also note that your original item has INVBAR and AUTOACTIVATE flags. This means the item will be placed in the inventory bar but then immediately activated, so it doesn't appear that you actually need INVBAR or an icon for your item. If I haven't understood your original intent correctly, please tell me how exactly you want the item to be activated, and I'll fix the code.

Code: Select allExpand view
class MedicalHarness : Inventory
{
     // 3 minutes
    const MORPHINE_INTERVAL_SECONDS = 180;

    // Timestamp of last morphine injection (0 if not used before)
    private int _lastMorphineTime;

    Default
    {
        Tag "MT-12B Medical Harness (Heals below 20 HP, morphine below 30 HP)";

        Inventory.Amount 1;
        Inventory.MaxAmount 1;
        Inventory.PickupMessage "Picked up a Medical Harness!";
    }

    States
    {
        Spawn:
            MDHR A -1;
            Loop;       
    }

    override void DoEffect()
    {
        if (Owner.Health <= 0)
        {
            // Dead
            return;
        }

        // Regeneration
        if ((Level.maptime % GameTicRate) == 0)
        {
            Owner.GiveBody(1, 20);
        }

        // Morphine
        if (Owner.Health < 30 && CanGiveMorphine())
        {
            Owner.GiveInventory('HarnessMorphine', 1);
            _lastMorphineTime = Level.totaltime;
        }
    }

    private bool CanGiveMorphine()
    {
        if (_lastMorphineTime == 0)
        {
            // Used for the first time
            return true;
        }

        // Check how much time passed since last activation
        int timePassed = Level.totaltime - _lastMorphineTime;
        return timePassed >= MORPHINE_INTERVAL_SECONDS * GameTicRate;
    }
}

class HarnessMorphine : PowerProtection
{
    Default
    {
        Powerup.Duration -60;
        DamageFactor "Normal", 0.7;
        +INVENTORY.PERSISTENTPOWER;
    }
}

If you encounter any kind of problems with the above code, please let me know.
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: Item That Heals Below Certain Health and Others

Postby CaptainNurbles » Wed Jan 13, 2021 9:53 am

This works perfectly! Many thanks to ya, pal.
CaptainNurbles
 
Joined: 27 Jan 2018
Location: The Deepest Reaches of Space
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia (Modern GZDoom)


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests