- Joined
- Jul 29, 2007
- Messages
- 5,174
I tried making this code that reduces the mana of any mechanical unit on the map that is moving... it isn't working lol.
I can only guess its because I don't know how to use unit groups... lol.
Here's the code
I can only guess its because I don't know how to use unit groups... lol.
Here's the code
JASS:
// This will reduce the mana of any mechanical unit on the map upon moving.
// The actual value is the distance that unit moves within 0.2 seconds.
// If you want to change that value, see the comment in 13 lines.
function mechanicCon takes nothing returns boolean
return IsUnitType(GetEnumUnit(),UNIT_TYPE_MECHANICAL)
endfunction
function mecahnictimer takes nothing returns nothing
local timer t = GetExpiredTimer()
local unit whichUnit = GetHandleUnit(t, "whichUnit")
local real x = GetHandleReal(t, "x")
local real y = GetHandleReal(t, "y")
local real newx = GetUnitX(whichUnit)
local real newy = GetUnitY(whichUnit)
local real distance = DistanceBetweenPointsXY(x, y, newx, newy)
local real mana = GetUnitStatePercent(whichUnit, UNIT_STATE_MANA, UNIT_STATE_MAX_MANA)
call SetUnitState(whichUnit, UNIT_STATE_MANA, mana-distance) // if you want to reduce or raise the value, just make it something like "mana-distance*20" or "mana-distance/20"
call FlushHandleLocals(t)
call DestroyTimer(t)
set t = null
set whichUnit = null
endfunction
function mechanic takes nothing returns nothing
local timer t = CreateTimer()
local group g = CreateGroup()
local unit whichUnit
local real x
local real y
call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, Condition(function mechanicCon))
loop
set whichUnit = FirstOfGroup(g)
exitwhen whichUnit == null
call GroupRemoveUnit(g, whichUnit)
set x = GetUnitX(whichUnit)
set y = GetUnitY(whichUnit)
call SetHandleReal(t, "x", x)
call SetHandleReal(t, "y", y)
call SetHandleHandle(t, "whichUnit", whichUnit)
call TimerStart(t, 0.2, true, function mecahnictimer)
endloop
set t = null
call DestroyGroup(g)
set g = null
set whichUnit = null
endfunction
//==== Init Trigger NewTrigger ====
function InitTrig_NewTrigger takes nothing returns nothing
set gg_trg_NewTrigger = CreateTrigger()
call TriggerRegisterTimerEventPeriodic( gg_trg_NewTrigger, 0.20 )
call TriggerAddAction(gg_trg_NewTrigger, function mechanic)
endfunction