Refactoring: Pimp, Pipe, SteamEngine

Now it is possible to connect lines from a pump to the steam engine. This is achieved by the steam engine being a liquid tank. It did not seem good to allow connection from pumps to all liquid containers (i.e. barrels). A liquid tank is also a liquid container, they share the same interface, but it is also a structure.

It is not yet possible to fill the steam engine with any liquid though, because it is not defined what kind of liquid it accepts. This will be oil in the future. Furthermore, the behaviour when the pump adds incompatible liquid is not defined yet.
liquid_container
Mark 2016-02-04 18:25:19 +01:00
parent 65bc68af3e
commit 016922fa2c
7 changed files with 121 additions and 29 deletions

View File

@ -1,10 +1,12 @@
/*-- Pipe line
Author: ST-DDT
Author: ST-DDT, Marky
--*/
local Name = "$Name$";
local pipe_kit;
local ActMap = {
Connect = {
Prototype = Action,
@ -41,12 +43,14 @@ public func IsPipeLine()
return GetAction() == "Connect";
}
/** Returns whether this pipe is connected to an object. */
public func IsConnectedTo(object obj)
/** Returns whether this pipe is connected to an object.
Returns only actually connected objects if the parameter 'strict' is true */
public func IsConnectedTo(object obj, bool strict)
{
return GetActionTarget(0) == obj || GetActionTarget(1) == obj;
return GetActionTarget(0) == obj || GetActionTarget(1) == obj || (!strict && pipe_kit == obj);
}
/** Returns the object which is connected to obj through this pipe. */
public func GetConnectedObject(object obj)
{
@ -57,37 +61,62 @@ public func GetConnectedObject(object obj)
return;
}
/** Switches connection from one object to another. */
public func SwitchConnection(object connected_to, object obj)
{
var target0 = GetActionTarget(0), target1 = GetActionTarget(1);
if (target0 == connected_to) target0 = obj;
if (target1 == connected_to) target1 = obj;
SetActionTargets(target0, target1);
}
/** Saves the pipe object that created this line. */
public func SetPipeKit(object obj)
{
pipe_kit = obj;
obj->Enter(this);
}
public func GetPipeKit()
{
if (pipe_kit)
{
return pipe_kit;
}
else
{
if (GetActionTarget(0)->GetID() == Pipe) return GetActionTarget(0);
if (GetActionTarget(1)->GetID() == Pipe) return GetActionTarget(1);
FatalError("Unexpected error: This pipe has lost its pipe kit!");
}
}
private func LineBreak(bool no_msg)
{
Sound("Objects::LineSnap");
if (!no_msg)
BreakMessage();
var line_end = GetActionTarget(0);
if (!line_end ||line_end->GetID() != Pipe)
line_end = GetActionTarget(1);
if (line_end)
line_end->~ResetPicture();
var line_end = GetPipeKit();
if (line_end) line_end->~ResetPicture();
return;
}
private func Destruction()
{
var line_end = GetActionTarget(0);
if (!line_end || line_end->GetID() != Pipe)
line_end = GetActionTarget(1);
if (line_end)
line_end->~ResetPicture();
var line_end = GetPipeKit();
if (line_end) line_end->~ResetPicture();
return;
}
private func BreakMessage()
{
var line_end = GetActionTarget(0);
if (!line_end || line_end->GetID() != Pipe)
line_end = GetActionTarget(1);
if (line_end)
line_end->Message("$TxtPipeBroke$");
var line_end = GetPipeKit();
if (line_end) line_end->Message("$TxtPipeBroke$");
return;
}

View File

@ -1,8 +1,11 @@
/*-- Pipe
Author: ST-DDT
Author: ST-DDT, Marky
--*/
static const PIPE_STATE_Source = "Source";
static const PIPE_STATE_Drain = "Drain";
local Name = "$Name$";
local Description = "$Description$";
local Collectible = 1;
@ -21,11 +24,17 @@ public func IsToolProduct() { return true; }
protected func ControlUse(object clonk, int x, int y)
{
// Is this already connected to a liquid pump?
if (FindObject(Find_Func("IsConnectedTo",this)))
return false;
var pipe = FindObject(Find_Func("IsConnectedTo", this));
if (pipe) return ConnectPipeToLiquidTank(clonk, pipe);
// Is there an object which accepts power lines?
return ConnectPipeToPump(clonk);
}
func ConnectPipeToPump(object clonk)
{
// Is there an object which accepts pipes?
var liquid_pump = FindObject(Find_AtPoint(), Find_Func("IsLiquidPump"));
// No liquid pump, display message.
if (!liquid_pump)
{
@ -39,7 +48,7 @@ protected func ControlUse(object clonk, int x, int y)
clonk->Message("$MsgHasPipes$");
return true;
}
// Create and connect pipe.
var pipe = CreateObjectAbove(PipeLine, 0, 0, NO_OWNER);
pipe->SetActionTargets(this, liquid_pump);
@ -54,7 +63,7 @@ protected func ControlUse(object clonk, int x, int y)
Description = "$DescriptionSource$";
Name = "$NameSource$";
pipe->SetSource();
PipeState = "Source";
PipeState = PIPE_STATE_Source;
}
// Otherwise if liquid pump has no drain, create one.
else
@ -65,11 +74,41 @@ protected func ControlUse(object clonk, int x, int y)
Description = "$DescriptionDrain$";
Name = "$NameDrain$";
pipe->SetDrain();
PipeState = "Drain";
PipeState = PIPE_STATE_Drain;
}
return true;
}
func ConnectPipeToLiquidTank(object clonk, object pipe)
{
// Is there an object that accepts pipes?
var tank = FindObject(Find_AtPoint(), Find_Func("IsLiquidTank"));
if (!tank)
{
clonk->Message("$MsgNoNewPipeToTank$");
return true;
}
if (PipeState == PIPE_STATE_Source && tank->QueryConnectSourcePipe(pipe))
{
clonk->Message("$MsgHasSourcePipe$");
return true;
}
else if (PipeState == PIPE_STATE_Drain && tank->QueryConnectDrainPipe(pipe))
{
clonk->Message("$MsgHasDrainPipe$");
return true;
}
pipe->SwitchConnection(this, tank);
pipe->SetPipeKit(this);
Sound("Objects::Connect");
clonk->Message("$MsgConnectedToTank$", Name, tank->GetName());
return true;
}
// Line broke or something
public func ResetPicture()
{

View File

@ -6,6 +6,11 @@ DescriptionSource=Zuflussrohr von dem aus Flüssigkeit in die Pumpe gepumpt wird
DescriptionDrain=Abflusssrohr aus dem Flüssigkeit aus der Pumpe heraus läuft.
MsgNoNewPipe=Hier ist keine Pumpe.
MsgNoNewPipeToTank=Anschluss hier nicht möglich.
MsgCreatedSource=Zuflussrohr angeschlossen.
MsgCreatedDrain=Abflussrohr angeschlossen.
MsgHasPipes=Die Pumpe hat schon ein Zu- und Abflussrohr.
MsgHasPipes=Die Pumpe hat schon ein Zu- und Abflussrohr.
MsgHasSourcePipe=Zuflussrohr kann nicht angeschlossen werden.
MsgHasDrainPipe=Abflussrohr kann nicht angeschlossen werden.
MsgConnectedToTank=%s an %s angeschlossen.

View File

@ -6,6 +6,11 @@ DescriptionSource=Source pipe from which liquid is pulled into the pump.
DescriptionDrain=Drain pipe where liquids are pumped to.
MsgNoNewPipe=There is no pump here.
MsgNoNewPipeToTank=Connection is not possible here.
MsgCreatedSource=Connected source pipe.
MsgCreatedDrain=Connected drain pipe.
MsgHasPipes=Pump already has a source and a drain pipe.
MsgHasPipes=Pump already has a source and a drain pipe.
MsgHasSourcePipe=Unable to connect source pipe.
MsgHasDrainPipe=Unable to connect drain pipe.
MsgConnectedToTank=Connected %s to %s.

View File

@ -3,7 +3,7 @@
#include Libary_LiquidContainer
/*
Author: ST-DDT
Author: ST-DDT, Marky
Import this to allow the structures to
-fill liquids which has been pumped into the building into the internal contained
-extract liquids from internal contained and pump it somewhere else
@ -80,6 +80,8 @@ func Construction()
_inherited(...);
}
func IsLiquidTank(){ return true;}
public func GetInteractionMenus(object clonk)
{
var menus = _inherited() ?? [];

View File

@ -11,6 +11,7 @@
#include Library_Ownable
#include Library_PowerProducer
#include Library_Flag
#include Library_Tank
local DefaultFlagRadius = 200;
@ -34,6 +35,10 @@ protected func Initialize()
public func IsContainer() { return true; }
// can connect a drain pipe that is connected to a pump
public func CanConnectDrainPipe(){ return true;}
protected func RejectCollect(id item, object obj)
{
if (obj->~IsFuel())

View File

@ -30,6 +30,13 @@ protected func InitializePlayer(int plr)
effect.testnr = 1;
effect.launched = false;
effect.plr = plr;
// Add pump
CreateObjectAbove(Pump, 100, 200);
CreateObjectAbove(SteamEngine, 150, 200);
GetCrew(plr)->CreateContents(Pipe);
GetCrew(plr)->CreateContents(Pipe);
GetCrew(plr)->CreateContents(Pipe);
return true;
}