1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game for X-windows |
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 (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (C) 2002-2003 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002-2003 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * 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 |
9 | * 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 |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * 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 | * |
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 at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | /* This file contains animation related code. */ |
25 | /* This file contains animation related code. */ |
26 | |
26 | |
27 | #include <global.h> |
27 | #include <global.h> |
28 | #include <stdio.h> |
28 | #include <stdio.h> |
29 | |
29 | |
30 | animhash_t animhash; |
30 | static animhash_t animhash; |
31 | std::vector<animation> animations; |
31 | std::vector<animation> animations; |
32 | |
32 | |
33 | void |
33 | void |
34 | animation::resize (int new_size) |
34 | animation::resize (int new_size) |
35 | { |
35 | { |
36 | sfree <faceidx> (faces, num_animations); |
36 | sfree<faceidx> (faces, num_animations); |
37 | num_animations = new_size; |
37 | num_animations = new_size; |
38 | faces = salloc<faceidx> (num_animations); |
38 | faces = salloc<faceidx> (num_animations); |
39 | } |
39 | } |
40 | |
40 | |
41 | animation & |
41 | animation & |
… | |
… | |
56 | animhash.insert (std::make_pair (anim.name, anim.number)); |
56 | animhash.insert (std::make_pair (anim.name, anim.number)); |
57 | |
57 | |
58 | return anim; |
58 | return anim; |
59 | } |
59 | } |
60 | |
60 | |
|
|
61 | /* 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 | */ |
61 | animation & |
64 | animation & |
62 | animation::find (const char *name) |
65 | animation::find (const char *name) |
63 | { |
66 | { |
64 | if (!name) |
67 | if (!name) |
65 | return animations [0]; |
68 | return animations [0]; |
… | |
… | |
67 | animhash_t::iterator i = animhash.find (name); |
70 | animhash_t::iterator i = animhash.find (name); |
68 | return animations [i == animhash.end () ? 0 : i->second]; |
71 | return animations [i == animhash.end () ? 0 : i->second]; |
69 | } |
72 | } |
70 | |
73 | |
71 | void |
74 | void |
72 | init_anim (void) |
75 | init_anim () |
73 | { |
76 | { |
74 | animation &anim0 = animation::create ("none", 1, 0); |
77 | animation &anim0 = animation::create ("none", 1, 0); |
75 | anim0.faces [0] = 0; |
78 | 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 | } |
79 | } |
87 | |
80 | |
88 | /* |
81 | /* |
89 | * animate_object(object) updates the face-variable of an object. |
82 | * 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. |
83 | * If the object is the head of a multi-object, all objects are animated. |
… | |
… | |
101 | int base_state; /* starting index # to draw from */ |
94 | int base_state; /* starting index # to draw from */ |
102 | |
95 | |
103 | if (!op->animation_id || !NUM_ANIMATIONS (op)) |
96 | if (!op->animation_id || !NUM_ANIMATIONS (op)) |
104 | { |
97 | { |
105 | LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ()); |
98 | LOG (llevError, "Object %s lacks animation.\n", op->debug_desc ()); |
106 | CLEAR_FLAG (op, FLAG_ANIMATE); |
99 | op->clr_flag (FLAG_ANIMATE); |
107 | return; |
100 | return; |
108 | } |
101 | } |
109 | |
102 | |
110 | if (op->head) |
103 | if (op->head_ () != op) |
111 | { |
104 | { |
112 | dir = op->head->direction; |
105 | dir = op->head->direction; |
113 | |
106 | |
114 | if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head)) |
107 | if (NUM_ANIMATIONS (op) == NUM_ANIMATIONS (op->head)) |
115 | op->state = op->head->state; |
108 | op->state = op->head->state; |
… | |
… | |
129 | * Note in old the is_turning, it was set so that the animation for a monster |
122 | * 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 |
123 | * was always towards the enemy - now it is whatever direction the monster |
131 | * is facing. |
124 | * is facing. |
132 | */ |
125 | */ |
133 | |
126 | |
|
|
127 | if (dir > 0) |
134 | if (NUM_FACINGS (op) == 2) |
128 | switch (NUM_FACINGS (op)) |
135 | { |
129 | { |
136 | if (dir < 5) |
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 |
137 | base_state = 0; |
135 | 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 | |
136 | |
156 | /* If beyond drawable states, reset */ |
137 | /* If beyond drawable states, reset */ |
157 | if (op->state >= max_state) |
138 | if (op->state >= max_state) |
158 | op->state = 0; |
139 | op->state = 0; |
159 | |
140 | |
… | |
… | |
165 | /* This block covers monsters (eg, pixies) which are supposed to |
146 | /* This block covers monsters (eg, pixies) which are supposed to |
166 | * cycle from visible to invisible and back to being visible. |
147 | * cycle from visible to invisible and back to being visible. |
167 | * as such, disable it for players, as then players would become |
148 | * as such, disable it for players, as then players would become |
168 | * visible. |
149 | * visible. |
169 | */ |
150 | */ |
170 | else if (op->type != PLAYER && op->arch->clone.flag [FLAG_ALIVE]) |
151 | else if (op->type != PLAYER && op->arch->flag [FLAG_ALIVE]) |
171 | { |
152 | { |
172 | if (op->face == 0) |
153 | if (op->face == 0) |
173 | { |
154 | { |
174 | op->invisible = 1; |
155 | op->invisible = 1; |
175 | CLEAR_FLAG (op, FLAG_ALIVE); |
156 | op->clr_flag (FLAG_ALIVE); |
176 | } |
157 | } |
177 | else |
158 | else |
178 | { |
159 | { |
179 | op->invisible = 0; |
160 | op->invisible = 0; |
180 | SET_FLAG (op, FLAG_ALIVE); |
161 | op->set_flag (FLAG_ALIVE); |
181 | } |
162 | } |
182 | } |
163 | } |
183 | |
164 | |
184 | if (op->more) |
165 | if (op->more) |
185 | animate_object (op->more, dir); |
166 | animate_object (op->more, dir); |
186 | |
167 | |
187 | /* update_object will also recursively update all the pieces. |
168 | /* update_object will also recursively update all the pieces. |
188 | * as such, we call it last, and only call it for the head |
169 | * as such, we call it last, and only call it for the head |
189 | * piece, and not for the other tail pieces. |
170 | * piece, and not for the other tail pieces. |
190 | */ |
171 | */ |
191 | if (!op->head) |
172 | if (op->head_ () == op) |
192 | update_object (op, UP_OBJ_FACE); |
173 | update_object (op, UP_OBJ_FACE); |
193 | } |
174 | } |
194 | |
175 | |