From 6fca66ad8adb461ce5c6c4ff7e4abd0393b5c597 Mon Sep 17 00:00:00 2001 From: qndel Date: Sat, 21 Aug 2021 16:50:59 +0200 Subject: [PATCH] debug commands: dropitem/dropunique --- Source/debug.cpp | 12 ++++++++++ Source/items.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ Source/items.h | 3 +++ 3 files changed, 72 insertions(+) diff --git a/Source/debug.cpp b/Source/debug.cpp index 63497e23..93b1003c 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -272,6 +272,16 @@ std::string DebugCmdRefillHealthMana(const std::string_view parameter) return "Ready for more."; } +std::string DebugCmdGenerateUniqueItem(const std::string_view parameter) +{ + return DebugSpawnItem(parameter.data(), true); +} + +std::string DebugCmdGenerateItem(const std::string_view parameter) +{ + return DebugSpawnItem(parameter.data(), false); +} + std::vector DebugCmdList = { { "help", "Prints help overview or help for a specific command.", "({command})", &DebugCmdHelp }, { "give gold", "Fills the inventory with gold.", "", &DebugCmdGiveGoldCheat }, @@ -286,6 +296,8 @@ std::vector DebugCmdList = { { "r_drawvision", "Togggles vision debug rendering.", "", &DebugCmdVision }, { "r_fullbright", "Toggles whether light shading is in effect.", "", &DebugCmdLighting }, { "refill", "Refills health and mana.", "", &DebugCmdRefillHealthMana }, + { "dropunique", "Attempts to generate unique item {name}.", "{name}", &DebugCmdGenerateUniqueItem }, + { "dropitem", "Attempts to generate item {name}.", "{name}", &DebugCmdGenerateItem }, }; } // namespace diff --git a/Source/items.cpp b/Source/items.cpp index cdcd0926..649998c8 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -4886,6 +4886,63 @@ void PutItemRecord(int nSeed, uint16_t wCI, int nIndex) } } +#ifdef _DEBUG +std::string DebugSpawnItem(std::string itemName, bool unique) +{ + if (ActiveItemCount >= MAXITEMS) + return "No space to generate the item!"; + + bool onlygood = true; + + int ii = AllocateItem(); + Point pos = Players[MyPlayerId].position.tile; + GetSuperItemSpace(pos, ii); + std::transform(itemName.begin(), itemName.end(), itemName.begin(), [](unsigned char c) { return std::tolower(c); }); + + std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); + MonsterStruct fake_m; + fake_m.MData = &MonsterData[0]; + fake_m._uniqtype = 0; + for (int i = 0;; i++) { + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + if (std::chrono::duration_cast(end - begin).count() > 3000) + return "Item not found in 3 seconds!"; + + if (i > 10000) + return "Item not found in 10000 tries!"; + + fake_m.mLevel = GenerateRnd(40) + 1; + int idx = RndItem(fake_m); + if (idx > 0) { + idx--; + onlygood = false; + } else + continue; + + int uper = (unique ? 15 : 1); + + Point bkp = Items[ii].position; + memset(&Items[ii], 0, sizeof(ItemStruct)); + Items[ii].position = bkp; + memset(UniqueItemFlags, 0, sizeof(UniqueItemFlags)); + SetupAllItems(ii, idx, AdvanceRndSeed(), fake_m.mLevel, uper, onlygood, false, false); + + std::string tmp(Items[ii]._iIName); + std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](unsigned char c) { return std::tolower(c); }); + if (tmp.find(itemName) != std::string::npos) + break; + + if (unique) + if (Items[ii]._iMagical != ITEM_QUALITY_UNIQUE) + continue; + } + + Items[ii]._iIdentified = true; + NetSendCmdDItem(false, ii); + return "Item generated successfully."; +} +#endif + void ItemStruct::SetNewAnimation(bool showAnimation) { int it = ItemCAnimTbl[_iCurs]; diff --git a/Source/items.h b/Source/items.h index d17c6338..03b1ce39 100644 --- a/Source/items.h +++ b/Source/items.h @@ -467,6 +467,9 @@ bool GetItemRecord(int nSeed, uint16_t wCI, int nIndex); void SetItemRecord(int nSeed, uint16_t wCI, int nIndex); void PutItemRecord(int nSeed, uint16_t wCI, int nIndex); +#ifdef _DEBUG +std::string DebugSpawnItem(std::string itemName, bool unique); +#endif /* data */ extern int MaxGold;