Dirac
22710180
- Reaction score
- 147
Hello guys i present you the chain system (requires T32) a system that creates a chain between 2 points
static method create takes unit source, unit target, string fx, real rate returns thistype
The problem is, it causes a lot of lag after a while, also that it's array size is HUGE so it can sustain more than 8 instances at the time. Help me fix it
Non T32 version
static method create takes unit source, unit target, string fx, real rate returns thistype
The problem is, it causes a lot of lag after a while, also that it's array size is HUGE so it can sustain more than 8 instances at the time. Help me fix it
JASS:
library Chain uses T32
globals
private timer TIMER = CreateTimer()
private constant real TIMEOUT = 0.03125
private integer DUMMY = 039;h000039;
endglobals
struct chain
unit target
real rate
string fx
unit array shackle[1000]
effect array shacklefx[1000]
integer shacklecount
real sourceheight
real targetheight
integer list
private method periodic takes nothing returns nothing
local real array x
local real array y
local real a
local integer i = 2
set x[0] = GetUnitX(.shackle[.shacklecount])
set y[0] = GetUnitY(.shackle[.shacklecount])
set x[1] = GetUnitX(.shackle[.shacklecount+1])
set y[1] = GetUnitY(.shackle[.shacklecount+1])
//This is the part where shackles are created if the last one is separated from the caster
loop
exitwhen .rate > SquareRoot((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]))
set a = Atan2(y[0]-y[1],x[0]-x[1])
set .shacklecount = .shacklecount+1
set .shackle[.shacklecount+1]= .shackle[.shacklecount]
set .shackle[.shacklecount] = CreateUnit(GetOwningPlayer(.shackle[.shacklecount+1]),DUMMY,x[0]-.rate*Cos(a),y[0]-.rate*Sin(a),bj_RADTODEG*a)
set .shacklefx[.shacklecount] = AddSpecialEffectTarget(.fx,.shackle[.shacklecount],"origin")
set x[0] = GetUnitX(.shackle[.shacklecount])
set y[0] = GetUnitY(.shackle[.shacklecount])
endloop
//This is where shackles are moved depending on the one that follows them in the chain. All but the first one
loop
exitwhen i > .shacklecount
set x[2] = GetUnitX(.shackle<i>)
set y[2] = GetUnitY(.shackle<i>)
set x[3] = GetUnitX(.shackle[i-1])
set y[3] = GetUnitY(.shackle[i-1])
set a = Atan2(y[3]-y[2],x[3]-x[2])
call SetUnitX(.shackle<i>,x[2]+(SquareRoot((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]))-.rate)*Cos(a))
call SetUnitY(.shackle<i>,y[2]+(SquareRoot((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]))-.rate)*Sin(a))
call SetUnitFacing(.shackle<i>,bj_RADTODEG*a)
call UnitAddAbility(.shackle<i>,039;Amrf039;)
call SetUnitFlyHeight(.shackle<i>,(((.sourceheight-.targetheight)*(i-1))/(.shacklecount-1))+.targetheight,0.)
call UnitRemoveAbility(.shackle<i>,039;Amrf039;)
set i = i+1
endloop
//The first shackle moves depending to the target's location
set x[4] = GetUnitX(.shackle[1])
set y[4] = GetUnitY(.shackle[1])
set x[5] = GetUnitX(.target)
set y[5] = GetUnitY(.target)
set a = Atan2(y[4]-y[5],x[4]-x[5])
call SetUnitX(.shackle[1],x[5]+.rate*Cos(a))
call SetUnitY(.shackle[1],y[5]+.rate*Sin(a))
call SetUnitFacing(.shackle[1],180+bj_RADTODEG*a)
endmethod
implement T32x
method SetSourceHeight takes real a returns nothing
set .sourceheight = a
endmethod
method SetTargetHeight takes real a returns nothing
set .targetheight = a
endmethod
method destroy takes nothing returns nothing
local integer i = 1
loop
exitwhen i > .shacklecount
call DestroyEffect(.shacklefx<i>)
call RemoveUnit(.shackle<i>)
set i = i+1
endloop
set .shacklecount = 0
call .deallocate()
endmethod
static method create takes unit source, unit target, string fx, real rate returns thistype
local thistype this = thistype.allocate()
local real a = Atan2(GetUnitY(target)-GetUnitY(source),GetUnitX(target)-GetUnitX(source))
set this.shacklecount = 1
set this.shackle[this.shacklecount+1] = source
set this.target = target
set this.rate = rate
set this.fx = fx
set this.sourceheight = 50.
set this.targetheight = 50.
set this.shackle[this.shacklecount] = CreateUnit(GetOwningPlayer(source),DUMMY,GetUnitX(target)-rate*Cos(a),GetUnitY(target)-rate*Sin(a),a*bj_RADTODEG)
set this.shacklefx[this.shacklecount] = AddSpecialEffectTarget(fx,this.shackle[this.shacklecount],"origin")
call UnitAddAbility(this.shackle[this.shacklecount],039;Amrf039;)
call SetUnitFlyHeight(this.shackle[this.shacklecount],this.targetheight,0.)
call UnitRemoveAbility(this.shackle[this.shacklecount],039;Amrf039;)
call this.startPeriodic()
return this
endmethod
endstruct
endlibrary</i></i></i></i></i></i></i></i></i></i>
Non T32 version
JASS:
library Chain
globals
private timer TIMER = CreateTimer()
private constant real TIMEOUT = 0.03125
private integer array DATA
private integer DATACOUNT = 0
private integer DUMMY = 039;h000039;
endglobals
struct chain
unit target
real rate
string fx
unit array shackle[1000]
effect array shacklefx[1000]
integer shacklecount
real sourceheight
real targetheight
integer list
private method periodic takes nothing returns nothing
local real array x
local real array y
local real a
local integer i = 2
set x[0] = GetUnitX(.shackle[.shacklecount])
set y[0] = GetUnitY(.shackle[.shacklecount])
set x[1] = GetUnitX(.shackle[.shacklecount+1])
set y[1] = GetUnitY(.shackle[.shacklecount+1])
//This is the part where shackles are created if the last one is separated from the caster
loop
exitwhen .rate > SquareRoot((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]))
set a = Atan2(y[0]-y[1],x[0]-x[1])
set .shacklecount = .shacklecount+1
set .shackle[.shacklecount+1]= .shackle[.shacklecount]
set .shackle[.shacklecount] = CreateUnit(GetOwningPlayer(.shackle[.shacklecount+1]),DUMMY,x[0]-.rate*Cos(a),y[0]-.rate*Sin(a),bj_RADTODEG*a)
set .shacklefx[.shacklecount] = AddSpecialEffectTarget(.fx,.shackle[.shacklecount],"origin")
set x[0] = GetUnitX(.shackle[.shacklecount])
set y[0] = GetUnitY(.shackle[.shacklecount])
endloop
//This is where shackles are moved depending on the one that follows them in the chain. All but the first one
loop
exitwhen i > .shacklecount
set x[2] = GetUnitX(.shackle<i>)
set y[2] = GetUnitY(.shackle<i>)
set x[3] = GetUnitX(.shackle[i-1])
set y[3] = GetUnitY(.shackle[i-1])
set a = Atan2(y[3]-y[2],x[3]-x[2])
call SetUnitX(.shackle<i>,x[2]+(SquareRoot((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]))-.rate)*Cos(a))
call SetUnitY(.shackle<i>,y[2]+(SquareRoot((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]))-.rate)*Sin(a))
call SetUnitFacing(.shackle<i>,bj_RADTODEG*a)
call UnitAddAbility(.shackle<i>,039;Amrf039;)
call SetUnitFlyHeight(.shackle<i>,(((.sourceheight-.targetheight)*(i-1))/(.shacklecount-1))+.targetheight,0.)
call UnitRemoveAbility(.shackle<i>,039;Amrf039;)
set i = i+1
endloop
//The first shackle moves depending to the target's location
set x[4] = GetUnitX(.shackle[1])
set y[4] = GetUnitY(.shackle[1])
set x[5] = GetUnitX(.target)
set y[5] = GetUnitY(.target)
set a = Atan2(y[4]-y[5],x[4]-x[5])
call SetUnitX(.shackle[1],x[5]+.rate*Cos(a))
call SetUnitY(.shackle[1],y[5]+.rate*Sin(a))
call SetUnitFacing(.shackle[1],180+bj_RADTODEG*a)
endmethod
private static method iterate takes nothing returns nothing
local integer i = 1
local chain d
loop
exitwhen i > DATACOUNT
set d = DATA<i>
call d.periodic()
set i = i+1
endloop
endmethod
method SetSourceHeight takes real a returns nothing
set .sourceheight = a
endmethod
method SetTargetHeight takes real a returns nothing
set .targetheight = a
endmethod
method destroy takes nothing returns nothing
local integer i = 1
loop
exitwhen i > .shacklecount
call DestroyEffect(.shacklefx<i>)
call RemoveUnit(.shackle<i>)
set i = i+1
endloop
set .shacklecount = 0
set DATA[.list] = DATA[DATACOUNT]
set DATACOUNT = DATACOUNT - 1
call .deallocate()
endmethod
static method create takes unit source, unit target, string fx, real rate returns thistype
local thistype this = thistype.allocate()
local real a = Atan2(GetUnitY(target)-GetUnitY(source),GetUnitX(target)-GetUnitX(source))
set this.shacklecount = 1
set this.shackle[this.shacklecount+1] = source
set this.target = target
set this.rate = rate
set this.fx = fx
set this.sourceheight = 50.
set this.targetheight = 50.
set this.shackle[this.shacklecount] = CreateUnit(GetOwningPlayer(source),DUMMY,GetUnitX(target)-rate*Cos(a),GetUnitY(target)-rate*Sin(a),a*bj_RADTODEG)
set this.shacklefx[this.shacklecount] = AddSpecialEffectTarget(fx,this.shackle[this.shacklecount],"origin")
call UnitAddAbility(this.shackle[this.shacklecount],039;Amrf039;)
call SetUnitFlyHeight(this.shackle[this.shacklecount],this.targetheight,0.)
call UnitRemoveAbility(this.shackle[this.shacklecount],039;Amrf039;)
set DATACOUNT = DATACOUNT+1
set DATA[DATACOUNT] = this
set this.list = DATACOUNT
if DATACOUNT == 1 then
call TimerStart(TIMER,TIMEOUT,true,function chain.iterate)
endif
return this
endmethod
endstruct
endlibrary</i></i></i></i></i></i></i></i></i></i></i>