- Joined
- May 16, 2007
- Messages
- 7,284
how do i detect if a unit is not moving for a certain amount of time?
// 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(GetFilterUnit(),UNIT_TYPE_MECHANICAL) == true
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 distance = DistanceBetweenPointsXY(x, y, GetUnitX(whichUnit), GetUnitY(whichUnit))
local real mana = GetUnitState(whichUnit, UNIT_STATE_MANA )
if distance > 0 then
call SetUnitState(whichUnit, UNIT_STATE_MANA, mana-distance/20) // if you want to reduce or raise the value, just make it something like "mana-distance*20" or "mana-distance/20"
endif
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 t = null
set whichUnit = FirstOfGroup(g)
exitwhen whichUnit == null
call GroupRemoveUnit(g, whichUnit)
set t = CreateTimer()
call SetHandleReal(t, "x", GetUnitX(whichUnit))
call SetHandleReal(t, "y", GetUnitY(whichUnit))
call SetHandleHandle(t, "whichUnit", whichUnit)
call TimerStart(t, 0.2, false, function mecahnictimer)
endloop
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
// --------------PUT ME IN YOUR HEADER-------------------
//****************************************************************************************
// Distance between two points using coordinates
// call DistanceBetweenPointsXY(real x1, real y1, real x2, real y2)
function DistanceBetweenPointsXY takes real x1, real y1, real x2, real y2 returns real
local real dx = oldx - x
local real dy = oldy - y
return SquareRoot(dx * dx + dy * dy)
endfunction
function DistanceBetweenPointsXY takes real x1, real y1, real x2, real y2 returns real
local real dx = x1 - x2
local real dy = y1 - y2
return SquareRoot(dx * dx + dy * dy)
endfunction
function onesecondCon takes nothing returns boolean
return GetUnitTypeId(GetFilterUnit()) == 'h000'
endfunction
function onesecondtimer 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 distance = DistanceBetweenPointsXY(x, y, GetUnitX(whichUnit), GetUnitY(whichUnit))
if distance == 0 then
call KillUnit(whichUnit)
endif
call FlushHandleLocals(t)
call DestroyTimer(t)
set t = null
set whichUnit = null
endfunction
function onesecond 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 onesecondCon))
loop
set t = null
set whichUnit = FirstOfGroup(g)
exitwhen whichUnit == null
call GroupRemoveUnit(g, whichUnit)
set t = CreateTimer()
call SetHandleReal(t, "x", GetUnitX(whichUnit))
call SetHandleReal(t, "y", GetUnitY(whichUnit))
call SetHandleHandle(t, "whichUnit", whichUnit)
call TimerStart(t, 1, false, function onesecondtimer)
endloop
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 onesecond)
endfunction
I hate to break it to you, but this is the GUI section. Oh and using handle vars is very slow. I try to avoid them at almost all times.
ok now im confused...
which scripts to i put in the header
and which scripts do i put in a trigger?
function DistanceBetweenPointsXY takes real x1, real y1, real x2, real y2 returns real
local real dx = x1 - x2
local real dy = y1 - y2
return SquareRoot(dx * dx + dy * dy)
endfunction
You do the reverse of handle vars. Instead of attaching the struct to the unit, you attach the unit to the struct. Then periodiclly you cycle through all the structs. (Instead of cylcing through the units) Of course there are times when this is neither practical nor smart, but I've used it many many times.And how exacly can you make it without handle vars ? x.X
call TimerStart(t, 1, false, function mecahnictimer)
function mechanicCon takes nothing returns boolean
return IsUnitType(GetFilterUnit(),UNIT_TYPE_MECHANICAL) == true
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 distance = DistanceBetweenPointsXY(x, y, GetUnitX(whichUnit), GetUnitY(whichUnit)) local real mana = GetUnitState(whichUnit, UNIT_STATE_MANA ) if distance > 0 then
call SetUnitState(whichUnit, UNIT_STATE_MANA, mana-distance/20)
endif
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 t = null
set whichUnit = FirstOfGroup(g)
exitwhen whichUnit == null
call GroupRemoveUnit(g, whichUnit)
set t = CreateTimer()
call SetHandleReal(t, "x", GetUnitX(whichUnit))
call SetHandleReal(t, "y", GetUnitY(whichUnit))
call SetHandleHandle(t, "whichUnit", whichUnit)
call TimerStart(t, 0.2, false, function mecahnictimer)
endloop
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
function onesecondCon takes nothing returns boolean
return GetUnitTypeId(GetFilterUnit()) == 'h000'
endfunction
function onesecondtimer 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 distance = DistanceBetweenPointsXY(x, y, GetUnitX(whichUnit), GetUnitY(whichUnit))
if distance == 0 then
call KillUnit(whichUnit)
endif
call FlushHandleLocals(t)
call DestroyTimer(t)
set t = null
set whichUnit = null
endfunction
function onesecond 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 onesecondCon))
loop
set t = null
set whichUnit = FirstOfGroup(g)
exitwhen whichUnit == null
call GroupRemoveUnit(g, whichUnit)
set t = CreateTimer()
call SetHandleReal(t, "x", GetUnitX(whichUnit))
call SetHandleReal(t, "y", GetUnitY(whichUnit))
call SetHandleHandle(t, "whichUnit", whichUnit)
call TimerStart(t, 1, false, function onesecondtimer)
endloop
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 onesecond)
endfunction
return GetUnitTypeId(GetFilterUnit()) == '[COLOR="Red"]h000[/COLOR]'