Atrinik Client 2.5
client/animations.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 <global.h>
00031 
00034 void read_anims()
00035 {
00036     int anim_len = 0, new_anim = 1;
00037     uint8 faces = 0;
00038     FILE *fp;
00039     char buf[HUGE_BUF];
00040     unsigned char anim_cmd[2048];
00041     size_t count = 0;
00042 
00043     if (animations_num)
00044     {
00045         size_t i;
00046 
00047         /* Clear both animation tables. */
00048         for (i = 0; i < animations_num; i++)
00049         {
00050             if (animations[i].faces)
00051             {
00052                 free(animations[i].faces);
00053             }
00054 
00055             if (anim_table[i].anim_cmd)
00056             {
00057                 free(anim_table[i].anim_cmd);
00058             }
00059         }
00060 
00061         free(animations);
00062         animations = NULL;
00063         free(anim_table);
00064         anim_table = NULL;
00065         animations_num = 0;
00066     }
00067 
00068     anim_table = malloc(sizeof(_anim_table));
00069 
00070     /* Animation #0 is like face id #0. */
00071     anim_cmd[0] = (unsigned char) ((count >> 8) & 0xff);
00072     anim_cmd[1] = (unsigned char) (count & 0xff);
00073     anim_cmd[2] = 0;
00074     anim_cmd[3] = 1;
00075     anim_cmd[4] = 0;
00076     anim_cmd[5] = 0;
00077 
00078     anim_table[count].anim_cmd = malloc(6);
00079     memcpy(anim_table[count].anim_cmd, anim_cmd, 6);
00080     anim_table[count].len = 6;
00081     count++;
00082 
00083     fp = server_file_open(SERVER_FILE_ANIMS);
00084 
00085     if (!fp)
00086     {
00087         LOG(llevError, "read_anims(): Could not open anims server file.\n");
00088     }
00089 
00090     while (fgets(buf, sizeof(buf) - 1, fp))
00091     {
00092         /* Are we outside an anim body? */
00093         if (new_anim == 1)
00094         {
00095             if (!strncmp(buf, "anim ", 5))
00096             {
00097                 new_anim = 0;
00098                 faces = 0;
00099                 anim_cmd[0] = (unsigned char)((count >> 8) & 0xff);
00100                 anim_cmd[1] = (unsigned char)(count & 0xff);
00101                 faces = 1;
00102                 anim_len = 4;
00103             }
00104             /* we should never hit this point */
00105             else
00106             {
00107                 LOG(llevBug, "load_anim_tmp(): Error parsing anims.tmp - unknown cmd: >%s<!\n", buf);
00108             }
00109         }
00110         /* No, we are inside! */
00111         else
00112         {
00113             if (!strncmp(buf, "facings ", 8))
00114             {
00115                 faces = atoi(buf + 8);
00116             }
00117             else if (!strncmp(buf, "mina", 4))
00118             {
00119                 anim_table = realloc(anim_table, sizeof(_anim_table) * (count + 1));
00120                 anim_cmd[2] = 0;
00121                 anim_cmd[3] = faces;
00122                 anim_table[count].len = anim_len;
00123                 anim_table[count].anim_cmd = malloc(anim_len);
00124                 memcpy(anim_table[count].anim_cmd, anim_cmd, anim_len);
00125                 count++;
00126                 new_anim = 1;
00127             }
00128             else
00129             {
00130                 uint16 face_id = atoi(buf);
00131 
00132                 anim_cmd[anim_len++] = (unsigned char) ((face_id >> 8) & 0xff);
00133                 anim_cmd[anim_len++] = (unsigned char) (face_id & 0xff);
00134             }
00135         }
00136     }
00137 
00138     animations_num = count;
00139     animations = calloc(animations_num, sizeof(Animations));
00140     fclose(fp);
00141 }
00142 
00146 void anims_reset()
00147 {
00148     size_t i;
00149 
00150     for (i = 0; i < animations_num; i++)
00151     {
00152         animations[i].loaded = 0;
00153     }
00154 }