function FindNearestMine takes real x, real y, real dist returns unit
local group g = CreateGroup()
local real tempDist
local unit u
set bj_meleeNearestMineDist = 4000001
set bj_meleeNearestMine = null
call GroupEnumUnitsInRange( g, x, y, dist, null )
loop
set u = FirstOfGroup(g)
exitwhen u == null
set tempDist = (GetUnitX(u) - x) * (GetUnitX(u) - x) + (GetUnitY(u) - y) * (GetUnitY(u) - y)
if tempDist < bj_meleeNearestMineDist then
set bj_meleeNearestMineDist = tempDist
set bj_meleeNearestMine = u
endif
call GroupRemoveUnit(g, u)
endloop
call DestroyGroup(g)
set g = null
return bj_meleeNearestMine
endfunction
function SetupUnits takes nothing returns nothing
local unit nearestMine
local real startX = GetStartLocationX( GetPlayerStartLocation(udg_Player) )
local real startY = GetStartLocationY( GetPlayerStartLocation(udg_Player) )
local real peonX
local real peonY
local real angle
local unit townHall = null
set nearestMine = FindNearestMine( startX, startY, bj_MELEE_MINE_SEARCH_RADIUS )
if (nearestMine != null) then
// Spawn Town Hall at the start location.
set townHall = CreateUnit( udg_Player, udg_TownhallType, startX, startY, bj_UNIT_FACING )
// Spawn Peasants near the mine.
set angle = Atan2( startY - GetUnitY(nearestMine), startX - GetUnitX(nearestMine))
set peonX = GetUnitX(nearestMine) + 320. * Cos( angle )
set peonY = GetUnitY(nearestMine) + 320. * Sin( angle )
call CreateUnit(udg_Player, udg_BuilderType, peonX + 0.00 * 64., peonY + 1.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX + 1.00 * 64., peonY + 0.15 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX - 1.00 * 64., peonY + 0.15 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX + 0.60 * 64., peonY - 1.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX - 0.60 * 64., peonY - 1.00 * 64., bj_UNIT_FACING)
else
// Spawn Town Hall at the start location.
set townHall = CreateUnit(udg_Player, udg_TownhallType, startX, startY, bj_UNIT_FACING)
// Spawn Peasants directly south of the town hall.
set peonX = startX
set peonY = startY - 224.00
call CreateUnit(udg_Player, udg_BuilderType, peonX + 2.00 * 64., peonY + 0.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX + 1.00 * 64., peonY + 0.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX + 0.00 * 64., peonY + 0.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX - 1.00 * 64., peonY + 0.00 * 64., bj_UNIT_FACING)
call CreateUnit(udg_Player, udg_BuilderType, peonX - 2.00 * 64., peonY + 0.00 * 64., bj_UNIT_FACING)
endif
call SetPlayerState(udg_Player, PLAYER_STATE_RESOURCE_HERO_TOKENS, bj_MELEE_STARTING_HERO_TOKENS)
if GetLocalPlayer() == udg_Player then
call SetCameraPosition(peonX, peonY)
call SetCameraQuickPosition(peonX, peonY)
endif
set townHall = null
set nearestMine = null
endfunction