Atrinik Client  4.0
animations.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 
35 void read_anims(void)
36 {
37  anims_deinit();
38 
39  size_t count = 0;
40  anim_table = emalloc(sizeof(*anim_table));
41 
42  /* Animation #0 is like face id #0. */
43  uint8_t anim_cmd[2048];
44  anim_cmd[0] = (uint8_t) ((count >> 8) & 0xff);
45  anim_cmd[1] = (uint8_t) (count & 0xff);
46  anim_cmd[2] = 0;
47  anim_cmd[3] = 1;
48  anim_cmd[4] = 0;
49  anim_cmd[5] = 0;
50 
51  anim_table[count].anim_cmd = emalloc(6);
52  memcpy(anim_table[count].anim_cmd, anim_cmd, 6);
53  anim_table[count].len = 6;
54  count++;
55 
56  FILE *fp = server_file_open_name(SERVER_FILE_ANIMS);
57  if (fp == NULL) {
58  return;
59  }
60 
61  uint8_t faces;
62  size_t anim_len;
63  bool in_anim = false;
64 
65  char buf[HUGE_BUF];
66  while (fgets(VS(buf), fp)) {
67  if (!in_anim) {
68  if (strncmp(buf, "anim ", 5) == 0) {
69  in_anim = true;
70  anim_cmd[0] = (uint8_t) ((count >> 8) & 0xff);
71  anim_cmd[1] = (uint8_t) (count & 0xff);
72  faces = 1;
73  anim_len = 4;
74  } else {
75  LOG(ERROR, "Invalid entry in animations file: %s", buf);
76  break;
77  }
78  } else {
79  if (!strncmp(buf, "facings ", 8)) {
80  faces = atoi(buf + 8);
81  } else if (!strncmp(buf, "mina", 4)) {
82  anim_table = erealloc(anim_table, sizeof(*anim_table) *
83  (count + 1));
84  anim_cmd[2] = 0;
85  anim_cmd[3] = faces;
86  anim_table[count].len = anim_len;
87  anim_table[count].anim_cmd = emalloc(anim_len);
88  memcpy(anim_table[count].anim_cmd, anim_cmd, anim_len);
89  count++;
90  in_anim = false;
91  } else {
92  uint16_t face_id = atoi(buf);
93  anim_cmd[anim_len++] = (uint8_t) ((face_id >> 8) & 0xff);
94  anim_cmd[anim_len++] = (uint8_t) (face_id & 0xff);
95  }
96  }
97  }
98 
99  animations_num = count;
100  animations = ecalloc(animations_num, sizeof(*animations));
101  fclose(fp);
102 }
103 
107 void anims_deinit(void)
108 {
109  /* Clear both animation tables. */
110  for (size_t i = 0; i < animations_num; i++) {
111  if (animations[i].faces != NULL) {
112  efree(animations[i].faces);
113  animations[i].faces = NULL;
114  }
115 
116  if (anim_table[i].anim_cmd != NULL) {
117  efree(anim_table[i].anim_cmd);
118  }
119  }
120 
121  animations_num = 0;
122 
123  if (animations != NULL) {
124  efree(animations);
125  animations = NULL;
126  }
127 
128  if (anim_table != NULL) {
129  efree(anim_table);
130  anim_table = NULL;
131  }
132 }
133 
138 void anims_reset(void)
139 {
140  size_t i;
141 
142  for (i = 0; i < animations_num; i++) {
143  animations[i].loaded = 0;
144  animations[i].num_animations = 0;
145 
146  if (animations[i].faces != NULL) {
147  efree(animations[i].faces);
148  animations[i].faces = NULL;
149  }
150  }
151 }
void read_anims(void)
Definition: animations.c:35
_anim_table * anim_table
Definition: main.c:68
FILE * server_file_open_name(const char *name)
Definition: server_files.c:437
void anims_reset(void)
Definition: animations.c:138
size_t animations_num
Definition: main.c:71
void anims_deinit(void)
Definition: animations.c:107