From b830d1528058539ed309b72b26dbb1d5bb2880dc Mon Sep 17 00:00:00 2001 From: David Dormagen Date: Thu, 25 Oct 2012 20:14:13 +0200 Subject: [PATCH] fixed the pump's irritating power consumption --- .../Structures.ocd/Pump.ocd/Script.c | 151 ++++++++++++------ 1 file changed, 98 insertions(+), 53 deletions(-) diff --git a/planet/Objects.ocd/Structures.ocd/Pump.ocd/Script.c b/planet/Objects.ocd/Structures.ocd/Pump.ocd/Script.c index 4383803ec..93e9cdded 100644 --- a/planet/Objects.ocd/Structures.ocd/Pump.ocd/Script.c +++ b/planet/Objects.ocd/Structures.ocd/Pump.ocd/Script.c @@ -9,6 +9,7 @@ #include Library_Ownable #include Library_PowerConsumer +func NeededPower() { return 100; } // This object is a liquid pump, thus pipes can be connected. public func IsLiquidPump() { return true; } @@ -20,9 +21,9 @@ public func Construction(object creator) protected func Initialize() { - MakePowerConsumer(100); - SetAction("Wait"); turned_on = true; + SetAction("Wait"); + CheckCurrentState(); return; } @@ -42,16 +43,8 @@ public func GetInteractionMetaInfo(object clonk) // On interaction the pump can be turned on or off. public func Interact(object clonk) { - if (turned_on) - { - turned_on = false; - SetAction("Wait"); - } - else - { - OnWaitStart(); - turned_on = true; - } + turned_on = !turned_on; + CheckCurrentState(); return true; } @@ -61,65 +54,57 @@ local source_pipe; local drain_pipe; // Set-Getters for source and drain pipe. -public func SetSource(object pipe) { source_pipe = pipe; } public func GetSource() { return source_pipe; } public func SetDrain(object pipe) { drain_pipe = pipe; } public func GetDrain() { return drain_pipe; } +public func SetSource(object pipe) +{ + source_pipe = pipe; + CheckCurrentState(); +} + func QueryWaivePowerRequest() { - // don't need power if not pumping anyway - if(GetAction() == "Wait") - return 50; - return 0; + // has less priority than other objects, but not too low + return 10; } func OnNotEnoughPower() { - if(GetAction() == "Pump") - SetAction("Wait"); + // assert: current action is "Pump" + SetAction("WillPump"); return _inherited(...); } func OnEnoughPower() { - OnWaitStart(); + // assert: current action is either WillPump or Wait + SetAction("Pump"); return _inherited(...); } -protected func OnPumpStart() -{ - if (!ReadyToPump()) - SetAction("Wait"); - return; -} - -protected func OnWaitStart() -{ - if (GetCon() < 100) return; - - if (ReadyToPump()) - SetAction("Pump"); - return; -} - local aMaterials=["", 0]; //contained liquids local pumpable_materials; // materials that can be pumped protected func Pumping() { - // Only if turned on. - if (!turned_on) - return SetAction("Wait"); - // Pump liquids. + // at this point we can assert that we have power + + // something went wrong in the meantime? + // let the central function handle that if (!source_pipe) - return SetAction("Wait"); - //IsEmpty? + return CheckCurrentState(); + + // not able to pump right now? + if(!HasLiquidToPump()) return; + + // is empty? if ((aMaterials[1] == 0) || (aMaterials[0] == "")) { - //Get new Materials + // get new materials aMaterials = source_pipe->GetLiquid(pumpable_materials, 5, this, true); - //No Material to pump? + // no material to pump? if ((aMaterials[0] == "") || (aMaterials[1] == 0)) return; } @@ -132,16 +117,56 @@ protected func Pumping() InsertMaterial(itMaterial); aMaterials = ["", 0]; } - //maybe add the possebility to empty pump (invaild mats?) + // maybe add the possebility to empty pump (invaild mats?) return; } -// Returns whether the pump can pump some liquid. -private func ReadyToPump() +func CheckCurrentState() +{ + if(turned_on) + { + var has_source_pipe = !!source_pipe; + var is_fullcon = GetCon() >= 100; + + if(GetAction() == "Wait") // waiting: not consuming power + { + if(has_source_pipe && is_fullcon) + { + SetAction("WillPump"); + MakePowerConsumer(NeededPower()); + return; + } + else return; // waiting and no source pipe, keep waiting + } + else // not waiting: consuming power + { + if(!has_source_pipe || !is_fullcon) + { + UnmakePowerConsumer(); + SetAction("Wait"); + return; + } + return; + } + // this point should not be reached + } + else // turned off + { + if(GetAction() != "Wait") // consuming power + { + UnmakePowerConsumer(); + SetAction("Wait"); + return; + } + else // already waiting + return; + } + FatalError("Not every case handled in Pump::CheckCurrentState"); +} + +// Returns whether the pump can pump some liquid. +private func HasLiquidToPump() { - // no power? - if(!CurrentlyHasPower()) - return false; // If there is no source pipe, return false. if (!source_pipe) return false; @@ -170,6 +195,12 @@ local Name = "$Name$"; local Description = "$Description$"; local BlastIncinerate = 50; local HitPoints = 70; + +/* + "Pump": the pump is currently working and consuming power + "WillPump": the pump wants to work as soon as there is power + "Wait": the pump has been turned off or some other need is not fulfilled (f.e. connected pipe) +*/ local ActMap = { Pump = { Prototype = Action, @@ -184,7 +215,7 @@ local ActMap = { Wdt = 28, Hgt = 32, NextAction = "Pump", - StartCall = "OnPumpStart", + StartCall = "CheckCurrentState", PhaseCall = "Pumping" }, Wait = { @@ -192,7 +223,7 @@ local ActMap = { Name = "Wait", Procedure = DFA_NONE, Length = 1, - Delay = 10, + Delay = 60, Directions = 2, FlipDir = 1, X = 0, @@ -200,6 +231,20 @@ local ActMap = { Wdt = 28, Hgt = 32, NextAction = "Wait", - StartCall = "OnWaitStart" + StartCall = "CheckCurrentState" + }, + WillPump = { + Prototype = Action, + Name = "WillPump", + Procedure = DFA_NONE, + Length = 1, + Delay = 60, + Directions = 2, + FlipDir = 1, + X = 0, + Y = 0, + Wdt = 28, + Hgt = 32, + NextAction = "WillPump", } };