fix flag removal effect on power network

issue1247
Maikel de Vries 2015-02-11 22:42:27 +01:00
parent ea354218bb
commit de8ee9d840
2 changed files with 38 additions and 59 deletions

View File

@ -58,26 +58,7 @@ public func IsFlagpole() { return true; }
/*-- Library Code --*/
private func RefreshAllFlagLinks()
{
// Schedule a refresh for all flags.
for (var flag in LIB_FLAG_FlagList)
if (flag)
flag->ScheduleRefreshLinkedFlags();
// Update power balance for power helpers after refreshing the linked flags.
// TODO: find out why this is called twice in the same frame in different ways.
Schedule(nil, "Library_Flag->RefreshAllPowerNetworks()", 2, 0);
AddEffect("ScheduleRefreshAllPowerNetworks", nil, 1, 2, nil, Library_Flag);
}
protected func FxScheduleRefreshAllPowerNetworksTimer()
{
Library_Flag->RefreshAllPowerNetworks();
return -1;
}
// Redraws the ownership markers of this flag according to the current circumstances.
public func RedrawFlagRadius()
{
// A flag with no radius is not drawn.
@ -189,8 +170,9 @@ private func AddOwnership()
RedrawAllFlagRadiuses();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
RefreshAllFlagLinks();
// Linked flags - refresh links for this flag and update the power system.
RefreshLinkedFlags();
RefreshAllPowerNetworks();
return;
}
@ -204,8 +186,9 @@ private func RemoveOwnership()
RedrawAllFlagRadiuses();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
RefreshAllFlagLinks();
// Linked flags - refresh links for this flag and update the power system.
RefreshLinkedFlags();
RefreshAllPowerNetworks();
return;
}
@ -246,26 +229,6 @@ protected func FxIntFlagMovementCheckTimer(object target, proplist effect)
return FX_OK;
}
public func ScheduleRefreshLinkedFlags()
{
if (!GetEffect("RefreshLinkedFlags", this))
AddEffect("RefreshLinkedFlags", this, 1, 1, this);
return;
}
public func StopRefreshLinkedFlags()
{
if (GetEffect("RefreshLinkedFlags", this))
RemoveEffect("RefreshLinkedFlags", this);
return;
}
protected func FxRefreshLinkedFlagsTimer()
{
this->RefreshLinkedFlags();
return -1;
}
// Returns all flags allied to owner of which the radius intersects the given circle.
public func FindFlagsInRadius(object center_object, int radius, int owner)
{
@ -286,6 +249,8 @@ public func FindFlagsInRadius(object center_object, int radius, int owner)
return flag_list;
}
// Refreshes the linked flags for this flags and also updates the linked flags of the flages linked to this flag.
// TODO: Maybe there is a need to update the links of the flags which were linked before but are not now.
public func RefreshLinkedFlags()
{
// Debugging logs.
@ -376,7 +341,6 @@ public func CopyLinkedFlags(object from, array flaglist)
for (var i = GetLength(lib_flag.linked_flags) - 1; i >= 0; --i)
if (lib_flag.linked_flags[i] == this)
lib_flag.linked_flags[i] = from;
StopRefreshLinkedFlags();
return;
}
@ -412,8 +376,8 @@ protected func OnHostilityChange(int player1, int player2, bool hostile, bool ol
RedrawFlagRadius();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
ScheduleRefreshLinkedFlags();
// Linked flags - refresh links for this flag.
RefreshLinkedFlags();
return _inherited(player1, player2, hostile, old_hostility);
}
@ -426,8 +390,8 @@ protected func OnTeamSwitch(int player, int new_team, int old_team)
RedrawFlagRadius();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
ScheduleRefreshLinkedFlags();
// Linked flags - refresh links for this flag.
RefreshLinkedFlags();
return _inherited(player, new_team, old_team, ...);
}
@ -441,6 +405,7 @@ public func CreateConstructionPreview(object constructing_clonk)
return CreateObjectAbove(Library_Flag_ConstructionPreviewer, constructing_clonk->GetX() - GetX(), constructing_clonk->GetY() - GetY(), constructing_clonk->GetOwner());
}
/*-- Flag properties --*/
public func SetFlagRadius(int to_radius)
@ -548,8 +513,8 @@ private func LogFlags()
{
for (var flag in LIB_FLAG_FlagList)
{
Log("FLAG - Flag (%v): owner = %d, con_time = %d, radius = %d, power_network = %v", flag, flag->GetOwner(), flag.lib_flag.construction_time, flag.lib_flag.radius, flag.lib_flag.power_helper);
Log("\tlinked flags = %v", flag.lib_flag.linked_flags);
Log("FLAG - Flag (%v): owner = %d, con_time = %d, radius = %d, power_network = %v", flag, flag->GetOwner(), flag->GetFlagConstructionTime(), flag->GetFlagRadius(), flag->GetPowerHelper());
Log("\tlinked flags = %v", flag->GetLinkedFlags());
}
return;
}

View File

@ -676,6 +676,11 @@ global func Test11_Completed()
global func Test11_OnFinished()
{
// Ensure the script player exists or is created.
ClearScheduleCall(nil, "EliminatePlayer");
ClearScheduleCall(nil, "CreateScriptPlayer");
if (script_plr == nil)
CreateScriptPlayer("PowerBuddy", RGB(0, 0, 255), nil, CSPF_NoEliminationCheck);
// Remove wind generator, steam engine, flagpole, shipyard, airship.
RemoveAll(Find_Or(Find_ID(WindGenerator), Find_ID(SteamEngine), Find_ID(Compensator), Find_ID(Shipyard), Find_ID(Airship)));
return;
@ -697,18 +702,22 @@ global func Test12_OnStart(int plr)
lab->CreateContents(Metal, 8);
lab->AddToQueue(TeleGlove, 4);
lab->SetNoPowerNeed(true);
ScheduleCall(nil, "Log", 1, 0, "Lab has no power need (per script).");
// Power connection: flagpole.
CreateObjectAbove(Flagpole, 304, 140, plr);
CreateObjectAbove(Flagpole, 304, 140, plr);
// Let the lab have a power need per script.
ScheduleCall(lab, "SetNoPowerNeed", 3 * 36, 0, false);
ScheduleCall(nil, "Log", 3 * 36, 0, "Lab has a power need (per script).");
// Create the no power need rule.
ScheduleCall(nil, "CreateObject", 3 * 36, 0, Rule_NoPowerNeed);
// Let the lab consumer power again.
ScheduleCall(lab, "SetNoPowerNeed", 6 * 36, 0, false);
ScheduleCall(nil, "CreateObject", 6 * 36, 0, Rule_NoPowerNeed);
ScheduleCall(nil, "Log", 6 * 36, 0, "No power need rule activated.");
// Remove the no power need rule.
Schedule(nil, "RemoveAll(Find_ID(Rule_NoPowerNeed))", 9 * 36, 0);
ScheduleCall(nil, "Log", 9 * 36, 0, "No power need rule removed.");
// Log what the test is about.
Log("No power need rule and no power need script functionality tested for a simple network.");
@ -725,7 +734,7 @@ global func Test12_Completed()
global func Test12_OnFinished()
{
// Remove wind generator, steam engine, flagpole, shipyard, airship.
RemoveAll(Find_Or(Find_ID(SteamEngine), Find_ID(ToolsWorkshop), Find_ID(InventorsLab), Find_ID(Flagpole)));
RemoveAll(Find_Or(Find_ID(SteamEngine), Find_ID(ToolsWorkshop), Find_ID(InventorsLab), Find_ID(Flagpole), Find_ID(Rule_NoPowerNeed)));
return;
}
@ -767,11 +776,14 @@ global func Test13_OnFinished()
return;
}
static POWER_SYSTEM_Test14_Time;
// Massive test which tests a lot of power structures and the performance of the system.
global func Test14_OnStart(int plr)
{
// Start the script profiler for this test.
StartScriptProfiler();
POWER_SYSTEM_Test14_Time = GetTime();
// Power source: one steam engine.
var steam_engine1 = CreateObjectAbove(SteamEngine, 36, 160, plr);
@ -879,8 +891,10 @@ global func Test14_Completed()
global func Test14_OnFinished()
{
// Stop the script profiler for this test.
StopScriptProfiler();
// Stop the script profiler for this test and log the total time.
var time = GetTime() - POWER_SYSTEM_Test14_Time;
Log("The test ran for %d ms and these functions have been consuming an amount of time:", time);
StopScriptProfiler();
// Restore water levels.
RestoreWaterLevels();
// Remove all the structures.