Crossfire Server, Branch 1.12
R12190
|
00001 /* 00002 CrossFire, A Multiplayer game for X-windows 00003 00004 Copyright (C) 2001-2007 Yann Chachkoff & Crossfire Development Team 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 00020 The authors can be reached via e-mail at crossfire-devel@real-time.com 00021 */ 00022 00040 #include <timers.h> 00041 #ifndef __CEXTRACT__ 00042 #include <sproto.h> 00043 #endif 00044 00045 /* Extern in header. */ 00046 cftimer timers_table[MAX_TIMERS]; 00047 00048 static void cftimer_process_event(tag_t ob_tag); 00049 00053 void cftimer_process_timers(void) { 00054 int i; 00055 00056 for (i = 0; i < MAX_TIMERS; i++) { 00057 if (timers_table[i].mode == TIMER_MODE_CYCLES) { 00058 timers_table[i].delay--; 00059 if (timers_table[i].delay == 0) { 00060 /* Call object timer event */ 00061 timers_table[i].mode = TIMER_MODE_DEAD; 00062 cftimer_process_event(timers_table[i].ob_tag); 00063 } 00064 } else if (timers_table[i].mode == TIMER_MODE_SECONDS) { 00065 if (timers_table[i].delay <= seconds()) { 00066 /* Call object timer event */ 00067 timers_table[i].mode = TIMER_MODE_DEAD; 00068 cftimer_process_event(timers_table[i].ob_tag); 00069 } 00070 } 00071 } 00072 } 00073 00080 static void cftimer_process_event(tag_t ob_tag) { 00081 object *ob = find_object(ob_tag); 00082 00083 if (ob) 00084 execute_event(ob, EVENT_TIMER, NULL, NULL, NULL, SCRIPT_FIX_ALL); 00085 } 00086 00106 int cftimer_create(int id, long delay, object *ob, int mode) { 00107 if (id >= MAX_TIMERS) 00108 return TIMER_ERR_ID; 00109 if (id < 0) 00110 return TIMER_ERR_ID; 00111 if (timers_table[id].mode != TIMER_MODE_DEAD) 00112 return TIMER_ERR_ID; 00113 if ((mode != TIMER_MODE_SECONDS) && (mode != TIMER_MODE_CYCLES)) 00114 return TIMER_ERR_MODE; 00115 if (ob == NULL) 00116 return TIMER_ERR_OBJ; 00117 if (find_obj_by_type_subtype(ob, EVENT_CONNECTOR, EVENT_TIMER) == NULL) 00118 return TIMER_ERR_OBJ; 00119 timers_table[id].mode = mode; 00120 timers_table[id].ob_tag = ob->count; 00121 if (mode == TIMER_MODE_CYCLES) 00122 timers_table[id].delay = delay; 00123 else 00124 timers_table[id].delay = seconds()+delay; 00125 return TIMER_ERR_NONE; 00126 } 00127 00137 int cftimer_destroy(int id) { 00138 if (id >= MAX_TIMERS) 00139 return TIMER_ERR_ID; 00140 if (id < 0) 00141 return TIMER_ERR_ID; 00142 timers_table[id].mode = TIMER_MODE_DEAD; 00143 return TIMER_ERR_NONE; 00144 } 00145 00153 int cftimer_find_free_id(void) { 00154 int i; 00155 00156 for (i = 0; i < MAX_TIMERS; i++) { 00157 if (timers_table[i].mode == TIMER_MODE_DEAD) 00158 return i; 00159 } 00160 return TIMER_ERR_ID; 00161 } 00162 00166 void cftimer_init(void) { 00167 memset(&timers_table[0], 0, sizeof(cftimer)*MAX_TIMERS); 00168 }