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

Calling unit with highest X value

Status
Not open for further replies.
Level 9
Joined
Jul 30, 2018
Messages
445
Hey!

I have a map where I've tried to create a turn based combat. So, I was planning to use the unit's Custom Value for unit's "Initial" as in that speed that determines how often the unit gets a turn.

So far I've made so that each turn all units gain their Custom Value (so their chance to be the next randomly chosen unit for the next turn rises) and once a unit is chosen for the current turn, then I set the Custom Value back to zero again. My plan is to prevent from same unit having consecutive turns (unless intended) and once a unit gets a turn, it's kind of pushed last on the line, and also make certain units have a higher chance to be chosen to get their turn.

So, the question is, is there a way to call a unit with highest Custom Value (or any other value, for that matter)? What I know have is "Pick every unit in (Random 1 units from (Units in Battlefield <gen> matching ((Custom value of (Matching unit)) Greater than or equal to 10))) and do (Actions)".

But my current system is flawed with the fixed threshold, because when there's a lot of units on the game, many slower units will get over it and can be chosen before the faster units, and when there's less units, none might be over the magical line.

The randomness elements is not a necessity. I'd be happy with a system that always picks the unit with highest gained Custom Value (as "faster" units gain it faster, they would be on the top more often).

Thanks already in advance for your advice! :)
 
Level 12
Joined
Jun 15, 2016
Messages
472
There's this ready piece of code which should do what you need: SortGroup [GUI Friendly].

This will allow you to sort all the units you need according to their custom value, then you can pick the one with the highest value. Note that sorting a large amount of units in very short time intervals (i.e. several times a second) can lead to performance issues.
 
Level 39
Joined
Feb 27, 2007
Messages
5,023
IMO the group sort is unnecessary since it only needs to find 1 unit and the OP has to define CV sort for the system to sort by it. This picks the highest CV unit. If multiple units are tied it will pick a random one between them.

  • Set MaxCV = 0
  • Unit Group - Remove all units from CVGroup
  • Custom script: set bj_wantDestroyGroup = true
  • Unit Group - Pick every unit in (Units in Playable Map Area maching ((Matching Unit) is alive equal to true))) and do (Actions)
    • Loop - Actions
      • Set TempCV = Custom Value of (Picked unit)
      • If (All conditions are true) then do (Then Actions) else do (Else actions)
        • If - Conditions
          • TempCV greater than MaxCV
        • Then - Actions
          • Set MaxCV = TempCV
          • Unit Group - Remove all units from CVGroup
          • Unit Group - Add (Picked Unit) to CVGroup
        • Else - Actions
          • If (All conditions are true) then do (Then Actions) else do (Else actions)
            • If - Conditions
              • TempCV equal to MaxCV
            • Then - Actions
              • Unit Group - Add (Picked Unit) to CVGroup
            • Else - Actions
  • Set TurnUnit = Random Unit from CVGroup
 
Level 9
Joined
Jul 30, 2018
Messages
445
Hey!

Thanks for the link, Nowow, but I agree with Pyrogasm, it seems rather complicated for my need.

And Pyrogasm, that seems simple enough. Guess my mistake was to try to do it all in one trigger, but thanks a lot, I’ll try it out tomorrow!

EDIT
Awesome, works perfectly! Thanks again. (And it does work all in one trigger >.< :D )
 
Last edited:
Status
Not open for further replies.
Top