1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/**
Pipe
Author: ST-DDT, Marky
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
*/
static const PIPE_STATE_Neutral = nil;
static const PIPE_STATE_Source = "Source";
static const PIPE_STATE_Drain = "Drain";
static const PIPE_STATE_Air = "Air";
local pipe_state = nil;
local pipe_line;
local ApertureOffsetX = 0;
local ApertureOffsetY = 3;
/* ---------- Callbacks ---------- */
protected func Hit()
{
Sound("Hits::GeneralHit?");
}
private func Destruction()
{
// Remove the line first, so that it does not provoke errors on destruction.
// Actually there is an ill-defined state where line contains the pipe and is
// removed. Then line = GetConnectedLine() causes an error, instead use the
// slower find object variant.
var line = FindObject(Find_Func("IsConnectedTo", this));
if (line)
line->RemoveObject();
return;
}
public func IsToolProduct() { return true;}
public func OnPipeLineRemoval()
{
SetNeutralPipe();
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()
{
var line = GetConnectedLine();
if (!line) return;
var percentage = 100 * line->GetPipeLength() / line.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;
}
public func CanBeStackedWith(object other)
{
// Do not stack source/drain/unused pipes
return _inherited(other) && (pipe_state == other.pipe_state);
}
/**
The pump calls this function to prevent clogging of the intake.
Cycles through several aperture offset indices.
*/
public func HasAperture() { return true; }
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;
}
/**
Container dies: Drop connected pipes so they don't
draw huge lines over the landscape
*/
func IsDroppedOnDeath(object clonk)
{
return !!GetConnectedLine();
}
/* ---------- Pipe States ---------- */
public func IsNeutralPipe() { return pipe_state == PIPE_STATE_Neutral; }
public func IsDrainPipe() { return pipe_state == PIPE_STATE_Drain; }
public func IsSourcePipe() { return pipe_state == PIPE_STATE_Source; }
public func IsAirPipe() { return pipe_state == PIPE_STATE_Air; }
public func GetPipeState() { return pipe_state; }
public func SetNeutralPipe()
{
pipe_state = PIPE_STATE_Neutral;
SetGraphics("", nil, GFX_Overlay, GFXOV_MODE_Picture);
Description = "$Description$";
Name = "$Name$";
var line = GetConnectedLine();
if (line)
line->SetNeutral();
}
public func SetDrainPipe()
{
pipe_state = PIPE_STATE_Drain;
SetGraphics("Drain", Pipe, GFX_Overlay, GFXOV_MODE_Picture);
SetObjDrawTransform(1000, 0, 0, 0, 1000, 10000, GFX_Overlay);
Description = "$DescriptionDrain$";
Name = "$NameDrain$";
var line = GetConnectedLine();
if (line)
line->SetDrain();
}
public func SetSourcePipe()
{
pipe_state = PIPE_STATE_Source;
SetGraphics("Source", Pipe, GFX_Overlay, GFXOV_MODE_Picture);
SetObjDrawTransform(1000, 0, 0, 0, 1000, 10000, GFX_Overlay);
Description = "$DescriptionSource$";
Name = "$NameSource$";
var line = GetConnectedLine();
if (line)
line->SetSource();
}
public func SetAirPipe()
{
pipe_state = PIPE_STATE_Air;
SetGraphics("Air", Pipe, GFX_Overlay, GFXOV_MODE_Picture);
SetObjDrawTransform(1000, 0, 0, 0, 1000, 10000, GFX_Overlay);
Description = "$DescriptionAir$";
Name = "$NameAir$";
var line = GetConnectedLine();
if (line)
line->SetAir();
}
/* ---------- Pipe Connection ---------- */
public func ConnectPipeTo(object target, string specific_pipe_state, bool block_cutting)
{
if (!target || target->~QueryConnectPipe(this, true))
return false;
AddLineConnectionTo(target, block_cutting);
target->OnPipeConnect(this, specific_pipe_state);
Sound("Objects::Connect");
return true;
}
/* ---------- Line Connection ---------- */
public func SetPipeLine(to_line)
{
pipe_line = to_line;
}
/**
Finds a line that is connected to this pipe kit.
@return object the pipe, or nil if nothing was found.
*/
public func GetConnectedLine()
{
return pipe_line;
}
/**
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
*/
public func AddLineConnectionTo(object target, bool block_cutting)
{
var line = GetConnectedLine();
if (line)
{
if (line->IsConnectedTo(this, true))
{
line->SwitchConnection(this, target);
SetPipeLine(line);
line.BlockPipeCutting = block_cutting;
// Delayed entrance, so that the message is still displayed above the clonk.
ScheduleCall(this, this.Enter, 1, nil, line);
return line;
}
else
{
FatalError("This line is connected to two objects already!");
}
}
else
{
return CreateLine(target, block_cutting);
}
}
/**
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
*/
public func CutLineConnection(object target)
{
var line = GetConnectedLine();
if (!line) return;
// connected only to the kit and a structure
if (line->IsConnectedTo(this, true))
{
target->OnPipeDisconnect(this);
line->RemoveObject();
}
// connected to the target and another structure
else if (line->IsConnectedTo(target, true))
{
target->OnPipeDisconnect(this);
Exit(); // the kit was inside the line at this point.
SetPosition(target->GetX(), target->GetY() + target->GetBottom() - this->GetBottom());
line->SwitchConnection(target, this);
SetPipeLine(line);
}
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)));
}
}
// Returns whether the cutting pipe is blocked.
public func QueryCutLineConnection(object target)
{
var line = GetConnectedLine();
if (!line)
return false;
return line.BlockPipeCutting;
}
/**
Creates a new pipe line that is connected to this pipe kit.
@par target the target object.
@return object the line that was created
*/
public func CreateLine(object target, bool block_cutting)
{
// Create and connect pipe line.
pipe_line = CreateObject(PipeLine, 0, 0, NO_OWNER);
pipe_line->SetActionTargets(this, target);
pipe_line->SetPipeKit(this);
pipe_line.BlockPipeCutting = block_cutting;
return pipe_line;
}
/** Will connect liquid line to building at the clonk's position. */
protected func ControlUse(object clonk, int x, int y)
{
var target = FindObject(Find_AtPoint(), Find_Func("CanConnectPipe"));
if (target)
{
ConnectPipeTo(target, GetPipeState());
}
return true;
}
// Returns to which object the given object is connected through this pipe.
public func GetConnectedObject(object obj)
{
if (!pipe_line)
return;
return pipe_line->GetConnectedObject(obj);
}
/**
Displays a message at top-level container of this object.
@par message the message
*/
public func Report(string message)
{
var reporter = this;
var next = Contained();
while (next)
{
reporter = next;
next = reporter->Contained();
}
reporter->Message(message, ...);
}
/*-- Saving --*/
public func SaveScenarioObject(props)
{
if (!inherited(props, ...)) return false;
if (pipe_line) props->AddCall("PipeLine", this, "SetPipeLine", pipe_line);
if (IsNeutralPipe()) props->AddCall("PipeStateNeutral", this, "SetNeutralPipe");
else if (IsDrainPipe()) props->AddCall("PipeStateDrain", this, "SetDrainPipe");
else if (IsSourcePipe()) props->AddCall("PipeStateSource", this, "SetSourcePipe");
else if (IsAirPipe()) props->AddCall("PipeStateAir", this, "SetAirPipe");
return true;
}
/*-- Properties --*/
local Name = "$Name$";
local Description = "$Description$";
local Collectible = 1;
local Components = {Metal = 1};
|