Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[Snippet] Group Units in line, with distance and optional spread-value No New Posts Codes & Snippets

Started by
rvonsonsnadtz

0 Members and 1 Guest are viewing this topic.

Category: Units, Variables
Language: vJASS

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
  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
  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.  call LineGroup(g, GetUnitX(u), GetUnitY(u), GetLocationX(loc), GetLocationY(loc), 800, 75, 15*bj_DEGTORAD, function SomeFilter)
  5.  /*Do the stuff with the group*/
  6.  call DestroyGoup(g)
  7.  call RemoveLocation(loc)
  8. endfunction
  9.  

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: December 19, 2017, 07:50:43 PM by moyack »



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.



Allright, thanks! (:
However, isn't library a vJass-function?




Allright, thanks! (:
However, isn't library a vJass-function?
hmm, yes ... why??



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.




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 ^^



 

Started by PitzerMike

Replies: 0
Views: 1638
Codes & Snippets

Started by Purgeandfire

Replies: 0
Views: 1714
Codes & Snippets

Started by Bribe

Replies: 0
Views: 1969
Codes & Snippets

Started by moyack

Replies: 0
Views: 9880
Codes & Snippets

Started by Magtheridon96

Replies: 1
Views: 8663
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...