game: port AVA2 cube renewal (HEADER_CG/GC_CUBE_RENEWAL 216/221)

Cube_open hands mt>=20261026 clients the AVA2 cuberenewal window: the
recipes of the clicked NPC from the loaded cube.txt, sent as GC 221
CLEAR/DATES/LOADING/OPEN; CG 216 MAKE_ITEM/CLOSE does the craft.
Unlike AVA2 the reward scales with successes, count is bounded to one
stack (<=200), the free cell is checked before anything is consumed,
materials are taken from inventory + special storages (never equipped,
locked, traded or shop items) and gold is int64. Older clients keep the
classic "cube open" window; the DB-transactional cube stays disabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-24 14:06:25 +09:00
co-authored by Claude Opus 5.5
parent b5e06c9a43
commit 3058ad6002
6 changed files with 322 additions and 0 deletions
+6
View File
@@ -333,6 +333,11 @@ void Cube_open (LPCHARACTER ch)
Cube_clean_item(ch);
if (CubeRenewal_ClientSupported(ch))
{
CubeRenewal_Open(ch, npc);
return;
}
ch->SetCubeNpc(npc);
ch->ChatPacket(CHAT_TYPE_COMMAND, "cube open %d", npc->GetRaceNum());
}
@@ -996,3 +1001,4 @@ void Cube_request_material_info(LPCHARACTER ch, int requestStartIndex, int reque
// PORT: current loaded Cube catalogue and read-only batch preflight.
#include "port_cube_catalog.inc"
#include "port_cube_renewal.inc"
+6
View File
@@ -63,5 +63,11 @@ void Cube_print();
bool Cube_InformationInitialize();
// AVA2 cuberenewal (port_cube_renewal.inc): same cube.txt recipes, native window.
bool CubeRenewal_ClientSupported(LPCHARACTER ch);
void CubeRenewal_Open(LPCHARACTER ch, LPCHARACTER npc);
void CubeRenewal_Make(LPCHARACTER ch, DWORD index, DWORD count);
void CubeRenewal_Close(LPCHARACTER ch);
#endif /* _cube_h_ */
+10
View File
@@ -43,6 +43,7 @@
#include "DragonSoul.h"
#include "port_switchbot.h"
#include "ava2_native_packets.h"
#include "cube.h"
extern void SendShout(const char * szText, BYTE bEmpire);
extern int g_nPortalLimitTime;
@@ -3678,6 +3679,15 @@ int CInputMain::Analyze(LPDESC d, BYTE bHeader, const char * c_pData)
case HEADER_CG_AVA2_DROP:
PortNativeDrop(ch,*reinterpret_cast<const TPacketCGAva2Drop*>(c_pData));break;
case HEADER_CG_CUBE_RENEWAL:
{
const TPacketCGCubeRenewalSend* p = reinterpret_cast<const TPacketCGCubeRenewalSend*>(c_pData);
if (p->subheader == CUBE_RENEWAL_SUB_HEADER_MAKE_ITEM)
CubeRenewal_Make(ch, p->index_item, p->count_item);
else if (p->subheader == CUBE_RENEWAL_SUB_HEADER_CLOSE)
CubeRenewal_Close(ch);
break;
}
#ifdef ENABLE_SHOP_SEARCH_SYSTEM
case HEADER_CG_SHOP_SEARCH:
ShopSearch(ch, c_pData);
+43
View File
@@ -122,6 +122,7 @@ enum
HEADER_CG_STATE_CHECKER = 206,
HEADER_CG_SASH = 211, // AVA2 sash
HEADER_CG_AVA2_DROP = 215,
HEADER_CG_CUBE_RENEWAL = 216, // AVA2 cuberenewal (port_cube_renewal.inc)
HEADER_CG_ITEMS_PICKUP = 235, // PORT: AVA2
HEADER_CG_SWITCHBOT = 171, // AVA2 switchbot (port_switchbot.cpp)
@@ -326,6 +327,7 @@ enum
HEADER_GC_CHESTDROP = 66, // PORT: AVA2, DWORD dynamic length
HEADER_GC_SWITCHBOT = 171, // AVA2 switchbot (port_switchbot.cpp)
HEADER_GC_EXTENDED_POINTS = 218, // PORT: AVA2 64-bit gold
HEADER_GC_CUBE_RENEWAL = 221, // AVA2 cuberenewal (port_cube_renewal.inc)
HEADER_GC_PORT_QUIVER = 219, // PORT: negotiated, transient actor arrow subtype.
#if defined(__BL_67_ATTR__)
HEADER_GC_OPEN_67_ATTR = 242,
@@ -2681,6 +2683,47 @@ struct TPacketGCExtendedPointGold
int64_t gold;
};
static_assert(sizeof(TPacketGCExtendedPointGold) == 10, "TPacketGCExtendedPointGold size mismatch");
// AVA2 ENABLE_CUBE_RENEWAL_WORLDARD, byte-identical to the AVA2 client.
enum ECubeRenewalSubHeader
{
CUBE_RENEWAL_SUB_HEADER_OPEN_RECEIVE,
CUBE_RENEWAL_SUB_HEADER_CLEAR_DATES_RECEIVE,
CUBE_RENEWAL_SUB_HEADER_DATES_RECEIVE,
CUBE_RENEWAL_SUB_HEADER_DATES_LOADING,
CUBE_RENEWAL_SUB_HEADER_MAKE_ITEM,
CUBE_RENEWAL_SUB_HEADER_CLOSE,
};
struct TPacketCGCubeRenewalSend
{
uint8_t header;
uint8_t subheader;
uint32_t index_item;
uint32_t count_item;
};
struct TInfoDateCubeRenewal
{
uint32_t npc_vnum;
uint32_t index;
uint32_t vnum_reward;
int32_t count_reward;
bool item_reward_stackable;
uint32_t vnum_material_1; int32_t count_material_1;
uint32_t vnum_material_2; int32_t count_material_2;
uint32_t vnum_material_3; int32_t count_material_3;
uint32_t vnum_material_4; int32_t count_material_4;
uint32_t vnum_material_5; int32_t count_material_5;
int32_t gold;
int32_t percent;
char category[100];
};
struct TPacketGCCubeRenewalReceive
{
uint8_t header;
uint8_t subheader;
TInfoDateCubeRenewal date_cube_renewal;
};
static_assert(sizeof(TPacketCGCubeRenewalSend)==10,"AVA2 CG216 size");
static_assert(sizeof(TPacketGCCubeRenewalReceive)==167,"AVA2 GC221 size");
#pragma pack(pop)
#pragma pack()
+1
View File
@@ -232,6 +232,7 @@ CPacketInfoCG::CPacketInfoCG()
Set(HEADER_CG_SASH, sizeof(TPacketSash), "Sash", true);
Set(HEADER_CG_ITEMS_PICKUP,sizeof(TPacketCGItemsPickUp),"ItemsPickup",true);
Set(HEADER_CG_AVA2_DROP,sizeof(TPacketCGAva2Drop),"Ava2Drop",true);
Set(HEADER_CG_CUBE_RENEWAL,sizeof(TPacketCGCubeRenewalSend),"CubeRenewal",true);
Set(HEADER_CG_SWITCHBOT, sizeof(TPacketCGSwitchbot), "Switchbot", true);
#if defined(__BL_67_ATTR__)
Set(HEADER_CG_67_ATTR, sizeof(TPacketCG67Attr), "67Attr", true);
+256
View File
@@ -0,0 +1,256 @@
// PORT: AVA2 cuberenewal.cpp (ENABLE_CUBE_RENEWAL_WORLDARD), included by cube.cpp after
// port_cube_catalog.inc so it serves the SAME loaded cube.txt recipes as classic Cube.
// Packets are the AVA2 HEADER_CG/GC_CUBE_RENEWAL ones; only mt>=20261026 clients get them.
// Fixed against AVA2: the reward scales with successes (not attempts), count is bounded,
// the preview never creates items, every listed npc gets the recipe, and failures don't leak.
#include "packet.h"
#include "desc.h"
#include "shop.h"
static const int CUBE_RENEWAL_MIN_CLIENT = 20261026;
bool CubeRenewal_ClientSupported(LPCHARACTER ch)
{
const char* version = ch && ch->GetDesc() ? strstr(ch->GetDesc()->GetClientVersion(), "mt") : NULL;
return version && atoi(version + 2) >= CUBE_RENEWAL_MIN_CLIENT;
}
// The renewal index is the position in this list, which the client echoes back.
static void FN_cube_renewal_recipes(DWORD npcVnum, std::vector<const CUBE_DATA*>& out)
{
for (size_t i = 0; i < s_cube_proto.size(); ++i)
{
const CUBE_DATA* data = s_cube_proto[i];
if (!data || data->reward.size() != 1 || data->item.empty() || data->item.size() > 5)
continue;
if (std::find(data->npc_vnum.begin(), data->npc_vnum.end(), npcVnum) == data->npc_vnum.end())
continue;
if (!ITEM_MANAGER::instance().GetTable(data->reward[0].vnum))
continue;
out.push_back(data);
}
}
static bool FN_cube_renewal_stackable(DWORD vnum)
{
const TItemTable* table = ITEM_MANAGER::instance().GetTable(vnum);
return table && IS_SET(table->dwFlags, ITEM_FLAG_STACKABLE) && !IS_SET(table->dwAntiFlags, ITEM_ANTIFLAG_STACK);
}
static void FN_cube_renewal_send(LPCHARACTER ch, BYTE subheader, const CUBE_DATA* data = NULL, DWORD npcVnum = 0, DWORD index = 0)
{
if (!ch->GetDesc())
return;
TPacketGCCubeRenewalReceive pack;
memset(&pack, 0, sizeof(pack));
pack.header = HEADER_GC_CUBE_RENEWAL;
pack.subheader = subheader;
if (data)
{
TInfoDateCubeRenewal& info = pack.date_cube_renewal;
info.npc_vnum = npcVnum;
info.index = index;
info.vnum_reward = data->reward[0].vnum;
info.count_reward = data->reward[0].count;
info.item_reward_stackable = FN_cube_renewal_stackable(data->reward[0].vnum);
uint32_t* vnums[5] = { &info.vnum_material_1, &info.vnum_material_2, &info.vnum_material_3, &info.vnum_material_4, &info.vnum_material_5 };
int32_t* counts[5] = { &info.count_material_1, &info.count_material_2, &info.count_material_3, &info.count_material_4, &info.count_material_5 };
for (size_t i = 0; i < data->item.size() && i < 5; ++i)
{
*vnums[i] = data->item[i].vnum;
*counts[i] = data->item[i].count;
}
info.gold = static_cast<int32_t>(MIN(data->gold, static_cast<unsigned int>(INT_MAX)));
info.percent = data->percent;
strlcpy(info.category, data->portCategory.c_str(), sizeof(info.category));
}
ch->GetDesc()->Packet(&pack, sizeof(pack));
}
void CubeRenewal_Open(LPCHARACTER ch, LPCHARACTER npc)
{
const DWORD npcVnum = npc->GetRaceNum();
std::vector<const CUBE_DATA*> recipes;
FN_cube_renewal_recipes(npcVnum, recipes);
FN_cube_renewal_send(ch, CUBE_RENEWAL_SUB_HEADER_CLEAR_DATES_RECEIVE);
for (size_t i = 0; i < recipes.size(); ++i)
FN_cube_renewal_send(ch, CUBE_RENEWAL_SUB_HEADER_DATES_RECEIVE, recipes[i], npcVnum, i);
FN_cube_renewal_send(ch, CUBE_RENEWAL_SUB_HEADER_DATES_LOADING);
FN_cube_renewal_send(ch, CUBE_RENEWAL_SUB_HEADER_OPEN_RECEIVE);
ch->SetCubeNpc(npc);
}
void CubeRenewal_Close(LPCHARACTER ch)
{
if (!ch || !ch->IsCubeOpen())
return;
Cube_clean_item(ch);
ch->SetCubeNpc(NULL);
}
// Ordinary inventory plus the special storages, skipping anything in use.
static void FN_cube_renewal_items(LPCHARACTER ch, DWORD vnum, std::vector<LPITEM>& out)
{
LPSHOP myShop = ch->GetMyShop();
struct { BYTE window; WORD count; } windows[] = {
{ INVENTORY, INVENTORY_MAX_NUM },
#ifdef ENABLE_SPECIAL_STORAGE
{ UPGRADE_INVENTORY, SPECIAL_INVENTORY_MAX_NUM },
{ BOOK_INVENTORY, SPECIAL_INVENTORY_MAX_NUM },
{ STONE_INVENTORY, SPECIAL_INVENTORY_MAX_NUM },
{ CHANGE_INVENTORY, SPECIAL_INVENTORY_MAX_NUM },
{ COSTUME_INVENTORY, SPECIAL_INVENTORY_MAX_NUM },
#endif
};
for (size_t w = 0; w < sizeof(windows) / sizeof(windows[0]); ++w)
{
for (WORD cell = 0; cell < windows[w].count; ++cell)
{
LPITEM item = ch->GetItem(TItemPos(windows[w].window, cell));
if (!item || item->GetVnum() != vnum || item->GetOwner() != ch || item->GetCell() != cell)
continue;
if (item->IsEquipped() || item->isLocked() || item->IsExchanging())
continue;
if (myShop && myShop->IsSellingItem(item->GetID()))
continue;
out.push_back(item);
}
}
}
static int64_t FN_cube_renewal_count(const std::vector<LPITEM>& items)
{
int64_t count = 0;
for (size_t i = 0; i < items.size(); ++i)
count += items[i]->GetCount();
return count;
}
static void FN_cube_renewal_remove(std::vector<LPITEM>& items, int64_t count)
{
for (size_t i = 0; i < items.size() && count > 0; ++i)
{
const int64_t have = items[i]->GetCount();
const int64_t take = MIN(have, count);
count -= take;
items[i]->SetCount(static_cast<DWORD>(have - take));
}
}
static int FN_cube_renewal_cell(LPCHARACTER ch, LPITEM item, BYTE& window)
{
if (item->IsDragonSoul()) { window = DRAGON_SOUL_INVENTORY; return ch->GetEmptyDragonSoulInventory(item); }
#ifdef ENABLE_SPECIAL_STORAGE
if (item->IsUpgradeItem()) { window = UPGRADE_INVENTORY; return ch->GetEmptyUpgradeInventory(item); }
if (item->IsBook()) { window = BOOK_INVENTORY; return ch->GetEmptyBookInventory(item); }
if (item->IsStone()) { window = STONE_INVENTORY; return ch->GetEmptyStoneInventory(item); }
if (item->IsChange()) { window = CHANGE_INVENTORY; return ch->GetEmptyChangeInventory(item); }
if (item->IsCostume()) { window = COSTUME_INVENTORY; return ch->GetEmptyCostumeInventory(item); }
#endif
window = INVENTORY;
return ch->GetEmptyInventory(item->GetSize());
}
void CubeRenewal_Make(LPCHARACTER ch, DWORD index, DWORD count)
{
if (!ch || PortCubeActorHeld(ch))
return;
if (!ch->IsCubeOpen())
{
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("To create an item you have to have the refinement window open"));
return;
}
LPCHARACTER npc = ch->GetCubeNpc();
if (!npc || !PortCubeContext(ch, npc->GetRaceNum()))
return;
std::vector<const CUBE_DATA*> recipes;
FN_cube_renewal_recipes(npc->GetRaceNum(), recipes);
if (index >= recipes.size())
return;
const CUBE_DATA& data = *recipes[index];
const CUBE_VALUE& reward = data.reward[0];
const bool stackable = FN_cube_renewal_stackable(reward.vnum);
// One item stack per make: stackable rewards up to ITEM_MAX_COUNT, others one at a time.
if (count < 1 || reward.count < 1 || (!stackable && count != 1) ||
static_cast<int64_t>(reward.count) * count > ITEM_MAX_COUNT)
{
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't create that many at once."));
return;
}
std::vector<std::vector<LPITEM> > materials(data.item.size());
for (size_t i = 0; i < data.item.size(); ++i)
{
FN_cube_renewal_items(ch, data.item[i].vnum, materials[i]);
if (data.item[i].count <= 0 || FN_cube_renewal_count(materials[i]) < static_cast<int64_t>(data.item[i].count) * count)
{
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You don't have the necessary materials."));
return;
}
}
const int64_t gold = static_cast<int64_t>(data.gold) * count;
if (ch->GetGold() < gold)
{
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("He doesn't have the necessary amount of yang."));
return;
}
LPITEM pItem = ITEM_MANAGER::instance().CreateItem(reward.vnum, reward.count * count);
if (!pItem)
return;
BYTE window = INVENTORY;
if (FN_cube_renewal_cell(ch, pItem, window) < 0)
{
M2_DESTROY_ITEM(pItem);
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You do not have enough space in your inventory."));
return;
}
if (data.portCopy && !materials[0].empty() && materials[0][0]->GetType() == pItem->GetType())
{
pItem->SetSockets(materials[0][0]->GetSockets());
pItem->SetAttributes(materials[0][0]->GetAttributes());
}
int successes = 0;
for (DWORD i = 0; i < count; ++i)
if (number(1, 100) <= data.percent)
++successes;
for (size_t i = 0; i < data.item.size(); ++i)
FN_cube_renewal_remove(materials[i], static_cast<int64_t>(data.item[i].count) * count);
if (gold > 0)
ch->PointChange(POINT_GOLD, -gold, false);
if (successes <= 0)
{
LogManager::instance().CubeLog(ch->GetPlayerID(), ch->GetX(), ch->GetY(), reward.vnum, 0, 0, 0);
M2_DESTROY_ITEM(pItem);
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("It has failed."));
return;
}
pItem->SetCount(reward.count * successes);
const int cell = FN_cube_renewal_cell(ch, pItem, window);
if (cell < 0)
{
// Removing materials only frees space, so this cannot happen; never lose the reward.
ch->AutoGiveItem(pItem, true);
}
else
pItem->AddToCharacter(ch, TItemPos(window, cell));
LogManager::instance().ItemLog(ch, pItem, "CUBE_RENEWAL", pItem->GetName());
LogManager::instance().CubeLog(ch->GetPlayerID(), ch->GetX(), ch->GetY(), reward.vnum, pItem->GetID(), reward.count * successes, 1);
}