ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.31
Committed: Thu Sep 17 01:57:31 2009 UTC (14 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.30: +9 -21 lines
Log Message:
just a simplification

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.28 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.17 *
4 root 1.30 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.25 * Copyright (©) 2002-2003,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.17 *
8 root 1.28 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.27 * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation, either version 3 of the License, or
11     * (at your option) any later version.
12 pippijn 1.17 *
13 root 1.27 * 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 pippijn 1.17 *
18 root 1.27 * You should have received a copy of the GNU General Public License
19     * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 root 1.25 *
21 root 1.28 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.17 */
23 elmex 1.1
24     /* This file contains animation related code. */
25    
26     #include <global.h>
27     #include <stdio.h>
28    
29 root 1.22 animhash_t animhash;
30     std::vector<animation> animations;
31    
32 root 1.7 void
33 root 1.22 animation::resize (int new_size)
34 root 1.4 {
35 root 1.29 sfree<faceidx> (faces, num_animations);
36 root 1.22 num_animations = new_size;
37     faces = salloc<faceidx> (num_animations);
38 elmex 1.1 }
39    
40 root 1.22 animation &
41     animation::create (const char *name, uint8 frames, uint8 facings)
42 root 1.7 {
43 root 1.23 if (animations.size () == MAXANIMNUM)
44     cleanup ("trying to create new animation, but MAXANIMNUM animations in use.");
45    
46 root 1.22 animations.push_back (animation ());
47     animation &anim = animations.back ();
48 root 1.7
49 root 1.22 anim.number = animations.size () - 1;
50     anim.name = name;
51     anim.num_animations = frames;
52     anim.facings = facings;
53     anim.faces = salloc<faceidx> (frames);
54 root 1.7
55 root 1.22 animhash.insert (std::make_pair (anim.name, anim.number));
56 root 1.7
57 root 1.22 return anim;
58     }
59 root 1.20
60 root 1.22 animation &
61     animation::find (const char *name)
62     {
63     if (!name)
64     return animations [0];
65 root 1.20
66 root 1.22 animhash_t::iterator i = animhash.find (name);
67     return animations [i == animhash.end () ? 0 : i->second];
68 elmex 1.1 }
69    
70 root 1.22 void
71     init_anim (void)
72 root 1.7 {
73 root 1.22 animation &anim0 = animation::create ("none", 1, 0);
74     anim0.faces [0] = 0;
75 elmex 1.1 }
76    
77     /* Tries to find the animation id that matches name. Returns an integer match
78 pippijn 1.14 * 0 if no match found (animation 0 is initialised as the 'bug' face
79 elmex 1.1 */
80 root 1.22 //TODO: nuke this function and replace all occurences by animations::find
81 root 1.7 int
82     find_animation (const char *name)
83 elmex 1.1 {
84 root 1.22 return animation::find (name).number;
85 elmex 1.1 }
86    
87     /*
88     * animate_object(object) updates the face-variable of an object.
89     * If the object is the head of a multi-object, all objects are animated.
90     * op is the object to animate.
91     * dir is the direction the object is facing. This is generally same as
92     * op->direction, but in some cases, op->facing is used instead - the
93     * caller has a better idea which one it really wants to be using,
94     * so let it pass along the right one.
95     */
96 root 1.7 void
97     animate_object (object *op, int dir)
98     {
99 root 1.12 int max_state; /* Max animation state object should be drawn in */
100     int base_state; /* starting index # to draw from */
101 root 1.7
102     if (!op->animation_id || !NUM_ANIMATIONS (op))
103     {
104 root 1.9 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
105 root 1.16 CLEAR_FLAG (op, FLAG_ANIMATE);
106 root 1.7 return;
107     }
108 root 1.9
109 root 1.24 if (op->head_ () != op)
110 root 1.7 {
111     dir = op->head->direction;
112    
113     if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head))
114     op->state = op->head->state;
115     else
116     ++op->state;
117     }
118     else
119 root 1.9 ++op->state; /* increase draw state */
120 root 1.7
121     /* If object is turning, then max animation state is half through the
122     * animations. Otherwise, we can use all the animations.
123     */
124     max_state = NUM_ANIMATIONS (op) / NUM_FACINGS (op);
125     base_state = 0;
126 root 1.20 /* at least in the older animations that used is_turning, the first half
127 root 1.7 * of the animations were left facing, the second half right facing.
128     * Note in old the is_turning, it was set so that the animation for a monster
129     * was always towards the enemy - now it is whatever direction the monster
130     * is facing.
131     */
132 root 1.9
133 root 1.31 if (dir > 0)
134     switch (NUM_FACINGS (op))
135     {
136     case 2: base_state = ((dir - 1) / (8 / 2)) * (NUM_ANIMATIONS (op) / 2); break;
137     case 4: base_state = ((dir - 1) / (8 / 4)) * (NUM_ANIMATIONS (op) / 4); break;
138     case 8: base_state = ((dir - 1) / (8 / 8)) * (NUM_ANIMATIONS (op) / 8); break;
139     }
140     else
141     base_state = 0;
142 root 1.7
143     /* If beyond drawable states, reset */
144     if (op->state >= max_state)
145     op->state = 0;
146    
147     SET_ANIMATION (op, op->state + base_state);
148    
149     if (op->face == blank_face)
150     op->invisible = 1;
151 root 1.21
152 root 1.7 /* This block covers monsters (eg, pixies) which are supposed to
153     * cycle from visible to invisible and back to being visible.
154     * as such, disable it for players, as then players would become
155     * visible.
156     */
157 root 1.26 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
158 root 1.7 {
159 root 1.21 if (op->face == 0)
160 root 1.7 {
161     op->invisible = 1;
162     CLEAR_FLAG (op, FLAG_ALIVE);
163     }
164     else
165     {
166     op->invisible = 0;
167     SET_FLAG (op, FLAG_ALIVE);
168     }
169     }
170    
171     if (op->more)
172     animate_object (op->more, dir);
173    
174     /* update_object will also recursively update all the pieces.
175     * as such, we call it last, and only call it for the head
176     * piece, and not for the other tail pieces.
177     */
178 root 1.24 if (op->head_ () == op)
179 root 1.7 update_object (op, UP_OBJ_FACE);
180 elmex 1.1 }
181 root 1.22