PowerSystem: Make system overloadable

Replaced hardcoded method calls with a wrapper function
install-platforms
Mark 2017-09-10 12:54:11 +02:00
parent 366ba86abc
commit 4fab0cf47e
5 changed files with 120 additions and 104 deletions

View File

@ -1,76 +1,76 @@
/**
Energy supply
A given object has to be supplied with energy.
@author ck
*/
#include Library_Goal
local target;
protected func Initialize()
{
target = 0;
return inherited(...);
}
// Set the target object to be supplied with energy
public func SetTarget(object target_)
{
target = target_;
}
// The goal is fulfilled if the target has been supplied with energy.
public func IsFulfilled()
{
// Get the power network for the target.
var network = Library_Power->GetPowerNetwork(target);
if (!network)
return false;
return network->GetBarePowerAvailable() > 0;
}
public func GetDescription(int plr)
{
var message;
if (IsFulfilled())
message = "$MsgGoalFulfilled$";
else
message = "$MsgGoalUnFulfilled$";
return message;
}
// Shows or hides a message window with information.
public func Activate(int plr)
{
// If goal message open -> hide it.
if (GetEffect("GoalMessage", this))
{
CustomMessage("", nil, plr, nil, nil, nil, nil, nil, MSG_HCenter);
RemoveEffect("GoalMessage", this);
return;
}
// Otherwise open a new message.
AddEffect("GoalMessage", this, 100, 0, this);
var message;
if (IsFulfilled())
message = "@$MsgGoalFulfilled$";
else
message = "@$MsgGoalUnFulfilled$";
CustomMessage(message, nil, plr, 0, 16 + 64, 0xffffff, GUI_MenuDeco, this, MSG_HCenter);
return;
}
protected func FxGoalMessageStart() {}
public func GetShortDescription(int plr)
{
return "";
}
/*-- Proplist --*/
local Name = "$Name$";
/**
Energy supply
A given object has to be supplied with energy.
@author ck
*/
#include Library_Goal
local target;
protected func Initialize()
{
target = 0;
return inherited(...);
}
// Set the target object to be supplied with energy
public func SetTarget(object target_)
{
target = target_;
}
// The goal is fulfilled if the target has been supplied with energy.
public func IsFulfilled()
{
// Get the power network for the target.
var network = GetPowerSystem()->GetPowerNetwork(target);
if (!network)
return false;
return network->GetBarePowerAvailable() > 0;
}
public func GetDescription(int plr)
{
var message;
if (IsFulfilled())
message = "$MsgGoalFulfilled$";
else
message = "$MsgGoalUnFulfilled$";
return message;
}
// Shows or hides a message window with information.
public func Activate(int plr)
{
// If goal message open -> hide it.
if (GetEffect("GoalMessage", this))
{
CustomMessage("", nil, plr, nil, nil, nil, nil, nil, MSG_HCenter);
RemoveEffect("GoalMessage", this);
return;
}
// Otherwise open a new message.
AddEffect("GoalMessage", this, 100, 0, this);
var message;
if (IsFulfilled())
message = "@$MsgGoalFulfilled$";
else
message = "@$MsgGoalUnFulfilled$";
CustomMessage(message, nil, plr, 0, 16 + 64, 0xffffff, GUI_MenuDeco, this, MSG_HCenter);
return;
}
protected func FxGoalMessageStart() {}
public func GetShortDescription(int plr)
{
return "";
}
/*-- Proplist --*/
local Name = "$Name$";

View File

@ -344,12 +344,12 @@ public func RefreshLinkedFlags()
// Since the connected flags have been updated it is necessary to update the power helper as well.
// First make sure the power system is initialized.
Library_Power->Init();
GetPowerSystem()->Init();
// Get the old power network for this flag.
var old_network = lib_flag.power_helper;
// Create a new power network for ths flag since we don't know whether flag links have been lost.
// We then just possibly remove the old ones if they exist.
lib_flag.power_helper = CreateObject(Library_Power, 0, 0, NO_OWNER);
lib_flag.power_helper = CreateObject(GetPowerSystem()->GetPowerSystemNetwork(), 0, 0, NO_OWNER);
PushBack(LIB_POWR_Networks, lib_flag.power_helper);
// Make a list of the power networks which need to be merged into the new one.
var to_merge = [old_network];
@ -522,7 +522,7 @@ private func RefreshPowerNetwork(object network)
{
if (!link || !link.obj)
continue;
var actual_network = Library_Power->GetPowerNetwork(link.obj);
var actual_network = GetPowerSystem()->GetPowerNetwork(link.obj);
if (!actual_network || actual_network == network)
continue;
// Remove from old network and add to new network.
@ -533,7 +533,7 @@ private func RefreshPowerNetwork(object network)
{
if (!link || !link.obj)
continue;
var actual_network = Library_Power->GetPowerNetwork(link.obj);
var actual_network = GetPowerSystem()->GetPowerNetwork(link.obj);
if (!actual_network || actual_network == network)
continue;
// Remove from old network and add to new network.

View File

@ -53,7 +53,7 @@ public func IsPowerConsumer() { return true; }
// a request for power of the specified amount.
private func RegisterPowerRequest(int amount)
{
Library_Power->RegisterPowerConsumer(this, amount);
GetPowerSystem()->RegisterPowerConsumer(this, amount);
return;
}
@ -61,7 +61,7 @@ private func RegisterPowerRequest(int amount)
// a the end of a power request.
private func UnregisterPowerRequest()
{
Library_Power->UnregisterPowerConsumer(this);
GetPowerSystem()->UnregisterPowerConsumer(this);
// Also ensure that the no-power symbol is not shown any more.
RemoveStatusSymbol(Library_PowerConsumer);
return;
@ -71,7 +71,7 @@ private func UnregisterPowerRequest()
// the power network of this consumer.
private func UpdatePowerRequest()
{
Library_Power->UpdateNetworkForPowerLink(this);
GetPowerSystem()->UpdateNetworkForPowerLink(this);
return;
}
@ -145,7 +145,7 @@ protected func Destruction()
// When ownership has changed, the consumer may have moved out of or into a new network.
public func OnOwnerChanged(int new_owner, int old_owner)
{
Library_Power->TransferPowerLink(this);
GetPowerSystem()->TransferPowerLink(this);
return _inherited(new_owner, old_owner, ...);
}

View File

@ -49,7 +49,7 @@ public func IsPowerProducer() { return true; }
// that this structure is available and able to produce the specified amount of power.
private func RegisterPowerProduction(int amount)
{
Library_Power->RegisterPowerProducer(this, amount);
GetPowerSystem()->RegisterPowerProducer(this, amount);
return;
}
@ -57,7 +57,7 @@ private func RegisterPowerProduction(int amount)
// that this structure is not able to produce any power any more.
private func UnregisterPowerProduction()
{
Library_Power->UnregisterPowerProducer(this);
GetPowerSystem()->UnregisterPowerProducer(this);
return;
}
@ -105,6 +105,6 @@ protected func Destruction()
// When ownership has changed, the producer may have moved out of or into a new network.
public func OnOwnerChanged(int new_owner, int old_owner)
{
Library_Power->TransferPowerLink(this);
GetPowerSystem()->TransferPowerLink(this);
return _inherited(new_owner, old_owner, ...);
}

View File

@ -50,6 +50,14 @@ static LIB_POWR_Networks;
// Construction for which variables are being used.
local lib_power;
// The definition of the power system. You can overload this function
// if you want to use a different power system.
global func GetPowerSystem()
{
return Library_Power;
}
// Initialize the local variables needed to keep track of each power network.
protected func Construction()
{
@ -75,9 +83,9 @@ protected func Construction()
public func RegisterPowerProducer(object producer, int amount)
{
// Definition call safety checks.
if (this != Library_Power || !producer || !producer->~IsPowerProducer())
if (this != GetPowerSystem() || !producer || !producer->~IsPowerProducer())
return FatalError("RegisterPowerProducer() either not called from definition context or no producer specified.");
Library_Power->Init();
GetPowerSystem()->Init();
// Find the network for this producer and add it.
var network = GetPowerNetwork(producer);
network->AddPowerProducer(producer, amount, producer->GetProducerPriority());
@ -88,9 +96,9 @@ public func RegisterPowerProducer(object producer, int amount)
public func UnregisterPowerProducer(object producer)
{
// Definition call safety checks.
if (this != Library_Power || !producer || !producer->~IsPowerProducer())
if (this != GetPowerSystem() || !producer || !producer->~IsPowerProducer())
return FatalError("UnregisterPowerProducer() either not called from definition context or no producer specified.");
Library_Power->Init();
GetPowerSystem()->Init();
// Find the network for this producer and remove it.
var network = GetPowerNetwork(producer);
network->RemovePowerProducer(producer);
@ -101,9 +109,9 @@ public func UnregisterPowerProducer(object producer)
public func RegisterPowerConsumer(object consumer, int amount)
{
// Definition call safety checks.
if (this != Library_Power || !consumer || !consumer->~IsPowerConsumer())
if (this != GetPowerSystem() || !consumer || !consumer->~IsPowerConsumer())
return FatalError("RegisterPowerConsumer() either not called from definition context or no consumer specified.");
Library_Power->Init();
GetPowerSystem()->Init();
// Find the network for this consumer and add it.
var network = GetPowerNetwork(consumer);
network->AddPowerConsumer(consumer, amount, consumer->GetConsumerPriority());
@ -114,9 +122,9 @@ public func RegisterPowerConsumer(object consumer, int amount)
public func UnregisterPowerConsumer(object consumer)
{
// Definition call safety checks.
if (this != Library_Power || !consumer || !consumer->~IsPowerConsumer())
if (this != GetPowerSystem() || !consumer || !consumer->~IsPowerConsumer())
return FatalError("UnregisterPowerConsumer() either not called from definition context or no consumer specified.");
Library_Power->Init();
GetPowerSystem()->Init();
// Find the network for this consumer and remove it.
var network = GetPowerNetwork(consumer);
network->RemovePowerConsumer(consumer);
@ -128,7 +136,7 @@ public func UnregisterPowerConsumer(object consumer)
public func TransferPowerLink(object link)
{
// Definition call safety checks.
if (this != Library_Power || !link)
if (this != GetPowerSystem() || !link)
return FatalError("TransferPowerLink() either not called from definition context or no link specified.");
// Get the new network for this power link.
var new_network = GetPowerNetwork(link);
@ -165,7 +173,7 @@ public func TransferPowerLink(object link)
public func UpdateNetworkForPowerLink(object link)
{
// Definition call safety checks.
if (this != Library_Power || !link)
if (this != GetPowerSystem() || !link)
return FatalError("UpdateNetworkForPowerLink() either not called from definition context or no link specified.");
// Find the network for this link and update it.
var network = GetPowerNetwork(link);
@ -177,7 +185,7 @@ public func UpdateNetworkForPowerLink(object link)
public func GetPowerNetwork(object for_obj)
{
// Definition call safety checks.
if (this != Library_Power || !for_obj)
if (this != GetPowerSystem() || !for_obj)
return FatalError("GetPowerNetwork() either not called from definition context or no object specified.");
// Get the actual power consumer for this object. This can for example be the elevator for the case.
@ -209,7 +217,7 @@ public func GetPowerNetwork(object for_obj)
// Create the helper if it does not exist yet.
if (helper == nil)
{
helper = CreateObject(Library_Power, 0, 0, NO_OWNER);
helper = CreateObject(GetPowerSystem()->GetPowerSystemNetwork(), 0, 0, NO_OWNER);
helper.lib_power.neutral_network = true;
LIB_POWR_Networks[GetLength(LIB_POWR_Networks)] = helper;
}
@ -221,7 +229,7 @@ public func GetPowerNetwork(object for_obj)
// Create the helper if it does not exist yet.
if (helper == nil)
{
helper = CreateObject(Library_Power, 0, 0, NO_OWNER);
helper = CreateObject(GetPowerSystem()->GetPowerSystemNetwork(), 0, 0, NO_OWNER);
LIB_POWR_Networks[GetLength(LIB_POWR_Networks)] = helper;
// Add to all linked flags.
flag->SetPowerHelper(helper);
@ -243,7 +251,7 @@ public func GetPowerNetwork(object for_obj)
public func Init()
{
// Definition call safety checks.
if (this != Library_Power)
if (this != GetPowerSystem())
return;
// Initialize the list of networks if not done already.
if (GetType(LIB_POWR_Networks) != C4V_Array)
@ -252,6 +260,14 @@ public func Init()
}
// Definition call: Get the type of network helper object to create.
// You can overload this function if you want to use a different
// power system network helper object.
private func GetPowerSystemNetwork()
{
return Library_Power;
}
/*-- Library Code --*/
public func AddPowerProducer(object producer, int amount, int prio)
@ -767,7 +783,7 @@ private func NotifyOnPowerBalanceChange()
// Notify all power display objects a balance change has occured.
for (var display_obj in FindObjects(Find_Func("IsPowerDisplay")))
{
if (Library_Power->GetPowerNetwork(display_obj) == this)
if (GetPowerSystem()->GetPowerNetwork(display_obj) == this)
display_obj->~OnPowerBalanceChange(this);
}
return;
@ -871,7 +887,7 @@ private func VisualizePowerChange(object obj, int old_val, int new_val, bool los
var before_current = nil;
var effect = GetEffect("VisualPowerChange", obj);
if (!effect)
effect = AddEffect("VisualPowerChange", obj, 1, 5, nil, Library_Power);
effect = AddEffect("VisualPowerChange", obj, 1, 5, nil, GetPowerSystem());
else
before_current = effect.current;
@ -953,7 +969,7 @@ protected func FxVisualPowerChangeTimer(object target, proplist effect, int time
// Helper object should not be saved.
public func SaveScenarioObject()
{
if (GetID() == Library_Power)
if (GetID() == GetPowerSystem())
return false;
return inherited(...);
}