//
// Generated By: dol2asm
// Translation Unit: d_a_tag_kmsg
//

#include "rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg.h"
#include "d/a/d_a_npc.h"
#include "d/a/d_a_player.h"
#include "SSystem/SComponent/c_counter.h"
#include "string.h"
#include "dol2asm.h"

//
// Forward References:
//

extern "C" void create__12daTag_KMsg_cFv();
extern "C" void Delete__12daTag_KMsg_cFv();
extern "C" void Execute__12daTag_KMsg_cFv();
extern "C" bool Draw__12daTag_KMsg_cFv();
extern "C" void isDelete__12daTag_KMsg_cFv();
extern "C" static void daTag_KMsg_Create__FPv();
extern "C" static void daTag_KMsg_Delete__FPv();
extern "C" static void daTag_KMsg_Execute__FPv();
extern "C" static void daTag_KMsg_Draw__FPv();
extern "C" static bool daTag_KMsg_IsDelete__FPv();
extern "C" void __dt__12daTag_KMsg_cFv();
extern "C" extern char const* const d_a_tag_kmsg__stringBase0;

//
// External References:
//

extern "C" void __ct__10fopAc_ac_cFv();
extern "C" void __dt__10fopAc_ac_cFv();
extern "C" void fopAcM_delete__FP10fopAc_ac_c();
extern "C" void fopAcM_orderChangeEventId__FP10fopAc_ac_csUsUs();
extern "C" void fopAcM_orderOtherEventId__FP10fopAc_ac_csUcUsUsUs();
extern "C" void fopAcM_cancelCarryNow__FP10fopAc_ac_c();
extern "C" void dComIfG_resLoad__FP30request_of_phase_process_classPCc();
extern "C" void dComIfG_resDelete__FP30request_of_phase_process_classPCc();
extern "C" void isSwitch__10dSv_info_cCFii();
extern "C" void reset__14dEvt_control_cFv();
extern "C" void reset__14dEvt_control_cFPv();
extern "C" void setObjectArchive__16dEvent_manager_cFPc();
extern "C" void getEventIdx__16dEvent_manager_cFP10fopAc_ac_cPCcUc();
extern "C" void endCheck__16dEvent_manager_cFs();
extern "C" void getMyStaffId__16dEvent_manager_cFPCcP10fopAc_ac_ci();
extern "C" void cutEnd__16dEvent_manager_cFi();
extern "C" void LineCross__4cBgSFP11cBgS_LinChk();
extern "C" void __ct__11dBgS_LinChkFv();
extern "C" void __dt__11dBgS_LinChkFv();
extern "C" void Set__11dBgS_LinChkFPC4cXyzPC4cXyzPC10fopAc_ac_c();
extern "C" void daNpcT_chkDoBtnIsSpeak__FP10fopAc_ac_c();
extern "C" void daNpcT_getDistTableIdx__Fii();
extern "C" void daNpcT_chkEvtBit__FUl();
extern "C" void __ct__10dMsgFlow_cFv();
extern "C" void __dt__10dMsgFlow_cFv();
extern "C" void init__10dMsgFlow_cFP10fopAc_ac_ciiPP10fopAc_ac_c();
extern "C" void doFlow__10dMsgFlow_cFP10fopAc_ac_cPP10fopAc_ac_ci();
extern "C" void __pl__4cXyzCFRC3Vec();
extern "C" void __mi__4cXyzCFRC3Vec();
extern "C" void __ml__4cXyzCFf();
extern "C" void norm__4cXyzCFv();
extern "C" void __ct__7cSAngleFs();
extern "C" void Sin__7cSAngleCFv();
extern "C" void Cos__7cSAngleCFv();
extern "C" void __dl__FPv();
extern "C" void _savegpr_25();
extern "C" void _restgpr_25();

//
// Declarations:
//

/* ############################################################################################## */
/* 8048EADC-8048EAE0 000000 0004+00 2/2 0/0 0/0 .rodata          @3884 */
SECTION_RODATA static f32 const lit_3884 = 10.0f;
COMPILER_STRIP_GATE(0x8048EADC, &lit_3884);

/* 8048EAFC-8048EAFC 000020 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */
#pragma push
#pragma force_active on
SECTION_DEAD static char const* const stringBase_8048EAFC = "";
SECTION_DEAD static char const* const stringBase_8048EAFD = "EXTINCTION";
SECTION_DEAD static char const* const stringBase_8048EB08 = "PURCHASE";
SECTION_DEAD static char const* const stringBase_8048EB11 = "Bans1";
SECTION_DEAD static char const* const stringBase_8048EB17 = "sekizoA";
SECTION_DEAD static char const* const stringBase_8048EB1F = "Lv6Gate";
#pragma pop

struct EventListItem {
    char* mEventName;
    u32 field_0x4;
};

/* 8048EB2C-8048EB44 -00001 0018+00 1/1 0/0 0/0 .data            l_evtList */
SECTION_DATA static EventListItem l_evtList[3] = {
    {"", 0},
    {"EXTINCTION", 2},
    {"PURCHASE", 1},
};

/* 8048EB44-8048EB50 -00001 000C+00 1/1 0/0 0/0 .data            l_resNameList */
SECTION_DATA static char* l_resNameList[3] = {
    "",
    "Bans1",
    "sekizoA",
};

/* 8048EB50-8048EB70 -00001 0020+00 1/0 0/0 0/0 .data            daTag_KMsg_MethodTable */
SECTION_DATA static void* daTag_KMsg_MethodTable[8] = {
    (void*)daTag_KMsg_Create__FPv,
    (void*)daTag_KMsg_Delete__FPv,
    (void*)daTag_KMsg_Execute__FPv,
    (void*)daTag_KMsg_IsDelete__FPv,
    (void*)daTag_KMsg_Draw__FPv,
    (void*)NULL,
    (void*)NULL,
    (void*)NULL,
};

/* 8048EB70-8048EBA0 -00001 0030+00 0/0 0/0 1/0 .data            g_profile_TAG_KMSG */
SECTION_DATA extern void* g_profile_TAG_KMSG[12] = {
    (void*)0xFFFFFFFD, (void*)0x0008FFFD,
    (void*)0x02EB0000, (void*)&g_fpcLf_Method,
    (void*)0x000005CC, (void*)NULL,
    (void*)NULL,       (void*)&g_fopAc_Method,
    (void*)0x01220000, (void*)&daTag_KMsg_MethodTable,
    (void*)0x00044000, (void*)0x000E0000,
};

/* 8048EBA0-8048EBAC 000074 000C+00 2/2 0/0 0/0 .data            __vt__12daTag_KMsg_c */
SECTION_DATA extern void* __vt__12daTag_KMsg_c[3] = {
    (void*)NULL /* RTTI */,
    (void*)NULL,
    (void*)__dt__12daTag_KMsg_cFv,
};

/* 8048DE78-8048DFDC 000078 0164+00 1/1 0/0 0/0 .text            create__12daTag_KMsg_cFv */
// Matches with vtable
#ifdef NONMATCHING
int daTag_KMsg_c::create() {
    attention_info.position = current.pos;
    eyePos = attention_info.position;
    fopAcM_SetupActor(this, daTag_KMsg_c);
    int rv;
    if (getType() == KMSG_TYPE_3) {
        rv = dComIfG_resLoad(&mPhase, "Lv6Gate");
        if (rv != cPhs_COMPLEATE_e) {
            return rv;
        }
    }
    if (getType() == KMSG_TYPE_2 || getType() == KMSG_TYPE_5) {
        scale.y *= 10.0f;
    }
    mFlowNodeNo = getFlowNodeNo();
    mEventIdx = -1;

    if (isDelete()) {
        return cPhs_ERROR_e;
    } else {
        return cPhs_COMPLEATE_e;
    }
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm int daTag_KMsg_c::create() {
    nofralloc
#include "asm/rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg/create__12daTag_KMsg_cFv.s"
}
#pragma pop
#endif

/* 8048DFDC-8048E010 0001DC 0034+00 1/1 0/0 0/0 .text            Delete__12daTag_KMsg_cFv */
// Matches with vtable
#ifndef NONMATCHING
int daTag_KMsg_c::Delete() {
    OS_REPORT("|%06d:%x|daTag_KMsg_c -> Delete\n", g_Counter, this);
    fopAcM_GetID(this);
    this->~daTag_KMsg_c();
    return 1;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm int daTag_KMsg_c::Delete() {
    nofralloc
#include "asm/rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg/Delete__12daTag_KMsg_cFv.s"
}
#pragma pop
#endif

/* ############################################################################################## */
/* 8048EAE0-8048EAE4 000004 0004+00 0/1 0/0 0/0 .rodata          @4206 */
#pragma push
#pragma force_active on
SECTION_RODATA static f32 const lit_4206 = 1000000000.0f;
COMPILER_STRIP_GATE(0x8048EAE0, &lit_4206);
#pragma pop

/* 8048EAE4-8048EAE8 000008 0004+00 0/1 0/0 0/0 .rodata          @4207 */
#pragma push
#pragma force_active on
SECTION_RODATA static f32 const lit_4207 = -0.5f;
COMPILER_STRIP_GATE(0x8048EAE4, &lit_4207);
#pragma pop

/* 8048EAE8-8048EAEC 00000C 0004+00 0/1 0/0 0/0 .rodata          @4208 */
#pragma push
#pragma force_active on
SECTION_RODATA static f32 const lit_4208 = -20.0f;
COMPILER_STRIP_GATE(0x8048EAE8, &lit_4208);
#pragma pop

/* 8048EAEC-8048EAF4 000010 0004+04 0/1 0/0 0/0 .rodata          @4209 */
#pragma push
#pragma force_active on
SECTION_RODATA static f32 const lit_4209[1 + 1 /* padding */] = {
    80.0f,
    /* padding */
    0.0f,
};
COMPILER_STRIP_GATE(0x8048EAEC, &lit_4209);
#pragma pop

/* 8048EAF4-8048EAFC 000018 0008+00 0/1 0/0 0/0 .rodata          @4211 */
#pragma push
#pragma force_active on
SECTION_RODATA static u8 const lit_4211[8] = {
    0x43, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
COMPILER_STRIP_GATE(0x8048EAF4, &lit_4211);
#pragma pop

/* 8048EAFC-8048EAFC 000020 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */
#pragma push
#pragma force_active on
SECTION_DEAD static char const* const stringBase_8048EB27 = "KMsg";
#pragma pop

/* 8048E010-8048E8B8 000210 08A8+00 1/1 0/0 0/0 .text            Execute__12daTag_KMsg_cFv */
// Various issues: stack, getTalkAngle
#ifdef NONMATCHING
int daTag_KMsg_c::Execute() {
    bool r28 = false;
    if (home.roomNo == dComIfGp_roomControl_getStayNo()) {
        if (isDelete()) {
            fopAcM_delete(this);
            return 1;
        }
        if (getType() == KMSG_TYPE_3) {
            if (field_0x5c3 == 0) {
                attention_info.flags = 0x80;
                attention_info.distances[7] = 0x4a;
            } else {
                attention_info.flags = 0;
            }
        } else {
            int angle = getTalkAngle();
            int talkDistance = getTalkDis();
            attention_info.distances[1] = daNpcT_getDistTableIdx(talkDistance, angle);
            attention_info.distances[3] = attention_info.distances[1];
            if (getAttnPosOffset() != 1000000000.0f) {
                attention_info.flags = 0xa;
            } else {
                attention_info.flags = 0x8;
            }
        }
        if ((getType() == KMSG_TYPE_1 || getType() == KMSG_TYPE_4 || getType() == KMSG_TYPE_5) &&
            daPy_py_c::i_checkNowWolf())
        {
            attention_info.flags = 0;
        }
        if (dComIfGp_event_runCheck()) {
            if (eventInfo.checkCommandTalk()) {
                u16 iVar10 = 0;
                switch (getType()) {
                case KMSG_TYPE_1:
                    iVar10 = 2;
                    break;
                }
                if (iVar10 != 0) {
                    if (strlen(l_evtList[iVar10].mEventName) != 0) {
                        if (strlen(l_resNameList[l_evtList[iVar10].field_0x4]) != 0) {
                            eventInfo.setArchiveName(l_resNameList[l_evtList[iVar10].field_0x4]);
                            dComIfGp_getEventManager().setObjectArchive(eventInfo.getArchiveName());
                        }
                        mEventIdx =
                            dComIfGp_getEventManager().getEventIdx(this, l_evtList[iVar10].mEventName, 0xff);
                        dComIfGp_getEvent().reset(this);
                        fopAcM_orderChangeEventId(this, mEventIdx, 1, 0xffff);
                    }

                } else {
                    r28 = true;
                    if (!field_0x5c4) {
                        mMsgFlow.init(this, mFlowNodeNo, 0, NULL);
                        field_0x5c4 = true;
                    }
                    if (mMsgFlow.doFlow(this, NULL, 0) != 0) {
                        dComIfGp_event_reset();
                        field_0x5c4 = false;
                    }
                }
            } else {
                if (eventInfo.checkCommandDemoAccrpt() && dComIfGp_getEventManager().endCheck(mEventIdx)) {
                    dComIfGp_event_reset();
                    mEventIdx = -1;
                } else {
                    int myStaffId = dComIfGp_getEventManager().getMyStaffId("KMsg", this, -1);
                    if (myStaffId != -1) {
                        dComIfGp_getEventManager().cutEnd(myStaffId);
                    }
                }
            }
        } else if (getType() == KMSG_TYPE_3) {
            if (field_0x5c2 != 0x0) {
                attention_info.flags &= ~0x10;
                fopAcM_cancelCarryNow(this);
                if (strlen(l_evtList[1].mEventName) != 0) {
                    if (strlen(l_resNameList[l_evtList[1].field_0x4]) != 0) {
                        eventInfo.setArchiveName(l_resNameList[l_evtList[1].field_0x4]);
                        dComIfGp_getEventManager().setObjectArchive(eventInfo.getArchiveName());
                    }
                    mEventIdx = dComIfGp_getEventManager().getEventIdx(this, l_evtList[1].mEventName, 0xff);
                    fopAcM_orderOtherEventId(this, mEventIdx, 0xff, 0xffff, 4, 1);
                }
            }
        } else {
            eventInfo.i_onCondition(1);
            if (getType() != KMSG_TYPE_1 && daNpcT_chkDoBtnIsSpeak(this)) {
                if (getChkType() != KMSG_TYPE_0) {
                    dComIfGp_setDoStatusForce(0x80, 2);
                } else {
                    dComIfGp_setDoStatus(8, 0);
                }
            }
        }
        if (getType() == KMSG_TYPE_3) {
            attention_info.position = current.pos;
            if (getAttnPosOffset() != 1000000000.0f) {
                attention_info.position.y += getAttnPosOffset();
            }
            eyePos = current.pos;
        } else {
            attention_info.position = current.pos;
            if (getType() == KMSG_TYPE_2 || getType() == KMSG_TYPE_5) {
                attention_info.position.y += scale.y;
            }
            eyePos = attention_info.position;
            if (getAttnPosOffset() != 1000000000.0f) {
                attention_info.position.y += getAttnPosOffset();
            }
            eyePos.y += getEyePosOffset();

            if (r28) {
                f32 f31;
                if (getAttnPosOffset() != 1000000000.0f) {
                    f31 = -0.5f * getAttnPosOffset();
                } else {
                    f31 = -20.0f;
                }
                 
                cSAngle angle = shape_angle.y;
                cXyz vec60 = attention_info.position;
                vec60.y += f31;
                cXyz vec54;
                cXyz vec9c;
                vec54.x = vec60.x + 80.0f * angle.Sin();
                vec54.y = vec60.y;
                vec54.z = vec60.x + 80.0f * angle.Cos();
                dBgS_LinChk lin_chk;
                lin_chk.ClrSttsWallOff();
                lin_chk.onBackFlag();
                lin_chk.onFrontFlag();
                lin_chk.Set(&vec54, &vec60, NULL);
                if (dComIfG_Bgsp().LineCross(&lin_chk)) {
                    cXyz vec30 = vec54 - vec60;
                    // !@bug If decompiled correctly, vec9c is uninitialized
                    cXyz vec48 = vec9c + vec30.norm() * 10.0f;
                    attention_info.position.x = vec48.x;
                    attention_info.position.z = vec48.z;
                }
            }
        }
        return 1;
    }
    return 0;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm int daTag_KMsg_c::Execute() {
    nofralloc
#include "asm/rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg/Execute__12daTag_KMsg_cFv.s"
}
#pragma pop
#endif

/* 8048E8B8-8048E8C0 000AB8 0008+00 1/1 0/0 0/0 .text            Draw__12daTag_KMsg_cFv */
int daTag_KMsg_c::Draw() {
    return 1;
}

/* 8048E8C0-8048E9A8 000AC0 00E8+00 2/2 0/0 0/0 .text            isDelete__12daTag_KMsg_cFv */
#ifndef NONMATCHING
int daTag_KMsg_c::isDelete() {
    switch(getType()) {
    case KMSG_TYPE_0:
        return 0;
    case KMSG_TYPE_1:
        return daNpcT_chkEvtBit(0x40) == 0;
    case KMSG_TYPE_2: 
        return 0;
    case KMSG_TYPE_3:
        bool rv = false;
        if (getBitSW() != 0xff && dComIfGs_isSwitch(getBitSW(), fopAcM_GetRoomNo(this))) {
            rv = true;
        }
        return rv;
    case KMSG_TYPE_4:
        return 0;
    case KMSG_TYPE_5:
        return 0;
    default:
        return 0;
    }
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm int daTag_KMsg_c::isDelete() {
    nofralloc
#include "asm/rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg/isDelete__12daTag_KMsg_cFv.s"
}
#pragma pop
#endif

/* 8048E9A8-8048E9C8 000BA8 0020+00 1/0 0/0 0/0 .text            daTag_KMsg_Create__FPv */
static int daTag_KMsg_Create(void* i_this) {
    return static_cast<daTag_KMsg_c*>(i_this)->create();
}

/* 8048E9C8-8048E9E8 000BC8 0020+00 1/0 0/0 0/0 .text            daTag_KMsg_Delete__FPv */
static int daTag_KMsg_Delete(void* i_this) {
    return static_cast<daTag_KMsg_c*>(i_this)->Delete();
}

/* 8048E9E8-8048EA08 000BE8 0020+00 1/0 0/0 0/0 .text            daTag_KMsg_Execute__FPv */
static int daTag_KMsg_Execute(void* i_this) {
    return static_cast<daTag_KMsg_c*>(i_this)->Execute();
}

/* 8048EA08-8048EA28 000C08 0020+00 1/0 0/0 0/0 .text            daTag_KMsg_Draw__FPv */
static int daTag_KMsg_Draw(void* i_this) {
    return static_cast<daTag_KMsg_c*>(i_this)->Draw();
}

/* 8048EA28-8048EA30 000C28 0008+00 1/0 0/0 0/0 .text            daTag_KMsg_IsDelete__FPv */
static int daTag_KMsg_IsDelete(void* param_0) {
    return 1;
}

/* 8048EA30-8048EAD4 000C30 00A4+00 1/0 0/0 0/0 .text            __dt__12daTag_KMsg_cFv */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void __dt__12daTag_KMsg_cFv() {
//asm daTag_KMsg_c::~daTag_KMsg_c() {
    nofralloc
#include "asm/rel/d/a/tag/d_a_tag_kmsg/d_a_tag_kmsg/__dt__12daTag_KMsg_cFv.s"
}
#pragma pop

/* 8048EAFC-8048EAFC 000020 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */
