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 (322 lines) | stat: -rw-r--r-- 8,641 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
316
317
318
319
320
321
322
/**
	Grapple Rope
	The rope used for grappling devices. Connect(obj1, obj2) connects two objects,
	BreakRope() breaks the rope, DrawIn() draws the hook in.
	
	@author Randrian
*/

#include Library_Rope

local has_hook_anchored;
local last_point;

// Call this to break the rope.
public func BreakRope()
{
	if (lib_rope_length == -1)
		return;
	lib_rope_length = -1;
	var act1 = lib_rope_objects[0][0];
	var act2 = lib_rope_objects[1][0];
	SetAction("Idle");
	// Notify action targets.
	if (act1 != nil)
		act1->~OnRopeBreak();
	if (act2 != nil)
		act2->~OnRopeBreak();
	RemoveRope();
	RemoveObject();
	return;
}

/*-- Rope Callbacks --*/

// From the rope library: to be overloaded for special segment behaviour.
private func CreateSegment(int index, object previous)
{
	if (index == 0)
		return;
	var segment;
	segment = CreateObjectAbove(GrappleRope);
	return segment;
}

/*-- Rope connecting --*/

// Connects two objects to the rope, but the length will vary on their positions.
public func Connect(object obj1, object obj2)
{
	StartRopeConnect(obj1, obj2);
	SetMaxLength(100);
	has_hook_anchored = false;
	SetAction("Hide");
	AddEffect("IntHang", this, 1, 1, this);
	return;
}

public func GetConnectStatus() { return !lib_rope_length_auto; }

// Callback form the hook, when it hits ground.
public func HookAnchored()
{
	has_hook_anchored = true;
}

public func HookRemoved()
{
	var new_hook = CreateObjectAbove(GrappleHook);
	new_hook->New(lib_rope_objects[1][0]->Contained(), this);
	lib_rope_objects[1][0]->SetHook(new_hook);
	lib_rope_objects[0][0] = new_hook;
	DrawIn();
}

/* Callback form the rope library */
public func MaxLengthReached()
{
	var clonk = lib_rope_objects[1][0];
	if (clonk->Contained()) 
		clonk = clonk->Contained();
	if (!has_hook_anchored)
	{
		for (var i = 0; i < lib_rope_particle_count; i++)
		{
			lib_rope_particles[i].oldx = lib_rope_particles[i].x;
			lib_rope_particles[i].oldy = lib_rope_particles[i].y;
		}
		DrawIn(true);
	}
}

// Adjust speed on swinging.
public func DoSpeed(int value)
{
	var speed = lib_rope_particles[-1].x - lib_rope_particles[-1].oldx;
	if (speed * value > 0) 
		value += speed / 10;
	lib_rope_particles[-1].oldx -= value;
}

public func FxIntHangTimer() { TimeStep(); }

public func FxDrawInTimer()
{
	if (lib_rope_length < 15)
	{
		BreakRope();
		return -1;
	}
	DoLength(-5);
	if (!has_hook_anchored)
	{
		for (var i = 0; i < lib_rope_particle_count; i++)
		{
			lib_rope_particles[i].oldx = lib_rope_particles[i].x;
			lib_rope_particles[i].oldy = lib_rope_particles[i].y;
		}
		DoLength(-3);
	}
}

public func DrawIn(bool no_control)
{
	if (!GetEffect("DrawIn", this))
	{
		AddEffect("DrawIn", this, 1, 1, this);
		SetFixed(false, true);
		lib_rope_objects[0][0]->SetXDir();
		lib_rope_objects[0][0]->SetYDir();
		ConnectPull();
		var clonk = lib_rope_objects[1][0];
		if (clonk->Contained()) 
			clonk = clonk->Contained();
		if (!no_control) 
		{
			// Make sure to only remove control effects for this rope.
			var control_effect = GetEffect("IntGrappleControl", clonk);
			if (control_effect && control_effect.CommandTarget->GetRope() == this)
				RemoveEffect(nil, clonk, control_effect);
		}
	}
}

public func AdjustClonkMovement()
{
	var clonk = lib_rope_objects[1][0];
	if (clonk->Contained()) 
		clonk = clonk->Contained();
	
	var rope_vector = Vec_Sub([lib_rope_particles[-1].x, lib_rope_particles[-1].y], [lib_rope_particles[-2].x, lib_rope_particles[-2].y]);
	var clonk_speed = [clonk->GetXDir(LIB_ROPE_Precision), clonk->GetYDir(LIB_ROPE_Precision)];

	var rope_orthogonal = [rope_vector[1], -rope_vector[0]];
	var new_speed = Vec_Dot(rope_orthogonal, clonk_speed)/Vec_Length(rope_orthogonal);
	
	var clonk_newspeed = Vec_Normalize(rope_orthogonal, new_speed);

	clonk->SetXDir(clonk_newspeed[0], LIB_ROPE_Precision);
	clonk->SetYDir(clonk_newspeed[1], LIB_ROPE_Precision);
}

public func UpdateLines()
{
	var oldangle;
	for (var i = 1; i < lib_rope_particle_count; i++)
	{
		// Update the Position of the segment
		lib_rope_segments[i]->SetPosition(GetPartX(i), GetPartY(i));

		// Calculate the angle to the previous segment
		var angle = Angle(lib_rope_particles[i].x, lib_rope_particles[i].y, lib_rope_particles[i-1].x, lib_rope_particles[i-1].y);

		// Draw the left line
		var start = [lib_rope_particles[i-1].x, lib_rope_particles[i-1].y];
		var end   = [lib_rope_particles[i].x, lib_rope_particles[i].y];

		if (i == 1 && lib_rope_particle_count > 2)
		{		
			angle = Angle(lib_rope_particles[2].x, lib_rope_particles[2].y, lib_rope_particles[0].x, lib_rope_particles[0].y);
			end = [lib_rope_particles[0].x, lib_rope_particles[0].y];
			end[0] += -Sin(angle, 45 * LIB_ROPE_Precision / 10);
			end[1] += +Cos(angle, 45 * LIB_ROPE_Precision / 10);
		}
		
		if (i == 2)
		{
			angle = Angle(lib_rope_particles[2].x, lib_rope_particles[2].y, lib_rope_particles[0].x, lib_rope_particles[0].y);
			start = [lib_rope_particles[0].x, lib_rope_particles[0].y];
			start[0] += -Sin(angle, 45 * LIB_ROPE_Precision / 10);
			start[1] += +Cos(angle, 45 * LIB_ROPE_Precision / 10);
		}
		
		var diff = Vec_Sub(end, start);
		var point = Vec_Add(start, Vec_Div(diff, 2));
		var length = Vec_Length(diff) * 1000 / LIB_ROPE_Precision / 10;
	
		if (i == lib_rope_particle_count - 1)
		{
			var old = [lib_rope_particles[i - 2].x, lib_rope_particles[i - 2].y];
			var old_diff = Vec_Sub(start, old);
			var o_length = Vec_Length(old_diff) * 1000 / LIB_ROPE_Precision / 10;
			if (!o_length) 
				diff = old_diff;
			else 
				diff = Vec_Div(Vec_Mul(old_diff, length), o_length);
			point = Vec_Add(start, Vec_Div(diff, 2));
			last_point = point;
		}

		var diffangle = Angle(diff[0], diff[1], 0, 0);
		
		if (i == 1)
		{
			lib_rope_segments[i]->SetGraphics(nil, GrappleHook);
			lib_rope_segments[i].MeshTransformation = Trans_Mul(Trans_Translate(0, -7000, 0), Trans_Scale(1500), Trans_Rotate(angle, 0, 0, 1));
			point[0] += -Cos(diffangle, 15 * LIB_ROPE_Precision / 10) + Sin(diffangle, 4 * LIB_ROPE_Precision);
			point[1] += -Cos(diffangle, 4 * LIB_ROPE_Precision) - Sin(diffangle, 15 * LIB_ROPE_Precision / 10);
			length = 1000;
		}
		
		// Only apply line transform to the rope segments and not to the hook.
		if (i != 1)
			SetLineTransform(lib_rope_segments[i], -diffangle, point[0] * 10 - GetPartX(i) * 1000, point[1] * 10 - GetPartY(i) * 1000, length);

		// Remember the angle.
		oldangle = angle;
	}
	return;
}

public func GetClonkAngle()
{
	if (lib_rope_particle_count > 3)
		return Angle(lib_rope_particles[-2].x, lib_rope_particles[-2].y, lib_rope_particles[-3].x, lib_rope_particles[-3].y);
	return 0;
}

public func GetClonkPos()
{
	return [lib_rope_particles[-1].x, lib_rope_particles[-1].y];
}

public func GetClonkOff()
{
	return Vec_Sub(GetClonkPos(), last_point);
}

public func SetLineTransform(object obj, int r, int xoff, int yoff, int length, int layer, int mirror_segments)
{
	if (!mirror_segments) 
		mirror_segments = 1;
	var fsin = Sin(r, 1000), fcos = Cos(r, 1000);
	// Set matrix values.
	obj->SetObjDrawTransform (
		+fcos * mirror_segments, +fsin * length / 1000, xoff,
		-fsin * mirror_segments, +fcos * length / 1000, yoff, layer
	);
}

/*-- Library Overloads --*/

public func ForcesOnObjects()
{
	if (!lib_rope_length)
		return;

	var redo = LengthAutoTryCount();
	while (lib_rope_length_auto && redo)
	{
		var speed = Vec_Length(Vec_Sub([lib_rope_particles[-1].x, lib_rope_particles[-1].y], [lib_rope_particles[-1].oldx, lib_rope_particles[-1].oldy]));
		if (lib_rope_length == GetMaxLength())
		{
			if (ObjContact(lib_rope_objects[1][0]))
				speed = 40;
			else
				speed = 100;
		}
		if (speed > 150)
			DoLength(1);
		else if (speed < 50)
			DoLength(-1); // TODO not just obj 1
		else
			redo = 0;
		if (redo > 0) 
			redo --;
	}
	var j = 0;
	if (PullObjects())
	{
		for (var i = 0; i < 2; i++)
		{
			if (i == 1) 
				j = lib_rope_particle_count-1;
			var obj = lib_rope_objects[i][0];
			if (obj == nil || !lib_rope_objects[i][1])
				continue;

			if (obj->Contained())
				obj = obj->Contained();

			if (obj->GetAction() == "Walk" || obj->GetAction() == "Scale" || obj->GetAction() == "Hangle" || obj->GetAction() == "Climb")
				obj->SetAction("Jump");

			var xdir = BoundBy(lib_rope_particles[j].x - lib_rope_particles[j].oldx, -300, 300);
			var ydir = BoundBy(lib_rope_particles[j].y - lib_rope_particles[j].oldy, -300, 300);

			obj->SetXDir(xdir, LIB_ROPE_Precision);
			obj->SetYDir(ydir, LIB_ROPE_Precision);
		}
	}
}

// Only the grappler is stored.
public func SaveScenarioObject() { return false; }

local ActMap = {
		Hide = {
			Prototype = Action,
			Name = "Hide",
		},
};
local Name = "$Name$";