ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/anim.C
Revision: 1.42
Committed: Wed Nov 16 23:41:59 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.41: +1 -1 lines
Log Message:
copyright update 2016

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.28 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.40 *
4 root 1.42 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.35 * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 Frank Tore Johansen
7 root 1.40 *
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 root 1.40 *
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 root 1.40 *
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.40 *
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.33 static animhash_t animhash;
31 root 1.22 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.41 /* 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 root 1.22 animation &
65     animation::find (const char *name)
66     {
67     if (!name)
68     return animations [0];
69 root 1.20
70 root 1.22 animhash_t::iterator i = animhash.find (name);
71     return animations [i == animhash.end () ? 0 : i->second];
72 elmex 1.1 }
73    
74 root 1.22 void
75 root 1.34 init_anim ()
76 root 1.7 {
77 root 1.22 animation &anim0 = animation::create ("none", 1, 0);
78     anim0.faces [0] = 0;
79 elmex 1.1 }
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 root 1.7 void
91     animate_object (object *op, int dir)
92     {
93 root 1.12 int max_state; /* Max animation state object should be drawn in */
94     int base_state; /* starting index # to draw from */
95 root 1.7
96     if (!op->animation_id || !NUM_ANIMATIONS (op))
97     {
98 root 1.9 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
99 root 1.37 op->clr_flag (FLAG_ANIMATE);
100 root 1.7 return;
101     }
102 root 1.9
103 root 1.24 if (op->head_ () != op)
104 root 1.7 {
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 root 1.9 ++op->state; /* increase draw state */
114 root 1.7
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 root 1.20 /* at least in the older animations that used is_turning, the first half
121 root 1.7 * 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 root 1.9
127 root 1.31 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 root 1.7
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 root 1.21
146 root 1.7 /* 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 root 1.26 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
152 root 1.7 {
153 root 1.21 if (op->face == 0)
154 root 1.7 {
155     op->invisible = 1;
156 root 1.37 op->clr_flag (FLAG_ALIVE);
157 root 1.7 }
158     else
159     {
160     op->invisible = 0;
161 root 1.37 op->set_flag (FLAG_ALIVE);
162 root 1.7 }
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 root 1.24 if (op->head_ () == op)
173 root 1.7 update_object (op, UP_OBJ_FACE);
174 elmex 1.1 }
175 root 1.22