Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [Snippet] Group Units in line, with distance and optional spread-value  (Read 11529 times)

  • Rookie - level 3
  • ***
  • Posts: 128
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 11
  • User
    • View Profile
Introduction

Before I start, I would just like to credit Moyack for patience with testing and pointing out certain flaws, especially with how the code is presented.

Description

I have created a function, that can group units in line with; a distance-value that is unchanged no matter where the end-point may be, a radius-value, a optional spread-value (determined in radians, easiest way to get the spread-angle you want is to create a constant with the spreadangle * bj_DEGTORAD) and an optional boolean exp-filter.

Requirements

 - vJass


Actual Code

Code: jass  [Select]
  1. library LineGroup /* library LineGroup, used to group units in a line with a optional boolexpr-filter.
  2.  
  3. §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
  4.  
  5.     This resourse is a [url=http://www.wc3jass.com]www.wc3jass.com[/url] exclusive resource. Please PM RVonSonSnadtz there
  6.     or in-game should you ever have a problem with this or have ideas of improvments.
  7.    
  8. ===============================================================================================
  9.  
  10.     Extra thanks to Moyack, for troubleshooting and general code-help.
  11.    
  12. ===============================================================================================
  13.  
  14.     Requirements:
  15.         vJass (haven't tried it with regular Jass, might work. But GUI is definetely not recommended
  16.         due to the lack of control over handles such as the groups
  17.    
  18.  
  19.     How to use?
  20.         Copy the trigger and paste it in your map, or copy the contents from here into an empty
  21.         trigger in your map.
  22.        
  23.         It needs an already existing group to function, and it wont empty the group by itself
  24.         (this could of course be used in certain situations, and is an important knowing.)
  25.        
  26. ================================================================================================
  27.  
  28.     Feel free to use this resource, but please give some creds atleast (:
  29.  
  30. §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§*/
  31.  
  32.  
  33. globals
  34.     private constant group GR = CreateGroup() //a constant unitgroup to avoid all CreateGroup, DestroyGroup and gr=null stuff
  35.     constant real RV_pi2 = Acos(0) //A half-pi, should not be changed
  36.     private constant real UNITEXTRASIZE = 16 //This number could be played with to fit your needs, it is basically just a number to make the radius-value more linear. Otherwise, only units with their centers in the cone passes the check.
  37. endglobals
  38.  
  39. private function LineGroupFunc takes group g, real startx, real starty, real endx, real endy, real distance, real radius, real spread, boolexpr bex returns nothing
  40.     local real tx = endx - startx
  41.     local real ty = endy - starty
  42.     local real distr1 = SquareRoot(ty*ty+tx*tx)
  43.     local real distr2
  44.     local real angle = Atan2(ty,tx)
  45.     local real dx = (radius + UNITEXTRASIZE) * Cos(angle + RV_pi2)
  46.     local real dy = (radius + UNITEXTRASIZE) * Sin(angle + RV_pi2)
  47.     local real fx = tx/distr1
  48.     local real fy = ty/distr1
  49.     local real ex
  50.     local real ey
  51.     local unit u
  52.     local real ex2
  53.     local real ey2
  54.     call GroupEnumUnitsInRange(GR, startx + fx * distance/2, starty + fy * distance/2, distance/2 + UNITEXTRASIZE , bex)
  55.     if tx > 0 then
  56.         for u in GR // 0-90 deg (also 180-270 in reverse)
  57.             set ex = Atan2(GetUnitY(u) - starty + dy, GetUnitX(u) - startx + dx) + spread
  58.             set ex2 = Atan2(GetUnitY(u) - starty - dy, GetUnitX(u) - startx - dx) - spread
  59.             if ex > angle and ex2 < angle then
  60.                    call GroupAddUnit(g, u)
  61.             endif
  62.         endfor
  63.     else
  64.         for u in GR // 90-180 deg (also 270-360 in reverse)
  65.             set ex = Atan2(GetUnitY(u)*-1 + starty - dy, GetUnitX(u)*-1 + startx - dx) + spread
  66.             set ex2 = Atan2(GetUnitY(u)*-1 + starty + dy, GetUnitX(u)*-1 + startx + dx) - spread
  67.             if ex > angle - RV_pi2*2 and ex2 < angle - RV_pi2*2 then
  68.                 call GroupAddUnit(g, u)
  69.             endif
  70.         endfor
  71.     endif
  72. endfunction
  73.  
  74. function LineGroup takes group g, real startx, real starty, real endx, real endy, real distance, real radius, real spread, boolexpr bex returns nothing //spread is declared with angle in radians.
  75.     local real rx = startx - endx //This is the function to call. To use it, create a group and call this function with the group.
  76.     local real ry = starty - endy
  77.     local real dist = SquareRoot(rx*rx+ry*ry)
  78.     set rx = rx / dist
  79.     set ry = ry / dist
  80.     if starty > endy then
  81.         set endx = startx - rx * distance
  82.         set endy = starty - ry * distance
  83.         call LineGroupFunc(g, endx, endy, startx, starty, distance, radius + Tan(spread)*distance, spread * -1, bex)
  84.     else
  85.         call LineGroupFunc(g, startx, starty, endx, endy, distance, radius, spread, bex)
  86.     endif
  87. endfunction
  88.  
  89. endlibrary
  90.  

What it does: it takes a group to fill (a group already existing), 4 coordinates (startx, starty, endx, endy), a real value distance (how far will this line go from the start-coordinates. It could reach and go further than the end-point or it could not get that far at all), a real angle expressed in radians that tells if and how much the line should change as it goes further (0.262 would give a 15~deg spread in angle, 0 will give a straight line) and a boolexpr "bex", this to actually lessen the cpu-usage in some cases, as the more units getting filtered away with the boolexpr, the less units it has to calculate for (just set this for null if you want it to take all units in the line)

How to import: Easiest way; download the testmap, open it with the NGJP-world editor, find the trigger named LineGroup and copy it, and paste it in your map. Alt. copy all text inside the LineGroup-trig and paste it in an empty text-trigger.

How to use:
Create a group with locals or use a group from an global variable, then call LineGroup with the group as the first value, and the rest with the values in the order told at a number of places in this text.


Sample Code

Code: jass  [Select]
  1. function TestGroupLineBlaBla takes nothing returns nothing
  2. local group g = CreateGroup //You need to have an already created goup in here.
  3. local unit u = GetTriggerUnit()
  4. local location loc = GetSpellTargetLoc()
  5.  call LineGroup(g, GetUnitX(u), GetUnitY(u), GetLocationX(loc), GetLocationY(loc), 800, 75, 15*bj_DEGTORAD, function SomeFilter)
  6.  /*Do the stuff with the group*/
  7.  call DestroyGoup(g)
  8.  call RemoveLocation(loc)
  9. endfunction
  10.  

And a little extra, the code won't empty the group itself, so you could make some special stuff, like fan-shapes and stuff (this might require some further math, if someone is interested, I could show how.)

If you find any trouble with this, know a way to improve it or just have a question, just ask in this thread or PM me.
(the map have a non-other-than-me-user-friendly version of the TickTack, or ChainTimer as I have comed to call it. Dont mind that.. )
« Last Edit: March 14, 2013, 08:48:58 AM by rvonsonsnadtz »

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 983
  • WC3 Models: 15
  • WC3 Tutorials: 12
  • WC3 Tools: 10
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 5
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Group Units in line, with distance and optional spread-value
« Reply #1 on: March 13, 2013, 08:30:12 AM »
Ok, renamed the topic to ad the Snippet category and I gave some small help in formatting.

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

  • Rookie - level 3
  • ***
  • Posts: 128
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 11
  • User
    • View Profile
[snippet] Group Units in line, with distance and optional spread-value
« Reply #2 on: March 13, 2013, 08:55:27 AM »
Allright, thanks! (:
However, isn't library a vJass-function?

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 983
  • WC3 Models: 15
  • WC3 Tutorials: 12
  • WC3 Tools: 10
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 5
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
[snippet] Group Units in line, with distance and optional spread-value
« Reply #3 on: March 13, 2013, 09:04:35 AM »
Allright, thanks! (:
However, isn't library a vJass-function?
hmm, yes ... why??

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
[snippet] Group Units in line, with distance and optional spread-value
« Reply #4 on: March 13, 2013, 12:38:25 PM »
It was a pretty good idea of you to allow the user to pass in his own group.
This makes memory management easier because he'd see the CreateGroup() and the respective DestroyGroup(g) in his own code.

The group doesn't need to be constant though.
If it's constant, the name should be GR :p

Either way, approved.

  • Rookie - level 3
  • ***
  • Posts: 128
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 11
  • User
    • View Profile
[Snippet] Group Units in line, with distance and optional spread-value
« Reply #5 on: March 13, 2013, 06:52:16 PM »
hmm, yes ... why??
Just thought that I should add vJass as a requirement then (:

It was a pretty good idea of you to allow the user to pass in his own group.
This makes memory management easier because he'd see the CreateGroup() and the respective DestroyGroup(g) in his own code.

The group doesn't need to be constant though.
If it's constant, the name should be GR :p

Either way, approved.
Thanks! (:
I could change the name of gr to GR, I do personally think a constant group works better than a ordinary variable one ^^

 

[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 21095
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 25941
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 8784
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7471
Last post January 09, 2012, 07:03:32 PM
by moyack
[Snippet] Parabolic Function

Started by moyackBoard Codes & Snippets

Replies: 1
Views: 7118
Last post January 13, 2012, 12:11:45 PM
by Magtheridon96
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!