/
hu_inventory.c
792 lines (642 loc) · 21.1 KB
/
hu_inventory.c
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2009 Daniel Swanson <danij@dengine.net>
*\author Copyright © 1993-1996 by id Software, Inc.
*
* 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
*/
/**
* hu_inventory.c: Heads-up display(s) for the player inventory.
*/
#if defined(__JHERETIC__) || defined(__JHEXEN__)
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include <assert.h>
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "p_tick.h"
#include "p_inventory.h"
// MACROS ------------------------------------------------------------------
// How many inventory slots are visible in the fixed-size inventory.
#define NUMVISINVSLOTS (7)
#define ST_INVENTORYHEIGHT (30)
#define ST_INVSLOTWIDTH (31)
// Inventory item counts (relative to each slot).
#define ST_INVCOUNTDIGITS (2)
#if __JHERETIC__
#define ST_INVICONOFFY (0)
#define ST_INVCOUNTOFFX (27)
#define ST_INVCOUNTOFFY (22)
#define ST_INVSLOTOFFX (1)
#define ST_INVSELECTOFFY (ST_INVENTORYHEIGHT)
#else
#define ST_INVICONOFFY (-1)
#define ST_INVCOUNTOFFX (28)
#define ST_INVCOUNTOFFY (22)
#define ST_INVSLOTOFFX (1)
#define ST_INVSELECTOFFY (1)
#endif
// HUD Inventory Flags:
#define HIF_VISIBLE 0x1
#define HIF_IS_DIRTY 0x8
// TYPES -------------------------------------------------------------------
typedef struct {
byte flags; // HIF_* flags
int hideTics;
uint numOwnedItemTypes;
uint slots[NUM_INVENTORYITEM_TYPES - 1];
uint numUsedSlots;
uint selected;
uint varCursorPos; // Variable-range, fullscreen inv cursor.
uint fixedCursorPos; // Fixed-range, statusbar inv cursor.
} hud_inventory_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
void ST_ResizeInventory(struct cvar_s* cvar);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void inventoryIndexes(const player_t* plr, const hud_inventory_t* hud,
uint maxVisSlots, int origCursor,
uint* firstVisible, uint* cursorPos,
uint* startSlot, uint* endSlot);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static hud_inventory_t hudInventories[MAXPLAYERS];
cvar_t hudInvCVars[] = {
{"hud-inventory-timer", 0, CVT_FLOAT, &cfg.inventoryTimer, 0, 30},
{"hud-inventory-slot-showempty", 0, CVT_BYTE, &cfg.inventorySlotShowEmpty, 0, 1},
{"hud-inventory-slot-max", CVF_NO_MAX, CVT_INT, &cfg.inventorySlotMaxVis, 0, 0, ST_ResizeInventory},
{NULL}
};
// CODE -------------------------------------------------------------------
/**
* Register CVARs and CCmds for the HUD inventory.
*/
void Hu_InventoryRegister(void)
{
int i;
for(i = 0; hudInvCVars[i].name; ++i)
Con_AddVariable(hudInvCVars + i);
}
/**
* Rebuild the inventory item type table.
* These indices can be used to associate slots in an inventory browser
* to the items held within.
*/
static void rebuildInventory(hud_inventory_t* inv)
{
int i, plrNum = inv - hudInventories;
uint numOwnedItemTypes;
inventoryitemtype_t selectedType = P_GetInvItem(
inv->slots[inv->selected])->type;
inv->selected = 0;
numOwnedItemTypes = 0;
for(i = 0; i < NUM_INVENTORYITEM_TYPES - 1; ++i)
if(P_InventoryCount(plrNum, IIT_FIRST + i) > 0)
numOwnedItemTypes++;
// Always 1:1
inv->numOwnedItemTypes = inv->numUsedSlots =
numOwnedItemTypes;
memset(inv->slots, 0,
sizeof(inv->slots[0]) * (NUM_INVENTORYITEM_TYPES - 1));
if(inv->numUsedSlots)
{
uint idx = 0;
for(i = 0; i < NUM_INVENTORYITEM_TYPES - 1; ++i)
{
if(P_InventoryCount(plrNum, IIT_FIRST + i))
{
inv->slots[idx] = i;
if(P_GetInvItem(i)->type == selectedType)
inv->selected = idx;
if(++idx >= inv->numUsedSlots)
break;
}
}
}
if(inv->flags & HIF_IS_DIRTY)
inv->flags &= ~HIF_IS_DIRTY;
}
static void inventoryIndexes(const player_t* plr, const hud_inventory_t* inv,
uint maxVisSlots, int origCursor,
uint* firstVisible, uint* cursorPos,
uint* fromSlot, uint* toSlot)
{
int cursor, first, from, to;
if(!firstVisible && !cursorPos && !fromSlot && !toSlot)
return;
if(cfg.inventorySelectMode)
{ // Scroll.
int last;
cursor = maxVisSlots / 2;
if(cfg.inventoryWrap)
{
first = inv->selected - cursor;
if(first < 0)
first += inv->numUsedSlots;
from = 0;
to = maxVisSlots;
}
else
{
first = inv->selected - cursor;
if(first < 0)
first = 0;
from = cursor - inv->selected;
if(from < 0)
from = 0;
last = inv->selected + cursor + (maxVisSlots % 2? 1 : 0);
if(last - 1 < (signed) inv->numUsedSlots)
to = maxVisSlots;
else
to = maxVisSlots - (last - inv->numUsedSlots);
}
}
else
{ // Cursor.
cursor = origCursor;
if(inv->numUsedSlots < maxVisSlots)
{
from = (maxVisSlots - inv->numUsedSlots) / 2;
to = from + inv->numUsedSlots;
cursor += from;
}
else
{
if(cfg.inventoryWrap)
{
from = 0;
to = maxVisSlots;
}
else
{
int last;
from = cursor - inv->selected;
if(from < 0)
from = 0;
last = inv->numUsedSlots - 1 -
(inv->selected + cursor);
to = maxVisSlots;
}
}
first = inv->selected - origCursor;
if(cfg.inventoryWrap)
{
if(first < 0)
first += inv->numUsedSlots;
}
else
{
if(inv->numUsedSlots < maxVisSlots ||
first + maxVisSlots > inv->numUsedSlots)
{
int shift = inv->numUsedSlots -
(first + maxVisSlots);
first += shift;
if(first < 0)
{
first = 0;
cursor = from + inv->selected;
}
else
cursor -= shift;
}
}
if(first < 0)
first = 0;
}
if(firstVisible)
*firstVisible = (unsigned) first;
if(cursorPos)
*cursorPos = (unsigned) cursor;
if(fromSlot)
*fromSlot = (unsigned) from;
if(toSlot)
*toSlot = (unsigned) to;
}
void Hu_InventoryDraw(int player, int x, int y, float alpha,
float textAlpha, float iconAlpha)
{
#define BORDER 1
const hud_inventory_t* inv;
player_t* plr;
uint i, from, to, idx, slot, first, selected,
numVisSlots, maxVisSlots, startSlot, endSlot;
float invScale, lightDelta;
if(alpha <= 0)
return;
if(player < 0 || player >= MAXPLAYERS)
return;
inv = &hudInventories[player];
plr = &players[player];
if(cfg.inventorySlotMaxVis)
maxVisSlots = cfg.inventorySlotMaxVis;
else
maxVisSlots = NUM_INVENTORYITEM_TYPES - 1;
inventoryIndexes(plr, inv, maxVisSlots, inv->varCursorPos,
&first, &selected, &startSlot, &endSlot);
numVisSlots = maxVisSlots;
{
#define EXTRA_SCALE .75f
float availWidth = SCREENWIDTH - 50 * 2,
width = (numVisSlots * ST_INVSLOTWIDTH) * EXTRA_SCALE;
if(width > availWidth)
invScale = availWidth / width;
else
invScale = 1;
invScale *= cfg.hudScale * EXTRA_SCALE;
#undef EXTRA_SCALE
}
if(maxVisSlots % 2)
lightDelta = 1.f / maxVisSlots;
else
lightDelta = 1.f / (maxVisSlots - 1);
lightDelta *= 2;
idx = first;
slot = (cfg.inventorySlotShowEmpty? 0 : startSlot);
from = (cfg.inventorySlotShowEmpty? 0 : startSlot);
if(cfg.inventorySlotShowEmpty)
{
to = maxVisSlots;
}
else
{
if(startSlot > 0)
to = maxVisSlots;
else
to = endSlot - startSlot;
if(inv->numUsedSlots - 1 < endSlot - startSlot)
to = from + inv->numUsedSlots;
}
Draw_BeginZoom(invScale, x, y + ST_INVENTORYHEIGHT);
x -= (numVisSlots * ST_INVSLOTWIDTH) / 2.f;
for(i = from; i < to; ++i)
{
float light, a;
if(i < maxVisSlots / 2)
light = (i + 1) * lightDelta;
else
light = (maxVisSlots - i) * lightDelta;
a = i == selected? .5f : light / 2;
GL_DrawPatchLitAlpha(x + slot * ST_INVSLOTWIDTH + ST_INVSLOTOFFX,
y,
light, a * alpha,
dpInvItemBox.lump);
if(i >= startSlot && i < endSlot)
{
uint count;
const invitem_t* item =
P_GetInvItem(inv->slots[idx]);
if((count = P_InventoryCount(player, item->type)))
{
GL_DrawPatchLitAlpha(x + slot * ST_INVSLOTWIDTH,
y + ST_INVICONOFFY, 1,
slot == selected? alpha : iconAlpha / 3,
item->patchLump);
if(count > 1)
Hu_DrawSmallNum(P_InventoryCount(player, item->type),
ST_INVCOUNTDIGITS,
x + slot * ST_INVSLOTWIDTH + ST_INVCOUNTOFFX,
y + ST_INVCOUNTOFFY,
slot == selected? alpha : textAlpha / 2);
}
if(++idx > inv->numOwnedItemTypes - 1)
idx = 0;
}
slot++;
}
GL_DrawPatchLitAlpha(x + selected * ST_INVSLOTWIDTH,
y + ST_INVSELECTOFFY - BORDER,
1, alpha, dpInvSelectBox.lump);
if(inv->numUsedSlots > maxVisSlots)
{
#define ARROW_RELXOFF 2
#define ARROW_YOFFSET 9
if(cfg.inventoryWrap || first != 0)
{
GL_DrawPatchLitAlpha(
#if __JHEXEN__
40, 167,
#else
x - dpInvPageLeft[0].width - ARROW_RELXOFF,
y + ARROW_YOFFSET,
#endif
1, iconAlpha,
dpInvPageLeft[!(mapTime & 4)? 1 : 0].lump);
}
if(cfg.inventoryWrap || inv->numUsedSlots - first > numVisSlots)
{
GL_DrawPatchLitAlpha(
#if __JHEXEN__
270, 167,
#else
x + numVisSlots * ST_INVSLOTWIDTH + ARROW_RELXOFF,
y + ARROW_YOFFSET,
#endif
1, iconAlpha,
dpInvPageRight[!(mapTime & 4)? 1 : 0].lump);
}
#undef ARROW_XOFFSET
#undef ARROW_YOFFSET
}
Draw_EndZoom();
#undef BORDER
}
void Hu_InventoryDraw2(int player, int x, int y, float alpha)
{
#define BORDER 1
const hud_inventory_t* inv;
player_t* plr;
uint i, idx, slot, from, to, first, cursor, startSlot,
endSlot;
if(alpha <= 0)
return;
if(player < 0 || player >= MAXPLAYERS)
return;
inv = &hudInventories[player];
plr = &players[player];
inventoryIndexes(plr, inv, NUMVISINVSLOTS,
inv->fixedCursorPos, &first, &cursor,
&startSlot, &endSlot);
idx = first;
from = startSlot;
slot = startSlot;
if(startSlot > 0)
to = NUMVISINVSLOTS;
else
to = endSlot - startSlot;
if(inv->numUsedSlots - 1 < endSlot - startSlot)
to = from + inv->numUsedSlots;
for(i = from; i < to; ++i)
{
if(i >= startSlot && i < endSlot)
{
uint count;
const invitem_t* item =
P_GetInvItem(inv->slots[idx]);
if((count = P_InventoryCount(player, item->type)))
{
GL_DrawPatchLitAlpha(x + slot * ST_INVSLOTWIDTH,
y + ST_INVICONOFFY, 1,
alpha,
item->patchLump);
if(count > 1)
Hu_DrawSmallNum(P_InventoryCount(player, item->type),
ST_INVCOUNTDIGITS,
x + slot * ST_INVSLOTWIDTH + ST_INVCOUNTOFFX,
y + ST_INVCOUNTOFFY,
alpha);
}
if(++idx > inv->numOwnedItemTypes - 1)
idx = 0;
}
slot++;
}
GL_DrawPatchLitAlpha(x + cursor * ST_INVSLOTWIDTH,
y + ST_INVSELECTOFFY - BORDER,
1, alpha, dpInvSelectBox.lump);
if(inv->numUsedSlots > NUMVISINVSLOTS)
{
// Draw more left indicator.
if(cfg.inventoryWrap || first != 0)
GL_DrawPatchLitAlpha(
#if __JHEXEN__
42, 163,
#else
x - 12, y - 1,
#endif
1, alpha,
dpInvPageLeft[!(mapTime & 4)? 1 : 0].lump);
// Draw more right indicator.
if(cfg.inventoryWrap || inv->numUsedSlots - first > NUMVISINVSLOTS)
GL_DrawPatchLitAlpha(269,
#if __JHEXEN__
163,
#else
y - 1,
#endif
1, alpha,
dpInvPageRight[!(mapTime & 4)? 1 : 0].lump);
}
#undef BORDER
}
static void inventoryMove(hud_inventory_t* inv, int dir, boolean canWrap)
{
if(dir == 1)
{ // Move right.
uint maxVisSlots;
if(inv->selected == inv->numUsedSlots - 1)
{
if(canWrap)
inv->selected = 0;
}
else
inv->selected++;
// First the fixed range statusbar cursor.
if(inv->fixedCursorPos < NUMVISINVSLOTS - 1 &&
!(inv->fixedCursorPos + 1 > inv->numUsedSlots - 1))
inv->fixedCursorPos++;
// Now the variable range full-screen cursor.
if(cfg.inventorySlotMaxVis)
maxVisSlots = cfg.inventorySlotMaxVis;
else
maxVisSlots = NUM_INVENTORYITEM_TYPES - 1;
if(inv->varCursorPos < maxVisSlots - 1 &&
!(inv->varCursorPos + 1 > inv->numUsedSlots - 1))
inv->varCursorPos++;
return;
}
// Else, a move left.
if(inv->selected == 0)
{
if(canWrap)
inv->selected = inv->numUsedSlots - 1;
}
else
inv->selected--;
// First the fixed range statusbar cursor.
if(inv->fixedCursorPos > 0)
inv->fixedCursorPos--;
// Now the variable range full-screen cursor.
if(inv->varCursorPos > 0)
inv->varCursorPos--;
}
void Hu_InventoryOpen(int player, boolean show)
{
player_t* plr;
hud_inventory_t* inv;
if(player < 0 || player >= MAXPLAYERS)
return;
plr = &players[player];
if(!plr->plr->inGame)
return;
inv = &hudInventories[player];
if(show)
{
inv->flags |= HIF_VISIBLE;
inv->hideTics = (int) (cfg.inventoryTimer * TICSPERSEC);
ST_HUDUnHide(player, HUE_FORCE);
}
else
{
inv->flags &= ~HIF_VISIBLE;
P_InventorySetReadyItem(player,
P_GetInvItem(inv->slots[inv->selected])->type);
}
}
boolean Hu_InventoryIsOpen(int player)
{
hud_inventory_t* inv;
if(player < 0 || player >= MAXPLAYERS)
return false;
inv = &hudInventories[player];
return ((inv->flags & HIF_VISIBLE)? true : false);
}
/**
* Mark the HUD inventory as dirty (i.e., the player inventory state has
* changed in such a way that would require the HUD inventory display(s)
* to be updated e.g., the player gains a new item).
*
* @param player Player whoose in HUD inventory is dirty.
*/
void Hu_InventoryMarkDirty(int player)
{
if(player < 0 || player >= MAXPLAYERS)
return;
hudInventories[player].flags |= HIF_IS_DIRTY;
}
boolean Hu_InventorySelect(int player, inventoryitemtype_t type)
{
assert(type == IIT_NONE ||
(type >= IIT_FIRST && type < NUM_INVENTORYITEM_TYPES));
if(player >= 0 && player < MAXPLAYERS)
{
hud_inventory_t* inv = &hudInventories[player];
if(P_InventoryCount(player, type))
{
uint i;
for(i = 0; i < inv->numUsedSlots; ++i)
if(P_GetInvItem(inv->slots[i])->type == type)
{
inv->selected = i;
inv->varCursorPos = inv->fixedCursorPos = 0;
return true;
}
}
}
return false;
}
boolean Hu_InventoryMove(int player, int dir, boolean canWrap, boolean silent)
{
player_t* plr;
hud_inventory_t* inv;
if(player < 0 || player >= MAXPLAYERS)
return false;
plr = &players[player];
if(!plr->plr->inGame)
return false;
inv = &hudInventories[player];
// Do the move first, before updating a possibly out of date inventory.
if(inv->numOwnedItemTypes > 1)
{
inventoryMove(inv, dir, canWrap);
}
if(inv->flags & HIF_IS_DIRTY)
{
rebuildInventory(inv);
}
if(inv->numOwnedItemTypes > 1)
{
P_InventorySetReadyItem(player,
P_GetInvItem(inv->selected)->type);
}
if(!silent)
{
inv->hideTics = (int) (cfg.inventoryTimer * TICSPERSEC);
}
return true;
}
void Hu_InventoryInit(void)
{
int i;
memset(hudInventories, 0, sizeof(hudInventories));
for(i = 0; i < MAXPLAYERS; ++i)
{
hud_inventory_t* inv = &hudInventories[i];
inv->flags = HIF_IS_DIRTY;
}
}
void Hu_InventoryTicker(void)
{
int i;
for(i = 0; i < MAXPLAYERS; ++i)
{
player_t* plr = &players[i];
hud_inventory_t* inv = &hudInventories[i];
if(!(plr->plr->inGame && (plr->plr->flags & DDPF_LOCAL)))
continue;
if(inv->flags & HIF_IS_DIRTY)
rebuildInventory(inv);
if(!P_IsPaused())
{
if(Hu_InventoryIsOpen(i))
{
// Turn inventory off after a certain amount of time?
if(cfg.inventoryTimer == 0)
{
inv->hideTics = 0;
}
else
{
if(inv->hideTics > 0)
inv->hideTics--;
if(inv->hideTics == 0 && cfg.inventoryTimer > 0)
Hu_InventoryOpen(i, false); // Close the inventory.
}
}
}
}
}
void ST_ResizeInventory(cvar_t* unused)
{
int i;
uint maxVisSlots;
if(cfg.inventorySlotMaxVis)
maxVisSlots = cfg.inventorySlotMaxVis;
else
maxVisSlots = NUM_INVENTORYITEM_TYPES - 1;
for(i = 0; i < MAXPLAYERS; ++i)
{
hud_inventory_t* inv = &hudInventories[i];
if(inv->varCursorPos >= maxVisSlots - 1)
{
inv->varCursorPos = maxVisSlots - 1;
}
inv->flags |= HIF_IS_DIRTY;
}
}
#endif