- Joined
- Mar 3, 2006
- Messages
- 1,564
I am creating a spell, and in order to improve it I tried to use Alloc
but I don't know how to implement it in my spell.
Here is a sample of my codes:
P.S. : I am not a pro-vJasser so make your answers/help clear, please. And thanks in advance.
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Alloc ~~ By Sevion ~~ Version 1.09 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is Alloc?
// - Alloc implements an intuitive allocation method for array structs
//
// =Pros=
// - Efficient.
// - Simple.
// - Less overhead than regular structs.
//
// =Cons=
// - Must use array structs (hardly a con).
// - Must manually call OnDestroy.
// - Must use Delegates for inheritance.
// - No default values for variables (use onInit instead).
// - No array members (use another Alloc struct as a linked list or type declaration).
//
// Methods:
// - struct.allocate()
// - struct.deallocate()
//
// These methods are used just as they should be used in regular structs.
//
// Modules:
// - Alloc
// Implements the most basic form of Alloc. Includes only create and destroy
// methods.
//
// Details:
// - Less overhead than regular structs
//
// - Use array structs when using Alloc. Put the implement at the top of the struct.
//
// - Alloc operates almost exactly the same as default structs in debug mode with the exception of onDestroy.
//
// How to import:
// - Create a trigger named Alloc.
// - Convert it to custom text and replace the whole trigger text with this.
//
// Thanks:
// - Nestharus for the method of allocation and suggestions on further merging.
// - Bribe for suggestions like the static if and method names.
// - PurgeandFire111 for some suggestions like the merging of Alloc and AllocX as well as OnDestroy stuff.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library Alloc
module Alloc
private static integer instanceCount = 0
private thistype recycle
static method allocate takes nothing returns thistype
local thistype this
if (thistype(0).recycle == 0) then
debug if (instanceCount == 8190) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to allocate too many instances!")
debug return 0
debug endif
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = thistype(0).recycle
set thistype(0).recycle = thistype(0).recycle.recycle
endif
debug set this.recycle = -1
return this
endmethod
method deallocate takes nothing returns nothing
debug if (this.recycle != -1) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to deallocate an invalid instance at [" + I2S(this) + "]!")
debug return
debug endif
set this.recycle = thistype(0).recycle
set thistype(0).recycle = this
endmethod
endmodule
endlibrary
but I don't know how to implement it in my spell.
Here is a sample of my codes:
JASS:
private struct MislData
unit Caster
unit Missile
location CastPoint
real Distance
real Damage
real Radius
real face
static method MissileMove takes nothing returns nothing
local MislData dat
local integer i = 0
local real X = 0
local real Y = 0
local location loc
loop
exitwhen i >= mindex
set dat = temp_mdat[i]
set dat.Distance = dat.Distance - M_DIST
set X = GetUnitX(dat.Missile)
set Y = GetUnitY(dat.Missile)
set loc = GetUnitLoc(dat.Missile)
if DistanceBetweenPoints(loc,dat.CastPoint) < M_DIST then
set mindex = mindex - 1
set temp_mdat[i] = temp_mdat[mindex]
set i = i - 1
call NovaData.NovaCreate(dat.Caster , dat.Damage , dat.Radius , GetUnitX(dat.Missile) , GetUnitY(dat.Missile))
call dat.destroy()
else
set X = X + M_DIST * Cos(dat.face)
set Y = Y + M_DIST * Sin(dat.face)
call SetUnitPosition(dat.Missile,X,Y)
endif
call RemoveLocation(loc)
set i = i + 1 // this index loops through all the casters
endloop
endmethod
static method MissileCreate takes unit u , location target returns MislData
local MislData dat = MislData.allocate()
local real x1
local real y1
local real x2
local real y2
local real dx
local real dy
set temp_mdat[mindex] = dat
set dat.Caster = u
set x1 = GetUnitX(dat.Caster)
set y1 = GetUnitY(dat.Caster)
set dat.CastPoint = target
set x2 = GetLocationX(dat.CastPoint)
set y2 = GetLocationY(dat.CastPoint)
set dat.Distance = 0
set dx = x2 - x1
set dy = y2 - y1
set dat.Distance = SquareRoot(dx * dx + dy * dy)
set dat.face = Atan2(y2 - y1, x2 - x1)
set dat.Missile = CreateUnit(GetOwningPlayer(dat.Caster),M_UNIT_ID,x1,y1,dat.face * bj_RADTODEG)
call SetUnitPosition(dat.Missile,x1,y1)
set dat.Damage = NovaDamage(GetUnitAbilityLevel(u , ABILTY_ID))
//call BJDebugMsg(R2S(dat.Damage))
set dat.Radius = NovaRadius(GetUnitAbilityLevel(u , ABILTY_ID))
if mindex == 0 then
call TimerStart(mtm,M_TIMER_INTERVAL,true,function MislData.MissileMove)
endif
set mindex = mindex + 1
return dat
endmethod
method onDestroy takes nothing returns nothing
call KillUnit(.Missile)
set .Missile = null
set .face = 0
call RemoveLocation(.CastPoint)
if mindex == 0 then
call PauseTimer(mtm)
endif
endmethod
endstruct
P.S. : I am not a pro-vJasser so make your answers/help clear, please. And thanks in advance.