2016-03-25 16:20:54 +00:00
/**
Pipe
2010-12-31 12:20:54 +00:00
2016-02-04 17:25:19 +00:00
Author : ST - DDT , Marky
2016-02-05 05:58:00 +00:00
The pipe has several states :
- neutral
- source ( green line )
- drain ( red line )
By default a pipe is neutral , and stays neutral if you connect it to a liquid tank .
- Connected neutral pipes cannot be connected to another ( neutral ) liquid tank
( this would not make any sense , or maybe it does if you want to have a liquid
tank array , but this would lead to all kinds of complications and special
cases eventually ) .
However , a pipe can be connected to a liquid transferring structure ( pump ) , too .
- Neutral pipes can be connected to a pump . They then become a drain or source pipe .
- Connected neutral pipes can be connected to a pump . See above .
- Source pipes can be connected to liquid tanks only .
- Drain pipes can be connected to liquid tanks only .
Logic for object use from inventory and interaction menu :
- Both know the using Clonk ( interaction menu : the container of the pipe )
- The user may want to connect a drain pipe before connecting a source pipe
- The user may want to connect a neutral pipe
= > separate functions are necessary
2016-03-25 16:20:54 +00:00
*/
2013-05-30 20:11:39 +00:00
2016-02-05 21:52:34 +00:00
static const PIPE_STATE_Neutral = nil ;
2016-02-04 17:25:19 +00:00
static const PIPE_STATE_Source = " Source " ;
static const PIPE_STATE_Drain = " Drain " ;
2016-02-05 21:52:34 +00:00
local ApertureOffsetX = 0 ;
local ApertureOffsetY = 3 ;
/* ---------- Callbacks ---------- */
2010-12-31 12:20:54 +00:00
protected func Hit ( )
{
2015-12-13 21:14:55 +00:00
Sound ( " Hits::GeneralHit? " ) ;
2010-12-31 12:20:54 +00:00
}
2016-02-06 08:06:50 +00:00
private func Destruction ( )
{
// remove the line first, so that it does not provoke errors on destruction
var line = GetConnectedLine ( ) ;
if ( line ) line - > RemoveObject ( ) ;
}
2016-02-05 21:52:34 +00:00
public func IsToolProduct ( ) { return true ; }
2010-12-31 12:20:54 +00:00
2016-03-25 16:20:54 +00:00
public func OnPipeLineRemoval ( )
{
2016-03-25 17:51:18 +00:00
SetNeutralPipe ( ) ;
2016-03-25 16:20:54 +00:00
OnPipeLengthChange ( ) ;
return ;
}
public func OnPipeLengthChange ( )
{
// Update usage bar for a possible carrier (the clonk).
var carrier = Contained ( ) ;
if ( carrier )
carrier - > ~ OnInventoryChange ( ) ;
return ;
}
// Display the line length bar over the pipe icon.
public func GetInventoryIconOverlay ( )
{
2016-03-25 17:51:18 +00:00
var pipe = GetConnectedLine ( ) ;
if ( ! pipe ) return ;
2016-03-25 16:20:54 +00:00
var percentage = 100 * pipe - > GetPipeLength ( ) / pipe . PipeMaxLength ;
var red = percentage * 255 / 100 ;
var green = 255 - red ;
// Overlay a usage bar.
var overlay =
{
Bottom = " 0.75em " ,
Margin = [ " 0.1em " , " 0.25em " ] ,
BackgroundColor = RGB ( 0 , 0 , 0 ) ,
margin =
{
Margin = " 0.05em " ,
bar =
{
BackgroundColor = RGB ( red , green , 0 ) ,
Right = Format ( " %d%% " , percentage ) ,
}
}
} ;
return overlay ;
}
2016-02-05 21:52:34 +00:00
public func CanBeStackedWith ( object other )
2010-12-31 12:20:54 +00:00
{
2016-02-05 21:52:34 +00:00
// Do not stack source/drain/unused pipes
return inherited ( other ) & & ( PipeState = = other . PipeState ) ;
2016-02-04 17:25:19 +00:00
}
2016-08-07 19:26:15 +00:00
2016-02-05 21:52:34 +00:00
/**
The pump calls this function to prevent clogging of the intake .
Cycles through several aperture offset indices .
*/
2016-08-07 19:26:15 +00:00
public func HasAperture ( ) { return true ; }
public func CycleApertureOffset ( )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
// 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 ;
2016-02-04 20:37:33 +00:00
}
2016-02-05 21:52:34 +00:00
/**
Container dies : Drop connected pipes so they don ' t
draw huge lines over the landscape
*/
func IsDroppedOnDeath ( object clonk )
2016-02-04 21:18:40 +00:00
{
2016-02-05 21:52:34 +00:00
return ! ! GetConnectedLine ( ) ;
2016-02-04 21:18:40 +00:00
}
2016-02-04 17:25:19 +00:00
2016-02-05 21:52:34 +00:00
/* ---------- Pipe States ---------- */
2016-02-04 17:25:19 +00:00
2010-12-31 12:20:54 +00:00
2016-02-05 21:52:34 +00:00
func IsNeutralPipe ( ) { return PipeState = = PIPE_STATE_Neutral ; }
func IsDrainPipe ( ) { return PipeState = = PIPE_STATE_Drain ; }
func IsSourcePipe ( ) { return PipeState = = PIPE_STATE_Source ; }
2016-02-04 17:25:19 +00:00
2016-02-05 21:52:34 +00:00
func SetNeutralPipe ( )
2016-02-04 17:25:19 +00:00
{
2016-02-05 21:52:34 +00:00
PipeState = PIPE_STATE_Neutral ;
2016-02-04 21:18:40 +00:00
2016-02-05 21:52:34 +00:00
SetGraphics ( " " , nil , GFX_Overlay , GFXOV_MODE_Picture ) ;
Description = " $Description$ " ;
Name = " $Name$ " ;
var line = GetConnectedLine ( ) ;
if ( line )
2016-02-04 21:18:40 +00:00
{
2016-02-05 21:52:34 +00:00
line - > SetNeutral ( ) ;
2016-02-04 17:25:19 +00:00
}
2016-02-04 20:37:33 +00:00
}
2016-02-05 21:52:34 +00:00
func SetDrainPipe ( )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
PipeState = PIPE_STATE_Drain ;
SetGraphics ( " Drain " , Pipe , GFX_Overlay , GFXOV_MODE_Picture ) ;
2016-03-25 17:55:22 +00:00
SetObjDrawTransform ( 1000 , 0 , 0 , 0 , 1000 , 10000 , GFX_Overlay ) ;
2016-02-05 21:52:34 +00:00
Description = " $DescriptionDrain$ " ;
Name = " $NameDrain$ " ;
2016-02-04 20:37:33 +00:00
2016-02-05 21:52:34 +00:00
var line = GetConnectedLine ( ) ;
if ( line )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
line - > SetDrain ( ) ;
2016-02-04 20:37:33 +00:00
}
}
2016-02-05 21:52:34 +00:00
func SetSourcePipe ( )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
PipeState = PIPE_STATE_Source ;
2016-02-04 20:37:33 +00:00
SetGraphics ( " Source " , Pipe , GFX_Overlay , GFXOV_MODE_Picture ) ;
2016-03-25 17:55:22 +00:00
SetObjDrawTransform ( 1000 , 0 , 0 , 0 , 1000 , 10000 , GFX_Overlay ) ;
2016-02-04 20:37:33 +00:00
Description = " $DescriptionSource$ " ;
Name = " $NameSource$ " ;
2016-02-05 21:52:34 +00:00
var line = GetConnectedLine ( ) ;
if ( line )
{
line - > SetSource ( ) ;
}
2016-02-04 20:37:33 +00:00
}
2016-02-05 21:52:34 +00:00
/* ---------- Pipe Connection ---------- */
func ConnectPipeTo ( object target , string specific_pipe_state )
{
if ( ! target | | target - > ~ QueryConnectPipe ( this ) ) return false ;
2016-02-08 16:45:32 +00:00
AddLineConnectionTo ( target ) ;
2016-02-06 22:12:26 +00:00
target - > OnPipeConnect ( this , specific_pipe_state ) ;
2016-02-06 07:28:02 +00:00
Sound ( " Objects::Connect " ) ;
2016-02-04 17:25:19 +00:00
return true ;
}
2016-02-05 21:52:34 +00:00
/* ---------- Line Connection ---------- */
/**
Finds a line that is connected to this pipe kit .
@ return object the pipe , or nil if nothing was found .
*/
func GetConnectedLine ( )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
return FindObject ( Find_Func ( " IsConnectedTo " , this ) ) ;
2016-02-04 20:37:33 +00:00
}
2016-02-05 21:52:34 +00:00
/**
Connects a line to an object .
The pipe kit will connect the line to the target object and itself first .
Otherwise , if the pipe kit already has a line , it connects that line to the target .
Note : Reports a fatal error if the line would be connected to more than two targets
at the same time .
@ par target the target object
*/
func AddLineConnectionTo ( object target )
{
var line = GetConnectedLine ( ) ;
if ( line )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
if ( line - > IsConnectedTo ( this , true ) )
2016-02-04 20:37:33 +00:00
{
2016-02-05 21:52:34 +00:00
line - > SwitchConnection ( this , target ) ;
2016-02-06 07:28:02 +00:00
ScheduleCall ( this , this . Enter , 1 , nil , line ) ; // delayed entrance, so that the message is still displayed above the clonk
2016-02-05 21:52:34 +00:00
return line ;
}
else
{
FatalError ( " This line is connected to two objects already! " ) ;
2016-02-04 20:37:33 +00:00
}
}
else
{
2016-02-05 21:52:34 +00:00
return CreateLine ( target ) ;
2016-02-04 20:37:33 +00:00
}
}
2016-02-05 21:52:34 +00:00
/**
Cuts the connection between the line and an object .
Note : Reports a fatal error if the target was not
connected to the line .
@ par target the target object
*/
func CutLineConnection ( object target )
2015-10-05 23:34:28 +00:00
{
2016-02-05 21:52:34 +00:00
var line = GetConnectedLine ( ) ;
if ( ! line ) return ;
// connected only to the kit and a structure
if ( line - > IsConnectedTo ( this , true ) )
{
2016-02-06 22:12:26 +00:00
target - > OnPipeDisconnect ( this ) ;
2016-02-05 21:52:34 +00:00
line - > RemoveObject ( ) ;
}
// connected to the target and another structure
else if ( line - > IsConnectedTo ( target , true ) )
{
2016-02-06 22:12:26 +00:00
target - > OnPipeDisconnect ( this ) ;
2016-02-05 21:52:34 +00:00
Exit ( ) ; // the kit was inside the line at this point.
SetPosition ( target - > GetX ( ) , target - > GetY ( ) ) ;
line - > SwitchConnection ( target , this ) ;
}
else
{
FatalError ( Format ( " An object %v is trying to cut the pipe connection, but only objects %v and %v may request a disconnect " , target , line - > GetActionTarget ( 0 ) , line - > GetActionTarget ( 1 ) ) ) ;
}
2016-01-11 02:34:28 +00:00
}
2016-02-05 21:52:34 +00:00
/**
Creates a new pipe line that is connected to this pipe kit .
@ par target the target object .
@ return object the line that was created
*/
func CreateLine ( object target )
2016-01-11 02:34:28 +00:00
{
2016-02-05 21:52:34 +00:00
// Create and connect pipe line.
var line = CreateObject ( PipeLine , 0 , 0 , NO_OWNER ) ;
line - > SetActionTargets ( this , target ) ;
line - > SetPipeKit ( this ) ;
return line ;
2015-10-05 23:34:28 +00:00
}
2015-09-05 17:10:19 +00:00
2016-02-05 21:52:34 +00:00
/** Will connect liquid line to building at the clonk's position. */
protected func ControlUse ( object clonk , int x , int y )
2015-09-05 17:10:19 +00:00
{
2016-02-05 21:52:34 +00:00
var target = FindObject ( Find_AtPoint ( ) , Find_Func ( " CanConnectPipe " ) ) ;
if ( target )
{
ConnectPipeTo ( target ) ;
}
2015-09-05 17:10:19 +00:00
return true ;
}
2011-10-11 22:32:46 +00:00
2015-09-26 21:56:36 +00:00
2016-02-05 21:52:34 +00:00
/**
Displays a message at top - level container of this object .
@ par message the message
*/
func Report ( string message )
2015-09-26 21:56:36 +00:00
{
2016-02-05 21:52:34 +00:00
var reporter = this ;
var next = Contained ( ) ;
while ( next )
{
reporter = next ;
next = reporter - > Contained ( ) ;
}
reporter - > Message ( message , . . . ) ;
2015-09-26 21:56:36 +00:00
}
2016-03-25 16:20:54 +00:00
/*-- Properties --*/
local Name = " $Name$ " ;
local Description = " $Description$ " ;
local Collectible = 1 ;
local PipeState = nil ;
2016-05-01 14:14:56 +00:00
local Components = { Metal = 1 } ;