File: Script.c

package info (click to toggle)
openclonk 8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 169,520 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (187 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download | duplicates (5)
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
/**
	Pipe line

	@author ST-DDT, Marky
*/

local pipe_kit;
local is_air_pipe = false;

private func Initialize()
{
	SetAction("Connect");
	SetVertexXY(0, GetX(), GetY());
	SetVertexXY(1, GetX(), GetY());
	SetNeutral();
}

// Greyish colour
public func SetNeutral()
{
	this.LineColors = [RGB(80, 80, 120), RGB(80, 80, 120)];
	is_air_pipe = false;
}

// Reddish colour.
// Please to not change otherwise people with dyschromatopsia will hunt you down.
public func SetDrain()
{
	this.LineColors = [RGB(238, 102, 0), RGB(238, 102, 0)];
	is_air_pipe = false;
}

// Greenish colour.
// Please to not change otherwise people with dyschromatopsia will hunt you down.
public func SetSource()
{
	this.LineColors = [RGB(102, 136, 34), RGB(102, 136, 34)];
	is_air_pipe = false;
}

// Blueish colour.
public func SetAir()
{
	this.LineColors = [RGB(0, 153, 255), RGB(0, 153, 255)];
	is_air_pipe = true;
}

// Returns true if this object is a functioning pipe.
public func IsPipeLine()
{
	return GetAction() == "Connect";
}

public func IsAirPipe()
{
	return this.is_air_pipe;
}

// 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 || (!strict && pipe_kit == obj);
}

// Returns the object which is connected to obj through this pipe.
public func GetConnectedObject(object obj)
{
	if (GetActionTarget(0) == obj)
		return GetActionTarget(1);
	if (GetActionTarget(1) == obj)
		return GetActionTarget(0);
	return;
}

// Switches connection from connected_to to another obj.
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;
}

public func GetPipeKit()
{
	if (pipe_kit)
	{
		return pipe_kit;
	}
	else
	{
		FatalError("Unexpected error: This pipe has lost its pipe kit!");
	}
}


private func OnLineBreak(bool no_msg)
{
	Sound("Objects::LineSnap");
	if (!no_msg)
		BreakMessage();

	if (GetPipeKit())
	{
		GetPipeKit()->SetNeutralPipe();
		if (GetActionTarget(0)) GetActionTarget(0)->~OnPipeDisconnect(GetPipeKit());
		if (GetActionTarget(1)) GetActionTarget(1)->~OnPipeDisconnect(GetPipeKit());
	}
	
	return;
}

private func OnLineChange()
{
	// Notify action targets about line change.
	var act1 = GetActionTarget(0);
	var act2 = GetActionTarget(1);	
	if (act1) act1->~OnPipeLengthChange(this);
	if (act2) act2->~OnPipeLengthChange(this);
	// Break line if it is too long.
	if (GetPipeLength() > this.PipeMaxLength)
	{
		OnLineBreak();
		RemoveObject();
	}
	return;
}

// Returns the length between all the vertices.
public func GetPipeLength()
{
	var current_length = 0;
	for (var index = 0; index < GetVertexNum() - 1; index++)
		current_length += Distance(GetVertex(index, VTX_X), GetVertex(index, VTX_Y), GetVertex(index + 1, VTX_X), GetVertex(index + 1, VTX_Y));
	return current_length;
}

private func Destruction()
{
	if (GetActionTarget(0)) GetActionTarget(0)->~OnPipeLineRemoval();
	if (GetActionTarget(1)) GetActionTarget(1)->~OnPipeLineRemoval();
	return;
}

private func BreakMessage()
{
	var line_end = GetPipeKit();
	if (line_end) line_end->Report("$TxtPipeBroke$");
	return;
}

public func SaveScenarioObject(props)
{
	if (!inherited(props, ...)) return false;
	SaveScenarioObjectAction(props);
	// Ensure vertices are moved to the action targets to ensure the shortest path length and prevent a line break.
	if (GetActionTarget()) props->AddCall("VtxX", this, "SetVertexXY", 0, GetActionTarget()->GetX(), GetActionTarget()->GetY());
	if (GetActionTarget(1)) props->AddCall("VtxX", this, "SetVertexXY", 1, GetActionTarget(1)->GetX(), GetActionTarget(1)->GetY());
	if (pipe_kit) props->AddCall("PipeKit", this, "SetPipeKit", pipe_kit);
	if (IsAirPipe()) props->AddCall("AirPipe", this, "SetAir");
	if (this.BlockPipeCutting) props->AddSet("BlockPipeCutting", this, "BlockPipeCutting", this.BlockPipeCutting);
	return true;
}

/*-- Properties --*/

local Name = "$Name$";
local PipeMaxLength = 1200;
local BlockPipeCutting = false;

local ActMap = {
	Connect = {
		Prototype = Action,
		Name = "Connect",
		Procedure = DFA_CONNECT,
		NextAction = "Connect"
	}
};