- Joined
- Nov 30, 2007
- Messages
- 1,202
Can you please help me understand why function "ClearArea" isn't working properly? The idea here is when a building other then farm finishes the area around it should be cleared from farm tile. I tested creating peasants in the area and sure enough it's in the right spot. But no tile gets changed back for some reason. :/
It finds the tile, and all but nothing happens to it for some reason even though the same function works in ClearFarm.
It finds the tile, and all but nothing happens to it for some reason even though the same function works in ClearFarm.
JASS:
library FarmLand initializer Init
globals
private constant integer farmTile = 'Vcrp'
private constant integer radius = 700
private constant integer gridArea = 5 // Change Name
private item dummy
private hashtable hash = InitHashtable()
private real array x
private real array y
private integer max
private real array sum
real array farmYield
endglobals
private function IsUnitFarm takes unit u returns boolean
if (GetUnitTypeId(u) == 'hhou') then
return true
endif
return false
endfunction
private function Display takes unit u returns nothing
local texttag txt
local string str
local integer i = GetUnitUserData(u)
if GetLocalPlayer() == GetOwningPlayer(u) then
set txt = CreateTextTag()
call SetTextTagPermanent(txt, false)
call SetTextTagColor(txt, 255, 255, 255, 255)
call SetTextTagPosUnit(txt, u, 50)
call SetTextTagVelocityBJ(txt, 30, 90)
call SetTextTagFadepoint(txt, 3.5)
call SetTextTagLifespan(txt, 4)
if farmYield[i] <= 0.2 then
set str = "|c00ff0000"
elseif farmYield[i] <= 0.4 then
set str = "|c00bf3f00"
elseif farmYield[i] <= 0.6 then
set str = "|c007f7f00"
elseif farmYield[i] <= 0.8 then
set str = "|c003fbf00"
else
set str = "|c0000ff00"
endif
call SetTextTagText(txt, str + I2S(R2I(farmYield[i]*100)) + "%|r", 12*0.023/10)
set txt = null
endif
endfunction
private function GetGridYield takes real x1, real y1 returns real
return LoadReal(hash, 0, GetTerrainType(x1, y1))
endfunction
private function SaveGrid takes real x1, real y1, unit u returns nothing
call SaveInteger(hash, R2I(x1), R2I(y1), GetTerrainType(x1,y1))
call SaveUnitHandle(hash, R2I(x1), R2I(y1), u)
endfunction
private function IsGridWalkable takes real x0, real y0 returns boolean
local real dx
local real dy
call SetItemPosition(dummy, x0, y0)
set dx = x0 - GetItemX(dummy)
set dy = y0 - GetItemY(dummy)
if SquareRoot(dx*dx + dy*dy) < 32 then
return true
endif
return false
endfunction
private function GrowFarm takes unit u returns nothing
local integer i = 0
local integer id = GetUnitUserData(u)
local real x1
local real y1
local real yield
local real yield_in = farmYield[id]
call SetItemVisible(dummy, true)
loop
exitwhen i > max
set x1 = GetUnitX(u) + x[i]
set y1 = GetUnitY(u) + y[i]
set yield = GetGridYield(x1, y1)
if yield > 0 and IsGridWalkable(x1, y1) then
set sum[id] = sum[id] + yield
call SaveGrid(x1, y1, u)
call SetTerrainType(x1, y1, farmTile, -1, 1, 1)
endif
set i = i + 1
endloop
call SetItemVisible(dummy, false)
set farmYield[id] = sum[id]/max
if yield_in != farmYield[id] then
call Display(u)
endif
endfunction
private function ResetTile takes real x0, real y0 returns nothing
call SetTerrainType(x0, y0, LoadInteger(hash, R2I(x0), R2I(y0)), -1, 1, 1)
call RemoveSavedInteger(hash, R2I(x0),R2I(y0))
call RemoveSavedHandle(hash, R2I(x0), R2I(y0))
endfunction
private function ClearArea takes real x0, real y0, integer size returns nothing
local integer i = 0
local integer j
local real x1
local real y1
set x0 = x0 - 64*size
set y0 = y0 + 64*size
call BJDebugMsg("WHYYY!!??")
loop
exitwhen i == size
set j = 0
loop
exitwhen j == size
set x1 = x0 + 128*i
set y1 = y0 - 128*j
call CreateUnit(Player(0), 'hpea', x1, y1, 0)
if GetTerrainType(x1, y1) == farmTile then
call BJDebugMsg("HELLO!")
call ResetTile(x1, y1)
endif
set j = j + 1
endloop
set i = i + 1
endloop
endfunction
private function ClearFarm takes unit u returns nothing
local integer i = 0
local real x1
local real y1
loop
exitwhen i > max
set x1 = GetUnitX(u) + x[i]
set y1 = GetUnitY(u) + y[i]
if GetTerrainType(x1, y1) == farmTile and u == LoadUnitHandle(hash, R2I(x1), R2I(y1)) then
call ResetTile(x1, y1)
endif
set i = i + 1
endloop
endfunction
private function UpdateArea takes real x1, real y1 returns nothing
local unit u
local group g = CreateGroup()
call GroupEnumUnitsInRange(g, x1, y1, radius, null)
loop
set u = FirstOfGroup(g)
exitwhen u == null
call GroupRemoveUnit(g, u)
if IsUnitFarm(u) == true and GetUnitState(u, UNIT_STATE_LIFE) >= 1 then
call GrowFarm(u)
endif
endloop
call DestroyGroup(g)
set u = null
endfunction
private function Main takes nothing returns boolean
local unit u = GetTriggerUnit()
local real x0 = GetUnitX(u)
local real y0 = GetUnitY(u)
if IsUnitFarm(u) == true then
if GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_FINISH then
set farmYield[GetUnitUserData(u)] = -1
call GrowFarm(u)
else
call ClearFarm(u)
call UpdateArea(x0, y0)
endif
else
call ClearArea(x0, y0, gridArea)
//call UpdateArea(x0, y0)
endif
set u = null
return false
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
local player p
loop
exitwhen i > 11
set p = Player(i)
call TriggerRegisterPlayerUnitEvent(t,p,EVENT_PLAYER_UNIT_CONSTRUCT_FINISH, null)
call TriggerRegisterPlayerUnitEvent(t,p,EVENT_PLAYER_UNIT_DEATH, null)
set i = i + 1
endloop
call TriggerAddCondition(t, Condition(function Main))
// Dummy Item
set dummy = CreateItem('I000', 0, 0)
// Yield
call SaveReal(hash, 0, 'Agrs', 1.00) // Grass
call SaveReal(hash, 0, 'Adrg', 0.75) // Grassy Dirt
call SaveReal(hash, 0, 'Adrd', 0.40) // Rough Dirt
// Farm Grid
set x[0] = 0
set y[0] = 0
set x[1] = 128
set y[1] = 0
set x[2] = -128
set y[2] = 0
set x[3] = 0
set y[3] = 128
set x[4] = 0
set y[4] = -128
set x[5] = 128
set y[5] = 128
set x[6] = -128
set y[6] = 128
set x[7] = 128
set y[7] = -128
set x[8] = -128
set y[8] = -128
set x[9] = 0
set y[9] = 256
set x[10] = 0
set y[10] = -256
set x[11] = 256
set y[11] = 0
set x[12] = -256
set y[12] = 0
set max = 12
endfunction
endlibrary
Last edited: