2016-01-28 16:39:16 +00:00
|
|
|
/**
|
|
|
|
* Liquid Container
|
|
|
|
*
|
|
|
|
* Basic interface for anything that can contain liquids.
|
|
|
|
*
|
2016-03-03 16:07:37 +00:00
|
|
|
* Author: Marky
|
2016-01-28 16:39:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
func IsLiquidContainer() { return true;}
|
|
|
|
|
|
|
|
func GetLiquidContainerMaxFillLevel()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:29:35 +00:00
|
|
|
func IsLiquidContainerForMaterial(string liquid_name)
|
2016-01-28 16:39:16 +00:00
|
|
|
{
|
2016-03-01 21:29:35 +00:00
|
|
|
return true;
|
2016-01-28 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:04:38 +00:00
|
|
|
func GetLiquidAmount(liquid_name)
|
2016-01-28 16:39:16 +00:00
|
|
|
{
|
2016-03-01 21:29:35 +00:00
|
|
|
var amount = 0;
|
2016-06-05 10:04:38 +00:00
|
|
|
var type = nil;
|
|
|
|
|
|
|
|
// in case that a value was supplied, try finding the type for that
|
|
|
|
if (liquid_name != nil)
|
|
|
|
{
|
|
|
|
type = GetLiquidDef(liquid_name);
|
|
|
|
if (type == nil) FatalError(Format("No such liquid: %s", liquid_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
// return everything if 'nil' was passed, or a specific amount if a value was passed
|
2016-03-01 21:29:35 +00:00
|
|
|
for (var liquid in GetLiquidContents())
|
2016-06-05 10:04:38 +00:00
|
|
|
if (liquid_name == nil || liquid->GetLiquidType() == type->GetLiquidType())
|
Liquid Object: Replaced liquid container logic
The liquid container now stores liquid objects, instead of volume and liquid type. Liquid objects can have be of a specific type, such as Liquid_Water, Liquid_Oil, etc..., but they can also just stay Library_Liquid for other liquids.
The liquid container logic has become more complex now, but still works as before. Transferring liquids to other objects may still need some improvements:
- at the moment, transferring liquids between liquid containers is possible
- maybe liquids objects, such as oil, can enter a liquid container while another liquid is in that container already - will need a unit test for that
- transferring liquid from a liquid container to a structure or crew member should be possible (but is not yet), if the target is not a liquid container but contains liquid containers
2016-02-19 05:53:44 +00:00
|
|
|
{
|
2016-03-01 21:29:35 +00:00
|
|
|
amount += liquid->~GetLiquidAmount();
|
Liquid Object: Replaced liquid container logic
The liquid container now stores liquid objects, instead of volume and liquid type. Liquid objects can have be of a specific type, such as Liquid_Water, Liquid_Oil, etc..., but they can also just stay Library_Liquid for other liquids.
The liquid container logic has become more complex now, but still works as before. Transferring liquids to other objects may still need some improvements:
- at the moment, transferring liquids between liquid containers is possible
- maybe liquids objects, such as oil, can enter a liquid container while another liquid is in that container already - will need a unit test for that
- transferring liquid from a liquid container to a structure or crew member should be possible (but is not yet), if the target is not a liquid container but contains liquid containers
2016-02-19 05:53:44 +00:00
|
|
|
}
|
2016-03-01 21:29:35 +00:00
|
|
|
return amount;
|
2016-01-28 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 21:43:44 +00:00
|
|
|
func GetLiquidAmountRemaining()
|
|
|
|
{
|
|
|
|
return GetLiquidContainerMaxFillLevel() - GetLiquidAmount();
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:29:35 +00:00
|
|
|
func GetLiquidContents()
|
2016-01-28 16:39:16 +00:00
|
|
|
{
|
2016-03-01 21:29:35 +00:00
|
|
|
return FindObjects(Find_Container(this), Find_Func("IsLiquid"));
|
2016-01-28 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------- Interaction
|
|
|
|
//
|
|
|
|
// Interfaces for interaction with other objects
|
|
|
|
|
|
|
|
/**
|
|
|
|
Extracts liquid from the container.
|
|
|
|
@param liquid_name: Material to extract; Wildcardsupport
|
|
|
|
Defaults to the current liquid if 'nil' is passed.
|
|
|
|
@param amount: Max Amount of liquid being extracted;
|
|
|
|
Defaults to all contained liquid if 'nil' is passed.
|
|
|
|
@param destination: Object that extracts the liquid
|
|
|
|
@return [returned_liquid, returned_amount]
|
|
|
|
- returned_liquid: Material being extracted
|
|
|
|
- returned_amount: Amount being extracted
|
|
|
|
*/
|
2016-06-05 10:04:38 +00:00
|
|
|
func RemoveLiquid(liquid_name, int amount, object destination)
|
2016-01-28 16:39:16 +00:00
|
|
|
{
|
2016-01-28 21:50:29 +00:00
|
|
|
if (amount < 0)
|
|
|
|
{
|
|
|
|
FatalError(Format("You can remove positive amounts of liquid only, got %d", amount));
|
|
|
|
}
|
2016-03-03 16:07:37 +00:00
|
|
|
|
|
|
|
amount = amount ?? GetLiquidAmount();
|
2016-03-01 21:29:35 +00:00
|
|
|
|
|
|
|
var removed = 0;
|
|
|
|
for (var liquid in GetLiquidContents())
|
2016-02-26 21:11:11 +00:00
|
|
|
{
|
2016-03-01 21:29:35 +00:00
|
|
|
if (removed >= amount) break;
|
|
|
|
|
|
|
|
if (!liquid_name) liquid_name = liquid->GetLiquidType();
|
|
|
|
|
2016-06-05 10:04:38 +00:00
|
|
|
removed += liquid->RemoveLiquid(liquid_name, amount - removed, destination)[1];
|
2016-02-26 21:11:11 +00:00
|
|
|
}
|
2016-03-01 21:29:35 +00:00
|
|
|
|
|
|
|
return [liquid_name, removed];
|
2016-01-28 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 21:29:35 +00:00
|
|
|
|
2016-01-28 16:39:16 +00:00
|
|
|
/**
|
|
|
|
Inserts liquid into the container.
|
|
|
|
@param liquid_name: Material to insert
|
2016-06-02 04:24:51 +00:00
|
|
|
@param amount: Max amount of material being inserted.
|
|
|
|
Passing a nil parameter will fill the
|
|
|
|
container to its maximum.
|
2016-01-28 16:39:16 +00:00
|
|
|
@param source: Object which inserts the liquid
|
|
|
|
@return returned_amount: The inserted amount
|
|
|
|
*/
|
2016-06-05 10:04:38 +00:00
|
|
|
func PutLiquid(liquid_name, int amount, object source)
|
2016-01-28 16:39:16 +00:00
|
|
|
{
|
2016-06-02 04:24:51 +00:00
|
|
|
amount = amount ?? this->GetLiquidAmountRemaining();
|
|
|
|
|
2016-01-28 21:50:29 +00:00
|
|
|
if (amount < 0)
|
|
|
|
{
|
|
|
|
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
|
|
|
|
}
|
2016-03-03 16:07:37 +00:00
|
|
|
|
2016-06-05 10:04:38 +00:00
|
|
|
var type = GetLiquidDef(liquid_name);
|
|
|
|
|
2016-05-05 18:58:16 +00:00
|
|
|
var max = this->GetLiquidContainerMaxFillLevel();
|
2016-03-01 21:29:35 +00:00
|
|
|
var before = GetLiquidAmount(liquid_name);
|
2016-05-05 18:58:16 +00:00
|
|
|
if (max > 0 && before >= max) return 0;
|
2016-03-25 15:24:43 +00:00
|
|
|
|
2016-03-25 18:34:36 +00:00
|
|
|
if (type)
|
2016-03-23 19:11:23 +00:00
|
|
|
{
|
2016-03-25 18:34:36 +00:00
|
|
|
var liquid = type->~CreateLiquid(amount);
|
|
|
|
if (liquid) Collect(liquid, true);
|
2016-03-23 19:11:23 +00:00
|
|
|
// the check is necessary here, because the liquid may get removed if the barrel already
|
|
|
|
// has a stack inside
|
|
|
|
if (liquid && !(liquid->Contained())) liquid->RemoveObject();
|
|
|
|
}
|
2016-03-03 16:07:37 +00:00
|
|
|
|
2016-03-01 21:29:35 +00:00
|
|
|
var after = GetLiquidAmount(liquid_name);
|
|
|
|
return after - before;
|
2016-02-26 14:19:41 +00:00
|
|
|
}
|
2016-06-05 10:04:38 +00:00
|
|
|
|
2016-08-07 19:26:15 +00:00
|
|
|
func AcceptsLiquid(liquid_name, int amount)
|
|
|
|
{
|
|
|
|
return amount <= this->GetLiquidContainerMaxFillLevel() - GetLiquidAmount(liquid_name);
|
|
|
|
}
|
|
|
|
|
2016-06-05 10:04:38 +00:00
|
|
|
private func GetLiquidDef(liquid_name)
|
|
|
|
{
|
|
|
|
if (GetType(liquid_name) == C4V_String)
|
|
|
|
{
|
|
|
|
return GetDefinition(liquid_name);
|
|
|
|
}
|
|
|
|
else if (GetType(liquid_name) == C4V_Def)
|
|
|
|
{
|
|
|
|
return liquid_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FatalError(Format("The first parameter of GetLiquidDef() must either be a string or definition. You passed %v.", liquid_name));
|
|
|
|
}
|
|
|
|
}
|