ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.44
Committed: Sun Nov 18 15:19:47 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.43: +8 -8 lines
Log Message:
*** empty log message ***

File Contents

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