Debian Codegraph @

src/live_sprites_manager.cpp

raw
1 /**
2 * Live sprites manager
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2008, 2009, 2010, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "game_engine.h"
29 #include "live_sprites_manager.h"
30 #include "gfx_sprites.h"
31 #include "dinkc.h"
32
33 struct sp spr[MAX_SPRITES_AT_ONCE]; //max sprite control systems at once
34 int last_sprite_created;
35
36 void live_sprites_manager_init() {
37 memset(&spr, 0, sizeof(spr));
38 last_sprite_created = 0;
39 }
40
41 bool lsm_isValidSprite(int sprite) {
42 return (sprite > 0 && sprite < MAX_SPRITES_AT_ONCE);
43 }
44
45 void lsm_remove_sprite(int sprite) {
46 if (!lsm_isValidSprite(sprite))
47 return;
48
49 spr[sprite].active = false;
50 if (spr[sprite].custom != NULL) {
51 delete spr[sprite].custom;
52 spr[sprite].custom = NULL;
53 }
54 if (spr[sprite].text_cache != NULL) {
55 delete spr[sprite].text_cache;
56 spr[sprite].text_cache = NULL;
57 }
58
59 // if cached text sprite -> free
60 }
61
62 int add_sprite(int x1, int y, int brain,int pseq, int pframe )
63 {
64 int x;
65 for (x = 1; x < MAX_SPRITES_AT_ONCE; x++)
66 {
67 if (!spr[x].active)
68 {
69 memset(&spr[x], 0, sizeof(spr[x]));
70
71 spr[x].active = true;
72 spr[x].x = x1;
73 spr[x].y = y;
74 spr[x].my = 0;
75 spr[x].mx = 0;
76 spr[x].speed = 1;
77 spr[x].brain = brain;
78 spr[x].frame = 0;
79 spr[x].pseq = pseq;
80 spr[x].pframe = pframe;
81 spr[x].seq = 0;
82 if (x > last_sprite_created)
83 last_sprite_created = x;
84 spr[x].timing = 33;
85 spr[x].wait = 0;
86 spr[x].lpx[0] = 0;
87 spr[x].lpy[0] = 0;
88 spr[x].moveman = 0;
89 spr[x].size = 100;
90 spr[x].que = 0;
91 spr[x].strength = 0;
92 spr[x].damage = 0;
93 spr[x].defense = 0;
94 spr[x].hard = 1;
95
96 if (spr[x].custom == NULL)
97 spr[x].custom = new std::map<std::string, int>;
98 else
99 spr[x].custom->clear();
100
101 return(x);
102 }
103
104 }
105
106 return(0);
107 }
108
109 /* Like add_sprit_dumb, except:
110 * speed : 1 -> 0
111 * size : 100 -> size
112 * timer : 33 -> 0
113 * que : 0 -> ?
114 * seq_orig : ? -> 0
115 * base_hit : ? -> -1
116 * base_walk : ? -> -1
117 * base_die : ? -> -1
118 * base_idle : ? -> -1
119 * base_attack: ? -> -1
120 * last_sound : ? -> 0
121 * hard : ? -> 1
122 * althard : ? -> 0
123 * sp_index : ? -> 0
124 * nocontrol : ? -> 0
125 * idle : ? -> 0
126 * hard : 1 -> ?
127 * alt : ? -> {0,0,0,0}
128 */
129 int add_sprite_dumb(int x1, int y, int brain,int pseq, int pframe,int size )
130 {
131 int x;
132 for (x = 1; x < MAX_SPRITES_AT_ONCE; x++)
133 {
134 if (!spr[x].active)
135 {
136 memset(&spr[x], 0, sizeof(spr[x]));
137
138 //Msg("Making sprite %d.",x);
139 spr[x].active = true;
140 spr[x].x = x1;
141 spr[x].y = y;
142 spr[x].my = 0;
143 spr[x].mx = 0;
144 spr[x].speed = 0;
145 spr[x].brain = brain;
146 spr[x].frame = 0;
147 spr[x].pseq = pseq;
148 spr[x].pframe = pframe;
149 spr[x].size = size;
150 spr[x].seq = 0;
151 if (x > last_sprite_created)
152 last_sprite_created = x;
153
154 spr[x].timing = 0;
155 spr[x].wait = 0;
156 spr[x].lpx[0] = 0;
157 spr[x].lpy[0] = 0;
158 spr[x].moveman = 0;
159 spr[x].seq_orig = 0;
160
161
162 spr[x].base_hit = -1;
163 spr[x].base_walk = -1;
164 spr[x].base_die = -1;
165 spr[x].base_idle = -1;
166 spr[x].base_attack = -1;
167 spr[x].last_sound = 0;
168 spr[x].hard = 1;
169
170 rect_set(&spr[x].alt, 0,0,0,0);
171 spr[x].althard = 0;
172 spr[x].sp_index = 0;
173 spr[x].nocontrol = 0;
174 spr[x].idle = 0;
175 spr[x].strength = 0;
176 spr[x].damage = 0;
177 spr[x].defense = 0;
178
179 if (spr[x].custom == NULL)
180 spr[x].custom = new std::map<std::string, int>;
181 else
182 spr[x].custom->clear();
183
184 return(x);
185 }
186
187 }
188
189 return(0);
190 }
191
192
193 void random_blood(int mx, int my, int sprite)
194 {
195 int myseq;
196 /* v1.08 introduces custom blood sequence, as well as
197 a slightly different default (select blood in range
198 187-189 included, instead of 187-188 included) */
199 int randy;
200 if (spr[sprite].bloodseq > 0 && spr[sprite].bloodnum > 0)
201 {
202 myseq = spr[sprite].bloodseq;
203 randy = spr[sprite].bloodnum;
204 }
205 else
206 {
207 myseq = 187;
208 // TODO: dropped dversion dependency for testsuite
209 // purposes, see how to reintroduce it cleanly
210 // if (dversion >= 108)
211 // randy = 3;
212 // else
213 // randy = 2;
214 randy = 3;
215 }
216 myseq += (rand () % randy);
217
218 int crap2 = add_sprite(mx,my,5,myseq,1);
219 /* TODO: add_sprite might return 0, and the following
220 would trash spr[0] - cf. bugs.debian.org/688934 */
221 spr[crap2].speed = 0;
222 spr[crap2].base_walk = -1;
223 spr[crap2].nohit = 1;
224 spr[crap2].seq = myseq;
225 if (sprite > 0)
226 spr[crap2].que = spr[sprite].y+1;
227
228 }
229
230 static bool kill_highest_nonlive_sprite() {
231 int highest_sprite = 0;
232 bool setlast = true;
233
234 for (int k = 1; k < MAX_SPRITES_AT_ONCE; k++) {
235 if (spr[k].active) {
236 if (spr[k].live)
237 setlast = false;
238 else
239 highest_sprite = k;
240 }
241 }
242
243 if (highest_sprite > 1) {
244 lsm_remove_sprite(highest_sprite);
245 if (setlast)
246 last_sprite_created = highest_sprite - 1;
247 return true;
248 }
249
250 //we didn't kill any sprites, only 1 remains
251 return false;
252 }
253
254 /**
255 * Mark all sprites as inactive except for 'live' sprites nor spr#1
256 */
257 void lsm_kill_all_nonlive_sprites() {
258 while (kill_highest_nonlive_sprite());
259 }
260
261 void get_last_sprite() {
262 for (int i = MAX_SPRITES_AT_ONCE - 1; i > 2; i--) {
263 if (spr[i].active) {
264 last_sprite_created = i;
265 return;
266 }
267 }
268 }
269
270
271 void kill_text_owned_by(int sprite)
272 {
273 int i;
274 for (i = 1; i < MAX_SPRITES_AT_ONCE; i++)
275 {
276 if (spr[i].active && spr[i].brain == 8 && spr[i].text_owner == sprite)
277 lsm_remove_sprite(i);
278 }
279 }
280
281
282
283
284 int does_sprite_have_text(int sprite) {
285 for (int i = 1; i < MAX_SPRITES_AT_ONCE; i++)
286 if (spr[i].active && spr[i].text_owner == sprite && spr[i].brain == 8)
287 return i;
288 return 0;
289 }
290
291
292 /**
293 * Is 'sprite' currently talking?
294 * Returns 1 if a text sprite is owned by sprite number 'sprite'.
295 */
296 /*bool*/int text_owned_by(int sprite)
297 {
298 int i = 1;
299 for (; i < MAX_SPRITES_AT_ONCE; i++)
300 if (spr[i].active && spr[i].brain == 8 && spr[i].text_owner == sprite)
301 return /*true*/1;
302 return /*false*/0;
303 }
304
305
306 /**
307 * Find an editor sprite in active sprites
308 */
309 int find_sprite(int editor_sprite)
310 {
311 int k;
312 for (k = 1; k <= last_sprite_created; k++)
313 if (spr[k].sp_index == editor_sprite)
314 return k;
315 return 0;
316 }
317