Refactoring: Liquid container

The liquid container library was severely reduced by the changes that the liquid objects bring. The remaining unit tests nearly work.
liquid_container
Mark 2016-03-01 22:29:35 +01:00
parent afc8797b2f
commit a7ed516c39
6 changed files with 297 additions and 608 deletions

View File

@ -7,6 +7,7 @@
#include Library_CarryHeavy
#include Library_LiquidContainer
#include Library_HasExtraSlot
public func GetCarryTransform(clonk)
{
@ -25,13 +26,25 @@ protected func Initialize()
AddTimer("Check", 5);
}
func RejectCollect(id def, object item)
{
if (item && item->~IsLiquid() && this->~IsLiquidContainerForMaterial(item->~GetLiquidType()))
{
return false; // Collect it!
}
else
{
return true; // Reject it!
}
}
private func Hit()
{
this->PlayBarrelHitSound();
if (!LiquidContainerIsEmpty())
if (Contents())
{
if (GBackLiquid(0, this.BarrelIntakeY)
&& GetMaterial(0, this.BarrelIntakeY) != GetLiquidType())
&& GetMaterial(0, this.BarrelIntakeY) != Contents()->GetLiquidType())
return;
EmptyBarrel(GetR());
@ -59,9 +72,9 @@ private func FillWithLiquid()
var mat = GetMaterial(0, intake);
var mat_name = MaterialName(mat);
if (!LiquidContainerAccepts(mat_name)) return;
if (!IsLiquidContainerForMaterial(mat_name)) return;
var remaining_volume = GetLiquidContainerMaxFillLevel() - GetLiquidFillLevel();
var remaining_volume = GetLiquidContainerMaxFillLevel() - GetLiquidAmount();
var extracted = 0;
while(extracted < remaining_volume && GetMaterial(0, intake) == mat)
{
@ -79,35 +92,29 @@ private func FillWithLiquid()
private func EmptyBarrel(int angle, int strength, object clonk)
{
var material = GetLiquidType();
var volume = GetLiquidFillLevel();
if (GetLiquidItem())
if (Contents())
{
GetLiquidItem()->Disperse(angle, strength);
}
var material = Contents()->~GetLiquidType();
var volume = Contents()->~GetLiquidAmount();
Contents()->~Disperse(angle, strength);
var spray = {};
spray.Liquid = material;
spray.Volume = volume;
spray.Strength = strength;
spray.Angle = angle;
spray.Clonk = clonk;
AddEffect("ExtinguishingSpray", clonk, 100, 1, this, nil, spray);
var spray = {};
spray.Liquid = material;
spray.Volume = volume;
spray.Strength = strength;
spray.Angle = angle;
spray.Clonk = clonk;
AddEffect("ExtinguishingSpray", clonk, 100, 1, this, nil, spray);
}
}
private func UpdateLiquidContainer()
{
if (LiquidContainerIsEmpty())
{
SetColor(RGB(0, 0, 0));
//Value. Base value is 10.
SetProperty("Value", 10); // TODO: this is a bug! The value is shared by barrel (value:12) and metal barrel (value:16)!
}
else
if (Contents())
{
var color;
var material = Material(GetLiquidType());
var material = Material(Contents()->GetLiquidType());
if (material >= 0)
{
var tex = GetMaterialVal("TextureOverlay", "Material", material);
@ -119,14 +126,21 @@ private func UpdateLiquidContainer()
}
SetColor(color);
}
this.Name = GetNameForBarrel(GetLiquidType());
else
{
SetColor(RGB(0, 0, 0));
//Value. Base value is 10.
SetProperty("Value", 10); // TODO: this is a bug! The value is shared by barrel (value:12) and metal barrel (value:16)!
}
this.Name = GetNameForBarrel();
return;
}
public func ControlUse(object clonk, int iX, int iY)
{
var AimAngle = Angle(0, 0, iX, iY);
if (!LiquidContainerIsEmpty())
if (Contents())
{
EmptyBarrel(AimAngle, 50, clonk);
if (iX > 1)
@ -134,7 +148,7 @@ public func ControlUse(object clonk, int iX, int iY)
if (iX < -1)
Contained()->SetDir(0);
}
return 1;
return true;
}
protected func FxExtinguishingSprayStart(object target, proplist effect, int temp, proplist spray)
@ -184,53 +198,35 @@ public func IsBarrel()
return true;
}
public func IsLiquidContainerForMaterial(string sznMaterial)
public func IsLiquidContainerForMaterial(string liquid_name)
{
return WildcardMatch("Water", sznMaterial) || WildcardMatch("Oil", sznMaterial);
return WildcardMatch("Water", liquid_name) || WildcardMatch("Oil", liquid_name);
}
public func CanBeStackedWith(object other)
{
// Does not take into account the fill level for now.
return inherited(other, ...) && (other->~GetLiquidType() == this->GetLiquidType());
var liquid = other->Contents();
var both_filled = Contents() && liquid;
var both_empty = !Contents() && !liquid;
if (both_filled) both_filled = liquid->~GetLiquidType() == Contents()->~GetLiquidType();
return inherited(other, ...) && (both_empty || both_filled);
}
public func CalcValue(object in_base, int for_player)
func GetNameForBarrel()
{
var val = GetDefValue();
if (!LiquidContainerIsEmpty())
if (Contents())
{
val += GetValueOf(GetLiquidType()) * GetLiquidFillLevel() / GetLiquidContainerMaxFillLevel();
var name = Format("%s $NameWith$ %s", this.Prototype.Name, Contents()->GetName());
return name;
}
else
{
return this.Prototype.Name;
}
return val;
}
private func GetValueOf(string szMaterial) // 300 px of...
{
// just some provisional values, feel free to change them
// for gameplay reasons
if (szMaterial == "Water") return -6;
if (szMaterial == "Lava") return -10;
if (szMaterial == "DuroLava") return -10;
if (szMaterial == "Acid") return -8;
if (szMaterial == "Firefluid") return 10;
return 0;
}
// When is this considered as fuel for the steam engine?
func IsFuel()
{
return WildcardMatch("Oil", GetLiquidType());
}
func GetNameForBarrel(string liquid)
{
if (liquid == nil) return this.Prototype.Name;
var liquid_name = LiquidNames[liquid] ?? "$MaterialUnknown$";
var name = Format("%s $NameWith$ %s", this.Prototype.Name, liquid_name);
return name;
}
local LiquidNames = {
@ -247,4 +243,4 @@ local Collectible = true;
local Name = "$Name$";
local Description = "$Description$";
local ContactIncinerate = 2;
local BarrelIntakeY = 3;
local BarrelIntakeY = 3;

View File

@ -13,7 +13,24 @@
static const FX_LIQUID_Dispersion = "IntLiquidDispersion";
func IsLiquid() { return true;}
func MaxStackCount() { return Stackable_Max_Count; }
func MaxStackCount()
{
if (this)
{
var container = Contained();
var limit = GetContainerLimit(container);
if (limit) return limit;
}
return Stackable_Max_Count;
}
protected func Construction()
{
_inherited(...);
SetStackCount(1); // this should be a single object, not the whole stack
}
// -------------- Getters
//
@ -186,3 +203,67 @@ func RemoveLiquid(string liquid_name, int amount, object destination)
return [liquid_name, amount];
}
/**
Converts a liquid name to a definition
that represents that liquid.
@par liquid_name the name of the liquid
@return the Id of the liquid object,
or nil if no such object exists
*/
func GetLiquidID(string liquid_name)
{
if (liquid_name == "Acid") return Liquid_Acid;
if (liquid_name == "Lava") return Liquid_Lava;
if (liquid_name == "Oil") return Liquid_Oil;
if (liquid_name == "Water") return Liquid_Water;
return Library_Liquid;
}
/**
Creates a liquid object with the specified name
and amount. Liquids with amount 0 can be created
that way.
*/
func CreateLiquid(string liquid_name, int amount)
{
var item = CreateObject(GetLiquidID(liquid_name));
item->SetStackCount(amount);
return item;
}
public func CanBeStackedWith(object other)
{
var is_same_liquid = other->~GetLiquidType() == this->~GetLiquidType();
return _inherited(other, ...) && is_same_liquid;
}
public func TryPutInto(object into, bool only_add_to_existing_stacks)
{
if (!_inherited(into, only_add_to_existing_stacks, ...))
{
var container_limit = GetContainerLimit(into);
if (container_limit && GetStackCount() > container_limit)
{
var sample = TakeObject();
sample->Enter(into);
if (sample)
{
return _inherited(into, only_add_to_existing_stacks, ...);
}
}
}
return false;
}
func GetContainerLimit(object container)
{
if (container && container->~IsLiquidContainer())
{
return container->~GetLiquidContainerMaxFillLevel();
}
return 0;
}

View File

@ -4,7 +4,7 @@ func GetLiquidType() { return "Water"; }
func Disperse()
{
DisperseMaterial(IsLiquid(), GetLiquidAmount());
DisperseMaterial(GetLiquidType(), GetLiquidAmount());
_inherited(...);
}

View File

@ -6,181 +6,33 @@
* Author: Ringwaul, ST-DDT, Marky
*/
local liquid_container_item;
// -------------- Properties
//
// Simple properties that define the object as a liquid container,
// what kind of liquid it can hold, how much it can hold
//
// naming scheme: [verb]LiquidContainer[attribute], because it concerns the container
func IsLiquidContainer() { return true;}
func IsLiquidContainerForMaterial(string liquid_name)
{
return false;
}
func GetLiquidContainerMaxFillLevel()
{
return 0;
}
// -------------- Current Status
//
// Simple boolean status checks
//
// naming scheme: LiquidContainer[attribute/question]
func LiquidContainerIsEmpty()
func IsLiquidContainerForMaterial(string liquid_name)
{
return (GetLiquidFillLevel() == 0);
return true;
}
func LiquidContainerIsFull()
func GetLiquidAmount(string liquid_name)
{
return GetLiquidFillLevel() == GetLiquidContainerMaxFillLevel();
}
func LiquidContainerAccepts(string liquid_name)
{
return this->IsLiquidContainerForMaterial(liquid_name)
&& (LiquidContainerIsEmpty() || GetLiquidType() == liquid_name);
}
// -------------- Getters
//
// Getters for stored liquid and amount
// - these should be used primarily by objects that include this library
//
// naming scheme: GetLiquid[attribute], because it concerns the liquid
func GetLiquidItem()
{
return liquid_container_item;
}
func TransferLiquidItem(object source)
{
if (source && source.IsLiquid != nil)
var amount = 0;
for (var liquid in GetLiquidContents())
if (liquid_name == nil || liquid->GetLiquidType() == liquid_name)
{
var liquid = source->IsLiquid();
if (liquid && !LiquidContainerAccepts(liquid)) return false;
var remaining = GetLiquidFillLevelRemaining();
if (source->GetLiquidAmount() <= remaining)
{
if (!GetLiquidItem())
{
SetLiquidItem(source);
}
else
{
var extracted = source->RemoveLiquid(nil, nil, this);
PutLiquid(extracted[0], extracted[1]);
}
return true;
}
else
{
var extracted = source->RemoveLiquid(nil, remaining, this);
if (!GetLiquidItem()) SetLiquidType(extracted[0]); // create liquid item if necessary
PutLiquid(extracted[0], extracted[1]);
return false;
}
amount += liquid->~GetLiquidAmount();
}
return false;
return amount;
}
func SetLiquidItem(object item)
func GetLiquidContents()
{
if (item && (item->~IsLiquid() || item.IsLiquid != nil))
{
liquid_container_item = item;
}
else
{
FatalError(Format("Object %v is not a liquid", item));
}
}
func ResetLiquidItem()
{
liquid_container_item = nil;
this->~UpdateLiquidContainer();
}
func GetLiquidType()
{
if (GetLiquidItem())
{
return GetLiquidItem()->IsLiquid();
}
return nil;
}
func GetLiquidFillLevel()
{
if (GetLiquidItem())
{
return GetLiquidItem()->GetLiquidAmount();
}
return 0;
}
func GetLiquidFillLevelRemaining()
{
return GetLiquidContainerMaxFillLevel() - GetLiquidFillLevel();
}
// -------------- Setters
//
// Setters for stored liquid and amount
// - these should be used primarily by objects that include this library
//
// naming scheme: SetLiquid[attribute], because it concerns the liquid
func SetLiquidType(string liquid_name)
{
var amount = 0; // for new items only
if (GetLiquidItem())
{
amount = GetLiquidItem()->GetLiquidAmount();
if (!WildcardMatch(liquid_name, GetLiquidItem()->IsLiquid()))
GetLiquidItem()->RemoveObject();
}
if (!GetLiquidItem())
{
var item = Library_Liquid->CreateLiquid(liquid_name, amount);
if (amount > 0) item->UpdateLiquidObject();
// if not removed because of amount
if (item) item->Enter(this);
}
}
func SetLiquidFillLevel(int amount)
{
if (!GetLiquidItem())
{
SetLiquidType(nil);
}
ChangeLiquidFillLevel(amount - GetLiquidFillLevel());
}
func ChangeLiquidFillLevel(int amount)
{
if (GetLiquidItem())
{
GetLiquidItem()->DoLiquidAmount(amount);
}
this->~UpdateLiquidContainer();
return FindObjects(Find_Container(this), Find_Func("IsLiquid"));
}
// -------------- Interaction
@ -204,17 +56,24 @@ func RemoveLiquid(string liquid_name, int amount, object destination)
{
FatalError(Format("You can remove positive amounts of liquid only, got %d", amount));
}
var removed = 0;
for (var liquid in GetLiquidContents())
{
if (removed >= amount) break;
if (!liquid_name) liquid_name = liquid->GetLiquidType();
//if (liquid->GetLiquidType() == liquid_name)
//{
removed += liquid->RemoveLiquid(liquid_name, amount - removed, destination)[1];
//}
}
if (GetLiquidItem())
{
return GetLiquidItem()->RemoveLiquid(liquid_name, amount, destination);
}
else
{
return [nil, 0];
}
return [liquid_name, removed];
}
/**
Inserts liquid into the container.
@param liquid_name: Material to insert
@ -228,47 +87,10 @@ func PutLiquid(string liquid_name, int amount, object source)
{
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
TransferLiquidItem(source);
if (!GetLiquidItem() && LiquidContainerAccepts(liquid_name))
{
SetLiquidType(liquid_name);
}
if (GetLiquidItem())
{
return GetLiquidItem()->PutLiquid(liquid_name, amount, source);
}
else //does not have a liquid item yet?
{
return 0;
}
}
// -------------- Internals --------------
//
// Internal stuff
func SaveScenarioObject(props)
{
if (!inherited(props, ...)) return false;
if (GetLiquidType())
props->AddCall("Fill", this, "SetLiquidContainer", Format("%v", GetLiquidType()), GetLiquidFillLevel());
return true;
}
// set the current state, without sanity checks
func SetLiquidContainer(string liquid_name, int amount)
{
SetLiquidType(liquid_name);
SetLiquidFillLevel(amount);
}
// lose the liquid item if it exits the container
func Ejection(object item)
{
if (item == GetLiquidItem())
ResetLiquidItem();
_inherited(...);
var before = GetLiquidAmount(liquid_name);
var type = Library_Liquid->GetLiquidID(liquid_name);
CreateContents(type, amount);
var after = GetLiquidAmount(liquid_name);
return after - before;
}

View File

@ -245,7 +245,6 @@ public func TryPutInto(object into, bool only_add_to_existing_stacks)
// then check this object
for (var content in contents)
{
var howmany = 0;
if (!content)
continue;
TryAddToStack(content);

View File

@ -152,23 +152,102 @@ global func Test2_OnStart(int plr){ return true;}
global func Test2_OnFinished(){ return; }
global func Test2_Execute()
{
Log("Test the behaviour of GetFillLevel and SetFillLevel");
Log("Test the behaviour of liquid objects entering liquid containers");
var container = CreateObject(Barrel);
var liquid = CreateObject(Liquid_Water);
// can fill empty barrel with the liquid
liquid->SetStackCount(100);
liquid->Enter(container);
var passed = true;
var test_data = [nil, -1, 0, 1, container->GetLiquidContainerMaxFillLevel()/2, container->GetLiquidContainerMaxFillLevel(), container->GetLiquidContainerMaxFillLevel() + 1];
var returned = container->Contents();
var test = (returned == liquid); passed &= test;
Log("- Liquid can fill empty barrel: %v", test);
returned = container->GetLiquidAmount("Water");
test = (100 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 100, test);
for (var value in test_data)
{
var expected_value = value;
container->SetLiquidFillLevel(value);
var returned = container->GetLiquidFillLevel();
if (value == nil || value == -1) expected_value = 0; // accept 0 as a return value in this case.
var test = (expected_value == returned); passed &= test;
Log("- Container returns %d (expected %d) if fill level is set to %d, values should be equal: %v", returned, expected_value, value, test);
}
// can fill barrel with more liquid, liquid object gets removed
liquid = CreateObject(Liquid_Water);
liquid->SetStackCount(100);
liquid->Enter(container);
test = (liquid == nil); passed &= test;
Log("- Liquid can enter filled barrel, liquid got removed: %v", test);
returned = container->GetLiquidAmount();
test = (200 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 200, test);
// cannot fill in more than the allowed amount
liquid = CreateObject(Liquid_Water);
liquid->SetStackCount(200);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel if the capacity is exceeded: %v", test);
returned = container->GetLiquidAmount();
test = (300 == returned); passed &= test;
Log("- Barrel does increase fill level, up to the allowed amount, contains %d units, expected %d: %v", returned, 300, test);
returned = liquid->GetLiquidAmount();
test = (100 == returned); passed &= test;
Log("- Liquid object still contains %d units, expected %d: %v", returned, 100, test);
Log("- Resetting liquid amount to 0");
liquid->RemoveObject();
container->Contents()->RemoveObject();
// cannot fill in empty barrel and empty liquid object partially
liquid = CreateObject(Liquid_Water);
liquid->SetStackCount(500);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter empty barrel if the capacity is exceeded: %v", test);
returned = container->GetLiquidAmount();
test = (300 == returned); passed &= test;
Log("- Barrel does increase fill level, up to the allowed amount, contains %d units, expected %d: %v", returned, 300, test);
returned = liquid->GetLiquidAmount();
test = (200 == returned); passed &= test;
Log("- Liquid object still contains %d units, expected %d: %v", returned, 200, test);
Log("- Resetting liquid amount to 200");
liquid->RemoveObject();
container->Contents()->SetStackCount(200);
// cannot fill in a different liquid
liquid = CreateObject(Liquid_Oil);
liquid->SetStackCount(50);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel of a different liquid type: %v", test);
returned = container->GetLiquidAmount();
test = (200 == returned); passed &= test;
Log("- Barrel does not increase fill level, contains %d units, expected %d: %v", returned, 200, test);
liquid->RemoveObject();
// barrel gets emptied when liquid exits it
liquid = container->Contents();
liquid->Exit();
returned = container->Contents();
test = (returned == nil); passed &= test;
Log("- Liquid container should be empty when liquid leaves it: %v", test);
returned = container->GetLiquidAmount();
test = (returned == 0); passed &= test;
Log("- Liquid container return a liquid amount of 0 when liquid leaves it: %v", test);
test = (liquid != nil); passed &= test;
Log("- Liquid exists after leaving the container: %v", test);
liquid->RemoveObject();
container->RemoveObject();
return passed;
}
@ -176,142 +255,6 @@ global func Test2_Execute()
global func Test3_OnStart(int plr){ return true;}
global func Test3_OnFinished(){ return; }
global func Test3_Execute()
{
Log("Test the behaviour of GetLiquidType and SetLiquidType");
var container = CreateObject(Barrel);
var passed = true;
var test_data = [nil, "Water", "Lava", "123", "#24942fwijvri"];
// set a special test function that accepts other material, too
container.IsLiquidContainerForMaterial = Barrel.Test3_IsLiquidContainerForMaterial;
for (var value in test_data)
{
container->SetLiquidType(value);
var returned = container->GetLiquidType();
var test = (value == returned); passed &= test;
Log("- Container returns %s if liquid name is set to %s, values should be equal", returned, value);
}
container->RemoveObject();
return passed;
}
global func Test4_OnStart(int plr){ return true;}
global func Test4_OnFinished(){ return; }
global func Test4_Execute()
{
Log("Test the behaviour of LiquidContainerIsEmpty");
// a loop would be cool, but that would work only with runtime overloadable functions
var container = CreateObject(Barrel);
Log("Max fill level for container is %d", container->GetLiquidContainerMaxFillLevel());
container->SetLiquidFillLevel(0);
var test1 = container->LiquidContainerIsEmpty();
Log("- Container fill level: %v", container->GetLiquidFillLevel());
Log("- Container returns 'true' if liquid fill level is 0: %v", test1);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel() / 2);
var test2 = container->LiquidContainerIsEmpty();
Log("- Container fill level: %v", container->GetLiquidFillLevel());
Log("- Container returns 'false' if liquid fill level is 50%: %v", !test2);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel());
var test3 = container->LiquidContainerIsEmpty();
Log("- Container fill level: %v", container->GetLiquidFillLevel());
Log("- Container returns 'false' if liquid fill level is 100%: %v", !test3);
container->RemoveObject();
return test1 && !test2 && !test3;
}
global func Test5_OnStart(int plr){ return true;}
global func Test5_OnFinished(){ return; }
global func Test5_Execute()
{
Log("Test the behaviour of LiquidContainerIsFull");
// a loop would be cool, but that would work only with runtime overloadable functions
var container = CreateObject(Barrel);
container->SetLiquidFillLevel(0);
var test1 = !container->LiquidContainerIsFull();
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel() / 2);
var test2 = !container->LiquidContainerIsFull();
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel());
var test3 = container->LiquidContainerIsFull();
Log("- Container returns 'false' if liquid fill level is 0: %v", test1);
Log("- Container returns 'false' if liquid fill level is 50%: %v", test2);
Log("- Container returns 'true' if liquid fill level is 100%: %v", test3);
container->RemoveObject();
return test1 && test2 && test3;
}
global func Test6_OnStart(int plr){ return true;}
global func Test6_OnFinished(){ return; }
global func Test6_Execute()
{
Log("Test the behaviour of LiquidContainerAccepts");
var container = CreateObject(Barrel);
var passed = true;
// incompatible material
var test = !container->LiquidContainerAccepts("Dummy"); passed &= test;
Log("- Container returns 'false' if material is wrong: %v", test);
// fill level
container->SetLiquidFillLevel(0);
test = container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'true' if liquid fill level is 0% and material is ok: %v", test);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel() / 2);
test = !container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'false' if liquid fill level is 50% and contained material is 'nil': %v", test);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel());
test = !container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'false' if liquid fill level is 100% and material is ok: %v", test);
// material
Log("Setting container to be filled with a material");
container->SetLiquidType("Oil");
Log("- Fill material is %s", container->GetLiquidType());
container->SetLiquidFillLevel(0);
container->SetLiquidType("Oil");
test = container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'true' if filled with material and liquid fill level is 0% and other material is ok: %v", test);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel() / 2);
container->SetLiquidType("Oil");
test = !container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'false' if filled with material and liquid fill level is 50% and other material is ok: %v", test);
container->SetLiquidFillLevel(container->GetLiquidContainerMaxFillLevel() / 2);
container->SetLiquidType("Water");
test = container->LiquidContainerAccepts("Water"); passed &= test;
Log("- Container returns 'true' if liquid fill level is 50% and material is ok: %v", test);
container->RemoveObject();
return passed;
}
global func Test7_OnStart(int plr){ return true;}
global func Test7_OnFinished(){ return; }
global func Test7_Execute()
{
Log("Test the behaviour of PutLiquid");
@ -322,40 +265,41 @@ global func Test7_Execute()
var test = (container->PutLiquid("Lava", 1, nil) == 0);
passed &= test;
Log("- Container returns '0' when inserting 1 pixel of incompatible material: %v", test);
test = container->GetLiquidType() == nil; passed &= test;
Log("- Container returns 'nil' for material name: %v, %v", test, container->GetLiquidType());
test = container->GetLiquidFillLevel() == 0; passed &= test;
Log("- Container returns '0' for fill level: %v", test);
test = container->Contents() == nil; passed &= test;
Log("- Container returns 'nil' for contents: %v, %v", test, container->Contents());
// compatible material
test = (container->PutLiquid("Water", 1, nil) == 1);
Log("- Container returns '1' when inserting 1 pixel of compatible material: %v", test);
test = container->GetLiquidType() == "Water"; passed &= test;
Log("- Container returns the liquid name when inserting 1 pixel of compatible material: %v", test);
test = container->GetLiquidFillLevel() == 1; passed &= test;
Log("- Container returns the fill level 1 when inserting 1 pixel of compatible material: %d, %v", container->GetLiquidFillLevel(), test);
test = container->FindContents(Liquid_Water) != nil; passed &= test;
Log("- Container has contents Liquid_Water when inserting 1 pixel of compatible material: %v", test);
if (passed)
{
test = container->FindContents(Liquid_Water)->GetLiquidAmount() == 1; passed &= test;
Log("- Container returns the fill level 1 when inserting 1 pixel of compatible material: %d, %v", container->FindContents(Liquid_Water)->GetLiquidAmount(), test);
}
test = (container->PutLiquid("Water", container->GetLiquidContainerMaxFillLevel(), nil) == (container->GetLiquidContainerMaxFillLevel() - 1));
passed &= test;
Log("- Container returns 'the actually inserted material' when inserting more than the volume: %v", test);
test = container->GetLiquidFillLevel() == container->GetLiquidContainerMaxFillLevel(); passed &= test;
test = container->GetLiquidAmount("Water") == container->GetLiquidContainerMaxFillLevel(); passed &= test;
Log("- Container returns the fill level when inserting more than the volume: %v", test);
container->RemoveObject();
return passed;
}
global func Test8_OnStart(int plr){ return true;}
global func Test8_OnFinished(){ return; }
global func Test8_Execute()
global func Test4_OnStart(int plr){ return true;}
global func Test4_OnFinished(){ return; }
global func Test4_Execute()
{
Log("Test the behaviour of RemoveLiquid");
var container = CreateObject(Barrel);
var passed = true;
container->SetLiquidContainer("Water", 100);
container->CreateContents(Liquid_Water, 100);
// incompatible material
var returned = container->RemoveLiquid("Lava", 0, nil);
var test = (returned[0] == "Water");
@ -363,7 +307,7 @@ global func Test8_Execute()
Log("- Container returns the contained material when removing incompatible material: %v", test);
test = (returned[1] == 0); passed &= test;
Log("- Container returns no amount when removing incompatible material: %v", test);
test = (container->GetLiquidFillLevel() == 100);
test = (container->GetLiquidAmount() == 100);
Log("- Container contents do not change when removing incompatible material: %v", test);
// compatible material
@ -372,7 +316,7 @@ global func Test8_Execute()
Log("- Container returns the extracted material name: %v", test);
test = returned[1] == 1; passed &= test;
Log("- Container returns the correct amount when removing 1 pixel of compatible material: %v", test);
test = (container->GetLiquidFillLevel() == 99);
test = (container->GetLiquidAmount() == 99);
Log("- Container contents do change when removing compatible material: %v", test);
returned = container->RemoveLiquid("Water", 100, nil);
@ -380,39 +324,39 @@ global func Test8_Execute()
Log("- Container returns the extracted material name: %v", test);
test = returned[1] == 99; passed &= test;
Log("- Container returns the correct amount when removing compatible material: %v", test);
test = (container->GetLiquidFillLevel() == 0);
test = (container->GetLiquidAmount() == 0);
Log("- Container contents do change when removing compatible material: %v", test);
// request everything
var material_alternative = "Oil";
container->SetLiquidContainer(material_alternative, 100);
container->CreateContents(Liquid_Oil, 100);
returned = container->RemoveLiquid(nil, 50, nil);
test = (returned[0] == material_alternative);
Log("- Container returns the contained material when extracting material 'nil': %v", test);
test = returned[1] == 50; passed &= test;
Log("- Container returns the correct amount when removing compatible material: %v", test);
test = (container->GetLiquidFillLevel() == 50);
test = (container->GetLiquidAmount() == 50);
Log("- Container contents do change when removing compatible material: %v", test);
container->SetLiquidContainer(material_alternative, 100);
container->CreateContents(Liquid_Oil, 100);
returned = container->RemoveLiquid(material_alternative, nil, nil);
test = (returned[0] == material_alternative);
Log("- Container returns the contained material when extracting amount 'nil': %v", test);
test = returned[1] == 100; passed &= test;
Log("- Container returns the contained amount when extracting amount 'nil': %v", test);
test = (container->GetLiquidFillLevel() == 0);
test = (container->GetLiquidAmount() == 0);
Log("- Container is empty after removing amount 'nil': %v", test);
container->SetLiquidContainer(material_alternative, 100);
container->CreateContents(Liquid_Oil, 100);
returned = container->RemoveLiquid(nil, nil, nil);
test = (returned[0] == material_alternative);
Log("- Container returns the contained material when extracting material and amount 'nil': %v", test);
test = returned[1] == 100; passed &= test;
Log("- Container returns the contained amount when extracting material and amount 'nil': %v", test);
test = (container->GetLiquidFillLevel() == 0);
test = (container->GetLiquidAmount() == 0);
Log("- Container is empty after removing amount material and amount 'nil': %v", test);
container->RemoveObject();
@ -420,55 +364,6 @@ global func Test8_Execute()
}
global func Test9_OnStart(int plr){ return true;}
global func Test9_OnFinished(){ return; }
global func Test9_Execute()
{
Log("Test the behaviour of SetLiquidFillLevel and SetLiquidType in combination");
var container = CreateObject(Barrel);
var passed = true;
var liquid = "Water";
container->SetLiquidType(liquid);
var returned = container->GetLiquidType();
var test = (liquid == returned); passed &= test;
Log("- Container returns %s if liquid name is set to %s, values should be equal", returned, liquid);
var level = 0;
returned = container->GetLiquidFillLevel();
test = (level == returned); passed &= test;
Log("- Container returns %d, expected %d, values should be equal", returned, level);
// ----
Log("- Changing fill level now");
level = 100;
container->SetLiquidFillLevel(level);
returned = container->GetLiquidFillLevel();
test = (level == returned); passed &= test;
Log("- Container returns %d if liquid level is set to %d, values should be equal", returned, level);
returned = container->GetLiquidType();
test = (liquid == returned); passed &= test;
Log("- Container returns %s, expected %s, values should not change if level changes", returned, liquid);
// ----
Log("Changing liquid now");
liquid = "Oil";
container->SetLiquidType(liquid);
returned = container->GetLiquidType();
test = (liquid == returned); passed &= test;
Log("- Container returns %s if liquid name is set to %s, values should be equal", returned, liquid);
returned = container->GetLiquidFillLevel();
test = (level == returned); passed &= test;
Log("- Container returns %d, expected %d, values should not change if liquid changes", returned, level);
container->RemoveObject();
return passed;
}
@ -653,8 +548,8 @@ global func Test11_Execute()
var container2 = CreateObject(Barrel);
// can stack filled barrel with other filled barrel of the same liquid
container1->SetLiquidContainer("Water", 100);
container2->SetLiquidContainer("Water", 300);
container1->CreateContents(Liquid_Water, 100);
container2->CreateContents(Liquid_Water, 300);
var passed = true;
var returned = container1->CanBeStackedWith(container2);
@ -665,8 +560,8 @@ global func Test11_Execute()
Log("- Barrel can be stacked with other barrel that contains the same liquid: %v", test);
// cannot stack filled barrel with other empty barrel
container1->SetLiquidContainer("Water", 100);
container2->SetLiquidFillLevel(0);
container1->Contents()->SetStackCount(100);
container2->Contents()->RemoveObject();
returned = container1->CanBeStackedWith(container2);
test = returned == false; passed &= test;
@ -676,16 +571,15 @@ global func Test11_Execute()
Log("- Empty barrel cannot be stacked with filled barrel: %v", test);
// can stack empty barrel with other empty barrel
container1->SetLiquidFillLevel(0);
container2->SetLiquidFillLevel(0);
container1->Contents()->RemoveObject();
returned = container1->CanBeStackedWith(container2);
test = returned == true; passed &= test;
Log("- Empty barrel can be stacked with empty barrel: %v", test);
// cannot stack filled barrel with other filled barrel of different liquid
container1->SetLiquidContainer("Water", 100);
container2->SetLiquidContainer("Oil", 100);
container1->CreateContents(Liquid_Water, 100);
container2->CreateContents(Liquid_Oil, 100);
returned = container1->CanBeStackedWith(container2);
test = returned == false; passed &= test;
@ -698,107 +592,4 @@ global func Test11_Execute()
container2->RemoveObject();
return passed;
}
global func Test12_OnStart(int plr){ return true;}
global func Test12_OnFinished(){ return; }
global func Test12_Execute()
{
Log("Test the behaviour of liquid objects entering liquid containers");
var container = CreateObject(Barrel);
var liquid = CreateObject(Liquid_Water);
// can fill empty barrel with the liquid
liquid->SetLiquidAmount(100);
liquid->Enter(container);
var passed = true;
var returned = container->GetLiquidItem();
var test = (returned == liquid); passed &= test;
Log("- Liquid can fill empty barrel: %v", test);
returned = container->GetLiquidFillLevel();
test = (100 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 100, test);
// can fill barrel with more liquid, liquid object gets removed
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(100);
liquid->Enter(container);
test = (liquid == nil); passed &= test;
Log("- Liquid can enter filled barrel, liquid got removed: %v", test);
returned = container->GetLiquidFillLevel();
test = (200 == returned); passed &= test;
Log("- Barrel contains %d units, expected %d: %v", returned, 200, test);
// cannot fill in more than the allowed amount
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(200);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel if the capacity is exceeded: %v", test);
returned = container->GetLiquidFillLevel();
test = (300 == returned); passed &= test;
Log("- Barrel does increase fill level, up to the allowed amount, contains %d units, expected %d: %v", returned, 300, test);
returned = liquid->GetLiquidAmount();
test = (100 == returned); passed &= test;
Log("- Liquid object still contains %d units, expected %d: %v", returned, 100, test);
Log("- Resetting liquid amount to 0");
liquid->RemoveObject();
container->GetLiquidItem()->RemoveObject();
// cannot fill in empty barrel and empty liquid object partially
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(500);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter empty barrel if the capacity is exceeded: %v", test);
returned = container->GetLiquidFillLevel();
test = (300 == returned); passed &= test;
Log("- Barrel does increase fill level, up to the allowed amount, contains %d units, expected %d: %v", returned, 300, test);
returned = liquid->GetLiquidAmount();
test = (200 == returned); passed &= test;
Log("- Liquid object still contains %d units, expected %d: %v", returned, 200, test);
Log("- Resetting liquid amount to 200");
liquid->RemoveObject();
container->SetLiquidFillLevel(200);
// cannot fill in a different liquid
liquid = CreateObject(Liquid_Oil);
liquid->SetLiquidAmount(50);
liquid->Enter(container);
returned = liquid->Contained();
test = (returned == nil); passed &= test;
Log("- Liquid cannot enter filled barrel of a different liquid type: %v", test);
returned = container->GetLiquidFillLevel();
test = (200 == returned); passed &= test;
Log("- Barrel does not increase fill level, contains %d units, expected %d: %v", returned, 200, test);
liquid->RemoveObject();
// barrel gets emptied when liquid exits it
liquid = container->GetLiquidItem();
liquid->Exit();
returned = container->LiquidContainerIsEmpty();
test = returned; passed &= test;
Log("- Liquid container should be empty when liquid leaves it: %v", test);
returned = container->GetLiquidItem();
test = (returned == nil); passed &= test;
Log("- Liquid container should not have a liquid item when liquid leaves it: %v", test);
test = (liquid != nil); passed &= test;
Log("- Liquid exists after leaving the container: %v", test);
liquid->RemoveObject();
container->RemoveObject();
return passed;
}
}