globals
integer array buildingOrder
integer array buildingType
constant integer CITY_RADIUS = 800
integer array owningCity
hashtable hash = InitHashtable()
endglobals
library CityBuilding initializer init
private function buildOrder takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer order
local integer i
local integer n = GetPlayerId(GetOwningPlayer(u))
local real x1
local real y1
local real x2
local real y2
local real dx
local real dy
if u == cityController[n] then
set order = GetIssuedOrderId()
set i = 1
loop
exitwhen buildingOrder[i] == null
if order == buildingOrder[i] then
set x1 = GetLocationX(GetUnitLoc(u))
set y1 = GetLocationY(GetUnitLoc(u))
set x2 = GetLocationX(GetOrderPointLoc())
set y2 = GetLocationY(GetOrderPointLoc())
set dx = x2 - x1
set dy = y2 - y1
if (SquareRoot(dx*dx + dy*dy) > CITY_RADIUS) then
set u = CreateUnit(Player(12), 'n000',x2,y2,0)
call UnitApplyTimedLife(u,'BTLF', 0.1)
endif
call SetUnitX(cityBuilder[n], x2)
call SetUnitY(cityBuilder[n], y2)
call IssueBuildOrderById(cityBuilder[n], buildingType[i], x2, y2)
endif
set i = i + 1
endloop
endif
set u = null
endfunction
private function startBuilding takes nothing returns nothing
local unit u = GetConstructingStructure()
local integer id = GetUnitUserData(u)
local integer city = selected[GetPlayerId(GetOwningPlayer(u))]
set owningCity[id] = city
call GroupAddUnit(cityBuildings[city], u)
call GroupAddUnit(cityBuildings[0], u)
set cityBuildingCount[city][0] = cityBuildingCount[city][0] + 1
call BJDebugMsg(I2S(cityBuildingCount[city][0]))
set u = null
endfunction
private function finishBuilding takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer id = GetUnitUserData(u)
local integer city = owningCity[id]
local integer load = LoadInteger(hash, GetUnitTypeId(u), 0)
set cityBuildingCount[city][0] = cityBuildingCount[city][0] - 1
set cityBuildingCount[city][load] = cityBuildingCount[city][load] + 1
call BJDebugMsg(I2S(cityBuildingCount[city][0]))
call BJDebugMsg("Building " + I2S(load) + ": " + I2S(cityBuildingCount[city][load]))
call GroupRemoveUnit(cityBuildings[0], u)
set u = null
endfunction
private function deathBuilding takes nothing returns nothing
local unit u = GetTriggerUnit()
local integer id = GetUnitUserData(u)
local integer city = owningCity[id]
local integer load
if IsUnitInGroup(u, cityBuildings[0]) == true then // This part don't work...
call GroupRemoveUnit(cityBuildings[0], u)
set cityBuildingCount[city][0] = cityBuildingCount[city][0] - 1
call BJDebugMsg(I2S(cityBuildingCount[city][0]))
else
set load = LoadInteger(hash, GetUnitTypeId(u), 0)
set cityBuildingCount[city][load] = cityBuildingCount[city][load] - 1
call BJDebugMsg(I2S(cityBuildingCount[city][load]))
endif
set u = null
endfunction
private function main takes nothing returns boolean
if GetTriggerEventId() == EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER then
call buildOrder()
elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_START then
call startBuilding()
elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_FINISH then
call finishBuilding()
elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_DEATH then
call deathBuilding()
endif
return false
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
loop
exitwhen i > 11
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, null)
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_CONSTRUCT_FINISH, null)
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_CONSTRUCT_START, null)
call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
set i = i + 1
endloop
call TriggerAddCondition(t, Condition(function main))
// Barracks
set buildingOrder[1] = String2OrderIdBJ("humanbarracks")
call SaveInteger(hash, 'hbar', 0, 1)
set buildingType[1] = 'hbar'
// Farm
set buildingOrder[2] = String2OrderIdBJ("farm")
call SaveInteger(hash, 'hhou', 0, 2)
set buildingType[2] = 'hhou'
// Granary
set buildingOrder[3] = String2OrderIdBJ("gryphonaviary")
call SaveInteger(hash, 'hgra', 0, 3)
set buildingType[3] = 'hgra'
// Well
set buildingOrder[4] = String2OrderIdBJ("moonwell")
call SaveInteger(hash, 'emow', 0, 4)
set buildingType[4] = 'emow'
endfunction
endlibrary