summaryrefslogtreecommitdiff
path: root/src/game-server/inventory.cpp
blob: d7baedf2023be2f3145c6980a42ca25595a9806e (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
/*
 *  The Mana Server
 *  Copyright (C) 2004-2010  The Mana World Development Team
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server 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
 *  any later version.
 *
 *  The Mana Server 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 The Mana Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <algorithm>
#include <cassert>

#include "game-server/gamehandler.h"
#include "game-server/inventory.h"
#include "game-server/item.h"
#include "game-server/itemmanager.h"
#include "net/messageout.h"
#include "utils/logger.h"

Inventory::Inventory(Character *p, bool d):
    mPoss(&p->getPossessions()), mInvMsg(GPMSG_INVENTORY),
    mEqmMsg(GPMSG_EQUIP), mCharacter(p), mDelayed(d)
{
}

Inventory::~Inventory()
{
    commit(false);
}

void Inventory::restart()
{
    mInvMsg.clear();
    mInvMsg.writeInt16(GPMSG_INVENTORY);
}

void Inventory::cancel()
{
    assert(mDelayed);
    Possessions &poss = mCharacter->getPossessions();
    if (mPoss != &poss)
    {
        delete mPoss;
        mPoss = &poss;
    }
    restart();
}

void Inventory::commit(bool doRestart)
{
    Possessions &poss = mCharacter->getPossessions();
    /* Sends changes, whether delayed or not. */
    if (mInvMsg.getLength() > 2)
    {
        /* Send the message to the client directly. Perhaps this should be
           done through an update flag, too? */
        gameHandler->sendTo(mCharacter, mInvMsg);
    }
    if (mPoss != &poss)
    {
        if (mDelayed)
        {
            /*
             * Search for any and all changes to equipment.
             * Search through equipment for changes between
             * old and new equipment.
             * Send changes directly when there is a change.
             * Even when equipment references to invy slots are the same,
             * it still needs to be searched for changes
             * to the internal equiment slot usage.
             * This is probably the worst part of doing this in delayed mode.
             */
            IdSlotMap oldEquip, newEquip;
            {
                EquipData::const_iterator it1, it2, it1_end, it2_end;
                for (it1 = mPoss->equipSlots.begin(),
                     it1_end = mPoss->equipSlots.end();
                    it1 != it1_end;
                    ++it1)
                {
#ifdef INV_CONST_BOUND_DEBUG
                        IdSlotMap::const_iterator temp2, temp =
#endif
                        newEquip.insert(
                                newEquip.upper_bound(it1->second),
                                std::make_pair(it1->second, it1->first));
#ifdef INV_CONST_BOUND_DEBUG
                        if (temp !=
                            --(temp2 = newEquip.upper_bound(it1->second)))
                            throw;
#endif
                }
                for (it2 = poss.equipSlots.begin(),
                     it2_end = poss.equipSlots.end();
                    it2 != it2_end;
                    ++it2)
                    oldEquip.insert(
                            oldEquip.upper_bound(it2->second),
                            std::make_pair(it2->second, it2->first));
            }
            {
                IdSlotMap::const_iterator it1     = newEquip.begin(),
                                          it2     = oldEquip.begin(),
                                          it1_end = newEquip.end(),
                                          it2_end = oldEquip.end(),
                                          temp1, temp2;
                while (it1 != it1_end || it2 != it2_end)
                {
                    if (it1 == it1_end)
                    {
                        if (it2 == it2_end)
                            break;
                        equip_sub(0, it1);
                    }
                    else if (it2 == it2_end)
                        equip_sub(newEquip.count(it2->first), it2);
                    else if (it1->first == it2->first)
                    {
                        double invSlot = it1->first;
                        while ((it1 != it1_end && it1->first == invSlot) ||
                               (it2 != it2_end && it2->first == invSlot))
                        {
                            /*
                             * Item is still equipped, but need to check
                             *      that the slots didn't change.
                             */
                            if (it1->second == it2->second)
                            {
                                // No change.
                                ++it1;
                                ++it2;
                                continue;
                            }
                            unsigned int itemId =
                                    mPoss->inventory.at(it1->first).itemId;
                            changeEquipment(itemId, itemId);
                            break;
                        }
                    }
                    else if (it1->first > it2->first)
                        equip_sub(newEquip.count(it2->first), it2);
                    else // it1->first < it2->first
                        equip_sub(0, it1);
                }
            }
        }
        poss = *mPoss;
        delete mPoss;
        mPoss = &poss;
    }

    /* Update server sided states if in delayed mode. If we are not in
       delayed mode, the server sided states already reflect the changes
       that have just been sent to the client. */

    if (mEqmMsg.getLength() > 2)
        gameHandler->sendTo(mCharacter, mEqmMsg);

    if (doRestart)
        restart();
}

void Inventory::equip_sub(unsigned int newCount, IdSlotMap::const_iterator &it)
{
    const unsigned int invSlot = it->first;
    unsigned int count = 0, eqSlot = it->second;
    mEqmMsg.writeInt16(invSlot);
    mEqmMsg.writeInt8(newCount);
    do {
        if (newCount)
        {
            if (it->second != eqSlot)
            {
                mEqmMsg.writeInt8(eqSlot);
                mEqmMsg.writeInt8(count);
                count = 1;
                eqSlot = it->second;
            }
            ++count;
        }
        if (itemManager->isEquipSlotVisible(it->second))
            mCharacter->raiseUpdateFlags(UPDATEFLAG_LOOKSCHANGE);
    } while ((++it)->first == invSlot);
    if (count)
    {
        mEqmMsg.writeInt8(eqSlot);
        mEqmMsg.writeInt8(count);
    }
    mEqmMsg.writeInt16(invSlot);
    changeEquipment(newCount ? 0 : mPoss->inventory.at(invSlot).itemId,
                    newCount ? mPoss->inventory.at(invSlot).itemId : 0);
}

void Inventory::prepare()
{
    if (!mDelayed)
        return;

    Possessions *poss = &mCharacter->getPossessions();
    if (mPoss == poss)
        mPoss = new Possessions(*poss);
}

void Inventory::sendFull() const
{
    /* Sends all the information needed to construct inventory
       and equipment to the client */
    MessageOut m(GPMSG_INVENTORY_FULL);

    m.writeInt16(mPoss->inventory.size());
    for (InventoryData::const_iterator l = mPoss->inventory.begin(),
         l_end = mPoss->inventory.end(); l != l_end; ++l)
    {
        assert(l->second.itemId);
        m.writeInt16(l->first); // Slot id
        m.writeInt16(l->second.itemId);
        m.writeInt16(l->second.amount);
    }

    for (EquipData::const_iterator k = mPoss->equipSlots.begin(),
         k_end = mPoss->equipSlots.end();
         k != k_end;
         ++k)
    {
        m.writeInt8(k->first);      // equip slot
        m.writeInt16(k->second);    // inventory slot
    }

    gameHandler->sendTo(mCharacter, m);
}

void Inventory::initialize()
{
    assert(!mDelayed);

    InventoryData::iterator it1;
    EquipData::const_iterator it2, it2_end = mPoss->equipSlots.end();
    /*
     * Apply all exists triggers.
     * Remove unknown inventory items.
     */

    typedef std::set<unsigned int> ItemIdSet;
    ItemIdSet itemIds;

    /*
     * Construct a set of itemIds to keep track of duplicate itemIds.
     */
    for (it1 = mPoss->inventory.begin(); it1 != mPoss->inventory.end();)
    {
        ItemClass *item = itemManager->getItem(it1->second.itemId);
        if (item)
        {
            if (itemIds.insert(it1->second.itemId).second)
                item->useTrigger(mCharacter, ITT_IN_INVY);
            ++it1;
        }
        else
        {
            LOG_WARN("Inventory: deleting unknown item type "
                     << it1->second.itemId << " from the inventory of '"
                     << mCharacter->getName()
                     << "'!");
            mPoss->inventory.erase(it1++);
        }
    }

    itemIds.clear();

    typedef std::set<unsigned int> SlotSet;
    SlotSet equipment;

    /*
     * Construct a set of slot references from equipment to keep track of
     * duplicate slot usage.
     */
    for (it2 = mPoss->equipSlots.begin(); it2 != it2_end; ++it2)
    {
        if (equipment.insert(it2->second).second)
        {
            /*
             * Perform checks for equipped items
             * Check that all needed slots are available.
             */
            // TODO - Not needed for testing everything else right now, but
            //        will be needed for production
            /*
             * Apply all equip triggers.
             */
            itemManager->getItem(mPoss->inventory.at(it2->second).itemId)
                    ->useTrigger(mCharacter, ITT_EQUIP);
        }
    }

    equipment.clear();

    checkInventorySize();
}

void Inventory::checkInventorySize()
{
    /*
     * Check that the inventory size is greater than or equal to the size
     *       needed.
     *       If not, forcibly delete (drop?) items from the end until it is.
     * Check that inventory capacity is greater than or equal to zero.
     *       If not, forcibly delete (drop?) items from the end until it is.
     */
    while (mPoss->inventory.size() > INVENTORY_SLOTS
           || mCharacter->getModifiedAttribute(ATTR_INV_CAPACITY) < 0)
    {
        LOG_WARN("Inventory: oversize inventory! Deleting '"
                 << mPoss->inventory.rbegin()->second.amount
                 << "' items of type '"
                 << mPoss->inventory.rbegin()->second.itemId
                 << "' from slot '"
                 << mPoss->inventory.rbegin()->first
                 << "' of character '"
                 << mCharacter->getName()
                 << "'!");
        // FIXME Should probably be dropped rather than deleted.
        removeFromSlot(mPoss->inventory.rbegin()->first,
                       mPoss->inventory.rbegin()->second.amount);
    }
}

unsigned int Inventory::getItem(unsigned int slot) const
{
    InventoryData::iterator item = mPoss->inventory.find(slot);
    return item != mPoss->inventory.end() ? item->second.itemId : 0;
}

unsigned int Inventory::insert(unsigned int itemId, unsigned int amount)
{
    unsigned int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot();
    if (!itemId || !amount)
        return 0;
    prepare();
    InventoryData::iterator it, it_end = mPoss->inventory.end();
    // Add to slots with existing items of this type first.
    for (it = mPoss->inventory.begin(); it != it_end; ++it)
        if (it->second.itemId == itemId)
        {
            // If the slot is full, try the next slot
            if (it->second.amount >= maxPerSlot)
                continue;

            // Add everything that'll fit to the stack
            unsigned short spaceleft = maxPerSlot - it->second.amount;
            if (spaceleft >= amount)
            {
                it->second.amount += amount;
                amount = 0;
            }
            else
            {
                it->second.amount += spaceleft;
                amount -= spaceleft;
            }

            mInvMsg.writeInt16(it->first);
            mInvMsg.writeInt16(itemId);
            mInvMsg.writeInt16(it->second.amount);
            if (!amount)
                return 0;
        }

    int slot = 0;
    // We still have some left, so add to blank slots.
    for (it = mPoss->inventory.begin();; ++it)
    {
        if (!amount)
            return 0;
        int lim = (it == it_end) ? INVENTORY_SLOTS : it->first;
        while (amount && slot < lim)
        {
            int additions = std::min(amount, maxPerSlot);
            mPoss->inventory[slot].itemId = itemId;
            mPoss->inventory[slot].amount = additions;
            amount -= additions;
            mInvMsg.writeInt16(slot++); // Last read, so also increment
            mInvMsg.writeInt16(itemId);
            mInvMsg.writeInt16(additions);
        }
        ++slot; // Skip the slot that the iterator points to
        if (it == it_end) break;
    }

    checkInventorySize();

    return amount;
}

unsigned int Inventory::count(unsigned int itemId) const
{
    unsigned int nb = 0;
    for (InventoryData::iterator it = mPoss->inventory.begin(),
                                 it_end = mPoss->inventory.end();
         it != it_end; ++it)
        if (it->second.itemId == itemId)
            nb += it->second.amount;
    return nb;
}

unsigned int Inventory::remove(unsigned int itemId, unsigned int amount, bool force)
{
    prepare();
    bool inv = false,
         eq = !itemManager->getItem(itemId)->getItemEquipData().empty();
    for (InventoryData::iterator it = mPoss->inventory.begin(),
                                 it_end = mPoss->inventory.end();
         it != it_end; ++it)
        if (it->second.itemId == itemId)
        {
            if (amount)
            {
                if (eq)
                {
                    // If the item is equippable,
                    // we have additional checks to make.
                    bool ch = false;
                    for (EquipData::iterator it2 = mPoss->equipSlots.begin(),
                                         it2_end = mPoss->equipSlots.end();
                         it2 != it2_end;
                         ++it2)
                        if (it2->second == it->first)
                        {
                            if (force)
                                unequip(it2);
                            else
                                ch = inv = true;
                            break;
                        }
                    if (ch && !force)
                        continue;
                }
                unsigned int sub = std::min(amount, it->second.amount);
                amount -= sub;
                it->second.amount -= sub;
                mInvMsg.writeInt16(it->first);
                if (it->second.amount)
                {
                    mInvMsg.writeInt16(it->second.itemId);
                    mInvMsg.writeInt16(it->second.amount);
                    // Some still exist, and we have none left to remove, so
                    // no need to run leave invy triggers.
                    if (!amount)
                        return 0;
                }
                else
                {
                    mInvMsg.writeInt16(0);
                    mPoss->inventory.erase(it);
                }
            }
            else
                // We found an instance of them existing and have none left to
                // remove, so no need to run leave invy triggers.
                return 0;
        }
    if (force)
        itemManager->getItem(itemId)->useTrigger(mCharacter, ITT_LEAVE_INVY);
    // Rather inefficient, but still usable for now assuming small invy size.
    // FIXME
    return inv && !force ? remove(itemId, amount, true) : amount;
}

unsigned int Inventory::move(unsigned int slot1, unsigned int slot2,
                             unsigned int amount)
{
    if (!amount || slot1 == slot2 || slot2 >= INVENTORY_SLOTS)
        return amount;
    prepare();
    InventoryData::iterator it1 = mPoss->inventory.find(slot1),
                            it2 = mPoss->inventory.find(slot2),
                            inv_end = mPoss->inventory.end();

    if (it1 == inv_end)
        return amount;

    EquipData::iterator it, it_end = mPoss->equipSlots.end();
    for (it = mPoss->equipSlots.begin();
         it != it_end;
         ++it)
        if (it->second == slot1)
            // Bad things will happen when you can stack multiple equippable
            //     items in the same slot anyway.
            it->second =  slot2;

    unsigned int nb = std::min(amount, it1->second.amount);
    if (it2 == inv_end)
    {
        // Slot2 does not yet exist.
        mPoss->inventory[slot2].itemId = it1->second.itemId;
        nb = std::min(itemManager->getItem(it1->second.itemId)->getMaxPerSlot(),
                      nb);

        mPoss->inventory[slot2].amount = nb;
        it1->second.amount -= nb;
        amount -= nb;

        mInvMsg.writeInt16(slot1);                  // Slot
        if (it1->second.amount)
        {
            mInvMsg.writeInt16(it1->second.itemId); // Item Id
            mInvMsg.writeInt16(it1->second.amount); // Amount
        }
        else
        {
            mInvMsg.writeInt16(0);
            mPoss->inventory.erase(it1);
        }
        mInvMsg.writeInt16(slot2);                  // Slot
        mInvMsg.writeInt16(it1->second.itemId);     // Item Id (same as slot 1)
        mInvMsg.writeInt16(nb);                     // Amount
    }
    else
    {
        // Slot2 exists.
        if (it2->second.itemId != it1->second.itemId)
            return amount; // Cannot stack items of a different type.
        nb = std::min(itemManager->getItem(it1->second.itemId)->getMaxPerSlot()
                          - it2->second.amount,
                      nb);

        it1->second.amount -= nb;
        it2->second.amount += nb;
        amount -= nb;

        mInvMsg.writeInt16(slot1);                  // Slot
        if (it1->second.amount)
        {
            mInvMsg.writeInt16(it1->second.itemId); // Item Id
            mInvMsg.writeInt16(it1->second.amount); // Amount
        }
        else
        {
            mInvMsg.writeInt16(0);
            mPoss->inventory.erase(it1);
        }
        mInvMsg.writeInt16(slot2);                  // Slot
        mInvMsg.writeInt16(it2->second.itemId);     // Item Id
        mInvMsg.writeInt16(it2->second.amount);     // Amount
    }
    return amount;
}

unsigned int Inventory::removeFromSlot(unsigned int slot, unsigned int amount)
{
    prepare();

    InventoryData::iterator it = mPoss->inventory.find(slot);

    // When the given slot doesn't exist, we can't remove anything
    if (it == mPoss->inventory.end())
        return amount;

    // Check if an item of the same class exists elsewhere in the inventory
    bool exists = false;
    for (InventoryData::const_iterator it2 = mPoss->inventory.begin(),
         it2_end = mPoss->inventory.end();
         it2 != it2_end; ++it2)
    {
        if (it2->second.itemId == it->second.itemId
                && it->first != it2->first)
        {
            exists = true;
            break;
        }
    }
    if (!exists && it->second.itemId) {
        if (ItemClass *ic = itemManager->getItem(it->second.itemId))
            ic->useTrigger(mCharacter, ITT_LEAVE_INVY);
    }

    unsigned int sub = std::min(amount, it->second.amount);
    amount -= sub;
    it->second.amount -= sub;
    mInvMsg.writeInt16(it->first);
    if (it->second.amount)
    {
        mInvMsg.writeInt16(it->second.itemId);
        mInvMsg.writeInt16(it->second.amount);
    }
    else
    {
        mInvMsg.writeInt16(0);
        mPoss->inventory.erase(it);
    }
    return amount;
}


void Inventory::changeEquipment(unsigned int oldId, unsigned int newId)
{
    if (!oldId && !newId)
        return;
    changeEquipment(oldId ? itemManager->getItem(oldId) : 0,
                    newId ? itemManager->getItem(newId) : 0);
}

void Inventory::changeEquipment(ItemClass *oldI, ItemClass *newI)
{
    // This should only be called when applying changes, either directly
    // in non-delayed mode or when the changes are committed in delayed mode.
    if (!oldI && !newI)
        return;
    if (oldI && newI)
        oldI->useTrigger(mCharacter, ITT_EQUIPCHG);
    else if (oldI)
        oldI->useTrigger(mCharacter, ITT_UNEQUIP);
    else if (newI)
        newI->useTrigger(mCharacter, ITT_EQUIP);
}

bool Inventory::equip(int slot, bool override)
{
    if (mPoss->equipSlots.count(slot))
        return false;
    InventoryData::iterator it;
    if ((it = mPoss->inventory.find(slot)) == mPoss->inventory.end())
        return false;
    const ItemEquipsInfo &eq = itemManager->getItem(it->second.itemId)
                                    ->getItemEquipData();
    if (eq.empty())
        return false;
    ItemEquipInfo const *ovd = 0;
    // Iterate through all possible combinations of slots
    for (ItemEquipsInfo::const_iterator it2 = eq.begin(),
         it2_end = eq.end(); it2 != it2_end; ++it2)
    {
        // Iterate through this combination of slots.
        /*
         * 0 = all ok, slots free
         * 1 = possible if other items are unequipped first
         * 2 = impossible, requires too many slots
         *     even with other equipment being removed
         */
        int fail = 0;
        ItemEquipInfo::const_iterator it3, it3_end;
        for (it3 = it2->begin(),
             it3_end = it2->end();
             it3 != it3_end;
             ++it3)
        {
            // it3 -> { slot id, number required }
            unsigned int max = itemManager->getMaxSlotsFromId(it3->first),
                         used = mPoss->equipSlots.count(it3->first);
            if (max - used >= it3->second)
                continue;
            else if (max >= it3->second)
            {
                fail |= 1;
                if (override)
                    continue;
                else
                    break;
            }
            else
            {
                fail |= 2;
                break;
            }
        }
        switch (fail)
        {
            case 0:
            /*
             * Clean fit. Equip and apply immediately.
             */
            if (!mDelayed) {
                mEqmMsg.writeInt16(slot);           // Inventory slot
                mEqmMsg.writeInt8(it2->size());     // Equip slot type count
            }
            for (it3 = it2->begin(),
                 it3_end = it2->end();
                 it3 != it3_end;
                 ++it3)
            {
                if (!mDelayed) {
                    mEqmMsg.writeInt8(it3->first);  // Equip slot
                    mEqmMsg.writeInt8(it3->second); // How many are used
                }
                /*
                 * This bit can be somewhat inefficient, but is far better for
                 *  average case assuming most equip use one slot max for each
                 *  type and infrequently (<1/3) two of each type max.
                 * If the reader cares, you're more than welcome to add
                 *  compile time options optimising for other usage.
                 * For now, this is adequate assuming `normal' usage.
                 */
                for (unsigned int i = 0; i < it3->second; ++i)
                    mPoss->equipSlots.insert(
                            std::make_pair(it3->first, slot));
            }
            if (!mDelayed)
                changeEquipment(0, it->second.itemId);
            return true;
            case 1:
            /*
             * Definitions earlier in the item file have precedence (even if it
             *      means requiring unequipping more), so no need to store more
             *      than the first.
             */
            if (override && !ovd)
                ovd = &*it2; // Iterator -> object -> pointer.
            break;
            case 2:
            default:
            /*
             * Since slots are currently static (and I don't see any reason to
             *      change this right now), something probably went wrong.
             * The logic to catch this is here rather than in the item manager
             *      just in case non-static equip slots do want to be
             *      implemented later. This would not be a trivial task,
             *      however.
             */
            LOG_WARN("Inventory - item '" << it->second.itemId <<
                     "' cannot be equipped, even by unequipping other items!");
            break;
        }
    }
    // We didn't find a clean equip.
    if (ovd)
    {
        /*
         * We did find an equip that works if we unequip other items,
         * and we can override.
         * Process unequip triggers for all items we have to unequip.
         * Process equip triggers for new item.
         * Attempt to reequip any equipment we had to remove,
         * but disallowing override.
         */

        // TODO - this would increase ease of use substatially, add as soon as
        // there is time to do so.

        return false; // Return true when this section is complete
    }
    /*
     * We cannot equip, either because we could not find any valid equip process
     *     or because we found a dirty equip and weren't allowed to override.
     */
    return false;
}

bool Inventory::unequip(EquipData::iterator it)
{
    return unequip(it->second, &it);
}

bool Inventory::unequip(unsigned int slot, EquipData::iterator *itp)
{
    prepare();
    EquipData::iterator it = itp ? *itp : mPoss->equipSlots.begin(),
                        it_end = mPoss->equipSlots.end();
    bool changed = false;

    // Erase all equip entries that point to the given inventory slot
    while (it != it_end)
    {
        if (it->second == slot)
        {
            changed = true;
            mPoss->equipSlots.erase(it++);
        }
        else
        {
            ++it;
        }
    }

    if (changed && !mDelayed)
    {
        changeEquipment(mPoss->inventory.at(slot).itemId, 0);
        mEqmMsg.writeInt16(slot);
        mEqmMsg.writeInt8(0);
    }

    return changed;
}