//
// GJBaseGameLayer::optimizeMoveGroups from 0x100117a30 to 0x100118371 (2369 bytes)
//
// 2 xrefs:
// > LevelEditorLayer::onResumePlaytest @ 0x1000f2d38
// > GJBaseGameLayer::updateSpecialGroupData @ 0x1001173d1
//
// 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 __fastcall GJBaseGameLayer::optimizeMoveGroups(GJBaseGameLayer *a1)
{
cocos2d::CCArray *m_objects; // rax
cocos2d::ccArray *data; // rax
cocos2d::CCObject **arr; // rbx
__int64 **v4; // rcx
std::__compressed_pair> *p_pair3; // r15
cocos2d::CCObject *v6; // r13
int m_uReference; // eax
int m_eObjType; // r12d
std::__tree_end_node *>::pointer v9; // rax
std::__compressed_pair *>> *v10; // r15
std::__compressed_pair *>> *v11; // r14
int v12; // eax
int m_nLuaID; // r12d
std::__tree_end_node *>::pointer left; // rax
std::__compressed_pair *>> *p_pair1; // r13
std::__compressed_pair *>> *v16; // r15
int v17; // eax
_DWORD *v18; // rax
std::__tree_end_node *> *v19; // rcx
int m_uID; // r12d
std::__tree_end_node *>::pointer v21; // rax
std::__compressed_pair *>> *v22; // r15
std::__compressed_pair *>> *v23; // r14
int v24; // eax
_DWORD *v25; // rax
int v26; // r12d
std::__tree_end_node *>::pointer v27; // rax
std::__compressed_pair *>> *v28; // r15
std::__compressed_pair *>> *v29; // r14
std::__tree_end_node *>::pointer v30; // rcx
int v31; // ecx
_DWORD *v32; // rax
int v33; // eax
std::__tree_end_node *>::pointer v34; // rax
int v35; // eax
int v36; // r12d
std::__tree_end_node *>::pointer v37; // rax
std::__compressed_pair *>> *v38; // r15
std::__compressed_pair *>> *v39; // r14
std::__tree_end_node *>::pointer v40; // rcx
int v41; // ecx
_DWORD *v42; // rax
int v43; // eax
int v44; // r12d
std::__tree_end_node *>::pointer v45; // rax
std::__compressed_pair *>> *v46; // r15
std::__compressed_pair *>> *v47; // r14
int v48; // eax
_DWORD *v49; // rax
int v50; // r12d
std::__tree_end_node *>::pointer v51; // rax
std::__compressed_pair *>> *v52; // r15
std::__compressed_pair *>> *v53; // r14
int v54; // eax
std::__tree::__iter_pointer begin_node; // r13
intptr_t left_high; // r14
cocos2d::CCDictionary *m_parentGroupsDict; // rdi
intptr_t v58; // rsi
GameObject *v59; // rax
std::set *v60; // rcx
std::set *v61; // rax
bool v62; // zf
cocos2d::CCArray *v63; // rbx
std::set *v64; // rsi
cocos2d::CCDictionary *m_groupDict; // rdi
std::__tree::__iter_pointer v66; // rdx
intptr_t v67; // rbx
cocos2d::CCArray *v68; // r14
cocos2d::CCObject *v69; // rax
GJBaseGameLayer *v70; // r15
cocos2d::ccArray *v71; // rax
__int64 num; // rcx
GameObject **v73; // rax
GameObject *v74; // r14
unsigned int v75; // r15d
cocos2d::CCDictionary *m_staticGroupDict; // rdi
cocos2d::CCArray *v77; // rsi
int GroupID; // eax
intptr_t v79; // r13
cocos2d::CCArray *v80; // r12
cocos2d::CCArray *v81; // rbx
GJBaseGameLayer *v82; // rcx
std::set *v83; // rdx
std::set *v84; // rcx
std::set *value; // rax
unsigned __int64 v86; // [rsp+8h] [rbp-78h]
std::__tree::__iter_pointer v87; // [rsp+10h] [rbp-70h]
unsigned __int64 v88; // [rsp+18h] [rbp-68h]
GameObject **v89; // [rsp+18h] [rbp-68h]
std::set v90; // [rsp+20h] [rbp-60h] BYREF
GJBaseGameLayer *v91; // [rsp+38h] [rbp-48h]
std::set v92[2]; // [rsp+40h] [rbp-40h] BYREF
*(_OWORD *)&v92[0].__tree_.__pair1_.__value_.__left_ = 0;
v92[0].__tree_.__begin_node_ = &v92[0].__tree_.__pair1_.__value_;
*(_OWORD *)&v90.__tree_.__pair1_.__value_.__left_ = 0;
v90.__tree_.__begin_node_ = &v90.__tree_.__pair1_.__value_;
v91 = a1;
m_objects = a1->m_objects;
if ( m_objects )
{
data = m_objects->data;
if ( data->num )
{
arr = data->arr;
v88 = (unsigned __int64)&arr[data->num - 1];
do
{
v6 = *arr;
if ( !*arr )
break;
m_uReference = v6[18].m_uReference;
if ( m_uReference > 3005 )
{
if ( (unsigned int)(m_uReference - 3006) >= 3 )
{
if ( m_uReference != 3661 && m_uReference != 3016 )
goto LABEL_6;
LABEL_27:
m_nLuaID = v6[26].m_nLuaID;
left = v92[0].__tree_.__pair1_.__value_.__left_;
p_pair1 = &v92[0].__tree_.__pair1_;
LABEL_29:
v16 = p_pair1;
if ( left )
{
do
{
p_pair1 = (std::__compressed_pair *>> *)left;
v17 = *((_DWORD *)left + 7);
if ( m_nLuaID < v17 )
{
left = p_pair1->__value_.__left_;
goto LABEL_29;
}
if ( m_nLuaID <= v17 )
goto LABEL_6;
left = p_pair1[1].__value_.__left_;
}
while ( left );
v16 = p_pair1 + 1;
}
v18 = operator new(0x20u);
v18[7] = m_nLuaID;
*(_OWORD *)v18 = 0;
*((_QWORD *)v18 + 2) = p_pair1;
v16->__value_.__left_ = (std::__tree_end_node *>::pointer)v18;
v19 = (std::__tree_end_node *> *)v92[0].__tree_.__begin_node_->__left_;
if ( !v92[0].__tree_.__begin_node_->__left_ )
goto LABEL_4;
LABEL_93:
v92[0].__tree_.__begin_node_ = v19;
goto LABEL_4;
}
m_eObjType = v6[27].m_eObjType;
if ( m_eObjType )
{
if ( m_eObjType <= 0 )
goto LABEL_6;
}
else
{
m_eObjType = v6[26].m_nLuaID;
if ( m_eObjType <= 0 )
goto LABEL_6;
}
v34 = v92[0].__tree_.__pair1_.__value_.__left_;
v10 = &v92[0].__tree_.__pair1_;
LABEL_69:
v11 = v10;
if ( v34 )
{
do
{
v10 = (std::__compressed_pair *>> *)v34;
v35 = *((_DWORD *)v34 + 7);
if ( m_eObjType < v35 )
{
v34 = v10->__value_.__left_;
goto LABEL_69;
}
if ( m_eObjType <= v35 )
goto LABEL_6;
v34 = v10[1].__value_.__left_;
}
while ( v34 );
v11 = v10 + 1;
}
}
else if ( m_uReference <= 1346 )
{
if ( m_uReference == 901 )
{
v36 = v6[26].m_nLuaID;
v37 = v92[0].__tree_.__pair1_.__value_.__left_;
v38 = &v92[0].__tree_.__pair1_;
v39 = &v92[0].__tree_.__pair1_;
if ( v92[0].__tree_.__pair1_.__value_.__left_ )
{
v40 = v92[0].__tree_.__pair1_.__value_.__left_;
while ( 1 )
{
while ( 1 )
{
v38 = (std::__compressed_pair *>> *)v40;
v41 = *((_DWORD *)v40 + 7);
if ( v36 >= v41 )
break;
v40 = v38->__value_.__left_;
v39 = v38;
if ( !v38->__value_.__left_ )
goto LABEL_82;
}
if ( v36 <= v41 )
break;
v40 = v38[1].__value_.__left_;
if ( !v40 )
{
v39 = v38 + 1;
goto LABEL_82;
}
}
}
else
{
LABEL_82:
v42 = operator new(0x20u);
v42[7] = v36;
*(_OWORD *)v42 = 0;
*((_QWORD *)v42 + 2) = v38;
v39->__value_.__left_ = (std::__tree_end_node *>::pointer)v42;
if ( v92[0].__tree_.__begin_node_->__left_ )
v92[0].__tree_.__begin_node_ = (std::__tree::__iter_pointer)v92[0].__tree_.__begin_node_->__left_;
sub_100054DD0((__int64 *)v92[0].__tree_.__pair1_.__value_.__left_, (__int64 *)v42);
++v92[0].__tree_.__pair3_.__value_;
v37 = v92[0].__tree_.__pair1_.__value_.__left_;
}
m_eObjType = v6[27].m_nTag;
v10 = &v92[0].__tree_.__pair1_;
LABEL_87:
v11 = v10;
if ( v37 )
{
do
{
v10 = (std::__compressed_pair *>> *)v37;
v43 = *((_DWORD *)v37 + 7);
if ( m_eObjType < v43 )
{
v37 = v10->__value_.__left_;
goto LABEL_87;
}
if ( m_eObjType <= v43 )
goto LABEL_6;
v37 = v10[1].__value_.__left_;
}
while ( v37 );
v11 = v10 + 1;
}
}
else
{
if ( m_uReference != 1346 )
goto LABEL_6;
m_uID = v6[26].m_uID;
v21 = v90.__tree_.__pair1_.__value_.__left_;
v22 = &v90.__tree_.__pair1_;
LABEL_40:
v23 = v22;
if ( v21 )
{
do
{
v22 = (std::__compressed_pair *>> *)v21;
v24 = *((_DWORD *)v21 + 7);
if ( m_uID < v24 )
{
v21 = v22->__value_.__left_;
goto LABEL_40;
}
if ( m_uID <= v24 )
goto LABEL_48;
v21 = v22[1].__value_.__left_;
}
while ( v21 );
v23 = v22 + 1;
}
v25 = operator new(0x20u);
v25[7] = m_uID;
*(_OWORD *)v25 = 0;
*((_QWORD *)v25 + 2) = v22;
v23->__value_.__left_ = (std::__tree_end_node *>::pointer)v25;
if ( v90.__tree_.__begin_node_->__left_ )
v90.__tree_.__begin_node_ = (std::__tree::__iter_pointer)v90.__tree_.__begin_node_->__left_;
sub_100054DD0((__int64 *)v90.__tree_.__pair1_.__value_.__left_, (__int64 *)v25);
++v90.__tree_.__pair3_.__value_;
LABEL_48:
v26 = v6[26].m_nLuaID;
v27 = v92[0].__tree_.__pair1_.__value_.__left_;
v28 = &v92[0].__tree_.__pair1_;
v29 = &v92[0].__tree_.__pair1_;
if ( v92[0].__tree_.__pair1_.__value_.__left_ )
{
v30 = v92[0].__tree_.__pair1_.__value_.__left_;
while ( 1 )
{
while ( 1 )
{
v28 = (std::__compressed_pair *>> *)v30;
v31 = *((_DWORD *)v30 + 7);
if ( v26 >= v31 )
break;
v30 = v28->__value_.__left_;
v29 = v28;
if ( !v28->__value_.__left_ )
goto LABEL_55;
}
if ( v26 <= v31 )
break;
v30 = v28[1].__value_.__left_;
if ( !v30 )
{
v29 = v28 + 1;
goto LABEL_55;
}
}
}
else
{
LABEL_55:
v32 = operator new(0x20u);
v32[7] = v26;
*(_OWORD *)v32 = 0;
*((_QWORD *)v32 + 2) = v28;
v29->__value_.__left_ = (std::__tree_end_node *>::pointer)v32;
if ( v92[0].__tree_.__begin_node_->__left_ )
v92[0].__tree_.__begin_node_ = (std::__tree::__iter_pointer)v92[0].__tree_.__begin_node_->__left_;
sub_100054DD0((__int64 *)v92[0].__tree_.__pair1_.__value_.__left_, (__int64 *)v32);
++v92[0].__tree_.__pair3_.__value_;
v27 = v92[0].__tree_.__pair1_.__value_.__left_;
}
m_eObjType = v6[27].m_nZOrder;
v10 = &v92[0].__tree_.__pair1_;
LABEL_60:
v11 = v10;
if ( v27 )
{
do
{
v10 = (std::__compressed_pair *>> *)v27;
v33 = *((_DWORD *)v27 + 7);
if ( m_eObjType < v33 )
{
v27 = v10->__value_.__left_;
goto LABEL_60;
}
if ( m_eObjType <= v33 )
goto LABEL_6;
v27 = v10[1].__value_.__left_;
}
while ( v27 );
v11 = v10 + 1;
}
}
}
else
{
if ( m_uReference == 1347 )
goto LABEL_27;
if ( m_uReference == 2067 )
{
v44 = v6[26].m_nLuaID;
v45 = v92[0].__tree_.__pair1_.__value_.__left_;
v46 = &v92[0].__tree_.__pair1_;
LABEL_96:
v47 = v46;
if ( v45 )
{
do
{
v46 = (std::__compressed_pair *>> *)v45;
v48 = *((_DWORD *)v45 + 7);
if ( v44 < v48 )
{
v45 = v46->__value_.__left_;
goto LABEL_96;
}
if ( v44 <= v48 )
goto LABEL_104;
v45 = v46[1].__value_.__left_;
}
while ( v45 );
v47 = v46 + 1;
}
v49 = operator new(0x20u);
v49[7] = v44;
*(_OWORD *)v49 = 0;
*((_QWORD *)v49 + 2) = v46;
v47->__value_.__left_ = (std::__tree_end_node *>::pointer)v49;
if ( v92[0].__tree_.__begin_node_->__left_ )
v92[0].__tree_.__begin_node_ = (std::__tree::__iter_pointer)v92[0].__tree_.__begin_node_->__left_;
sub_100054DD0((__int64 *)v92[0].__tree_.__pair1_.__value_.__left_, (__int64 *)v49);
++v92[0].__tree_.__pair3_.__value_;
LABEL_104:
v50 = v6[26].m_uID;
v51 = v90.__tree_.__pair1_.__value_.__left_;
v52 = &v90.__tree_.__pair1_;
LABEL_106:
v53 = v52;
if ( v51 )
{
do
{
v52 = (std::__compressed_pair *>> *)v51;
v54 = *((_DWORD *)v51 + 7);
if ( v50 < v54 )
{
v51 = v52->__value_.__left_;
goto LABEL_106;
}
if ( v50 <= v54 )
goto LABEL_6;
v51 = v52[1].__value_.__left_;
}
while ( v51 );
v53 = v52 + 1;
}
v18 = operator new(0x20u);
v18[7] = v50;
*(_OWORD *)v18 = 0;
*((_QWORD *)v18 + 2) = v52;
v53->__value_.__left_ = (std::__tree_end_node *>::pointer)v18;
if ( v90.__tree_.__begin_node_->__left_ )
v90.__tree_.__begin_node_ = (std::__tree::__iter_pointer)v90.__tree_.__begin_node_->__left_;
v4 = (__int64 **)&v90.__tree_.__pair1_;
p_pair3 = &v90.__tree_.__pair3_;
goto LABEL_5;
}
if ( m_uReference != 2915 )
goto LABEL_6;
m_eObjType = v6[26].m_uID;
v9 = v92[0].__tree_.__pair1_.__value_.__left_;
v10 = &v92[0].__tree_.__pair1_;
LABEL_15:
v11 = v10;
if ( v9 )
{
do
{
v10 = (std::__compressed_pair *>> *)v9;
v12 = *((_DWORD *)v9 + 7);
if ( m_eObjType < v12 )
{
v9 = v10->__value_.__left_;
goto LABEL_15;
}
if ( m_eObjType <= v12 )
goto LABEL_6;
v9 = v10[1].__value_.__left_;
}
while ( v9 );
v11 = v10 + 1;
}
}
v18 = operator new(0x20u);
v18[7] = m_eObjType;
*(_OWORD *)v18 = 0;
*((_QWORD *)v18 + 2) = v10;
v11->__value_.__left_ = (std::__tree_end_node *>::pointer)v18;
v19 = (std::__tree_end_node *> *)v92[0].__tree_.__begin_node_->__left_;
if ( v92[0].__tree_.__begin_node_->__left_ )
goto LABEL_93;
LABEL_4:
v4 = (__int64 **)&v92[0].__tree_.__pair1_;
p_pair3 = &v92[0].__tree_.__pair3_;
LABEL_5:
sub_100054DD0(*v4, (__int64 *)v18);
++p_pair3->__value_;
LABEL_6:
++arr;
}
while ( (unsigned __int64)arr <= v88 );
}
}
GJBaseGameLayer::addRemapTargets(v91, v92);
begin_node = v92[0].__tree_.__begin_node_;
if ( (std::__compressed_pair *>> *)v92[0].__tree_.__begin_node_ != &v92[0].__tree_.__pair1_ )
{
do
{
left_high = SHIDWORD(begin_node[3].__left_);
if ( left_high > 0 )
{
m_parentGroupsDict = v91->m_parentGroupsDict;
v58 = SHIDWORD(begin_node[3].__left_);
v59 = (GameObject *)cocos2d::CCDictionary::objectForKey(m_parentGroupsDict, v58);
if ( v59 )
goto LABEL_119;
if ( (unsigned int)left_high >= 0x270F )
left_high = 9999;
v63 = v91->m_groups.__begin_[left_high];
if ( v63
|| (v63 = (cocos2d::CCArray *)sub_10071F9E0(m_parentGroupsDict, v58),
cocos2d::CCDictionary::setObject(v91->m_groupDict, v63, left_high),
(v91->m_groups.__begin_[left_high] = v63) != nullptr) )
{
if ( (unsigned int)sub_100720070(v63) == 1 )
{
v59 = (GameObject *)sub_1007200C0(v63, 0);
if ( v59 )
LABEL_119:
GJBaseGameLayer::moveObjectToStaticGroup(v91, v59);
}
}
}
v60 = (std::set *)begin_node[1].__left_;
if ( v60 )
{
do
{
v61 = v60;
v60 = (std::set *)v60->__tree_.__begin_node_;
}
while ( v60 );
}
else
{
do
{
v61 = (std::set *)begin_node[2].__left_;
v62 = v61->__tree_.__begin_node_ == begin_node;
begin_node = (std::__tree::__iter_pointer)v61;
}
while ( !v62 );
}
begin_node = (std::__tree::__iter_pointer)v61;
}
while ( v61 != (std::set *)&v92[0].__tree_.__pair1_ );
}
v64 = &v90;
m_groupDict = (cocos2d::CCDictionary *)v91;
GJBaseGameLayer::addRemapTargets(v91, &v90);
v66 = v90.__tree_.__begin_node_;
if ( (std::__compressed_pair *>> *)v90.__tree_.__begin_node_ != &v90.__tree_.__pair1_ )
{
while ( 1 )
{
v87 = v66;
v67 = HIDWORD(v66[3].__left_);
if ( (int)v67 >= 9999 )
v67 = 9999;
if ( (int)v67 <= 0 )
v67 = 0;
v68 = v91->m_groups.__begin_[v67];
if ( v68
|| (v69 = (cocos2d::CCObject *)sub_10071F9E0(m_groupDict, v64),
v70 = v91,
v68 = (cocos2d::CCArray *)v69,
m_groupDict = v91->m_groupDict,
v64 = (std::set *)v69,
cocos2d::CCDictionary::setObject(m_groupDict, v69, v67),
(v70->m_groups.__begin_[v67] = v68) != nullptr) )
{
v71 = v68->data;
num = v71->num;
if ( v71->num )
break;
}
LABEL_161:
v83 = (std::set *)v87;
v84 = (std::set *)v87[1].__left_;
if ( v84 )
{
do
{
value = v84;
v84 = (std::set *)v84->__tree_.__begin_node_;
}
while ( v84 );
}
else
{
do
{
value = (std::set *)v83->__tree_.__pair3_.__value_;
v62 = value->__tree_.__begin_node_ == (std::__tree::__iter_pointer)v83;
v83 = value;
}
while ( !v62 );
}
v66 = (std::__tree::__iter_pointer)value;
if ( value == (std::set *)&v90.__tree_.__pair1_ )
goto LABEL_133;
}
v73 = (GameObject **)v71->arr;
v86 = (unsigned __int64)&v73[num - 1];
while ( 1 )
{
v74 = *v73;
if ( !*v73 )
goto LABEL_161;
v89 = v73;
if ( v74->m_isDecoration2 )
{
v74->m_isDecoration2 = 0;
if ( v74->m_groupCount > 0 )
break;
}
LABEL_143:
v73 = v89 + 1;
if ( (unsigned __int64)(v89 + 1) > v86 )
goto LABEL_161;
}
v75 = 0;
while ( 1 )
{
m_staticGroupDict = (cocos2d::CCDictionary *)v74;
v77 = (cocos2d::CCArray *)v75;
GroupID = GameObject::getGroupID(v74, v75);
v79 = (unsigned int)GroupID;
if ( GroupID >= 9999 )
v79 = 9999;
if ( (int)v79 <= 0 )
v79 = 0;
v80 = v91->m_staticGroups.__begin_[v79];
if ( v80 )
{
v81 = v91->m_optimizedGroups.__begin_[v79];
if ( !v81 )
goto LABEL_156;
}
else
{
v80 = (cocos2d::CCArray *)sub_10071F9E0(v74, v75);
m_staticGroupDict = v91->m_staticGroupDict;
v77 = v80;
cocos2d::CCDictionary::setObject(m_staticGroupDict, v80, v79);
v82 = v91;
v91->m_staticGroups.__begin_[v79] = v80;
v81 = v82->m_optimizedGroups.__begin_[v79];
if ( !v81 )
{
LABEL_156:
v81 = (cocos2d::CCArray *)sub_10071F9E0(m_staticGroupDict, v77);
cocos2d::CCDictionary::setObject(v91->m_optimizedGroupDict, v81, v79);
v91->m_optimizedGroups.__begin_[v79] = v81;
}
}
if ( !(unsigned __int8)sub_100720160(v80, v74) )
sub_10071FCD0(v80, v74);
m_groupDict = (cocos2d::CCDictionary *)v81;
v64 = (std::set *)v74;
sub_100720220(v81, v74, 1);
if ( (int)++v75 >= v74->m_groupCount )
goto LABEL_143;
}
}
LABEL_133:
sub_100054D90((__int64)&v90, (_QWORD *)v90.__tree_.__pair1_.__value_.__left_);
sub_100054D90((__int64)v92, (_QWORD *)v92[0].__tree_.__pair1_.__value_.__left_);
}