forked from SecretsOTheP/EQMacEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.cpp
2783 lines (2401 loc) · 94.7 KB
/
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
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2003 EQEMu Development Team (http://eqemulator.net)
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; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "../common/global_define.h"
#include "../common/eqemu_logsys.h"
#include "../common/strings.h"
#include "quest_parser_collection.h"
#include "worldserver.h"
#include "zonedb.h"
#include "queryserv.h"
#include "string_ids.h"
extern WorldServer worldserver;
extern QueryServ* QServ;
// @merth: this needs to be touched up
uint32 Client::NukeItem(uint32 itemnum, uint8 where_to_check) {
if (itemnum == 0)
return 0;
uint32 x = 0;
EQ::ItemInstance *cur = nullptr;
int i;
if(where_to_check & invWhereWorn) {
for (i = EQ::invslot::EQUIPMENT_BEGIN; i <= EQ::invslot::EQUIPMENT_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
}
if(where_to_check & invWhereCursor) {
if (GetItemIDAt(EQ::invslot::slotCursor) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(EQ::invslot::slotCursor) != INVALID_ID)) {
cur = m_inv.GetItem(EQ::invslot::slotCursor);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(EQ::invslot::slotCursor, 0, true);
}
for (i = EQ::invbag::CURSOR_BAG_BEGIN; i <= EQ::invbag::CURSOR_BAG_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
}
if(where_to_check & invWherePersonal) {
for (i = EQ::invslot::GENERAL_BEGIN; i <= EQ::invslot::GENERAL_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
for (i = EQ::invbag::GENERAL_BAGS_BEGIN; i <= EQ::invbag::GENERAL_BAGS_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
}
if(where_to_check & invWhereBank) {
for (i = EQ::invslot::BANK_BEGIN; i <= EQ::invslot::BANK_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
for (i = EQ::invbag::BANK_BAGS_BEGIN; i <= EQ::invbag::BANK_BAGS_END; i++) {
if (GetItemIDAt(i) == itemnum || (itemnum == 0xFFFE && GetItemIDAt(i) != INVALID_ID)) {
cur = m_inv.GetItem(i);
if(cur && cur->GetItem()->Stackable) {
x += cur->GetCharges();
} else {
x++;
}
DeleteItemInInventory(i, 0, true);
}
}
}
return x;
}
bool Client::CheckLoreConflict(const EQ::ItemData* item) {
if (!item)
return false;
if (item->Lore[0] != '*' && item->Lore[0] != '#')
return false;
if (item->Lore[0] == '*') // Standard lore items; look everywhere except unused, return the result
return (m_inv.HasItem(item->ID, 0, ~invWhereUnused) != INVALID_INDEX);
else if(item->Lore[0] == '#')
return (m_inv.HasArtifactItem() != INVALID_INDEX);
return false;
}
bool Client::SummonItem(uint32 item_id, int8 quantity, uint16 to_slot, bool force_charges) {
this->EVENT_ITEM_ScriptStopReturn();
// TODO: update calling methods and script apis to handle a failure return
const EQ::ItemData* item = database.GetItem(item_id);
// make sure the item exists
if(item == nullptr) {
Message(CC_Red, "Item %u does not exist.", item_id);
Log(Logs::Detail, Logs::Inventory, "Player %s on account %s attempted to create an item with an invalid id.\n(Item: %u)\n",
GetName(), account_name, item_id);
return false;
}
// check that there is not a lore conflict between base item and existing inventory
else if(CheckLoreConflict(item)) {
// DuplicateLoreMessage(item_id);
if(item->Lore[0] == '#')
Message(CC_Red, "You already have an artifact item in your inventory.");
else
Message(CC_Red, "You already have a lore %s (%i) in your inventory.", item->Name, item_id);
return false;
}
// This code is ready to implement once the item load code is changed to process the 'minstatus' field.
// Checking #iteminfo in-game verfies that item->MinStatus is set to '0' regardless of field value.
// An optional sql script will also need to be added, once this goes live, to allow changing of the min status.
// check to make sure we are a GM if the item is GM-only
else if(item->GMFlag == -1 && this->Admin() < RuleI(GM, MinStatusToUseGMItem)) {
Message(CC_Red, "You are not a GM or do not have the status to summon this item.");
Log(Logs::Detail, Logs::Inventory, "Player %s on account %s attempted to create a GM-only item with a status of %i.\n(Item: %u, GMFlag: %u)\n",
GetName(), account_name, this->Admin(), item->ID, item->GMFlag);
return false;
}
uint32 classes = item->Classes;
uint32 races = item->Races;
uint32 slots = item->Slots;
// validation passed..so, set the quantity and create the actual item
if(quantity < 0)
{
quantity = 1;
}
else if (quantity == 0)
{
if(database.ItemQuantityType(item_id) == EQ::item::Quantity_Normal)
{
quantity = 1;
}
else if(database.ItemQuantityType(item_id) == EQ::item::Quantity_Charges)
{
if(!force_charges)
quantity = item->MaxCharges;
}
else if(database.ItemQuantityType(item_id) == EQ::item::Quantity_Stacked)
{
//If no value is set coming from a quest method, only summon a single item.
if(to_slot == EQ::legacy::SLOT_QUEST)
{
quantity = 1;
}
else
{
quantity = item->StackSize;
}
}
}
// in any other situation just use quantity as passed
EQ::ItemInstance* inst = database.CreateItem(item, quantity);
if(inst == nullptr) {
Message(CC_Red, "An unknown server error has occurred and your item was not created.");
// this goes to logfile since this is a major error
Log(Logs::General, Logs::Error, "Player %s on account %s encountered an unknown item creation error.\n(Item: %u)\n",
GetName(), account_name, item->ID);
return false;
}
// check to see if item is usable in requested slot
if(to_slot != EQ::legacy::SLOT_QUEST && ((to_slot >= EQ::invslot::slotEar1) && (to_slot <= EQ::invslot::slotAmmo))) {
uint32 slottest = 22; // can't change '22' just yet...
if(!(slots & ((uint32)1 << slottest))) {
Message(CC_Default, "This item is not equipable at slot %u - moving to cursor.", to_slot);
Log(Logs::Detail, Logs::Inventory, "Player %s on account %s attempted to equip an item unusable in slot %u - moved to cursor.\n(Item: %u)\n",
GetName(), account_name, to_slot, item->ID);
to_slot = EQ::invslot::slotCursor;
}
}
//We're coming from a quest method.
if(to_slot == EQ::legacy::SLOT_QUEST)
{
bool stacking = TryStacking(inst);
//If we were able to stack, there is no need to continue on as we're set.
if(stacking)
{
safe_delete(inst);
return true;
}
else
{
bool bag = false;
if(inst->IsType(EQ::item::ItemClassBag))
{
bag = true;
}
to_slot = m_inv.FindFreeSlot(bag, true, item->Size);
//make sure we are not completely full...
if(to_slot == EQ::invslot::slotCursor || to_slot == INVALID_INDEX)
{
if (inst->GetItem()->NoDrop == 0 || zone && zone->GetGuildID() != GUILD_NONE)
{
//If it's no drop, force it to the cursor. This carries the risk of deletion if the player already has this item on their cursor
// or if the cursor queue is full. But in this situation, we have little other recourse.
PushItemOnCursorWithoutQueue(inst);
Log(Logs::General, Logs::Inventory, "%s has a full inventory and %s is a no drop item. Forcing to cursor.", GetName(), inst->GetItem()->Name);
safe_delete(inst);
return true;
}
else if(m_inv.GetItem(EQ::invslot::slotCursor) != nullptr || to_slot == INVALID_INDEX)
{
CreateGroundObject(inst, glm::vec4(GetX(), GetY(), GetZ(), 0), RuleI(Groundspawns, FullInvDecayTime), true);
safe_delete(inst);
return true;
}
}
}
}
if (to_slot == EQ::invslot::slotCursor) {
PushItemOnCursorWithoutQueue(inst);
}
else
PutItemInInventory(to_slot, *inst, true);
safe_delete(inst);
// discover item
if((RuleB(Character, EnableDiscoveredItems)) && !GetGM()) {
if(!IsDiscovered(item_id))
DiscoverItem(item_id);
}
return true;
}
// Drop item from inventory to ground (generally only dropped from SlotCursor)
void Client::DropItem(int16 slot_id)
{
if(GetInv().CheckNoDrop(slot_id) && RuleI(World, FVNoDropFlag) == 0 ||
RuleI(Character, MinStatusForNoDropExemptions) < Admin() && RuleI(World, FVNoDropFlag) == 2) {
database.SetHackerFlag(this->AccountName(), this->GetCleanName(), "Tried to drop an item on the ground that was nodrop!");
GetInv().DeleteItem(slot_id);
return;
}
// Take control of item in client inventory
EQ::ItemInstance *inst = m_inv.PopItem(slot_id);
if(inst) {
int i = parse->EventItem(EVENT_DROP_ITEM, this, inst, nullptr, "", 0);
if(i != 0) {
safe_delete(inst);
}
} else {
// Item doesn't exist in inventory!
Log(Logs::General, Logs::Error, "Item not found in slot %i", slot_id);
return;
}
// Save client inventory change to database
if (slot_id == EQ::invslot::slotCursor) {
auto s = m_inv.cursor_cbegin(), e = m_inv.cursor_cend();
database.SaveCursor(this, s, e);
} else {
database.SaveInventory(CharacterID(), nullptr, slot_id);
}
if(!inst)
return;
CreateGroundObject(inst, glm::vec4(GetX(), GetY(), GetZ(), 0), RuleI(Groundspawns, DecayTime));
safe_delete(inst);
}
//This differs from EntityList::CreateGroundObject by using the inst, so bag contents are
//preserved. EntityList creates a new instance using ID, so bag contents are lost. Also,
//EntityList can be used by NPCs for things like disarm.
void Client::CreateGroundObject(const EQ::ItemInstance* inst, glm::vec4 coords, uint32 decay_time, bool message)
{
if (zone && zone->GetGuildID() != GUILD_NONE)
{
auto broken_string = fmt::format("You cannot drop items in the ground. item {} (qty {} ).This item is eligible for reimbursement via petition at a cost of 100 platinum per item.", inst->GetID(), inst->GetCharges());
Message(CC_Red, broken_string.c_str());
if (RuleB(QueryServ, PlayerLogItemDesyncs))
{
QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID());
}
return;
}
if (!inst) {
// Item doesn't exist in inventory!
Message(CC_Red, "Error: Item not found");
return;
}
if (inst->GetItem()->NoDrop == 0)
{
auto broken_string = fmt::format("Item almost fell to the ground with nodrop item {} (qty {} ). This item is eligible for reimbursement via petition at a cost of 100 platinum per item.", inst->GetID(), inst->GetCharges());
Message(CC_Red, broken_string.c_str());
if (RuleB(QueryServ, PlayerLogItemDesyncs))
{
QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID());
}
return;
}
if (inst->IsType(EQ::item::ItemClassBag))
{
for (uint8 sub_slot = EQ::invbag::SLOT_BEGIN; (sub_slot <= EQ::invbag::SLOT_END); ++sub_slot)
{
const EQ::ItemInstance* bag_inst = inst->GetItem(sub_slot);
if (bag_inst)
{
if (bag_inst->GetItem()->NoDrop == 0)
{
auto broken_string = fmt::format("Bag almost fell to the ground with nodrop item {} (qty {} ). This item is eligible for reimbursement via petition at a cost of 100 platinum per item.", bag_inst->GetID(), bag_inst->GetCharges());
Message(CC_Red, broken_string.c_str());
if (RuleB(QueryServ, PlayerLogItemDesyncs))
{
QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID());
}
return;
}
}
}
}
if (RuleB(QueryServ, PlayerLogGroundSpawn) && inst)
{
QServ->QSGroundSpawn(CharacterID(), inst->GetID(), inst->GetCharges(), 0, GetZoneID(), true, message);
if (inst->IsType(EQ::item::ItemClassBag))
{
for (uint8 sub_slot = EQ::invbag::SLOT_BEGIN; (sub_slot <= EQ::invbag::SLOT_END); ++sub_slot)
{
const EQ::ItemInstance* bag_inst = inst->GetItem(sub_slot);
if (bag_inst)
{
QServ->QSGroundSpawn(CharacterID(), bag_inst->GetID(), bag_inst->GetCharges(), inst->GetID(), GetZoneID(), true, message);
}
}
}
}
if (message)
{
Message_StringID(CC_Yellow, DROPPED_ITEM);
}
// Package as zone object
Object *object = new Object(inst, coords.x, coords.y, coords.z, coords.w ,decay_time, true, this);
entity_list.AddObject(object, true);
object->Save();
}
// Returns a slot's item ID (returns INVALID_ID if not found)
int32 Client::GetItemIDAt(int16 slot_id) {
const EQ::ItemInstance* inst = m_inv[slot_id];
if (inst)
return inst->GetItem()->ID;
// None found
return INVALID_ID;
}
bool Client::FindOnCursor(uint32 item_id) {
if (m_inv.CursorSize() > 1) {
for (auto iter = m_inv.cursor_cbegin(); iter != m_inv.cursor_cend(); ++iter) {
// m_inv.cursor_begin() is referenced as SlotCursor
if (iter == m_inv.cursor_cbegin())
continue;
EQ::ItemInstance* inst = *iter;
if (inst != nullptr && inst->GetItem()->ID == item_id)
return true;
}
}
return false;
}
void Client::ClearMoney()
{
SendClientMoneyUpdate(0, -GetCopper());
SendClientMoneyUpdate(1, -GetSilver());
SendClientMoneyUpdate(2, -GetGold());
SendClientMoneyUpdate(3, -GetPlatinum());
m_pp.copper = 0;
m_pp.copper_bank = 0;
m_pp.copper_cursor = 0;
m_pp.silver = 0;
m_pp.silver_bank = 0;
m_pp.silver_cursor = 0;
m_pp.gold = 0;
m_pp.gold_bank = 0;
m_pp.gold_cursor = 0;
m_pp.platinum = 0;
m_pp.platinum_bank = 0;
m_pp.platinum_cursor = 0;
SaveCurrency();
}
void Client::ResetStartingSkills()
{
m_pp.level2 = 1;
m_pp.points = 5;
//Set all skills to 0.
for (int s = 0; s <= EQ::skills::HIGHEST_SKILL; s++)
{
m_pp.skills[s] = 0;
}
//Set all languages to 0.
for (int l = 0; l <= MAX_PP_LANGUAGE; l++)
{
m_pp.languages[l] = 0;
}
/* Set Racial and Class specific language and skills */
RemoveAllSkills();
SetRacialLanguages();
SetRaceStartingSkills();
SetClassLanguages();
}
void Client::ClearPlayerInfoAndGrantStartingItems(bool goto_death)
{
//Clear player's money.
ClearMoney();
//Remove spells.
UnscribeSpellAll(false);
UnmemSpellAll(false);
//Fade all buffs.
BuffFadeAll(false, true);
//Clear AAs.
RefundAA();
SetAAPoints(0);
m_pp.aapoints_spent = 0;
//Remove all factions.
database.RemoveAllFactions(this);
factionvalues.clear();
//Remove starting skills.
ResetStartingSkills();
//Remove all items from their trade if they're doing one.
Mob *Other = trade->With();
if (Other)
{
FinishTrade(this);
if (Other->IsClient())
Other->CastToClient()->FinishTrade(Other);
/* Reset both sides of the trade */
trade->Reset();
Other->trade->Reset();
}
//Delete all items from their inventory.
for (int16 i = EQ::invslot::SLOT_BEGIN; i <= EQ::invbag::BANK_BAGS_END;)
{
const EQ::ItemInstance* newinv = m_inv.GetItem(i);
if (i == EQ::invslot::GENERAL_END + 1) {
i = EQ::invbag::GENERAL_BAGS_BEGIN;
continue;
}
else if (i == EQ::invbag::CURSOR_BAG_END + 1) {
i = EQ::invslot::BANK_BEGIN;
continue;
}
else if (i == EQ::invslot::BANK_END + 1) {
i = EQ::invbag::BANK_BAGS_BEGIN;
continue;
}
if (newinv)
{
DeleteItemInInventory(i, 0, true, true);
}
i++;
}
//Grant starting items to the player again, since we just removed their inventory.
int return_zone_id = 0;
database.ResetStartingItems(this, m_pp.race, m_pp.class_, m_pp.deity, m_pp.binds[4].zoneId, m_pp.name, Admin(), return_zone_id );
//Set Level / EXP to 0.
SetLevel(1, true);
SetEXP(0, 0);
SetHardcoreDeathTimeStamp(0);
//Their state is likely all sorts of messed up. Commit immediately (Save) and then...
Save(1);
if (goto_death)
{
ForceGoToDeath();
}
}
// Remove item from inventory
void Client::DeleteItemInInventory(int16 slot_id, int8 quantity, bool client_update, bool update_db)
{
Log(Logs::Detail, Logs::Inventory, "DeleteItemInInventory(%i, %i, %s)", slot_id, quantity, (client_update) ? "true":"false");
// Added 'IsSlotValid(slot_id)' check to both segments of client packet processing.
// - cursor queue slots were slipping through and crashing client
if(!m_inv[slot_id]) {
// Make sure the client deletes anything in this slot to match the server.
if(client_update && IsValidSlot(slot_id)) {
auto outapp = new EQApplicationPacket(OP_MoveItem, sizeof(MoveItem_Struct));
MoveItem_Struct* delitem = (MoveItem_Struct*)outapp->pBuffer;
delitem->from_slot = slot_id;
delitem->to_slot = 0xFFFFFFFF;
delitem->number_in_stack = 0xFFFFFFFF;
QueuePacket(outapp);
safe_delete(outapp);
}
return;
}
// start QS code
if(RuleB(QueryServ, PlayerLogDeletes)) {
uint16 delete_count = 0;
if(m_inv[slot_id]) { delete_count += m_inv.GetItem(slot_id)->GetTotalItemCount(); }
auto pack = new ServerPacket(ServerOP_QSPlayerLogItemDeletes, sizeof(QSPlayerLogItemDelete_Struct) * delete_count);
QSPlayerLogItemDelete_Struct* QS = (QSPlayerLogItemDelete_Struct*)pack->pBuffer;
QS->char_id = character_id;
QS->stack_size = quantity;
QS->char_count = delete_count;
QS->char_slot = slot_id;
QS->item_id = m_inv[slot_id]->GetID();
QS->charges = m_inv[slot_id]->GetCharges();
if(m_inv[slot_id]->IsType(EQ::item::ItemClassBag)) {
for(uint8 bag_idx = EQ::invbag::SLOT_BEGIN; bag_idx < m_inv[slot_id]->GetItem()->BagSlots; bag_idx++) {
EQ::ItemInstance* bagitem = m_inv[slot_id]->GetItem(bag_idx);
if(bagitem) {
int16 bagslot_id = EQ::InventoryProfile::CalcSlotId(slot_id, bag_idx);
QS++;
QS->char_id = character_id;
QS->stack_size = quantity;
QS->char_count = delete_count;
QS->char_slot = bagslot_id;
QS->item_id = bagitem->GetID();
QS->charges = bagitem->GetCharges();
}
}
}
pack->Deflate();
if(worldserver.Connected()) { worldserver.SendPacket(pack); }
safe_delete(pack);
}
// end QS code
bool isDeleted = m_inv.DeleteItem(slot_id, quantity);
const EQ::ItemInstance* inst=nullptr;
if (slot_id == EQ::invslot::slotCursor) {
auto s = m_inv.cursor_cbegin(), e = m_inv.cursor_cend();
if (update_db)
database.SaveCursor(this, s, e);
}
else {
// Save change to database
inst = m_inv[slot_id];
if(update_db)
database.SaveInventory(character_id, inst, slot_id);
}
bool returnitem = false;
if(inst && !isDeleted)
{
if(inst->GetCharges() <= 0)
{
if (inst->IsStackable()
|| (!inst->IsStackable() && ((inst->GetItem()->MaxCharges == 0) || inst->IsExpendable())))
{
returnitem = false;
}
else
returnitem = true;
}
}
if(client_update && IsValidSlot(slot_id))
{
EQApplicationPacket* outapp;
if(inst) {
if(!isDeleted){
// Non stackable item with charges = Item with clicky spell effect ? Delete a charge.
outapp = new EQApplicationPacket(OP_DeleteCharge, sizeof(MoveItem_Struct));
MoveItem_Struct* delitem = (MoveItem_Struct*)outapp->pBuffer;
delitem->from_slot = slot_id;
delitem->to_slot = 0xFFFFFFFF;
delitem->number_in_stack = 0xFFFFFFFF;
QueuePacket(outapp);
safe_delete(outapp);
if(returnitem)
{
SendItemPacket(slot_id, inst, ItemPacketTrade);
}
return;
}
}
outapp = new EQApplicationPacket(OP_MoveItem, sizeof(MoveItem_Struct));
MoveItem_Struct* delitem = (MoveItem_Struct*)outapp->pBuffer;
delitem->from_slot = slot_id;
delitem->to_slot = 0xFFFFFFFF;
delitem->number_in_stack = 0xFFFFFFFF;
QueuePacket(outapp);
safe_delete(outapp);
}
}
// Puts an item into the person's inventory
// Any items already there will be removed from user's inventory
// (Also saves changes back to the database: this may be optimized in the future)
// client_update: Sends packet to client
bool Client::PushItemOnCursor(const EQ::ItemInstance& inst, bool client_update)
{
if(inst.GetItem()->GMFlag == -1 && this->Admin() < RuleI(GM, MinStatusToUseGMItem)) {
Message(CC_Red, "You are not a GM or do not have the status to this item. Please relog to avoid a desync.");
Log(Logs::Detail, Logs::Inventory, "Player %s on account %s attempted to create a GM-only item with a status of %i.\n(Item: %u, GMFlag: %u)\n",
GetName(), account_name, this->Admin(), inst.GetID(), inst.GetItem()->GMFlag);
return false;
}
Log(Logs::Detail, Logs::Inventory, "Putting item %s (%d) on the cursor", inst.GetItem()->Name, inst.GetItem()->ID);
m_inv.PushCursor(inst);
if (client_update) {
SendItemPacket(EQ::invslot::slotCursor, &inst, ItemPacketSummonItem);
}
auto s = m_inv.cursor_cbegin(), e = m_inv.cursor_cend();
return database.SaveCursor(this, s, e);
}
bool Client::PushItemOnCursorWithoutQueue(EQ::ItemInstance* inst, bool drop)
{
const EQ::ItemData* item = database.GetItem(inst->GetID());
if (item && CheckLoreConflict(item))
{
Message_StringID(CC_Default, DUP_LORE);
return false;
}
if (zone && zone->GetGuildID() != GUILD_NONE)
drop = false;
EQ::ItemInstance* cursoritem = m_inv.GetItem(EQ::invslot::slotCursor);
if (cursoritem == nullptr)
{
// If the cursor is empty, we have to send an item packet or we'll desync.
if (!PushItemOnCursor(*inst))
{
if (inst)
{
auto broken_string = fmt::format("Cursor queue full or item {} (qty {} ) is a duplicate. This item is eligible for reimbursement.", inst->GetID(), inst->GetCharges());
Log(Logs::General, Logs::Inventory, "Cursor queue has too many items or item %d (qty %d ) is a duplicate. It will be deleted.", inst->GetID(), inst->GetCharges());
if (RuleB(QueryServ, PlayerLogItemDesyncs)) { QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID()); }
Message(CC_Red, broken_string.c_str());
Log(Logs::General, Logs::Inventory, "Saving item %d to the cursor queue failed.", inst->GetID());
}
return false;
}
SendItemPacket(EQ::invslot::slotCursor, inst, ItemPacketSummonItem);
}
else
{
if (Admin() == 0)
{
// There are items on the cursor, so we don't want to send packet updates. But,
// we do want to enforce the client's rules about items on the cursor. No dupe items,
// and a max count of 10.
int16 inv_slot_id = GetInv().HasItem(inst->GetID(), 1, invWhereCursor);
if (inv_slot_id != INVALID_INDEX || m_inv.CursorSize() >= 10)
{
if (drop)
{
CreateGroundObject(inst, glm::vec4(GetX(), GetY(), GetZ(), 0), RuleI(Groundspawns, FullInvDecayTime), true);
return true;
}
else
{
auto broken_string = fmt::format("Cursor queue full or item {} (qty {} ) is a duplicate. This item is eligible for reimbursement.", inst->GetID(), inst->GetCharges());
Log(Logs::General, Logs::Inventory, "Cursor queue has too many items or item %d (qty %d ) is a duplicate. It will be deleted.", inst->GetID(), inst->GetCharges());
if (RuleB(QueryServ, PlayerLogItemDesyncs)) { QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID()); }
Message(CC_Red, broken_string.c_str());
return false;
}
}
}
inst->SetCursorQueue(false);
if (!PushItemOnCursor(*inst))
{
if (inst)
{
Log(Logs::General, Logs::Inventory, "Saving item %d to the cursor queue failed.", inst->GetID());
auto broken_string = fmt::format("Cursor queue full or item {} (qty {} ) is a duplicate. This item is eligible for reimbursement.", inst->GetID(), inst->GetCharges());
Log(Logs::General, Logs::Inventory, "Cursor queue has too many items or item %d (qty %d ) is a duplicate. It will be deleted.", inst->GetID(), inst->GetCharges());
if (RuleB(QueryServ, PlayerLogItemDesyncs)) { QServ->QSItemDesyncs(CharacterID(), broken_string.c_str(), GetZoneID()); }
Message(CC_Red, broken_string.c_str());
}
return false;
}
}
return true;
}
bool Client::PutItemInInventory(int16 slot_id, const EQ::ItemInstance& inst, bool client_update) {
Log(Logs::Detail, Logs::Inventory, "Putting item %s (%d) into slot %d", inst.GetItem()->Name, inst.GetItem()->ID, slot_id);
if(inst.GetItem()->GMFlag == -1 && this->Admin() < RuleI(GM, MinStatusToUseGMItem)) {
Message(CC_Red, "You are not a GM or do not have the status to this item. Please relog to avoid a desync.");
Log(Logs::Detail, Logs::Inventory, "Player %s on account %s attempted to create a GM-only item with a status of %i.\n(Item: %u, GMFlag: %u)\n",
GetName(), account_name, this->Admin(), inst.GetID(), inst.GetItem()->GMFlag);
return false;
}
if (slot_id == EQ::invslot::slotCursor)
return PushItemOnCursor(inst, client_update);
else
m_inv.PutItem(slot_id, inst);
if (client_update)
SendItemPacket(slot_id, &inst, ((slot_id == EQ::invslot::slotCursor) ? ItemPacketSummonItem : ItemPacketTrade));
if (slot_id == EQ::invslot::slotCursor) {
auto s = m_inv.cursor_cbegin(), e = m_inv.cursor_cend();
return database.SaveCursor(this, s, e);
}
else {
return database.SaveInventory(this->CharacterID(), &inst, slot_id);
}
CalcBonuses();
}
void Client::PutLootInInventory(int16 slot_id, const EQ::ItemInstance &inst, ServerLootItem_Struct** bag_item_data)
{
Log(Logs::Detail, Logs::Inventory, "Putting loot item %s (%d) into slot %d", inst.GetItem()->Name, inst.GetItem()->ID, slot_id);
m_inv.PutItem(slot_id, inst);
SendLootItemInPacket(&inst, slot_id);
if (slot_id == EQ::invslot::slotCursor) {
auto s = m_inv.cursor_cbegin(), e = m_inv.cursor_cend();
database.SaveCursor(this, s, e);
} else
database.SaveInventory(this->CharacterID(), &inst, slot_id);
if(bag_item_data) // bag contents
{
int16 interior_slot;
// solar: our bag went into slot_id, now let's pack the contents in
for(int i = EQ::invbag::SLOT_BEGIN; i <= EQ::invbag::SLOT_END; i++)
{
if(bag_item_data[i] == nullptr)
continue;
const EQ::ItemData* sub_item_item_data = database.GetItem(bag_item_data[i]->item_id);
if (sub_item_item_data == nullptr)
continue;
const EQ::ItemInstance *bagitem = database.CreateItem(bag_item_data[i]->item_id, bag_item_data[i]->charges);
interior_slot = EQ::InventoryProfile::CalcSlotId(slot_id, i);
Log(Logs::Detail, Logs::Inventory, "Putting bag loot item %s (%d) into slot %d (bag slot %d)", inst.GetItem()->Name, inst.GetItem()->ID, interior_slot, i);
PutLootInInventory(interior_slot, *bagitem);
safe_delete(bagitem);
}
}
CalcBonuses();
}
bool Client::TryStacking(EQ::ItemInstance* item, uint8 type, bool try_worn, bool try_cursor)
{
if(!item || !item->IsStackable())
return false;
int16 i;
uint32 item_id = item->GetItem()->ID;
// Do all we can get to arrows to go to quiver first.
if(item->GetItem()->ItemType == EQ::item::ItemTypeArrow)
{
for (i = EQ::invslot::GENERAL_BEGIN; i <= EQ::invslot::GENERAL_END; i++)
{
EQ::ItemInstance* bag = m_inv.GetItem(i);
if(bag)
{
if(bag->GetItem()->BagType == EQ::item::BagTypeQuiver)
{
int8 slots = bag->GetItem()->BagSlots;
uint16 emptyslot = 0;
for (uint8 j = EQ::invbag::SLOT_BEGIN; j < slots; j++)
{
uint16 slotid = EQ::InventoryProfile::CalcSlotId(i, j);
EQ::ItemInstance* tmp_inst = m_inv.GetItem(slotid);
if(!tmp_inst)
{
emptyslot = slotid;
}
// Partial stack found use this first
if(tmp_inst && tmp_inst->GetItem()->ID == item_id && tmp_inst->GetCharges() < tmp_inst->GetItem()->StackSize){
MoveItemCharges(*item, slotid, type);
CalcBonuses();
if(item->GetCharges()) // we didn't get them all
return AutoPutLootInInventory(*item, try_worn, try_cursor, 0);
return true;
}
}
// Use empty slot if no partial stacks
if(emptyslot != 0)
{
PutItemInInventory(emptyslot, *item, true);
return true;
}
}
}
}
}
for (i = EQ::invslot::GENERAL_BEGIN; i <= EQ::invslot::GENERAL_END; i++)
{
EQ::ItemInstance* tmp_inst = m_inv.GetItem(i);
if(tmp_inst && tmp_inst->GetItem()->ID == item_id && tmp_inst->GetCharges() < tmp_inst->GetItem()->StackSize){
MoveItemCharges(*item, i, type);
CalcBonuses();
if(item->GetCharges()) // we didn't get them all
return AutoPutLootInInventory(*item, try_worn, try_cursor, 0);
return true;
}
}
for (i = EQ::invslot::GENERAL_BEGIN; i <= EQ::invslot::GENERAL_END; i++)
{
for (uint8 j = EQ::invbag::SLOT_BEGIN; j <= EQ::invbag::SLOT_END; j++)
{
uint16 slotid = EQ::InventoryProfile::CalcSlotId(i, j);
EQ::ItemInstance* tmp_inst = m_inv.GetItem(slotid);
if(tmp_inst && tmp_inst->GetItem()->ID == item_id && tmp_inst->GetCharges() < tmp_inst->GetItem()->StackSize){
MoveItemCharges(*item, slotid, type);
CalcBonuses();
if(item->GetCharges()) // we didn't get them all
return AutoPutLootInInventory(*item, try_worn, try_cursor, 0);
return true;
}
}
}
return false;
}
int16 Client::GetStackSlot(EQ::ItemInstance* item, bool try_worn, bool try_cursor)
{
if(!item || !item->IsStackable())
return -1;
int16 i;
uint32 item_id = item->GetItem()->ID;
// Do all we can get to arrows to go to quiver first.
if(item->GetItem()->ItemType == EQ::item::ItemTypeArrow)
{
for (i = EQ::invslot::GENERAL_BEGIN; i <= EQ::invslot::GENERAL_END; i++)
{
EQ::ItemInstance* bag = m_inv.GetItem(i);
if(bag)
{
if(bag->GetItem()->BagType == EQ::item::BagTypeQuiver)
{
int8 slots = bag->GetItem()->BagSlots;
uint16 emptyslot = 0;
for (uint8 j = EQ::invbag::SLOT_BEGIN; j < slots; j++)
{
uint16 slotid = EQ::InventoryProfile::CalcSlotId(i, j);
EQ::ItemInstance* tmp_inst = m_inv.GetItem(slotid);
if(!tmp_inst)
{
emptyslot = slotid;
}
// Partial stack found use this first
if(tmp_inst && tmp_inst->GetItem()->ID == item_id && tmp_inst->GetCharges() < tmp_inst->GetItem()->StackSize){
return slotid;
}
}
// Use empty slot if no partial stacks
if(emptyslot != 0)
{
return emptyslot;
}
}
}
}
}