ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.32
Committed: Mon Oct 12 14:00:57 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81
Changes since 1.31: +7 -6 lines
Log Message:
clarify license

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.32 * Deliantra is free software: you can redistribute it and/or modify it under
9     * the terms of the Affero GNU General Public License as published by the
10     * Free Software Foundation, either version 3 of the License, or (at your
11     * 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.32 * You should have received a copy of the Affero GNU General Public License
19     * and the GNU General Public License along with this program. If not, see
20     * <http://www.gnu.org/licenses/>.
21 root 1.25 *
22 root 1.28 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.17 */
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.29 sfree<faceidx> (faces, num_animations);
37 root 1.22 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.24 if (op->head_ () != op)
111 root 1.7 {
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.31 if (dir > 0)
135     switch (NUM_FACINGS (op))
136     {
137     case 2: base_state = ((dir - 1) / (8 / 2)) * (NUM_ANIMATIONS (op) / 2); break;
138     case 4: base_state = ((dir - 1) / (8 / 4)) * (NUM_ANIMATIONS (op) / 4); break;
139     case 8: base_state = ((dir - 1) / (8 / 8)) * (NUM_ANIMATIONS (op) / 8); break;
140     }
141     else
142     base_state = 0;
143 root 1.7
144     /* If beyond drawable states, reset */
145     if (op->state >= max_state)
146     op->state = 0;
147    
148     SET_ANIMATION (op, op->state + base_state);
149    
150     if (op->face == blank_face)
151     op->invisible = 1;
152 root 1.21
153 root 1.7 /* This block covers monsters (eg, pixies) which are supposed to
154     * cycle from visible to invisible and back to being visible.
155     * as such, disable it for players, as then players would become
156     * visible.
157     */
158 root 1.26 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
159 root 1.7 {
160 root 1.21 if (op->face == 0)
161 root 1.7 {
162     op->invisible = 1;
163     CLEAR_FLAG (op, FLAG_ALIVE);
164     }
165     else
166     {
167     op->invisible = 0;
168     SET_FLAG (op, FLAG_ALIVE);
169     }
170     }
171    
172     if (op->more)
173     animate_object (op->more, dir);
174    
175     /* update_object will also recursively update all the pieces.
176     * as such, we call it last, and only call it for the head
177     * piece, and not for the other tail pieces.
178     */
179 root 1.24 if (op->head_ () == op)
180 root 1.7 update_object (op, UP_OBJ_FACE);
181 elmex 1.1 }
182 root 1.22