• 🏆 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] Trigger to detect closest unit of type

Status
Not open for further replies.
Level 19
Joined
Sep 4, 2007
Messages
2,826
I'm currently trying to reduce the microing in my map and trying to make the map more userfriendly. The problem is that I'm having trouble ordering my ships to find/go to the nearest unit of type which in this case is a Dock/shipyard.(They'll be all over the map)
Please help I'll give credit + rep.
Thanks in advance.
 
Level 17
Joined
Apr 13, 2008
Messages
1,597
Yay, since I was the one who suggested the automatization of oil ships :p I must answer.

There is no function to get the closest unit of a type.
You'll have to write one.
Well, I think it's solvable in GUI but it would be much easier in JASS.
If you know JASS then there is a GetClosestUnit() function allready in the JASS section made by DiscipleOfLife.

If you don't you should learn it, I like your maps. If you don't want well.. It should be a loop in a loop. The outer loop would change the distance you check around the oil tanker unit and pick the pick all units in the distance then save it to a variable. The inner loop would take the group from the outer loop and it would check if the first unit in the group is a Shipyard or not. If it's not a shipyard then remove the first unit in the group from the group. If it's a shipyard then save this unit in a variable then exit from the loops.
There you go, your closest unit.

But to tell you the truth, it's not the optimal solution for your problem. I would rather save the location of each players' shipyards to a variable and just do some very simple math to figure out which one is the closest. That would require less processing from the computer, less coding and thinking from you.
 
If you would do this in GUI you could just make a Periodic Timer as event and make your units attack-move to random unit of type battleship owned by "enemy group"..

Then the battleships would act realistic and always attacking eachother :)

If it sounds interesting I can give you a couple of tips, BUT I cant make the ships attack the closest one, you have to go to JASS for that, but I think that would be a instable JASS code, what happends if 2 ships are as close to yours.. Your ship would simply be confused or wc3 crash maybe :/
 
Level 19
Joined
Sep 4, 2007
Messages
2,826
Yay, since I was the one who suggested the automatization of oil ships :p I must answer.

There is no function to get the closest unit of a type.
You'll have to write one.
Well, I think it's solvable in GUI but it would be much easier in JASS.
If you know JASS then there is a GetClosestUnit() function allready in the JASS section made by DiscipleOfLife.

If you don't you should learn it, I like your maps. If you don't want well.. It should be a loop in a loop. The outer loop would change the distance you check around the oil tanker unit and pick the pick all units in the distance then save it to a variable. The inner loop would take the group from the outer loop and it would check if the first unit in the group is a Shipyard or not. If it's not a shipyard then remove the first unit in the group from the group. If it's a shipyard then save this unit in a variable then exit from the loops.
There you go, your closest unit.

But to tell you the truth, it's not the optimal solution for your problem. I would rather save the location of each players' shipyards to a variable and just do some very simple math to figure out which one is the closest. That would require less processing from the computer, less coding and thinking from you.

You mean like the user shall manually store the shipyard and the oil rig then a trigger will do the rest?
ReDbEaN87, I'm not really going to attack ships around my map. Though thanks for trying +rep.
 
Level 17
Joined
Apr 13, 2008
Messages
1,597
Er, sorry, I posted this immediately, but I couldn't communicate with Hive's server. So here is the post:

No, I meant that when a player finishes constructing a shipyard you should save the shipyard's location in a variable. When the Oil Tanker is full of oil you compare it's location with all the player owned shipyards' locations and order it to go to the closest.

OR

Your idea is good too. You give the oil tanker an ability that can only be casted on Shipyards and you save that location in a variable and always redirect the Oil Tanker here when it's full with oil.

OR

You simply make Oil rigs gold mines. You give the Oil Tankers the return resource ability and shipyards gold dropoff ability.

Choice 3 is the easiest. But Oil Rigs are going to have the gold icon thingy and when oil tankers return the "oil" the player will get gold, you can deal with it by simply decreasing the players gold when it happens, but I'm not sure if you can remove the +gold text. It wouldn't hurt my eye, but it's your map, your call.
 
Level 19
Joined
Sep 4, 2007
Messages
2,826
Er, sorry, I posted this immediately, but I couldn't communicate with Hive's server. So here is the post:

No, I meant that when a player finishes constructing a shipyard you should save the shipyard's location in a variable. When the Oil Tanker is full of oil you compare it's location with all the player owned shipyards' locations and order it to go to the closest.

OR

Your idea is good too. You give the oil tanker an ability that can only be casted on Shipyards and you save that location in a variable and always redirect the Oil Tanker here when it's full with oil.

OR

You simply make Oil rigs gold mines. You give the Oil Tankers the return resource ability and shipyards gold dropoff ability.

Choice 3 is the easiest. But Oil Rigs are going to have the gold icon thingy and when oil tankers return the "oil" the player will get gold, you can deal with it by simply decreasing the players gold when it happens, but I'm not sure if you can remove the +gold text. It wouldn't hurt my eye, but it's your map, your call.

I thought about the same thing some days ago but I forgot all about it. Thanks for reminding me.
I'll give you credit and +rep.
 
  • Closest Unit of Type
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Detect Goblin Shipyard
    • Actions
      • Set TempLocA = (Position of (Casting unit))
      • Set TempRealB = 1000000000.00
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in (Playable map area) matching ((((Matching unit) is alive) Equal to True) and ((((Matching unit) belongs to an ally of (Owner of (Casting unit))) Equal to True) and ((Unit-type of (Matching unit)) Equal to Goblin Shipyard)))) and do (Actions)
        • Loop - Actions
          • Set TempLocB = (Position of (Picked unit))
          • Set TempRealA = (Distance between TempLocA and TempLocB)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • TempRealA Less than TempRealB
            • Then - Actions
              • Custom script: set udg_TempUnitA = null
              • Set TempUnitA = (Picked unit)
              • Set TempRealB = TempRealA
            • Else - Actions
          • Custom script: call RemoveLocation(udg_TempLocB)
      • Custom script: call RemoveLocation(udg_TempLocA)
      • -------- So in the end TempUnitA will be the closest Goblin Shipyard that is allied to your unit. --------
I know that TempUnitA leaks in the end, but I didnt null it because it needs to be used in the end of the trigger.
 
Status
Not open for further replies.
Top