ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.35
Committed: Fri Mar 26 00:59:20 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.34: +2 -2 lines
Log Message:
remove bogus 2007 copyright that was added wrongly by the script, update to affero license

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 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 animation &
62 animation::find (const char *name)
63 {
64 if (!name)
65 return animations [0];
66
67 animhash_t::iterator i = animhash.find (name);
68 return animations [i == animhash.end () ? 0 : i->second];
69 }
70
71 void
72 init_anim ()
73 {
74 animation &anim0 = animation::create ("none", 1, 0);
75 anim0.faces [0] = 0;
76 }
77
78 /* Tries to find the animation id that matches name. Returns an integer match
79 * 0 if no match found (animation 0 is initialised as the 'bug' face
80 */
81 //TODO: nuke this function and replace all occurences by animations::find
82 int
83 find_animation (const char *name)
84 {
85 return animation::find (name).number;
86 }
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 void
98 animate_object (object *op, int dir)
99 {
100 int max_state; /* Max animation state object should be drawn in */
101 int base_state; /* starting index # to draw from */
102
103 if (!op->animation_id || !NUM_ANIMATIONS (op))
104 {
105 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
106 CLEAR_FLAG (op, FLAG_ANIMATE);
107 return;
108 }
109
110 if (op->head_ () != op)
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 ++op->state; /* increase draw state */
121
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 /* at least in the older animations that used is_turning, the first half
128 * 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
134 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
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
153 /* 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 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
159 {
160 if (op->face == 0)
161 {
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 if (op->head_ () == op)
180 update_object (op, UP_OBJ_FACE);
181 }
182