Do you find this helpful?

Dirac

22710180
Reaction score
147
Because i do, and i find it to be a very good replacement to the ugly Location native
The join method would be used to constantly have useful data of both locations at any time without repeating the same calculations
If you don't know what the "slope" variable is for, it's the relation between the difference of the height of two locs and the distance between them.
JASS:
library Loc /* v1.0.0

Replacement of the Location native.

***********************************************************************
*
*   struct Loc
*
*       readonly real x
*       readonly real y
*       readonly real z
*           -   These 3 read-only variables are the Loc's coordinates
*
*       static method create takes real x, real y, real z returns Loc
*           -   Creates a new Loc with the given coordinates.
*       method destroy takes nothing returns nothing
*           -   Deallocates the Loc.
*
*       method move takes real x, real y, real z returns nothing
*           -   Assigns the Loc new coordinates.
*
*       static method join takes Loc v1, Loc v2 returns nothing
*           -   Joins two Locs together.
*       method disjoint takes nothing returns nothing
*           -   Breaks the joint of the given Loc.
*
*       readonly real angle
*       readonly real distance
*       readonly real slope
*           -   These 3 read-only members store the relation between
*           -   two joined Locs. Updates everytime a Loc is moved.
*
**********************************************************************/
    
    struct Loc extends array
        
        private static integer ic = 0
        private static integer array next
        
        readonly static location point = Location(0,0)
        
        readonly real x 
        readonly real y
        readonly real z
        
        readonly real angle
        readonly real distance
        readonly real slope
        
        private static method refresh takes Loc v1, Loc v2 returns nothing
            local real a = Atan2(v2.y-v1.y,v2.x-v1.x)
            local real d = SquareRoot((v1.x-v2.x)*(v1.x-v2.x)+(v1.y-v2.y)*(v1.y-v2.y))
            local real s = (v2.z-v1.z)/d
            set v1.angle = a
            set v2.angle = a+bj_PI
            set v1.distance = d
            set v2.distance = d
            set v1.slope = s
            set v2.slope = -s
        endmethod
        
        static method join takes Loc v1, Loc v2 returns nothing
            call refresh(v1,v2)
            set next[v1]=v2
            set next[v2]=v1
        endmethod
        
        method disjoint takes nothing returns nothing
            set next[next[this]]=next[this]
            set next[this]=this
        endmethod
        
        method move takes real sx, real sy, real sz returns nothing
            call MoveLocation(point,sx,sy)
            set x=sx
            set y=sy
            set z=sz+GetLocationZ(point)
            if this!=next[this] then
                call refresh(this,next[this])
            endif
        endmethod
        
        static method create takes real sx, real sy, real sz returns thistype
            local thistype this
            if next[0]==0 then
                set ic = ic+1
                set this = ic
            else
                set this = next[0]
                set next[0] = next[next[0]]
            endif
            set next[this]=this
            call this.move(sx,sy,sz)
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.disjoint()
            set next[this]=next[0]
            set next[0]=this
        endmethod
        
    endstruct
    
endlibrary
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
i would add something like this:
JASS:
static method compare takes thistype this, thistype this2 returns real
    return SquareRoot((this.x - this2.x, 2) * (this.x - this2.x, 2) + (this.y - this2.y) * (this.y - this2.y) + (this.z - this2.z, 2) * (this.z - this2.z, 2))
endmethod //to return the real distance between points taking height into consideration


and if you werent limited to 2 points linked together

Also if you increased the link ammounts, something like this:

JASS:
static method compareChain takes thistype this returns real
    local real dist = 0
    local thistype i = this
    loop
        set dist = dist + .compare(i, i.next)
        exitwhen i.next = this
        set i = i.next
    endloop
    return dist
endmethod


thats actually something i would use (as long as its truly more efficient than just using x/y arguments and locals)

also... why do you use an integer array instead of a dynamic struct variable, ie:

JASS:
struct Example extends array
    static integer array next
    static integer array previous

    ---->

    thistype next
    thistype previous

    or just

    thistype.next
endstruct


this makes it much simpler for deallocation and referencing next/previous

ie this.next.previous.next.previous = this

no need for
.next[.previous[.next[.previous]]]
or
thistype(thistype(thistype(this.next).previous).next).previous

which are way more complicated
it compiles the same but its more readable



Also also:

why do you link them with 0 when you destroy them? what happens when someone destroys a bunch 1 after another, do they just all point to 0 and not get pointed to at all?
 

Dirac

22710180
Reaction score
147
Thanks for the feedback.
[ljass]loc.distance/Atan(loc.slope)[/ljass]
Would be equal to the distance of the locs considering the height of both

I limit myself to two points because of two reasons:
  • It's the most common situation
  • I would have to use Table instead, would be much slower

I guess i could implement some kind of "sync" between two locs, but would be monodirectional data.

Example, I have 5 locs and 4 of them are linked to the 5th one.
If i move the 5th one, all other 4 locs would get their data updated, but the 5th one would only get the data from 1 of them (as it can only be synced with 1 node)
In that case I would have to use LinkedList and it would be less user friendly
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
if you need to link 2 locations together to compare the distance i wouldnt find that useful, i would rather just compare them via regular math for slope/distance etc
i mean i guess it would be usefull to someone whos not to great at math and would spend a ton of time researching how to do it and still not get it, but other than that it would basically be a new version of the [ljass]PolarProjectionBJ[/ljass] with a slight twist, more efficient to use whats inside rather than the function
 

Dirac

22710180
Reaction score
147
why do you link them with 0 when you destroy them? what happens when someone destroys a bunch 1 after another, do they just all point to 0 and not get pointed to at all?
It's a stack.

0 -> 0 -> 0

instance (4) gets deallocated
{
next[4] = next[0] -> 0
next[0] = 4
}

0 -> 4 -> 0

instance (7) gets deallocated
{
next[7] = next[0] -> 4
next[0] = 7
}

0 -> 7 -> 4 -> 0



Using your way of doing things
JASS:
local real dist = 0
local thistype i = this
loop
    set dist = dist + .compare(i, i.next)
    exitwhen i.next = this
    set i = i.next
endloop

Would update every other Loc "i" with "this" , but won't update the data on "this"
That would cause issues if the user has joined only 2 Locs together, because now only one of those locations knows the actual distance, the one you didn't move.
In case the user has more than 2 joined... as an example Locs A, B and C are together, if I move A then B and C would know their distance to A, but they won't know their distance to each other and A would know the distance between itself and one of the other Locs (chosen randomly)
A.distance = distance between A and ??
B.distance = distance between B and A
C.distance = distance between C and A

Current method i wrote using my linked list module
JASS:
private method refresh takes nothing returns nothing
    local thistype exit = this
    local real a
    local real d
    local real s
    loop
        set this = next
        exitwhen this == exit
        set a = Atan2(this.y-exit.y,this.x-exit.x)
        set d = SquareRoot((exit.x-this.x)*(exit.x-this.x)+(exit.y-this.y)*(exit.y-this.y))
        set s = (this.z-exit.z)/d
        set exit.angle = a
        set this.angle = a+bj_PI
        set exit.distance = d
        set this.distance = d
        set exit.slope = s
        set this.slope = -s
    endloop
endmethod
 
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