Crossfire Server, Trunk
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 */
54  }
55  } else if (timers_table[i].mode == TIMER_MODE_SECONDS) {
56  if (timers_table[i].delay <= seconds()) {
57  /* Call object timer event */
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)
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;
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 }
TIMER_ERR_OBJ
#define TIMER_ERR_OBJ
Definition: timers.h:68
TIMER_MODE_SECONDS
#define TIMER_MODE_SECONDS
Definition: timers.h:48
MAX_TIMERS
#define MAX_TIMERS
Definition: timers.h:61
EVENT_CONNECTOR
@ EVENT_CONNECTOR
Definition: object.h:227
_cftimer::delay
long delay
Definition: timers.h:56
_cftimer::mode
int mode
Definition: timers.h:55
_cftimer::ob_tag
tag_t ob_tag
Definition: timers.h:57
cftimer_create
int cftimer_create(int id, long delay, object *ob, int mode)
Definition: timers.c:97
guildjoin.ob
ob
Definition: guildjoin.py:42
EVENT_TIMER
#define EVENT_TIMER
Definition: events.h:34
events_execute_object_event
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:274
TIMER_MODE_CYCLES
#define TIMER_MODE_CYCLES
Definition: timers.h:49
cftimer_init
void cftimer_init(void)
Definition: timers.c:157
TIMER_ERR_ID
#define TIMER_ERR_ID
Definition: timers.h:67
TIMER_ERR_NONE
#define TIMER_ERR_NONE
Definition: timers.h:66
object_find_by_type_subtype
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:4272
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:377
cftimer_process_timers
void cftimer_process_timers(void)
Definition: timers.c:44
TIMER_ERR_MODE
#define TIMER_ERR_MODE
Definition: timers.h:69
tag_t
uint32_t tag_t
Definition: object.h:12
sproto.h
cftimer_destroy
int cftimer_destroy(int id)
Definition: timers.c:128
object_find_by_tag_global
object * object_find_by_tag_global(tag_t i)
Definition: object.c:731
timers_table
cftimer timers_table[MAX_TIMERS]
Definition: timers.c:37
timers.h
cftimer_process_event
static void cftimer_process_event(tag_t ob_tag)
Definition: timers.c:71
TIMER_MODE_DEAD
#define TIMER_MODE_DEAD
Definition: timers.h:47
seconds
long seconds(void)
Definition: time.c:344
_cftimer
Definition: timers.h:54
cftimer_find_free_id
int cftimer_find_free_id(void)
Definition: timers.c:144
diamondslots.id
id
Definition: diamondslots.py:53