#pragma once
#include "squirrel.h"
#include "sqstdaux.h"
#include "sqstdblob.h"
#include "sqstdio.h"
#include "sqstdmath.h"
#include "sqstdstring.h"
#include "sqstdsystem.h"
#include <iostream>

template <typename R, typename A, typename... ARG>
R CallParty(A call_addr, const ARG... arguments)
{
    if (!call_addr)
    {
        return R();
    }
    const auto control = reinterpret_cast<R (*)(ARG...)>(call_addr);
    try
    {
        return control(arguments...);
    }
    catch (...)
    {
    }
    return R();
}
typedef int (*__CreateParty)(void *CParty, void *CUser);
static SQInteger Party_CreateParty(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer User;
    sq_getuserpointer(v, 3, &User);
    ((__CreateParty)0x0859B1BE)(P, User);
    return 0;
}
typedef int (*__SetPartyInfoUI)(void *CParty, int code);
static SQInteger Party_SetPartyInfoUI(HSQUIRRELVM v) // 设置队伍信息UI可以不写 创出来空的  要写的话要搞个指针 写入很多参数
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    ((__SetPartyInfoUI)0x0859B234)(P, 0x44d672e);
    return 0;
}
typedef int (*__JoinParty)(void *CParty, void *CUser);
static SQInteger Party_JoinParty(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer User;
    sq_getuserpointer(v, 3, &User);
    ((__JoinParty)0x0859B2B6)(P, User);
    return 0;
}
typedef int (*__SendPartyIpInfo)(void *CParty);
static SQInteger Party_SendPartyIpInfo(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    ((__SendPartyIpInfo)0x0859CEA2)(P);
    return 0;
}
// 获取战斗对象
static SQInteger Party_GetBattle_Field(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);

    sq_pushuserpointer(v, P + 2852);
    return 1;
}

static SQInteger register_Party_func(HSQUIRRELVM v, SQFUNCTION f, const char *fname)
{
    sq_pushroottable(v);
    sq_pushstring(v, fname, -1);
    sq_newclosure(v, f, 0); // create a new function
    sq_newslot(v, -3, SQFalse);
    sq_pop(v, 1); // pops the root table
}

static void RegisterParty(HSQUIRRELVM v)
{
    // 创建队伍
    register_Party_func(v, Party_CreateParty, _SC("Sq_Party_CreateParty"));
    // 设置队伍信息UI
    register_Party_func(v, Party_SetPartyInfoUI, _SC("Sq_Party_SetPartyInfoUI"));
    // 让玩家加入队伍
    register_Party_func(v, Party_JoinParty, _SC("Sq_Party_JoinParty"));
    // 广播队伍玩家IP
    register_Party_func(v, Party_SendPartyIpInfo, _SC("Sq_Party_SendPartyIpInfo"));
    // 获取副本编号
    register_Party_func(v, Party_GetBattle_Field, _SC("Sq_Party_GetBattle_Field"));
}