/
p_inventory.cpp
540 lines (452 loc) · 15.5 KB
/
p_inventory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/** @file p_inventory.cpp Common code for player inventory.
*
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#if defined(__JHERETIC__) || defined(__JHEXEN__) || defined(__JDOOM64__)
#include "common.h"
#include "p_inventory.h"
#include "g_common.h"
#include "player.h"
#include "d_net.h"
#include "hu_inventory.h"
#include <de/memory.h>
#include <cstring>
struct inventoryitem_t
{
int useCount;
inventoryitem_t *next;
};
struct playerinventory_t
{
inventoryitem_t *items[NUM_INVENTORYITEM_TYPES-1];
inventoryitemtype_t readyItem;
};
int didUseItem = false;
static def_invitem_t const itemDefs[NUM_INVENTORYITEM_TYPES-1] = {
#if __JHERETIC__
{IIF_USE_PANIC, "TXT_ARTIINVULNERABILITY", "A_Invulnerability", "artiuse", "ARTIINVU", CTL_INVULNERABILITY},
{IIF_USE_PANIC, "TXT_ARTIINVISIBILITY", "A_Invisibility", "artiuse", "ARTIINVS", CTL_INVISIBILITY},
{IIF_USE_PANIC, "TXT_ARTIHEALTH", "A_Health", "artiuse", "ARTIPTN2", CTL_HEALTH},
{IIF_USE_PANIC, "TXT_ARTISUPERHEALTH", "A_SuperHealth", "artiuse", "ARTISPHL", CTL_SUPER_HEALTH},
{IIF_USE_PANIC, "TXT_ARTITOMEOFPOWER", "A_TombOfPower", "artiuse", "ARTIPWBK", CTL_TOME_OF_POWER},
{IIF_USE_PANIC, "TXT_ARTITORCH", "A_Torch", "artiuse", "ARTITRCH", CTL_TORCH},
{IIF_USE_PANIC, "TXT_ARTIFIREBOMB", "A_FireBomb", "artiuse", "ARTIFBMB", CTL_FIREBOMB},
{IIF_USE_PANIC, "TXT_ARTIEGG", "A_Egg", "artiuse", "ARTIEGGC", CTL_EGG},
{IIF_USE_PANIC, "TXT_ARTIFLY", "A_Wings", "artiuse", "ARTISOAR", CTL_FLY},
{IIF_USE_PANIC, "TXT_ARTITELEPORT", "A_Teleport", "artiuse", "ARTIATLP", CTL_TELEPORT},
#elif __JHEXEN__
{IIF_USE_PANIC, "TXT_ARTIINVULNERABILITY", "A_Invulnerability", "ARTIFACT_USE", "ARTIINVU", CTL_INVULNERABILITY},
{IIF_USE_PANIC, "TXT_ARTIHEALTH", "A_Health", "ARTIFACT_USE", "ARTIPTN2", CTL_HEALTH},
{IIF_USE_PANIC, "TXT_ARTISUPERHEALTH", "A_SuperHealth", "ARTIFACT_USE", "ARTISPHL", CTL_MYSTIC_URN},
{IIF_USE_PANIC, "TXT_ARTIHEALINGRADIUS", "A_HealRadius", "ARTIFACT_USE", "ARTIHRAD", -1},
{IIF_USE_PANIC, "TXT_ARTISUMMON", "A_SummonTarget", "ARTIFACT_USE", "ARTISUMN", CTL_DARK_SERVANT},
{IIF_USE_PANIC, "TXT_ARTITORCH", "A_Torch", "ARTIFACT_USE", "ARTITRCH", CTL_TORCH},
{IIF_USE_PANIC, "TXT_ARTIEGG", "A_Egg", "ARTIFACT_USE", "ARTIPORK", CTL_EGG},
{IIF_USE_PANIC, "TXT_ARTIFLY", "A_Wings", "ARTIFACT_USE", "ARTISOAR", CTL_FLY},
{IIF_USE_PANIC, "TXT_ARTIBLASTRADIUS", "A_BlastRadius", "ARTIFACT_USE", "ARTIBLST", CTL_BLAST_RADIUS},
{IIF_USE_PANIC, "TXT_ARTIPOISONBAG", "A_PoisonBag", "ARTIFACT_USE", "ARTIPSBG", CTL_POISONBAG},
{IIF_USE_PANIC, "TXT_ARTITELEPORTOTHER", "A_TeleportOther", "ARTIFACT_USE", "ARTITELO", CTL_TELEPORT_OTHER},
{IIF_USE_PANIC, "TXT_ARTISPEED", "A_Speed", "ARTIFACT_USE", "ARTISPED", CTL_SPEED_BOOTS},
{IIF_USE_PANIC, "TXT_ARTIBOOSTMANA", "A_BoostMana", "ARTIFACT_USE", "ARTIBMAN", CTL_KRATER},
{IIF_USE_PANIC, "TXT_ARTIBOOSTARMOR", "A_BoostArmor", "ARTIFACT_USE", "ARTIBRAC", -1},
{IIF_USE_PANIC, "TXT_ARTITELEPORT", "A_Teleport", "ARTIFACT_USE", "ARTIATLP", CTL_TELEPORT},
{0, "TXT_ARTIPUZZSKULL", "A_PuzzSkull", "PUZZLE_SUCCESS", "ARTISKLL", -1},
{0, "TXT_ARTIPUZZGEMBIG", "A_PuzzGemBig", "PUZZLE_SUCCESS", "ARTIBGEM", -1},
{0, "TXT_ARTIPUZZGEMRED", "A_PuzzGemRed", "PUZZLE_SUCCESS", "ARTIGEMR", -1},
{0, "TXT_ARTIPUZZGEMGREEN1", "A_PuzzGemGreen1", "PUZZLE_SUCCESS", "ARTIGEMG", -1},
{0, "TXT_ARTIPUZZGEMGREEN2", "A_PuzzGemGreen2", "PUZZLE_SUCCESS", "ARTIGMG2", -1},
{0, "TXT_ARTIPUZZGEMBLUE1", "A_PuzzGemBlue1", "PUZZLE_SUCCESS", "ARTIGEMB", -1},
{0, "TXT_ARTIPUZZGEMBLUE2", "A_PuzzGemBlue2", "PUZZLE_SUCCESS", "ARTIGMB2", -1},
{0, "TXT_ARTIPUZZBOOK1", "A_PuzzBook1", "PUZZLE_SUCCESS", "ARTIBOK1", -1},
{0, "TXT_ARTIPUZZBOOK2", "A_PuzzBook2", "PUZZLE_SUCCESS", "ARTIBOK2", -1},
{0, "TXT_ARTIPUZZSKULL2", "A_PuzzSkull2", "PUZZLE_SUCCESS", "ARTISKL2", -1},
{0, "TXT_ARTIPUZZFWEAPON", "A_PuzzFWeapon", "PUZZLE_SUCCESS", "ARTIFWEP", -1},
{0, "TXT_ARTIPUZZCWEAPON", "A_PuzzCWeapon", "PUZZLE_SUCCESS", "ARTICWEP", -1},
{0, "TXT_ARTIPUZZMWEAPON", "A_PuzzMWeapon", "PUZZLE_SUCCESS", "ARTIMWEP", -1},
{0, "TXT_ARTIPUZZGEAR1", "A_PuzzGear1", "PUZZLE_SUCCESS", "ARTIGEAR", -1},
{0, "TXT_ARTIPUZZGEAR2", "A_PuzzGear2", "PUZZLE_SUCCESS", "ARTIGER2", -1},
{0, "TXT_ARTIPUZZGEAR3", "A_PuzzGear3", "PUZZLE_SUCCESS", "ARTIGER3", -1},
{0, "TXT_ARTIPUZZGEAR4", "A_PuzzGear4", "PUZZLE_SUCCESS", "ARTIGER4", -1}
#elif __JDOOM64__
{IIF_READY_ALWAYS, "DEMONKEY1", "", "", "", -1},
{IIF_READY_ALWAYS, "DEMONKEY2", "", "", "", -1},
{IIF_READY_ALWAYS, "DEMONKEY3", "", "", "", -1}
#endif
};
static invitem_t invItems[NUM_INVENTORYITEM_TYPES - 1];
static playerinventory_t inventories[MAXPLAYERS];
static inline def_invitem_t const *itemDefForType(inventoryitemtype_t type)
{
return &itemDefs[type - 1];
}
static inventoryitem_t *allocItem()
{
inventoryitem_t *item = (inventoryitem_t *)M_Malloc(sizeof(*item));
return item;
}
static void freeItem(inventoryitem_t *item)
{
M_Free(item);
}
static inline uint countItems2(playerinventory_t const *inv, inventoryitemtype_t type)
{
uint count = 0;
for(inventoryitem_t const *item = inv->items[type - 1]; item; item = item->next)
{
count++;
}
return count;
}
static uint countItems(playerinventory_t const *inv, inventoryitemtype_t type)
{
if(type == IIT_NONE)
{
uint count = 0;
for(int i = IIT_FIRST; i < NUM_INVENTORYITEM_TYPES; ++i)
{
count += countItems2(inv, inventoryitemtype_t(i));
}
return count;
}
return countItems2(inv, type);
}
static dd_bool useItem(playerinventory_t *inv, inventoryitemtype_t type, dd_bool panic)
{
if(!countItems(inv, type))
return false; // That was a non-starter.
int plrnum = inv - inventories;
invitem_t const *item = &invItems[type-1];
// Is this usable?
if(!item->action)
{
return false; // No.
}
// How about when panicked?
if(panic)
{
def_invitem_t const *def = itemDefForType(type);
if(!(def->flags & IIF_USE_PANIC))
{
return false;
}
}
/**
* @attention Kludge:
* Action ptrs do not currently support return values. For now, rather
* than rewrite each use routine, use a global var to get the result.
* Ugly.
*/
didUseItem = false;
de::function_cast<void (*)(mobj_t *)>(item->action)(players[plrnum].plr->mo);
return didUseItem;
}
static dd_bool giveItem(playerinventory_t *inv, inventoryitemtype_t type)
{
uint count = countItems(inv, type);
#if __JHEXEN__
// Can't carry more than 1 puzzle item in coop netplay.
if(count && type >= IIT_FIRSTPUZZITEM && IS_NETGAME && !G_Rules().deathmatch)
{
return false;
}
#endif
// Carrying the maximum allowed number of these items?
if(count >= MAXINVITEMCOUNT)
{
return false;
}
inventoryitem_t *item = allocItem();
item->useCount = 0;
item->next = inv->items[type-1];
inv->items[type-1] = item;
return true;
}
static int takeItem(playerinventory_t *inv, inventoryitemtype_t type)
{
if(!inv->items[type - 1])
{
return false; // Don't have one to take.
}
inventoryitem_t *next = inv->items[type - 1]->next;
freeItem(inv->items[type -1]);
inv->items[type - 1] = next;
if(!inv->items[type - 1])
{
// Took last item of this type.
if(inv->readyItem == type)
{
inv->readyItem = IIT_NONE;
}
}
return true;
}
static int tryTakeItem(playerinventory_t *inv, inventoryitemtype_t type)
{
if(takeItem(inv, type))
{
// An item was taken.
int player = inv - inventories;
players[player].update |= PSF_INVENTORY;
#if __JHERETIC__ || __JHEXEN__
// Inform the HUD.
Hu_InventoryMarkDirty(player);
// Set position markers and set the next readyItem?
if(inv->readyItem == IIT_NONE)
{
Hu_InventoryMove(player, -1, false, true);
}
#endif
return true;
}
return false;
}
static int tryUseItem(playerinventory_t *inv, inventoryitemtype_t type, int panic)
{
if(useItem(inv, type, panic))
{
// Item was used.
return tryTakeItem(inv, type);
}
return false;
}
def_invitem_t const *P_GetInvItemDef(inventoryitemtype_t type)
{
DENG2_ASSERT(type >= IIT_FIRST && type < NUM_INVENTORYITEM_TYPES);
return itemDefForType(type);
}
static acfnptr_t getActionPtr(char const *name)
{
if(name && name[0])
{
for(actionlink_t *link = actionlinks; link->name; link++)
{
if(!strcmp(name, link->name))
return link->func;
}
}
return 0;
}
void P_InitInventory()
{
de::zap(invItems);
for(int i = 0; i < NUM_INVENTORYITEM_TYPES - 1; ++i)
{
inventoryitemtype_t type = inventoryitemtype_t(IIT_FIRST + i);
def_invitem_t const *def = P_GetInvItemDef(type);
invitem_t *data = &invItems[i];
data->type = type;
data->niceName = textenum_t(Def_Get(DD_DEF_TEXT, (char*) def->niceName, NULL));
data->action = getActionPtr(def->action);
data->useSnd = sfxenum_t(Def_Get(DD_DEF_SOUND, (char*) def->useSnd, NULL));
data->patchId = R_DeclarePatch(def->patch);
}
de::zap(inventories);
}
void P_ShutdownInventory()
{
for(int i = 0; i < MAXPLAYERS; ++i)
{
P_InventoryEmpty(i);
}
}
invitem_t const *P_GetInvItem(int id)
{
if(id < 0 || id >= NUM_INVENTORYITEM_TYPES - 1)
return 0;
return &invItems[id];
}
void P_InventoryEmpty(int player)
{
if(player < 0 || player >= MAXPLAYERS)
return;
playerinventory_t *inv = &inventories[player];
for(int i = 0; i < NUM_INVENTORYITEM_TYPES - 1; ++i)
{
inventoryitem_t *item;
while((item = inv->items[i]))
{
inventoryitem_t *next = item->next;
freeItem(item);
inv->items[i] = next;
}
}
de::zap(inv->items);
inv->readyItem = IIT_NONE;
}
uint P_InventoryCount(int player, inventoryitemtype_t type)
{
if(player < 0 || player >= MAXPLAYERS)
return 0;
if(!(type == IIT_NONE ||
(type >= IIT_FIRST && type < NUM_INVENTORYITEM_TYPES)))
{
return 0;
}
return countItems(&inventories[player], type);
}
int P_InventorySetReadyItem(int player, inventoryitemtype_t type)
{
if(player < 0 || player >= MAXPLAYERS)
return 0;
if(type < IIT_NONE || type >= NUM_INVENTORYITEM_TYPES)
return 0;
playerinventory_t *inv = &inventories[player];
if(type == IIT_NONE || countItems(inv, type))
{
// A valid ready request.
dd_bool mustEquip = true;
if(type != IIT_NONE)
{
def_invitem_t const *def = P_GetInvItemDef(type);
mustEquip = ((def->flags & IIF_READY_ALWAYS)? false : true);
}
if(mustEquip && inv->readyItem != type)
{
// Make it so.
inv->readyItem = type;
#if __JHERETIC__ || __JHEXEN__
// Inform the HUD.
Hu_InventoryMarkDirty(player);
#endif
}
return 1;
}
return 0;
}
inventoryitemtype_t P_InventoryReadyItem(int player)
{
if(player < 0 || player >= MAXPLAYERS)
return IIT_NONE;
return inventories[player].readyItem;
}
int P_InventoryGive(int player, inventoryitemtype_t type, int silent)
{
if(player < 0 || player >= MAXPLAYERS)
return 0;
if(!(type >= IIT_FIRST && type < NUM_INVENTORYITEM_TYPES))
return 0;
playerinventory_t *inv = &inventories[player];
uint oldNumItems = countItems(inv, IIT_NONE);
if(giveItem(inv, type))
{
// Item was given.
players[player].update |= PSF_INVENTORY;
#if __JHERETIC__ || __JHEXEN__
// Inform the HUD.
Hu_InventoryMarkDirty(player);
#endif
if(oldNumItems == 0)
{
// This is the first item the player has been given; ready it.
def_invitem_t const *def = P_GetInvItemDef(type);
if(!(def->flags & IIF_READY_ALWAYS))
{
inv->readyItem = type;
#if __JHERETIC__ || __JHEXEN__
Hu_InventorySelect(player, type);
#endif
}
}
// Maybe unhide the HUD?
#if __JHERETIC__ || __JHEXEN__
if(!silent)
{
ST_HUDUnHide(player, HUE_ON_PICKUP_INVITEM);
}
#else
DENG2_UNUSED(silent);
#endif
return 1;
}
return 0;
}
int P_InventoryTake(int player, inventoryitemtype_t type, int /*silent*/)
{
if(player < 0 || player >= MAXPLAYERS)
return 0;
if(!(type >= IIT_FIRST && type < NUM_INVENTORYITEM_TYPES))
return 0;
playerinventory_t *inv = &inventories[player];
return tryTakeItem(inv, type);
}
int P_InventoryUse(int player, inventoryitemtype_t type, int silent)
{
inventoryitemtype_t lastUsed = IIT_NONE;
if(player < 0 || player >= MAXPLAYERS)
return false;
if(!(type >= IIT_FIRST || type <= NUM_INVENTORYITEM_TYPES))
return false;
playerinventory_t *inv = &inventories[player];
App_Log(DE2_DEV_MAP_VERBOSE, "P_InventoryUse: Player %i using item %i", player, type);
if(IS_CLIENT)
{
if(countItems(inv, type))
{
// Clients will send a request to use the item, nothing else.
NetCl_PlayerActionRequest(&players[player], GPA_USE_FROM_INVENTORY, type);
lastUsed = type;
}
}
else
{
if(type != NUM_INVENTORYITEM_TYPES)
{
if(tryUseItem(inv, type, false))
{
lastUsed = type;
}
}
else
{
// Panic! Use one of each item that is usable when panicked.
for(int i = IIT_FIRST; i < NUM_INVENTORYITEM_TYPES; ++i)
{
if(tryUseItem(inv, inventoryitemtype_t(i), true))
{
lastUsed = inventoryitemtype_t(i);
}
}
}
if(lastUsed == IIT_NONE)
{
// Failed to use an item.
// Set current to the next available?
#if __JHERETIC__ || __JHEXEN__
if(type != NUM_INVENTORYITEM_TYPES && cfg.inventoryUseNext)
{
# if __JHEXEN__
if(lastUsed < IIT_FIRSTPUZZITEM)
# endif
{
Hu_InventoryMove(player, -1, true /* allow wrap */, true);
}
}
#endif
return false;
}
}
if(!silent && lastUsed != IIT_NONE)
{
invitem_t *item = &invItems[lastUsed - 1];
S_ConsoleSound(item->useSnd, NULL, player);
#if __JHERETIC__ || __JHEXEN__
ST_FlashCurrentItem(player);
#endif
}
return true;
}
#endif