summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tilereg-inv.cc
blob: 2cc4b8c6fb9058718408f540189176ab2c8efc58 (plain) (blame)
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
#include "AppHdr.h"

#ifdef USE_TILE_LOCAL

#include "tilereg-inv.h"
#include "process_desc.h"

#include "butcher.h"
#include "cio.h"
#include "describe.h"
#include "env.h"
#include "invent.h"
#include "itemname.h"
#include "itemprop.h"
#include "items.h"
#include "item_use.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#include "misc.h"
#include "mon-util.h"
#include "options.h"
#include "output.h"
#include "spl-book.h"

#include "tiledef-dngn.h"
#include "tiledef-icons.h"
#include "tiledef-icons.h"
#include "tiledef-main.h"
#include "tilepick.h"
#include "unicode.h"
#include "viewgeom.h"

InventoryRegion::InventoryRegion(const TileRegionInit &init) : GridRegion(init)
{
}

void InventoryRegion::pack_buffers()
{
    // Pack base separately, as it comes from a different texture...
    unsigned int i = 0 + (m_grid_page*mx*my - m_grid_page*2); // this has to match the logic in cursor_index()
    for (int y = 0; y < my; y++)
    {
        for (int x = 0; x < mx; x++)
        {
            if (i >= m_items.size())
                break;

            InventoryTile &item = m_items[i++];

            if ((y==my-1 && x==mx-1 && item.tile)
                || (y==0 && x==0 && m_grid_page>0))
            {
                // draw a background for paging tiles
                m_buf.add_dngn_tile(TILE_ITEM_SLOT, x, y);
                continue;
            }

            if (item.flag & TILEI_FLAG_FLOOR)
            {
                if (i >= (unsigned int) mx * my * (m_grid_page+1) && item.tile)
                    break;

                int num_floor = tile_dngn_count(env.tile_default.floor);
                tileidx_t t = env.tile_default.floor + m_flavour[i] % num_floor;
                m_buf.add_dngn_tile(t, x, y);
            }
            else
                m_buf.add_dngn_tile(TILE_ITEM_SLOT, x, y);
        }
    }

    i = 0 + (m_grid_page*mx*my - m_grid_page*2); // this also has to match the logic in cursor_index()
    for (int y = 0; y < my; y++)
    {
        for (int x = 0; x < mx; x++)
        {
            if (i >= m_items.size())
                break;

            InventoryTile &item = m_items[i++];

            if (y==my-1 && x==mx-1 && item.tile)
            {
                // continuation to next page icon
                m_buf.add_main_tile(TILE_UNSEEN_ITEM, x, y);
                continue;
            }
            else if (y==0 && x==0 && m_grid_page>0)
            {
                // previous page icon
                m_buf.add_main_tile(TILE_UNSEEN_ITEM, x, y);
                continue;
            }

            if (item.flag & TILEI_FLAG_EQUIP)
            {
                if (item.flag & TILEI_FLAG_CURSE)
                    m_buf.add_main_tile(TILE_ITEM_SLOT_EQUIP_CURSED, x, y);
                else
                    m_buf.add_main_tile(TILE_ITEM_SLOT_EQUIP, x, y);

                if (item.flag & TILEI_FLAG_MELDED)
                    m_buf.add_icons_tile(TILEI_MESH, x, y);
            }
            else if (item.flag & TILEI_FLAG_CURSE)
                m_buf.add_main_tile(TILE_ITEM_SLOT_CURSED, x, y);

            if (item.flag & TILEI_FLAG_SELECT)
                m_buf.add_icons_tile(TILEI_ITEM_SLOT_SELECTED, x, y);

            if (item.flag & TILEI_FLAG_CURSOR)
                m_buf.add_icons_tile(TILEI_CURSOR, x, y);

            if (item.tile)
                m_buf.add_main_tile(item.tile, x, y);

            if (item.quantity != -1)
                draw_number(x, y, item.quantity);

            if (item.special)
                m_buf.add_main_tile(item.special, x, y, 0, 0);

            if (item.flag & TILEI_FLAG_TRIED)
                m_buf.add_icons_tile(TILEI_TRIED, x, y, 3, TILE_Y / 2);

            if (item.flag & TILEI_FLAG_INVALID)
                m_buf.add_icons_tile(TILEI_MESH, x, y);
        }
    }
}

int InventoryRegion::handle_mouse(MouseEvent &event)
{
    unsigned int item_idx;
    if (!place_cursor(event, item_idx))
        return 0;

    // handle paging
    if (m_cursor.x==(mx-1) && m_cursor.y==(my-1) && event.button==MouseEvent::LEFT)
    {
        // next page
        m_grid_page++;
        update();
        return CK_NO_KEY;
    }
    else if (m_cursor.x==0 && m_cursor.y==0 && m_grid_page>0 && event.button==MouseEvent::LEFT)
    {
        // prev page
        m_grid_page--;
        update();
        return CK_NO_KEY;
    }

    int idx = m_items[item_idx].idx;

    bool on_floor = m_items[item_idx].flag & TILEI_FLAG_FLOOR;

    ASSERT(idx >= 0);

    if (tiles.is_using_small_layout())
    {
        // close the inventory tab after successfully clicking on an item
        tiles.deactivate_tab();
    }

    // TODO enne - this is all really only valid for the on-screen inventory
    // Do we subclass InventoryRegion for the onscreen and offscreen versions?
    char key = m_items[item_idx].key;
    if (key)
        return key;

    if (event.button == MouseEvent::LEFT)
    {
        m_last_clicked_item = item_idx;
        tiles.set_need_redraw();
        if (on_floor)
        {
            if (event.mod & MOD_SHIFT)
                tile_item_use_floor(idx);
            else
                tile_item_pickup(idx, (event.mod & MOD_CTRL));
        }
        else
        {
            if (event.mod & MOD_SHIFT)
                tile_item_drop(idx, (event.mod & MOD_CTRL));
            else if (event.mod & MOD_CTRL)
                tile_item_use_secondary(idx);
            else
                tile_item_use(idx);
        }
        return CK_MOUSE_CMD;
    }
    else if (event.button == MouseEvent::RIGHT)
    {
        if (on_floor)
        {
            if (event.mod & MOD_SHIFT)
            {
                m_last_clicked_item = item_idx;
                tiles.set_need_redraw();
                tile_item_eat_floor(idx);
            }
            else
            {
                describe_item(mitm[idx]);
                redraw_screen();
            }
        }
        else // in inventory
        {
            describe_item(you.inv[idx], true);
            redraw_screen();
        }
        return CK_MOUSE_CMD;
    }

    return 0;
}

// NOTE: Assumes the item is equipped in the first place!
static bool _is_true_equipped_item(const item_def &item)
{
    // Weapons and staves are only truly equipped if wielded.
    if (item.link == you.equip[EQ_WEAPON])
        return is_weapon(item);

    // Cursed armour and rings are only truly equipped if *not* wielded.
    return item.link != you.equip[EQ_WEAPON];
}

// Returns whether there's any action you can take with an item in inventory
// apart from dropping it.
static bool _can_use_item(const item_def &item, bool equipped)
{
#if TAG_MAJOR_VERSION == 34
    // There's nothing you can do with an empty box if you can't unwield it.
    if (!equipped && item.sub_type == MISC_BUGGY_EBONY_CASKET)
        return false;
#endif

    // Vampires can drain corpses.
    if (item.base_type == OBJ_CORPSES)
    {
        return you.species == SP_VAMPIRE
               && item.sub_type != CORPSE_SKELETON
               && !food_is_rotten(item)
               && mons_has_blood(item.mon_type);
    }

    if (equipped && item.cursed())
    {
        // Misc. items/rods can always be evoked, cursed or not.
        if (item_is_evokable(item))
            return true;

        // You can't unwield/fire a wielded cursed weapon/staff
        // but cursed armour and rings can be unwielded without problems.
        return !_is_true_equipped_item(item);
    }

    // Mummies can't do anything with food or potions.
    if (you.species == SP_MUMMY)
        return item.base_type != OBJ_POTIONS && item.base_type != OBJ_FOOD;

    // In all other cases you can use the item in some way.
    return true;
}

static void _handle_wield_tip(string &tip, vector<command_type> &cmd,
                              const string prefix = "", bool unwield = false)
{
    tip += prefix;
    if (unwield)
        tip += "Unwield (%-)";
    else
        tip += "Wield (%)";
    cmd.push_back(CMD_WIELD_WEAPON);
}

bool InventoryRegion::update_tab_tip_text(string &tip, bool active)
{
    const char *prefix1 = active ? "" : "[L-Click] ";
    const char *prefix2 = active ? "" : "          ";

    tip = make_stringf("%s%s\n%s%s",
                       prefix1, "Display inventory",
                       prefix2, "Use items");

    return true;
}

bool InventoryRegion::update_tip_text(string& tip)
{
    if (m_cursor == NO_CURSOR)
        return false;

    unsigned int item_idx = cursor_index();
    if (item_idx >= m_items.size() || m_items[item_idx].empty())
        return false;

    // page next/prev
    if (_is_next_button(item_idx))
    {
        tip = "Next page\n[L-Click] Show next page of items";
        return true;
    }
    else if (_is_prev_button(item_idx))
    {
        tip = "Previous page\n[L-Click] Show previous page of items";
        return true;
    }

    int idx = m_items[item_idx].idx;

    // TODO enne - consider subclassing this class, rather than depending
    // on "key" to determine if this is the crt inventory or the on screen one.
    bool display_actions = (m_items[item_idx].key == 0
                    && mouse_control::current_mode() == MOUSE_MODE_COMMAND);

    // TODO enne - should the command keys here respect keymaps?
    vector<command_type> cmd;
    if (m_items[item_idx].flag & TILEI_FLAG_FLOOR)
    {
        const item_def &item = mitm[idx];

        if (!item.defined())
            return false;

        tip = "";
        if (m_items[item_idx].key)
        {
            tip = m_items[item_idx].key;
            tip += " - ";
        }

        tip += item.name(DESC_A);

        if (!display_actions)
            return true;

        if (item_is_stationary_net(item))
        {
            tip += make_stringf(" (holding %s)",
                                net_holdee(item)->name(DESC_A).c_str());
        }

        if (!item_is_stationary(item))
        {
            tip += "\n[L-Click] Pick up (%)";
            cmd.push_back(CMD_PICKUP);
            if (item.quantity > 1)
            {
                tip += "\n[Ctrl + L-Click] Partial pick up (%)";
                cmd.push_back(CMD_PICKUP_QUANTITY);
            }
        }
        if (item.base_type == OBJ_CORPSES
            && item.sub_type != CORPSE_SKELETON
            && !food_is_rotten(item))
        {
            tip += "\n[Shift + L-Click] ";
            if (can_bottle_blood_from_corpse(item.mon_type))
                tip += "Bottle blood";
            else
                tip += "Chop up";
            tip += " (%)";
            cmd.push_back(CMD_BUTCHER);

            if (you.species == SP_VAMPIRE)
            {
                tip += "\n\n[Shift + R-Click] Drink blood (e)";
                cmd.push_back(CMD_EAT);
            }
        }
        else if (item.base_type == OBJ_FOOD
                 && you.is_undead != US_UNDEAD
                 && you.species != SP_VAMPIRE)
        {
            tip += "\n[Shift + R-Click] Eat (e)";
            cmd.push_back(CMD_EAT);
        }
    }
    else
    {
        const item_def &item = you.inv[idx];
        if (!item.defined())
            return false;

        tip = item.name(DESC_INVENTORY_EQUIP);

        if (!display_actions)
            return true;

        int type = item.base_type;
        const bool equipped = m_items[item_idx].flag & TILEI_FLAG_EQUIP;
        bool wielded = (you.equip[EQ_WEAPON] == idx);

        const int EQUIP_OFFSET = NUM_OBJECT_CLASSES;

        if (_can_use_item(item, equipped))
        {
            string tip_prefix = "\n[L-Click] ";
            string tmp = "";
            if (equipped)
            {
                if (wielded && !item_is_evokable(item))
                {
                    if (type == OBJ_JEWELLERY || type == OBJ_ARMOUR
                        || is_weapon(item))
                    {
                        type = OBJ_WEAPONS + EQUIP_OFFSET;
                    }
                }
                else
                    type += EQUIP_OFFSET;
            }

            switch (type)
            {
            // first equipable categories
            case OBJ_WEAPONS:
            case OBJ_STAVES:
            case OBJ_RODS:
                if (you.species != SP_FELID)
                {
                    _handle_wield_tip(tmp, cmd);
                    if (is_throwable(&you, item))
                    {
                        tmp += "\n[Ctrl + L-Click] Fire (f)";
                        cmd.push_back(CMD_FIRE);
                    }
                }
                break;
            case OBJ_WEAPONS + EQUIP_OFFSET:
                _handle_wield_tip(tmp, cmd, "", true);
                if (is_throwable(&you, item))
                {
                    tmp += "\n[Ctrl + L-Click] Fire (f)";
                    cmd.push_back(CMD_FIRE);
                }
                break;
            case OBJ_MISCELLANY:
                if (item.sub_type >= MISC_DECK_OF_ESCAPE
                    && item.sub_type <= MISC_DECK_OF_DEFENCE)
                {
                    _handle_wield_tip(tmp, cmd);
                    break;
                }
                tmp += "Evoke (V)";
                cmd.push_back(CMD_EVOKE);
                break;
            case OBJ_MISCELLANY + EQUIP_OFFSET:
                if (item.sub_type >= MISC_DECK_OF_ESCAPE
                    && item.sub_type <= MISC_DECK_OF_DEFENCE)
                {
                    tmp += "Draw a card (%)";
                    cmd.push_back(CMD_EVOKE_WIELDED);
                    _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                    break;
                }
                // else fall-through
            case OBJ_RODS + EQUIP_OFFSET:
                tmp += "Evoke (%)";
                cmd.push_back(CMD_EVOKE_WIELDED);
                _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                break;
            case OBJ_ARMOUR:
                if (you.species != SP_FELID)
                {
                    tmp += "Wear (%)";
                    cmd.push_back(CMD_WEAR_ARMOUR);
                }
                break;
            case OBJ_ARMOUR + EQUIP_OFFSET:
                tmp += "Take off (%)";
                cmd.push_back(CMD_REMOVE_ARMOUR);
                break;
            case OBJ_JEWELLERY:
                tmp += "Put on (%)";
                cmd.push_back(CMD_WEAR_JEWELLERY);
                break;
            case OBJ_JEWELLERY + EQUIP_OFFSET:
                tmp += "Remove (%)";
                cmd.push_back(CMD_REMOVE_JEWELLERY);
                break;
            case OBJ_MISSILES:
                if (you.species != SP_FELID)
                {
                    tmp += "Fire (%)";
                    cmd.push_back(CMD_FIRE);

                    if (wielded || you.can_wield(item))
                        _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", wielded);
                }
                break;
            case OBJ_WANDS:
                if (you.species != SP_FELID)
                {
                    tmp += "Evoke (%)";
                    cmd.push_back(CMD_EVOKE);
                    if (wielded)
                        _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                }
                break;
            case OBJ_BOOKS:
                if (item_type_known(item) && item_is_spellbook(item)
                    && can_learn_spell(true))
                {
                    if (player_can_memorise_from_spellbook(item)
                        || has_spells_to_memorise(true))
                    {
                        tmp += "Memorise (%)";
                        cmd.push_back(CMD_MEMORISE_SPELL);
                    }
                    if (wielded)
                        _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                    break;
                }
                if (item.sub_type == BOOK_MANUAL)
                    break;
                // else fall-through
            case OBJ_SCROLLS:
                tmp += "Read (%)";
                cmd.push_back(CMD_READ);
                if (wielded)
                    _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                break;
            case OBJ_POTIONS:
                tmp += "Quaff (%)";
                cmd.push_back(CMD_QUAFF);
                if (wielded)
                    _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                break;
            case OBJ_FOOD:
                tmp += "Eat (%)";
                cmd.push_back(CMD_EAT);
                if (wielded)
                    _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                break;
            case OBJ_CORPSES:
                if (you.species == SP_VAMPIRE)
                {
                    tmp += "Drink blood (%)";
                    cmd.push_back(CMD_EAT);
                }

                if (wielded)
                {
                    if (you.species == SP_VAMPIRE)
                        tmp += "\n";
                    _handle_wield_tip(tmp, cmd, "\n[Ctrl + L-Click] ", true);
                }
                break;
            default:
                tmp += "Use";
            }

            if (!tmp.empty())
                tip += tip_prefix + tmp;
        }

        tip += "\n[R-Click] Describe";
        // Has to be non-equipped or non-cursed to drop.
        if (!equipped || !_is_true_equipped_item(you.inv[idx])
            || !you.inv[idx].cursed())
        {
            tip += "\n[Shift + L-Click] Drop (%)";
            cmd.push_back(CMD_DROP);
            if (you.inv[idx].quantity > 1)
            {
                tip += "\n[Ctrl-Shift + L-Click] Drop quantity (%#)";
                cmd.push_back(CMD_DROP);
            }
        }
    }

    insert_commands(tip, cmd);
    return true;
}

bool InventoryRegion::update_alt_text(string &alt)
{
    if (m_cursor == NO_CURSOR)
        return false;

    unsigned int item_idx = cursor_index();
    if (item_idx >= m_items.size() || m_items[item_idx].empty())
        return false;

    if (m_last_clicked_item >= 0
        && item_idx == (unsigned int) m_last_clicked_item)
    {
        return false;
    }

    int idx = m_items[item_idx].idx;
    const item_def *item;

    if (m_items[item_idx].flag & TILEI_FLAG_FLOOR)
        item = &mitm[idx];
    else
        item = &you.inv[idx];

    if (!item->defined())
        return false;

    describe_info inf;
    if (_is_next_button(item_idx))
    {
        // alt text for next page button
        inf.title = "Next page";
    }
    else if (_is_prev_button(item_idx))
    {
        // alt text for prev page button
        inf.title = "Previous page";
    }
    else
        get_item_desc(*item, inf);

    alt_desc_proc proc(crawl_view.msgsz.x, crawl_view.msgsz.y);
    process_description<alt_desc_proc>(proc, inf);

    proc.get_string(alt);

    return true;
}

void InventoryRegion::draw_tag()
{
    if (m_cursor == NO_CURSOR)
        return;
    int curs_index = cursor_index();
    if (curs_index >= (int)m_items.size())
        return;
    int idx = m_items[curs_index].idx;
    if (idx == -1)
        return;

    bool floor = m_items[curs_index].flag & TILEI_FLAG_FLOOR;

    if (_is_next_button(curs_index))
        draw_desc("Next page");
    else if (_is_prev_button(curs_index))
        draw_desc("Previous page");
    else if (floor && mitm[idx].defined())
        draw_desc(mitm[idx].name(DESC_PLAIN).c_str());
    else if (!floor && you.inv[idx].defined())
        draw_desc(you.inv[idx].name(DESC_INVENTORY_EQUIP).c_str());
}

void InventoryRegion::activate()
{
    if (inv_count() < 1)
    {
        canned_msg(MSG_NOTHING_CARRIED);
        flush_prev_message();
    }

    // switch to first page on activation
    m_grid_page = 0;
}

static void _fill_item_info(InventoryTile &desc, const item_info &item)
{
    desc.tile = tileidx_item(item);

    int type = item.base_type;
    if (type == OBJ_FOOD || type == OBJ_SCROLLS
        || type == OBJ_POTIONS || type == OBJ_MISSILES)
    {
        // -1 specifies don't display anything
        desc.quantity = (item.quantity == 1) ? -1 : item.quantity;
    }
    else if (type == OBJ_WANDS
             && ((item.flags & ISFLAG_KNOW_PLUSES)
                 || item.plus2 == ZAPCOUNT_EMPTY))
    {
        desc.quantity = item.plus;
    }
    else if (type == OBJ_RODS && item.flags & ISFLAG_KNOW_PLUSES)
        desc.quantity = item.plus / ROD_CHARGE_MULT;
    else
        desc.quantity = -1;

    if (type == OBJ_WEAPONS || type == OBJ_MISSILES
        || type == OBJ_ARMOUR || type == OBJ_RODS)
    {
        desc.special = tileidx_known_brand(item);
    }
    else if (type == OBJ_CORPSES)
        desc.special = tileidx_corpse_brand(item);

    desc.flag = 0;
    if (item.cursed())
        desc.flag |= TILEI_FLAG_CURSE;
    if (item.flags & ISFLAG_TRIED)
        desc.flag |= TILEI_FLAG_TRIED;
    if (item.pos.x != -1)
        desc.flag |= TILEI_FLAG_FLOOR;
}

void InventoryRegion::update()
{
    m_items.clear();
    m_dirty = true;

    if (mx * my == 0)
        return;

    const int max_pack_items = ENDOFPACK;

    bool inv_shown[ENDOFPACK];
    memset(inv_shown, 0, sizeof(inv_shown));

    int num_ground = 0;
    for (int i = you.visible_igrd(you.pos()); i != NON_ITEM; i = mitm[i].link)
        num_ground++;

    ucs_t c;
    const char *tp = Options.tile_show_items.c_str();
    int s;
    do // Do one last iteration with the 0 char at the end.
    {
        tp += s = utf8towc(&c, tp); // could be better to store this pre-parsed

        if ((int)m_items.size() >= max_pack_items)
            break;

        bool show_any = !c;
        object_class_type type = item_class_by_sym(c);

        // First, normal inventory
        for (int i = 0; i < ENDOFPACK; ++i)
        {
            if ((int)m_items.size() >= max_pack_items)
                break;

            if (inv_shown[i]
                || !you.inv[i].defined()
                || you.inv[i].quantity == 0
                || (!show_any && you.inv[i].base_type != type))
            {
                continue;
            }

            InventoryTile desc;
            _fill_item_info(desc, get_item_info(you.inv[i]));
            desc.idx = i;

            for (int eq = 0; eq < NUM_EQUIP; ++eq)
            {
                if (you.equip[eq] == i)
                {
                    desc.flag |= TILEI_FLAG_EQUIP;
                    if (you.melded[eq])
                        desc.flag |= TILEI_FLAG_MELDED;
                    break;
                }
            }

            inv_shown[i] = true;
            m_items.push_back(desc);
        }
    } while (s);

    int remaining = mx*my*(m_grid_page+1) - m_items.size();
    int empty_on_this_row = mx - m_items.size() % mx;

    // If we're not on the last row...
    if ((int)m_items.size() < mx * (my-1))
        // let's deliberately not do this on page 2
    {
        if (num_ground > remaining - empty_on_this_row)
        {
            // Fill out part of this row.
            int fill = remaining - num_ground;
            for (int i = 0; i < fill; ++i)
            {
                InventoryTile desc;
                if ((int)m_items.size() >= max_pack_items)
                    desc.flag |= TILEI_FLAG_INVALID;
                m_items.push_back(desc);
            }
        }
        else
        {
            // Fill out the rest of this row.
            while (m_items.size() % mx != 0)
            {
                InventoryTile desc;
                if ((int)m_items.size() >= max_pack_items)
                    desc.flag |= TILEI_FLAG_INVALID;
                m_items.push_back(desc);
            }

            // Add extra rows, if needed.
            unsigned int ground_rows = max((num_ground-1) / mx + 1, 1);

            while ((int)(m_items.size() / mx + ground_rows) < my
                   && ((int)m_items.size()) < max_pack_items)
            {
                for (int i = 0; i < mx; i++)
                {
                    InventoryTile desc;
                    if ((int)m_items.size() >= max_pack_items)
                        desc.flag |= TILEI_FLAG_INVALID;
                    m_items.push_back(desc);
                }
            }
        }
    }

    // Then, as many ground items as we can fit.
    bool ground_shown[MAX_ITEMS];
    memset(ground_shown, 0, sizeof(ground_shown));

    tp = Options.tile_show_items.c_str();
    do
    {
        tp += s = utf8towc(&c, tp);

        if ((int)m_items.size() >= mx * my * (m_grid_page+1))
            break;

        bool show_any = !c;
        object_class_type type = item_class_by_sym(c);

        for (int i = you.visible_igrd(you.pos()); i != NON_ITEM;
             i = mitm[i].link)
        {
            if ((int)m_items.size() >= mx * my * (m_grid_page+1))
                break;

            if (ground_shown[i] || !show_any && mitm[i].base_type != type)
                continue;

            InventoryTile desc;
            _fill_item_info(desc, get_item_info(mitm[i]));
            desc.idx = i;
            ground_shown[i] = true;

            m_items.push_back(desc);
        }
    } while (s);

    while ((int)m_items.size() < mx * my)
        // let's not do this for p2 either
    {
        InventoryTile desc;
        desc.flag = TILEI_FLAG_FLOOR;
        m_items.push_back(desc);
    }
}

bool InventoryRegion::_is_prev_button(int idx)
{
    // idx is an index in m_items as returned by cursor_index()
    return m_grid_page>0 && idx == mx*my*m_grid_page-2*m_grid_page;
}

bool InventoryRegion::_is_next_button(int idx)
{
    // idx is an index in m_items as returned by cursor_index()
    return idx == mx*my*(m_grid_page+1)-1;
}
#endif