Crossfire Server, Trunk
timers.cpp
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
object_find_by_type_subtype
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.cpp:4301
MAX_TIMERS
#define MAX_TIMERS
Definition: timers.h:61
EVENT_CONNECTOR
@ EVENT_CONNECTOR
Definition: object.h:232
cftimer_find_free_id
int cftimer_find_free_id(void)
Definition: timers.cpp:144
cftimer_init
void cftimer_init(void)
Definition: timers.cpp:157
cftimer::delay
long delay
Definition: timers.h:56
guildjoin.ob
ob
Definition: guildjoin.py:42
EVENT_TIMER
#define EVENT_TIMER
Definition: events.h:35
timers_table
cftimer timers_table[MAX_TIMERS]
Definition: timers.cpp:37
object_find_by_tag_global
object * object_find_by_tag_global(tag_t i)
Definition: object.cpp:727
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:299
TIMER_MODE_CYCLES
#define TIMER_MODE_CYCLES
Definition: timers.h:49
TIMER_ERR_ID
#define TIMER_ERR_ID
Definition: timers.h:67
TIMER_ERR_NONE
#define TIMER_ERR_NONE
Definition: timers.h:66
cftimer_create
int cftimer_create(int id, long delay, object *ob, int mode)
Definition: timers.cpp:97
cftimer_process_timers
void cftimer_process_timers(void)
Definition: timers.cpp:44
cftimer::ob_tag
tag_t ob_tag
Definition: timers.h:57
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:389
TIMER_ERR_MODE
#define TIMER_ERR_MODE
Definition: timers.h:69
tag_t
uint32_t tag_t
Definition: object.h:14
sproto.h
cftimer
Definition: timers.h:54
seconds
long seconds(void)
Definition: time.cpp:348
timers.h
TIMER_MODE_DEAD
#define TIMER_MODE_DEAD
Definition: timers.h:47
cftimer::mode
int mode
Definition: timers.h:55
cftimer_destroy
int cftimer_destroy(int id)
Definition: timers.cpp:128
cftimer_process_event
static void cftimer_process_event(tag_t ob_tag)
Definition: timers.cpp:71
diamondslots.id
id
Definition: diamondslots.py:53