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 (315 lines) | stat: -rw-r--r-- 7,310 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
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
/**
	Sawmill
	Cuts trees or other objects into wood. Accepts only objects purely made from wood (like trees).
	
	@authors Ringwaul, Clonkonaut
*/

#include Library_Structure
#include Library_Ownable
#include Library_PowerConsumer

local running = false;
local power = false;

public func Construction()
{
	SetProperty("MeshTransformation", Trans_Rotate(-20, 0, 1, 0));
	SetAction("Default");
	return _inherited(...);
}

public func IsHammerBuildable() { return true; }

public func Initialize()
{
	this.SpinAnimation = PlayAnimation("work", 10, Anim_Const(0));
	AddTimer("CollectTrees", 4);
	return _inherited(...);
}

/*-- Interaction --*/

// Sawmill acts as a container to be able to collect wooden objects.
public func IsContainer() { return true; }

// Do not show normal inventory menu. Instead we show the remaining wood in an extra menu.
public func RejectContentsMenu() { return true; }

// Sawmill can't be interacted with.
public func IsInteractable() { return false; }

/*-- Production --*/

private func Collection(object obj)
{
	Sound("Objects::Clonk");
	Saw(obj);
}

private func RejectCollect(id id_def, object collect)
{
	// Don't collect wood
	if (id_def == Wood)
		return true;
	if (CheckWoodObject(collect))
		return false;
	return true;
}

// Timer, check for objects to collect in the designated collection zone
public func CollectTrees()
{
	if (GetCon() < 100)
		return;
	// Only take one tree at a time
	if (!ContentsCount(Wood))
		FindTrees();
}

// Automatically search for trees in front of sawmill. Temporary solution?
private func FindTrees()
{
	var tree = FindObject(Find_AtPoint(), Find_Func("IsTree"), Find_Not(Find_Func("IsStanding")), Find_Func("GetComponent", Wood));
	if (!tree)
		return;
	
	return Saw(tree);
}

// Returns whether the object is made purely out of wood.
private func CheckWoodObject(object target)
{
	if (target->GetComponent(nil, 0) != Wood) 
		return false;
	if (target->GetComponent(nil, 1)) 
		return false;
	return true;
}

// Provides an own interaction menu.
public func HasInteractionMenu() { return true; }

// Show a helpful hint to the player. The hint is colored and titled the same as the production menu for more visual coherence.
public func GetInteractionMenus(object clonk)
{
	var menus = _inherited(clonk, ...) ?? [];
	var prod_menu =
	{
		title = "$Production$",
		entries_callback = this.GetInfoMenuEntries,
		callback = nil,
		BackgroundColor = RGB(0, 0, 50),
		Priority = 20
	};
	PushBack(menus, prod_menu);
	
	return menus;
}

public func GetInfoMenuEntries()
{
	var wood_count = ContentsCount(Wood);
	var info_text =
	{
		Right = "100%", Bottom = "6em",
		text = {Left = "2em", Text = "$AutoProduction$", Style = GUI_TextVCenter | GUI_TextHCenter},
		image = {Right = "2em", Bottom = "2em", Symbol = Wood},
		queue =
		{
			Top = "100% - 1.5em",
			Style = GUI_TextRight,
			Text = Format("$WoodInQueue$: %2d {{Wood}}", wood_count)
		}
	};
	return [{symbol = Wood, custom = info_text}];
}

public func Saw(object target)
{
	if (target->Contained() != this)
		target->Enter(this);
	target->Split2Components();
	AddEffect("WoodProduction", this, 100, 3, this);
	// Refresh interaction menus to show the wood count.
	UpdateInteractionMenus(this.GetInfoMenuEntries);
	return true;
}

private func ProductionTime(id product) { return _inherited(product, ...) ?? 100; }
private func PowerNeed() { return 20; }

private func FxWoodProductionStart(object t, proplist effect, int temp)
{
	if (temp) return;

	effect.runtime = 0;
	effect.starttime = 0;

	RegisterPowerRequest(PowerNeed());
}

private func FxWoodProductionTimer(object t, proplist effect, int time)
{
	if (!power)
	{
		if (effect.starttime)
			effect.starttime = 0;
		return FX_OK;
	}

	if (!(time%20))
		Smoke(10 * GetCalcDir(),10,10);

	if (!running)
	{
		SpinOn(effect.starttime);
		effect.starttime += 3;
		return FX_OK;
	}

	effect.runtime += 3;
	var dir = GetCalcDir();
	CreateParticle("WoodChip", PV_Random(-7 * dir, -3 * dir), PV_Random(3, 6), PV_Random(-5 * dir, -11 * dir), PV_Random(-4, -2), PV_Random(36 * 3, 36 * 10), Particles_WoodChip(), 3);

	if (effect.runtime >= ProductionTime())
	{
		EjectWood();
		effect.runtime = 0;
	}
	else
		return FX_OK;

	if (!ContentsCount(Wood))
		return FX_Execute_Kill;
}

private func FxWoodProductionStop(object t, proplist effect, int r, bool temp)
{
	if (temp) return;

	UnregisterPowerRequest();
	SpinOff();
	power = false;
}

public func OnEnoughPower()
{
	if (power) return _inherited(...);

	power = true;
	return _inherited(...);
}

public func OnNotEnoughPower()
{
	if (!power) return _inherited(...);

	power = false;
	SpinOff();
	return _inherited(...);
}

public func EjectWood()
{
	var wood = FindContents(Wood);
	if (!wood) return;

	wood->Exit(-25 * GetCalcDir(), -8, 30 - Random(59), -2 * GetCalcDir(), 1);
	Sound("Structures::EjectionPop");
	
	// Refresh interaction menus to show the wood count.
	UpdateInteractionMenus(this.GetInfoMenuEntries);
}

/*-- Animation --*/

private func SpinOn(int diff)
{
	var spin;
	var rotate = 0;
	if (diff == 0)
	{
		ClearScheduleCall(this, "SpinOff");
		Sound("Sawmill::EngineStart");
		SetMeshMaterial("Beltspin", 1);
	}
	if (diff > 20) rotate = Sin(70 * diff, 1) - 1;
	if (Inside(diff, 0, 20)) spin = 100;
	if (Inside(diff, 21, 40)) spin = 75;
	if (Inside(diff, 41, 60)) spin = 50;
	if (Inside(diff, 61, 80)) spin = 30;
	if (diff > 80)
	{
		spin = 30;
		rotate = 0;
		SetMeshMaterial("SawmillBlade.Spin", 2);
		running = true;
		Sound("Structures::SawmillRipcut", {loop_count = +1});
		Sound("Sawmill::EngineLoop", {loop_count = +1});
	}

	SetProperty("MeshTransformation", Trans_Mul(Trans_Rotate(-20, 0, 1, 0), Trans_Rotate(rotate, 1, 0, 0)));
	SetAnimationPosition(this.SpinAnimation, Anim_Linear(GetAnimationPosition(this.SpinAnimation), 0, GetAnimationLength("work"), spin, ANIM_Loop));
}

private func SpinOff(int call)
{
	var spin;
	if (!call)
	{
		running = false;
		spin = 50;
		SetMeshMaterial("SawmillBlade", 2);
		Sound("Structures::SawmillRipcut", {loop_count = -1});
		SetProperty("MeshTransformation", Trans_Rotate(-20, 0, 1, 0));
	}
	if (call == 1) spin = 75;
	if (call == 2)
	{
		spin = 100;
		Sound("Sawmill::EngineLoop", {loop_count = -1});
		Sound("Sawmill::EngineStop");
	}
	if (call == 3) spin = 150;
	if (call == 4)
	{
		SetMeshMaterial("SawmillBelt", 1);
		SetAnimationPosition(this.SpinAnimation, Anim_Const(GetAnimationPosition(this.SpinAnimation)));
		return;
	}

	SetAnimationPosition(this.SpinAnimation, Anim_Linear(GetAnimationPosition(this.SpinAnimation), 0, GetAnimationLength("work"), spin, ANIM_Loop));

	ScheduleCall(this, "SpinOff", this.SpinStep * 2, nil, call+1);
}

/*-- Properties --*/

local ActMap = {
		Default = {
			Prototype = Action,
			Name = "Default",
			Procedure = DFA_NONE,
			Directions = 2,
			FlipDir = 1,
			Length = 1,
			Delay = 0,
			FacetBase = 1,
			NextAction = "Default",
		},
};

func Definition(def) 
{
	SetProperty("PictureTransformation", Trans_Mul(Trans_Translate(2000, 0, 7000), Trans_Rotate(-20, 1, 0, 0),Trans_Rotate(30, 0, 1, 0)), def);
	return _inherited(def, ...);
}
local Name = "$Name$";
local Description = "$Description$";
local SpinStep = 30;
local ContainBlast = true;
local BlastIncinerate = 100;
local FireproofContainer = true;
local HitPoints = 70;
local Components = {Rock = 4, Wood = 1};