Formatted code, no change in functionality

Brackets, protected/private func => func, whitespaces
master
Mark 2018-12-21 11:25:19 +01:00
parent d9bc1d9142
commit 6bdb4275f7
1 changed files with 189 additions and 85 deletions

View File

@ -32,7 +32,7 @@ local queue;
// Possibly connected cable station // Possibly connected cable station
local cable_station; local cable_station;
protected func Initialize() func Initialize()
{ {
queue = []; queue = [];
AddTimer("ProcessQueue", 10); AddTimer("ProcessQueue", 10);
@ -42,8 +42,10 @@ protected func Initialize()
/*-- Player interface --*/ /*-- Player interface --*/
public func IsProducer() { return true; } public func IsProducer() { return true; }
// All producers are accessible. // All producers are accessible.
public func IsContainer() { return true; } public func IsContainer() { return true; }
// Provides an own interaction menu, even if it wouldn't be a container. // Provides an own interaction menu, even if it wouldn't be a container.
public func HasInteractionMenu() { return true; } public func HasInteractionMenu() { return true; }
@ -182,33 +184,45 @@ public func OnProductHover(symbol, extra_data, desc_menu_target, menu_id)
for (var comp in costs) for (var comp in costs)
{ {
if (GetLength(cost_msg)) if (GetLength(cost_msg))
{
cost_msg = Format("%s +", cost_msg); cost_msg = Format("%s +", cost_msg);
}
if (!comp[2]) if (!comp[2])
{
cost_msg = Format("%s %s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0]); cost_msg = Format("%s %s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0]);
}
else else
{ {
if (GetType(comp[2]) == C4V_Array) if (GetType(comp[2]) == C4V_Array)
{ {
cost_msg = Format("%s (%s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0]); cost_msg = Format("%s (%s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0]);
for (var subs in comp[2]) for (var subs in comp[2])
{
cost_msg = Format("%s / %s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(subs, comp[1])), subs); cost_msg = Format("%s / %s {{%i}}", cost_msg, GetCostString(comp[1], CheckComponent(subs, comp[1])), subs);
}
cost_msg = Format("%s)", cost_msg); cost_msg = Format("%s)", cost_msg);
} else { }
else
{
cost_msg = Format("%s (%s {{%i}} / %s {{%i}})", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0], GetCostString(comp[1], CheckComponent(comp[2], comp[1])), comp[2]); cost_msg = Format("%s (%s {{%i}} / %s {{%i}})", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0], GetCostString(comp[1], CheckComponent(comp[2], comp[1])), comp[2]);
//cost_msg = Format("%s %s ({{%i}} / {{%i}})", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0], comp[2]); //cost_msg = Format("%s %s ({{%i}} / {{%i}})", cost_msg, GetCostString(comp[1], CheckComponent(comp[0], comp[1])), comp[0], comp[2]);
} }
} }
} }
if (this->~FuelNeed(product_id)) if (this->~FuelNeed(product_id))
{
cost_msg = Format("%s %s {{Icon_Producer_Fuel}}", cost_msg, GetCostString(1, CheckFuel(product_id))); cost_msg = Format("%s %s {{Icon_Producer_Fuel}}", cost_msg, GetCostString(1, CheckFuel(product_id)));
}
if (this->~PowerNeed(product_id)) if (this->~PowerNeed(product_id))
{
cost_msg = Format("%s + {{Library_PowerConsumer}}", cost_msg); cost_msg = Format("%s + {{Library_PowerConsumer}}", cost_msg);
}
new_box.requirements.Text = cost_msg; new_box.requirements.Text = cost_msg;
GuiUpdate(new_box, menu_id, 1, desc_menu_target); GuiUpdate(new_box, menu_id, 1, desc_menu_target);
} }
private func GetCostString(int amount, bool available) func GetCostString(int amount, bool available)
{ {
// Format amount to colored string; make it red if it's not available // Format amount to colored string; make it red if it's not available
if (available) return Format("%dx", amount); if (available) return Format("%dx", amount);
@ -259,14 +273,14 @@ public func FxIntUpgradeProductProgressBarTimer(object target, effect fx, int ti
// This function may be overloaded by the actual producer. // This function may be overloaded by the actual producer.
// If set to true, the producer will show every product which is assigned to it instead of checking the knowledge base of its owner. // If set to true, the producer will show every product which is assigned to it instead of checking the knowledge base of its owner.
private func IgnoreKnowledge() { return false; } func IgnoreKnowledge() { return false; }
/** Determines whether the product specified can be produced. Should be overloaded by the producer. /** Determines whether the product specified can be produced. Should be overloaded by the producer.
@param product_id item's id of which to determine if it is producible. @param product_id item's id of which to determine if it is producible.
@return \c true if the item can be produced, \c false otherwise. @return \c true if the item can be produced, \c false otherwise.
*/ */
private func IsProduct(id product_id) func IsProduct(id product_id)
{ {
return false; return false;
} }
@ -279,7 +293,9 @@ public func GetProducts(object for_clonk)
{ {
var for_plr = GetOwner(); var for_plr = GetOwner();
if (for_clonk) if (for_clonk)
{
for_plr = for_clonk-> GetOwner(); for_plr = for_clonk-> GetOwner();
}
var products = []; var products = [];
// Cycle through all definitions to find the ones this producer can produce. // Cycle through all definitions to find the ones this producer can produce.
var index = 0, product; var index = 0, product;
@ -288,14 +304,18 @@ public func GetProducts(object for_clonk)
while (product = GetPlrKnowledge(for_plr, nil, index, C4D_Object)) while (product = GetPlrKnowledge(for_plr, nil, index, C4D_Object))
{ {
if (IsProduct(product)) if (IsProduct(product))
{
products[GetLength(products)] = product; products[GetLength(products)] = product;
}
index++; index++;
} }
index = 0; index = 0;
while (product = GetPlrKnowledge(for_plr, nil, index, C4D_Vehicle)) while (product = GetPlrKnowledge(for_plr, nil, index, C4D_Vehicle))
{ {
if (IsProduct(product)) if (IsProduct(product))
{
products[GetLength(products)] = product; products[GetLength(products)] = product;
}
index++; index++;
} }
} }
@ -304,7 +324,9 @@ public func GetProducts(object for_clonk)
while (product = GetDefinition(index)) while (product = GetDefinition(index))
{ {
if (IsProduct(product)) if (IsProduct(product))
{
products[GetLength(products)] = product; products[GetLength(products)] = product;
}
index++; index++;
} }
} }
@ -364,7 +386,9 @@ public func ModifyQueueIndex(int position, int amount, bool infinite_production)
var item = queue[position]; var item = queue[position];
if (infinite_production != nil) if (infinite_production != nil)
{
item.Infinite = infinite_production; item.Infinite = infinite_production;
}
item.Amount += amount; item.Amount += amount;
// It might be necessary to remove the item from the queue. // It might be necessary to remove the item from the queue.
@ -373,7 +397,9 @@ public func ModifyQueueIndex(int position, int amount, bool infinite_production)
// Move all things on the right one slot to the left. // Move all things on the right one slot to the left.
var index = position; var index = position;
while (++index < queue_length) while (++index < queue_length)
{
queue[index - 1] = queue[index]; queue[index - 1] = queue[index];
}
SetLength(queue, queue_length - 1); SetLength(queue, queue_length - 1);
return false; return false;
} }
@ -391,7 +417,10 @@ public func AddToQueue(id product_id, int amount, bool infinite, int producing_p
// Check if this producer can produce the requested item. // Check if this producer can produce the requested item.
if (!IsProduct(product_id)) if (!IsProduct(product_id))
return nil; return nil;
if (amount < 0) FatalError("Producer::AddToQueue called with negative amount."); if (amount < 0)
{
FatalError("Producer::AddToQueue called with negative amount.");
}
// if the product is already in the queue, just modify the amount // if the product is already in the queue, just modify the amount
var found = false; var found = false;
@ -406,13 +435,16 @@ public func AddToQueue(id product_id, int amount, bool infinite, int producing_p
// Otherwise create a new entry in the queue. // Otherwise create a new entry in the queue.
if (!found) if (!found)
{
PushBack(queue, { Product = product_id, Amount = amount, Infinite = infinite, ProducingPlayer=producing_player }); PushBack(queue, { Product = product_id, Amount = amount, Infinite = infinite, ProducingPlayer=producing_player });
}
// Notify all production menus open for this producer. // Notify all production menus open for this producer.
UpdateInteractionMenus(this.GetProductionMenuEntries); UpdateInteractionMenus(this.GetProductionMenuEntries);
} }
/** Shifts the queue one space to the left. The first item will be put in the very right slot. /**
Shifts the queue one space to the left. The first item will be put in the very right slot.
*/ */
public func CycleQueue() public func CycleQueue()
{ {
@ -420,12 +452,15 @@ public func CycleQueue()
var first = queue[0]; var first = queue[0];
var queue_length = GetLength(queue); var queue_length = GetLength(queue);
for (var i = 1; i < queue_length; ++i) for (var i = 1; i < queue_length; ++i)
{
queue[i - 1] = queue[i]; queue[i - 1] = queue[i];
}
queue[-1] = first; queue[-1] = first;
} }
/** Clears the complete production queue. /**
Clears the complete production queue.
*/ */
public func ClearQueue(bool abort) // TODO: parameter is never used public func ClearQueue(bool abort) // TODO: parameter is never used
{ {
@ -435,12 +470,13 @@ public func ClearQueue(bool abort) // TODO: parameter is never used
} }
/** Modifies a certain production item arbitrarily. This is only used by the interaction menu. /**
Modifies a certain production item arbitrarily. This is only used by the interaction menu.
This also creates a new production order if none exists yet. This also creates a new production order if none exists yet.
@param info @param info
proplist with Product, Amount. If the player holds the menu-modifier key, this will toggle infinite production. proplist with Product, Amount. If the player holds the menu-modifier key, this will toggle infinite production.
*/ */
private func ModifyProduction(proplist info, int player) func ModifyProduction(proplist info, int player)
{ {
if (Hostile(GetOwner(), player)) return; if (Hostile(GetOwner(), player)) return;
@ -472,7 +508,8 @@ private func ModifyProduction(proplist info, int player)
} }
/** Returns the current queue. /**
Returns the current queue.
@return an array containing the queue elements (.Product for id, .Amount for amount). @return an array containing the queue elements (.Product for id, .Amount for amount).
*/ */
public func GetQueue() public func GetQueue()
@ -480,15 +517,19 @@ public func GetQueue()
return queue; return queue;
} }
private func ProcessQueue() func ProcessQueue()
{ {
// If target is currently producing, don't do anything. // If target is currently producing, don't do anything.
if (IsProducing()) if (IsProducing())
{
return FX_OK; return FX_OK;
}
// Wait if there are no items in the queue. // Wait if there are no items in the queue.
if (!queue[0]) if (!queue[0])
{
return FX_OK; return FX_OK;
}
// Produce first item in the queue. // Produce first item in the queue.
var product_id = queue[0].Product; var product_id = queue[0].Product;
@ -507,7 +548,9 @@ private func ProcessQueue()
var is_still_there = ModifyQueueIndex(0, -1); var is_still_there = ModifyQueueIndex(0, -1);
// And cycle to enable rotational production of (infinite) objects. // And cycle to enable rotational production of (infinite) objects.
if (is_still_there) if (is_still_there)
{
CycleQueue(); CycleQueue();
}
// We changed something. Update menus. // We changed something. Update menus.
UpdateInteractionMenus(this.GetProductionMenuEntries); UpdateInteractionMenus(this.GetProductionMenuEntries);
// Done with production checks. // Done with production checks.
@ -518,29 +561,37 @@ private func ProcessQueue()
/*-- Production --*/ /*-- Production --*/
// These functions may be overloaded by the actual producer. // These functions may be overloaded by the actual producer.
private func ProductionTime(id product) { return product->~GetProductionTime(); } func ProductionTime(id product) { return product->~GetProductionTime(); }
private func FuelNeed(id product) { return product->~GetFuelNeed(); } func FuelNeed(id product) { return product->~GetFuelNeed(); }
public func PowerNeed() { return 80; } public func PowerNeed() { return 80; }
public func GetConsumerPriority() { return 50; } public func GetConsumerPriority() { return 50; }
private func Produce(id product, producing_player) func Produce(id product, producing_player)
{ {
// Already producing? Wait a little. // Already producing? Wait a little.
if (IsProducing()) if (IsProducing())
{
return false; return false;
}
// Check if components are available. // Check if components are available.
if (!CheckComponents(product)) if (!CheckComponents(product))
{
return false; return false;
}
// Check need for fuel. // Check need for fuel.
if (!CheckFuel(product)) if (!CheckFuel(product))
{
return false; return false;
}
// Check need for power. // Check need for power.
if (!CheckForPower()) if (!CheckForPower())
{
return false; return false;
}
// Everything available? Start production. // Everything available? Start production.
// Remove needed components, fuel and liquid. // Remove needed components, fuel and liquid.
@ -554,7 +605,7 @@ private func Produce(id product, producing_player)
} }
private func CheckComponents(id product, bool remove) func CheckComponents(id product, bool remove)
{ {
for (var item in ProductionCosts(product)) for (var item in ProductionCosts(product))
{ {
@ -578,15 +629,25 @@ private func CheckComponents(id product, bool remove)
} }
} }
if (!found) if (!found)
{
return false; // Substitutes missing. return false; // Substitutes missing.
} else { }
}
else
{
// Check substitute components // Check substitute components
if (CheckComponent(mat_substitute, mat_cost)) if (CheckComponent(mat_substitute, mat_cost))
{
mat_id = mat_substitute; mat_id = mat_substitute;
}
else else
{
return false; // Substitute missing. return false; // Substitute missing.
} }
} else { }
}
else
{
return false; // Components missing. return false; // Components missing.
} }
} }
@ -603,10 +664,12 @@ private func CheckComponents(id product, bool remove)
i += num - 1; // -1 to offset loop advancement i += num - 1; // -1 to offset loop advancement
} }
else else
{
obj->RemoveObject(); obj->RemoveObject();
} }
} }
} }
}
return true; return true;
} }
@ -615,7 +678,9 @@ public func GetAvailableComponentAmount(id material)
{ {
// Normal object? // Normal object?
if (!material->~IsStackable()) if (!material->~IsStackable())
{
return ContentsCount(material); return ContentsCount(material);
}
// If not, we need to check stacked objects. // If not, we need to check stacked objects.
var real_amount = 0; var real_amount = 0;
var contents = FindObjects(Find_Container(this), Find_ID(material)); var contents = FindObjects(Find_Container(this), Find_ID(material));
@ -642,7 +707,9 @@ public func CheckFuel(id product, bool remove)
var fuel_amount = 0; var fuel_amount = 0;
// Find fuel in this producer. // Find fuel in this producer.
for (var fuel in FindObjects(Find_Container(this), Find_Func("IsFuel"))) for (var fuel in FindObjects(Find_Container(this), Find_Func("IsFuel")))
{
fuel_amount += fuel->~GetFuelAmount(); fuel_amount += fuel->~GetFuelAmount();
}
if (fuel_amount < fuel_needed) if (fuel_amount < fuel_needed)
{ {
return false; return false;
@ -671,13 +738,13 @@ public func CheckFuel(id product, bool remove)
} }
private func CheckForPower() func CheckForPower()
{ {
return true; // always assume that power is available return true; // always assume that power is available
} }
private func IsProducing() func IsProducing()
{ {
if (GetEffect("ProcessProduction", this)) if (GetEffect("ProcessProduction", this))
return true; return true;
@ -685,7 +752,7 @@ private func IsProducing()
} }
protected func FxProcessProductionStart(object target, proplist effect, int temporary, id product, int producing_player) func FxProcessProductionStart(object target, proplist effect, int temporary, id product, int producing_player)
{ {
if (temporary) if (temporary)
return FX_OK; return FX_OK;
@ -710,7 +777,9 @@ protected func FxProcessProductionStart(object target, proplist effect, int temp
// change its power need during production. Only do this for producers // change its power need during production. Only do this for producers
// which are power consumers. // which are power consumers.
if (this->~IsPowerConsumer()) if (this->~IsPowerConsumer())
{
RegisterPowerRequest(this->PowerNeed()); RegisterPowerRequest(this->PowerNeed());
}
return FX_OK; return FX_OK;
} }
@ -725,7 +794,9 @@ public func OnNotEnoughPower()
this->~OnProductionHold(effect.Product, effect.Duration); this->~OnProductionHold(effect.Product, effect.Duration);
} }
else else
{
FatalError("Producer effect removed when power still active!"); FatalError("Producer effect removed when power still active!");
}
return _inherited(...); return _inherited(...);
} }
@ -739,12 +810,14 @@ public func OnEnoughPower()
this->~OnProductionContinued(effect.Product, effect.Duration); this->~OnProductionContinued(effect.Product, effect.Duration);
} }
else else
{
FatalError("Producer effect removed when power still active!"); FatalError("Producer effect removed when power still active!");
}
return _inherited(...); return _inherited(...);
} }
protected func FxProcessProductionTimer(object target, proplist effect, int time) func FxProcessProductionTimer(object target, proplist effect, int time)
{ {
if (!effect.Active) if (!effect.Active)
return FX_OK; return FX_OK;
@ -766,7 +839,7 @@ protected func FxProcessProductionTimer(object target, proplist effect, int time
} }
protected func FxProcessProductionStop(object target, proplist effect, int reason, bool temp) func FxProcessProductionStop(object target, proplist effect, int reason, bool temp)
{ {
if (temp) if (temp)
return FX_OK; return FX_OK;
@ -775,10 +848,14 @@ protected func FxProcessProductionStop(object target, proplist effect, int reaso
// process, because OnNotEnoughPower relies on it and it gives other producers the chance // process, because OnNotEnoughPower relies on it and it gives other producers the chance
// to get some power. Do not unregister if this producer does not consumer power. // to get some power. Do not unregister if this producer does not consumer power.
if (this->~IsPowerConsumer()) if (this->~IsPowerConsumer())
{
UnregisterPowerRequest(); UnregisterPowerRequest();
}
if (reason != 0) if (reason != 0)
{
return FX_OK; return FX_OK;
}
// Callback to the producer. // Callback to the producer.
this->~OnProductionFinish(effect.Product); this->~OnProductionFinish(effect.Product);
@ -787,7 +864,9 @@ protected func FxProcessProductionStop(object target, proplist effect, int reaso
OnProductEjection(product); OnProductEjection(product);
// Global callback. // Global callback.
if (product) if (product)
{
GameCallEx("OnProductionFinished", product, effect.producing_player); GameCallEx("OnProductionFinished", product, effect.producing_player);
}
// Try to process the queue immediately and don't wait for the timer to prevent pauses. // Try to process the queue immediately and don't wait for the timer to prevent pauses.
ProcessQueue(); ProcessQueue();
return FX_OK; return FX_OK;
@ -799,7 +878,9 @@ public func OnProductEjection(object product)
{ {
// Safety for the product removing itself on construction. // Safety for the product removing itself on construction.
if (!product) if (!product)
{
return; return;
}
// Vehicles in front of buildings, and objects with special needs as well. // Vehicles in front of buildings, and objects with special needs as well.
if (product->GetCategory() & C4D_Vehicle || product->~OnCompletionEjectProduct()) if (product->GetCategory() & C4D_Vehicle || product->~OnCompletionEjectProduct())
{ {
@ -814,7 +895,9 @@ public func OnProductEjection(object product)
} }
// Items should stay inside. // Items should stay inside.
else else
{
product->Enter(this); product->Enter(this);
}
return; return;
} }
@ -836,14 +919,18 @@ public func ConnectCableStation(object station)
public func RequestAllMissingComponents(proplist product) public func RequestAllMissingComponents(proplist product)
{ {
if (!cable_station) if (!cable_station)
{
return false; return false;
}
var item_id = product.Product; var item_id = product.Product;
var amount = product.Amount; var amount = product.Amount;
// Take by batches of five for infinite production. // Take by batches of five for infinite production.
// TODO: Can we somehow make this smarter? Take all available from source container? // TODO: Can we somehow make this smarter? Take all available from source container?
if (product.Infinite) if (product.Infinite)
{
amount = 5; amount = 5;
}
// Request all currently unavailable components. // Request all currently unavailable components.
for (var item in ProductionCosts(item_id)) for (var item in ProductionCosts(item_id))
@ -852,11 +939,15 @@ public func RequestAllMissingComponents(proplist product)
var mat_cost = item[1]; var mat_cost = item[1];
// No way to request liquids currently, player must use pumps instead. // No way to request liquids currently, player must use pumps instead.
if (mat_id->~IsLiquid()) if (mat_id->~IsLiquid())
{
continue; continue;
}
var available = GetAvailableComponentAmount(mat_id); var available = GetAvailableComponentAmount(mat_id);
if (available < mat_cost) if (available < mat_cost)
{
RequestObject(mat_id, mat_cost - available, amount * mat_cost - available); RequestObject(mat_id, mat_cost - available, amount * mat_cost - available);
} }
}
// Also check item fuel need. // Also check item fuel need.
var fuel_needed = FuelNeed(item_id); var fuel_needed = FuelNeed(item_id);
@ -872,7 +963,9 @@ public func RequestAllMissingComponents(proplist product)
public func RequestObject(id item_id, int min_amount, int max_amount) public func RequestObject(id item_id, int min_amount, int max_amount)
{ {
if (cable_station) if (cable_station)
{
cable_station->AddRequest({type = item_id, min_amount = min_amount, max_amount = max_amount}); cable_station->AddRequest({type = item_id, min_amount = min_amount, max_amount = max_amount});
}
return; return;
} }
@ -884,7 +977,9 @@ public func IsCollectionAllowed(object item)
{ {
// Some objects might just bypass this check // Some objects might just bypass this check
if (item->~ForceEntry(this)) if (item->~ForceEntry(this))
{
return false; return false;
}
var item_id = item->GetID(); var item_id = item->GetID();
// Products itself may be collected. // Products itself may be collected.
if (IsProduct(item_id)) return true; if (IsProduct(item_id)) return true;
@ -896,17 +991,24 @@ public func IsCollectionAllowed(object item)
while (component_id = product->GetComponent(nil, i)) while (component_id = product->GetComponent(nil, i))
{ {
if (component_id == item_id) if (component_id == item_id)
{
return true; return true;
}
if (product->~GetSubstituteComponent(component_id)) if (product->~GetSubstituteComponent(component_id))
{ {
var subs = product->GetSubstituteComponent(component_id); var subs = product->GetSubstituteComponent(component_id);
if (GetType(subs) == C4V_Array) if (GetType(subs) == C4V_Array)
{ {
if (IsValueInArray(subs, item_id)) if (IsValueInArray(subs, item_id))
{
return true; return true;
} else if (subs == item_id) }
}
else if (subs == item_id)
{
return true; return true;
} }
}
i++; i++;
} }
} }
@ -957,7 +1059,9 @@ public func RejectCollect(id item_id, object item)
} }
// Can we collect the object itself? // Can we collect the object itself?
if (IsCollectionAllowed(item)) if (IsCollectionAllowed(item))
{
return false; return false;
}
return true; return true;
} }
@ -967,7 +1071,7 @@ public func RejectCollect(id item_id, object item)
// and this functionality may be removed in // and this functionality may be removed in
// the near future. // the near future.
// TODO // TODO
private func ConvertToLiquid(object obj) func ConvertToLiquid(object obj)
{ {
var liquid = GetDefinition(obj->CanConvertToLiquidType())->CreateLiquid(obj->GetLiquidAmount()); var liquid = GetDefinition(obj->CanConvertToLiquidType())->CreateLiquid(obj->GetLiquidAmount());