#include "UI/UILapineScrollBar.h" #include "Client/Mode/CGameMode.h" #include "Client/Mode/ModeMgr.h" #include "Client/Mode/ModeTooltip.h" #include "Client/Session.h" #include "Contents/Items/ItemDescription.h" #include "Contents/Items/ItemInfo.h" #include "Contents/MessageTable.h" #include "Network/Connection/Connection.h" #include "Network/Packets/PacketId.h" #include "Script/LuaScript.h" #include "UI/Buttons/UIBitmapButton.h" #include "UI/UIIngredientInfoWnd.h" #include "UI/Window/UIWindowMgr.h" #include "UI/Window/WindowManagerWindows.h" #include "Utility/Text/FormatString.h" #include #include #include #include #include #include #include #include typedef bool(__thiscall* AddMaterialSlotFn)(UILapineBoxWnd*, int, int); typedef void(__thiscall* ReadMaterialFn)(UILapineBoxWnd*, void*, int, int); typedef void(__thiscall* MaterialPositionFn)(UILapineBoxWnd*, void*, int); typedef void(__thiscall* ReturnMaterialFn)(UILapineBox_IngredientListWnd*, int); typedef bool(__thiscall* CheckMaterialFn)(UILapineBox_IngredientListWnd*, int); typedef void(__thiscall* FillItemIdsFn)(CSession*, std::vector*, int, unsigned char); typedef void(__thiscall* LayoutChildFn)(UILapineBox_IngredientListWnd*, UIWindow*, int, int); typedef int(__thiscall* ScrollWndSendMsgFn)(UILapineBox_IngredientListWnd*, UIWindow*, int, std::intptr_t, std::intptr_t, std::intptr_t, std::intptr_t); typedef void(__thiscall* BeginItemOnModeFn)(CGameMode*, int, ITEM_INFO*, int, int); typedef void(__thiscall* CloseNotifyFn)(UIWindowMgr*, int); namespace { static void SortChildWindows(UILapineBox_IngredientListWnd* window) { typedef void(__thiscall* SortFn)(UILapineBox_IngredientListWnd*); SortFn fn = reinterpret_cast((*reinterpret_cast(window))[53]); fn(window); } struct RandomCombineItemPacket { std::int16_t id; std::int16_t length; std::int32_t ingredientId; }; } // OFFSET: 0x006006D0 // FUNCTION: fn_04160 void UIIngredientInfoWnd::Click(int, int) { if (this->insufficient == 1) { reinterpret_cast(0x00602620)(static_cast(this->parent), this->item.unknown_0000[1]); return; } const int id = this->item.unknown_0000[1]; if (id == 0) { return; } UILapineBox_IngredientListWnd* list = static_cast(this->parent); UILapineBoxWnd* box = static_cast(list->parent); if (box->items.size() < static_cast(box->maxItems)) { if (list->SendMsg(list, 0x68, id, 0, 0, 0) >= 0) { reinterpret_cast(0x00601D20)(box, id, this->quantity); } } } // OFFSET: 0x00600830 // FUNCTION: fn_04162 void UIIngredientInfoWnd::Slot25(int x, int y) { if (this->insufficient != 1 && this->item.unknown_0000[1] > 0) { ItemDescriptions descriptions{g_itemDescriptions}; static_cast(g_modeMgr.GetGameMode())->drag.BeginItem(this->m_frameContext->windows, descriptions, 27, this->item, this->quantity, -1); } } // OFFSET: 0x00600940 // FUNCTION: fn_04164 void UIIngredientInfoWnd::Slot31(int, int) { if (this->insufficient != 1) { this->selected = 1; this->Invalidate(); if (this->parent) { this->parent->SendMsg(this, 6, 0, 0, 0, 0); } } } // OFFSET: 0x00600980 // FUNCTION: fn_04165 void UIIngredientInfoWnd::Slot28(int x, int y) { this->Slot27(x, y); if (this->tooltip.size() != 0) { int tipX = 0; int tipY = -20; this->ToScreen(tipX, tipY); (*reinterpret_cast(0x013495BC))->Show(this->tooltip.c_str(), tipX, tipY, -1, 0, 1); } } // OFFSET: 0x00600770 // FUNCTION: fn_04161 void UILapineBoxWnd::DropMaterial(int x, int y) { alignas(ITEM_INFO) unsigned char storage[sizeof(ITEM_INFO)]; ITEM_INFO& item = *reinterpret_cast(storage); reinterpret_cast(0x00602410)(this, &item, x, y); if (item.unknown_0000[1] != 0) { if (reinterpret_cast(0x00602030)(this->ingredientList, item.unknown_0000[1]) == 1) { this->SendMsg(this, 0x68, item.unknown_0000[1], 0, 0, 0); } } item.~ITEM_INFO(); } // OFFSET: 0x00600870 // FUNCTION: fn_04163 void UILapineBoxWnd::DragIngredient(int x, int y) { if (y < 0x11) { UIFrameWnd::Slot25(x, y); } alignas(ITEM_INFO) unsigned char storage[sizeof(ITEM_INFO)]; ITEM_INFO& item = *reinterpret_cast(storage); reinterpret_cast(0x00602410)(this, &item, x, y); if (item.unknown_0000[1] != 0) { reinterpret_cast(0x00CFE140)(static_cast(g_modeMgr.GetGameMode()), 0x1A, &item, item.quantity, -1); } item.~ITEM_INFO(); } // OFFSET: 0x006009F0 // FUNCTION: fn_04166 void UILapineBoxWnd::Slot28(int x, int y) { this->Slot27(x, y); alignas(ITEM_INFO) unsigned char storage[sizeof(ITEM_INFO)]; ITEM_INFO& item = *reinterpret_cast(storage); reinterpret_cast(0x00602410)(this, &item, x, y); if (item.unknown_0000[1] != 0) { int tipX = 0; int tipY = -20; const int count = static_cast(this->items.size()); for (int index = 0; index < count; ++index) { if (this->items[index].unknown_0000[1] == item.unknown_0000[1]) { int position[2] = {0, 0}; reinterpret_cast(0x006024F0)(this, position, index); tipX = position[0]; tipY = position[1] - 0x14; break; } } this->ToScreen(tipX, tipY); (*reinterpret_cast(0x013495BC))->Show(item.unknown_0044.c_str(), tipX, tipY, -1, 0, 1); } item.~ITEM_INFO(); } // OFFSET: 0x00600B60 // FUNCTION: fn_04167 void UILapineBoxWnd::Slot19() { if (this->waiting == 0) { const int count = static_cast(this->items.size()); if (this->maxItems == count) { if (this->makeButton->Disabled() == 1) { this->makeButton->SetDisabled(0); this->makeButton->Invalidate(); this->Invalidate(); } } else if (this->makeButton->Disabled() == 0) { this->makeButton->SetDisabled(1); this->makeButton->Invalidate(); this->Invalidate(); } } if (this->noticeTick + this->noticeDuration < timeGetTime()) { this->notice = ""; } } // OFFSET: 0x00600C00 // FUNCTION: fn_04168 void UIIngredientInfoWnd::Slot32(int, int) { if (this->item.unknown_0000[1] != 0) { UIWindow* window = this->m_frameContext->windows.EnsureWindow(12); window->SendMsg(this, 0x18, reinterpret_cast(&this->item), 0, 0, 0); } } // OFFSET: 0x006024F0 // FUNCTION: fn_04181 __declspec(noinline) void UILapineBoxWnd::MaterialPosition(int* out, int index) { if (index >= 0) { const unsigned int count = this->items.size(); if (static_cast(index) <= count) { int left = count > 5 ? 0x19 : 0x21; int top = count > 5 ? 0x98 : 0xA2; if (index >= 5) { left = 0x2A; top = 0xB7; } out[1] = top; out[0] = left + static_cast(index) % 5 * 0x22; return; } } out[0] = 0; out[1] = 0; } // OFFSET: 0x00602410 // FUNCTION: fn_04180 __declspec(noinline) void UILapineBoxWnd::ReadMaterial(ITEM_INFO* out, int x, int y) { const unsigned int count = this->items.size(); for (unsigned int index = 0; index < count; ++index) { int position[2]; this->MaterialPosition(position, index); if (x >= position[0] && x <= position[0] + 0x18 && y >= position[1] && y <= position[1] + 0x18) { new (out) ITEM_INFO(this->items[index]); return; } } new (out) ITEM_INFO(0); } // OFFSET: 0x00602AC0 // FUNCTION: fn_04184 void UILapineBoxWnd::SetWaiting(unsigned char waiting) { UIBitmapButton* button = this->makeButton; this->waiting = waiting; button->SetDisabled(waiting); button->Invalidate(); this->Invalidate(); } // OFFSET: 0x00600C40 // FUNCTION: fn_04169 void UILapineBoxWnd::NotifyWindowAt(int x, int y) { alignas(ITEM_INFO) unsigned char storage[sizeof(ITEM_INFO)]; ITEM_INFO& item = *reinterpret_cast(storage); this->ReadMaterial(&item, x, y); if (static_cast(item.unknown_0000[1]) > 0) { UIWindow* window = g_windowMgr.EnsureWindow(12); window->SendMsg(nullptr, 0x18, reinterpret_cast(&item), 0, 0, 0); } item.~ITEM_INFO(); } // OFFSET: 0x00600D00 // FUNCTION: fn_04170 void UIIngredientInfoWnd::Slot35(int argument) { if (this->parent) { this->parent->Slot35(argument); } } // OFFSET: 0x00600D20 // FUNCTION: fn_04171 void UILapineBox_IngredientListWnd::Slot35(int argument) { if (this->wheelScroll == 1) { if (argument < 0) { this->SendMsg(this->scrollBar, 7, 15, 0, 0, 0); } else { this->SendMsg(this->scrollBar, 7, -15, 0, 0, 0); } } } // OFFSET: 0x00600D60 // FUNCTION: fn_04172 int UILapineBoxWnd::SendMsg(UIWindow* sender, int message, std::intptr_t a, std::intptr_t b, std::intptr_t c, std::intptr_t d) { if (message != 6) { if (message == 0x68) { std::vector& items = this->items; const std::vector::iterator end = items.end(); std::vector::iterator found; for (found = items.begin(); found != end; ++found) { ITEM_INFO copy(*found); if (a == copy.unknown_0000[1]) { break; } } if (found != end) { std::vector::iterator it = found + 1; for (; it != end; ++it) { *(it - 1) = *it; } items.pop_back(); if (items.size() < static_cast(this->maxItems)) { this->moveButton->SetDisabled(0); this->moveButton->Invalidate(); } this->Invalidate(); } } else if (message == 0x6F) { this->noticeTick = timeGetTime(); const char* text = reinterpret_cast(a); this->notice.assign(text, std::strlen(text)); this->Invalidate(); } else { return UIFrameWnd::SendMsg(sender, message, a, b, c, d); } } else if (a == 0xC9) { reinterpret_cast(0x00A8CF20)(&g_windowMgr, 0x122); } else if (a == 0x1F7) { const int count = static_cast(this->items.size()); const int length = count * 4 + 8; RandomCombineItemPacket* packet = new (std::nothrow) RandomCombineItemPacket(); packet->id = ID_CZ_REQ_RANDOM_COMBINE_ITEM; packet->length = static_cast(length); packet->ingredientId = this->unknown_00C4; unsigned char* payload = static_cast(std::malloc(length)); std::memcpy(payload, packet, 8); for (int index = 0; index < count; ++index) { const ITEM_INFO& material = this->items[index]; const std::uint32_t value = (static_cast(material.quantity) << 16) | static_cast(material.unknown_0000[1]); std::memcpy(payload + 8 + index * 4, &value, sizeof(value)); } GetRagConnection().Send(length, payload); this->waiting = 1; this->makeButton->SetDisabled(1); this->makeButton->Invalidate(); this->Invalidate(); delete packet; std::free(payload); } else if (a == 0x1F9) { for (UIIngredientInfoWnd* entry : this->ingredientList->items) { if (entry->selected == 1) { if (entry != nullptr && entry->insufficient != 1) { if (this->ingredientList->SendMsg(this, 0x68, entry->item.unknown_0000[1], 0, 0, 0) >= 0) { reinterpret_cast(0x00601D20)(this, entry->item.unknown_0000[1], entry->quantity); } } break; } } } return 0; } // OFFSET: 0x006011E0 // FUNCTION: fn_04173 int UILapineBox_IngredientListWnd::SendMsg(UIWindow* sender, int message, std::intptr_t a, std::intptr_t b, std::intptr_t c, std::intptr_t d) { switch (message) { case 6: for (UIIngredientInfoWnd* entry : this->items) { if (sender != entry) { entry->selected = 0; entry->Invalidate(); } } this->Invalidate(); break; case 0x17: for (const ITEM_INFO& material : this->materials) { const int id = std::atoi(material.itemId.c_str()); std::vector ids = g_session.FindInventoryIndicesByItemId(id, 1); for (const int value : ids) { this->AddIngredient(static_cast(value)); } } for (UIIngredientInfoWnd* entry : this->items) { const ITEM_INFO owned = LookupClientItemByIdentity(g_session.m_inventoryItems, entry->item.unknown_0000[1]); if (owned.unknown_0000[1] == 0) { entry->Show(0); } } this->SendMsg(this, 0x57, 0, 0, 0, 0); break; case 0x57: { std::vector& items = this->items; std::size_t index = 0; int y = 7; while (index < items.size()) { UIIngredientInfoWnd* entry = items[index]; if (entry->geometry.visible == 0) { entry->Invalidate(); items.erase(items.begin() + static_cast(index)); } else { entry->Move(0, y); this->childOrigins[entry] = std::pair(0, y); SortChildWindows(this); entry->Invalidate(); ++index; y += 0x1E; } } SortChildWindows(this); this->Invalidate(); break; } case 0x68: { UIIngredientInfoWnd* selected = nullptr; for (UIIngredientInfoWnd* entry : this->items) { if (static_cast(a) == entry->item.unknown_0000[1]) { selected = entry; break; } } if (selected == nullptr) { return UIWindow::SendMsg(sender, message, a, b, c, d); } std::vector::iterator found = this->materials.end(); for (std::vector::iterator it = this->materials.begin(); it != this->materials.end(); ++it) { ITEM_INFO copy(*it); if (std::atoi(selected->item.itemId.c_str()) == std::atoi(copy.itemId.c_str())) { found = it; break; } } if (found != this->materials.end()) { if (selected->item.quantity < found->quantity) { const char* text = FormatString(LookupMessage(0xB52), found->quantity); this->parent->SendMsg(this, 0x6F, reinterpret_cast(text), 0, 0, 0); return -1; } if (selected->item.unknown_0060 < found->unknown_0060) { this->parent->SendMsg(this, 0x6F, reinterpret_cast(LookupMessage(0xB53)), 0, 0, 0); return -1; } selected->item.quantity -= found->quantity; if (selected->item.quantity < 1) { selected->Show(0); } else { selected->insufficient = 1; selected->Invalidate(); } this->SendMsg(this, 0x57, 0, 0, 0, 0); break; } break; } default: break; } return UIWindow::SendMsg(sender, message, a, b, c, d); } // OFFSET: 0x006016C0 // FUNCTION: fn_04174 void UILapineBoxWnd::SetIngredient(int id) { this->unknown_00C4 = id; ITEM_INFO item; item.SetItemId(id); char name[0x104]; std::memset(name, 0, sizeof(name)); int capacity = 0x104; ItemDescriptions descriptions{g_itemDescriptions}; descriptions.Name(item, name, capacity, 0); this->title.assign(name, std::strlen(name)); int optionCount = 0; g_luaScript->CallTable("tblLapineDdukddakBox", "set_resource_item_list", "d>d", id, &optionCount); int minRefine = 0; g_luaScript->CallTable("tblLapineDdukddakBox", "get_min_need_refine", "d>d", id, &minRefine); this->ingredientList->minRefine = minRefine; int refine = 0; g_luaScript->CallTable("tblLapineDdukddakBox", "get_max_need_refine", "d>d", id, &refine); this->ingredientList->maxRefine = refine; this->ingredientList->SendMsg(this, 0x17, 0, 0, 0, 0); g_luaScript->CallTable("tblLapineDdukddakBox", "get_need_item_count", "d>d", id, &this->maxItems); const char* source = nullptr; g_luaScript->CallTable("tblLapineDdukddakBox", "get_need_source_string", "d>s", id, &source); this->description.assign(source, std::strlen(source)); } // OFFSET: 0x00601BC0 // FUNCTION: fn_04176 void UILapineBoxWnd::AddMaterial(int id, int quantity) { std::vector& materials = this->ingredientList->materials; std::vector::iterator it; const std::vector::iterator end = materials.end(); for (it = materials.begin(); it != end; ++it) { ITEM_INFO copy(*it); if (std::atoi(copy.itemId.c_str()) == id) { break; } } if (it == end) { ITEM_INFO material; material.SetItemId(id); material.quantity = quantity; materials.push_back(material); } }