Atrinik Server 2.5
server/timers.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <timers.h>
00031 
00032 cftimer timers_table[MAX_TIMERS];
00033 
00034 static void cftimer_process_event(object *ob);
00035 
00038 void cftimer_process_timers()
00039 {
00040     int i;
00041 
00042     for (i = 0; i < MAX_TIMERS; i++)
00043     {
00044         if (timers_table[i].mode == TIMER_MODE_CYCLES)
00045         {
00046             timers_table[i].delay--;
00047 
00048             if (timers_table[i].delay == 0)
00049             {
00050                 /* Call object timer event */
00051                 timers_table[i].mode = TIMER_MODE_DEAD;
00052                 cftimer_process_event(timers_table[i].ob);
00053             }
00054         }
00055         else if (timers_table[i].mode == TIMER_MODE_SECONDS)
00056         {
00057             if (timers_table[i].delay <= seconds())
00058             {
00059                 /* Call object timer event */
00060                 timers_table[i].mode = TIMER_MODE_DEAD;
00061                 cftimer_process_event(timers_table[i].ob);
00062             }
00063         }
00064     }
00065 }
00066 
00070 static void cftimer_process_event(object *ob)
00071 {
00072     if (ob && !OBJECT_FREE(ob))
00073     {
00074         trigger_event(EVENT_TIMER, NULL, ob, NULL, NULL, 0, 0, 0, SCRIPT_FIX_ALL);
00075     }
00076 }
00077 
00089 int cftimer_create(int id, long delay, object *ob, int mode)
00090 {
00091     if (id >= MAX_TIMERS)
00092     {
00093         return TIMER_ERR_ID;
00094     }
00095 
00096     if (id < 0)
00097     {
00098         return TIMER_ERR_ID;
00099     }
00100 
00101     if (timers_table[id].mode != TIMER_MODE_DEAD)
00102     {
00103         return TIMER_ERR_ID;
00104     }
00105 
00106     if ((mode != TIMER_MODE_SECONDS) && (mode != TIMER_MODE_CYCLES))
00107     {
00108         return TIMER_ERR_MODE;
00109     }
00110 
00111     if (!ob || !get_event_object(ob, EVENT_TIMER))
00112     {
00113         return TIMER_ERR_OBJ;
00114     }
00115 
00116     timers_table[id].mode = mode;
00117     timers_table[id].ob = ob;
00118 
00119     if (mode == TIMER_MODE_CYCLES)
00120     {
00121         timers_table[id].delay = delay;
00122     }
00123     else
00124     {
00125         timers_table[id].delay = seconds() + delay;
00126     }
00127 
00128     return TIMER_ERR_NONE;
00129 }
00130 
00136 int cftimer_destroy(int id)
00137 {
00138     if (id >= MAX_TIMERS)
00139     {
00140         return TIMER_ERR_ID;
00141     }
00142 
00143     if (id < 0)
00144     {
00145         return TIMER_ERR_ID;
00146     }
00147 
00148     timers_table[id].mode = TIMER_MODE_DEAD;
00149     return TIMER_ERR_NONE;
00150 }
00151 
00156 int cftimer_find_free_id()
00157 {
00158     int i;
00159 
00160     for (i = 0; i < MAX_TIMERS; i++)
00161     {
00162         if (timers_table[i].mode == TIMER_MODE_DEAD)
00163         {
00164             return i;
00165         }
00166     }
00167 
00168     return TIMER_ERR_ID;
00169 }
00170 
00173 void cftimer_init()
00174 {
00175     memset(&timers_table[0], 0, sizeof(cftimer) * MAX_TIMERS);
00176 }