• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Swarm Function

Status
Not open for further replies.
Level 12
Joined
Aug 20, 2007
Messages
866
I just started learning JASS, and wanted to do a simple function for a spell I was making

I do not have JassCraft (using a computer that isn't mine), so I need to know the proper syntax for this code

JASS:
function Swarm takes real x, real y, real r, integer n, unit-type unit returns nothing
local real DegreesBetween = 360/n
local real D = 0
loop
exitwhen n==0
D = D + DistanceBetween
CreateUnit(x + (cos(D) * r), y + (sin(D) * r), unit)
n = n -1
endloop
endfunction


I'm not sure exactly what the syntax is, but I assume it is something similar to that
Also, I haven't added a bit for making the unit face the center (not sure wah tthe syntax is) but I imagine it's just setting unit facing = to D - 180
If somebody could also tell me the syntax for changing their facing that would be good too
(I will give +rep)
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
What are you trying to make ?

About the angle, do this to make a unit turn backwards:

call SetUnitFacing(yourUnit) = GetUnitFacing(yourUnit) - 180

If you want a unit to face a point, you can use the AngleBetweenPoints function but its not suggested.
Its usually better to use your own coordinates-based angle function.

Here are two usefull basic functions you'll probably want in your JASSing:

JASS:
//****************************************************************************************
//               Distance between two points using coordinates
// call DistanceBetweenCoordinates(real x1, real y1, real x2, real y2)

function DistanceBetweenCoordinates takes real x1, real y1, real x2, real y2 returns real
    local real dx = x1 - x2
    local real dy = y1 - y2
    return SquareRoot(dx * dx + dy * dy)
endfunction

//****************************************************************************************
//               Angle between two points using coordinates
// call AngleBetweenCoordinates(real x1, real y1, real x2, real y2)

function AngleBetweenCoordinates takes real x1, real y1, real x2, real y2 returns real
    return bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
endfunction
 
Level 12
Joined
Aug 20, 2007
Messages
866
Heh

Whoops forgot to give enough info

The function is supposed to create a circle of units evenly spaced out and equidistant from the origin

x,y is the origin of the circle
r is the radius
n is the number of units
unit is supposed to be the unit type, but I have no idea how it should work in JASS (I don't have JassCraft or strong knowledge of the syntax)

JASS:
function Swarm takes real x, real y, real r, integer n, unit-type unit returns nothing
local real DegreesBetween = 360/n //Finds the degrees between each unit
local real D = 0                           //allocates memory for the (Degrees) of each unit's polar coordinates
loop
exitwhen n==0
set D = D + DistanceBetween //Each loop adds another fraction of the degrees, spacing the units out evenly
call CreateUnit(x + (cos(D) * r), y + (sin(D) * r), unit) //I am not sure what this code is supposed to be, but it is the mathematical function to find polar coordinates
call SetUnitFacing(bj_last_Created()) = D - 180 //Makes the units face the center
set n = n -1                                      //with each loop, n drops, and the loop will exit when n = 0
endloop
endfunction
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
JASS:
set D = D + DistanceBetween

I guess you meant AngleBetween ?
JASS:
call CreateUnit(x + (cos(D) * r), y + (sin(D) * r), unit)

This should be like this:
JASS:
call CreateUnit(player owner, integer rawcode, real x, real y, real facing angle)

So for what you want you'll do something like this
JASS:
call CreateUnit(somePlayer, someRawCode, x + r * cos(D), y + r * sin(D), someAngle)
 
Level 12
Joined
Aug 20, 2007
Messages
866
Heh

I downloaded JASScraft to a flash drive, and then reviewed it on another computer that has wc3 installed

heres the finished
JASS:
function Swarm takes player owner, integer unitid, real x, real y, real r, integer n returns nothing
local real DegreesBetween = 360/n
local real D=0
loop
exitwhen n==0
set D = D + DegreesBetween
call CreateUnit(owner, unitid, (x + (Cos(D * bj_DEGTORAD)*r)), (y + (Sin(D * bj_DEGTORAD)*r)), D - 180)
set n = n - 1
endloop
endfunction

Not too user friendly, but comes out looking really cool when you use it (try 45 units at a radius of 1000)
 
Status
Not open for further replies.
Top