Liquds: Liquids can be passed either as a string or as a definition

qteditor
Mark 2016-06-05 12:04:38 +02:00
parent 923ee485ec
commit d51a8cfa6d
3 changed files with 65 additions and 11 deletions

View File

@ -161,7 +161,7 @@ Inserts liquid into the object.
@param source: Object which inserts the liquid
@return returned_amount: The inserted amount
*/
func PutLiquid(string liquid_name, int amount, object source)
func PutLiquid(liquid_name, int amount, object source)
{
amount = amount ?? 0;
@ -169,7 +169,13 @@ func PutLiquid(string liquid_name, int amount, object source)
{
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
if (GetType(liquid_name) != C4V_String && GetType(liquid_name) != C4V_Def)
{
FatalError(Format("The first parameter of PutLiquid() must either be a string or definition. You passed %v.", GetType(liquid_name)));
}
if (GetType(liquid_name) == C4V_Def) liquid_name = liquid_name->~GetLiquidType();
if (liquid_name == GetLiquidType())
{
amount = BoundBy(MaxStackCount() - GetLiquidAmount(), 0, amount);
@ -194,15 +200,21 @@ Extracts liquid from the object.
- returned_liquid: Material being extracted
- returned_amount: Amount being extracted
*/
func RemoveLiquid(string liquid_name, int amount, object destination)
func RemoveLiquid(liquid_name, int amount, object destination)
{
if (amount < 0)
{
FatalError(Format("You can remove positive amounts of liquid only, got %d", amount));
}
if (GetType(liquid_name) != C4V_String && GetType(liquid_name) != C4V_Def)
{
FatalError(Format("The first parameter of RemoveLiquid() must either be a string or definition. You passed %v.", GetType(liquid_name)));
}
// default parameters if nothing is provided: the current material and level
liquid_name = liquid_name ?? GetLiquidType();
if (GetType(liquid_name) == C4V_Def) liquid_name = liquid_name->~GetLiquidType();
amount = amount ?? GetLiquidAmount();
//Wrong material?

View File

@ -19,11 +19,21 @@ func IsLiquidContainerForMaterial(string liquid_name)
return true;
}
func GetLiquidAmount(string liquid_name)
func GetLiquidAmount(liquid_name)
{
var amount = 0;
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
for (var liquid in GetLiquidContents())
if (liquid_name == nil || liquid->GetLiquidType() == liquid_name)
if (liquid_name == nil || liquid->GetLiquidType() == type->GetLiquidType())
{
amount += liquid->~GetLiquidAmount();
}
@ -55,7 +65,7 @@ Extracts liquid from the container.
- returned_liquid: Material being extracted
- returned_amount: Amount being extracted
*/
func RemoveLiquid(string liquid_name, int amount, object destination)
func RemoveLiquid(liquid_name, int amount, object destination)
{
if (amount < 0)
{
@ -71,10 +81,7 @@ func RemoveLiquid(string liquid_name, int amount, object destination)
if (!liquid_name) liquid_name = liquid->GetLiquidType();
//if (liquid->GetLiquidType() == liquid_name)
//{
removed += liquid->RemoveLiquid(liquid_name, amount - removed, destination)[1];
//}
removed += liquid->RemoveLiquid(liquid_name, amount - removed, destination)[1];
}
return [liquid_name, removed];
@ -90,7 +97,7 @@ Inserts liquid into the container.
@param source: Object which inserts the liquid
@return returned_amount: The inserted amount
*/
func PutLiquid(string liquid_name, int amount, object source)
func PutLiquid(liquid_name, int amount, object source)
{
amount = amount ?? this->GetLiquidAmountRemaining();
@ -99,11 +106,12 @@ func PutLiquid(string liquid_name, int amount, object source)
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
var type = GetLiquidDef(liquid_name);
var max = this->GetLiquidContainerMaxFillLevel();
var before = GetLiquidAmount(liquid_name);
if (max > 0 && before >= max) return 0;
var type = GetDefinition(liquid_name);
if (type)
{
var liquid = type->~CreateLiquid(amount);
@ -116,3 +124,19 @@ func PutLiquid(string liquid_name, int amount, object source)
var after = GetLiquidAmount(liquid_name);
return after - before;
}
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));
}
}

View File

@ -162,6 +162,7 @@ global func Test2_Execute()
var passed = true;
passed &= doTest("Liquid can fill empty barrel. Got %v, expected %v.", container->Contents(), liquid);
passed &= doTest("Barrel contains %d units, expected %d.", container->GetLiquidAmount("Water"), 100);
passed &= doTest("GetLiquidAmount() can be called with definitions, too. Got %d units, expected %d.", container->GetLiquidAmount(Water), 100);
passed &= doTest("The liquid returns a max stack count of %d, expected %d.", container->Contents()->MaxStackCount(), 300);
// -----
@ -292,6 +293,11 @@ global func Test3_Execute()
container->Contents()->SetStackCount(filled);
passed &= doTest("Container is filled fully if no amount parameter is passed and the container is filled partially. Got %d, expected %d.", container->PutLiquid("Water"), container->GetLiquidContainerMaxFillLevel() - filled);
if (container) container->RemoveObject();
container = CreateObject(Barrel);
passed &= doTest("Container can be filled with definition input. Got %d, expected %d.", container->PutLiquid(Oil, filled), filled);
container->RemoveObject();
return passed;
}
@ -362,6 +368,18 @@ global func Test4_Execute()
passed &= doTest("Container returns the correct amount when extracting material and amount 'nil'. Got %d, expected %d.", returned[1], expected[1]);
passed &= doTest("Container is empty after removing material and amount 'nil'. Got %d, expected %d.", container->GetLiquidAmount(), 0);
// -----
Log("Parameters");
container->PutLiquid(Oil, 100);
returned = container->RemoveLiquid(Oil, 50, nil);
expected = [Oil, 50];
passed &= doTest("Container returns the contained material when extracting material and amount 'nil'. Got %v, expected %v.", returned[0], expected[0]);
passed &= doTest("Container returns the correct amount when extracting material and amount 'nil'. Got %d, expected %d.", returned[1], expected[1]);
passed &= doTest("Container is not empty after partially removing material. Got %d, expected %d.", container->GetLiquidAmount(), 50);
container->RemoveObject();
return passed;
}