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 (119 lines) | stat: -rw-r--r-- 2,873 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
/*
	ObjectFade Rule
	Author: Caesar

	Removes unused objects. Like Hazard-Arena.
	
	TODO:
	Use ChangeEffect properly to save calls
*/

local fade_time = 18;

public func Activate(int by_plr)
{
	MessageWindow(this.Description, by_plr);
	return true;
}

public func DoFadeTime(int to_add)
{
	fade_time += to_add;
	return;
}

protected func Initialize() 
{
	if(ObjectCount(Find_ID(Rule_ObjectFade), Find_Exclude(this))) {
		FindObject(Find_ID(Rule_ObjectFade), Find_Exclude(this))->DoFadeTime(36);
		return RemoveObject();
	}
	AddTimer("Timer");
}

protected func Timer() 
{
	for (var fade in FindObjects(Find_Category(C4D_Object), Find_NoContainer(), Find_Not(Find_OCF(OCF_HitSpeed1)))) 
	{
		if (!CheckFadeConditions(fade)) continue;
		if (GetEffect("IntFadeOut*", fade)) continue;
		AddEffect("IntFadeOutCandidate", fade, 1, 36, this, Rule_ObjectFade);
	}
}

public func FxIntFadeOutCandidateTimer(object target, effect, int time) 
{
	// Re-check condition a few times
	if (!CheckFadeConditions(target)) return FX_Execute_Kill;
	if (time > 300)
	{
		// OK - now fade out
		AddEffect("IntFadeOut", target, 100, 1, this, Rule_ObjectFade);
		return FX_Execute_Kill;
	}
	return FX_OK;
}

public func FadeOutObject(object target)
{
	// Definition or hooked call: Fade out an object (even if rule is not active)
	return AddEffect("IntFadeOut", target, 100, 1, nil, Rule_ObjectFade);
}

func CheckFadeConditions(object fade)
{
	// Moving objects should not.
	// Allow small movement because engine physics bug sometimes cause objects to wiggle forever
	if (Distance(fade->GetXDir(), fade->GetYDir()) > 4) return false;
	// No InEarth objects
	if (fade->GBackSolid()) return false;
	// No objects that explicitely want to stay
	if (fade->~HasNoFadeOut()) return false;
	// Not if picked up
	if (fade->Contained()) return false;
	// Fade out OK
	return true;
}


public func FxIntFadeOutStart(object target, effect) 
{
	effect.x = target->GetX();
	effect.y = target->GetY();
	effect.color = target->GetClrModulation() & 0x00ffffff; //Safe pure rgb
	effect.alpha = target->GetClrModulation() >> 24 & 255; //Safe alpha
}

public func FxIntFadeOutTimer(object target, effect, int time) 
{
	if (time < fade_time * 2/3) 
		return;

	if (!(target->Contained()) && effect.x == target->GetX() && effect.y == target->GetY())
	{
		if(time >= fade_time) 
		{
			target->RemoveObject();
			return -1;
		}
	}
	else 
	{
		target->SetClrModulation(effect.alpha << 24 | effect.color);
		return -1;
	}

	target->SetClrModulation(((fade_time - time) * effect.alpha / (fade_time/3)) << 24 | effect.color);
	return 1;
}

public func FxIntFadeOutTimerEffect(string new_effect_name) 
{
	if (new_effect_name == "IntFadeOut")
		return -1;
}

local Name = "Object Fade";
local Description = "$Description$";
local Visibility = VIS_Editor;
local EditorPlacementLimit = 1; // Rules are to be placed only once