Liquid object: Refactored object entering system.

Unit test 3 still fails, because the functions are not runtime overloadable. Test 12 fails, because liquid transfer does not work correctly yet. This fill be fixed in the next checkin.

Added debug logging, this has to be removed again later.
liquid_container
Mark 2016-02-26 15:19:41 +01:00
parent af6a31c489
commit 79df36eb6b
4 changed files with 83 additions and 33 deletions

View File

@ -48,14 +48,7 @@ func CannotEnter(object into)
// Enters liquid containers only, will be removed anyway if the liquid object is "empty"
if (into->~IsLiquidContainer() || into->~IsLiquidPump())
{
if (!GetLiquidAmount()) // the object is "empty", because it was just created
{
return false;
}
var exists = this;
var transferred = into->PutLiquid(IsLiquid(), GetLiquidAmount(), this);
if (exists) DoLiquidAmount(-transferred);
return !(into->TransferLiquidItem(this));
}
return true; // Cannot enter
@ -237,7 +230,7 @@ func PutLiquid(string liquid_name, int amount, object source)
if (Contained() && Contained()->~IsLiquidContainer())
{
return Contained()->PutLiquid(liquid_name, amount, source);
amount = BoundBy(Contained()->~GetLiquidContainerMaxFillLevel() - GetLiquidAmount(), 0, amount);
}
if (IsLiquid() == liquid_name)

View File

@ -61,10 +61,43 @@ func GetLiquidItem()
return liquid_container_item;
}
func TransferLiquidItem(object source)
{
if (source) Log(" Transfer Liquid item %v? already has item: %v, is liquid %v", source, GetLiquidItem(), source->~IsLiquid());
if (!GetLiquidItem() && source && source.IsLiquid != nil)
{
var liquid = source->IsLiquid();
if (liquid && !LiquidContainerAccepts(liquid)) return false;
var remaining = GetLiquidFillLevelRemaining();
Log(" Transfer? %d %d", source->GetLiquidAmount(), remaining);
if (source->GetLiquidAmount() <= remaining)
{
Log(" Transferred complete item");
SetLiquidItem(source);
return true;
}
else
{
Log(" Will create new item and transfer partial");
SetLiquidType(nil);
var extracted = source->RemoveLiquid(nil, remaining, this);
PutLiquid(extracted[0], extracted[1]);
return false;
}
}
return false;
}
func SetLiquidItem(object item)
{
if (item && (item->~IsLiquid() || item.IsLiquid != nil))
{
Log(" Set Liquid item %v", item);
liquid_container_item = item;
}
else
@ -97,6 +130,11 @@ func GetLiquidFillLevel()
return 0;
}
func GetLiquidFillLevelRemaining()
{
return GetLiquidContainerMaxFillLevel() - GetLiquidFillLevel();
}
// -------------- Setters
//
// Setters for stored liquid and amount
@ -117,10 +155,14 @@ func SetLiquidType(string liquid_name)
if (!GetLiquidItem())
{
var item = Library_Liquid->CreateLiquid(liquid_name, amount);
SetLiquidItem(item);
Log(" Created liquid item %v", item);
if (amount > 0) item->UpdateLiquidObject();
// if not removed because of amount
if (item) item->Enter(this);
if (item)
{
Log(" Liquid item %v shall enter container %v", item, this);
item->Enter(this);
}
}
}
@ -131,6 +173,7 @@ func SetLiquidFillLevel(int amount)
SetLiquidType(nil);
}
Log(" Set fill level: Change by %d", amount);
ChangeLiquidFillLevel(amount - GetLiquidFillLevel());
}
@ -193,14 +236,18 @@ func PutLiquid(string liquid_name, int amount, object source)
FatalError(Format("You can insert positive amounts of liquid only, got %d", amount));
}
if (LiquidContainerAccepts(liquid_name))
TransferLiquidItem(source);
if (!GetLiquidItem())
{
Log(" Does not have liquid item yet");
SetLiquidType(liquid_name);
amount = BoundBy(GetLiquidContainerMaxFillLevel() - GetLiquidFillLevel(), 0, amount);
ChangeLiquidFillLevel(+amount);
return amount;
}
else //Wrong material?
if (GetLiquidItem())
{
return GetLiquidItem()->PutLiquid(liquid_name, amount, source);
}
else //does not have a liquid item yet?
{
return 0;
}
@ -232,4 +279,4 @@ func Ejection(object item)
if (item == GetLiquidItem())
ResetLiquidItem();
_inherited(...);
}
}

View File

@ -182,6 +182,8 @@ global func Test3_Execute()
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)
{
@ -197,6 +199,8 @@ global func Test3_Execute()
global func Test4_OnStart(int plr){ return true;}
global func Test4_OnFinished(){ return; }
global func Test4_Execute()
@ -296,16 +300,13 @@ global func Test6_Execute()
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("Lava");
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("-- Debug: %v", container->IsLiquidContainerForMaterial("Lava"));
// Log("-- Debug: %v", container->LiquidContainerIsEmpty());
// Log("-- Debug: %v, %s", container->GetLiquidName() == "Lava", container->GetLiquidName());
Log("- Container returns 'true' if liquid fill level is 50% and material is ok: %v", test);
container->RemoveObject();
@ -388,30 +389,31 @@ global func Test8_Execute()
Log("- Container contents do change when removing compatible material: %v", test);
// request everything
container->SetLiquidContainer("Lava", 100);
var material_alternative = "Oil";
container->SetLiquidContainer(material_alternative, 100);
returned = container->RemoveLiquid(nil, 50, nil);
test = (returned[0] == "Lava");
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);
Log("- Container contents do change when removing compatible material: %v", test);
container->SetLiquidContainer("Lava", 100);
container->SetLiquidContainer(material_alternative, 100);
returned = container->RemoveLiquid("Lava", nil, nil);
test = (returned[0] == "Lava");
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);
Log("- Container is empty after removing amount 'nil': %v", test);
container->SetLiquidContainer("Lava", 100);
container->SetLiquidContainer(material_alternative, 100);
returned = container->RemoveLiquid(nil, nil, nil);
test = (returned[0] == "Lava");
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);
@ -717,8 +719,8 @@ global func Test12_Execute()
liquid->Enter(container);
var passed = true;
var returned = liquid;
var test = (returned == nil); passed &= test;
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;
@ -728,9 +730,8 @@ global func Test12_Execute()
liquid = CreateObject(Liquid_Water);
liquid->SetLiquidAmount(100);
liquid->Enter(container);
returned = liquid;
test = (returned == nil); passed &= test;
test = (liquid == nil); passed &= test;
Log("- Liquid can enter filled barrel, liquid got removed: %v", test);
returned = container->GetLiquidFillLevel();
test = (200 == returned); passed &= test;

View File

@ -0,0 +1,9 @@
#appendto Barrel
func Test3_IsLiquidContainerForMaterial(string liquid)
{
return liquid == "Water"
|| liquid == "Lava"
|| liquid == "123"
|| liquid == "#24942fwijvri";
}