Debian Codegraph @

src/inventory.cpp

raw
1 /**
2 * Game inventory
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2007, 2008, 2009, 2010, 2012, 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 "inventory.h"
29 #include "game_engine.h"
30 #include "log.h"
31 #include "str_util.h" /* compare */
32 #include "IOGfxDisplay.h"
33 #include "gfx.h" /* GFX_lpDDSBack */
34 #include "gfx_sprites.h" /* draw_status_all, GFX_k */
35 #include "dinkini.h" /* check_seq_status */
36 #include "game_choice.h" /* talk.active */
37 #include "input.h"
38 #include "sfx.h"
39 #include "update_frame.h" /* talk_process */
40 #include "status.h" /* draw_status_all */
41
42 /* Blinking selector in the inventory screen */
43 static Uint32 item_timer;
44 static int item_pic;
45
46 /* Used by dinkc_bindings.c only */
47 void add_item(char* name, int mseq, int mframe, enum item_type type)
48 {
49 if (type == ITEM_REGULAR)
50 {
51 //add reg item
52 int i;
53 for (i = 0; i < NB_ITEMS; i++)
54 {
55 if (play.item[i].active == 0)
56 {
57 log_info("Weapon/item %s added to inventory.", name);
58 play.item[i].seq = mseq;
59 play.item[i].frame = mframe;
60 strncpy(play.item[i].name, name, sizeof(play.item[i].name));
61 play.item[i].name[sizeof(play.item[i].name)-1] = '\0';
62 play.item[i].active = 1;
63
64 int crap1 = load_script(play.item[i].name, 1000);
65 if (locate(crap1, "PICKUP"))
66 run_script(crap1);
67
68 break;
69 }
70 }
71 }
72 else
73 {
74 //add magic item
75 int i;
76 for (i = 0; i < NB_MITEMS; i++)
77 {
78 if (play.mitem[i].active == 0)
79 {
80 log_info("Magic %s added to inventory.", name);
81 play.mitem[i].seq = mseq;
82 play.mitem[i].frame = mframe;
83 strncpy(play.mitem[i].name, name, sizeof(play.mitem[i].name));
84 play.mitem[i].name[sizeof(play.mitem[i].name)-1] = '\0';
85 play.mitem[i].active = 1;
86
87 int crap = load_script(play.mitem[i].name, 1000);
88 if (locate(crap, "PICKUP"))
89 run_script(crap);
90
91 break;
92 }
93 }
94 }
95 }
96
97 /* Used by dinkc_bindings.c only */
98 void kill_cur_item()
99 {
100 if (*pcur_weapon >= 1 && *pcur_weapon <= NB_ITEMS)
101 {
102 if (play.item[*pcur_weapon - 1].active == 1)
103 {
104 if (weapon_script != 0 && locate(weapon_script, "DISARM"))
105 run_script(weapon_script);
106 weapon_script = load_script(play.item[*pcur_weapon - 1].name, 0);
107 play.item[*pcur_weapon - 1].active = 0;
108 *pcur_weapon = 0;
109 if (weapon_script != 0 && locate(weapon_script, "HOLDINGDROP"))
110 run_script(weapon_script);
111 if (weapon_script != 0 && locate(weapon_script, "DROP"))
112 run_script(weapon_script);
113 weapon_script = 0;
114 }
115 else
116 {
117 log_error("Can't kill cur item, none armed.");
118 }
119 }
120 }
121
122 /* Used by dinkc_bindings.c only */
123 void kill_item_script(char* name)
124 {
125 int select = 0;
126 {
127 int i = 0;
128 for (; i < NB_ITEMS; i++)
129 {
130 if (play.item[i].active)
131 if (compare(play.item[i].name, name))
132 {
133 select = i;
134 goto found;
135 }
136 }
137 }
138 return;
139
140 found:
141 if (*pcur_weapon - 1 == select)
142 {
143 //holding it right now
144 if (locate(weapon_script, "HOLDINGDROP"))
145 run_script(weapon_script);
146 if (locate(weapon_script, "DISARM"))
147 run_script(weapon_script);
148
149 *pcur_weapon = 0;
150 weapon_script = 0;
151 }
152
153 int script = load_script(play.item[select].name, 0);
154 play.item[select].active = /*false*/0;
155
156 if (locate(script, "DROP"))
157 run_script(script);
158
159 draw_status_all();
160 }
161
162 /* Used by dinkc_bindings.c only */
163 void kill_mitem_script(char* name)
164 {
165 int select = 0;
166 {
167 int i = 0;
168 for (; i < NB_MITEMS; i++)
169 {
170 if (play.mitem[i].active)
171 if (compare(play.mitem[i].name, name))
172 {
173 select = i;
174 goto found;
175 }
176 }
177 }
178 return;
179
180 found:
181 if (*pcur_magic - 1 == select)
182 {
183 //holding it right now
184 if (locate(magic_script, "HOLDINGDROP"))
185 run_script(magic_script);
186 if (locate(magic_script, "DISARM"))
187 run_script(magic_script);
188
189 // TODO: this should be *pcur_magic; keeping for compatibility
190 // for now:
191 *pcur_weapon = 0;
192 magic_script = 0;
193 }
194
195 int script = load_script(play.mitem[select].name, 0);
196 play.mitem[select].active = 0;
197
198 if (locate(script, "DROP"))
199 run_script(script);
200
201 draw_status_all();
202 }
203
204 /* Used by dinkc_bindings.c only */
205 void kill_cur_magic()
206 {
207 if (*pcur_magic >= 1 && *pcur_magic <= NB_MITEMS)
208 {
209 if (play.mitem[*pcur_magic - 1].active == 1)
210 {
211 if (magic_script != 0 && locate(magic_script, "DISARM"))
212 run_script(magic_script);
213 magic_script = load_script(play.mitem[*pcur_magic - 1].name, 0);
214 play.mitem[*pcur_magic - 1].active = /*false*/0;
215 *pcur_magic = 0;
216
217 if (magic_script != 0 && locate(magic_script, "HOLDINGDROP"))
218 run_script(magic_script);
219 if (magic_script != 0 && locate(magic_script, "DROP"))
220 run_script(magic_script);
221 magic_script = 0;
222 }
223 else
224 {
225 log_error("Can't kill cur magic, none armed.");
226 }
227 }
228 }
229
230
231
232 /**
233 * Draw an item icon (or an item selection square) in the inventory
234 * screen
235 */
236 static void draw_item(int item_idx0, enum item_type type, int mseq, int mframe)
237 {
238 int mx = 0;
239 int my = 0;
240
241 if (type == ITEM_REGULAR)
242 {
243 mx = 260;
244 my = 83;
245
246 mx += (item_idx0 % 4) * (18 + 65);
247 my += (item_idx0 / 4) * (20 + 55);
248 }
249 else
250 {
251 mx = 45;
252 my = 83;
253
254 mx += (item_idx0 % 2) * (18 + 65);
255 my += (item_idx0 / 2) * (20 + 55);
256 }
257
258 check_seq_status(mseq);
259
260 if (GFX_k[seq[mseq].frame[mframe]].k == NULL)
261 {
262 if (type == ITEM_REGULAR)
263 {
264 log_debug("Whups, item %d seq %d frame %d not loaded, killed it",
265 item_idx0, mseq, mframe);
266 play.item[item_idx0].active = 0;
267 }
268 else
269 {
270 log_debug("Whups, magic %d seq %d frame %d not loaded, killed it",
271 item_idx0, mseq, mframe);
272 play.mitem[item_idx0].active = 0;
273 }
274 return;
275 }
276 SDL_Rect dst;
277 dst.x = mx; dst.y = my;
278 IOGFX_backbuffer->blit(GFX_k[seq[mseq].frame[mframe]].k, NULL, &dst);
279 }
280
281
282 /**
283 * Inventory screen mode
284 */
285 void process_item()
286 {
287 IOGFX_backbuffer->blit(IOGFX_background, NULL, NULL);
288
289 check_seq_status(423);
290 //lets blit the main screen over it
291 SDL_Rect dst = {20, 0};
292 IOGFX_backbuffer->blit(GFX_k[seq[423].frame[1]].k, NULL, &dst);
293
294 //draw all currently owned items; magic
295 {
296 int i = 0;
297 for (; i < NB_MITEMS; i++)
298 if (play.mitem[i].active)
299 draw_item(i, ITEM_MAGIC, play.mitem[i].seq, play.mitem[i].frame);
300 }
301 //draw selection box around armed magic
302 if (*pcur_magic >= 1 && *pcur_magic <= NB_MITEMS && play.item[*pcur_magic - 1].active)
303 draw_item(*pcur_magic - 1, ITEM_MAGIC, 423, 5);
304
305
306 //draw all currently owned items; normal
307 {
308 int i = 0;
309 for (; i < NB_ITEMS; i++)
310 if (play.item[i].active)
311 draw_item(i, ITEM_REGULAR, play.item[i].seq, play.item[i].frame);
312 }
313 //draw selection box around armed weapon
314 if (*pcur_weapon >= 1 && *pcur_weapon <= NB_ITEMS && play.item[*pcur_weapon - 1].active)
315 draw_item(*pcur_weapon - 1, ITEM_REGULAR, 423, 4);
316
317
318 if (play.curitem < 0
319 || (play.item_type == ITEM_REGULAR && play.curitem >= NB_ITEMS)
320 || (play.item_type == ITEM_MAGIC && play.curitem >= NB_MITEMS))
321 play.curitem = 0;
322
323 if (thisTickCount > item_timer)
324 {
325 //draw the selector around it, alternating from 2 to 3
326 if (item_pic == 2)
327 item_pic = 3;
328 else
329 item_pic = 2;
330 item_timer = thisTickCount + 400;
331 }
332 draw_item(play.curitem, play.item_type, 423, item_pic);
333
334 if (play.item_type == ITEM_REGULAR)
335 {
336 int hor = play.curitem % 4;
337 int vert = play.curitem / 4;
338
339 //choosing weapon/item
340 if (sjoy.button[ACTION_ATTACK])
341 {
342 if (play.item[play.curitem].active)
343 {
344 //arm weapon
345 SoundPlayEffect(18, 42050,0,0,0);
346 if (*pcur_weapon != 0)
347 {
348 //disarm old weapon
349 if (locate(weapon_script, "DISARM"))
350 run_script(weapon_script);
351 }
352
353 //load weapons script
354 *pcur_weapon = play.curitem + 1;
355 weapon_script = load_script(play.item[*pcur_weapon - 1].name, 1000);
356 if (locate(weapon_script, "ARM"))
357 run_script(weapon_script);
358 if (locate(weapon_script, "ARMMOVIE"))
359 run_script(weapon_script);
360 draw_status_all();
361 }
362 }
363 else if (sjoy.rightd)
364 {
365 if (hor < 3)
366 {
367 play.curitem++;
368 SoundPlayEffect(11, 22050,0,0,0);
369 }
370 }
371 else if (sjoy.leftd)
372 {
373 if (hor > 0)
374 {
375 play.curitem--;
376 SoundPlayEffect(11, 22050,0,0,0);
377 }
378 else
379 {
380 SoundPlayEffect(11, 22050,0,0,0);
381 //switch to magic mode
382 play.item_type = ITEM_MAGIC;
383 play.curitem = vert * 2 + 1;
384 }
385 }
386 else if (sjoy.downd)
387 {
388 if (vert < 3)
389 {
390 play.curitem += 4;
391 SoundPlayEffect(11, 22050,0,0,0);
392 }
393 }
394 else if (sjoy.upd)
395 {
396 if (vert > 0)
397 {
398 play.curitem -= 4;
399 SoundPlayEffect(11, 22050,0,0,0);
400 }
401 }
402 }
403 else
404 {
405 int hor = play.curitem % 2;
406 int vert = play.curitem / 2;
407
408 if (sjoy.button[ACTION_ATTACK])
409 {
410 if (play.mitem[play.curitem].active)
411 {
412 //arm magic
413 SoundPlayEffect(18, 42050,0,0,0);
414 if (*pcur_magic != 0)
415 {
416 //disarm old weapon
417 if (locate(magic_script, "DISARM"))
418 run_script(magic_script);
419 }
420 //load magics script
421 *pcur_magic = play.curitem + 1;
422 magic_script = load_script(play.mitem[*pcur_magic - 1].name, 1000);
423 if (locate(magic_script, "ARM"))
424 run_script(magic_script);
425 if (locate(magic_script, "ARMMOVIE"))
426 run_script(magic_script);
427 draw_status_all();
428 }
429 }
430 else if (sjoy.rightd)
431 {
432 if (hor < 1)
433 {
434 play.curitem++;
435 SoundPlayEffect(11, 22050,0,0,0);
436 }
437 else
438 {
439 play.item_type = ITEM_REGULAR;
440 play.curitem = vert * 4;
441 SoundPlayEffect(11, 22050,0,0,0);
442 }
443 }
444 else if (sjoy.leftd)
445 {
446 if (hor > 0)
447 {
448 play.curitem--;
449 SoundPlayEffect(11, 22050,0,0,0);
450 }
451 }
452 else if (sjoy.downd)
453 {
454 if (vert < 3)
455 {
456 play.curitem += 2;
457 SoundPlayEffect(11, 22050,0,0,0);
458 }
459 }
460 else if (sjoy.upd)
461 {
462 if (vert > 0)
463 {
464 play.curitem -= 2;
465 SoundPlayEffect(11, 22050,0,0,0);
466 }
467 }
468 }
469
470 //a special process callbacks for just stuff that was created in this mode?
471 // process_callbacks_special();
472
473 if (sjoy.button[ACTION_INVENTORY])
474 {
475 SoundPlayEffect(17, 22050,0,0,0);
476 show_inventory = 0;
477 }
478 }