So I just began learning vJass today, about a couple of hours ago. All I had before today was Handle Vars knowledge, for the record.
For practice, I was trying to convert this Game-Cache-heavy set of functions (just the projectile one in particular, the others are included because they're required)...
...into vJass ones. I came up with the following:
Questions:
1. Is the code as efficient as could be, and have I missed anything out? Is anything leaking? I ran the trigger in the WE and got exactly the same results, so I was wondering if that was all to using structs.
2. I'm planning to put this function and a lot of others into a library. Thing is, if a library is placed at the top of the *.j file, do the Handle Vars functions (or those of any other attachment system) have to be a part of the library as well? Would I have to create a new library for them? I'd prefer to leave them in the header, if possible, to organise the code better.
3. I've always called the projectile function this way:
I dealt with the actions this way (Game Cache):
I'm now doing (structs):
Is this okay, and is this the best way? Now that I'm using structs, are there alternative possibilities?
4. Finally, I know the old code (projectile actions) wasn't MUI. I know structs instance, but I'm not exactly sure to what extent. Therefore, my question is: is the new one MUI?
Thanks in advance for the help.
For practice, I was trying to convert this Game-Cache-heavy set of functions (just the projectile one in particular, the others are included because they're required)...
JASS:
function PolarOffsetX takes real originx, real offset, real angle returns real
return originx + offset * Cos(angle)
endfunction
function PolarOffsetY takes real originy, real offset, real angle returns real
return originy + offset * Sin(angle)
endfunction
function AngleBetweenUnits takes unit a, unit b returns real // Values are taken in radians.
local real dx = GetUnitX(b) - GetUnitX(a)
local real dy = GetUnitY(b) - GetUnitY(a)
return Atan2(dy, dx)
endfunction
function DistanceBetweenUnits takes unit a, unit b returns real
local real dx = GetUnitX(b) - GetUnitX(a)
local real dy = GetUnitY(b) - GetUnitY(a)
return SquareRoot(dx * dx + dy * dy)
endfunction
function SafeX takes real x returns real
local real maparea=GetRectMinX(bj_mapInitialPlayableArea)+50
if(x<maparea)then
return maparea
endif
set maparea=GetRectMaxX(bj_mapInitialPlayableArea)-50
if(x>maparea)then
return maparea
endif
return x
endfunction
function SafeY takes real y returns real
local real maparea=GetRectMinY(bj_mapInitialPlayableArea)+50
if(y<maparea)then
return maparea
endif
set maparea=GetRectMaxY(bj_mapInitialPlayableArea)-50
if(y>maparea)then
return maparea
endif
return y
endfunction
function GetHandleUnitString takes string actions, string name returns unit
return GetStoredInteger(LocalVars(), actions, name)
return null
endfunction
function FlushHandleString takes string actions returns nothing
call FlushStoredMission(LocalVars(), actions)
endfunction
function CreateProjectilePeriodic takes nothing returns nothing
local trigger t = GetTriggeringTrigger()
local real speed = GetHandleReal(t, "speed")
local unit source = GetHandleUnit(t, "source")
local unit target = GetHandleUnit(t, "target")
local unit projectile = GetHandleUnit(t, "projectile")
local string actions = GetHandleString(t, "actions")
local real targetx = GetUnitX(target)
local real targety = GetUnitY(target)
local real projectilex = GetUnitX(projectile)
local real projectiley = GetUnitY(projectile)
local real angle = AngleBetweenUnits(projectile, target)
local real dist = DistanceBetweenUnits(projectile, target)
local real offset = speed * 0.02
local real x = PolarOffsetX(projectilex, offset, angle)
local real y = PolarOffsetY(projectiley, offset, angle)
call SetUnitX(projectile, SafeX(x))
call SetUnitY(projectile, SafeY(y))
call SetUnitFacing(projectile, angle * bj_RADTODEG)
if dist <= offset then
call SetUnitX(projectile, SafeX(targetx))
call SetUnitY(projectile, SafeY(targety))
call KillUnit(projectile)
if actions != null then
call StoreInteger(LocalVars(), actions, "source", H2I(source))
call StoreInteger(LocalVars(), actions, "target", H2I(target))
call ExecuteFunc(actions)
endif
call DisposeOfTrigger(t)
elseif GetUnitState(target, UNIT_STATE_LIFE) < 0. then
call KillUnit(projectile)
call DisposeOfTrigger(t)
endif
set t = null
set target = null
set source = null
set projectile = null
endfunction
function CreateProjectile takes unit source, unit target, integer projectileid, real speed, string actions returns unit
local trigger t = CreateTrigger()
local unit projectile = CreateUnit(GetOwningPlayer(source), projectileid, GetUnitX(source), GetUnitY(source), bj_RADTODEG * AngleBetweenUnits(source, target))
call SetHandleHandle(t, "projectile", projectile)
call SetHandleHandle(t, "source", source)
call SetHandleHandle(t, "target", target)
call SetHandleString(t, "actions", actions)
call SetHandleReal(t, "speed", speed)
call TriggerRegisterTimerEvent(t, .02, true)
call TriggerAddAction(t, function CreateProjectilePeriodic)
set t = null
return projectile
endfunction
...into vJass ones. I came up with the following:
JASS:
scope CreateProjectile
struct projectile
unit dummy
real speed
string actions
unit source
unit target
real targetx
real targety
method onDestroy takes nothing returns nothing
set .dummy = null
set .source = null
set .target = null
endmethod
endstruct
function AngleBetweenUnits takes unit a, unit b returns real
local real dx = GetUnitX(b) - GetUnitX(a)
local real dy = GetUnitY(b) - GetUnitY(a)
return Atan2(dy, dx)
endfunction
function SafeX takes real x returns real
local real maparea=GetRectMinX(bj_mapInitialPlayableArea)+50
if(x<maparea)then
return maparea
endif
set maparea=GetRectMaxX(bj_mapInitialPlayableArea)-50
if(x>maparea)then
return maparea
endif
return x
endfunction
function SafeY takes real y returns real
local real maparea=GetRectMinY(bj_mapInitialPlayableArea)+50
if(y<maparea)then
return maparea
endif
set maparea=GetRectMaxY(bj_mapInitialPlayableArea)-50
if(y>maparea)then
return maparea
endif
return y
endfunction
function DisposeOfTrigger takes trigger t returns nothing
call DisableTrigger(t)
call FlushHandleLocals(t)
call DestroyTrigger(t)
endfunction
function CreateProjectilePeriodic takes nothing returns nothing
local trigger t = GetTriggeringTrigger()
local projectile p = projectile(GetHandleInt(t, "p"))
local real dummyx = GetUnitX(p.dummy)
local real dummyy = GetUnitY(p.dummy)
local real dx = p.targetx - dummyx
local real dy = p.targety - dummyy
local real angle = Atan2(dy, dx)
local real dist = SquareRoot(dx * dx + dy * dy)
local real offset = p.speed * 0.02
local real x = dummyx + offset * Cos(angle)
local real y = dummyy + offset * Sin(angle)
call SetUnitX(p.dummy, SafeX(x))
call SetUnitY(p.dummy, SafeY(y))
call SetUnitFacing(p.dummy, angle * bj_RADTODEG)
if dist <= offset then
call SetUnitX(p.dummy, SafeX(p.targetx))
call SetUnitY(p.dummy, SafeY(p.targety))
call KillUnit(p.dummy)
if p.actions != null then
call ExecuteFunc(p.actions)
endif
call DisposeOfTrigger(t)
call p.destroy()
elseif GetUnitState(p.target, UNIT_STATE_LIFE) < 0. then
call KillUnit(p.dummy)
call DisposeOfTrigger(t)
call p.destroy()
endif
set t = null
endfunction
function CreateProjectile takes unit source, unit target, integer projectileid, real speed, string actions returns unit
local trigger t = CreateTrigger()
local projectile p = projectile.create()
local unit dummy = CreateUnit(GetOwningPlayer(source), projectileid, GetUnitX(source), GetUnitY(source), bj_RADTODEG * AngleBetweenUnits(source, target))
set p.dummy = dummy
set p.source = source
set p.target = target
set p.targetx = GetUnitX(target)
set p.targety = GetUnitY(target)
set p.speed = speed
set p.actions = actions
call SetHandleInt(t, "p", p)
call TriggerRegisterTimerEvent(t, .02, true)
call TriggerAddAction(t, function CreateProjectilePeriodic)
set t = null
set dummy = null
return p.dummy
endfunction
endscope
Questions:
1. Is the code as efficient as could be, and have I missed anything out? Is anything leaking? I ran the trigger in the WE and got exactly the same results, so I was wondering if that was all to using structs.
2. I'm planning to put this function and a lot of others into a library. Thing is, if a library is placed at the top of the *.j file, do the Handle Vars functions (or those of any other attachment system) have to be a part of the library as well? Would I have to create a new library for them? I'd prefer to leave them in the header, if possible, to organise the code better.
3. I've always called the projectile function this way:
JASS:
function Trig_Test_Ability_Actions takes nothing returns nothing
local unit caster = GetTriggerUnit()
local unit target = GetSpellTargetUnit()
call CreateProjectile(caster, target, 039;e000039;, 900., "ProjectileActions")
set caster = null
set target = null
endfunction
I dealt with the actions this way (Game Cache):
JASS:
function ProjectileActions takes nothing returns nothing
local unit target = GetHandleUnitString("ProjectileActions", "target")
call RemoveUnit(target)
endfunction
I'm now doing (structs):
JASS:
function ProjectileActions takes nothing returns nothing
local projectile p = projectile(GetHandleInt(GetTriggeringTrigger(), "p"))
call RemoveUnit(p.target)
endfunction
Is this okay, and is this the best way? Now that I'm using structs, are there alternative possibilities?
4. Finally, I know the old code (projectile actions) wasn't MUI. I know structs instance, but I'm not exactly sure to what extent. Therefore, my question is: is the new one MUI?
Thanks in advance for the help.