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

Tower Targeting

Status
Not open for further replies.
Level 8
Joined
Aug 13, 2018
Messages
338
Hi again. Could it be possible to set up a system in the game that the towers attack their closest enemy and do not free it until their target is destroyed? (What I want is something like clash of clans, clash royale or boom beach about tower targeting).(The default game is the attack on the weakest).
 
Level 9
Joined
Apr 23, 2011
Messages
527
Simplest way I can think of is to parse units within tower range for each and every tower, detect which one has the least distance and order the tower to attack the unit. Highly intensive for such a simple feature.
 
Level 39
Joined
Feb 27, 2007
Messages
5,025
You can use the "<Specific Unit> Acquires a Target" event to redirect them to the closest target whenever they find something new to attack, but it will need to be manually added to your trigger for every tower in your map. If towers can be removed or die then this becomes a real memory hog over time unless you clear the trigger and remake it with out all the events for dead units. I'm not gonna write how to do that part unless you seriously attempt this approach because it requires some JASS Custom scripts because GUI can't clear and remake triggers.

  • Events
    • Unit - A unit enters (playable map area)
  • Conditions
    • ((Entering Unit) is a Structure) equal to true
    • -------- Other conditions may be necessary if in your map there are other buildable structures that are not towers --------
  • Actions
    • Trigger - Add to NEXT_TRIGGER_IN_THIS_POST <gen> the event (Unit - (Triggering Unit) Acquires a target)
  • Events
    • -------- none because we add them with the other trigger --------
  • Conditions
    • -------- none needed because only specific units trigger this --------
  • Actions
    • Set TempPoint = Position of (Triggering Unit)
    • Set MIN_DIST = Current Acquisition range of (Triggering Unit)
    • Set TOWER_TARGET = (no unit)
    • Custom script: set bj_wantDestroyGroup = true //cleaning the unit group leak
    • Unit Group - Pick every unit in (Units within MIN_DIST of TempPoint Matching (((Matching Unit) belongs to an enemy of (Owner of (Triggering Unit)) equal to true) and ((Matching Unit) is alive equal to true)) and ((Matching Unit) is Invulnerable equal to false))) and do (Actions)
      • Loop - Actions
        • Set TempPoint2 = (Position of (Picked Unit))
        • Set DIST = Distance between TempPoint and TempPoint2
        • If (All conditions are true) then do (Then actions) else do (Else actions)
          • If - Conditions
            • DIST less than or equal to MIN_DIST
          • Then - Actions
            • Set MIN_DIST = DIST
            • Set TOWER_TARGET = (Picked Unit)
          • Else - Actions
        • Custom script: call RemoveLocation(udg_TempPoint2) //make sure this matches the name of your variable, but keep the udg_
    • Custom script: call RemoveLocation(udg_TempPoint) //same here, keep the udg_
    • Trigger - Disable (this trigger) //these lines may be necessary to avoid infinite loops or they may not be, I didn't test
    • Unit - Order (Triggering Unit) to Attack TOWER_TARGET
    • Trigger - Enable (this trigger)
 
Level 39
Joined
Feb 27, 2007
Messages
5,025
"Unit acquires a target" doesn't fire when you manually order a unit to attack something. Otherwise it does exactly as specified. When a unit finds a target on its own it will always pick the closest possible target.
 
Level 8
Joined
Oct 4, 2016
Messages
208
If the tower is owned by a AI player you can try removing guard position.

RemoveGuardPosition(unit)

I don't remember the action in GUI but is in AI section.

Remove guard position makes that the AI don't gives orders to the units (except for casting spells)
 
If your towers are prelaced (in Editor placed) they will not fire the "enter map" event. In that case one would have to loop all units on the map, and add wanted units at map start.

Edit: Also one can't order the tower directly in the "ACQUIRED_TARGET Event" there has to be a short delay. At least I did that in some map of mine where I used this event for towers and minions.
 
Last edited:
Level 39
Joined
Feb 27, 2007
Messages
5,025
How can i do it?
If the tower is owned by a AI player you can try removing guard position.
When someone says "you could do <thing>", please try doing <thing> or finding how to do <thing> or looking for <thing> instead of immediately asking "how" again. Show some initiative and you will learn more quickly than when you are just being spoonfed answers. However since I'm reasonably confident you won't look for yourself, I'll throw you a bone: you have two options, listed below.
  • AI - Ignore <specific tower>'s guard position
  • AI - Ignore the guard positions of all <your computer player that owns the towers> units
The first works for a single unit, the second works for all units owned by the player. As Tasyen suggested, the first option requires you to do it for each and every tower on your map, including those that were preplaced. This is not exceptionally difficult, but it may be more work than you have to do if you can just use option 2. Depending on how your map is set up, though, option 2 may cause other computer units to have behavior you don't like, and will entirely break any Melee AI you have set up for the map if the AI isn't triggered manually. Try 2; if it causes undesirable behavior, refer back to this thread for how to make 1 work. (Hint, both things you need to know were posted by Tasyen and myself in each of our first posts in this thread.)
 
Level 39
Joined
Feb 27, 2007
Messages
5,025
That's not how you learn anything. Persevere and you'll get it. If you explain which players own the towers and if they have other units whose AI needs to be preserved, it becomes easy to suggest what you should test.
 
Status
Not open for further replies.
Top