Debian Codegraph @

server/timers.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This handles custom timers. See @ref page_timers.
17 *
18 * @page page_timers Custom timers
19 *
20 * It is possible, through the functions in the @ref timers.c file, to create
21 * custom timers that get activated after some specified time.
22 *
23 * A timer is associated to a specific ::object, and is given a delay, either
24 * in server ticks or in seconds. When the delay expires, and if the item still
25 * is valid, an ::EVENT_TIMER is generated for the specific object. Actual handling
26 * is thus delegated to plugins.
27 *
28 * Note that timers are one shot only, they reset after they activate.
29 */
30
31 #include <string.h>
32
33 #include "timers.h"
34 #include "sproto.h"
35
36 /* Extern in header. */
37 cftimer timers_table[MAX_TIMERS];
38
39 static void cftimer_process_event(tag_t ob_tag);
40
41 /**
42 * Processes all timers.
43 */
44 void cftimer_process_timers(void) {
45 int i;
46
47 for (i = 0; i < MAX_TIMERS; i++) {
48 if (timers_table[i].mode == TIMER_MODE_CYCLES) {
49 timers_table[i].delay--;
50 if (timers_table[i].delay == 0) {
51 /* Call object timer event */
52 timers_table[i].mode = TIMER_MODE_DEAD;
53 cftimer_process_event(timers_table[i].ob_tag);
54 }
55 } else if (timers_table[i].mode == TIMER_MODE_SECONDS) {
56 if (timers_table[i].delay <= seconds()) {
57 /* Call object timer event */
58 timers_table[i].mode = TIMER_MODE_DEAD;
59 cftimer_process_event(timers_table[i].ob_tag);
60 }
61 }
62 }
63 }
64
65 /**
66 * Triggers the ::EVENT_TIMER of the given object.
67 *
68 * @param ob_tag
69 * object tag to use.
70 */
71 static void cftimer_process_event(tag_t ob_tag) {
72 object *ob = object_find_by_tag_global(ob_tag);
73
74 if (ob)
75 execute_event(ob, EVENT_TIMER, NULL, NULL, NULL, SCRIPT_FIX_ALL);
76 }
77
78 /**
79 * Creates a new timer.
80 * @param id
81 * desired timer identifier.
82 * @param delay
83 * desired timer delay.
84 * @param ob
85 * object that will be linked to this timer. Should have an ::EVENT_TIMER handler.
86 * @param mode
87 * unit for delay, should be ::TIMER_MODE_SECONDS or ::TIMER_MODE_CYCLES. See timers.h.
88 * @retval ::TIMER_ERR_NONE
89 * timer was successfully created.
90 * @retval ::TIMER_ERR_ID
91 * invalid ID.
92 * @retval ::TIMER_ERR_MODE
93 * invalid mode.
94 * @retval ::TIMER_ERR_OBJ
95 * ob is NULL or has no ::EVENT_TIMER handler.
96 */
97 int cftimer_create(int id, long delay, object *ob, int mode) {
98 if (id >= MAX_TIMERS)
99 return TIMER_ERR_ID;
100 if (id < 0)
101 return TIMER_ERR_ID;
102 if (timers_table[id].mode != TIMER_MODE_DEAD)
103 return TIMER_ERR_ID;
104 if ((mode != TIMER_MODE_SECONDS) && (mode != TIMER_MODE_CYCLES))
105 return TIMER_ERR_MODE;
106 if (ob == NULL)
107 return TIMER_ERR_OBJ;
108 if (object_find_by_type_subtype(ob, EVENT_CONNECTOR, EVENT_TIMER) == NULL)
109 return TIMER_ERR_OBJ;
110 timers_table[id].mode = mode;
111 timers_table[id].ob_tag = ob->count;
112 if (mode == TIMER_MODE_CYCLES)
113 timers_table[id].delay = delay;
114 else
115 timers_table[id].delay = seconds()+delay;
116 return TIMER_ERR_NONE;
117 }
118
119 /**
120 * Destroys an existing timer.
121 * @param id
122 * identifier of the timer to destroy.
123 * @retval ::TIMER_ERR_NONE
124 * no problem encountered.
125 * @retval ::TIMER_ERR_ID
126 * unknown id - timer not found or invalid.
127 */
128 int cftimer_destroy(int id) {
129 if (id >= MAX_TIMERS)
130 return TIMER_ERR_ID;
131 if (id < 0)
132 return TIMER_ERR_ID;
133 timers_table[id].mode = TIMER_MODE_DEAD;
134 return TIMER_ERR_NONE;
135 }
136
137 /**
138 * Finds a free ID for a new timer.
139 * @retval ::TIMER_ERR_ID
140 * no free ID available.
141 * @retval >=0
142 * an available ID.
143 */
144 int cftimer_find_free_id(void) {
145 int i;
146
147 for (i = 0; i < MAX_TIMERS; i++) {
148 if (timers_table[i].mode == TIMER_MODE_DEAD)
149 return i;
150 }
151 return TIMER_ERR_ID;
152 }
153
154 /**
155 * Initialize timers.
156 */
157 void cftimer_init(void) {
158 memset(&timers_table[0], 0, sizeof(cftimer)*MAX_TIMERS);
159 }