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, 6 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

# 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.43 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.42 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.35 * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.40 *
9 root 1.32 * 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 root 1.40 *
14 root 1.27 * 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 root 1.40 *
19 root 1.32 * 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 root 1.40 *
23 root 1.28 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.17 */
25 elmex 1.1
26     /* This file contains animation related code. */
27    
28     #include <global.h>
29     #include <stdio.h>
30    
31 root 1.33 static animhash_t animhash;
32 root 1.22 std::vector<animation> animations;
33    
34 root 1.7 void
35 root 1.22 animation::resize (int new_size)
36 root 1.4 {
37 root 1.29 sfree<faceidx> (faces, num_animations);
38 root 1.22 num_animations = new_size;
39     faces = salloc<faceidx> (num_animations);
40 elmex 1.1 }
41    
42 root 1.22 animation &
43     animation::create (const char *name, uint8 frames, uint8 facings)
44 root 1.7 {
45 root 1.23 if (animations.size () == MAXANIMNUM)
46     cleanup ("trying to create new animation, but MAXANIMNUM animations in use.");
47    
48 root 1.22 animations.push_back (animation ());
49     animation &anim = animations.back ();
50 root 1.7
51 root 1.22 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 root 1.7
57 root 1.22 animhash.insert (std::make_pair (anim.name, anim.number));
58 root 1.7
59 root 1.22 return anim;
60     }
61 root 1.20
62 root 1.41 /* 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 root 1.22 animation &
66     animation::find (const char *name)
67     {
68     if (!name)
69     return animations [0];
70 root 1.20
71 root 1.22 animhash_t::iterator i = animhash.find (name);
72     return animations [i == animhash.end () ? 0 : i->second];
73 elmex 1.1 }
74    
75 root 1.22 void
76 root 1.34 init_anim ()
77 root 1.7 {
78 root 1.22 animation &anim0 = animation::create ("none", 1, 0);
79     anim0.faces [0] = 0;
80 elmex 1.1 }
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 root 1.7 void
92     animate_object (object *op, int dir)
93     {
94 root 1.12 int max_state; /* Max animation state object should be drawn in */
95     int base_state; /* starting index # to draw from */
96 root 1.7
97 root 1.44 if (!op->animation_id || !op->anim_frames ())
98 root 1.7 {
99 root 1.9 LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ());
100 root 1.37 op->clr_flag (FLAG_ANIMATE);
101 root 1.7 return;
102     }
103 root 1.9
104 root 1.24 if (op->head_ () != op)
105 root 1.7 {
106     dir = op->head->direction;
107    
108 root 1.44 if (op->anim_frames () == op->head->anim_frames ())
109 root 1.7 op->state = op->head->state;
110     else
111     ++op->state;
112     }
113     else
114 root 1.9 ++op->state; /* increase draw state */
115 root 1.7
116     /* If object is turning, then max animation state is half through the
117     * animations. Otherwise, we can use all the animations.
118     */
119 root 1.44 max_state = op->anim_frames () / op->anim_facings ();
120 root 1.7 base_state = 0;
121 root 1.20 /* at least in the older animations that used is_turning, the first half
122 root 1.7 * 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 root 1.9
128 root 1.31 if (dir > 0)
129 root 1.44 switch (op->anim_facings ())
130 root 1.31 {
131 root 1.44 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 root 1.31 }
135     else
136     base_state = 0;
137 root 1.7
138     /* If beyond drawable states, reset */
139     if (op->state >= max_state)
140     op->state = 0;
141    
142 root 1.44 op->set_anim_frame (op->state + base_state);
143 root 1.7
144     if (op->face == blank_face)
145     op->invisible = 1;
146 root 1.21
147 root 1.7 /* 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 root 1.26 else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE])
153 root 1.7 {
154 root 1.21 if (op->face == 0)
155 root 1.7 {
156     op->invisible = 1;
157 root 1.37 op->clr_flag (FLAG_ALIVE);
158 root 1.7 }
159     else
160     {
161     op->invisible = 0;
162 root 1.37 op->set_flag (FLAG_ALIVE);
163 root 1.7 }
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 root 1.24 if (op->head_ () == op)
174 root 1.7 update_object (op, UP_OBJ_FACE);
175 elmex 1.1 }
176 root 1.22