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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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