ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.22
Committed: Thu Apr 12 14:18:04 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.21: +33 -116 lines
Log Message:
move animation info into facedata and make it reloadable at runtime

File Contents

# Content
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 animhash_t animhash;
31 std::vector<animation> animations;
32
33 void
34 animation::resize (int new_size)
35 {
36 sfree <faceidx> (faces, num_animations);
37 num_animations = new_size;
38 faces = salloc<faceidx> (num_animations);
39 }
40
41 animation &
42 animation::create (const char *name, uint8 frames, uint8 facings)
43 {
44 animations.push_back (animation ());
45 animation &anim = animations.back ();
46
47 anim.number = animations.size () - 1;
48 anim.name = name;
49 anim.num_animations = frames;
50 anim.facings = facings;
51 anim.faces = salloc<faceidx> (frames);
52
53 animhash.insert (std::make_pair (anim.name, anim.number));
54
55 return anim;
56 }
57
58 animation &
59 animation::find (const char *name)
60 {
61 if (!name)
62 return animations [0];
63
64 animhash_t::iterator i = animhash.find (name);
65 return animations [i == animhash.end () ? 0 : i->second];
66 }
67
68 void
69 init_anim (void)
70 {
71 animation &anim0 = animation::create ("none", 1, 0);
72 anim0.faces [0] = 0;
73 }
74
75 /* Tries to find the animation id that matches name. Returns an integer match
76 * 0 if no match found (animation 0 is initialised as the 'bug' face
77 */
78 //TODO: nuke this function and replace all occurences by animations::find
79 int
80 find_animation (const char *name)
81 {
82 return animation::find (name).number;
83 }
84
85 /*
86 * animate_object(object) updates the face-variable of an object.
87 * If the object is the head of a multi-object, all objects are animated.
88 * op is the object to animate.
89 * dir is the direction the object is facing. This is generally same as
90 * op->direction, but in some cases, op->facing is used instead - the
91 * caller has a better idea which one it really wants to be using,
92 * so let it pass along the right one.
93 */
94 void
95 animate_object (object *op, int dir)
96 {
97 int max_state; /* Max animation state object should be drawn in */
98 int base_state; /* starting index # to draw from */
99
100 if (!op->animation_id || !NUM_ANIMATIONS (op))
101 {
102 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
103 CLEAR_FLAG (op, FLAG_ANIMATE);
104 return;
105 }
106
107 if (op->head)
108 {
109 dir = op->head->direction;
110
111 if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head))
112 op->state = op->head->state;
113 else
114 ++op->state;
115 }
116 else
117 ++op->state; /* increase draw state */
118
119 /* If object is turning, then max animation state is half through the
120 * animations. Otherwise, we can use all the animations.
121 */
122 max_state = NUM_ANIMATIONS (op) / NUM_FACINGS (op);
123 base_state = 0;
124 /* at least in the older animations that used is_turning, the first half
125 * of the animations were left facing, the second half right facing.
126 * Note in old the is_turning, it was set so that the animation for a monster
127 * was always towards the enemy - now it is whatever direction the monster
128 * is facing.
129 */
130
131 if (NUM_FACINGS (op) == 2)
132 {
133 if (dir < 5)
134 base_state = 0;
135 else
136 base_state = NUM_ANIMATIONS (op) / 2;
137 }
138 else if (NUM_FACINGS (op) == 4)
139 {
140 if (dir == 0)
141 base_state = 0;
142 else
143 base_state = ((dir - 1) / 2) * (NUM_ANIMATIONS (op) / 4);
144 }
145 else if (NUM_FACINGS (op) == 8)
146 {
147 if (dir == 0)
148 base_state = 0;
149 else
150 base_state = (dir - 1) * (NUM_ANIMATIONS (op) / 8);
151 }
152
153 /* If beyond drawable states, reset */
154 if (op->state >= max_state)
155 op->state = 0;
156
157 SET_ANIMATION (op, op->state + base_state);
158
159 if (op->face == blank_face)
160 op->invisible = 1;
161
162 /* This block covers monsters (eg, pixies) which are supposed to
163 * cycle from visible to invisible and back to being visible.
164 * as such, disable it for players, as then players would become
165 * visible.
166 */
167 else if (op->type != PLAYER && op->arch->clone.flag [FLAG_ALIVE])
168 {
169 if (op->face == 0)
170 {
171 op->invisible = 1;
172 CLEAR_FLAG (op, FLAG_ALIVE);
173 }
174 else
175 {
176 op->invisible = 0;
177 SET_FLAG (op, FLAG_ALIVE);
178 }
179 }
180
181 if (op->more)
182 animate_object (op->more, dir);
183
184 /* update_object will also recursively update all the pieces.
185 * as such, we call it last, and only call it for the head
186 * piece, and not for the other tail pieces.
187 */
188 if (!op->head)
189 update_object (op, UP_OBJ_FACE);
190 }
191