//
// SecretRewardsLayer::generateChestItems from 0xa69fa0 to 0xa6aaf4 (2900 bytes)
//
// 2 xrefs:
// > SecretRewardsLayer::createSecondaryLayer @ 0xa6adf8
// > ??> @ 0x38280
//
// 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
//
cocos2d::CCArray *SecretRewardsLayer::generateChestItems(SecretRewardsLayer *a1, int a2)
{
cocos2d::CCDirector *v3; // x0
cocos2d::CCDictionary *m_treasureRoomChests; // x0
unsigned int v5; // w1
int v6; // w22
int v7; // w0
int v8; // w1
unsigned __int64 v9; // x0
unsigned int v10; // w23
size_t v11; // x20
__int64 v12; // x28
void *v13; // x0
int v14; // w2
float x; // w20
__int64 v16; // x0
int *v17; // x28
unsigned __int64 v18; // x1
__int64 insert_hint_unique_pos; // x0
int *v20; // x1
int *v21; // x3
_BOOL8 v22; // x0
cocos2d::CCPoint v23; // x1
char *v24; // x0
int v25; // w27
unsigned int v26; // w21
cocos2d::CCMenu *v27; // x23
int *v28; // x20
int v29; // w28
__int64 v30; // x19
__int64 v31; // x19
__int64 v32; // x0
int *v33; // x1
int *v34; // x3
_BOOL8 v35; // x0
GameStatsManager *v36; // x0
GJChestSprite *v37; // x22
CCMenuItemSpriteExtra *v38; // x19
char *v39; // x0
const cocos2d::CCSize *(__fastcall *getContentSize)(const cocos2d::CCNode *__hidden); // x1
float width; // s15
__int64 v42; // x0
void (__fastcall *setPosition)(cocos2d::CCNode *__hidden, const cocos2d::CCPoint *); // x22
unsigned int *v44; // x1
int v45; // w2
unsigned int *v46; // x1
int v47; // w2
__int64 v48; // x19
cocos2d::CCLayer *v49; // x19
__int64 v50; // x19
void *v51; // x0
signed int v53; // w0
__int64 v54; // x19
cocos2d::CCArray *v55; // x1
unsigned int *v56; // x2
int v57; // w1
int *v58; // x0
char *v59; // x28
unsigned __int64 v60; // x20
cocos2d::CCArray *v61; // x3
unsigned __int64 v62; // x0
int *v63; // x0
__int64 v64; // [xsp+0h] [xbp+0h] BYREF
cocos2d::CCArray *v65; // [xsp+A8h] [xbp+A8h]
cocos2d::CCPoint *v66; // [xsp+B0h] [xbp+B0h]
int v67[3]; // [xsp+BCh] [xbp+BCh]
SecretRewardsLayer *v68; // [xsp+C8h] [xbp+C8h]
cocos2d::CCArray *v69; // [xsp+D0h] [xbp+D0h]
unsigned __int64 setContentSize; // [xsp+D8h] [xbp+D8h]
int *v71; // [xsp+E0h] [xbp+E0h]
cocos2d::CCArray *v72; // [xsp+E8h] [xbp+E8h]
unsigned __int64 v73; // [xsp+F0h] [xbp+F0h]
char *v74; // [xsp+F8h] [xbp+F8h]
int v75[4]; // [xsp+108h] [xbp+108h] BYREF
cocos2d::CCPoint v76; // [xsp+118h] [xbp+118h] BYREF
unsigned int v77; // [xsp+120h] [xbp+120h] BYREF
cocos2d::CCPoint v78; // [xsp+128h] [xbp+128h] BYREF
char *v79; // [xsp+130h] [xbp+130h] BYREF
__int64 v80; // [xsp+138h] [xbp+138h] BYREF
int v81; // [xsp+140h] [xbp+140h] BYREF
__int64 v82; // [xsp+148h] [xbp+148h]
int *v83; // [xsp+150h] [xbp+150h]
int *v84; // [xsp+158h] [xbp+158h]
__int64 v85; // [xsp+160h] [xbp+160h]
size_t v86; // [xsp+168h] [xbp+168h] BYREF
unsigned __int64 v87; // [xsp+170h] [xbp+170h]
char *v88; // [xsp+178h] [xbp+178h]
__int64 v89; // [xsp+180h] [xbp+180h] BYREF
cocos2d::SEL_MenuHandler v90; // 0:x2.16
v68 = a1;
v66 = &v76;
v67[0] = a2;
v3 = cocos2d::CCDirector::sharedDirector();
cocos2d::CCDirector::getWinSize(v3);
cocos2d::CCPoint::CCPoint(&v76, *(float *)&v75[2] * 0.5, (float)(*(float *)&v75[3] * 0.5) + 80.0);
m_treasureRoomChests = GameStatsManager::sharedState()->m_treasureRoomChests;
v5 = a2 - 3;
v81 = 0;
v82 = 0;
v85 = 0;
v83 = &v81;
v84 = &v81;
v72 = (cocos2d::CCArray *)m_treasureRoomChests;
if ( (unsigned int)(a2 - 3) > 5 )
{
v9 = 1;
v73 = 1;
v69 = nullptr;
*(_QWORD *)&v67[1] = 0;
}
else
{
v6 = dword_F131C0[v5];
v7 = dword_F131E0[v5];
v8 = v7 + 1;
v9 = (unsigned int)(v6 + v7);
v73 = __PAIR64__(v9, v8);
if ( v8 <= (int)v9 )
{
v10 = v8;
v65 = nullptr;
v74 = (char *)&v89;
v69 = nullptr;
*(_QWORD *)&v67[1] = 0;
v71 = v75;
while ( 1 )
{
v87 = 500;
v86 = 0;
v88 = v74;
v77 = v10;
fmt::BasicWriter::FormatInt,char>>(&v86, v10);
v11 = v86;
if ( v86 + 1 > v87 )
fmt::internal::Array::grow(&v86, v86 + 1);
v88[v11] = 0;
sub_D153B8((unsigned int)&v64 + 304, v88);
sub_D153B8((int)&v78, v79);
v12 = cocos2d::CCDictionary::objectForKey((cocos2d::CCDictionary *)v72, (const std::string *)&v78);
v13 = (void *)(*(_QWORD *)&v78 - 24LL);
if ( (__int64 *)(*(_QWORD *)&v78 - 24LL) == &qword_124B6B0 )
goto LABEL_7;
if ( &pthread_create )
{
v44 = (unsigned int *)(*(_QWORD *)&v78 - 8LL);
do
v45 = __ldaxr(v44);
while ( __stlxr(v45 - 1, v44) );
}
else
{
v45 = *(_DWORD *)(*(_QWORD *)&v78 - 8LL);
*(_DWORD *)(*(_QWORD *)&v78 - 8LL) = v45 - 1;
}
if ( v45 > 0 )
{
LABEL_7:
if ( !v12 )
goto LABEL_56;
}
else
{
operator delete(v13);
if ( !v12 )
{
LABEL_56:
if ( v65 != v69 )
{
if ( v69 )
LODWORD(v69->__vftable) = v10;
v23 = (cocos2d::CCPoint)v79;
v69 = (cocos2d::CCArray *)((char *)v69 + 4);
v24 = v79 - 24;
if ( v79 - 24 == (char *)&qword_124B6B0 )
goto LABEL_28;
LABEL_60:
if ( &pthread_create )
{
v46 = (unsigned int *)(*(_QWORD *)&v23 - 8LL);
do
v47 = __ldaxr(v46);
while ( __stlxr(v47 - 1, v46) );
}
else
{
v47 = *(_DWORD *)(*(_QWORD *)&v23 - 8LL);
*(_DWORD *)(*(_QWORD *)&v23 - 8LL) = v47 - 1;
}
if ( v47 <= 0 )
operator delete(v24);
goto LABEL_28;
}
v59 = (char *)v65 - *(_QWORD *)&v67[1];
v60 = ((__int64)v65 - *(_QWORD *)&v67[1]) >> 2;
if ( !v60 )
{
setContentSize = 4;
goto LABEL_102;
}
v62 = 2 * v60;
setContentSize = -4;
if ( v60 > 2 * v60 || v62 > 0x3FFFFFFFFFFFFFFFLL || (setContentSize = 8 * v60, v61 = nullptr, v62) )
LABEL_102:
v61 = (cocos2d::CCArray *)operator new(setContentSize);
if ( &v59[(_QWORD)v61] )
*(_DWORD *)&v59[(_QWORD)v61] = v10;
if ( v60 )
{
v61 = (cocos2d::CCArray *)memmove(v61, *(const void **)&v67[1], (size_t)v59);
v69 = (cocos2d::CCArray *)&v59[(_QWORD)v61 + 4];
LABEL_107:
v65 = v61;
operator delete(*(void **)&v67[1]);
v61 = v65;
}
else
{
v69 = (cocos2d::CCArray *)&v59[(_QWORD)v61 + 4];
if ( *(_QWORD *)&v67[1] )
goto LABEL_107;
}
*(_QWORD *)&v67[1] = v61;
v65 = (cocos2d::CCArray *)((char *)v61 + setContentSize);
goto LABEL_27;
}
}
v14 = *(_DWORD *)(v12 + 56);
if ( v14 > v6 || v14 < 0 )
{
*(_DWORD *)(v12 + 56) = 1;
LODWORD(v78.x) = 1;
if ( &v81 == (int *)std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::find(
(__int64)&v80,
(int *)&v78) )
goto LABEL_72;
}
else
{
v78.x = *(float *)(v12 + 56);
if ( &v81 == (int *)std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::find(
(__int64)&v80,
(int *)&v78) )
goto LABEL_72;
}
LODWORD(v78.x) = 1;
if ( v6 > 0 )
{
while ( &v81 != (int *)std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::find(
(__int64)&v80,
(int *)&v78) )
{
++LODWORD(v78.x);
if ( SLODWORD(v78.x) > v6 )
goto LABEL_72;
}
x = v78.x;
*(float *)(v12 + 56) = v78.x;
goto LABEL_16;
}
LABEL_72:
x = *(float *)(v12 + 56);
LABEL_16:
v16 = v82;
v17 = &v81;
if ( !v82 )
goto LABEL_21;
do
{
while ( SLODWORD(x) <= *(_DWORD *)(v16 + 32) )
{
v17 = (int *)v16;
v16 = *(_QWORD *)(v16 + 16);
if ( !v16 )
goto LABEL_19;
}
v16 = *(_QWORD *)(v16 + 24);
}
while ( v16 );
LABEL_19:
if ( v17 == &v81 || SLODWORD(x) < v17[8] )
{
LABEL_21:
setContentSize = (unsigned __int64)v17;
v17 = (int *)operator new(0x28u);
v18 = setContentSize;
*((float *)v17 + 8) = x;
v17[9] = 0;
insert_hint_unique_pos = std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::_M_get_insert_hint_unique_pos(
&v80,
v18,
v17 + 8);
v21 = (int *)insert_hint_unique_pos;
if ( v20 )
{
v22 = insert_hint_unique_pos || &v81 == v20 || v17[8] < v20[8];
sub_CFAD74(v22, v17, v20, &v81);
++v85;
}
else
{
v58 = v17;
v17 = v21;
operator delete(v58);
}
}
v17[9] = v10;
LABEL_27:
v23 = (cocos2d::CCPoint)v79;
v24 = v79 - 24;
if ( v79 - 24 != (char *)&qword_124B6B0 )
goto LABEL_60;
LABEL_28:
if ( v88 != v74 && v88 )
operator delete[](v88);
v9 = HIDWORD(v73);
if ( SHIDWORD(v73) < (int)++v10 )
goto LABEL_32;
}
}
v69 = nullptr;
*(_QWORD *)&v67[1] = 0;
}
LABEL_32:
v72 = (cocos2d::CCArray *)cocos2d::CCArray::create((cocos2d::CCArray *)v9);
v65 = (cocos2d::CCArray *)cocos2d::CCArray::create(v72);
v25 = 0;
if ( SHIDWORD(v73) >= (int)v73 )
{
while ( 1 )
{
v26 = vcvtms_s32_f32((float)v25 / 12.0);
if ( (unsigned int)cocos2d::CCArray::count(v72) <= v26 )
{
v49 = cocos2d::CCLayer::create();
v27 = cocos2d::CCMenu::create();
v49->addChild(v49, v27);
cocos2d::CCArray::addObject(v72, v49);
cocos2d::CCArray::addObject(v65, v27);
}
else
{
cocos2d::CCArray::objectAtIndex(v72, v26);
v27 = (cocos2d::CCMenu *)cocos2d::CCArray::objectAtIndex(v65, v26);
}
v28 = &v81;
v29 = v25 + 1;
v75[0] = v25 + 1;
if ( &v81 == (int *)std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::find(
(__int64)&v80,
v75) )
{
v48 = ((__int64)v69 - *(_QWORD *)&v67[1]) >> 2;
if ( v48 )
{
v53 = vcvtms_s32_f32((float)((float)rand() * 4.6566e-10) * (float)(unsigned __int64)v48);
if ( v48 - 1 > (unsigned __int64)v53 )
{
if ( v53 )
v54 = 4LL * v53;
else
v54 = 0;
}
else if ( v48 == 1 )
{
v54 = 0;
}
else
{
v54 = 4LL * ((int)v48 - 1);
}
LODWORD(v74) = *(_DWORD *)(*(_QWORD *)&v67[1] + v54);
v55 = (cocos2d::CCArray *)(*(_QWORD *)&v67[1] + v54 + 4);
if ( v55 != v69 && ((char *)v69 - (char *)v55) >> 2 )
memmove((void *)(*(_QWORD *)&v67[1] + v54), v55, (char *)v69 - (char *)v55);
v69 = (cocos2d::CCArray *)((char *)v69 - 4);
}
else
{
LODWORD(v74) = 0;
}
}
else
{
v30 = v82;
if ( !v82 )
goto LABEL_41;
do
{
while ( v75[0] <= *(_DWORD *)(v30 + 32) )
{
v28 = (int *)v30;
v30 = *(_QWORD *)(v30 + 16);
if ( !v30 )
goto LABEL_39;
}
v30 = *(_QWORD *)(v30 + 24);
}
while ( v30 );
LABEL_39:
if ( v28 == &v81 || v75[0] < v28[8] )
{
LABEL_41:
v31 = (__int64)v28;
v28 = (int *)operator new(0x28u);
*((_QWORD *)v28 + 4) = (unsigned int)v75[0];
v32 = std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::_M_get_insert_hint_unique_pos(
&v80,
v31,
v28 + 8);
v34 = (int *)v32;
if ( v33 )
{
v35 = v32 || &v81 == v33 || v28[8] < v33[8];
sub_CFAD74(v35, v28, v33, &v81);
++v85;
}
else
{
v63 = v28;
v28 = v34;
operator delete(v63);
}
}
LODWORD(v74) = v28[9];
}
v36 = GameStatsManager::sharedState();
LODWORD(v71) = GameStatsManager::isSecretChestUnlocked(v36, (int)v74);
v37 = GJChestSprite::create(v67[0]);
GJChestSprite::switchToState(v37, Closed, 0);
v37->setScale(v37, 0.55);
v90.ptr = (void (__cdecl *)(cocos2d::CCObject *))v68;
v90.adj = (size_t)SecretRewardsLayer::onSelectItem;
v38 = CCMenuItemSpriteExtra::create(v37, nullptr, v90);
sub_D153B8((int)&v79, "chestClick.ogg");
sub_D18EA8(&v38->m_selectSound, &v79);
v39 = v79 - 24;
if ( v79 - 24 != (char *)&qword_124B6B0 )
{
if ( &pthread_create )
{
v56 = (unsigned int *)(v79 - 8);
do
v57 = __ldaxr(v56);
while ( __stlxr(v57 - 1, v56) );
}
else
{
v57 = *((_DWORD *)v79 - 2);
*((_DWORD *)v79 - 2) = v57 - 1;
}
if ( v57 <= 0 )
operator delete(v39);
}
setContentSize = (unsigned __int64)v38->setContentSize;
cocos2d::CCSize::CCSize((cocos2d::CCSize *)&v79, 55.0, 44.0);
((void (__fastcall *)(CCMenuItemSpriteExtra *, char **))setContentSize)(v38, &v79);
getContentSize = v38->getContentSize;
setContentSize = (unsigned __int64)v37->setPosition;
width = getContentSize(v38)->width;
v42 = (__int64)v38->getContentSize(v38);
cocos2d::CCPoint::CCPoint((cocos2d::CCPoint *)&v79, width * 0.5, *(float *)(v42 + 4) * 0.5);
((void (__fastcall *)(GJChestSprite *, char **))setContentSize)(v37, &v79);
v27->addChild(v27, v38);
v38->setTag(v38, (int)v74);
v37->setTag(v37, v75[0]);
setPosition = v38->setPosition;
cocos2d::CCNode::convertToNodeSpace(v27, v66);
cocos2d::CCPoint::CCPoint(
&v78,
(float)((float)((int)(v25 - 12 * v26) % 4) - 1.5) * 90.0,
(float)-vcvtms_s32_f32((float)((int)(v25 - 12 * v26) / 4)) * 80.0);
cocos2d::CCPoint::operator+(&v79, &v77, &v78);
setPosition(v38, (const cocos2d::CCPoint *)&v79);
if ( (_DWORD)v71 )
{
SecretRewardsLayer::switchToOpenedState(v68, v38);
if ( SHIDWORD(v73) < v29 + (int)v73 )
break;
}
else if ( SHIDWORD(v73) < v29 + (int)v73 )
{
break;
}
++v25;
}
}
v50 = v82;
while ( v50 )
{
std::_Rb_tree,std::_Select1st>,std::less,std::allocator>>::_M_erase(
(__int64)&v80,
*(_QWORD **)(v50 + 24));
v51 = (void *)v50;
v50 = *(_QWORD *)(v50 + 16);
operator delete(v51);
}
if ( *(_QWORD *)&v67[1] )
operator delete(*(void **)&v67[1]);
return v72;
}