dep-ygopro-core/scriptlib.cpp

80 lines
1.8 KiB
C++
Raw Permalink Normal View History

2015-09-25 21:07:16 +08:00
/*
2024-01-03 01:21:08 +01:00
* Copyright (c) 2010-2015, Argon Sun (Fluorohydride)
* Copyright (c) 2018-2025, Edoardo Lolletti (edo9300) <edoardo762@gmail.com>
2015-09-25 21:07:16 +08:00
*
2024-01-03 01:21:08 +01:00
* SPDX-License-Identifier: AGPL-3.0-or-later
2015-09-25 21:07:16 +08:00
*/
#include "duel.h"
#include "field.h"
2024-01-05 12:17:47 +01:00
#include "lua_obj.h"
#include "scriptlib.h"
2015-09-25 21:07:16 +08:00
namespace scriptlib {
const char* get_lua_type_name(lua_State* L, int32_t index) {
switch(auto type = lua_type(L, index); type) {
case LUA_TFUNCTION:
return "Function";
case LUA_TSTRING:
return "String";
case LUA_TNUMBER:
return "Int";
case LUA_TBOOLEAN:
return "boolean";
case LUA_TTABLE:
return "table";
case LUA_TNIL:
case LUA_TNONE:
return "nil";
case LUA_TUSERDATA:
if(auto* obj = lua_get<lua_obj*>(L, index); obj != nullptr) {
switch(obj->lua_type) {
case LuaParam::CARD:
return "Card";
case LuaParam::GROUP:
return "Group";
case LuaParam::EFFECT:
return "Effect";
case LuaParam::DELETED:
return "Deleted";
default:
unreachable();
}
}
[[fallthrough]];
default:
return "unknown";
}
2015-09-25 21:07:16 +08:00
}
void check_action_permission(lua_State* L) {
if(lua_get<duel*>(L)->lua->no_action)
lua_error(L, "Action is not allowed here.");
}
int32_t push_return_cards(lua_State* L, int32_t/* status*/, lua_KContext ctx) {
const auto pduel = lua_get<duel*>(L);
bool cancelable = (bool)ctx;
if(pduel->game_field->return_cards.canceled) {
if(cancelable) {
lua_pushnil(L);
} else {
auto pgroup = pduel->new_group();
interpreter::pushobject(L, pgroup);
}
} else {
auto pgroup = pduel->new_group(pduel->game_field->return_cards.list);
interpreter::pushobject(L, pgroup);
}
return 1;
}
int32_t is_deleted_object(lua_State* L) {
if(auto obj = lua_touserdata(L, 1)) {
2022-11-02 13:01:42 +01:00
auto* ret = *static_cast<lua_obj**>(obj);
2023-12-08 19:53:35 +01:00
lua_pushboolean(L, ret->lua_type == LuaParam::DELETED);
} else {
lua_pushboolean(L, false);
}
return 1;
}
}