ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.41
Committed: Wed Nov 14 06:21:39 2012 UTC (11 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_1
Changes since 1.40: +3 -10 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * 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 *
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 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 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 /* This file contains animation related code. */
26
27 #include <global.h>
28 #include <stdio.h>
29
30 static 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 if (animations.size () == MAXANIMNUM)
45 cleanup ("trying to create new animation, but MAXANIMNUM animations in use.");
46
47 animations.push_back (animation ());
48 animation &anim = animations.back ();
49
50 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
56 animhash.insert (std::make_pair (anim.name, anim.number));
57
58 return anim;
59 }
60
61 /* Tries to find the animation id that matches name. Returns an integer match
62 * 0 if no match found (animation 0 is initialised as the 'bug' face
63 */
64 animation &
65 animation::find (const char *name)
66 {
67 if (!name)
68 return animations [0];
69
70 animhash_t::iterator i = animhash.find (name);
71 return animations [i == animhash.end () ? 0 : i->second];
72 }
73
74 void
75 init_anim ()
76 {
77 animation &anim0 = animation::create ("none", 1, 0);
78 anim0.faces [0] = 0;
79 }
80
81 /*
82 * animate_object(object) updates the face-variable of an object.
83 * If the object is the head of a multi-object, all objects are animated.
84 * op is the object to animate.
85 * dir is the direction the object is facing. This is generally same as
86 * op->direction, but in some cases, op->facing is used instead - the
87 * caller has a better idea which one it really wants to be using,
88 * so let it pass along the right one.
89 */
90 void
91 animate_object (object *op, int dir)
92 {
93 int max_state; /* Max animation state object should be drawn in */
94 int base_state; /* starting index # to draw from */
95
96 if (!op->animation_id || !NUM_ANIMATIONS (op))
97 {
98 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
99 op->clr_flag (FLAG_ANIMATE);
100 return;
101 }
102
103 if (op->head_ () != op)
104 {
105 dir = op->head->direction;
106
107 if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head))
108 op->state = op->head->state;
109 else
110 ++op->state;
111 }
112 else
113 ++op->state; /* increase draw state */
114
115 /* If object is turning, then max animation state is half through the
116 * animations. Otherwise, we can use all the animations.
117 */
118 max_state = NUM_ANIMATIONS (op) / NUM_FACINGS (op);
119 base_state = 0;
120 /* at least in the older animations that used is_turning, the first half
121 * of the animations were left facing, the second half right facing.
122 * Note in old the is_turning, it was set so that the animation for a monster
123 * was always towards the enemy - now it is whatever direction the monster
124 * is facing.
125 */
126
127 if (dir > 0)
128 switch (NUM_FACINGS (op))
129 {
130 case 2: base_state = ((dir - 1) / (8 / 2)) * (NUM_ANIMATIONS (op) / 2); break;
131 case 4: base_state = ((dir - 1) / (8 / 4)) * (NUM_ANIMATIONS (op) / 4); break;
132 case 8: base_state = ((dir - 1) / (8 / 8)) * (NUM_ANIMATIONS (op) / 8); break;
133 }
134 else
135 base_state = 0;
136
137 /* If beyond drawable states, reset */
138 if (op->state >= max_state)
139 op->state = 0;
140
141 SET_ANIMATION (op, op->state + base_state);
142
143 if (op->face == blank_face)
144 op->invisible = 1;
145
146 /* This block covers monsters (eg, pixies) which are supposed to
147 * cycle from visible to invisible and back to being visible.
148 * as such, disable it for players, as then players would become
149 * visible.
150 */
151 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
152 {
153 if (op->face == 0)
154 {
155 op->invisible = 1;
156 op->clr_flag (FLAG_ALIVE);
157 }
158 else
159 {
160 op->invisible = 0;
161 op->set_flag (FLAG_ALIVE);
162 }
163 }
164
165 if (op->more)
166 animate_object (op->more, dir);
167
168 /* update_object will also recursively update all the pieces.
169 * as such, we call it last, and only call it for the head
170 * piece, and not for the other tail pieces.
171 */
172 if (op->head_ () == op)
173 update_object (op, UP_OBJ_FACE);
174 }
175