Crossfire Server, Branches 1.12  R18729
timers.c
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2001-2007 Yann Chachkoff & Crossfire Development Team
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 
20  The authors can be reached via e-mail at crossfire-devel@real-time.com
21 */
22 
40 #include <timers.h>
41 #ifndef __CEXTRACT__
42 #include <sproto.h>
43 #endif
44 
45 /* Extern in header. */
47 
48 static void cftimer_process_event(tag_t ob_tag);
49 
54  int i;
55 
56  for (i = 0; i < MAX_TIMERS; i++) {
57  if (timers_table[i].mode == TIMER_MODE_CYCLES) {
58  timers_table[i].delay--;
59  if (timers_table[i].delay == 0) {
60  /* Call object timer event */
61  timers_table[i].mode = TIMER_MODE_DEAD;
62  cftimer_process_event(timers_table[i].ob_tag);
63  }
64  } else if (timers_table[i].mode == TIMER_MODE_SECONDS) {
65  if (timers_table[i].delay <= seconds()) {
66  /* Call object timer event */
67  timers_table[i].mode = TIMER_MODE_DEAD;
68  cftimer_process_event(timers_table[i].ob_tag);
69  }
70  }
71  }
72 }
73 
80 static void cftimer_process_event(tag_t ob_tag) {
81  object *ob = find_object(ob_tag);
82 
83  if (ob)
84  execute_event(ob, EVENT_TIMER, NULL, NULL, NULL, SCRIPT_FIX_ALL);
85 }
86 
106 int cftimer_create(int id, long delay, object *ob, int mode) {
107  if (id >= MAX_TIMERS)
108  return TIMER_ERR_ID;
109  if (id < 0)
110  return TIMER_ERR_ID;
111  if (timers_table[id].mode != TIMER_MODE_DEAD)
112  return TIMER_ERR_ID;
113  if ((mode != TIMER_MODE_SECONDS) && (mode != TIMER_MODE_CYCLES))
114  return TIMER_ERR_MODE;
115  if (ob == NULL)
116  return TIMER_ERR_OBJ;
118  return TIMER_ERR_OBJ;
119  timers_table[id].mode = mode;
120  timers_table[id].ob_tag = ob->count;
121  if (mode == TIMER_MODE_CYCLES)
122  timers_table[id].delay = delay;
123  else
124  timers_table[id].delay = seconds()+delay;
125  return TIMER_ERR_NONE;
126 }
127 
137 int cftimer_destroy(int id) {
138  if (id >= MAX_TIMERS)
139  return TIMER_ERR_ID;
140  if (id < 0)
141  return TIMER_ERR_ID;
142  timers_table[id].mode = TIMER_MODE_DEAD;
143  return TIMER_ERR_NONE;
144 }
145 
154  int i;
155 
156  for (i = 0; i < MAX_TIMERS; i++) {
157  if (timers_table[i].mode == TIMER_MODE_DEAD)
158  return i;
159  }
160  return TIMER_ERR_ID;
161 }
162 
166 void cftimer_init(void) {
167  memset(&timers_table[0], 0, sizeof(cftimer)*MAX_TIMERS);
168 }
object * find_obj_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:3656
static void cftimer_process_event(tag_t ob_tag)
Definition: timers.c:80
int cftimer_destroy(int id)
Definition: timers.c:137
void cftimer_process_timers(void)
Definition: timers.c:53
#define TIMER_ERR_ID
Definition: timers.h:69
int mode
Definition: timers.h:57
cftimer timers_table[MAX_TIMERS]
Definition: timers.c:46
#define EVENT_TIMER
Definition: plugin.h:73
#define TIMER_ERR_OBJ
Definition: timers.h:70
object * find_object(tag_t i)
Definition: object.c:439
#define SCRIPT_FIX_ALL
Definition: global.h:450
tag_t ob_tag
Definition: timers.h:59
int cftimer_find_free_id(void)
Definition: timers.c:153
long delay
Definition: timers.h:58
uint32 tag_t
Definition: object.h:40
#define TIMER_MODE_CYCLES
Definition: timers.h:51
#define TIMER_MODE_DEAD
Definition: timers.h:49
long seconds(void)
Definition: time.c:417
int cftimer_create(int id, long delay, object *ob, int mode)
Definition: timers.c:106
#define MAX_TIMERS
Definition: timers.h:63
#define TIMER_ERR_MODE
Definition: timers.h:71
#define TIMER_ERR_NONE
Definition: timers.h:68
tag_t count
Definition: object.h:157
#define TIMER_MODE_SECONDS
Definition: timers.h:50
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: standalone.c:225
#define EVENT_CONNECTOR
Definition: define.h:300
void cftimer_init(void)
Definition: timers.c:166