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

@ -1,10 +1,10 @@
/** /**
Producer Producer
Library for production facilities. This library handles the automatic production of Library for production facilities. This library handles the automatic production of
items in structures. The library provides the interface for the player, checks for items in structures. The library provides the interface for the player, checks for
components, need for liquids or fuel and power. Then handles the production process and may in the future components, need for liquids or fuel and power. Then handles the production process and may in the future
provide an interface with other systems (e.g. railway). provide an interface with other systems (e.g. railway).
@author Maikel @author Maikel
*/ */
@ -19,7 +19,7 @@
* Also an infinite amount is possible, equals indefinite production. * Also an infinite amount is possible, equals indefinite production.
Possible interaction with cable network: Possible interaction with cable network:
* Producers request the cable network for raw materials. * Producers request the cable network for raw materials.
*/ */
#include Library_PowerConsumer #include Library_PowerConsumer
@ -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; }
@ -51,7 +53,7 @@ public func HasInteractionMenu() { return true; }
public func GetProductionMenuEntries(object clonk) public func GetProductionMenuEntries(object clonk)
{ {
var products = GetProducts(clonk); var products = GetProducts(clonk);
// default design of a control menu item // default design of a control menu item
var control_prototype = var control_prototype =
{ {
@ -60,12 +62,12 @@ public func GetProductionMenuEntries(object clonk)
OnMouseOut = GuiAction_SetTag("Std") OnMouseOut = GuiAction_SetTag("Std")
}; };
var custom_entry = var custom_entry =
{ {
Right = "3em", Bottom = "2em", Right = "3em", Bottom = "2em",
image = {Right = "2em", Prototype = control_prototype, Style = GUI_TextBottom | GUI_TextRight} image = {Right = "2em", Prototype = control_prototype, Style = GUI_TextBottom | GUI_TextRight}
}; };
var menu_entries = []; var menu_entries = [];
var index = 0; var index = 0;
for (var product in products) for (var product in products)
@ -79,7 +81,7 @@ public func GetProductionMenuEntries(object clonk)
info = nil; info = nil;
} }
// Prepare menu entry. // Prepare menu entry.
var entry = new custom_entry var entry = new custom_entry
{ {
image = new custom_entry.image{}, image = new custom_entry.image{},
remove = new control_prototype remove = new control_prototype
@ -90,13 +92,13 @@ public func GetProductionMenuEntries(object clonk)
disabled = {BackgroundColor = RGBa(0, 0, 0, 150)} disabled = {BackgroundColor = RGBa(0, 0, 0, 150)}
} }
}; };
entry.image.Symbol = product; entry.image.Symbol = product;
if (info) // Currently in queue? if (info) // Currently in queue?
{ {
if (info.Infinite) if (info.Infinite)
{ {
entry.image.infinity = entry.image.infinity =
{ {
Top = "1em", Left = "1em", Top = "1em", Left = "1em",
Symbol = Icon_Number, Symbol = Icon_Number,
@ -109,19 +111,19 @@ public func GetProductionMenuEntries(object clonk)
entry.remove.disabled = nil; entry.remove.disabled = nil;
entry.BackgroundColor = RGB(50, 100, 50); entry.BackgroundColor = RGB(50, 100, 50);
} }
entry.Priority = 1000 * product->GetValue() + index; // Sort by (estimated) value and then by index. entry.Priority = 1000 * product->GetValue() + index; // Sort by (estimated) value and then by index.
entry.Tooltip = product->GetName(); entry.Tooltip = product->GetName();
entry.image.OnClick = GuiAction_Call(this, "ModifyProduction", {Product = product, Amount = +1}); entry.image.OnClick = GuiAction_Call(this, "ModifyProduction", {Product = product, Amount = +1});
PushBack(menu_entries, {symbol = product, extra_data = nil, custom = entry}); PushBack(menu_entries, {symbol = product, extra_data = nil, custom = entry});
} }
// Below the symbols, we leave some space for a progress bar to indicate the current product progress. // Below the symbols, we leave some space for a progress bar to indicate the current product progress.
var entry = var entry =
{ {
Bottom = "1em", BackgroundColor = RGBa(0, 0, 0, 50), Bottom = "1em", BackgroundColor = RGBa(0, 0, 0, 50),
Priority = 999998, Priority = 999998,
bar = bar =
{ {
BackgroundColor = RGBa(200, 200, 200, 100), BackgroundColor = RGBa(200, 200, 200, 100),
Right = "0%" Right = "0%"
@ -130,7 +132,7 @@ public func GetProductionMenuEntries(object clonk)
var updating_effect = AddEffect("IntUpgradeProductProgressBar", this, 1, 2, this); var updating_effect = AddEffect("IntUpgradeProductProgressBar", this, 1, 2, this);
PushBack(menu_entries, {symbol = nil, extra_data = nil, custom = entry, fx = updating_effect}); PushBack(menu_entries, {symbol = nil, extra_data = nil, custom = entry, fx = updating_effect});
// At the bottom of the menu, we add some helpful information about the additional features. // At the bottom of the menu, we add some helpful information about the additional features.
entry = entry =
{ {
Style = GUI_TextBottom | GUI_FitChildren, Style = GUI_TextBottom | GUI_FitChildren,
Bottom = "1em", BackgroundColor = RGBa(0, 0, 0, 100), Bottom = "1em", BackgroundColor = RGBa(0, 0, 0, 100),
@ -157,7 +159,7 @@ public func GetInteractionMenus(object clonk)
Priority = 20 Priority = 20
}; };
PushBack(menus, prod_menu); PushBack(menus, prod_menu);
return menus; return menus;
} }
@ -165,50 +167,62 @@ public func GetInteractionMenus(object clonk)
public func OnProductHover(symbol, extra_data, desc_menu_target, menu_id) public func OnProductHover(symbol, extra_data, desc_menu_target, menu_id)
{ {
if (symbol == nil) return; if (symbol == nil) return;
var new_box = var new_box =
{ {
Text = Format("%s:|%s", symbol.Name, symbol.Description,), Text = Format("%s:|%s", symbol.Name, symbol.Description,),
requirements = requirements =
{ {
Top = "100% - 1em", Top = "100% - 1em",
Style = GUI_TextBottom Style = GUI_TextBottom
} }
}; };
var product_id = symbol; var product_id = symbol;
var costs = ProductionCosts(product_id); var costs = ProductionCosts(product_id);
var cost_msg = ""; var cost_msg = "";
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);
@ -244,7 +258,7 @@ public func FxIntUpgradeProductProgressBarTimer(object target, effect fx, int ti
return FX_OK; return FX_OK;
} }
} }
fx.is_showing = true; fx.is_showing = true;
var max = ProductionTime(fx.production_effect.Product); var max = ProductionTime(fx.production_effect.Product);
var current = Min(max, fx.production_effect.Duration); var current = Min(max, fx.production_effect.Duration);
@ -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,23 +304,29 @@ 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++;
} }
} }
else else
{ {
while (product = GetDefinition(index)) while (product = GetDefinition(index))
{ {
if (IsProduct(product)) if (IsProduct(product))
{
products[GetLength(products)] = product; products[GetLength(products)] = product;
}
index++; index++;
} }
} }
@ -353,27 +375,31 @@ public func GetQueueIndex(id product_id)
@param position index in the queue @param position index in the queue
@param amount change of amount or nil @param amount change of amount or nil
@param infinite_production Sets the state of infinite production for the item. Can also be nil to not modify anything. @param infinite_production Sets the state of infinite production for the item. Can also be nil to not modify anything.
@return False if the item was in the queue and has now been removed. True otherwise. @return False if the item was in the queue and has now been removed. True otherwise.
*/ */
public func ModifyQueueIndex(int position, int amount, bool infinite_production) public func ModifyQueueIndex(int position, int amount, bool infinite_production)
{ {
// safety // safety
var queue_length = GetLength(queue); var queue_length = GetLength(queue);
if (position >= queue_length) return true; if (position >= queue_length) return true;
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.
if (!item.Infinite && item.Amount <= 0) if (!item.Infinite && item.Amount <= 0)
{ {
// 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,8 +417,11 @@ 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;
for (var info in queue) for (var info in queue)
@ -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,27 +470,28 @@ 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;
var product = info.Product; var product = info.Product;
var infinite = GetPlayerControlState(player, CON_ModifierMenu1) != 0; var infinite = GetPlayerControlState(player, CON_ModifierMenu1) != 0;
var amount = info.Amount; var amount = info.Amount;
var index = GetQueueIndex(product); var index = GetQueueIndex(product);
if (index == nil && (amount > 0 || infinite)) if (index == nil && (amount > 0 || infinite))
{ {
AddToQueue(product, amount, infinite, player); AddToQueue(product, amount, infinite, player);
} }
else if (index != nil) else if (index != nil)
{ {
// Toggle infinity? // Toggle infinity?
if (infinite) if (infinite)
{ {
@ -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,16 +517,20 @@ 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;
var producing_player = queue[0].ProducingPlayer; var producing_player = queue[0].ProducingPlayer;
@ -502,12 +543,14 @@ private func ProcessQueue()
CycleQueue(); CycleQueue();
return FX_OK; return FX_OK;
} }
// Update queue, reduce amount. // Update queue, reduce amount.
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,43 +561,51 @@ 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.
// Power will be substracted during the production process. // Power will be substracted during the production process.
CheckComponents(product, true); CheckComponents(product, true);
CheckFuel(product, true); CheckFuel(product, true);
// Add production effect. // Add production effect.
AddEffect("ProcessProduction", this, 100, 2, this, nil, product, producing_player); AddEffect("ProcessProduction", this, 100, 2, this, nil, product, producing_player);
return true; return true;
} }
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,7 +664,9 @@ 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();
}
} }
} }
} }
@ -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;
@ -654,13 +721,13 @@ public func CheckFuel(id product, bool remove)
{ {
// Extract the fuel amount from stored objects // Extract the fuel amount from stored objects
var fuel_extracted = fuel->~GetFuelAmount(fuel_needed); var fuel_extracted = fuel->~GetFuelAmount(fuel_needed);
if (fuel_extracted > 0) if (fuel_extracted > 0)
{ {
if (!fuel->~OnFuelRemoved(fuel_extracted)) fuel->RemoveObject(); if (!fuel->~OnFuelRemoved(fuel_extracted)) fuel->RemoveObject();
fuel_needed -= fuel_extracted; fuel_needed -= fuel_extracted;
} }
// Converted enough? Stop here. // Converted enough? Stop here.
if (fuel_needed <= 0) if (fuel_needed <= 0)
break; break;
@ -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,24 +752,24 @@ 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;
// Set product information // Set product information
effect.Product = product; effect.Product = product;
effect.producing_player = producing_player; effect.producing_player = producing_player;
// Set production duration to zero. // Set production duration to zero.
effect.Duration = 0; effect.Duration = 0;
// Production is active. // Production is active.
effect.Active = true; effect.Active = true;
// Callback to the producer. // Callback to the producer.
this->~OnProductionStart(effect.Product); this->~OnProductionStart(effect.Product);
// Consume power by registering as a consumer for the needed amount. // Consume power by registering as a consumer for the needed amount.
// But first hold the production until the power system gives it ok. // But first hold the production until the power system gives it ok.
// Always register the power request even if power need is zero. The // Always register the power request even if power need is zero. The
@ -710,8 +777,10 @@ 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;
} }
@ -723,9 +792,11 @@ public func OnNotEnoughPower()
{ {
effect.Active = false; effect.Active = false;
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(...);
} }
@ -737,21 +808,23 @@ public func OnEnoughPower()
{ {
effect.Active = true; effect.Active = true;
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;
// Add effect interval to production duration. // Add effect interval to production duration.
effect.Duration += effect.Interval; effect.Duration += effect.Interval;
// Check if production time has been reached. // Check if production time has been reached.
var production_time = ProductionTime(effect.Product); var production_time = ProductionTime(effect.Product);
if (effect.Duration >= production_time) if (effect.Duration >= production_time)
@ -766,28 +839,34 @@ 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;
// No need to consume power anymore. Always unregister even if there's a queue left to // No need to consume power anymore. Always unregister even if there's a queue left to
// 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);
// Create product. // Create product.
var product = CreateObject(effect.Product); var product = CreateObject(effect.Product);
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,15 +919,19 @@ 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,16 +939,20 @@ 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);
if (fuel_needed > 0) if (fuel_needed > 0)
{ {
// For now just use coal as a fuel. // For now just use coal as a fuel.
var coal_needed = 1 + (fuel_needed - 1) / Coal->GetFuelAmount(); var coal_needed = 1 + (fuel_needed - 1) / Coal->GetFuelAmount();
RequestObject(Coal, coal_needed, amount * coal_needed); RequestObject(Coal, coal_needed, amount * coal_needed);
@ -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,16 +991,23 @@ 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++;
} }
@ -921,14 +1023,14 @@ public func IsCollectionAllowed(object item)
// This uses the queue instead of the product list, because other items may need the original object. // This uses the queue instead of the product list, because other items may need the original object.
// This extremely special case is used by the ice object only, and should be removed in my opinion, // This extremely special case is used by the ice object only, and should be removed in my opinion,
// but it is included for compatibility reasons at the moment. // but it is included for compatibility reasons at the moment.
// TODO // TODO
//Log("Checking for conversion: queue is %v", queue); //Log("Checking for conversion: queue is %v", queue);
if (item->~CanConvertToLiquidType()) if (item->~CanConvertToLiquidType())
{ {
for (var queued in queue) for (var queued in queue)
{ {
var product = queued.Product; var product = queued.Product;
var i = 0, component_id; var i = 0, component_id;
while (component_id = product->GetComponent(nil, i)) while (component_id = product->GetComponent(nil, i))
{ {
@ -956,8 +1058,10 @@ public func RejectCollect(id item_id, object item)
GrabContents(item); GrabContents(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());