ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.23
Committed: Fri Apr 13 07:26:29 2007 UTC (17 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +3 -0 lines
Log Message:
*** empty log message ***

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