• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Ammunition on Items as Weapon

Status
Not open for further replies.
Level 8
Joined
Jun 26, 2019
Messages
318
Hello all. I am thinking. I want to have ammunition on items that are weapons. The item weapons are actually ability, but being equipped into the inventory on heros. I am thinking, I want each item weapon to have 1 mana cost at each attack that shooting out of weapon for until the max mana runs out and have to go get reloaded to get more ammunition. I want the ammunition to be based on mana cost. How do you do this in trigger? Thanks.
 

Uncle

Warcraft Moderator
Level 63
Joined
Aug 10, 2018
Messages
6,456
The above trigger has a lot of issues since it's going on an order event. An order can be issued from anywhere, regardless of distance, and occurs well before the unit even begins it's attack.

I attached a map with a simple design you could use, it seems to work pretty well. Note that it's not currently MUI but it's an easy feature to implement. It'll also run into problems with multiple guns but again this is easy to fix, I just wanted to create something simple for now.

  • Ammo Setup
    • Events
      • Map initialization
    • Conditions
    • Actions
      • -------- Configure: --------
      • Set VariableSet Ammo_Gun_Item = Gun Item
      • Set VariableSet Ammo_Gun_Ability = Gun Ability
      • Set VariableSet Ammo_Disarm_Ability = Cargo Hold (Orc Burrow)
      • Set VariableSet Ammo_Reload_Ability = Reload
      • -------- --------
      • -------- You want your units that use this system to be disarmed by default (if they don't start out with a gun) --------
      • Unit - Add Ammo_Disarm_Ability to Rifleman 0000 <gen>
  • Ammo Acquire Gun
    • Events
      • Unit - A unit Acquires an item
    • Conditions
      • (Item-type of (Item being manipulated)) Equal to Ammo_Gun_Item
    • Actions
      • -------- Set Max Mana (Ammo) - The Item's Priority field in the Object Editor determines it's Max Ammo: --------
      • Unit - Set Max Mana of (Triggering unit) to (Item: (Item being manipulated)'s Integer Field: Priority ('ipri'))
      • Unit - Set mana of (Triggering unit) to (Real((Charges remaining in (Item carried by (Triggering unit) of type Ammo_Gun_Item))))
      • -------- --------
      • -------- Check Mana and either Remove Disarm or Add Reload: --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Mana of (Triggering unit)) Greater than 0.00
        • Then - Actions
          • Unit - Remove Ammo_Disarm_Ability from (Triggering unit)
        • Else - Actions
          • -------- Add Reload: --------
          • Unit - Add Ammo_Reload_Ability to (Triggering unit)
      • -------- --------
      • -------- Add Gun Ability: --------
      • Unit - Add Ammo_Gun_Ability to (Triggering unit)
      • Unit - Order (Triggering unit) to Night Elf Priestess Of The Moon - Activate Searing Arrows.
  • Ammo Lose Gun
    • Events
      • Unit - A unit Loses an item
    • Conditions
      • (Item-type of (Item being manipulated)) Equal to Ammo_Gun_Item
    • Actions
      • -------- Remove Gun Ability: --------
      • Unit - Remove Ammo_Gun_Ability from (Triggering unit)
      • -------- --------
      • -------- Remove Reload: --------
      • Unit - Remove Ammo_Reload_Ability from (Triggering unit)
      • -------- --------
      • -------- Add Disarm: --------
      • Unit - Add Ammo_Disarm_Ability to (Triggering unit)
      • -------- --------
      • -------- Remove Max Mana (Ammo): --------
      • Unit - Set Max Mana of (Triggering unit) to 0
  • Ammo Fire Gun
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • ((Attacking unit) has an item of type Ammo_Gun_Item) Equal to True
    • Actions
      • Custom script: local unit u = GetAttacker()
      • Custom script: local item i = GetItemOfTypeFromUnitBJ(u, udg_Ammo_Gun_Item)
      • -------- --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Mana of (Attacking unit)) Greater than 0.00
        • Then - Actions
          • -------- This prevents the Item from being dropped while attacking: --------
          • Custom script: call SetItemDroppable(i, false)
          • -------- --------
          • -------- This Wait should be long enough for the unit to have spent it's mana on the Gun Ability (Searing Arrows) --------
          • Wait 0.25 seconds
          • -------- --------
          • -------- Adjust Item Charges (Ammo) --------
          • Custom script: call SetItemCharges( i, R2I(GetUnitStateSwap(UNIT_STATE_MANA, u)) )
          • -------- --------
          • -------- Allow the Item to be dropped again: --------
          • Custom script: call SetItemDroppable(i, true)
          • -------- --------
          • -------- Check if Mana is 0 and add Disarm/Reload: --------
          • Custom script: if (GetUnitStateSwap(UNIT_STATE_MANA, u) == 0.00) then
          • Custom script: call UnitAddAbility(u, udg_Ammo_Disarm_Ability)
          • Custom script: call UnitAddAbility(u, udg_Ammo_Reload_Ability)
          • Custom script: endif
        • Else - Actions
          • -------- Adjust Item Charges (Ammo) --------
          • Custom script: call SetItemCharges( i, R2I(GetUnitStateSwap(UNIT_STATE_MANA, u)) )
          • -------- --------
          • -------- Add Disarm/Reload: --------
          • Custom script: if (GetUnitStateSwap(UNIT_STATE_MANA, u) == 0.00) then
          • Custom script: call UnitAddAbility(u, udg_Ammo_Disarm_Ability)
          • Custom script: call UnitAddAbility(u, udg_Ammo_Reload_Ability)
          • Custom script: endif
      • -------- --------
      • Custom script: set u = null
      • Custom script: set i = null
  • Ammo Reload
    • Events
      • Unit - A unit Finishes casting an ability
    • Conditions
      • (Ability being cast) Equal to Ammo_Reload_Ability
    • Actions
      • -------- Refill Mana (Ammo) --------
      • Unit - Set mana of (Triggering unit) to 100.00%
      • -------- --------
      • -------- Refill Charges: --------
      • Item - Set charges remaining in (Item carried by (Triggering unit) of type Ammo_Gun_Item) to (Max Mana of (Triggering unit))
      • -------- --------
      • -------- Remove Reload: --------
      • Unit - Remove Ammo_Reload_Ability from (Triggering unit)
      • -------- --------
      • -------- Remove Disarm: --------
      • Unit - Remove Ammo_Disarm_Ability from (Triggering unit)
      • -------- --------
      • -------- Reload Special Effect: --------
      • Special Effect - Create a special effect attached to the origin of (Triggering unit) using Abilities\Spells\Items\AIma\AImaTarget.mdl
      • Special Effect - Destroy (Last created special effect)

How it works:
When the unit acquires a Gun Item I add a hidden Searing Arrows ability to it and order it to activate the ability. This will force the unit to spend mana whenever it launches an attack.

I then check when a unit with the Gun Item attacks, and determine what needs to be done based on their Mana. If the unit has 0 mana then it's out of ammo and is disarmed (can't attack) and given the reload ability (channel to refill ammo and remove disarm).

I manage the Reload/Disarm abilities so that the unit can only ever attack when it has a gun equipped and it has > 0 mana.

To keep track of the ammo on the Gun itself I use charges. This way the player can't drop the Gun and pick it back up to get a "free" reload.

A potential issue that comes to mind is what happens if the unit becomes Silenced. I'm unsure if Silence disables autocast abilities like Searing Arrows but it could cause conflicts with the system. There's ways around this though with custom Silence systems and disabling/enabling abilities.
 

Attachments

  • Ammo 1.w3m
    21.2 KB · Views: 21
Last edited:
Status
Not open for further replies.
Top