File: Script.c

package info (click to toggle)
openclonk 8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 169,500 kB
  • sloc: cpp: 180,478; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 139; sh: 101; javascript: 34
file content (267 lines) | stat: -rw-r--r-- 5,363 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
/**
	Dynamite
	A volatile tool that can be pressed into wallsfor accurate mining, burning a short fuse before exploding.
	
	@author: Newton
*/

/*-- Engine Callbacks --*/

// time in frames until explosion
local FuseTime = 140;

func Hit()
{
	Sound("Hits::GeneralHit?");
}

func Incineration(int caused_by)
{
	Extinguish();
	Fuse();
	SetController(caused_by);
}

public func Entrance(object new_container)
{
	// Collection back into dynamite box: Kill any connected fuses
	if (new_container && new_container->~IsDynamiteBox())
	{
		var fuses = FindObjects(Find_Category(C4D_StaticBack), Find_Func("IsFuse"), Find_ActionTargets(this));
		if (GetLength(fuses) >= 2)
		{
			// Two fuses? Then bridge over this stick
			var t1 = fuses[0]->GetConnectedItem(this);
			var t2 = fuses[1]->GetConnectedItem(this);
			fuses[0]->Connect(t1, t2);
			fuses[0] = nil;
		}
		for (var fuse in fuses) if (fuse) fuse->RemoveObject();
	}
}

/*-- Callbacks --*/

public func OnCannonShot(object cannon)
{
	Fuse();
}

// Drop fusing dynamite on death to prevent explosion directly after respawn
public func IsDroppedOnDeath(object clonk)
{
	return (GetAction() == "Fuse");
}

public func IsFusing()
{
	return GetAction() == "Fuse";
}

// Called by the Dynamite box
public func SetReady()
{
	SetAction("Ready");
}
// Called by the Dynamite box
public func SetFuse()
{
	SetAction("Fuse");
	// Object can't be collected anymore when it fuses.
	this.Collectible = false;
}

public func Reset()
{
	SetAction("Idle");
	// Object can be collected again.
	this.Collectible = true;
}

public func OnFuseFinished(object fuse)
{
	SetController(fuse->GetController());
	DoExplode();
}

public func IsInfiniteStackCount()
{
	return false;
}

public func IsGrenadeLauncherAmmo() { return true; }

/*-- Usage --*/

public func ControlUse(object clonk, int x, int y)
{
	return ControlPlace(clonk, x, y, GetLength(FindFuses()));
}

private func FindFuses()
{
	return FindObjects(Find_Category(C4D_StaticBack), Find_Func("IsFuse"), Find_ActionTargets(this));
}

public func ControlPlace(object clonk, int x, int y, bool box)
{
	// if already activated, nothing (so, throw)
	if(GetAction() == "Fuse" || box)
	{
		if(Place(clonk,x,y,box))
			return true;
		// if placed with the box, we are more tolerant where the
		// user clicks and search for other positions too
		else if(box)
		{

			// get rough direction (left, right, up down)
			var angle = (Angle(0,0,x,y)+45)/90*90;

			var plusminus = -1;
			// first check if it is possible to place the dynamite
			// in roughly the same direction as he clicked, then
			// in each left or right of that direction and then
			// in the opposite direction.
			for(var i=0; i<=3; ++i)
			{

				angle += plusminus * i * 90;
				x = Sin(angle, 300);
				y = -Cos(angle, 300);

				if(Place(clonk,x,y,box))
					return true;

				plusminus *= -1;
			}
			
		}
	}
	else
	{
		Fuse();
		return true;
	}
	return false;
}

public func Fuse()
{
	if (GetAction() != "Fuse")
	{
		if (!GetLength(FindFuses())) 
			Sound("Fire::Fuse");
		SetAction("Fuse");
		// Object can't be collected anymore when it fuses.
		this.Collectible = false;	
	}
}

func Place(object clonk, int x, int y, bool box)
{
	var angle = Angle(0,0,x,y);
	var pos = GetWall(angle);
	if(pos)
	{
		if(box) SetReady();
		
		// put into ...
		Sound("Objects::Connect");
		Exit(pos[0], pos[1], Angle(pos[0],pos[1]));
		SetPosition(clonk->GetX()+pos[0], clonk->GetY()+pos[1]);
		return true;
	}
	return false;
}

// returns true if there is a wall in direction in which "clonk" looks
// and puts the offset to the wall into "xo, yo" - looking from the clonk
func GetWall(int angle)
{
	var dist = 12;
	for (var dist = 12; dist < 18; dist++)
	{
		var x = Sin(angle, dist);
		var y = -Cos(angle, dist);
		if (GBackSolid(x, y))
			return [Sin(angle, dist-5), -Cos(angle, dist-5)];
	}
	return false;
}

func Fusing()
{
	var x = Sin(GetR(), 5);
	var y = -Cos(GetR(), 5);

	if (Contained()!=nil)
	{
		//If the dynamite is held, sparks come from clonk's center.
		x = y = 0;
	}

	// Effect: fire particles.
	if (GetActTime() < FuseTime - 20)
		CreateParticle("Fire", x, y, PV_Random(x - 5, x + 5), PV_Random(y - 15, y + 5), PV_Random(10, 40), Particles_Glimmer(), 3);
	// Explosion: after fusetime is over.
	else if (GetActTime() > FuseTime)
		DoExplode();
	return;
}


public func DoExplode()
{
	// Activate all fuses.
	for (var obj in FindFuses())
		obj->~StartFusing(this);
	Explode(26);
}

public func IsExplosive() { return true; }

/*-- Scenario saving --*/
// Do not save within dynamite box - will be handled by box

public func SaveScenarioObject(props, ...)
{
	var c = Contained();
	if (c && c->~IsDynamiteBox()) return false;
	return inherited(props, ...);
}

/*-- Production --*/

public func IsChemicalProduct() { return true; }

/*-- Properties --*/

local ActMap = {
	Fuse = {
		Prototype = Action,
		Name = "Fuse",
		Procedure = DFA_NONE,
		NextAction = "Fuse",
		Delay = 1,
		Length = 1,
		FacetBase = 1,
		Sound = "Fire::FuseLoop",
		StartCall = "Fusing"
	},
	Ready = {
		Prototype = Action,
		Name = "Ready",
		Procedure = DFA_NONE,
		NextAction = "Ready",
		Delay = 1,
		Length = 1,
		FacetBase = 1,
	}
};
local Name = "$Name$";
local Description = "$Description$";
local Collectible = true;
local BlastIncinerate = 1;
local ContactIncinerate = 1;
local Components = {Coal = 1, Firestone = 1};