local g_squad_counter = {};
local g_company_counter = {};
local g_officer_counter = {};
local g_is_init = {};
-------------------------------------------------
-- Ordinal help function
-------------------------------------------------
function get_ordinal( number )
if ( math.abs( number ) % 100 < 21 and math.abs( number ) % 100 > 4 ) then
return number .. "th";
end
local tmp = math.abs( number ) % 10;
if ( tmp < 1 ) then
return number .. "th";
end
if ( tmp < 2 ) then
return number .. "st";
end
if ( tmp < 3 ) then
return number .. "nd";
end
if ( tmp < 4 ) then
return number .. "rd";
end
return number .. "th";
end
-------------------------------------------------
-- Change Unit Name On Created
-------------------------------------------------
function MyOnUnitCreated( playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible )
if( Players[ playerID ] == nil or
Players[ playerID ]:GetUnitByID( unitID ) == nil or
Players[ playerID ]:GetUnitByID( unitID ):IsDead() or
not Players[ playerID ]:GetUnitByID( unitID ):IsCanAttack() )
then
return;
end
-- Init stuff
local new_name = "";
if ( g_is_init[tostring( playerID )] == nil ) then
g_company_counter[tostring( playerID )] = 1;
g_squad_counter[tostring( playerID )] = 1;
g_officer_counter[tostring( playerID )] = 0;
g_is_init[tostring( playerID )] = 1;
end
-- Check if unit is "officer"
if ( Players[ playerID ]:GetUnitByID( unitID ):GetName() == "Warrior" ) then
-- Reset Company if it've got at-least 1 Squad and 1 "officer"
if ( g_squad_counter[tostring( playerID )] > 1 and g_officer_counter[tostring( playerID )] > 0) then
g_company_counter[tostring( playerID )] = g_company_counter[tostring( playerID )] + 1;
g_officer_counter[tostring( playerID )] = 0;
g_squad_counter[tostring( playerID )] = 1;
end
-- Set "officer" as Lieutenant if Company already have a Captain
if ( g_officer_counter[tostring( playerID )] > 0 ) then
new_name = get_ordinal( g_officer_counter[tostring( playerID )] ) .. " Lieutenant, " .. get_ordinal( g_company_counter[tostring( playerID )] ) .. " Company";
g_officer_counter[tostring( playerID )] = g_officer_counter[tostring( playerID )] + 1;
else
new_name = "Captain, " .. get_ordinal( g_company_counter[tostring( playerID )] ) .. " Company";
g_officer_counter[tostring( playerID )] = g_officer_counter[tostring( playerID )] + 1;
end
else
new_name = get_ordinal( g_squad_counter[tostring( playerID )] ) .. " Squad, " .. get_ordinal( g_company_counter[tostring( playerID )] ) .. " Company";
g_squad_counter[tostring( playerID )] = g_squad_counter[tostring( playerID )] + 1;
end
Network.SendRenameUnit(unitID, new_name );
print( "New name: " .. new_name );
end
Events.SerialEventUnitCreated.Add( MyOnUnitCreated );