2010-12-31 12:20:54 +00:00
|
|
|
/*--
|
|
|
|
Pump
|
2013-05-30 20:11:39 +00:00
|
|
|
Author: Maikel, ST-DDT, Sven2, Newton
|
|
|
|
|
|
|
|
Pumps liquids using drain and source pipes. Features include:
|
|
|
|
+ switch on and off
|
|
|
|
+ consume/produce a variable amount of power depending on the height of
|
|
|
|
source and drain
|
2010-12-31 12:20:54 +00:00
|
|
|
|
|
|
|
--*/
|
|
|
|
|
2012-05-10 19:20:06 +00:00
|
|
|
#include Library_Structure
|
2012-02-18 21:50:18 +00:00
|
|
|
#include Library_Ownable
|
2010-12-31 12:20:54 +00:00
|
|
|
#include Library_PowerConsumer
|
2013-04-28 11:57:25 +00:00
|
|
|
#include Library_PowerProducer
|
2010-12-31 12:20:54 +00:00
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
local Name = "$Name$";
|
|
|
|
local Description = "$Description$";
|
|
|
|
local BlastIncinerate = 50;
|
|
|
|
local HitPoints = 70;
|
|
|
|
|
|
|
|
/*
|
|
|
|
States
|
|
|
|
"Wait": turned off or source pipe not connected
|
|
|
|
"WaitForPower": turned on but no power (does consume power)
|
|
|
|
"WaitForLiquid": turned on but no liquid (does not consume power)
|
|
|
|
"Pump": currently working and consuming/producing power
|
|
|
|
|
|
|
|
*/
|
|
|
|
local ActMap = {
|
|
|
|
Pump = {
|
|
|
|
Prototype = Action,
|
|
|
|
Name = "Pump",
|
|
|
|
Length = 30,
|
|
|
|
Delay = 3,
|
2013-10-31 21:51:18 +00:00
|
|
|
Sound = "Pumpjack",
|
2013-05-30 20:11:39 +00:00
|
|
|
NextAction = "Pump",
|
|
|
|
StartCall = "CheckState",
|
|
|
|
PhaseCall = "Pumping"
|
|
|
|
},
|
|
|
|
Wait = {
|
|
|
|
Prototype = Action,
|
|
|
|
Name = "Wait",
|
|
|
|
Delay = 90,
|
|
|
|
NextAction = "Wait",
|
|
|
|
EndCall = "CheckState"
|
|
|
|
},
|
|
|
|
WaitForPower = {
|
|
|
|
Prototype = Action,
|
|
|
|
Name = "WaitForPower",
|
2013-06-04 18:10:51 +00:00
|
|
|
Delay = 30,
|
2013-05-30 20:11:39 +00:00
|
|
|
NextAction = "WaitForPower",
|
|
|
|
EndCall = "CheckState"
|
|
|
|
},
|
|
|
|
WaitForLiquid = {
|
|
|
|
Prototype = Action,
|
|
|
|
Name = "WaitForLiquid",
|
|
|
|
Delay = 30,
|
|
|
|
NextAction = "WaitForLiquid",
|
|
|
|
EndCall = "CheckState"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
local animation; // animation handle
|
|
|
|
|
2013-05-31 20:34:17 +00:00
|
|
|
local switched_on; // controlled by Interaction. Indicates whether the user wants to pump or not
|
2013-05-30 20:11:39 +00:00
|
|
|
|
2013-05-31 20:34:17 +00:00
|
|
|
local powered; // whether the pump has enough power as a consumer, always true if producing
|
|
|
|
local power_used; // the amount of power currently consumed or (if negative) produced
|
2013-05-30 20:11:39 +00:00
|
|
|
|
|
|
|
local stored_material_index; //contained liquid
|
|
|
|
local stored_material_amount;
|
|
|
|
|
|
|
|
local source_pipe;
|
|
|
|
local drain_pipe;
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** This object is a liquid pump, thus pipes can be connected. */
|
2010-12-31 12:20:54 +00:00
|
|
|
public func IsLiquidPump() { return true; }
|
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
func Definition(def)
|
2013-05-27 11:24:42 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
// for title image
|
|
|
|
SetProperty("PictureTransformation",Trans_Rotate(50,0,1,0),def);
|
|
|
|
// for building preview
|
|
|
|
SetProperty("MeshTransformation",Trans_Rotate(50,0,1,0),def);
|
2013-05-27 11:24:42 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
func Construction()
|
2012-04-16 15:31:38 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
// Rotate at a 45 degree angle towards viewer and add a litte bit of Random
|
|
|
|
this.MeshTransformation = Trans_Rotate(50 + RandomX(-10,10),0,1,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
func Initialize()
|
|
|
|
{
|
|
|
|
switched_on = true;
|
2013-05-27 11:24:42 +00:00
|
|
|
var start = 0;
|
|
|
|
var end = GetAnimationLength("pump");
|
2013-05-30 20:11:39 +00:00
|
|
|
animation = PlayAnimation("pump", 5, Anim_Linear(GetAnimationPosition(animation), start, end, 35, ANIM_Loop), Anim_Const(1000));
|
|
|
|
SetState("Wait");
|
2010-12-31 12:20:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 22:47:40 +00:00
|
|
|
|
2012-03-10 19:45:18 +00:00
|
|
|
/*-- Interaction --*/
|
|
|
|
|
2015-02-27 10:23:20 +00:00
|
|
|
public func GetInteractionMenus(object clonk)
|
2012-03-10 19:45:18 +00:00
|
|
|
{
|
2015-02-27 10:23:20 +00:00
|
|
|
var menus = _inherited() ?? [];
|
|
|
|
// only open the menus if ready
|
|
|
|
if (GetCon() >= 100)
|
2014-10-18 17:12:55 +00:00
|
|
|
{
|
2015-02-27 10:23:20 +00:00
|
|
|
var menu_entries = [];
|
|
|
|
var custom_entry =
|
|
|
|
{
|
|
|
|
Right = "12em", Bottom = "4em",
|
|
|
|
BackgroundColor = {Std = 0, OnHover = 0x50ff0000},
|
|
|
|
image = {Right = "4em"},
|
|
|
|
text = {Left = "4em"}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (!switched_on)
|
|
|
|
PushBack(menu_entries, {symbol = Icon_Play, extra_data = "on",
|
|
|
|
custom =
|
|
|
|
{
|
|
|
|
Prototype = custom_entry,
|
|
|
|
text = {Prototype = custom_entry.text, Text = "$MsgTurnOn$"},
|
|
|
|
image = {Prototype = custom_entry.image, Symbol = Icon_Play}
|
|
|
|
}});
|
2014-10-18 17:12:55 +00:00
|
|
|
else
|
2015-02-27 10:23:20 +00:00
|
|
|
PushBack(menu_entries, {symbol = Icon_Stop, extra_data = "off",
|
|
|
|
custom =
|
|
|
|
{
|
|
|
|
Prototype = custom_entry,
|
|
|
|
text = {Prototype = custom_entry.text, Text = "$MsgTurnOff$"},
|
|
|
|
image = {Prototype = custom_entry.image, Symbol = Icon_Stop}
|
|
|
|
}});
|
|
|
|
if (source_pipe)
|
|
|
|
PushBack(menu_entries, {symbol = Icon_Cancel, extra_data = "cutsource",
|
|
|
|
custom =
|
|
|
|
{
|
|
|
|
Prototype = custom_entry,
|
|
|
|
text = {Prototype = custom_entry.text, Text = "$MsgCutSource$"},
|
|
|
|
image = {Prototype = custom_entry.image, Symbol = Icon_Cancel}
|
|
|
|
}});
|
|
|
|
if (drain_pipe)
|
|
|
|
PushBack(menu_entries, {symbol = Icon_Cancel, extra_data = "cutdrain",
|
|
|
|
custom =
|
|
|
|
{
|
|
|
|
Prototype = custom_entry,
|
|
|
|
text = {Prototype = custom_entry.text, Text = "$MsgCutDrain$"},
|
|
|
|
image = {Prototype = custom_entry.image, Symbol = Icon_Cancel}
|
|
|
|
}});
|
|
|
|
|
|
|
|
|
|
|
|
var prod_menu =
|
|
|
|
{
|
|
|
|
title = "$Control$",
|
|
|
|
entries = menu_entries,
|
|
|
|
callback = "OnPumpControl",
|
|
|
|
callback_hover = "OnPumpControlHover",
|
|
|
|
callback_target = this,
|
|
|
|
BackgroundColor = RGB(0, 50, 50),
|
|
|
|
Priority = 20
|
|
|
|
};
|
|
|
|
PushBack(menus, prod_menu);
|
2014-10-18 17:12:55 +00:00
|
|
|
}
|
2015-02-27 10:23:20 +00:00
|
|
|
return menus;
|
|
|
|
}
|
|
|
|
|
|
|
|
public func OnPumpControlHover(id symbol, string action, desc_menu_target, menu_id)
|
|
|
|
{
|
|
|
|
var text = "";
|
|
|
|
if (action == "on") text = "$DescTurnOn$";
|
|
|
|
else if (action == "off") text = "$DescTurnOff$";
|
|
|
|
else if (action == "cutdrain") text = "$DescCutDrain$";
|
|
|
|
else if (action == "cutsource") text = "$DescCutSource$";
|
|
|
|
GuiUpdateText(text, menu_id, 1, desc_menu_target);
|
2014-10-18 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 10:23:20 +00:00
|
|
|
public func OnPumpControl(id symbol, string action, bool alt)
|
2014-10-18 17:12:55 +00:00
|
|
|
{
|
2015-02-27 10:23:20 +00:00
|
|
|
if (action == "on" || action == "off")
|
2014-10-18 17:12:55 +00:00
|
|
|
{
|
|
|
|
switched_on = !switched_on;
|
|
|
|
CheckState();
|
|
|
|
}
|
2015-02-27 10:23:20 +00:00
|
|
|
else if (action == "cutsource" && source_pipe)
|
2014-10-18 17:12:55 +00:00
|
|
|
source_pipe->RemoveObject();
|
2015-02-27 10:23:20 +00:00
|
|
|
else if (action == "cutdrain" && drain_pipe)
|
2014-10-18 17:12:55 +00:00
|
|
|
drain_pipe->RemoveObject();
|
2012-03-10 19:45:18 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 12:20:54 +00:00
|
|
|
/*-- Pipe connection --*/
|
|
|
|
|
|
|
|
public func GetSource() { return source_pipe; }
|
|
|
|
public func SetDrain(object pipe) { drain_pipe = pipe; }
|
|
|
|
public func GetDrain() { return drain_pipe; }
|
|
|
|
|
2012-10-25 18:14:13 +00:00
|
|
|
public func SetSource(object pipe)
|
|
|
|
{
|
|
|
|
source_pipe = pipe;
|
2013-05-25 15:58:19 +00:00
|
|
|
CheckState();
|
2012-10-25 18:14:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/*-- Power stuff --*/
|
|
|
|
|
2012-02-18 21:50:18 +00:00
|
|
|
func QueryWaivePowerRequest()
|
|
|
|
{
|
2012-10-25 18:14:13 +00:00
|
|
|
// has less priority than other objects, but not too low
|
|
|
|
return 10;
|
2012-02-18 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 20:47:08 +00:00
|
|
|
public func GetConsumerPriority() { return 25; }
|
|
|
|
|
|
|
|
public func GetProducerPriority() { return 100; }
|
|
|
|
|
2012-02-18 21:50:18 +00:00
|
|
|
func OnNotEnoughPower()
|
|
|
|
{
|
2013-06-04 18:10:51 +00:00
|
|
|
_inherited(...);
|
2013-05-30 20:11:39 +00:00
|
|
|
powered = false;
|
2013-06-04 18:10:51 +00:00
|
|
|
CheckState();
|
2012-02-18 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OnEnoughPower()
|
|
|
|
{
|
2013-06-04 18:10:51 +00:00
|
|
|
_inherited(...);
|
2013-05-30 20:11:39 +00:00
|
|
|
powered = true;
|
2013-06-04 18:10:51 +00:00
|
|
|
CheckState();
|
2012-02-18 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Returns object to which the liquid is pumped */
|
2013-04-28 11:57:25 +00:00
|
|
|
private func GetDrainObject()
|
|
|
|
{
|
|
|
|
if (drain_pipe) return drain_pipe->GetConnectedObject(this) ?? this;
|
|
|
|
return this;
|
|
|
|
}
|
2011-10-11 22:32:46 +00:00
|
|
|
|
2014-10-18 17:12:55 +00:00
|
|
|
/** Returns object from which the liquid is pumped */
|
2013-05-25 15:58:19 +00:00
|
|
|
private func GetSourceObject()
|
|
|
|
{
|
|
|
|
if (source_pipe) return source_pipe->GetConnectedObject(this) ?? this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
/** Returns amount of pixels to pump per 30 frames */
|
|
|
|
public func GetPumpSpeed()
|
|
|
|
{
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** PhaseCall of Pump: Pump the liquid from the source to the drain pipe */
|
2010-12-31 12:20:54 +00:00
|
|
|
protected func Pumping()
|
|
|
|
{
|
2012-10-25 18:14:13 +00:00
|
|
|
// at this point we can assert that we have power
|
|
|
|
|
|
|
|
// something went wrong in the meantime?
|
2013-05-30 20:11:39 +00:00
|
|
|
// let the central function handle that on next check
|
|
|
|
if (!source_pipe) return;
|
2013-04-28 11:57:25 +00:00
|
|
|
|
|
|
|
var pump_ok = true;
|
2012-10-25 18:14:13 +00:00
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
// is empty? -> try to get liquid
|
|
|
|
if (!stored_material_amount)
|
2011-10-11 22:32:46 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
|
2012-10-25 18:14:13 +00:00
|
|
|
// get new materials
|
2013-05-30 20:11:39 +00:00
|
|
|
var aMat = GetSourceObject()->ExtractLiquidAmount(0,0, GetPumpSpeed()/10);
|
|
|
|
|
2012-10-25 18:14:13 +00:00
|
|
|
// no material to pump?
|
2013-05-30 20:11:39 +00:00
|
|
|
if (aMat)
|
|
|
|
{
|
|
|
|
stored_material_index = aMat[0];
|
|
|
|
stored_material_amount = aMat[1];
|
|
|
|
}
|
2013-04-28 12:02:57 +00:00
|
|
|
else
|
2013-05-30 20:11:39 +00:00
|
|
|
{
|
|
|
|
pump_ok = false;
|
|
|
|
}
|
2011-10-11 22:32:46 +00:00
|
|
|
}
|
2013-04-28 11:57:25 +00:00
|
|
|
if (pump_ok)
|
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
var i = stored_material_amount;
|
|
|
|
while (i > 0)
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
if (GetDrainObject()->InsertMaterial(stored_material_index))
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
// Drain is stuck.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pump_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
|
|
|
|
stored_material_amount = i;
|
|
|
|
if (stored_material_amount <= 0)
|
|
|
|
stored_material_index = nil;
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
|
|
|
|
if(!pump_ok)
|
2011-10-11 22:32:46 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
SetState("WaitForLiquid");
|
2011-10-11 22:32:46 +00:00
|
|
|
}
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
/** Re check state and change the state if needed */
|
2013-05-25 15:58:19 +00:00
|
|
|
func CheckState()
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
var is_fullcon = GetCon() >= 100;
|
|
|
|
var can_pump = source_pipe && is_fullcon && switched_on;
|
|
|
|
|
|
|
|
// can't pump at all -> wait
|
|
|
|
if (!can_pump)
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
SetState("Wait");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// can pump but has no liquid -> wait for liquid
|
|
|
|
if (!HasLiquidToPump())
|
2013-05-25 15:58:19 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
SetState("WaitForLiquid");
|
2013-05-25 15:58:19 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
else
|
2013-05-25 15:58:19 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
// can pump, has liquid but has no power -> wait for power
|
|
|
|
if (!powered)
|
2013-05-25 15:58:19 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
SetState("WaitForPower");
|
2013-05-25 15:58:19 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
// otherwise, pump! :-)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetState("Pump");
|
|
|
|
}
|
|
|
|
|
|
|
|
// regularly update the power usage while pumping or waiting for power
|
|
|
|
UpdatePowerUsage();
|
2013-05-25 15:58:19 +00:00
|
|
|
}
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Get current height the pump has to push liquids upwards (input.y - output.y) */
|
|
|
|
private func GetPumpHeight()
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
// compare each the surfaces of the bodies of liquid pumped
|
2013-05-25 15:58:19 +00:00
|
|
|
|
|
|
|
// find Y position of surface of liquid that is pumped to target
|
2013-05-30 20:11:39 +00:00
|
|
|
var source_y = 0;
|
|
|
|
if (GetSourceObject()->GBackLiquid())
|
2013-05-25 15:58:19 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
var src_mat = GetSourceObject()->GetMaterial();
|
|
|
|
while (src_mat == GetSourceObject()->GetMaterial(0, source_y-1))
|
|
|
|
--source_y;
|
2013-05-25 15:58:19 +00:00
|
|
|
}
|
2013-06-04 18:09:09 +00:00
|
|
|
// same for target (use same function as if inserting)
|
|
|
|
var target_pos = {X=0, Y=0};
|
|
|
|
GetDrainObject()->CanInsertMaterial(Material("Water"),0,0,target_pos);
|
|
|
|
return (GetSourceObject()->GetY() + source_y) - target_pos.Y;
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Recheck power usage/production for current pump height
|
|
|
|
and make the pump a producer / consumer for the power system */
|
|
|
|
private func UpdatePowerUsage()
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-25 15:58:19 +00:00
|
|
|
var new_power;
|
|
|
|
if(IsUsingPower())
|
|
|
|
new_power = PumpHeight2Power(GetPumpHeight());
|
|
|
|
else
|
|
|
|
new_power = 0;
|
|
|
|
|
2013-05-31 20:34:17 +00:00
|
|
|
// do nothing if not necessary
|
|
|
|
if (new_power == power_used)
|
|
|
|
return;
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
// and update energy system
|
2013-04-28 11:57:25 +00:00
|
|
|
if (new_power > 0)
|
|
|
|
{
|
2013-05-31 20:34:17 +00:00
|
|
|
if (power_used < 0)
|
|
|
|
{
|
|
|
|
powered = false; // needed since the flag was set manually
|
|
|
|
UnmakePowerProducer();
|
|
|
|
}
|
2013-04-28 11:57:25 +00:00
|
|
|
MakePowerConsumer(new_power);
|
2013-05-31 20:34:17 +00:00
|
|
|
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
2013-05-31 20:34:17 +00:00
|
|
|
else if (new_power < 0)
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-31 20:34:17 +00:00
|
|
|
if (power_used > 0)
|
|
|
|
UnmakePowerConsumer();
|
2013-05-25 15:58:19 +00:00
|
|
|
MakePowerProducer(-new_power);
|
2013-05-31 20:34:17 +00:00
|
|
|
powered = true; // when producing, we always have power
|
2013-05-25 15:58:19 +00:00
|
|
|
}
|
2013-05-31 20:34:17 +00:00
|
|
|
else // new_power == 0
|
|
|
|
{
|
|
|
|
if (power_used < 0) UnmakePowerProducer();
|
|
|
|
else if (power_used > 0) UnmakePowerConsumer();
|
2013-06-04 18:10:51 +00:00
|
|
|
powered = true;
|
2013-05-31 20:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
power_used = new_power;
|
2013-04-28 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Return whether the pump should be using power in the current state */
|
|
|
|
private func IsUsingPower()
|
2013-04-28 11:57:25 +00:00
|
|
|
{
|
2013-05-31 20:34:17 +00:00
|
|
|
return switched_on && (GetAction() == "Pump" || GetAction() == "WaitForPower");
|
2010-12-31 12:20:54 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Transform pump height (input.y - output.y) to required power */
|
|
|
|
private func PumpHeight2Power(int pump_height)
|
2012-10-25 18:14:13 +00:00
|
|
|
{
|
2013-05-25 15:58:19 +00:00
|
|
|
// pumping downwards will only produce energy after an offset
|
2013-06-04 18:10:51 +00:00
|
|
|
var power_offset = 40;
|
2013-05-25 15:58:19 +00:00
|
|
|
// max power consumed/produced
|
|
|
|
var max_power = 150;
|
|
|
|
|
2013-06-04 18:10:51 +00:00
|
|
|
return BoundBy((pump_height + power_offset)/15*5, -max_power,max_power+power_offset);
|
2012-10-25 18:14:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:58:19 +00:00
|
|
|
/** Returns whether there is liquid at the source pipe to pump */
|
2012-10-25 18:14:13 +00:00
|
|
|
private func HasLiquidToPump()
|
2010-12-31 12:20:54 +00:00
|
|
|
{
|
|
|
|
if (!source_pipe)
|
|
|
|
return false;
|
2013-05-25 15:58:19 +00:00
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
// source
|
|
|
|
if(!GetSourceObject()->GBackLiquid())
|
|
|
|
return false;
|
|
|
|
|
2013-06-04 18:10:51 +00:00
|
|
|
// target (test with the very popular liquid "water")
|
2013-05-30 20:11:39 +00:00
|
|
|
if(!GetDrainObject()->CanInsertMaterial(Material("Water"),0,0))
|
2010-12-31 12:20:54 +00:00
|
|
|
return false;
|
2013-05-30 20:11:39 +00:00
|
|
|
|
2010-12-31 12:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
/** Set the state of the pump, retaining the animation position and updating the power usage */
|
|
|
|
func SetState(string act)
|
2012-03-10 17:23:48 +00:00
|
|
|
{
|
2013-05-30 20:11:39 +00:00
|
|
|
if(act == GetAction()) return;
|
2012-03-10 17:23:48 +00:00
|
|
|
|
2013-05-30 20:11:39 +00:00
|
|
|
var start = 0;
|
|
|
|
var end = GetAnimationLength("pump");
|
2013-06-04 18:10:51 +00:00
|
|
|
var anim_pos = GetAnimationPosition(animation);
|
2013-05-30 20:11:39 +00:00
|
|
|
if (act == "Pump")
|
|
|
|
{
|
2013-06-04 18:10:51 +00:00
|
|
|
SetAnimationPosition(animation, Anim_Linear(anim_pos, start, end, 35, ANIM_Loop));
|
2011-10-11 22:32:46 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
else if(act == "WaitForLiquid")
|
2013-05-27 11:24:42 +00:00
|
|
|
{
|
2013-06-04 18:10:51 +00:00
|
|
|
SetAnimationPosition(animation, Anim_Linear(anim_pos, start, end, 350, ANIM_Loop));
|
2013-05-30 20:11:39 +00:00
|
|
|
}
|
|
|
|
else
|
2013-05-27 11:24:42 +00:00
|
|
|
{
|
2013-06-04 18:10:51 +00:00
|
|
|
SetAnimationPosition(animation, Anim_Const(anim_pos));
|
2013-05-27 11:24:42 +00:00
|
|
|
}
|
2013-05-30 20:11:39 +00:00
|
|
|
|
2013-05-31 20:34:17 +00:00
|
|
|
// deactivate power usage when not pumping
|
|
|
|
if (powered && (act == "Wait" || act == "WaitForLiquid"))
|
2013-05-27 11:24:42 +00:00
|
|
|
{
|
2013-05-31 20:34:17 +00:00
|
|
|
if (power_used < 0) UnmakePowerProducer();
|
|
|
|
else if(power_used > 0) UnmakePowerConsumer();
|
|
|
|
power_used = 0;
|
|
|
|
powered = false;
|
2013-05-30 20:11:39 +00:00
|
|
|
}
|
|
|
|
// finally, set the action
|
|
|
|
SetAction(act);
|
|
|
|
}
|