library FiledLib initializer Init
globals
private unit array FIELD[10][10]
private unit array FIELD_UNIT[10][10]
private boolean array FIELD_ALLOWED[10][10]
private hashtable HASH = InitHashtable()
private group G = CreateGroup()
endglobals
function SetUnitFieldLoc takes unit u, integer i, integer j, boolean move returns nothing
local integer row = 1
local integer col = 1
loop
loop
set FIELD_ALLOWED[row][col] = false
call SetUnitColor(FIELD[row][col], PLAYER_COLOR_BROWN)
set col = col + 1
exitwhen col > 5
endloop
set col = 0
set row = row + 1
exitwhen row > 5
endloop
if FIELD[i][j+1] != null then
if FIELD_UNIT[i][j+1] == null then
call SetUnitColor(FIELD[i][j+1], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i][j+1] = true
else
call SetUnitColor(FIELD[i][j+1], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i][j+1] = false
endif
endif
if FIELD[i][j+2] != null then
if FIELD_UNIT[i][j+2] == null then
call SetUnitColor(FIELD[i][j+2], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i][j+2] = true
else
call SetUnitColor(FIELD[i][j+2], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i][j+2] = false
endif
endif
if FIELD[i+1][j] != null then
if FIELD_UNIT[i+1][j] == null then
call SetUnitColor(FIELD[i+1][j], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i+1][j] = true
else
call SetUnitColor(FIELD[i+1][j], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i+1][j] = false
endif
endif
if FIELD[i+2][j] != null then
if FIELD_UNIT[i+2][j] == null then
call SetUnitColor(FIELD[i+2][j], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i+2][j] = true
else
call SetUnitColor(FIELD[i+2][j], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i+2][j] = false
endif
endif
if FIELD[i][j-1] != null then
if FIELD_UNIT[i][j-1] == null then
call SetUnitColor(FIELD[i][j-1], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i][j-1] = true
else
call SetUnitColor(FIELD[i][j-1], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i][j-1] = false
endif
endif
if FIELD[i][j-2] != null then
if FIELD_UNIT[i][j-2] == null then
call SetUnitColor(FIELD[i][j-2], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i][j-2] = true
else
call SetUnitColor(FIELD[i][j-2], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i][j-2] = false
endif
endif
if FIELD[i-1][j] != null then
if FIELD_UNIT[i-1][j] == null then
call SetUnitColor(FIELD[i-1][j], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i-1][j] = true
else
call SetUnitColor(FIELD[i-1][j], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i-1][j] = false
endif
endif
if FIELD[i-2][j] != null then
if FIELD_UNIT[i-2][j] == null then
call SetUnitColor(FIELD[i-2][j], PLAYER_COLOR_GREEN)
set FIELD_ALLOWED[i-2][j] = true
else
call SetUnitColor(FIELD[i-2][j], PLAYER_COLOR_RED)
set FIELD_ALLOWED[i-2][j] = false
endif
endif
if move then
call SetUnitPosition(u, GetUnitX(FIELD[i][j]), GetUnitY(FIELD[i][j]))
call SaveInteger(HASH, GetHandleId(u), 0, GetHandleId(FIELD[i][j]))
call SaveInteger(HASH, GetHandleId(u), 1, i)
call SaveInteger(HASH, GetHandleId(u), 2, j)
set FIELD_UNIT[i][j] = u
endif
endfunction
function TryMove takes nothing returns nothing
local integer id = GetHandleId(GetSpellTargetUnit())
local integer i = LoadInteger(HASH, id, 1)
local integer j = LoadInteger(HASH, id, 2)
if FIELD_ALLOWED[i][j] then
set FIELD_UNIT[LoadInteger(HASH, GetHandleId(GetTriggerUnit()), 1)][LoadInteger(HASH, GetHandleId(GetTriggerUnit()), 2)] = null
call SetUnitFieldLoc(GetTriggerUnit(), i, j, true)
endif
endfunction
function ShowFields takes nothing returns nothing
local integer id = LoadInteger(HASH, GetHandleId(GetTriggerUnit()), 0)
local integer i = LoadInteger(HASH, id, 1)
local integer j = LoadInteger(HASH, id, 2)
call SetUnitFieldLoc(GetTriggerUnit(), i, j, false)
endfunction
function RegisterField takes integer i, integer j, real x, real y returns nothing
set FIELD[i][j] = CreateUnit(Player(15), 'ncop', x, y,0)
call SaveInteger(HASH, GetHandleId(FIELD[i][j]), 1, i)
call SaveInteger(HASH, GetHandleId(FIELD[i][j]), 2, j)
set FIELD_ALLOWED[i][j] = false
call SetUnitColor(FIELD[i][j], PLAYER_COLOR_BROWN)
endfunction
function Init takes nothing returns nothing
local trigger trig = CreateTrigger()
call RegisterField(1,1, -2880.00, 1472.00)
call RegisterField(1,2, -2496.00, 1472.00)
call RegisterField(1,3, -2080.00, 1472.00)
call RegisterField(1,4, -1696.00, 1472.00)
call RegisterField(1,5, -1344.00, 1472.00)
call RegisterField(2,1, -2880.00, 1056.00)
call RegisterField(2,2, -2496.00, 1056.00)
call RegisterField(2,3, -2080.00, 1056.00)
call RegisterField(2,4, -1696.00, 1056.00)
call RegisterField(2,5, -1344.00, 1056.00)
call RegisterField(3,1, -2880.00, 672.00)
call RegisterField(3,2, -2496.00, 672.00)
call RegisterField(3,3, -2080.00, 672.00)
call RegisterField(3,4, -1696.00, 672.00)
call RegisterField(3,5, -1344.00, 672.00)
call RegisterField(4,1, -2880.00, 288.00)
call RegisterField(4,2, -2496.00, 288.00)
call RegisterField(4,3, -2080.00, 288.00)
call RegisterField(4,4, -1696.00, 288.00)
call RegisterField(4,5, -1344.00, 288.00)
call RegisterField(5,1, -2880.00, -64.00)
call RegisterField(5,2, -2496.00, -64.00)
call RegisterField(5,3, -2080.00, -64.00)
call RegisterField(5,4, -1696.00, -64.00)
call RegisterField(5,5, -1344.00, -64.00)
//Test Unit and initial shit
call SetCameraPosition(-2080.00, 704.00)
call SetUnitFieldLoc(CreateUnit(Player(0), 'nvlk', -2880.00, 1472.00,0), 1,1,true)
call SetUnitFieldLoc(CreateUnit(Player(0), 'nvlk', -2080.00, 704.00,0), 3,3,true)
call SetUnitFieldLoc(CreateUnit(Player(0), 'nvlk', -2080.00, -64.00,0), 5,3,true)
call TriggerRegisterPlayerUnitEvent(trig, Player(0), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
call TriggerAddAction( trig, function TryMove )
set trig = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(trig, Player(0), EVENT_PLAYER_UNIT_SELECTED, null)
call TriggerAddAction( trig, function ShowFields )
set trig = null
endfunction
endlibrary