(Keeps Hive Alive)
Go Back   The Hive Workshop - A Warcraft III Modding Site > Warcraft III Resources > JASS Functions

JASS Functions Approved JASS functions will be located here.
Remember to submit your own resources to the submission forum.

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2007, 07:25 PM   #1 (permalink)

iRawr
 
Join Date: Dec 2005
Posts: 8,349

PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)

Paired Mapping Contest #4 Winner: Fallen Angel - Lucifer's Keep Respected User: This user has been given the respected user award. Map Development Mini-Contest #1 Winner: Stand of the Elements 

Small Code Snippets

To keep out a potential deluge of threads with very small functions, I've created this thread for you to post relatively short functions you wish to share.

Normal or long length snippets (nearing coding systems or libraries and so on) still go the same place as always.

So, if you have your own special GetClosestUnit, Log, or whatever function, post it here!



D'you guys think I should merge the GetPlayerColorTag functions into here? They sort of fit into either place, due to the fact that there have been several of them presented.



Functions
Get Closest Unit

Silvenon

//Uses global unit udg_ClosestUnit
function GetClosestUnit takes real x, real y, boolexpr e returns unit
    local real md = 100000
    local group g = CreateGroup()
    local unit u
    local real dx
    local real dy
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, e)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        if (dx * dx + dy * dy) / 100000 < md then
            set udg_ClosestUnit = u
            set md = (dx * dx + dy * dy) / 100000
        endif
    endloop
    call DestroyGroup(g)
    call DestroyBoolExpr(e)
    set g = null
    return udg_ClosestUnit
endfunction


quraji

//***************************
//----GetUnitSomewhere by Quraji----
//***************************
//
// To use in your map simply paste this whole code into your custom script
// section, or an empty trigger (above all functions that will use this code)
//
// (feel free to omit this section from your code, I only ask credit if
// you copy this function for your own system/submitted code, no credits
// needed if you use it in a map)
//
// Contains one main function:
//
//
// GetUnitSomewhere takes real sourceX, real sourceY, real range, boolean outsiderange, boolean nearest, boolean considerdead, boolexpr cond returns unit
// Don't let the number of parameters scare you, this is easy to use. This
// function allows you to select the nearest or farthest unit inside or outside
// a designated circle.
//
// Parameters:
//
// real sourceX and real sourceY
// these are the X and Y coordinates of the point at the center of the area
// to be considered.
//
// real range
// this is the range of the function, or the radius of the area to be considered
// (a circle).
//
// boolean outsiderange
// if false, the function will consider units inside the circle, if true it will
// consider all units selected BUT the ones in the circle.
// (remember that the circle's radius is decided by the range parameter)
//
// boolean nearest
// if true, the function will select the nearest unit to the center point inside
// the area to be considered (as you'd expect a normal GetClosestUnit()
// function to do. if false, however, it will select the farthest unit
//
// boolean considerdead
// if false the function will ignore dead units.
// (leave this at false unless you need the function to select recently killed
// units)
//
// boolexpr cond
// finally, this is where you add your own conditions if you need them by
// using a boolexpr. if you don't, pass "Condition(function ReturnTrue)"
//
// function GetUnitSomewhereEx takes real x, real y, real range returns unit
// A simplified version, returns a non-dead unit within range
//
// function GetUnitSomewhereExCond takes real x, real y, real range, boolexpr cond returns unit
// A simplified version, returns a non-dead unit within range, matching condition
//
// Extra feature:
//
// This function sets the bj_lastHauntedGoldMine game variable to the unit
// it selects, as well as returning the unit. This enables JASS users to use
// the function as normal, but provides a convenience for GUI users, as they
// can simply select the unit from the drop down menu for any unit field by
// selecting "Last Haunted Gold Mine". This also allows all variables to be
// nulled. If you wish this variable not to be tampered with, uncomment the
// "return winner" line of code at the bottom of the function.
//**********************

function ReturnTrue takes nothing returns boolean
  return true
endfunction

function GetUnitSomewhere takes real sourceX, real sourceY, real range, boolean outsiderange, boolean nearest, boolean considerdead, boolexpr cond returns unit
 local group g=CreateGroup()
 local unit current
 local unit winner
 local real currentX
 local real currentY
 local real dist
 local real rangeorig = range

   if outsiderange then
     call GroupEnumUnitsInRect(g,bj_mapInitialPlayableArea,cond)
   else
     call GroupEnumUnitsInRange(g,sourceX,sourceY,range,cond)
   endif

   if not nearest then
     set range=.0
   endif

   loop

    set current=FirstOfGroup(g)
     exitwhen current==null

       if GetWidgetLife(current)>0.405 or considerdead then

         set currentX=GetWidgetX(current)-sourceX
         set currentY=GetWidgetY(current)-sourceY
         set dist=SquareRoot(currentX*currentX+currentY*currentY)

         if outsiderange then
           set range=dist
           set outsiderange = not outsiderange
         endif

         if (outsiderange and (dist > rangeorig)) or outsiderange == false then

           if (nearest and dist<=range) or (not nearest and dist>=range) then

             set winner=current
             set range=dist

           endif

         endif

       endif

     call GroupRemoveUnit(g,current)
   endloop

     call DestroyGroup(g)

     set g=null
set current=null

     //return winner
     //uncomment above line if you wish bj_lastHauntedGoldMine to remain
//untampered with

     set bj_lastHauntedGoldMine=winner
     set winner=null

   return bj_lastHauntedGoldMine
endfunction

function GetUnitSomewhereEx takes real x, real y, real range returns unit
  return GetUnitSomewhere(x,y,range,false,true,false, Condition(function ReturnTrue))
endfunction

function GetUnitSomewhereExCond takes real x, real y, real range, boolexpr cond returns unit
  return GetUnitSomewhere(x,y,range,false,true,false,cond)
endfunction


Silvenon, Alternate version (fixes) by PurplePoot

//Uses global group udg_enumGrp
//Uses global unit udg_ClosestUnit
function GetClosestUnit takes real x, real y, boolexpr e returns unit
    local real md = 100000
    local unit u
    local real dx
    local real dy
    call GroupEnumUnitsInRect(udg_enumGrp, bj_mapInitialPlayableArea, e)
    loop
        set u = FirstOfGroup(udg_enumGrp)
        exitwhen u == null
        call GroupRemoveUnit(udg_enumGrp, u)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        if (dx * dx + dy * dy) / 100000 < md then
            set udg_ClosestUnit = u
            set md = (dx * dx + dy * dy) / 100000
        endif
    endloop
    return udg_ClosestUnit
endfunction


Stun Unit

By Fulla:
globals
    integer array StunArray
    integer StunTotal=0
endglobals

struct stundata
    unit u=null
    real d=0
    boolean destroyplease=false
endstruct

function StunUnit_KeepStunned takes nothing returns nothing
    local integer i = 0
    local stundata dat
    loop
        exitwhen i==Stun_Total
        set dat=StunArray[i]
        if dat.destroyplease then
            set Stun_Total=Stun_Total-1
            set StunArray[i]=StunArray[Stun_Total]
            call dat.destroy()
            set i=i-1
else
            set dat.d=dat.d-0.01
            call SetUnitPosition(dat.u,GetUnitX(dat.u),GetUnitY(dat.u))
            set dat.destroyplease=(dat.d<=0)
        endif
        set i=i+1
    endloop
    if Stun_Total==0 then
        call ReleaseTimer(GetExpiredTimer())
    endif
endfunction

function StunUnit takes unit u,real d returns nothing
    local stundata dat=stundata.create()
    set dat.d=d
    set dat.u=u
    if (Stun_Total==0) then
        call TimerStart(NewTimer(),.01,true,function StunUnit_KeepStunned)
    endif
    set Stun_Total=Stun_Total+1
    set StunArray[Stun_Total-1]=dat
endfunction


Preload Sound Path

By Silvenon:
function PreloadSoundPath takes string path returns nothing
    local sound snd = CreateSound(path, false, false, false, 10, 10, "")
    call SetSoundVolume(snd, 0)
    call StartSound(snd)
    call KillSoundWhenDone(snd)
    set snd = null
endfunction


Quick Timer Attach

By DiscipleOfLife:
library QuickTimer
    //============================================================================
    // Attaches and integer to a timer and makes it expire after about 0 seconds.
    //
    function TimerExpireQuick takes timer t, integer data, code handlerFunc returns nothing
        call TimerStart(t, data/2147483648., false, handlerFunc)
    endfunction

    //============================================================================
    // Returns the integer attached to a timer.
    //
    function QuickTimerGetData takes timer t returns integer
        return R2I(TimerGetTimeout(t)*2147483648.)
    endfunction
endlibrary


Last edited by PurplePoot; 09-13-2008 at 09:40 PM..
PurplePoot is offline   Reply With Quote
Old 09-30-2007, 10:37 AM   #2 (permalink)
 
Silvenon's Avatar

BBoy Silv
 
Join Date: Nov 2006
Posts: 866

Silvenon is on a distinguished road (81)Silvenon is on a distinguished road (81)


function GetClosestUnit takes real x, real y, boolexpr e returns unit
    local real md = 100000
    local real d
    local group g = CreateGroup()
    local unit u
    local real dx
    local real dy
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, e)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        if (dx * dx + dy * dy) / 100000 < md then
            set udg_ClosestUnit = u
            set md = (dx * dx + dy * dy) / 100000
        endif
    endloop
    call DestroyGroup(g)
    call DestroyBoolExpr(e)
    set g = null
    return udg_ClosestUnit
endfunction


Instructions:

Before using anything, you must create a global variable named ClosestUnit of type unit.

Parameters:
  • real x - x coordinate of the point from where the function searches for the closest unit
  • real y - y coordinate of the point from where the function searches for the closest unit
  • boolexpr e - used for filtering (enemy, illusion, hero...), you can put null if you don't want any filter

You can use the function in two ways:

function FirstWay takes nothing returns nothing
    local unit u = SomeUnit()
    local unit c
    set c = GetClosestUnit(GetUnitX(u), GetUnitY(u))
    call DoSomethingWithUnit(c)
    set u = null
    set c = null
endfunction


Or:

function SecondWay takes nothing returns nothing
    local unit u = SomeUnit()
    call GetClosestUnit(GetUnitX(u), GetUnitY(u))
    call DoSomethingWithUnit(udg_ClosestUnit)
    set u = null
endfunction
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters.
Silvenon: It's voila, not whala, you... stupidhead :)
PurgeandFire111: Yeah, I was in a rush. =( *cry*
Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better
PurgeandFire111: No, viola is an instrument. Stupidhead... =D

Last edited by Silvenon; 12-09-2007 at 11:38 AM..
Silvenon is offline   Reply With Quote
Old 09-30-2007, 05:46 PM   #3 (permalink)

iRawr
 
Join Date: Dec 2005
Posts: 8,349

PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)

Paired Mapping Contest #4 Winner: Fallen Angel - Lucifer's Keep Respected User: This user has been given the respected user award. Map Development Mini-Contest #1 Winner: Stand of the Elements 

Looks fine, but you don't need the globals. (Well, I suppose it's easier to use Closest as a global rather than the H2I trick...)

Skip the player, let them pass a boolexpr.
PurplePoot is offline   Reply With Quote
Old 10-01-2007, 01:58 PM   #4 (permalink)
 
Silvenon's Avatar

BBoy Silv
 
Join Date: Nov 2006
Posts: 866

Silvenon is on a distinguished road (81)Silvenon is on a distinguished road (81)


Quote:
Well, I suppose it's easier to use Closest as a global rather than the H2I trick...
Yeah, that's what I thought too, so I hope it's ok if I leave it like that.

Quote:
Skip the player, let them pass a boolexpr.
Yeah you're right, that way they can filter with more flexibility than just a IsUnitEnemy filter :P
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters.
Silvenon: It's voila, not whala, you... stupidhead :)
PurgeandFire111: Yeah, I was in a rush. =( *cry*
Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better
PurgeandFire111: No, viola is an instrument. Stupidhead... =D
Silvenon is offline   Reply With Quote
Old 10-01-2007, 08:59 PM   #5 (permalink)

iRawr
 
Join Date: Dec 2005
Posts: 8,349

PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)

Paired Mapping Contest #4 Winner: Fallen Angel - Lucifer's Keep Respected User: This user has been given the respected user award. Map Development Mini-Contest #1 Winner: Stand of the Elements 

Yeah, it's fine, it would be nice if you did an alternate version though since not everyone uses vJass (though it would be nice if they did...)

Anyways, boolexpr e? Reading a few functions recently? -.- (That's what they use in common and blizzard)
PurplePoot is offline   Reply With Quote
Old 10-01-2007, 09:11 PM   #6 (permalink)
 
Fulla's Avatar

Chaos Overlord
 
Join Date: Aug 2005
Posts: 711

Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)

Paired Mapping Contest #3 Winner: Warcraft Arena 

Stun a Unit, for X seconds:
You could add an SFX extension as well.

(I tried using jass tags but it screws up, becoming 1 gigantic line).

Uses a Timer Stack

globals
    integer array StunArray
    integer StunTotal=0
endglobals

struct stundata
    unit u=null
    real d=0
    boolean destroyplease=false
endstruct

function StunUnit_KeepStunned takes nothing returns nothing
    local integer i = 0
    local stundata dat
    loop
        exitwhen i==Stun_Total
        set dat=StunArray[i]
        if dat.destroyplease then
            set Stun_Total=Stun_Total-1
            set StunArray[i]=StunArray[Stun_Total]
            call dat.destroy()
            set i=i-1
else
            set dat.d=dat.d-0.01
            call SetUnitPosition(dat.u,GetUnitX(dat.u),GetUnitY(dat.u))
            set dat.destroyplease=(dat.d<=0)
        endif
        set i=i+1
    endloop
    if Stun_Total==0 then
        call ReleaseTimer(GetExpiredTimer())
    endif
endfunction

function StunUnit takes unit u,real d returns nothing
    local stundata dat=stundata.create()
    set dat.d=d
    set dat.u=u
    if (Stun_Total==0) then
        call TimerStart(NewTimer(),.01,true,function StunUnit_KeepStunned)
    endif
    set Stun_Total=Stun_Total+1
    set StunArray[Stun_Total-1]=dat
endfunction
__________________

Last edited by PurplePoot; 10-02-2007 at 08:45 PM..
Fulla is offline   Reply With Quote
Old 10-02-2007, 02:20 AM   #7 (permalink)
 
PandaMine's Avatar

User
 
Join Date: Sep 2007
Posts: 58

PandaMine has little to show at this moment (24)PandaMine has little to show at this moment (24)PandaMine has little to show at this moment (24)


You should probably mention there is a global integer array StunArray and also that the function requires JassNewGen, also I don't see any actual stunning going on (just moving a unit).

It also uses a timer stack (release timer) which you havent specified, other then that the function looks good and efficient
PandaMine is offline   Reply With Quote
Old 10-02-2007, 10:25 AM   #8 (permalink)
 
Fulla's Avatar

Chaos Overlord
 
Join Date: Aug 2005
Posts: 711

Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)

Paired Mapping Contest #3 Winner: Warcraft Arena 

Aha yea thx, updated.

Yea its an alternative way of stunning without using A Stormbolt spell with 100 levels.

On my map the function also takes an effect + attachment point so you can customise the overhead buff. I'd recommend the same.
(Id need to included the whole AddTimedSfx for that).
__________________
Fulla is offline   Reply With Quote
Old 10-02-2007, 01:47 PM   #9 (permalink)
 
PandaMine's Avatar

User
 
Join Date: Sep 2007
Posts: 58

PandaMine has little to show at this moment (24)PandaMine has little to show at this moment (24)PandaMine has little to show at this moment (24)


Yeah what I was saying is that you dont have IssueTargetOrder order for the spell or whatever you need to do to stun. Someone will put this in their map and say WTF this doesnt do anything. I know how custom stun systems work, they stun forever and then a timer removes the buff. There is however none of that in what you posted
PandaMine is offline   Reply With Quote
Old 10-02-2007, 06:19 PM   #10 (permalink)
 
Silvenon's Avatar

BBoy Silv
 
Join Date: Nov 2006
Posts: 866

Silvenon is on a distinguished road (81)Silvenon is on a distinguished road (81)


Quote:
it would be nice if you did an alternate version though since not everyone uses vJass (though it would be nice if they did...)
I can do that if you want. I just have to tell them to create a global.
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters.
Silvenon: It's voila, not whala, you... stupidhead :)
PurgeandFire111: Yeah, I was in a rush. =( *cry*
Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better
PurgeandFire111: No, viola is an instrument. Stupidhead... =D
Silvenon is offline   Reply With Quote
Old 10-02-2007, 08:45 PM   #11 (permalink)

iRawr
 
Join Date: Dec 2005
Posts: 8,349

PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)PurplePoot is a splendid one to behold (807)

Paired Mapping Contest #4 Winner: Fallen Angel - Lucifer's Keep Respected User: This user has been given the respected user award. Map Development Mini-Contest #1 Winner: Stand of the Elements 

Yeah, that's what I meant.

And Fulla, I edited your post to use [jass] tags.

Also, it'd be easier to just store the struct to a cache on a timer with the duration of the stun, and then remove the stun after the end of the duration. (However you need to Pause them this way)

Or cast a dummy ability that stuns forever then remove the buff after long enough, thus also adding a buff.
PurplePoot is offline   Reply With Quote
Old 10-02-2007, 08:54 PM   #12 (permalink)
 
Fulla's Avatar

Chaos Overlord
 
Join Date: Aug 2005
Posts: 711

Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)Fulla will become famous soon enough (119)

Paired Mapping Contest #3 Winner: Warcraft Arena 

Quote:
Or cast a dummy ability that stuns forever then remove the buff after long enough, thus also adding a buff.
That actually sounds like a very sensible way.
__________________
Fulla is offline   Reply With Quote
Old 10-02-2007, 09:55 PM   #13 (permalink)
 
Silvenon's Avatar

BBoy Silv
 
Join Date: Nov 2006
Posts: 866

Silvenon is on a distinguished road (81)Silvenon is on a distinguished road (81)


*Fixed.

Quote:
Anyways, boolexpr e? Reading a few functions recently? -.- (That's what they use in common and blizzard)
I didn't see this. Hahaha......yeah, I have been reading a few functions recently :)
__________________
PurgeandFire111: Then you can delete the sound and whala... You have the label,filepath, and other parameters.
Silvenon: It's voila, not whala, you... stupidhead :)
PurgeandFire111: Yeah, I was in a rush. =( *cry*
Herman: Voila is an instrument that I quit playing in 6th grade, whala works just fine if not better
PurgeandFire111: No, viola is an instrument. Stupidhead... =D
Silvenon is offline   Reply With Quote