Added anti-clogging mechanism to pump.

Now pipes cycle through several offsets before giving up on the pumping.
shapetextures
Sven Eberhardt 2015-09-05 13:10:19 -04:00
parent 0e6c7ada15
commit dd690e62b9
2 changed files with 38 additions and 3 deletions

View File

@ -8,7 +8,6 @@ local Description = "$Description$";
local UsageHelp = "$UsageHelp$";
local Collectible = 1;
local Rebuy = true;
local ApertureOffsetY = 3; // pump from bottom vertex
protected func Hit()
{
@ -62,5 +61,19 @@ protected func ControlUse(object clonk, int x, int y)
return true;
}
/* Cycling through several aperture offset indices to prevent easy clogging */
// default: pump from bottom vertex
local ApertureOffsetX = 0;
local ApertureOffsetY = 3;
public func CycleApertureOffset()
{
// Cycle in three steps of three px each through X and Y
// covering a 3x3 grid on points -3,0,+3
ApertureOffsetX = (ApertureOffsetX + 6) % 9 - 3;
if (!ApertureOffsetX) ApertureOffsetY = (ApertureOffsetY + 6) % 9 - 3;
return true;
}

View File

@ -26,6 +26,9 @@ local stored_material_amount;
local source_pipe;
local drain_pipe;
local clog_count; // increased when the pump doesn't find liquid or can't insert it. When it reaches max_clog_count, it will put the pump into temporary idle mode.
local max_clog_count = 5; // note that even when max_clog_count is reached, the pump will search through offsets (but in idle mode)
/** This object is a liquid pump, thus pipes can be connected. */
public func IsLiquidPump() { return true; }
@ -220,6 +223,7 @@ protected func Pumping()
}
else
{
source_obj->~CycleApertureOffset(this); // try different offsets, so we don't stop pumping just because 1px of earth was dropped on the source pipe
pump_ok = false;
}
}
@ -236,6 +240,7 @@ protected func Pumping()
// Drain is stuck.
else
{
drain_obj->~CycleApertureOffset(this); // try different offsets, so we don't stop pumping just because 1px of earth was dropped on the drain pipe
pump_ok = false;
break;
}
@ -246,8 +251,18 @@ protected func Pumping()
stored_material_index = nil;
}
if (!pump_ok)
SetState("WaitForLiquid");
if (pump_ok)
{
clog_count = 0;
}
else
{
// Put into wait state if no liquid could be pumped for a while
if (++clog_count >= max_clog_count)
{
SetState("WaitForLiquid");
}
}
return;
}
@ -279,6 +294,7 @@ func CheckState()
// otherwise, pump! :-)
else
{
clog_count = 0;
SetState("Pump");
}
@ -390,12 +406,18 @@ private func HasLiquidToPump()
// source
var source_obj = GetSourceObject();
if(!source_obj->GBackLiquid(source_obj.ApertureOffsetX, source_obj.ApertureOffsetY))
{
source_obj->~CycleApertureOffset(this); // try different offsets, so we can resume pumping after clog because 1px of earth was dropped on the source pipe
return false;
}
// target (test with the very popular liquid "water")
var drain_obj = GetDrainObject();
if(!drain_obj->CanInsertMaterial(Material("Water"),drain_obj.ApertureOffsetX, drain_obj.ApertureOffsetY))
{
drain_obj->~CycleApertureOffset(this); // try different offsets, so we can resume pumping after clog because 1px of earth was dropped on the source pipe
return false;
}
return true;
}