- Joined
- Jun 20, 2011
- Messages
- 249
JASS:
library Loc /* v2.1.0
Replacement of the Location native.
***********************************************************************
*
* struct Loc
*
* readonly real x
* readonly real y
* - The Loc's position in the XY grid.
* readonly real z
* - The Loc's total height (including ground z)
*
*
* static method create takes real x, real y, real z returns Loc
* - Creates a new Loc with the given coordinates.
* method lock takes nothing returns nothing
* - Locks the instance, preventing it from being
* - deallocating when unlocked
* method unlock takes nothing returns nothing
* - Attempts to deallocate the instance, wont happen if
* - it's still being used.
*
* method move takes real x, real y, real z returns nothing
* - Assigns the Loc new coordinates.
*
**********************************************************************/
struct Loc extends array
private static integer array r
private static integer ic = 0
readonly static location global = Location(0,0)
readonly real x
readonly real y
readonly real z
//*********************************************************************
// Lock counters: a way to keep track of how many times a Loc has been
// locked, starts counting from 1. Unlocking drops the counters by 1,
// if it reaches 0 the instance is deallocated.
private integer lc
method move takes real sx, real sy, real sz returns nothing
call MoveLocation(global,sx,sy)
set x = sx
set y = sy
set z = sz+GetLocationZ(global)
endmethod
static method create takes real sx, real sy, real sz returns thistype
local thistype this = r[0]
if this==0 then
set ic = ic+1
set this = ic
else
set r[0] = r[this]
endif
call this.move(sx,sy,sz)
set lc = 1
return this
endmethod
method lock takes nothing returns nothing
set lc = lc+1
endmethod
/* idea brought by Nestharus */
method unlock takes nothing returns nothing
set lc = lc-1
if lc==0 then
set r[this] = r[0]
set r[0] = this
endif
endmethod
endstruct
endlibrary
JASS:
library AdvLoc /* v1.0.0
*/uses/*
*/ Loc /* hiveworkshop.com/forums/submissions-414/snippet-lacking-loc-209322/
Advanced Loc handling
***********************************************************************
*
* struct AdvLoc
* - Has the exact same API as Loc with extended functionality
*
* static method link takes Loc a, Loc b returns nothing
* - Creates a link between the two Locs, linked Locs keep
* - data relative to the other Loc's position such as...
*
* readonly real angle
* readonly real distance
* readonly real slope
* - Each Loc knows the angle/distance/slope from itself
* - to it's linked partner. Updates automatically.
*
* method lock takes nothing returns nothing
* - Locks the instance, preventing it from being
* - deallocating when unlocked
* method unlock takes nothing returns nothing
* - Attempts to deallocate the instance, wont happen if
* - it's still being used.
*
**********************************************************************/
struct AdvLoc extends array
readonly thistype ref
readonly real angle
readonly real distance
readonly real slope
//*********************************************************************
// Lock counters: a way to keep track of how many times a Loc has been
// locked, starts counting from 1. Unlocking drops the counters by 1,
// if it reaches 0 the instance is deallocated.
private integer lc
method operator x takes nothing returns real
return Loc(this).x
endmethod
method operator y takes nothing returns real
return Loc(this).y
endmethod
method operator z takes nothing returns real
return Loc(this).z
endmethod
static method create takes real ox, real oy, real oz returns thistype
local thistype this = Loc.create(ox,oy,oz)
set ref = this
return this
endmethod
private static method math takes thistype a, thistype b returns nothing
set a.angle = Atan2(b.y-a.y,b.x-a.x)
set a.distance = SquareRoot((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y))
set a.slope = (b.z-a.z)/a.distance
set b.angle = a.angle+bj_PI
set b.distance = a.distance
set b.slope = -a.slope
endmethod
static method link takes AdvLoc a, AdvLoc b returns nothing
set a.ref = b
set b.ref = a
call math(a,b)
endmethod
method unlink takes nothing returns nothing
if lc==0 then
set ref.ref = ref
set ref = this
endif
endmethod
method lock takes nothing returns nothing
set lc = lc+1
call Loc(this).lock()
endmethod
method unlock takes nothing returns nothing
set lc = lc-1
if lc==0 then
set ref.ref = ref
set ref = this
endif
call Loc(this).unlock()
endmethod
method move takes real nx, real ny, real nz returns nothing
call Loc(this).move(nx,ny,nz)
if this!=ref then
call math(this,ref)
endif
endmethod
endstruct
endlibrary
Last edited: