• 🏆 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!

[Trigger] Help Detecting when fighting is over

Status
Not open for further replies.
Level 2
Joined
Jan 23, 2020
Messages
3
I am making an AI for a hero.
Im using knowledge i've learned from this tutorial.

The AI has two parts. Attack and defend. The AI will decide wether or not to attack depending on wether or not it is defending.

The hero will defend when ever a unit of the same team is getting attacked.

I am using a seperate trigger to detect when a unit is getting attacked (when hero needs to defend). Checking when a unit on the same team is attacked is not a problem, the problem is when it isn't attacked anymore.

Main state:
  • AI Mainstate
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Set VariableSet AIUnitGroup = (Units owned by Player 2 (Blue) matching ((((Matching unit) is alive) Equal to True) and ((Unit-type of (Matching unit)) Equal to Hero 1)).)
      • Unit Group - Pick every unit in AIUnitGroup and do (Actions)
        • Loop - Actions
          • -------- Defend --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • AIDefend Equal to True
            • Then - Actions
              • Trigger - Run AI Defend <gen> (checking conditions)
            • Else - Actions
          • -------- Charge --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • AIDefend Not equal to True
              • AIAttack Not equal to True
            • Then - Actions
              • Trigger - Run AI Attack <gen> (checking conditions)
            • Else - Actions
          • -------- Back to Charge --------
      • Custom script: call DestroyGroup(udg_AIUnitGroup)
Attack trigger:

  • AI Attack
    • Events
    • Conditions
    • Actions
      • Set VariableSet AIDefend = False
      • Set VariableSet AIAttack = True
      • Set VariableSet AI_Loc = (Center of Town 2 Claim <gen>)
      • Unit - Order (Picked unit) to Move To AI_Loc
      • Custom script: call RemoveLocation(udg_AI_Loc)
Defend trigger:

  • AI Defend
    • Events
    • Conditions
    • Actions
      • Set VariableSet AIAttack = False
      • Set VariableSet AIUnitGroupGrunts = (Units within 512.00 of (Position of (Picked unit)).)
      • Unit - Order (Picked unit) to Attack-Move To AI_Loc
      • Unit Group - Order AIUnitGroupGrunts to Attack-Move To AI_Loc
      • Custom script: call DestroyGroup(udg_AIUnitGroupGrunts)
      • Custom script: call RemoveLocation(udg_AI_Loc)
Attack detector: (ignore the horrible trigger name)

  • AI Attacked state
    • Events
      • Unit - A unit owned by Player 2 (Blue) Is attacked
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • AIDefend Not equal to True
        • Then - Actions
          • Set VariableSet AIDefend = True
          • Set VariableSet AI_Loc = (Position of (Attacked unit))
        • Else - Actions
          • Wait 20.00 seconds
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • AIDefend Equal to True
              • ((Region centered at (Position of (Attacked unit)) with size (700.00, 700.00)) contains (Attacking unit)) Equal to False
            • Then - Actions
              • Trigger - Run AI Attack <gen> (checking conditions)
            • Else - Actions
I tried by checking wether or not the attacking unit was in a radius close to the attacked unit, but that doesn't seem to do the trick.
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,188
You need to track all units that are attacked and have them stop being tracked after not taking damage for some amount of time. If no units are tracked then you go onto attacking, otherwise defend. Units that are attacked have to have the timeout reset if they are already tracked.

Such a tracking system could be implemented by a set of arrays corresponding to each tracked unit. On top of the unit, a counter could be used to track the time left to track the unit. Every second all tracked units have the counter decremented by 1 and if the counter is 0 they get removed from being tracked. Every time an ally unit is attacked it gets checked for being tracked and if already tracked it has its counter reset to some timeout, otherwise it is added to being tracked. For performance hashtable or other methods of mapping units to indices might be required.
 
Status
Not open for further replies.
Top