//! zinc
library BuildTime
{
hashtable hash;
group g;
public function RegisterBuildings()
{
integer i = 0;
integer h;
while(i < udg_total)
{
SaveReal(hash, udg_building[i], 0, udg_time[i]);
i += 1;
}
}
function Time2Percent(real time, real maxTime) -> integer
{
return R2I((time / maxTime) * 100);
}
public function ReduceConstructionTime(unit u, real seconds)
{
integer h = GetHandleId(u);
real time = LoadReal(hash, h, 1);
real maxTime = LoadReal(hash, GetUnitTypeId(u), 0);
integer cPercent = Time2Percent(time, maxTime);
integer nPercent = Time2Percent(seconds, maxTime);
UnitSetConstructionProgress(u, cPercent + nPercent);
BJDebugMsg(GetUnitName(u) + " construction progress increased by " + R2S(cPercent + nPercent) + "%" + "(" + R2S(seconds) + ")");
}
function gActions()
{
unit u = GetEnumUnit();
integer h = GetHandleId(u);
real counter = LoadReal(hash, h, 1) + 1;
BJDebugMsg(R2S(counter));
SaveReal(hash, h, 1, counter);
u = null;
}
function onPeriodic()
{
ForGroup(g, function gActions);
}
function onConstruction()
{
GroupAddUnit(g, GetTriggerUnit());
BJDebugMsg(GetUnitName(GetTriggerUnit()));
}
function onConstructionFinish()
{
unit u = GetTriggerUnit();
GroupRemoveUnit(g, u);
FlushChildHashtable(hash, GetHandleId(u));
u = null;
}
function onInit()
{
trigger t = CreateTrigger();
trigger t2 = CreateTrigger();
hash = InitHashtable();
g = CreateGroup();
TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_CONSTRUCT_FINISH);
TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_CONSTRUCT_START);
TriggerAddAction(t, function onConstruction);
TriggerAddAction(t2, function onConstructionFinish);
TimerStart(CreateTimer(), 1, true, function onPeriodic);
t = null;
t2 = null;
}
}
//! endzinc