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

# User Rev Content
1 elmex 1.1 /*
2 pippijn 1.17 * 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 elmex 1.1
25     /* This file contains animation related code. */
26    
27     #include <global.h>
28     #include <stdio.h>
29    
30 root 1.22 animhash_t animhash;
31     std::vector<animation> animations;
32    
33 root 1.7 void
34 root 1.22 animation::resize (int new_size)
35 root 1.4 {
36 root 1.22 sfree <faceidx> (faces, num_animations);
37     num_animations = new_size;
38     faces = salloc<faceidx> (num_animations);
39 elmex 1.1 }
40    
41 root 1.22 animation &
42     animation::create (const char *name, uint8 frames, uint8 facings)
43 root 1.7 {
44 root 1.22 animations.push_back (animation ());
45     animation &anim = animations.back ();
46 root 1.7
47 root 1.22 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 root 1.7
53 root 1.22 animhash.insert (std::make_pair (anim.name, anim.number));
54 root 1.7
55 root 1.22 return anim;
56     }
57 root 1.20
58 root 1.22 animation &
59     animation::find (const char *name)
60     {
61     if (!name)
62     return animations [0];
63 root 1.20
64 root 1.22 animhash_t::iterator i = animhash.find (name);
65     return animations [i == animhash.end () ? 0 : i->second];
66 elmex 1.1 }
67    
68 root 1.22 void
69     init_anim (void)
70 root 1.7 {
71 root 1.22 animation &anim0 = animation::create ("none", 1, 0);
72     anim0.faces [0] = 0;
73 elmex 1.1 }
74    
75     /* Tries to find the animation id that matches name. Returns an integer match
76 pippijn 1.14 * 0 if no match found (animation 0 is initialised as the 'bug' face
77 elmex 1.1 */
78 root 1.22 //TODO: nuke this function and replace all occurences by animations::find
79 root 1.7 int
80     find_animation (const char *name)
81 elmex 1.1 {
82 root 1.22 return animation::find (name).number;
83 elmex 1.1 }
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 root 1.7 void
95     animate_object (object *op, int dir)
96     {
97 root 1.12 int max_state; /* Max animation state object should be drawn in */
98     int base_state; /* starting index # to draw from */
99 root 1.7
100     if (!op->animation_id || !NUM_ANIMATIONS (op))
101     {
102 root 1.9 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
103 root 1.16 CLEAR_FLAG (op, FLAG_ANIMATE);
104 root 1.7 return;
105     }
106 root 1.9
107 root 1.7 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 root 1.9 ++op->state; /* increase draw state */
118 root 1.7
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 root 1.20 /* at least in the older animations that used is_turning, the first half
125 root 1.7 * 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 root 1.9
131 root 1.7 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 root 1.21
162 root 1.7 /* 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 root 1.20 else if (op->type != PLAYER && op->arch->clone.flag [FLAG_ALIVE])
168 root 1.7 {
169 root 1.21 if (op->face == 0)
170 root 1.7 {
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 elmex 1.1 }
191 root 1.22