File: Explode.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 (599 lines) | stat: -rw-r--r-- 18,054 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
	Explode.c
	Everything about the explosion.
	
	@author Newton
*/

// Particle definitions used by the explosion effect.
// They will be initialized lazily whenever the first blast goes off.
static ExplosionParticles_Smoke;
static ExplosionParticles_Blast;
static ExplosionParticles_BlastSmooth;
static ExplosionParticles_BlastSmoothBackground;
static ExplosionParticles_Star;
static ExplosionParticles_Shockwave;
static ExplosionParticles_Glimmer;

global func ExplosionParticles_Init()
{
	ExplosionParticles_Smoke =
	{	    
		Size = PV_KeyFrames(0, 180, 25, 1000, 50),
	    DampingY = PV_Random(890, 920, 5),
		DampingX = PV_Random(900, 930, 5),
		ForceY=-1,
		ForceX = PV_Wind(20, PV_Random(-2, 2)),
		Rotation=PV_Random(0,360,0),
		R=PV_KeyFrames(0, 0, 255, 260, 64, 1000, 64),
		G=PV_KeyFrames(0, 0, 128,  260, 64, 1000, 64),
		B=PV_KeyFrames(0, 0, 0, 260, 108, 1000, 108),
	    Alpha = PV_KeyFrames(0, 0, 0, 100, 20, 500, 20, 1000, 0)
	};
	
	ExplosionParticles_Blast =
	{
		Size = PV_KeyFrames(0, 0, 0, 260, 25, 1000, 40),
		DampingY = PV_Random(890, 920, 0),
		DampingX = PV_Random(900, 930, 0),
		ForceY = PV_Random(-8,-2,0),
		ForceX = PV_Random(-5,5,0),
		R = 255,
		G = PV_Random(64, 120, 0),
		Rotation = PV_Random(0, 360, 0),
		B = 0,
		Alpha = PV_KeyFrames(0, 260, 100, 1000, 0),
		BlitMode = GFX_BLIT_Additive,
		Phase = PV_Random(0, 1)
	};
	
	ExplosionParticles_BlastSmooth =
	{
		Size = PV_KeyFrames(0, 0, 0, 250, PV_Random(30, 50), 1000, 80),
		R = PV_KeyFrames(0, 0, 255, 250, 128, 1000, 0),
		G = PV_KeyFrames(0, 0, 255, 125, 64, 1000, 0),
		Rotation = PV_Random(0, 360, 0),
		B = PV_KeyFrames(0, 0, 100, 250, 64, 100, 0),
		Alpha = PV_KeyFrames(0, 0, 255, 750, 250, 1000, 0),
		BlitMode = GFX_BLIT_Additive
	};
	
	ExplosionParticles_BlastSmoothBackground =
	{
		Prototype = ExplosionParticles_BlastSmooth,
		BlitMode = nil,
		R = PV_Linear(50, 0),
		G = PV_Linear(50, 0),
		B = PV_Linear(50, 0),
		Alpha = PV_Linear(128, 0)
	};
	
	ExplosionParticles_Star =
    {
        Size = PV_KeyFrames(0, 0, 0, 500, 60, 1000, 0),
        R = PV_KeyFrames(0, 750, 255, 1000, 0),
        G = PV_KeyFrames(0, 300, 255, 1000, 0),
        B = PV_KeyFrames(0, 300, 255, 500, 0),
		Rotation = PV_Random(0, 360, 4),
        Alpha = PV_KeyFrames(0, 750, 255, 1000, 0),
		BlitMode = GFX_BLIT_Additive,
		Stretch = PV_Speed(1000, 1000)
    };
    
	ExplosionParticles_Shockwave =
    {
        Size = PV_Linear(0, 120),
        R = 255,
        G = 128,
        B = PV_KeyFrames(0, 0, 128, 200, 0),
		Rotation = PV_Step(20),
		BlitMode = GFX_BLIT_Additive,
        Alpha = PV_Linear(255, 0)
    };
    
    ExplosionParticles_Glimmer=
	{
		Size = PV_Linear(2, 0),
	    ForceY = GetGravity(),
		DampingY = PV_Linear(1000,700),
		DampingX = PV_Linear(1000,700),
		Stretch = PV_Speed(1000, 500),
		Rotation = PV_Direction(),
		OnCollision = PC_Die(),
		CollisionVertex = 500,
	    R = 255,
	    G = PV_Linear(128,32),
	    B = PV_Random(0, 128, 2),
	    Alpha = PV_Random(255,0,3),
		BlitMode = GFX_BLIT_Additive,
	};
}

/*-- Explosion --*/

// documented in /docs/sdk/script/fn
global func Explode(int level, bool silent, int damage_level)
{
	if(!this) FatalError("Function Explode must be called from object context");
	
	// Special: Implode
	if (level <= 0) return RemoveObject();

	// Shake the viewport.
	ShakeViewport(level);
	
	// Explosion parameters.
	var x = GetX(), y = GetY();
	var cause_plr = GetController();
	var container = Contained();
	var exploding_id = GetID();
	var layer = GetObjectLayer();

	// Explosion parameters saved: Remove object now, since it should not be involved in the explosion.
	RemoveObject();

	// Execute the explosion in global context.
	// There is no possibility to interact with the global context, apart from GameCall.
	// So at least remove the object context.
	exploding_id->DoExplosion(x, y, level, container, cause_plr, layer, silent, damage_level);
}

global func DoExplosion(int x, int y, int level, object inobj, int cause_plr, object layer, bool silent, int damage_level)
{
	// Zero-size explosion doesn't affect anything
	if (level <= 0) return;
	// If the object is contained, loop through all parent containers until the blast is contained or no container is found.
	// Blast the containers it passes through and the objects inside them.
	var container = inobj;
	var prev_container = nil;
	while (container)
	{
		// Determine first whether the container has a parent and if it contains the blast.
		// This is needed because the container might be exploded 
		var contains_blast = container.ContainBlast;
		var parent_container = container->Contained();
		// Blast the current container, but not the previous container.
		BlastObjects(x + GetX(), y + GetY(), level, container, cause_plr, damage_level, layer, prev_container);
		// Break the blasting if this container contains the blast.
		if (contains_blast)
			break;
		// Move one container up if possible.
		prev_container = container;
		container = parent_container;
	}
	
	// Explosion outside: Explosion effects.
	if (!container)
	{
		// Blast objects outside if there was no final container containing the blast.
		BlastObjects(x + GetX(), y + GetY(), level, container, cause_plr, damage_level, layer, prev_container);
		// Incinerate oil.
		if (!IncinerateLandscape(x, y, cause_plr))
			if (!IncinerateLandscape(x, y - 10, cause_plr))
				if (!IncinerateLandscape(x - 5, y - 5, cause_plr))
					IncinerateLandscape(x + 5, y - 5, cause_plr);
		// Graphic effects.
		Call("ExplosionEffect", level, x, y, 0, silent, damage_level);
		// Landscape destruction. Happens after BlastObjects, so that recently blown-free materials are not affected
		BlastFree(x, y, level, cause_plr);
	}

	return true;
}

/*
Creates a visual explosion effect at a position.
smoothness (in percent) determines how round the effect will look like.

Defined in Objects.ocd/System.ocg because it depends on particles/definitions to be loaded.
*/
global func ExplosionEffect(...)
{
	return _inherited(...);
}

/*-- Blast objects & shockwaves --*/

// Damage and hurl objects away.
// documented in /docs/sdk/script/fn
global func BlastObjects(int x, int y, int level, object container, int cause_plr, int damage_level, object layer, object prev_container)
{
	var obj;
	
	// Coordinates are always supplied globally, convert to local coordinates.
	var l_x = x - GetX(), l_y = y - GetY();
	
	// caused by: if not specified, controller of calling object
	if (cause_plr == nil)
		if (this)
			cause_plr = GetController();
	
	// damage: if not specified this is the same as the explosion radius
	if (damage_level == nil)
		damage_level = level;
	
	// In a container?
	if (container)
	{
		if (container->GetObjectLayer() == layer)
		{
			container->BlastObject(damage_level, cause_plr);
			if (!container)
				return true; // Container could be removed in the meanwhile.
			for (obj in FindObjects(Find_Container(container), Find_Layer(layer), Find_Exclude(prev_container)))
				if (obj)
					obj->BlastObject(damage_level, cause_plr);
		}
	}
	else
	{
		// Object is outside.
		var at_rect = Find_AtRect(l_x - 5, l_y - 5, 10, 10);
		// Damage objects at point of explosion.
		for (var obj in FindObjects(at_rect, Find_NoContainer(), Find_Layer(layer), Find_Exclude(prev_container)))
			if (obj) obj->BlastObject(damage_level, cause_plr);
			
		// Damage objects in radius.
		for (var obj in FindObjects(Find_Distance(level, l_x, l_y), Find_Not(at_rect), Find_NoContainer(), Find_Layer(layer), Find_Exclude(prev_container)))
			if (obj) obj->BlastObject(damage_level / 2, cause_plr);

		
		// Perform the shockwave at the location where the top level container previously was.
		// This ensures reliable flint jumps for explosives that explode inside a crew member.
		var off_x = 0, off_y = 0;
		if (prev_container)
		{	
			var max_offset = 300;
			off_x = BoundBy(-prev_container->GetXDir(100), -max_offset, max_offset);
			off_y = BoundBy(-prev_container->GetYDir(100), -max_offset, max_offset);			
		}
		DoShockwave(x, y, level, cause_plr, layer, off_x, off_y);
	}
	// Done.
	return true;
}

global func BlastObject(int level, int caused_by)
{
	var self = this;
	if (caused_by == nil)
		caused_by = GetController();

	DoDamage(level, FX_Call_DmgBlast, caused_by);
	if (!self) return;

	if (GetAlive())
		DoEnergy(-level, false, FX_Call_EngBlast, caused_by);
	if (!self) return;

	if (this.BlastIncinerate && GetDamage() >= this.BlastIncinerate)
		Incinerate(level, caused_by);
	return;
}

// documented in /docs/sdk/script/fn
global func DoShockwave(int x, int y, int level, int cause_plr, object layer, int off_x, int off_y)
{
	// Zero-size shockwave
	if (level <= 0) return;
	
	// Coordinates are always supplied globally, convert to local coordinates.
	var l_x = x - GetX(), l_y = y - GetY();
	
	// caused by: if not specified, controller of calling object
	if (cause_plr == nil)
		if (this)
			cause_plr = GetController();

	// Hurl objects in explosion radius.
	var shockwave_objs = FindObjects(Find_Distance(level, l_x, l_y), Find_NoContainer(), Find_Layer(layer),
		Find_Or(Find_Category(C4D_Object|C4D_Living|C4D_Vehicle), Find_Func("CanBeHitByShockwaves", cause_plr)), Find_Func("DoShockwaveCheck", x, y, cause_plr));
	var cnt = GetLength(shockwave_objs);
	if (cnt)
	{
		// The hurl energy is distributed over the objects.
		var shock_speed = Sqrt(2 * level * level / BoundBy(cnt, 2, 12));
		for (var obj in shockwave_objs)
		{
			if (obj) // Test obj, cause OnShockwaveHit could have removed objects.
			{
				var cat = obj->GetCategory();
				// Object has special reaction on shockwave?
				if (obj->~OnShockwaveHit(level, x, y, cause_plr))
					continue;
				// Killtracing for projectiles.
				if (cat & C4D_Object)
					obj->SetController(cause_plr);
				// Shockwave.
				var mass_fact = 20, mass_mul = 100;
				if (cat & C4D_Living)
				{
					mass_fact = 8;
					mass_mul = 80;
				}
				mass_fact = BoundBy(obj->GetMass() * mass_mul / 1000, 4, mass_fact);
				// Determine difference between object and explosion center, take into account the offset.
				var dx = 100 * (obj->GetX() - x) - off_x + Random(51) - 25;
				var dy = 100 * (obj->GetY() - y) - off_y + Random(51) - 25;
				var vx, vy;
				if (dx)
					vx = Abs(dx) / dx * (100 * level - Abs(dx)) * shock_speed / level / mass_fact;
				vy = (Abs(dy) - 100 * level) * shock_speed / level / mass_fact;
				if (cat & C4D_Object)
				{
					// Objects shouldn't move too fast.
					var ovx = obj->GetXDir(100), ovy = obj->GetYDir(100);
					if (ovx * vx > 0)
						vx = (Sqrt(vx * vx + ovx * ovx) - Abs(vx)) * Abs(vx) / vx;
					if (ovy * vy > 0)
						vy = (Sqrt(vy * vy + ovy * ovy) - Abs(vy)) * Abs(vy) / vy;
				}
				obj->Fling(vx, vy, 100, true);
			}
		}
	}
	return;
}

global func DoShockwaveCheck(int x, int y, int cause_plr)
{
	var def = GetID();
	// Some special cases, which won't go into FindObjects.
	if (def->GetDefHorizontalFix())
		return false;
	// Only move vehicles and floating objects which can be grabbed and pushed (Touchable = 1).	
	if (this.Touchable != 1)
	{
		if (GetCategory() & C4D_Vehicle)
			return false;
		if (GetProcedure() == "FLOAT")
			return false;
	}
	// Projectiles not when they fly downwards or are exactly in the explosion point.
	// This will catch the most cases in which multiple clonks throw flints at the same time.
	if (GetCategory() & C4D_Object)
	{
		if (GetX() == x && GetY() == y) return false;
		if (GetYDir() > 5) return false;
	}
	// No stuck objects.
	if (Stuck())
		return false;
	return true;
}


/** Shake the player viewports near the given position. This disorienting effect is used for earthquakes, explosions
and other rumbling effects. The further away the player is from the source, the less his viewport is shaken. The 
strength falls off linearly by distance from 100% to 0% when the player is 700 pixels away.

@param level strength of the shake in pixels. As a point of reference, for explosions, the shake strength is the same
             as the explosion level.
@param x_off x offset in relative coordinates from the calling object
@param y_off y offset in relative coordinates from the calling object
@param range range of clonk to explosion at which shaking falls off to 0%
*/
// documented in /docs/sdk/script/fn
global func ShakeViewport(int level, int x_off, int y_off, range)
{
	if (level <= 0)
		return false;
		
	x_off += GetX();
	y_off += GetY();
	
	AddEffect("ShakeViewport", nil, 300, 1, nil, nil, level, x_off, y_off, range ?? 700);
}

global func FxShakeViewportEffect(string new_name)
{
	// there is only one global ShakeViewport effect which manages all the shake positions and strengths
	if (new_name == "ShakeViewport")
		return -2;
	return;
}

global func FxShakeViewportStart(object target, effect e, int temporary, level, xpos, ypos, range)
{
	if(temporary != 0) return;
	
	e.shakers = CreateArray();
	e.shakers[0] = { x = xpos, y = ypos, strength = level, time = 0, range = range };
}

global func FxShakeViewportAdd(object target, effect e, string new_name, int new_timer, level, xpos, ypos, range)
{
	e.shakers[GetLength(e.shakers)] = { x = xpos, y = ypos, strength = level, time = e.Time, range = range };
}

global func FxShakeViewportTimer(object target, effect e, int time)
{
	// shake for all players
	for (var i = 0; i < GetPlayerCount(); i++)
	{
		var plr = GetPlayerByIndex(i);
		var cursor = GetCursor(plr);
		if (!cursor)
			continue;

		var totalShakeStrength = 0;
		for(var shakerIndex = 0; shakerIndex < GetLength(e.shakers); ++shakerIndex)
		{
			var shaker = e.shakers[shakerIndex];
			var shakerTime = time - shaker.time;

			// shake strength lowers as a function of the distance
			var distance = Distance(cursor->GetX(), cursor->GetY(), shaker.x, shaker.y);
			var maxDistance = shaker.range;
			var level = shaker.strength * BoundBy(100-100*distance/maxDistance,0,100)/100;

			// calculate total shake strength by adding up all shake positions in the player's vicinity
			totalShakeStrength += level / Max(1,shakerTime*2/3) - shakerTime**2 / 400;
		}
		SetViewOffset(plr, Sin(time * 100, totalShakeStrength), Cos(time * 100, totalShakeStrength));
	}

	// remove shakers that are done shaking
	for(var shakerIndex = 0; shakerIndex < GetLength(e.shakers); ++shakerIndex)
	{
		var shaker = e.shakers[shakerIndex];
		var shakerTime = time - shaker.time;
		if (shaker.strength / Max(1,shakerTime*2/3) - shakerTime**2 / 400 <= 0)
			e.shakers[shakerIndex] = nil;
	}
	RemoveHoles(e.shakers);
	
	// no shakers left: remove this effect
	if(GetLength(e.shakers) == 0)
	{
		return -1;
	}
}

global func FxShakeViewportStop()
{
	for (var i = 0; i < GetPlayerCount(); i++)
	{
		SetViewOffset(GetPlayerByIndex(i), 0, 0);
	}
}

/*-- Smoke trails --*/

global func CreateSmokeTrail(int strength, int angle, int x, int y, int color, bool noblast)
{
	var e = AddEffect("SmokeTrail", nil, 300, 1, nil, nil, color);
	e.x = 100*(GetX() + x);
	e.y = 100*(GetY() + y);
	e.strength = strength;
	e.curr_strength = strength;
	e.noblast = noblast;
	e.angle = angle;
}

global func FxSmokeTrailStart(object target, proplist e, int temp, int color)
{
	if (temp)
		return;

	e.color = color ?? RGBa(255, 128, 0, 200);
	var alpha = (e.color >> 24) & 0xff;
	e.particles_smoke =
	{
		R = PV_Linear(255, 64),
		G = PV_Linear(255, 64),
		B = PV_Linear(255, 64),
		Alpha = PV_KeyFrames(0, 0, alpha/4, 200, alpha, 1000, 0),
		Rotation = PV_Random(-45,45),
		ForceX = PV_Wind(20),
		ForceY = PV_Gravity(-10),
	};
	
	e.particles_blast =
	{
		R = PV_Linear((e.color >> 16) & 0xff, 0),
		G = PV_Linear((e.color >>  8) & 0xff, 0),
		B = PV_Linear((e.color >>  0) & 0xff, 0),
		Alpha = PV_KeyFrames(0, 0, alpha, 500, 3*alpha/4, 1000, 0),
		Rotation = PV_Direction(),
		BlitMode = GFX_BLIT_Additive,
		Stretch = PV_Speed(1500, 1000)
	};
}

global func FxSmokeTrailTimer(object target, effect e, int fxtime)
{
	var strength = e.strength;
	e.curr_strength = e.curr_strength * 94 / 100;
	
	var str = e.curr_strength;
	
	var initial_speed = 100 * (strength+20)/6;
	var speed = initial_speed * str / strength;
	var angle = e.angle + RandomX(-20,20);
	var x_dir = Sin(angle, speed);
	var y_dir = -Cos(angle , speed);
	
	if (speed < 2*100) return -1;
	
	// gravity
	y_dir += GetGravity() * 15;
	
	// draw
	e.particles_smoke.Size = PV_KeyFrames(0, 0, str / 2, 1000, str);
	e.particles_blast.Size = PV_KeyFrames(0, 0, 0, 200, str / 3, 1000, 0);

	CreateParticle("SmokeThick", e.x/100, e.y/100, RandomX(-1,1), RandomX(-1,1), 50, e.particles_smoke,1);
		
	// then calc next position
	e.x += x_dir;
	e.y += y_dir;
	
	if (!e.noblast)
	{
		var x_dir_blast = x_dir / 200;
		var y_dir_blast = y_dir / 200;
		CreateParticle("SmokeDirty", e.x/100, e.y/100, PV_Random(x_dir_blast - 2, x_dir_blast + 2), PV_Random(y_dir_blast - 2, y_dir_blast + 2), 10, e.particles_blast, 1);
	}
	
	
	if (GBackSemiSolid(e.x/100, e.y/100))
		return -1;
	
	e.curr_strength = str;
}

/*-- Fireworks --*/

global func Fireworks(int color, int x, int y)
{
	if (!color)
		color = HSL(Random(8) * 32, 255, 160);
	
	var flash =
	{
		Prototype = Particles_Flash(),
		R = (color >> 16) & 0xff,
		G = (color >>  8) & 0xff,
		B = (color >>  0) & 0xff,
		Size = PV_KeyFrames(0, 0, 0, 100, 200, 1000, 0),
	};
	
	var glimmer = 
	{
		R = (color >> 16) & 0xff,
		G = (color >>  8) & 0xff,
		B = (color >>  0) & 0xff,
		DampingY = PV_Linear(950,800),
		DampingX = PV_Linear(950,800),
		Stretch = PV_Speed(3000, 500),
		Size = PV_Linear(2, 1),
		ForceY = PV_Gravity(50),
		Rotation = PV_Direction(),
		OnCollision = PC_Die(),
		CollisionVertex = 500,
		Alpha = PV_Random(255,0,3),
		BlitMode = GFX_BLIT_Additive,
	};
	
	var start_angle = Random(360);
	
	var num = 25;
	for(var i=0; i<num; ++i)
	{
		for(var j=0; j<num; ++j)
		{
			var speed = 1000;
			var angle = start_angle + i*360/num + Random(15) + j*15;
			var speed3d = Cos(j*90/num - Random(15),speed);
			var xdir = Sin(angle, speed3d/10);
			var ydir = -Cos(angle,speed3d/10);
		
			CreateParticle("MagicFire", x, y, xdir, ydir, PV_Random(50, 200), glimmer, 1);
		}
	}
	
	CreateParticle("Flash", x, y, 0, 0, 20, flash);
}