1 |
/* |
2 |
* CrossFire, A Multiplayer game for X-windows |
3 |
* |
4 |
* Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 |
* Copyright (C) 2002-2003 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (C) 1992 Frank Tore Johansen |
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 authors can be reached via e-mail at <crossfire@schmorp.de> |
23 |
*/ |
24 |
|
25 |
/* This file contains animation related code. */ |
26 |
|
27 |
#include <global.h> |
28 |
#include <stdio.h> |
29 |
|
30 |
void |
31 |
free_all_anim (void) |
32 |
{ |
33 |
for (int i = 0; i <= num_animations; i++) |
34 |
{ |
35 |
animations[i].name = 0; |
36 |
free (animations[i].faces); |
37 |
} |
38 |
|
39 |
animations.clear (); |
40 |
} |
41 |
|
42 |
void |
43 |
init_anim (void) |
44 |
{ |
45 |
char buf[MAX_BUF]; |
46 |
FILE * fp; |
47 |
static int anim_init = 0; |
48 |
int num_frames = 0, faces[MAX_ANIMATIONS], i; |
49 |
|
50 |
if (anim_init) |
51 |
return; |
52 |
|
53 |
num_animations = 0; |
54 |
/* Make a default. New animations start at one, so if something |
55 |
* thinks it is animated but hasn't set the animation_id properly, |
56 |
* it will have a default value that should be pretty obvious. |
57 |
*/ |
58 |
/* set the name so we don't try to dereferance null. |
59 |
* Put # at start so it will be first in alphabetical |
60 |
* order. |
61 |
*/ |
62 |
{ |
63 |
animation anim0; |
64 |
|
65 |
anim0.name = "###none"; |
66 |
anim0.num_animations = 1; |
67 |
anim0.faces = (uint16 *)malloc (sizeof (uint16)); |
68 |
anim0.faces[0] = 0; |
69 |
anim0.facings = 0; |
70 |
|
71 |
animations.push_back (anim0); |
72 |
} |
73 |
|
74 |
sprintf (buf, "%s/animations", settings.datadir); |
75 |
LOG (llevDebug, "Reading animations from %s...\n", buf); |
76 |
|
77 |
if ((fp = fopen (buf, "r")) == NULL) |
78 |
{ |
79 |
LOG (llevError, "Cannot open animations file %s: %s\n", buf, strerror (errno)); |
80 |
exit (-1); |
81 |
} |
82 |
|
83 |
while (fgets (buf, MAX_BUF - 1, fp) != NULL) |
84 |
{ |
85 |
if (*buf == '#') |
86 |
continue; |
87 |
|
88 |
/* Kill the newline */ |
89 |
buf[strlen (buf) - 1] = '\0'; |
90 |
if (!strncmp (buf, "anim ", 5)) |
91 |
{ |
92 |
if (num_frames) |
93 |
{ |
94 |
LOG (llevError, "Didn't get a mina before %s\n", buf); |
95 |
num_frames = 0; |
96 |
} |
97 |
|
98 |
num_animations++; |
99 |
|
100 |
animation anim; |
101 |
|
102 |
anim.name = buf + 5; |
103 |
anim.num = num_animations; /* for bsearch */ |
104 |
anim.facings = 1; |
105 |
animations.push_back (anim); |
106 |
} |
107 |
else if (!strncmp (buf, "mina", 4)) |
108 |
{ |
109 |
animations[num_animations].faces = (uint16 *) malloc (sizeof (uint16) * num_frames); |
110 |
|
111 |
for (i = 0; i < num_frames; i++) |
112 |
animations[num_animations].faces[i] = faces[i]; |
113 |
|
114 |
animations[num_animations].num_animations = num_frames; |
115 |
if (num_frames % animations[num_animations].facings) |
116 |
LOG (llevDebug, "Animation %s frame numbers (%d) is not a multiple of facings (%d)\n", |
117 |
&animations[num_animations].name, num_frames, animations[num_animations].facings); |
118 |
|
119 |
num_frames = 0; |
120 |
} |
121 |
else if (!strncmp (buf, "facings ", 8)) |
122 |
{ |
123 |
if (!(animations[num_animations].facings = atoi (buf + 8))) |
124 |
{ |
125 |
LOG (llevDebug, "Animation %s has 0 facings, line=%s\n", &animations[num_animations].name, buf); |
126 |
animations[num_animations].facings = 1; |
127 |
} |
128 |
|
129 |
} |
130 |
else |
131 |
{ |
132 |
if (!(faces[num_frames++] = face_find (buf))) |
133 |
LOG (llevDebug, "Could not find face %s for animation %s\n", buf, &animations[num_animations].name); |
134 |
} |
135 |
} |
136 |
|
137 |
fclose (fp); |
138 |
LOG (llevDebug, "done. got (%d)\n", num_animations); |
139 |
} |
140 |
|
141 |
static int |
142 |
anim_compare (const animation *a, const animation *b) |
143 |
{ |
144 |
return std::strcmp (a->name, b->name); |
145 |
} |
146 |
|
147 |
/* Tries to find the animation id that matches name. Returns an integer match |
148 |
* 0 if no match found (animation 0 is initialised as the 'bug' face |
149 |
*/ |
150 |
int |
151 |
find_animation (const char *name) |
152 |
{ |
153 |
animation search, *match; |
154 |
|
155 |
search.name = name; |
156 |
|
157 |
match = (animation *) bsearch (&search, &animations[0], (num_animations + 1), |
158 |
sizeof (animation), (int (*)(const void *, const void *)) anim_compare); |
159 |
|
160 |
|
161 |
if (match) |
162 |
return match->num; |
163 |
|
164 |
LOG (llevError, "Unable to find animation %s\n", name); |
165 |
|
166 |
return 0; |
167 |
} |
168 |
|
169 |
/* |
170 |
* animate_object(object) updates the face-variable of an object. |
171 |
* If the object is the head of a multi-object, all objects are animated. |
172 |
* op is the object to animate. |
173 |
* dir is the direction the object is facing. This is generally same as |
174 |
* op->direction, but in some cases, op->facing is used instead - the |
175 |
* caller has a better idea which one it really wants to be using, |
176 |
* so let it pass along the right one. |
177 |
*/ |
178 |
void |
179 |
animate_object (object *op, int dir) |
180 |
{ |
181 |
int max_state; /* Max animation state object should be drawn in */ |
182 |
int base_state; /* starting index # to draw from */ |
183 |
|
184 |
if (!op->animation_id || !NUM_ANIMATIONS (op)) |
185 |
{ |
186 |
LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ()); |
187 |
CLEAR_FLAG (op, FLAG_ANIMATE); |
188 |
return; |
189 |
} |
190 |
|
191 |
if (op->head) |
192 |
{ |
193 |
dir = op->head->direction; |
194 |
|
195 |
if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head)) |
196 |
op->state = op->head->state; |
197 |
else |
198 |
++op->state; |
199 |
} |
200 |
else |
201 |
++op->state; /* increase draw state */ |
202 |
|
203 |
/* If object is turning, then max animation state is half through the |
204 |
* animations. Otherwise, we can use all the animations. |
205 |
*/ |
206 |
max_state = NUM_ANIMATIONS (op) / NUM_FACINGS (op); |
207 |
base_state = 0; |
208 |
/* at least in the older animations that used is_turning, the first half |
209 |
* of the animations were left facing, the second half right facing. |
210 |
* Note in old the is_turning, it was set so that the animation for a monster |
211 |
* was always towards the enemy - now it is whatever direction the monster |
212 |
* is facing. |
213 |
*/ |
214 |
|
215 |
if (NUM_FACINGS (op) == 2) |
216 |
{ |
217 |
if (dir < 5) |
218 |
base_state = 0; |
219 |
else |
220 |
base_state = NUM_ANIMATIONS (op) / 2; |
221 |
} |
222 |
else if (NUM_FACINGS (op) == 4) |
223 |
{ |
224 |
if (dir == 0) |
225 |
base_state = 0; |
226 |
else |
227 |
base_state = ((dir - 1) / 2) * (NUM_ANIMATIONS (op) / 4); |
228 |
} |
229 |
else if (NUM_FACINGS (op) == 8) |
230 |
{ |
231 |
if (dir == 0) |
232 |
base_state = 0; |
233 |
else |
234 |
base_state = (dir - 1) * (NUM_ANIMATIONS (op) / 8); |
235 |
} |
236 |
|
237 |
/* If beyond drawable states, reset */ |
238 |
if (op->state >= max_state) |
239 |
op->state = 0; |
240 |
|
241 |
SET_ANIMATION (op, op->state + base_state); |
242 |
|
243 |
if (op->face == blank_face) |
244 |
op->invisible = 1; |
245 |
|
246 |
/* This block covers monsters (eg, pixies) which are supposed to |
247 |
* cycle from visible to invisible and back to being visible. |
248 |
* as such, disable it for players, as then players would become |
249 |
* visible. |
250 |
*/ |
251 |
else if (op->type != PLAYER && op->arch->clone.flag [FLAG_ALIVE]) |
252 |
{ |
253 |
if (op->face == 0) |
254 |
{ |
255 |
op->invisible = 1; |
256 |
CLEAR_FLAG (op, FLAG_ALIVE); |
257 |
} |
258 |
else |
259 |
{ |
260 |
op->invisible = 0; |
261 |
SET_FLAG (op, FLAG_ALIVE); |
262 |
} |
263 |
} |
264 |
|
265 |
if (op->more) |
266 |
animate_object (op->more, dir); |
267 |
|
268 |
/* update_object will also recursively update all the pieces. |
269 |
* as such, we call it last, and only call it for the head |
270 |
* piece, and not for the other tail pieces. |
271 |
*/ |
272 |
if (!op->head) |
273 |
update_object (op, UP_OBJ_FACE); |
274 |
} |