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 (254 lines) | stat: -rw-r--r-- 4,961 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
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
/**
	Locomotive
	Transportation device for long distances and heavy cargo.
	
	@author Pyrit
*/

#include Library_ElevatorControl
#include Library_Tank


local move_dir;
local fuel_amount;
local drive_anim;
local rot_wheels;

protected func Initialize()
{
	move_dir = 0;
	fuel_amount = 0;
	rot_wheels = 0;
	AddTimer("Move", 1);
	SetAction("Drive");
	// Entrance is always open.
	SetEntrance(true);
	// Start driving animation.
	drive_anim = PlayAnimation("Main TrainAction", 1, Anim_Const(0));
	return;
}

public func OnContentMenuOpened()
{
	//return Sound("Locomotive::Chuff");
}

public func OnContentMenuClosed()
{
	//return Sound("Locomotive::Chuff");
}

public func Collection2(object obj)
{
	if (obj->GetOCF() & OCF_CrewMember)
	{
		obj->SetAction("Walk");
		obj->PlayAnimation("Drive", CLONK_ANIM_SLOT_Movement, Anim_Const(10), Anim_Const(1000));
	}
	return;
}

public func Ejection(object obj)
{
	if (obj->GetOCF() & OCF_CrewMember)
	{
		
	}
	return;
}


/*-- Liquid Control --*/

public func IsLiquidContainerForMaterial(string liquid)
{
	return WildcardMatch("Water", liquid);
}

public func GetLiquidContainerMaxFillLevel(liquid_name)
{
	return this.LiquidCapacity;
}

// The locomotive may only have a drain pipe.
public func QueryConnectPipe(object pipe, bool do_msg)
{
	if (pipe->IsDrainPipe() && GetDrainPipe())
	{
		if (do_msg)
			pipe->Report("$MsgHasDrainPipe$");
		return true;
	}
	if (pipe->IsSourcePipe() || pipe->IsAirPipe())
	{
		if (do_msg)
			pipe->Report("$MsgPipeProhibited$");
		return true;
	}
	return false;
}

// Set to source or drain pipe.
public func OnPipeConnect(object pipe, string specific_pipe_state)
{
	if (specific_pipe_state == PIPE_STATE_Drain)
	{
		SetDrainPipe(pipe);
		pipe->SetDrainPipe();
	}
	else
	{
		if (!GetDrainPipe())
			OnPipeConnect(pipe, PIPE_STATE_Drain);
	}
	pipe->Report("$MsgConnectedPipe$");
}



/*-- Movement --*/

public func Move()
{
	UpdateFuel();
	var water_amount = GetLiquidAmount(Water);
	
	if (move_dir == 0)
		return;
	
	if (fuel_amount <= 0 && water_amount <= 0)
		return Message("$MsgNoCoalAndWater$");
	if (water_amount <= 0)
		return Message("$MsgNoWater$");
	if (fuel_amount <= 0)
		return Message("$MsgNoCoal$");
	if (Stuck())
		return Message("$MsgStuck$");
		
	fuel_amount--;
	RemoveLiquid(Water, 1);
	
	if (move_dir == -1)
		SetDir(DIR_Left);
	if (move_dir == 1)
		SetDir(DIR_Right);
	SetXDir(15 * move_dir);
	DoExhaust();
	TurnWheels();
	return;
}

private func DoExhaust()
{
	//Sound("Locomotive::Chuff");
	Smoke(11 * (2 * GetDir() - 1), -18, 6);
	var spark = {
		Size = PV_Linear(1, 0), 
	    ForceY = RandomX(-50, -60),
	    ForceX = PV_Random(-1, 1, 10),
		DampingY = PV_Linear(600, 0),
		Stretch = PV_Speed(1000, 500),
		Rotation = PV_Direction(),
		CollisionVertex = 500, 
		OnCollision = PC_Stop(),
	    R = 255,
	    G = PV_Linear(128, 32),
	    B = PV_Random(0, 128, 2),
	    Alpha = PV_Random(255, 0, 3),
		BlitMode = GFX_BLIT_Additive,
	};
	CreateParticle("Magic", 12 * (2 * GetDir() - 1), -18, PV_Random(-3, 3), PV_Random(-3, -4), 36, spark, 1);
	return;
}

private func TurnWheels()
{
	var anim_pos = GetAnimationPosition(drive_anim);
	anim_pos += Abs(GetXDir()) * 8;
	while (anim_pos < 0) 
		anim_pos += 2000;
	while (anim_pos > 2000) 
		anim_pos -= 2000;
	SetAnimationPosition(drive_anim, Anim_Const(anim_pos));
	return;
}

private func UpdateFuel()
{
	if (fuel_amount > 0)
		return;
	var fuel = FindObject(Find_Or(Find_ID(Wood), Find_ID(Coal)), Find_Container(this));
	if (fuel)
	{
		fuel_amount += fuel->GetFuelAmount();
		fuel->RemoveObject();	
	}
	return;
}

public func ContainedDown(object clonk)
{
	move_dir = 0;
}

public func ContainedLeft(object clonk)
{
	move_dir = -1;
}

public func ContainedRight(object clonk)
{
	move_dir = 1;
}

public func ContainedStop(object clonk)
{
	move_dir = 0;
}

public func Hit2()
{
	Sound("Hits::Materials::Metal::DullMetalHit?");
}

public func IsVehicle() { return true; }
public func IsContainer() { return true; }


/*-- Contents --*/

protected func RejectCollect(id object_id)
{
	if (object_id == Coal || object_id->~IsLiquid())
		return false;
	return true;
}


/*-- Properties --*/

local ActMap = {
	Drive = {
		Prototype = Action,
		Name = "Drive",
		Procedure = DFA_NONE,
		Directions = 2,
		FlipDir = 1,
		X = 0,
		Y = 0,
		Wdt = 24,
		Hgt = 22,
		NextAction = "Drive",
	},
};

// this.MeshTransformation = Trans_Mul(Trans_Rotate(90, 0, 0, 1), Trans_Scale(580, -580, 580), Trans_Translate(24000, -27000, 0))
local MeshTransformation = [0, 580, 0, -15660, 580, 0, 0, 13920, 0, 0, 580, 0];
// this.PictureTransformation = Trans_Mul(Trans_Translate(-18000, 27000, 0), Trans_Rotate(90, 0, 0, 1), Trans_Rotate(-20, 1, 0, 0))
local PictureTransformation = [0, -940, -342, -18000, 1000, 0, 0, 27000, 0, -342, 940, 0];
local Name = "$Name$";
local Description = "$Description$";
local BorderBound = C4D_Border_Sides;
local ContactCalls = true;
local Components = {Wood = 1, Metal = 4};
local LiquidCapacity = 2400;