[snippet] Get Units in Line

Scripts

13 years
edited 8 years
Description


This small library allows users to get the units in a line. This line can be defined by 2 points or 2 widgets.

Requirements:


- GeometryLibBase
- Select Cohadar's jasshelper in JNGP 5e and up

Actual Code

Code (jass) Select
/******************************************************************
*                     GROUP UNITS IN LINE v1.0                    *
*                           By moyack                             *
*                              2012                               *
*              ===================================                *
*              Exclusive resource from wc3jass.com                *
*              ===================================                *
******************************************************************/
library GetUnitsInLine requires GeometryLibBase
/* This library catch all the units in a line. This line however,
can be defined in several ways, each one will offer a function for
that purpose:
* Between 2 points:  GetUnitsInLine_byPoints()
* Between 2 widgets: GetUnitsInLine_byWidgets()

The first function takes as arguments a group variable and 4 reals
which are the X/Y coordinates of the first and second point.

The second function takes as arguments a group variable and 2 widgets,
these widgets can be any combination of units, items and destructables.

The group variable for both functions will store the units catched in
the line defined by points or widgets, so it's advisable that this group
should be empty.
*/
globals
    private constant real SPREAD = 30. // this value defines how wide is the line of units selected
    private group G = CreateGroup()
endglobals

public function byPoints takes group g, real x1, real y1, real x2, real y2 returns nothing
    local unit u
    local real a
    local real b
    local real x = 0.5 * (x1 + x2)
    local real y = 0.5 * (y1 + y2)
    if g == null then
        return
    endif
    call GroupEnumUnitsInRange(G, x, y, GetDistance(x, y, x1, y1) + SPREAD, null)
    for u in G
        if x2 - x1 == 0. then // manages vertical lines
            set x = x1
            set y = GetUnitY(u)
        else
            set x = GetUnitX(u)
            set a = (y2 - y1) / (x2 - x1)
            set b = -a * x1 + y1
            set y = a * x + b
        endif
        if IsUnitInRangeXY(u, x, y, SPREAD) then
            call GroupAddUnit(g, u)
        endif
    endfor
endfunction

public function byWidgets takes group g, widget A, widget B returns nothing
    call byPoints(g, GetWidgetX(A), GetWidgetY(A), GetWidgetX(B), GetWidgetY(B))
endfunction

endlibrary


Changelog:
1.0: Initial release.

Feel free to comment.
13 years
Hmm tested the map and its a hell of fun! (Just talking about the test system on the test map) actually you should make a mini-game map with those codes. XD

Anyway, where is the rating system? O.o
13 years
Quote from: sonofjay on January 03, 2013, 03:23:30 AM
Hmm tested the map and its a hell of fun! (Just talking about the test system on the test map) actually you should make a mini-game map with those codes. XD
Sure!!! why not??

QuoteAnyway, where is the rating system? O.o
When it gets approved you can rate it :)
13 years
Nice.
This requires cohadar's JassHelper I see :o

Not a lot of people are using that, so I guess it should be mentioned at least :p
13 years
Hoho, nice to see you back Mag since your last visit was December 12..
13 years
Yeah, I apologize for that.
JASS Class on Hive is taking up a lot of time :v
I don't even have time to take shits now.
13 years
Approved!