Snippet The coordinates type

Darius34

New Member
Reaction score
30
JASS:
function Trig_Sonic_Wave_Actions takes nothing returns nothing
    local unit caster = GetTriggerUnit()
    local location targetloc = GetSpellTargetLoc()
    local real targetx = GetLocationX(targetloc)
    local real targety = GetLocationY(targetloc)
    local real casterx = GetUnitX(caster)
    local real castery = GetUnitY(caster)
    local real dx = targetx - casterx
    local real dy = targety - castery
    local real angle = Atan2(dy, dx)
    local real distance = SquareRoot(dx * dx + dy * dy)

    ...

endfunction

I'm sure most JASS users have seen something like this before. A mess of code for calculating something simple, like an offset, just to avoid the use of locations. In an attempt to shorten and simplify that whole process...

JASS:
library CoordinatesType
struct coordinates
    real sourcex
    real sourcey
    real destx
    real desty
    real dist
    real angle
    real x
    real y

    static method create takes real sourcex, real sourcey, real destx, real desty returns coordinates
        local coordinates c = coordinates.allocate()
        local real dx = destx - sourcex
        local real dy = desty - sourcey
        
        set c.sourcex = sourcex
        set c.sourcey = sourcey
        set c.destx = destx
        set c.desty = desty
        set c.dist = SquareRoot(dx * dx + dy * dy)
        set c.angle = Atan2(dy, dx)
        
        return c
    endmethod
    
    // This offsets the sourcex/y coordinates and stores the new ones in x/y.
    method offset takes real offset, real angle returns nothing
        set .x = .sourcex + offset * Cos(angle)
        set .y = .sourcey + offset * Sin(angle)
    endmethod

    // This updates dist and angle.
    method update takes nothing returns nothing
        local real dx = .destx - .sourcex
        local real dy = .desty - .sourcey
        
        set .dist = SquareRoot(dx * dx + dy * dy)
        set .angle = Atan2(dy, dx)
    endmethod
    
    // This function takes new source coordinates and basically resets the struct.
    method updatewith takes real sourcex, real sourcey, real destx, real desty returns nothing
        set .sourcex = sourcex
        set .sourcey = sourcey
        set .destx = destx
        set .desty = desty
        
        call .update()
    endmethod
endstruct
endlibrary

The code is pretty straightforward; it's in essence a means to put all that untidy code away somewhere. Consider it a type like the location (or rather, a struct, in standard programming), with some extended syntax and without the leaks and stuff.

Requires:
- vJass
- JASS knowledge.

Implementation:
-Copy the code/trigger into a blank text trigger, or into some other utility library. Voila.

What is it for?
Handling coordinates. Simplifies everything. All that's required is a create() function call to take coordinates, and the struct members are assigned and do the rest.

It's not very user-friendly, admittedly, but then it's used pretty much like a normal struct:

JASS:
function Weird takes nothing returns nothing
    local unit caster = GetTriggerUnit()
    local location targetloc = GetSpellTargetLoc()
    local coordinates c = coordinates.create(GetUnitX(caster), GetUnitY(caster), GetLocationX(targetloc), GetLocationY(targetloc))
    
    // Moves "caster" 600 units forward in the direction of casting, a la Blink:
    call c.offset(600., c.angle)
    call SetUnitPosition(caster, c.x, c.y)

    // Sets "caster"'s hitpoints to 20% of the (remaining) distance between it and the targeted point, a la Spear of Holy Calculations
    set c.sourcex = GetUnitX(caster)
    set c.sourcey = GetUnitY(caster)
    call c.update()
    call SetUnitState(caster, UNIT_STATE_LIFE, c.dist * .2)

    // Like all structs, coordinates must be destroyed. They don't leak, but they needlessly take up struct instances.
    call c.destroy()

    call RemoveLocation(targetloc)
    set targetloc = null
    set caster = null
endfunction

A demo map in which this snippet is used can be found here, if it's needed; it's actually for my Wait System.

Comments and suggestions for improvement are appreciated.
 

Vestras

Retired
Reaction score
249
I love you.
BUT, i think you should add more functions, since 3 or 4 functions isn't much and offsets and distance between points isn't the only ones who are... annoying...
 

Flare

Stops copies me!
Reaction score
662
Any particular reason why the struct name is so long? :p

since 3 or 4 functions isn't much
It's a snippet, it doesn't need loads of functionality

offsets and distance between points isn't the only ones who are... annoying..
Such as? I can't think of anything that is regularly used that's not there (and there's no point adding stuff that's seldom used, since it's just a waste of time to add it)
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
So in order of avoiding using the BJs that return a location, you should instead create a struct and then call a function similar to the BJs with the only exception that they return coordinates?

Sorry if I'm the only one who doesn't find this useful.
 

Tukki

is Skeleton Pirate.
Reaction score
29
I'll have to agree with Larcenist here.. It seems that we're back to using BJs with coordinates. Though, it could be useful to new coders.

Furthermore I think you should add methods for moving units with spherical coordinates, as it could be more useful then..
 

Darius34

New Member
Reaction score
30
I love you.
BUT, i think you should add more functions, since 3 or 4 functions isn't much and offsets and distance between points isn't the only ones who are... annoying...
Like Flare said, feel free to suggest any additions.

Any particular reason why the struct name is so long? :p
"Coordinates"? I don't know. Accurate description, I guess. :p

So in order of avoiding using the BJs that return a location, you should instead create a struct and then call a function similar to the BJs with the only exception that they return coordinates?

Sorry if I'm the only one who doesn't find this useful.
Well, using this struct is essentially the same as using reals with a couple of additional function calls. That's still better then using locations, I think, since you're not creating any handles (no leaks, faster), there aren't the extra GetLocationX/Y() and MoveLocation() functions, and since you still get the flexibility of using reals (in complex calculations, or in functions that take reals, for example SetUnitX/Y() instead of SetUnitPositionLoc() - former doesn't interrupt orders).

I guess it's personal preference, to some extent. It started off as an experiment with structs; I plan to add more to it to make it more useful.

Furthermore I think you should add methods for moving units with spherical coordinates, as it could be more useful then..
What do you mean? But yeah, I'm open to suggestions and could try. Could turn this snippet into something else.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top