1 |
elmex |
1.1 |
#include <timers.h> |
2 |
|
|
#ifndef __CEXTRACT__ |
3 |
|
|
#include <sproto.h> |
4 |
|
|
#endif |
5 |
|
|
|
6 |
|
|
/*****************************************************************************/ |
7 |
|
|
/* Processes all timers. */ |
8 |
|
|
/*****************************************************************************/ |
9 |
|
|
void cftimer_process_timers(void) |
10 |
|
|
{ |
11 |
|
|
int i; |
12 |
|
|
for(i=0;i<MAX_TIMERS;i++) |
13 |
|
|
{ |
14 |
|
|
if (timers_table[i].mode == TIMER_MODE_CYCLES) |
15 |
|
|
{ |
16 |
|
|
timers_table[i].delay --; |
17 |
|
|
if (timers_table[i].delay == 0) |
18 |
|
|
{ |
19 |
|
|
/* Call object timer event */ |
20 |
|
|
timers_table[i].mode = TIMER_MODE_DEAD; |
21 |
|
|
cftimer_process_event(timers_table[i].ob); |
22 |
|
|
} |
23 |
|
|
} |
24 |
|
|
else if (timers_table[i].mode == TIMER_MODE_SECONDS) |
25 |
|
|
{ |
26 |
|
|
if (timers_table[i].delay <= seconds()) |
27 |
|
|
{ |
28 |
|
|
/* Call object timer event */ |
29 |
|
|
timers_table[i].mode = TIMER_MODE_DEAD; |
30 |
|
|
cftimer_process_event(timers_table[i].ob); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/*****************************************************************************/ |
37 |
|
|
/* Triggers the EVENT_TIMER of the given object */ |
38 |
|
|
/*****************************************************************************/ |
39 |
|
|
void cftimer_process_event(object* ob) |
40 |
|
|
{ |
41 |
|
|
execute_event(ob, EVENT_TIMER,NULL,NULL,NULL,SCRIPT_FIX_ALL); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/*****************************************************************************/ |
45 |
|
|
/* Creates a new timer. */ |
46 |
|
|
/* - id : Desired timer identifier. */ |
47 |
|
|
/* - delay : Desired timer delay. */ |
48 |
|
|
/* - ob : Object that will be linked to this timer. */ |
49 |
|
|
/* - mode : Count mode (seconds or cycles). See timers.h. */ |
50 |
|
|
/*****************************************************************************/ |
51 |
|
|
/* Return value: */ |
52 |
|
|
/* TIMER_ERR_NONE : Timer was successfully created. */ |
53 |
|
|
/* TIMER_ERR_ID : Invalid ID. */ |
54 |
|
|
/* TIMER_ERR_MODE : Invalid mode. */ |
55 |
|
|
/* TIMER_ERR_OBJ : Invalid object. */ |
56 |
|
|
/*****************************************************************************/ |
57 |
|
|
int cftimer_create(int id, long delay, object* ob, int mode) |
58 |
|
|
{ |
59 |
|
|
if (id >= MAX_TIMERS) |
60 |
|
|
return TIMER_ERR_ID; |
61 |
|
|
if (id < 0) |
62 |
|
|
return TIMER_ERR_ID; |
63 |
|
|
if (timers_table[id].mode != TIMER_MODE_DEAD) |
64 |
|
|
return TIMER_ERR_ID; |
65 |
|
|
if ((mode != TIMER_MODE_SECONDS) && (mode != TIMER_MODE_CYCLES)) |
66 |
|
|
return TIMER_ERR_MODE; |
67 |
|
|
if (ob == NULL) |
68 |
|
|
return TIMER_ERR_OBJ; |
69 |
|
|
if (find_event(ob, EVENT_TIMER) == NULL) |
70 |
|
|
return TIMER_ERR_OBJ; |
71 |
|
|
timers_table[id].mode = mode; |
72 |
|
|
timers_table[id].ob = ob; |
73 |
|
|
if (mode == TIMER_MODE_CYCLES) |
74 |
|
|
timers_table[id].delay = delay; |
75 |
|
|
else |
76 |
|
|
timers_table[id].delay = seconds() + delay; |
77 |
|
|
return TIMER_ERR_NONE; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/*****************************************************************************/ |
81 |
|
|
/* Destroys an existing timer. */ |
82 |
|
|
/* - id : Identifier of the timer to destroy. */ |
83 |
|
|
/*****************************************************************************/ |
84 |
|
|
/* Return value: */ |
85 |
|
|
/* TIMER_ERR_NONE : No problem encountered. */ |
86 |
|
|
/* TIMER_ERR_ID : Unknown ID - timer not found. */ |
87 |
|
|
/*****************************************************************************/ |
88 |
|
|
int cftimer_destroy(int id) |
89 |
|
|
{ |
90 |
|
|
if (id >= MAX_TIMERS) |
91 |
|
|
return TIMER_ERR_ID; |
92 |
|
|
if (id < 0) |
93 |
|
|
return TIMER_ERR_ID; |
94 |
|
|
timers_table[id].mode = TIMER_MODE_DEAD; |
95 |
|
|
return TIMER_ERR_NONE; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/*****************************************************************************/ |
99 |
|
|
/* Finds a free ID for a new timer. */ |
100 |
|
|
/*****************************************************************************/ |
101 |
|
|
/* Return value: */ |
102 |
|
|
/* TIMER_ERR_ID : No free ID available. */ |
103 |
|
|
/* >0 : an available ID. */ |
104 |
|
|
/*****************************************************************************/ |
105 |
|
|
int cftimer_find_free_id(void) |
106 |
|
|
{ |
107 |
|
|
int i; |
108 |
|
|
for(i=0;i<MAX_TIMERS;i++) |
109 |
|
|
{ |
110 |
|
|
if (timers_table[i].mode == TIMER_MODE_DEAD) |
111 |
|
|
return i; |
112 |
|
|
} |
113 |
|
|
return TIMER_ERR_ID; |
114 |
|
|
} |
115 |
|
|
|