//
// SecretRewardsLayer::onGoldChest from 0xa64394 to 0xa6472c (920 bytes)
//
// 3 xrefs:
// > ??> @ 0x44238
// > SecretRewardsLayer::init @ 0xa630f0
// > ??> @ 0x122a2c0
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
void SecretRewardsLayer::onGoldChest(SecretRewardsLayer *a1, cocos2d::CCObject *a2)
{
GameStatsManager *v3; // x0
int Stat; // w22
GameStatsManager *v5; // x0
int NextGoldChestID; // w0
_BOOL4 v7; // w21
GameStatsManager *v8; // x0
GJRewardItem *v9; // x0
GJRewardItem *v10; // x21
int v11; // w20
int v12; // w22
DialogObject *v13; // x0
DialogLayer *v14; // x20
void *v15; // x0
void *v16; // x0
void *v17; // x0
unsigned int *v18; // x20
int v19; // w1
RewardUnlockLayer *v20; // x22
cocos2d::CCLabelBMFont *m_goldKeysLabel; // x21
void (__fastcall *setString)(cocos2d::CCLabelBMFont *__hidden, const unsigned __int8 *); // x22
GameStatsManager *v23; // x0
unsigned int v24; // w0
cocos2d::CCString *v25; // x0
const char *CString; // x0
unsigned int *v27; // x1
int v28; // w2
unsigned int *v29; // x1
int v30; // w2
int v31; // [xsp+48h] [xbp+48h]
__int64 v32; // [xsp+60h] [xbp+60h] BYREF
__int64 v33; // [xsp+68h] [xbp+68h] BYREF
__int64 v34; // [xsp+70h] [xbp+70h] BYREF
v3 = GameStatsManager::sharedState();
Stat = GameStatsManager::getStat(v3, "43");
v5 = GameStatsManager::sharedState();
NextGoldChestID = GameStatsManager::getNextGoldChestID(v5);
v7 = NextGoldChestID == 0;
if ( NextGoldChestID && Stat > 0 )
{
v31 = NextGoldChestID;
v8 = GameStatsManager::sharedState();
v9 = GameStatsManager::unlockGoldChest(v8, v31);
v10 = v9;
if ( v9 )
{
v20 = RewardUnlockLayer::create(v9->m_rewardType, nullptr);
a1->addChild_0(a1, v20, 100);
RewardUnlockLayer::showCollectReward(v20, v10);
m_goldKeysLabel = a1->m_goldKeysLabel;
setString = m_goldKeysLabel->setString;
v23 = GameStatsManager::sharedState();
v24 = GameStatsManager::getStat(v23, "43");
v25 = (cocos2d::CCString *)cocos2d::CCString::createWithFormat((cocos2d::CCString *)"%i", (const char *)v24);
CString = cocos2d::CCString::getCString(v25);
setString(m_goldKeysLabel, (const unsigned __int8 *)CString);
SecretRewardsLayer::updateUnlockedGoldLabel(a1);
SecretRewardsLayer::fadeInOutMusic(a1);
}
}
else
{
sub_D153B8((int)&v32, &byte_F288A0);
v11 = 1 - Stat;
if ( v7 )
sub_D1798C(&v32, "All out of Gold Chests right now. Come back later.", 58);
else
sub_D1798C(&v32, "You need a Gold Key to unlock this chest.", 57);
v12 = 18;
if ( v11 <= 50 )
{
v12 = 17;
if ( v11 <= 25 )
{
if ( v11 >= 11 )
v12 = 4;
else
v12 = 2;
}
}
sub_D153B8((int)&v33, "The Keymaster");
sub_D18D28(&v34);
v13 = (DialogObject *)DialogObject::create(1.0, (__int64)&v33, (__int64)&v34, v12, 0, (int)&unk_FFFFFF);
v14 = DialogLayer::create(v13, (int)&dword_0 + 2);
v15 = (void *)(v34 - 24);
if ( (__int64 *)(v34 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v29 = (unsigned int *)(v34 - 8);
do
v30 = __ldaxr(v29);
while ( __stlxr(v30 - 1, v29) );
}
else
{
v30 = *(_DWORD *)(v34 - 8);
*(_DWORD *)(v34 - 8) = v30 - 1;
}
if ( v30 <= 0 )
operator delete(v15);
}
v16 = (void *)(v33 - 24);
if ( (__int64 *)(v33 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v27 = (unsigned int *)(v33 - 8);
do
v28 = __ldaxr(v27);
while ( __stlxr(v28 - 1, v27) );
}
else
{
v28 = *(_DWORD *)(v33 - 8);
*(_DWORD *)(v33 - 8) = v28 - 1;
}
if ( v28 <= 0 )
operator delete(v16);
}
DialogLayer::addToMainScene(v14);
DialogLayer::updateChatPlacement(v14, Center);
DialogLayer::animateInRandomSide(v14);
v17 = (void *)(v32 - 24);
if ( (__int64 *)(v32 - 24) != &qword_124B6B0 )
{
if ( &pthread_create )
{
v18 = (unsigned int *)(v32 - 8);
do
v19 = __ldaxr(v18);
while ( __stlxr(v19 - 1, v18) );
}
else
{
v19 = *(_DWORD *)(v32 - 8);
*(_DWORD *)(v32 - 8) = v19 - 1;
}
if ( v19 <= 0 )
operator delete(v17);
}
}
}