#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 CallBattleField(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();
}

// 获取副本对象
static SQInteger BattleField_GetDgn(HSQUIRRELVM v)
{
    SQUserPointer P;
    sq_getuserpointer(v, 2, &P);
    SQUserPointer B = CallBattleField<void *>(0x80FDCFC, P);
    sq_pushuserpointer(v, B);
    return 1;
}

static SQInteger register_BattleField_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 RegisterBattleField(HSQUIRRELVM v)
{
    // 获取副本对象
    register_BattleField_func(v, BattleField_GetDgn, _SC("Sq_BattleField_GetDgn"));
}