- Joined
- Nov 30, 2007
- Messages
- 1,202
Farm Land System
This is a Farm Land System. Basicially you can decide which terrain should allow the spawning of farmland and how much return (0-100%) that pieace of land should give. You can use it to change income or food supply given by a unit or what ever you want. I'd like to make this into a system and not only for my personal use so any feedback on that would be appreciated.
If you just want to copy and paste the code to test it you need to use ashenvale tilsets, a worker to build farms and a unit indexing system.
This is a Farm Land System. Basicially you can decide which terrain should allow the spawning of farmland and how much return (0-100%) that pieace of land should give. You can use it to change income or food supply given by a unit or what ever you want. I'd like to make this into a system and not only for my personal use so any feedback on that would be appreciated.
If you just want to copy and paste the code to test it you need to use ashenvale tilsets, a worker to build farms and a unit indexing system.
JASS:
library FarmLand initializer init
globals
private real array x
private real array y
private integer max
private hashtable hash = InitHashtable()
real array farmReturn
endglobals
private function IsUnitFarm takes unit u returns boolean
if (GetUnitTypeId(u) == 'hhou') then
return true
endif
return false
endfunction
private function GrowFarm takes unit u returns nothing
local integer uID = GetUnitUserData(u)
local real x0 = GetUnitX(u)
local real y0 = GetUnitY(u)
local real x1
local real y1
local texttag tt
local integer i = 0
local real c = 0
local real d = 0
local real r
local string s
loop
exitwhen i > max
set x1 = x0 + x[i]
set y1 = y0 + y[i]
set r = LoadReal(hash, 0, GetTerrainType(x1,y1))
if (r > 0) then
call SaveInteger(hash, R2I(x1), R2I(y1), GetTerrainType(x1,y1))
call SaveUnitHandle(hash, R2I(x1), R2I(y1), u)
call SetTerrainType(x1,y1,'Vcrp',-1,1,1)
set c = c + r
set d = d + 1
endif
set i = i + 1
endloop
set farmReturn[uID] = (farmReturn[uID]*(max+1-d) + c)/(max+1)
if (GetLocalPlayer() == GetOwningPlayer(u)) then // this wont crash?
set tt = CreateTextTag()
call SetTextTagPermanent(tt,false)
call SetTextTagColor(tt, 255, 255, 255, 255)
call SetTextTagPosUnit(tt,u,50)
call SetTextTagVelocityBJ(tt,30,90)
//
//call SetTextTagVisibility(tt,true) // This isnt needed?
//
call SetTextTagFadepoint(tt,3.5)
call SetTextTagLifespan(tt,4)
if (farmReturn[uID] < 0.2) then
set s = "|c00ff0000"
elseif (farmReturn[uID] >= 0.2 and farmReturn[uID] < 0.4) then
set s = "|c00bf3f00"
elseif (farmReturn[uID] >= 0.4 and farmReturn[uID] < 0.6) then
set s = "|c007f7f00"
elseif (farmReturn[uID] >= 0.6 and farmReturn[uID] < 0.8) then
set s = "|c003fbf00"
elseif (farmReturn[uID] >= 0.8) then
set s = "|c0000ff00"
endif
call SetTextTagText(tt,s + I2S(R2I(farmReturn[uID]*100)) + "%|r", 12*0.023/10)
//
set tt = null // is this even needed?
//
endif
endfunction
private function Update takes nothing returns nothing
local unit u = GetEnumUnit()
if (IsUnitFarm(u) == true and GetUnitState(u, UNIT_STATE_LIFE) >= 1) then
call GrowFarm(u)
endif
set u = null
endfunction
private function RemoveFarm takes unit u returns nothing
local real x0 = GetUnitX(u)
local real y0 = GetUnitY(u)
local real x1
local real y1
local integer i = 0
loop
exitwhen i > max
set x1 = x0+x[i]
set y1 = y0+y[i]
if (GetTerrainType(x1,y1) == 'Vcrp' and u == LoadUnitHandle(hash, R2I(x1), R2I(y1))) then
call SetTerrainType(x1,y1,LoadInteger(hash, R2I(x1),R2I(y1)),-1,1,1)
call RemoveSavedInteger(hash, R2I(x1),R2I(y1))
endif
set i = i + 1
endloop
call RemoveSavedHandle(hash, R2I(x1), R2I(y1))
set farmReturn[GetUnitUserData(u)] = 0
endfunction
private function main takes nothing returns boolean
local unit u = GetTriggerUnit()
local location l
if (IsUnitFarm(u) == true) then
if (GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_FINISH) then
call GrowFarm(u)
else
set l = GetUnitLoc(u)
call RemoveFarm(u)
set bj_wantDestroyGroup = TRUE
call ForGroup(GetUnitsInRangeOfLocMatching(700,l,null), function Update)
// the 700 number should be replaced with something flexible based on the radian of the grid. (Don't know how!)
call RemoveLocation(l)
endif
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))
set t = null
// Change this!
call SaveReal(hash, 0, 'Agrs', 1.00)
call SaveReal(hash, 0, 'Adrg', 0.75)
call SaveReal(hash, 0, 'Adrd', 0.40)
// Change this!
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
Attachments
Last edited: