File: Schedule.c

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (116 lines) | stat: -rw-r--r-- 2,952 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
/**
	Schedule.c
	Schedule can be used to execute scripts or functions repetitively with delay.

	@author
*/

// Executes a script repetitively with delay.
// documented in /docs/sdk/script/fn
global func Schedule(object obj, string script, int interval, int repeats)
{
	// Defaults.
	if (!repeats)
		repeats = 1;
	// Create effect.
	var fx = AddEffect("IntSchedule", obj, 1, interval, obj);
	if (!fx)
		return false;
	// Set variables.
	fx.Script = script;
	fx.Repeats = repeats;
	return true;
}

global func FxIntScheduleTimer(object obj, effect fx)
{
	// Just a specific number of repeats.
	var done = --fx.Repeats <= 0;
	// Execute.
	eval(fx.Script);
	// Remove schedule if done.
	if (done)
		return FX_Execute_Kill;
	return FX_OK;
}

// Adds a timed call to an object, replacement of DefCore TimerCall.
global func AddTimer(call_function, int interval)
{
	if (!this)
		return false;
	// Default to one second.
	if (interval == nil)
		interval = 36;
	// Create effect and add function and repeat infinitely.
	var fx = AddEffect("IntScheduleCall", this, 1, interval, this);
	fx.Function = call_function;
	fx.NoStop = true;
	fx.Pars = [];
	return true;
}

// removes a timer from an object that was added earlier with AddTimer. This removes exactly one timer that fits to the name and returns true on success
global func RemoveTimer(call_function /* name or pointer to the timer to remove */)
{
	if(!this)
		return false;
		
	var fx, index = 0;
	while(fx = GetEffect("IntScheduleCall", this, index++))
	{
		if(fx.Function != call_function) continue;
		if(fx.NoStop != true) continue;
		RemoveEffect(nil, this, fx);
		return true;
	}
	
	// not found
	return false;
}

// Executes a function repetitively with delay.
// documented in /docs/sdk/script/fn
global func ScheduleCall(object obj, call_function, int interval, int repeats, par0, par1, par2, par3, par4)
{
	// Defaults.
	if (!repeats)
		repeats = 1;
	// Create effect.
	var fx = AddEffect("IntScheduleCall", obj, 1, interval, obj);
	if (!fx)
		return false;
	// Set variables.
	fx.Function = call_function;
	fx.Repeats = repeats;
	fx.Pars = [par0, par1, par2, par3, par4];
	return true;
}

global func FxIntScheduleCallTimer(object obj, effect fx)
{
	// Just a specific number of repeats.
	var done = --fx.Repeats <= 0;
	// Execute.
	Call(fx.Function, fx.Pars[0], fx.Pars[1], fx.Pars[2], fx.Pars[3], fx.Pars[4]);
	// Remove schedule call if done, take into account infinite schedules.
	if (done && !fx.NoStop)
		return FX_Execute_Kill;
	return FX_OK;
}

// documented in /docs/sdk/script/fn
global func ClearScheduleCall(object obj, call_function)
{
	var i, fx;
	// Count downwards from effectnumber, to remove effects.
	i = GetEffectCount("IntScheduleCall", obj);
	while (i--)
		// Check All ScheduleCall-Effects.
		if (fx = GetEffect("IntScheduleCall", obj, i))
			// Found right function.
			if (fx.Function == call_function)
				// Remove effect.
				RemoveEffect(nil, obj, fx);
	return;
}