1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
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 |
5 | * Copyright (C) 2002-2003 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program; if not, write to the Free Software |
|
|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
21 | * |
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 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
24 | */ |
24 | |
25 | |
25 | /* This file contains animation related code. */ |
26 | /* This file contains animation related code. */ |
26 | |
27 | |
27 | #include <global.h> |
28 | #include <global.h> |
28 | #include <stdio.h> |
29 | #include <stdio.h> |
29 | |
30 | |
30 | animhash_t animhash; |
31 | static animhash_t animhash; |
31 | std::vector<animation> animations; |
32 | std::vector<animation> animations; |
32 | |
33 | |
33 | void |
34 | void |
34 | animation::resize (int new_size) |
35 | animation::resize (int new_size) |
35 | { |
36 | { |
36 | sfree <faceidx> (faces, num_animations); |
37 | sfree<faceidx> (faces, num_animations); |
37 | num_animations = new_size; |
38 | num_animations = new_size; |
38 | faces = salloc<faceidx> (num_animations); |
39 | faces = salloc<faceidx> (num_animations); |
39 | } |
40 | } |
40 | |
41 | |
41 | animation & |
42 | animation & |
… | |
… | |
56 | animhash.insert (std::make_pair (anim.name, anim.number)); |
57 | animhash.insert (std::make_pair (anim.name, anim.number)); |
57 | |
58 | |
58 | return anim; |
59 | return anim; |
59 | } |
60 | } |
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 | */ |
61 | animation & |
65 | animation & |
62 | animation::find (const char *name) |
66 | animation::find (const char *name) |
63 | { |
67 | { |
64 | if (!name) |
68 | if (!name) |
65 | return animations [0]; |
69 | return animations [0]; |
… | |
… | |
67 | animhash_t::iterator i = animhash.find (name); |
71 | animhash_t::iterator i = animhash.find (name); |
68 | return animations [i == animhash.end () ? 0 : i->second]; |
72 | return animations [i == animhash.end () ? 0 : i->second]; |
69 | } |
73 | } |
70 | |
74 | |
71 | void |
75 | void |
72 | init_anim (void) |
76 | init_anim () |
73 | { |
77 | { |
74 | animation &anim0 = animation::create ("none", 1, 0); |
78 | animation &anim0 = animation::create ("none", 1, 0); |
75 | anim0.faces [0] = 0; |
79 | 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 | } |
80 | } |
87 | |
81 | |
88 | /* |
82 | /* |
89 | * animate_object(object) updates the face-variable of an object. |
83 | * 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. |
84 | * If the object is the head of a multi-object, all objects are animated. |
… | |
… | |
98 | animate_object (object *op, int dir) |
92 | animate_object (object *op, int dir) |
99 | { |
93 | { |
100 | int max_state; /* Max animation state object should be drawn in */ |
94 | int max_state; /* Max animation state object should be drawn in */ |
101 | int base_state; /* starting index # to draw from */ |
95 | int base_state; /* starting index # to draw from */ |
102 | |
96 | |
103 | if (!op->animation_id || !NUM_ANIMATIONS (op)) |
97 | if (!op->animation_id || !op->anim_frames ()) |
104 | { |
98 | { |
105 | LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ()); |
99 | LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ()); |
106 | CLEAR_FLAG (op, FLAG_ANIMATE); |
100 | op->clr_flag (FLAG_ANIMATE); |
107 | return; |
101 | return; |
108 | } |
102 | } |
109 | |
103 | |
110 | if (op->head_ () != op) |
104 | if (op->head_ () != op) |
111 | { |
105 | { |
112 | dir = op->head->direction; |
106 | dir = op->head->direction; |
113 | |
107 | |
114 | if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head)) |
108 | if (op->anim_frames () == op->head->anim_frames ()) |
115 | op->state = op->head->state; |
109 | op->state = op->head->state; |
116 | else |
110 | else |
117 | ++op->state; |
111 | ++op->state; |
118 | } |
112 | } |
119 | else |
113 | else |
120 | ++op->state; /* increase draw state */ |
114 | ++op->state; /* increase draw state */ |
121 | |
115 | |
122 | /* If object is turning, then max animation state is half through the |
116 | /* If object is turning, then max animation state is half through the |
123 | * animations. Otherwise, we can use all the animations. |
117 | * animations. Otherwise, we can use all the animations. |
124 | */ |
118 | */ |
125 | max_state = NUM_ANIMATIONS (op) / NUM_FACINGS (op); |
119 | max_state = op->anim_frames () / op->anim_facings (); |
126 | base_state = 0; |
120 | base_state = 0; |
127 | /* at least in the older animations that used is_turning, the first half |
121 | /* 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. |
122 | * 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 |
123 | * 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 |
124 | * was always towards the enemy - now it is whatever direction the monster |
131 | * is facing. |
125 | * is facing. |
132 | */ |
126 | */ |
133 | |
127 | |
134 | if (NUM_FACINGS (op) == 2) |
128 | if (dir > 0) |
|
|
129 | switch (op->anim_facings ()) |
135 | { |
130 | { |
136 | if (dir < 5) |
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 |
137 | base_state = 0; |
136 | base_state = 0; |
138 | else |
|
|
139 | base_state = NUM_ANIMATIONS (op) / 2; |
|
|
140 | } |
|
|
141 | else if (NUM_FACINGS (op) == 4) |
|
|
142 | { |
|
|
143 | if (dir == 0) |
|
|
144 | base_state = 0; |
|
|
145 | else |
|
|
146 | base_state = ((dir - 1) / 2) * (NUM_ANIMATIONS (op) / 4); |
|
|
147 | } |
|
|
148 | else if (NUM_FACINGS (op) == 8) |
|
|
149 | { |
|
|
150 | if (dir == 0) |
|
|
151 | base_state = 0; |
|
|
152 | else |
|
|
153 | base_state = (dir - 1) * (NUM_ANIMATIONS (op) / 8); |
|
|
154 | } |
|
|
155 | |
137 | |
156 | /* If beyond drawable states, reset */ |
138 | /* If beyond drawable states, reset */ |
157 | if (op->state >= max_state) |
139 | if (op->state >= max_state) |
158 | op->state = 0; |
140 | op->state = 0; |
159 | |
141 | |
160 | SET_ANIMATION (op, op->state + base_state); |
142 | op->set_anim_frame (op->state + base_state); |
161 | |
143 | |
162 | if (op->face == blank_face) |
144 | if (op->face == blank_face) |
163 | op->invisible = 1; |
145 | op->invisible = 1; |
164 | |
146 | |
165 | /* This block covers monsters (eg, pixies) which are supposed to |
147 | /* This block covers monsters (eg, pixies) which are supposed to |
166 | * cycle from visible to invisible and back to being visible. |
148 | * cycle from visible to invisible and back to being visible. |
167 | * as such, disable it for players, as then players would become |
149 | * as such, disable it for players, as then players would become |
168 | * visible. |
150 | * visible. |
169 | */ |
151 | */ |
170 | else if (op->type != PLAYER && op->arch->clone.flag [FLAG_ALIVE]) |
152 | else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE]) |
171 | { |
153 | { |
172 | if (op->face == 0) |
154 | if (op->face == 0) |
173 | { |
155 | { |
174 | op->invisible = 1; |
156 | op->invisible = 1; |
175 | CLEAR_FLAG (op, FLAG_ALIVE); |
157 | op->clr_flag (FLAG_ALIVE); |
176 | } |
158 | } |
177 | else |
159 | else |
178 | { |
160 | { |
179 | op->invisible = 0; |
161 | op->invisible = 0; |
180 | SET_FLAG (op, FLAG_ALIVE); |
162 | op->set_flag (FLAG_ALIVE); |
181 | } |
163 | } |
182 | } |
164 | } |
183 | |
165 | |
184 | if (op->more) |
166 | if (op->more) |
185 | animate_object (op->more, dir); |
167 | animate_object (op->more, dir); |