I just test the position of the first unit in the group, to handle groups of multiple unit types you may want to simply make sure the first unit in the group is the slowest.
A size 1 cellmap = 0 harvester unit might be a very good idea. I had another idea that doesn't work unfortunately (low HP unit with regen = -1, then catch the death event). But a simple modification, adding a second unit to actually kill it (repeatedly) would be much simpler and easier to set up...
Anyway, I'm setup with the resource timer, so using that, this is how you could route units...
The helper functions: [These need to be before any calls to them, so put them at the top of the startup event]
----------------------------------------------------------------------------
-- Unit Creation and Group Management --
----------------------------------------------------------------------------
-- creates a unit and puts its id in group
function createAndGroup ( group, type, faction, pos )
createUnit ( type, faction, pos );
group[#group+1] = lastCreatedUnit ();
end
-- create a group of num units of a single unit type
function groupOfType ( group, type, faction, num, pos )
for i=1,num do createAndGroup ( group, type, faction, pos ) end
end
-- create a group of units of different types
function groupFromTypes ( group, types, faction, pos )
for i=1,#types do createAndGroup ( group, types[i], faction, pos ) end
end
-- remove a unit id from a group
function removeFromGroup ( group, unitId )
for i=1,#group do
if ( group[i] == unitId ) then
table.remove ( group, i );
return;
end
end
end
----------------------------------------------------------------------------
-- Group Commands --
----------------------------------------------------------------------------
-- give a group of units move commands to location
function groupMoveCommon ( units, location )
for i=1,#units do givePositionCommand ( units[i], "move", location ) end
end
-- give a group of units move commands to distinct locations
function groupMoveSpecific ( units, locations )
for i=1,#units do givePositionCommand ( units[i], "move", locations[i] ) end
end
-- give a group of units attack commands to location
function groupAttack ( units, pos )
for i=1,#units do givePositionCommand ( units[i], "attack", pos ) end
end
----------------------------------------------------------------------------
-- Geometry --
----------------------------------------------------------------------------
-- is pos in rectangle
function pointIsIn ( pos, rectangle )
if ( pos[1] >= rectangle[1] and rectangle[3] > pos[1]
and pos[2] >= rectangle[2] and rectangle[4] > pos[2] ) then
return true;
end
return false;
end
-- returns a rectangle around the point 'pos'
function areaAround ( pos )
return { pos[1]-5, pos[2]-5, pos[1]+5, pos[2]+5 };
end
set up: [the rest of the startup event]
----------------------------------------------------------------------------
-- Start-Up --
----------------------------------------------------------------------------
-- the patrol table will store the waypoints and regions around them
patrol = {};
patrol.waypoints = { {15,15}, {50,15}, {50,50}, {15,50} };
patrol.regions = {};
for i=1,#patrol.waypoints do
patrol.regions[#patrol.regions+1] = areaAround(patrol.waypoints[i]);
end
-- Make a group of 8 daemons...
disableAi (2);
daemons = {};
daemons.units = {};
groupOfType ( daemons.units, "daemon", 2, 8, patrol.waypoints[1] );
-- start them on their way...
groupMoveCommon ( daemons.units, patrol.waypoints[2] );
-- remember where they are currently going...
daemons.dest = 2; -- index of current target
-- Timer
timer = {};
groupFromTypes ( timer, {"mage_tower","initiate"}, 1, startLocation(1) );
giveResource ( "energy", 1, 1 );
tickCount = 0;
and in the resourceHarvested event...
if ( resourceAmount ("gold", 1) > 0 ) then
-- increment the tick counter
tickCount = tickCount + 1;
-- Check if daemons have reached target...
local cur_pos = unitPosition(daemons.units[1]);
local cur_dest = patrol.regions[daemons.dest];
if ( pointIsIn(cur_pos, cur_dest) ) then
-- we have arrived, set next destination...
daemons.dest = daemons.dest % #patrol.waypoints + 1;
-- and head on out..
groupMoveCommon ( daemons.units, patrol.waypoints[daemons.dest] );
end
-- Do things at particular times here...
if ( tickCount == 10 ) then
-- scripted action...
elseif ( tickCount == 20 ) then
-- another scripted action...
elseif ( tickCount == 30 ) then
-- etc
end
-- now take the gold back, to reset for next time...
giveResource ("gold", 1, -10);
end