Crossfire Server, Trunk  R20513
timers.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
31 #include <string.h>
32 
33 #include "timers.h"
34 #include "sproto.h"
35 
36 /* Extern in header. */
38 
39 static void cftimer_process_event(tag_t ob_tag);
40 
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 
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 
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;
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 
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 
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 
157 void cftimer_init(void) {
158  memset(&timers_table[0], 0, sizeof(cftimer)*MAX_TIMERS);
159 }
static void cftimer_process_event(tag_t ob_tag)
Triggers the EVENT_TIMER of the given object.
Definition: timers.c:71
int cftimer_destroy(int id)
Destroys an existing timer.
Definition: timers.c:128
void cftimer_process_timers(void)
Processes all timers.
Definition: timers.c:44
#define TIMER_ERR_ID
Invalid timer id.
Definition: timers.h:67
int mode
Timer mode.
Definition: timers.h:55
cftimer timers_table[MAX_TIMERS]
The actual timers.
Definition: timers.c:37
#define EVENT_TIMER
Timer connected triggered it.
Definition: plugin.h:76
#define TIMER_ERR_OBJ
NULL object, or no EVENT_TIMER handler.
Definition: timers.h:68
#define SCRIPT_FIX_ALL
Definition: global.h:361
tag_t ob_tag
Object associated to the timer.
Definition: timers.h:57
int cftimer_find_free_id(void)
Finds a free ID for a new timer.
Definition: timers.c:144
long delay
Either server ticks before trigger, or trigger time.
Definition: timers.h:56
object * object_find_by_tag_global(tag_t i)
Returns the object which has the count-variable equal to the argument.
Definition: object.c:476
#define TIMER_MODE_CYCLES
Timer is in server cycles.
Definition: timers.h:49
#define TIMER_MODE_DEAD
Used to mark a timer as unused in the list.
Definition: timers.h:47
One timer.
Definition: timers.h:54
long seconds(void)
Return wall clock time in seconds.
Definition: time.c:344
int cftimer_create(int id, long delay, object *ob, int mode)
Creates a new timer.
Definition: timers.c:97
uint32_t tag_t
Object tag, unique during the whole game.
Definition: object.h:12
#define MAX_TIMERS
Maximum number of timers.
Definition: timers.h:61
#define TIMER_ERR_MODE
Invalid timer mode.
Definition: timers.h:69
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Find object in inventory.
Definition: object.c:4195
#define TIMER_ERR_NONE
No error.
Definition: timers.h:66
Variables for the custom timers.
tag_t count
Unique object number for this object.
Definition: object.h:299
#define TIMER_MODE_SECONDS
Timer is in seconds.
Definition: timers.h:48
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
Lauwenmark: an invisible object holding a plugin event hook.
Definition: object.h:227
void cftimer_init(void)
Initialize timers.
Definition: timers.c:157