Debian Codegraph @

Descent3/Inventory.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/Inventory.cpp $
21 * $Revision: 80 $
22 * $Date: 5/19/99 4:23p $
23 * $Author: Chris $
24 *
25 * Inventory control source file
26 *
27 * $Log: /DescentIII/main/Inventory.cpp $
28 *
29 * 80 5/19/99 4:23p Chris
30 * Fixed level goal problems with keys and persistant inventory items
31 *
32 * 79 5/19/99 3:24p Jason
33 * fixed wrong ordering of InitObjectScripts and MultiSendObject
34 *
35 * 78 5/12/99 6:01p Jeff
36 * sanity check. fixed reset
37 *
38 * 76 5/08/99 4:12p Chris
39 * Added AI hearing noises... version 1
40 *
41 * 75 4/29/99 1:28p Jeff
42 * play sound when switching inven/countermeasres
43 *
44 * 74 4/28/99 8:32p Jeff
45 * fixed bug adding an object to the inventory, was forgetting to set
46 * count
47 *
48 * 73 4/28/99 5:20p Jeff
49 * fixed missing return
50 *
51 * 72 4/28/99 5:22a Jeff
52 * added function to get list of inventory items, without having to go
53 * through inventory one by one
54 *
55 * 71 4/25/99 5:20p Chris
56 * Made the GB work with the inventory system... CT_AI not remapped for
57 * non-vis USED inventory items.
58 *
59 * 70 4/20/99 3:06p Jeff
60 * fixed inventory displaying of non-usable items
61 *
62 * 69 4/20/99 1:14p Samir
63 * added function to determine if inventory item is usable.
64 *
65 * 68 4/14/99 2:50a Jeff
66 * fixed some case mismatched #includes
67 *
68 * 67 3/02/99 4:41p Jeff
69 * Fixed inventory save/load
70 *
71 * 66 2/25/99 8:54p Jeff
72 * Inventory supports level change persistant items. Inventory supports
73 * time-out objects. Inventory Reset changed (takes a level of reset
74 * now). Quad lasers stay across level change (single player). Guidebot
75 * bug fixed (now back in ship on level start). Quads time out when
76 * spewed. Invulnerability and cloak powerups no longer use game
77 * event/callbacks, so they can be saved in game saves (moved to
78 * MakePlayerInvulnerable and MakeObjectInvisible)
79 *
80 * 65 2/23/99 7:37p Jeff
81 * fixed reset so it doesn't remove non-spewable items...made multiplayer
82 * friendly
83 *
84 * 64 2/22/99 1:20a Jeff
85 * added support for inventory (simple) in dallas. Moved end-level
86 * sequence to use IGC. Add position clipboard stuff for dallas. Fixed
87 * some inventory bug with storing object handles
88 *
89 * 63 2/14/99 4:29a Jeff
90 * able to set custom descriptions for an item when adding it
91 *
92 * 62 2/13/99 12:37a Jeff
93 * new inventory system. Supports objects that don't die when put in (by
94 * objhandles). Also changed Inventory::Reset()
95 *
96 * 61 2/08/99 5:24p Jeff
97 * removed all calls to MultiSendRemoveObject, incorportated into
98 * SetObjectDeadFlag. Fixes sequencing issues in multiplayer
99 *
100 * 60 1/21/99 11:15p Jeff
101 * pulled out some structs and defines from header files and moved them
102 * into seperate header files so that multiplayer dlls don't require major
103 * game headers, just those new headers. Side effect is a shorter build
104 * time. Also cleaned up some header file #includes that weren't needed.
105 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
106 * manage.h and multi.h
107 *
108 * 59 1/13/99 2:28a Chris
109 * Massive AI, OSIRIS update
110 *
111 * 58 1/08/99 2:55p Samir
112 * Ripped out OSIRIS1.
113 *
114 * 57 1/04/99 12:23p Jeff
115 * added to evt_use and support for mission module scripts
116 *
117 * 56 12/17/98 12:08p Jeff
118 * first checkin of new implementation of OSIRIS (old OSIRIS no longer
119 * works)
120 *
121 * 55 10/08/98 4:23p Kevin
122 * Changed code to comply with memory library usage. Always use mem_malloc
123 * , mem_free and mem_strdup
124 *
125 * 54 10/06/98 2:37p Jeff
126 * fixed next,prev pos bugs
127 *
128 * 53 10/03/98 8:05p Matt
129 * Added asserts
130 *
131 * 52 8/31/98 6:50p Jeff
132 * made inventory and countermeasure keys customizable
133 *
134 * 51 8/25/98 1:35p Jeff
135 * fixed 0 byte malloc and removed mprintfs
136 *
137 * 50 8/19/98 2:17p Jeff
138 * added a function to get the aux type/id
139 *
140 * 49 8/19/98 12:38p Jason
141 * made countermeasure spewing work correctly
142 *
143 * 48 8/16/98 2:00a Jeff
144 * fixed ugly mprintf
145 *
146 * 47 8/13/98 11:56a Jeff
147 * handle new flags for inventory use
148 *
149 * 46 8/12/98 10:38a Jeff
150 * fixed some major bugs with reset
151 *
152 * 45 8/10/98 11:18a Jeff
153 * reset takes a bool now whether to reset everything
154 *
155 * 44 7/23/98 6:26p Jeff
156 * added checks to make sure scripts match
157 *
158 * 43 7/11/98 9:14p Jeff
159 * fixed an mprintf
160 *
161 * 42 7/10/98 7:49p Jeff
162 * fixed way of getting icon info for countermeasure
163 *
164 * 41 7/09/98 7:51p Jeff
165 * forgot to tell the clients to remove countermeasures on use
166 *
167 * 40 7/09/98 5:44p Jeff
168 * fixed bug with unique count for countermeasures
169 *
170 * 39 7/08/98 6:01p Jeff
171 * first chance at making multiplayer friendly
172 *
173 * 38 7/06/98 7:17p Jeff
174 * countermeasure support added
175 *
176 * 37 7/03/98 3:10p Jeff
177 * some error handling and added functions to get inventory information
178 *
179 * 36 5/25/98 6:38p Matt
180 * Added needed include.
181 *
182 * 35 5/11/98 4:40p Chris
183 * AI info is now a malloc'ed pointer
184 *
185 * 34 5/08/98 1:38p Jeff
186 * D3XExecScript returns true by default, not false (made change in
187 * EVT_USE)
188 *
189 * 33 5/08/98 1:31p Jeff
190 * if EVT_USE returns true, it won't remove item from inventory on use
191 *
192 * 32 4/24/98 7:09p Jeff
193 * added a flag for non-useable inventory items
194 *
195 * 31 4/23/98 12:02p Jeff
196 * added a limit to how many items you can put in your inventory
197 *
198 * 30 4/19/98 7:32p Jeff
199 * Moved inventory wrappers to DLLWrappers
200 *
201 * 29 4/09/98 6:43p Craig
202 *
203 * 28 4/09/98 6:09p Craig
204 *
205 * 27 4/09/98 6:02p Craig
206 * Fixed the non-linked player getting an inventory add crash.
207 *
208 * 26 4/03/98 11:49a Jeff
209 * Added another function wrapper, renamed others to fit naming convention
210 *
211 * 25 3/31/98 3:55p Jeff
212 * Added some inventory wrappers
213 *
214 * 24 3/26/98 2:58p Jeff
215 * Added a GetTypeIDCount function
216 *
217 * 23 3/23/98 5:36p Jeff
218 * added a parameter to Add to specify if you want it destroyed when it's
219 * added
220 *
221 * 22 3/20/98 9:34p Jason
222 * added SetObjectDeadFlag inlined function
223 *
224 * 21 3/20/98 2:58p Jason
225 * fixed Jeff's dumb error :P
226 *
227 * 20 3/20/98 2:44p Jeff
228 * removed ValidatePos() from ResetPos()....ValidatePos() should be called
229 * after a ResetPos() if you want to make sure you are on a selectable
230 * item
231 *
232 * 19 2/20/98 5:50p Jeff
233 * Changed it so that whether an inventory item was selected was placed in
234 * the objinfo flags
235 *
236 * 18 2/20/98 5:02p Jeff
237 * fixed bug so you can switch to a nonselectable item while playing the
238 * game
239 *
240 * 17 2/20/98 4:56p Jeff
241 * Changed inventory so it now supports non selectable items, plus made
242 * the list into a circular list
243 *
244 * 16 2/15/98 4:49p Jeff
245 * Added a reset function to clear the inventory
246 *
247 * 15 2/12/98 1:41p Jeff
248 * commented out constructor/destructor mprintfs
249 *
250 * 14 2/11/98 4:54p Jeff
251 * Moved the inventory into the Player struct
252 *
253 * 13 2/09/98 4:58p Jeff
254 * Connected inventory to hud
255 *
256 * 12 2/07/98 6:34p Jeff
257 *
258 * 11 2/06/98 2:04p Jeff
259 * inventory system much more stable now, and works with telcom
260 *
261 * 10 2/05/98 7:38p Jeff
262 * changed inventory system completly...now is classes...works in telcom
263 * to, added use feature
264 *
265 * 9 2/04/98 7:42p Jeff
266 * began connecting telcom to inventory
267 *
268 * 8 2/04/98 6:09p Matt
269 * Changed object room number to indicate a terrain cell via a flag. Got
270 * rid of the object flag which used to indicate terrain.
271 *
272 * 7 2/04/98 12:18p Jeff
273 *
274 * 6 2/03/98 6:15p Jeff
275 *
276 * 5 1/31/98 8:49p Jeff
277 * Added a function to count the number of items in the inventory
278 *
279 * 4 1/31/98 8:12p Jeff
280 * Added more inventory functions
281 *
282 * 3 1/30/98 7:02p Jeff
283 * wrote basic internals inven system
284 *
285 * 2 1/29/98 3:52p Jeff
286 * initial creation
287 *
288 * $NoKeywords: $
289 */
290
291 #include "Inventory.h"
292
293 #include "mono.h"
294 #include "player.h"
295 #include "pserror.h"
296 #include "objinfo.h"
297 #include "room.h"
298 #include "weapon.h"
299 #include "game.h"
300 #include "multi.h"
301 #include "osiris_dll.h"
302 #include "mem.h"
303 #include "ObjScript.h"
304 #include "osiris_share.h"
305 #include "stringtable.h"
306 #include "hud.h"
307 #include "hlsoundlib.h"
308 #include "sounds.h"
309 #include "AIMain.h"
310 #include "levelgoal.h"
311
312 // constructor
313 Inventory::Inventory(void) {
314 // mprintf(0,"Inventory System: Initialize\n");
315 root = NULL;
316 count = 0;
317 pos = NULL;
318 }
319
320 // destructor
321 Inventory::~Inventory(void) { Reset(false); }
322
323 // Resets the inventory, cleaning it out
324 // in_game: set to true if this is being called from during gameplay
325 // reset_stage:
326 // INVRESET_ALL: Reset _EVERYTHING_
327 // INVRESET_LEVELCHANGE: Remove everything except those that last across levels
328 // INVRESET_DEATHSPEW: Remove everything except those that do not spew (Default)
329 void Inventory::Reset(bool in_game, int reset_stage) {
330 if (reset_stage < 0 || reset_stage > 2)
331 reset_stage = 2;
332
333 inven_item *current = root, *next, *new_root;
334
335 int item_count = count;
336 new_root = NULL;
337 object *obj;
338
339 bool remove_nonspewers = false;
340 bool remove_levelchangers = false;
341
342 switch (reset_stage) {
343 case INVRESET_ALL: // everything (mission over)
344 remove_nonspewers = true;
345 remove_levelchangers = true;
346 break;
347 case INVRESET_LEVELCHANGE: // leave level static (level change)
348 remove_nonspewers = true;
349 remove_levelchangers = false;
350 break;
351 case INVRESET_DEATHSPEW: // leave non spewers (player death spew reset)
352 remove_nonspewers = false;
353 remove_levelchangers = true;
354 break;
355 }
356
357 bool no_spew, leave_across_level, should_spew;
358
359 while (item_count) {
360 no_spew = false;
361 leave_across_level = false;
362 should_spew = true;
363
364 next = current->next;
365
366 if (current->iflags & INVF_NOTSPEWABLE)
367 no_spew = true;
368 if (current->iflags & INVF_MISSIONITEM)
369 leave_across_level = true;
370
371 // determine if we should spew
372 if (leave_across_level && !remove_levelchangers && (!(current->iflags & INVF_OBJECT)))
373 should_spew = false;
374 if (no_spew && !remove_nonspewers)
375 should_spew = false;
376
377 if (in_game && current->iflags & INVF_OBJECT && should_spew) {
378 // unmark this object as being in an inventory
379 obj = ObjGet(current->type);
380 if (obj) {
381 obj->flags &= ~OF_INPLAYERINVENTORY;
382 }
383 }
384
385 if (should_spew)
386 RemoveNode(current);
387
388 item_count--;
389 current = next;
390 }
391 ValidatePos(true);
392 }
393
394 // adds an object to the inventory (marked by it's objhandle)
395 bool Inventory::AddObject(int object_handle, int flags, const char *description) {
396 // make sure we can fit another object
397 if (count >= MAX_UNIQUE_INVEN_ITEMS) {
398 mprintf(0, "Max unique count hit on add to inventory\n");
399 return false;
400 }
401
402 object *obj = ObjGet(object_handle);
403 if (!obj) {
404 mprintf(0, "INVEN: Invalid object trying to be added\n");
405 return false;
406 }
407
408 if (obj->flags & OF_INFORM_DESTROY_TO_LG) {
409 Level_goals.Inform(LIT_OBJECT, LGF_COMP_DESTROY, obj->handle);
410 }
411
412 bool in_as_dummy = false;
413 if (obj->type == OBJ_DUMMY) {
414 // type coming in is already dummy, un-dummy before adding it
415 ObjUnGhostObject(OBJNUM(obj));
416 in_as_dummy = true;
417 }
418
419 ASSERT(obj->type == OBJ_BUILDING || obj->type == OBJ_ROBOT || obj->type == OBJ_POWERUP || obj->type == OBJ_CLUTTER);
420
421 inven_item *current = root, *prev = root, *newnode;
422
423 if (count == 0) {
424 // there are no items in the list...time to add
425 root = new inven_item;
426 newnode = root;
427 root->next = root;
428 root->prev = root;
429 newnode->iflags = INVF_OBJECT;
430 newnode->count = 1;
431 } else {
432 newnode = new inven_item;
433
434 prev = root->prev;
435
436 newnode->prev = prev;
437 prev->next = newnode;
438 newnode->next = root;
439 root->prev = newnode;
440
441 newnode->iflags = INVF_OBJECT;
442 newnode->count = 1;
443 }
444
445 newnode->type = object_handle;
446 newnode->id = -1;
447 newnode->flags = 0;
448 newnode->otype = obj->type;
449 newnode->oid = obj->id;
450
451 if (Object_info[newnode->oid].description) {
452 newnode->description = mem_strdup(Object_info[newnode->oid].description);
453 } else {
454 newnode->description = (char *)mem_malloc(sizeof(char));
455 newnode->description[0] = 0;
456 }
457
458 if (Object_info[newnode->oid].flags & OIF_INVEN_SELECTABLE)
459 newnode->iflags |= INVF_SELECTABLE;
460 // if(!(Object_info[newnode->oid].flags & OIF_INVEN_NONUSEABLE))
461 // newnode->iflags |= INVF_USEABLE;
462 if (Object_info[newnode->oid].flags & OIF_INVEN_TYPE_MISSION)
463 newnode->iflags |= INVF_MISSIONITEM;
464 if (Object_info[newnode->oid].flags & OIF_INVEN_NOREMOVE)
465 newnode->iflags |= INVF_NOREMOVEONUSE;
466 if (Object_info[newnode->oid].flags & OIF_INVEN_VISWHENUSED)
467 newnode->iflags |= INVF_VISWHENUSED;
468
469 if (flags & INVAF_NOTSPEWABLE)
470 newnode->iflags |= INVF_NOTSPEWABLE;
471 if (flags & INVAF_TIMEOUTONSPEW)
472 newnode->iflags |= INVF_TIMEOUTONSPEW;
473 if (flags & INVAF_LEVELLAST)
474 newnode->iflags |= INVF_MISSIONITEM;
475
476 obj->flags |= OF_INPLAYERINVENTORY;
477
478 if (in_as_dummy || (!(Game_mode & GM_MULTI)) || Netgame.local_role == LR_SERVER) {
479 ObjGhostObject(OBJNUM(obj));
480
481 if (Game_mode & GM_MULTI && Netgame.local_role == LR_SERVER) {
482 MultiSendGhostObject(obj, true);
483 }
484 }
485
486 newnode->icon_name = mem_strdup(Object_info[newnode->oid].icon_name);
487
488 if (description) {
489 newnode->name = mem_strdup(description);
490 } else {
491 newnode->name = mem_strdup(Object_info[newnode->oid].name);
492 }
493
494 count++;
495
496 if (newnode->iflags & INVF_SELECTABLE)
497 pos = newnode;
498
499 return true;
500 }
501
502 // adds a new type/id item to the inventory
503 bool Inventory::Add(int type, int id, object *parent, int aux_type, int aux_id, int flags, const char *description) {
504 // make sure we can fit another object
505 if (count >= MAX_UNIQUE_INVEN_ITEMS) {
506 mprintf(0, "Max unique count hit on add to inventory\n");
507 return false;
508 }
509
510 if ((type < 0) || (type == OBJ_NONE)) {
511 mprintf(0, "Invalid type on add to inventory\n");
512 return false;
513 }
514
515 if (type != OBJ_WEAPON) {
516 ASSERT(type == OBJ_BUILDING || type == OBJ_ROBOT || type == OBJ_POWERUP || type == OBJ_CLUTTER);
517 return AddObjectItem(type, id, (aux_type != -1) ? aux_type : type, (aux_id != -1) ? aux_id : id, flags,
518 description);
519 } else {
520 // special case for countermeasures
521 return AddCounterMeasure(id, aux_type, aux_id, flags, description);
522 }
523
524 return false;
525 }
526
527 // adds a special cased CounterMeasure into the inventory
528 bool Inventory::AddCounterMeasure(int id, int aux_type, int aux_id, int flags, const char *description) {
529 // make sure we can fit another object
530 if (count >= MAX_UNIQUE_INVEN_ITEMS) {
531 mprintf(0, "Hit max unique in counter measure add\n");
532 return false;
533 }
534
535 inven_item *current = root, *prev = root, *newnode;
536
537 if (count == 0) {
538 // there are no items in the list...time to add
539 root = new inven_item;
540 newnode = root;
541 root->next = root;
542 root->prev = root;
543 newnode->count = 1;
544 } else {
545 newnode = FindItem(OBJ_WEAPON, id);
546
547 if (!newnode) {
548 // NEEDTODO: adjust so it adds in order
549
550 newnode = new inven_item;
551
552 prev = root->prev;
553
554 newnode->prev = prev;
555 prev->next = newnode;
556 newnode->next = root;
557 root->prev = newnode;
558
559 newnode->count = 1;
560 } else {
561 // there is an item of that type/id already, just increase it's count
562 newnode->count++;
563 // mprintf(0,"Inventory: Item #%d (%s) Count increased to %d\n",count,newnode->name,newnode->count);
564 }
565 }
566
567 // its a new item type/id, so fill in its info
568 if (newnode->count == 1) {
569 newnode->type = OBJ_WEAPON;
570 newnode->id = id;
571 newnode->flags = 0;
572 newnode->iflags = 0;
573 newnode->otype = aux_type;
574 newnode->oid = aux_id;
575
576 if ((aux_type != -1) && (aux_id != -1) && (Object_info[aux_id].description)) {
577 newnode->description = mem_strdup(Object_info[aux_id].description);
578 } else {
579 newnode->description = mem_strdup(Weapons[id].name);
580 }
581
582 newnode->iflags |= INVF_SELECTABLE | INVF_USEABLE | INVF_MISSIONITEM | INVF_TIMEOUTONSPEW;
583
584 newnode->icon_name =
585 (char *)mem_malloc(strlen(GameBitmaps[GameTextures[Weapons[id].icon_handle].bm_handle].name) + 1);
586 strcpy(newnode->icon_name, GameBitmaps[GameTextures[Weapons[id].icon_handle].bm_handle].name);
587
588 if (description) {
589 newnode->name = mem_strdup(description);
590 } else {
591 newnode->name = mem_strdup(Weapons[id].name);
592 }
593
594 count++;
595 // mprintf(0,"Inventory: Item #%d Added Countermeasure (%s) ID=%d\n",count,newnode->name,newnode->id);
596 }
597
598 pos = newnode;
599 return true;
600 }
601
602 // adds an object to the inventory
603 bool Inventory::AddObjectItem(int otype, int oid, int oauxt, int oauxi, int flags, const char *description) {
604 // make sure we can fit another object
605 if (count >= MAX_UNIQUE_INVEN_ITEMS)
606 return false;
607
608 inven_item *current = root, *prev = root, *newnode;
609
610 if (count == 0) {
611 // there are no items in the list...time to add
612 root = new inven_item;
613 newnode = root;
614 root->next = root;
615 root->prev = root;
616 newnode->count = 1;
617 } else {
618 newnode = FindItem(otype, oid);
619
620 if (!newnode) {
621 // NEEDTODO: adjust so it adds in order
622
623 newnode = new inven_item;
624
625 prev = root->prev;
626
627 newnode->prev = prev;
628 prev->next = newnode;
629 newnode->next = root;
630 root->prev = newnode;
631
632 newnode->count = 1;
633 } else {
634 // there is an item of that type/id already, just increase it's count
635 newnode->count++;
636 }
637 }
638
639 // its a new item type/id, so fill in its info
640 if (newnode->count == 1) {
641 newnode->type = otype;
642 newnode->id = oid;
643 newnode->flags = 0;
644 newnode->iflags = 0;
645 newnode->otype = oauxt;
646 newnode->oid = oauxi;
647
648 if (Object_info[oid].description) {
649 newnode->description = (char *)mem_malloc(strlen(Object_info[oid].description) + 1);
650 strcpy(newnode->description, Object_info[oid].description);
651 } else {
652 newnode->description = (char *)mem_malloc(sizeof(char));
653 newnode->description[0] = 0;
654 }
655
656 if (Object_info[oid].flags & OIF_INVEN_SELECTABLE)
657 newnode->iflags |= INVF_SELECTABLE;
658 // if(!(Object_info[oid].flags & OIF_INVEN_NONUSEABLE))
659 // newnode->iflags |= INVF_USEABLE;
660 if (Object_info[oid].flags & OIF_INVEN_TYPE_MISSION)
661 newnode->iflags |= INVF_MISSIONITEM;
662 if (Object_info[oid].flags & OIF_INVEN_NOREMOVE)
663 newnode->iflags |= INVF_NOREMOVEONUSE;
664 if (Object_info[oid].flags & OIF_INVEN_VISWHENUSED)
665 newnode->iflags |= INVF_VISWHENUSED;
666
667 if (flags & INVAF_NOTSPEWABLE)
668 newnode->iflags |= INVF_NOTSPEWABLE;
669 if (flags & INVAF_TIMEOUTONSPEW)
670 newnode->iflags |= INVF_TIMEOUTONSPEW;
671 if (flags & INVAF_LEVELLAST)
672 newnode->iflags |= INVAF_LEVELLAST;
673
674 newnode->icon_name = (char *)mem_malloc(strlen(Object_info[oid].icon_name) + 1);
675 strcpy(newnode->icon_name, Object_info[oid].icon_name);
676
677 if (description) {
678 newnode->name = mem_strdup(description);
679 } else {
680 newnode->name = mem_strdup(Object_info[oid].name);
681 }
682
683 count++;
684 }
685
686 if (newnode->iflags & INVF_SELECTABLE)
687 pos = newnode;
688
689 return true;
690 }
691
692 // uses an item in the inventory (returns false if the item doesn't exist)
693 bool Inventory::Use(int type, int id, object *parent) {
694 inven_item *node;
695
696 node = FindItem(type, id);
697
698 if (!node)
699 return false;
700
701 if (!(node->iflags & INVF_USEABLE))
702 return false;
703
704 bool multiplayer = (bool)((Game_mode & GM_MULTI) != 0);
705 bool client;
706 bool server;
707 bool ret = false;
708
709 if (multiplayer) {
710 if (Netgame.local_role & LR_SERVER) {
711 client = false;
712 server = true;
713 } else {
714 client = true;
715 server = false;
716 }
717 } else {
718 client = false;
719 server = false;
720 }
721
722 if (client) {
723 // OK, we're a client in a multiplayer game, so send a request to the server to use this Item
724 SendRequestToServerToUse(node->type, node->id);
725
726 return false;
727 }
728
729 // If we got here, then we are either the server in a multiplayer game, or in a single player game
730
731 // get player object (needed when we recreate the object)
732 object *player;
733 ASSERT(parent);
734 player = parent;
735
736 if (player == NULL) {
737 Int3();
738 return false;
739 }
740
741 // if type is OBJ_WEAPON then it's a countermeasure
742 if (type == OBJ_WEAPON) {
743 mprintf(0, "CounterMeasures: Use\n");
744 // countermeasure
745 CreateCountermeasureFromObject(player, id);
746 Remove(node->type, node->id);
747 ret = true;
748 } else {
749 mprintf(0, "Inventory: Use\n");
750 // regular
751 // recreate the object
752 int objnum;
753 int roomnum;
754 bool remove_on_use;
755 bool vis_when_created = false;
756
757 if (node->iflags & INVF_NOREMOVEONUSE)
758 remove_on_use = false;
759 else
760 remove_on_use = true;
761
762 if (node->iflags & INVF_VISWHENUSED)
763 vis_when_created = true;
764 else
765 vis_when_created = false;
766
767 roomnum = player->roomnum;
768
769 if (node->iflags & INVF_OBJECT) {
770 // don't recreate the object..it already exists
771 object *obj = ObjGet(node->type);
772 if (!obj) {
773 Int3(); // object no longer exists
774 return false;
775 }
776 objnum = OBJNUM(obj);
777
778 obj->flags &= ~OF_INPLAYERINVENTORY;
779
780 if (vis_when_created) {
781 ObjUnGhostObject(objnum);
782 MultiSendGhostObject(obj, false);
783 }
784
785 } else {
786
787 objnum = ObjCreate(node->type, node->id, roomnum, &player->pos, NULL, player->handle);
788 if (objnum == -1) {
789 Int3();
790 return false;
791 }
792
793 if (!vis_when_created) {
794 if (Objects[objnum].control_type != CT_AI) {
795 SetObjectControlType(&Objects[objnum], CT_NONE);
796 }
797 Objects[objnum].movement_type = MT_NONE;
798 Objects[objnum].render_type = RT_NONE;
799 }
800
801 Objects[objnum].flags = node->flags;
802
803 if (server) // if we're the server, then we need to send this object to the clients
804 MultiSendObject(&Objects[objnum], 0);
805
806 InitObjectScripts(&Objects[objnum]);
807 }
808
809 tOSIRISEventInfo ei;
810 ei.evt_use.it_handle = player->handle;
811 if (Osiris_CallEvent(&Objects[objnum], EVT_USE, &ei)) {
812 // if we're the server tell the clients to remove this item from their inventory
813 Remove(node->type, node->id);
814 ret = true;
815 } else {
816 if (node->iflags & INVF_OBJECT)
817 Objects[objnum].flags |= OF_INPLAYERINVENTORY; // mark as being in inventory
818 }
819
820 if (remove_on_use) {
821 // now we need to kill the object
822 SetObjectDeadFlag(&Objects[objnum], true);
823 }
824 }
825 return ret;
826 }
827
828 // sends a request to the server to use a particular item in the inventory
829 void Inventory::SendRequestToServerToUse(int type, int id) {
830 // mprintf(0,"Sending request to server for T=%d ID=%d\n",type,id);
831 inven_item *node = FindItem(type, id);
832 if (node) {
833 MultiSendClientInventoryUseItem(type, id);
834 } else {
835 mprintf(0, "Sorry couldn't find it in your inventory\n");
836 }
837 }
838
839 // searches the inventory for the specified type/id, sets the pos to it
840 bool Inventory::FindPos(int type, int id) {
841 int oldt, oldi;
842 int ttype, tid;
843
844 // save current pos
845 GetPosTypeID(oldt, oldi);
846
847 // try to move to the specified pos
848 GotoPos(type, id);
849
850 // see if we got there
851 GetPosTypeID(ttype, tid);
852 if ((ttype == type) && (tid == id))
853 return true;
854 else {
855 // nope, so restore the old pos
856 GotoPos(oldt, oldi);
857 return false;
858 }
859 }
860
861 // uses an item in the inventory (currently selected one) (returns false if the item doesn't exist)
862 bool Inventory::UsePos(object *parent) {
863 if (pos) {
864 return Use(pos->type, pos->id, parent);
865 } else
866 return false;
867 }
868
869 bool Inventory::Use(int objhandle, object *parent) { return Use(objhandle, -1, parent); }
870
871 // removes an item from inventory, without using it (returns true on success, false if object didn't exist)
872 bool Inventory::Remove(int type, int id) {
873 inven_item *node;
874
875 node = FindItem(type, id);
876
877 if (!node)
878 return false;
879
880 if (node->iflags & INVF_OBJECT) {
881 // always remove
882 object *obj = ObjGet(type);
883 ASSERT(obj);
884 if (obj) {
885 obj->flags &= ~OF_INPLAYERINVENTORY;
886 }
887
888 RemoveNode(node);
889 } else {
890 node->count--;
891 mprintf(0, "Inventory System: Remove\n");
892
893 if (node->count <= 0)
894 RemoveNode(node);
895 }
896
897 return true;
898 }
899
900 // removes a node from the list, decrementing the count
901 void Inventory::RemoveNode(inven_item *node) {
902 if (!node)
903 return;
904
905 bool movepos = false;
906 count--;
907 inven_item *prev, *next;
908
909 prev = node->prev;
910 next = node->next;
911
912 if (node->description)
913 mem_free(node->description);
914 if (node->icon_name)
915 mem_free(node->icon_name);
916 if (node->name)
917 mem_free(node->name);
918
919 if (pos == node) {
920 if (pos->next != node) {
921 movepos = true;
922 pos = pos->next;
923 } else
924 pos = NULL;
925 }
926
927 if (node == root) {
928 if (root->next != root) {
929 inven_item *n = root->next;
930 inven_item *p = root->prev;
931 delete node;
932 root = n;
933 root->prev = p;
934 p->next = root;
935 } else {
936 delete node;
937 root = NULL;
938 }
939 } else {
940 delete node;
941 prev->next = next;
942 next->prev = prev;
943 }
944
945 if (movepos)
946 ValidatePos();
947 }
948
949 // given a type and id, it returns the first matching inventory item
950 inven_item *Inventory::FindItem(int type, int id) {
951 inven_item *current = root;
952
953 if (count == 0)
954 return NULL; // there are no items, don't even bother
955
956 int counter = count;
957
958 while (counter) {
959 if ((current->type == type) && (current->id == id)) // we got a match
960 {
961 // mprintf(0,"Inventory: FindItem found Type(%d) ID(%d)\n",type,id);
962 return current;
963 }
964
965 current = current->next;
966 counter--;
967 }
968
969 // mprintf(0,"Inventory: FindItem couldn't find Type(%d) ID(%d)\n",type,id);
970 return NULL;
971 }
972
973 // returns how many items are in the inventory
974 int Inventory::Size(void) {
975 // mprintf(0,"Inventory System: Size\n");
976 return count;
977 }
978
979 // returns true if there is an item in the inventory with the given type/id
980 bool Inventory::CheckItem(int type, int id) {
981 // mprintf(0,"Inventory System: CheckItem\n");
982 if (FindItem(type, id))
983 return true;
984 else
985 return false;
986 }
987
988 // saves the inventory to the file (returns number of bytes written)
989 int Inventory::SaveInventory(CFILE *file) {
990 int num_items = Size();
991
992 int start_pos = cftell(file);
993
994 int pos_pos = 0;
995 int pos_count = 0;
996
997 cf_WriteInt(file, num_items);
998 if (num_items > 0) {
999 inven_item *curr;
1000 curr = root;
1001
1002 while (num_items > 0) {
1003 if (pos == curr) {
1004 pos_pos = pos_count;
1005 }
1006
1007 if (curr->id == -1) {
1008 // make sure it is a valid object
1009 object *obj = ObjGet(curr->type);
1010 ASSERT(obj);
1011 if (!obj) {
1012 mprintf(0, "Invalid object saving inventory\n");
1013 curr = curr->next;
1014 num_items--;
1015 pos_count++;
1016 continue;
1017 }
1018 }
1019
1020 cf_WriteInt(file, curr->type);
1021 cf_WriteInt(file, curr->otype);
1022 cf_WriteInt(file, curr->id);
1023 cf_WriteInt(file, curr->oid);
1024 cf_WriteInt(file, curr->flags);
1025 cf_WriteInt(file, curr->count);
1026
1027 if (curr->description)
1028 cf_WriteString(file, curr->description);
1029 else
1030 cf_WriteByte(file, 0);
1031
1032 if (curr->icon_name)
1033 cf_WriteString(file, curr->icon_name);
1034 else
1035 cf_WriteByte(file, 0);
1036
1037 if (curr->name)
1038 cf_WriteString(file, curr->name);
1039 else
1040 cf_WriteByte(file, 0);
1041
1042 cf_WriteInt(file, curr->iflags);
1043
1044 curr = curr->next;
1045 num_items--;
1046 pos_count++;
1047 }
1048 }
1049
1050 cf_WriteInt(file, pos_pos);
1051
1052 int end_pos = cftell(file);
1053
1054 return (end_pos - start_pos);
1055 }
1056
1057 // restores the inventory from file (returns number of bytes read)
1058 int Inventory::ReadInventory(CFILE *file) {
1059 int start_pos = cftell(file);
1060
1061 int num_items = cf_ReadInt(file);
1062 count = num_items;
1063 root = NULL;
1064 char temp[512];
1065
1066 int t, i, otype;
1067
1068 if (num_items > 0) {
1069 inven_item *item, *prev;
1070
1071 while (num_items > 0) {
1072 t = cf_ReadInt(file);
1073 otype = cf_ReadInt(file);
1074 i = cf_ReadInt(file);
1075
1076 // make sure the object is valid
1077 if (i == -1) {
1078 object *obj = ObjGet(t);
1079 ASSERT(obj);
1080 if (!obj) {
1081 mprintf(0, "Invalid object restoring inventory\n");
1082 // skip this object
1083 cf_ReadInt(file);
1084 cf_ReadInt(file);
1085 cf_ReadInt(file);
1086 cf_ReadString(temp, 512, file);
1087 cf_ReadString(temp, 512, file);
1088 cf_ReadString(temp, 512, file);
1089 cf_ReadInt(file);
1090 num_items--;
1091 count--;
1092 continue;
1093 }
1094 }
1095
1096 if (root == NULL) {
1097 // there are no items in the list...time to add
1098 root = new inven_item;
1099 item = root;
1100 root->next = root;
1101 root->prev = root;
1102 } else {
1103 item = new inven_item;
1104
1105 prev = root->prev;
1106
1107 item->prev = prev;
1108 prev->next = item;
1109 item->next = root;
1110 root->prev = item;
1111 }
1112
1113 item->type = t;
1114 item->id = i;
1115 item->otype = otype;
1116 item->oid = cf_ReadInt(file);
1117 item->flags = cf_ReadInt(file);
1118 item->count = cf_ReadInt(file);
1119
1120 cf_ReadString(temp, 512, file);
1121 item->description = mem_strdup(temp);
1122
1123 cf_ReadString(temp, 512, file);
1124 item->icon_name = mem_strdup(temp);
1125
1126 cf_ReadString(temp, 512, file);
1127 item->name = mem_strdup(temp);
1128
1129 item->iflags = cf_ReadInt(file);
1130
1131 num_items--;
1132 }
1133 }
1134
1135 int pos_index = cf_ReadInt(file);
1136 GotoPos(pos_index);
1137 ValidatePos();
1138
1139 int end_pos = cftell(file);
1140
1141 return (end_pos - start_pos);
1142 }
1143
1144 // resets the position pointer in the list to the beginning
1145 void Inventory::ResetPos(void) { pos = root; }
1146
1147 // moves the position pointer to the next inventory item
1148 void Inventory::NextPos(bool skip) {
1149 if ((pos) && (pos->next)) {
1150 pos = pos->next;
1151 } else if (!pos) {
1152 pos = root;
1153 } else
1154 return;
1155
1156 if (!skip)
1157 ValidatePos();
1158 }
1159
1160 // moves the position pointer to the previous inventory item
1161 void Inventory::PrevPos(bool skip) {
1162 if ((pos) && (pos->prev)) {
1163 pos = pos->prev;
1164 } else if (!pos) {
1165 pos = root;
1166 } else
1167 return;
1168
1169 if (!skip)
1170 ValidatePos(false);
1171 }
1172
1173 // returns true if the position pointer is at the begining of the inventory list
1174 bool Inventory::AtBeginning(void) {
1175 if (!pos)
1176 return true;
1177 if (pos == root)
1178 return true;
1179 else
1180 return false;
1181 }
1182
1183 // returns false if the position pointer is at the end of the inventory list
1184 bool Inventory::AtEnd(void) {
1185 if (!pos)
1186 return true;
1187 if (pos->next == root)
1188 return true;
1189 else
1190 return false;
1191 }
1192
1193 // returns the type/id of the item at the current position
1194 // returns true if the pos is a real object in the game
1195 // returns false if the pos is just a type/id inventory item
1196 bool Inventory::GetPosTypeID(int &type, int &id) {
1197 if (!pos) {
1198 type = id = 0;
1199 return false;
1200 }
1201
1202 type = pos->type;
1203 id = pos->id;
1204
1205 if (pos->iflags & INVF_OBJECT)
1206 return true;
1207 else
1208 return false;
1209 }
1210
1211 // returns the aux type/id of the item
1212 // returns true if the pos is a real object in the game
1213 // returns false if the pos is just a type/id inventory item
1214 bool Inventory::GetAuxPosTypeID(int &type, int &id) {
1215 if (!pos) {
1216 type = id = 0;
1217 return false;
1218 }
1219 type = pos->otype;
1220 id = pos->oid;
1221
1222 ASSERT(type != OBJ_NONE);
1223
1224 if (pos->iflags & INVF_OBJECT)
1225 return true;
1226 else
1227 return false;
1228 }
1229
1230 // returns the description of the item at the current position
1231 char *Inventory::GetPosDescription(void) {
1232 // mprintf(0,"Getting Pos Description (%s)\n",pos->description);
1233 if (!pos)
1234 return NULL;
1235 return pos->description;
1236 }
1237
1238 // returns the name of the item at the current position
1239 char *Inventory::GetPosName(void) {
1240 if (!pos)
1241 return NULL;
1242 return pos->name;
1243 }
1244
1245 // returns the icon name of the item at the current position
1246 char *Inventory::GetPosIconName(void) {
1247 if (!pos)
1248 return NULL;
1249 return pos->icon_name;
1250 }
1251
1252 // returns the count of the item at the current position
1253 int Inventory::GetPosCount(void) {
1254 // mprintf(0,"Getting Pos Count (%d)\n",pos->count);
1255 if (!pos)
1256 return 0;
1257
1258 if (pos->iflags & INVF_OBJECT)
1259 return 1;
1260
1261 return pos->count;
1262 }
1263
1264 // return information about the current position item
1265 // returns true if the pos is a real object in the game
1266 // returns false if the pos is just a type/id inventory item
1267 bool Inventory::GetPosInfo(uint16_t &iflags, int &flags) {
1268 if (!pos) {
1269 iflags = 0;
1270 flags = 0;
1271 return false;
1272 }
1273
1274 iflags = pos->iflags;
1275 flags = pos->flags;
1276
1277 if (pos->iflags & INVF_OBJECT)
1278 return true;
1279 else
1280 return false;
1281 }
1282
1283 // goes to a position in the list
1284 void Inventory::GotoPos(int newpos) {
1285 // mprintf(0,"Going to Pos (%d)\n",newpos);
1286 ResetPos();
1287 int i;
1288 for (i = 0; i < newpos; i++) {
1289 if ((pos) && (pos->next != NULL))
1290 pos = pos->next;
1291 }
1292 }
1293
1294 // moves the current item pointer to the specified type/id
1295 void Inventory::GotoPos(int type, int id) {
1296 inven_item *node = FindItem(type, id);
1297
1298 if (node)
1299 pos = node;
1300 }
1301
1302 // returns the "index" position of the current item
1303 int Inventory::GetPos(void) {
1304 int type, id;
1305 int ctype, cid;
1306 int count = 0;
1307 bool done = false;
1308
1309 if (!Size())
1310 return -1;
1311
1312 bool is_object, cis_object;
1313
1314 is_object = GetPosTypeID(type, id);
1315 ResetPos();
1316
1317 done = AtEnd();
1318
1319 while (!done) {
1320 cis_object = GetPosTypeID(ctype, cid);
1321 if ((ctype == type) && (cid == id) && (cis_object == is_object))
1322 return count;
1323
1324 count++;
1325 done = AtEnd();
1326 NextPos(false);
1327 }
1328 return 0;
1329 }
1330
1331 // moves to the next/prev item in the inventory list (forward==TRUE means forward, forward==FALSE means go backwards)
1332 void InventorySwitch(bool forward) {
1333 int ctype, cid;
1334
1335 Players[Player_num].inventory.GetPosTypeID(ctype, cid);
1336
1337 if (ctype != 0) {
1338 if (forward) {
1339 Players[Player_num].inventory.NextPos();
1340 } else {
1341 Players[Player_num].inventory.PrevPos();
1342 }
1343
1344 int ntype, nid;
1345 Players[Player_num].inventory.GetPosTypeID(ntype, nid);
1346
1347 if (ntype != ctype || nid != cid) {
1348 // AddHUDMessage(TXT_WPNSELECT, Players[Player_num].inventory.GetPosName());
1349 Sound_system.Play2dSound(SOUND_CHANGE_INVENTORY);
1350
1351 ain_hear hear;
1352 hear.f_directly_player = true;
1353 hear.hostile_level = 0.0f;
1354 hear.curiosity_level = 0.3f;
1355 hear.max_dist = AI_SOUND_SHORT_DIST;
1356 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1357 }
1358 }
1359 }
1360
1361 // moves to the next/prev item in the counter measures list (forward==TRUE means forward, forward==FALSE means go
1362 // backwards)
1363 void CounterMeasuresSwitch(bool forward) {
1364 int ctype, cid;
1365
1366 Players[Player_num].counter_measures.GetPosTypeID(ctype, cid);
1367
1368 if (ctype != 0) {
1369 if (forward) {
1370 Players[Player_num].counter_measures.NextPos();
1371 } else {
1372 Players[Player_num].counter_measures.PrevPos();
1373 }
1374
1375 int ntype, nid;
1376 Players[Player_num].counter_measures.GetPosTypeID(ntype, nid);
1377
1378 if (ntype != ctype || nid != cid) {
1379 AddHUDMessage(TXT_WPNSELECT, Players[Player_num].counter_measures.GetPosName());
1380 Sound_system.Play2dSound(SOUND_CHANGE_COUNTERMEASURE);
1381
1382 ain_hear hear;
1383 hear.f_directly_player = true;
1384 hear.hostile_level = 0.0f;
1385 hear.curiosity_level = 0.3f;
1386 hear.max_dist = AI_SOUND_SHORT_DIST;
1387 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1388 }
1389 }
1390 }
1391
1392 // repositions the pos so its in the correct spot
1393 void Inventory::ValidatePos(bool forward) {
1394 if (!pos)
1395 return;
1396 if (pos->iflags & INVF_SELECTABLE)
1397 return;
1398
1399 inven_item *node;
1400 if (forward)
1401 node = pos->next;
1402 else
1403 node = pos->prev;
1404
1405 while (node != pos) {
1406 if (node->iflags & INVF_SELECTABLE) {
1407 pos = node;
1408 return;
1409 }
1410
1411 if (forward)
1412 node = node->next;
1413 else
1414 node = node->prev;
1415 }
1416 pos = NULL;
1417 }
1418
1419 // returns how many of a type/id exists in the inventory
1420 int Inventory::GetTypeIDCount(int type, int id) {
1421 inven_item *node = FindItem(type, id);
1422
1423 if (!node)
1424 return 0;
1425 if (node->flags & INVF_OBJECT) {
1426 return 1;
1427 }
1428
1429 return node->count;
1430 }
1431
1432 // determines whether the position is selectable
1433 bool Inventory::IsSelectable(void) {
1434 if (!pos)
1435 return false;
1436
1437 return ((pos->iflags & INVF_SELECTABLE) != 0);
1438 }
1439
1440 // determines whether the position is selectable
1441 bool Inventory::IsUsable(void) {
1442 if (!pos)
1443 return false;
1444
1445 return ((pos->iflags & INVF_USEABLE) != 0);
1446 }
1447
1448 // gets a detailed list of information about what is in the inventory
1449 // returns the number of items filled in.
1450 int Inventory::GetInventoryItemList(tInvenList *list, int max_amount, int *cur_sel) {
1451 ASSERT(cur_sel);
1452 *cur_sel = -1;
1453
1454 if (max_amount <= 0)
1455 return 0;
1456
1457 inven_item *current = root;
1458 if (count == 0)
1459 return 0; // there are no items, don't even bother
1460
1461 int counter = count;
1462 int cur_count = 0;
1463
1464 while (counter) {
1465 if (cur_count < max_amount) {
1466 if (current == pos) {
1467 // current selected
1468 *cur_sel = cur_count;
1469 }
1470
1471 list[cur_count].amount = current->count;
1472 list[cur_count].hud_name = current->name;
1473 list[cur_count].selectable = (current->iflags & INVF_SELECTABLE) ? true : false;
1474 cur_count++;
1475 }
1476
1477 current = current->next;
1478 counter--;
1479 }
1480
1481 return count;
1482 }
1483
1484 // use the currently selected inventory item
1485 bool UseInventoryItem() {
1486 int type, id;
1487
1488 Players[Player_num].inventory.GetPosTypeID(type, id);
1489 if (!type && !id)
1490 return false;
1491
1492 if (Players[Player_num].inventory.UsePos(&Objects[Players[Player_num].objnum])) {
1493 if (Game_mode & GM_MULTI && (Netgame.local_role == LR_SERVER)) {
1494 MultiSendInventoryRemoveItem(Player_num, type, id);
1495 }
1496 }
1497 return true;
1498 }
1499
1500 // use the currently selected countermeasure
1501 bool UseCountermeasure() {
1502 int type, id;
1503
1504 Players[Player_num].counter_measures.GetPosTypeID(type, id);
1505 if (!type && !id)
1506 return false;
1507
1508 if (Players[Player_num].counter_measures.UsePos(&Objects[Players[Player_num].objnum])) {
1509 if (Game_mode & GM_MULTI && (Netgame.local_role == LR_SERVER)) {
1510 MultiSendInventoryRemoveItem(Player_num, type, id);
1511 }
1512 }
1513 return true;
1514 }
1515
1516 // Checks for an object in any of the players inventorys and removes it
1517 void InventoryRemoveObject(int objhandle) {
1518 object *obj = ObjGet(objhandle);
1519 ASSERT(obj);
1520 if (!obj)
1521 return;
1522
1523 if (!(obj->flags & OF_INPLAYERINVENTORY)) // not in the player's inventory
1524 return;
1525
1526 // go through all the players and look for the object
1527 for (int i = 0; i < MAX_PLAYERS; i++) {
1528 if (Players[i].inventory.CheckItem(objhandle, -1)) {
1529 // this player has it!!
1530 mprintf(0, "INVEN: Removing dead object from %d\n", i);
1531 Players[i].inventory.Remove(objhandle, -1);
1532 return;
1533 }
1534 }
1535 }