- Joined
- Jul 10, 2007
- Messages
- 6,306
JASS:
library MapGrid /* v1.0.0.4
*************************************************************************************
*
* Creates a grid of 64x64 tiles across the map. These tiles allow one to figure out
* whenever a unit moves a certain distance. This is useful for checking when units
* leave ranges of other units. Rather than a timer or a unit movement list, one can
* check only when units have moved a certain distance.
*
* While this is not as accurate as other things, it performs much better.
*
*************************************************************************************
*
* */uses/*
* */ WorldBounds /* hiveworkshop.com/forums/jass-functions-413/snippet-worldbounds-180494/
* */ Event /* hiveworkshop.com/forums/submissions-414/snippet-event-186555/
* */ UnitIndexer /* hiveworkshop.com/forums/jass-functions-413/unit-indexer-172090/
*
**************************************************************************************
*
* struct MapGrid extends array
*
* readonly static Event CHANGE_CELL
* - Fires whenever a unit changes to a different cell on the map (128x128 tile)
* readonly static unit EVENT_UNIT
* - The unit that changed cells
*
*************************************************************************************/
globals
private integer cy=0
private integer my=0
private integer cx=0
private region r1=null
private boolean array ur
private integer array un
private timer ut=CreateTimer()
endglobals
private function C takes nothing returns boolean
local integer y=cy
local integer x=cx
local integer dy=my
local rect r2
loop
exitwhen y>=dy
set r2=Rect(x,y,x+64,y+64)
call RegionAddRect(r1,r2)
call RemoveRect(r2)
set y=y+128
endloop
set r2=null
return false
endfunction
private function T takes nothing returns nothing
local UnitIndex i=un[0]
loop
set ur[i]=false
call i.unlock()
set i=un[i]
exitwhen i==0
endloop
set un[0]=0
endfunction
private function B takes nothing returns boolean
local UnitIndex i=GetIndexedUnitId()
call i.lock()
set un[i]=un[0]
set un[0]=i
set ur[i]=true
call TimerStart(ut,0,false,function T)
return false
endfunction
private module I
private static method E takes nothing returns boolean
local unit u=EVENT_UNIT
set EVENT_UNIT=GetTriggerUnit()
if (not ur[GetUnitUserData(EVENT_UNIT)]) then
call CHANGE_CELL.fire()
endif
set EVENT_UNIT=u
set u=null
return false
endmethod
private static method onInit takes nothing returns nothing
//will loop over the entire map and create a region+rect at every 128x128 square
local integer x=R2I(WorldBounds.minX)+512
local trigger t=CreateTrigger()
local conditionfunc b=Condition(function C)
local integer mx
local integer s=-1
local trigger q=CreateTrigger()
local region r2=CreateRegion()
local region r3
set r1=CreateRegion()
call TriggerRegisterEnterRegion(q,r1,null)
call TriggerRegisterLeaveRegion(q,r1,null)
call TriggerRegisterEnterRegion(q,r2,null)
call TriggerRegisterLeaveRegion(q,r2,null)
set CHANGE_CELL=CreateEvent()
call TriggerAddCondition(q,Condition(function thistype.E))
call TriggerAddCondition(t,b)
set cy=R2I(WorldBounds.minY)+256
set my=R2I(WorldBounds.maxY)-256
set mx=R2I(WorldBounds.maxX)-512
loop
exitwhen x>=mx
set cx=x
call TriggerEvaluate(t)
set x=x+64
set cy=cy+64*s
set s=-s
set r3=r1
set r1=r2
set r2=r3
endloop
call DestroyCondition(b)
call DestroyTrigger(t)
set t=null
set b=null
set q=null
set r1=null
set r2=null
set r3=null
call RegisterUnitIndexEvent(Condition(function B),UnitIndexer.INDEX)
endmethod
endmodule
struct MapGrid extends array
readonly static Event CHANGE_CELL=0
readonly static unit EVENT_UNIT=null
implement I
endstruct
endlibrary
Last edited: