2015-09-25 21:07:16 +08:00
|
|
|
/*
|
|
|
|
|
* scriptlib.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2010-7-29
|
|
|
|
|
* Author: Argon
|
|
|
|
|
*/
|
|
|
|
|
#include "scriptlib.h"
|
|
|
|
|
#include "duel.h"
|
|
|
|
|
|
2018-09-11 18:39:26 +08:00
|
|
|
static int32 check_data_type(lua_State* L, int32 index, const char* tname) {
|
2018-08-12 11:37:00 +08:00
|
|
|
int32 result = FALSE;
|
|
|
|
|
if(lua_getmetatable(L, index)) {
|
|
|
|
|
lua_getglobal(L, tname);
|
|
|
|
|
if(lua_rawequal(L, -1, -2))
|
|
|
|
|
result = TRUE;
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-09-25 21:07:16 +08:00
|
|
|
int32 scriptlib::check_param(lua_State* L, int32 param_type, int32 index, int32 retfalse) {
|
|
|
|
|
switch (param_type) {
|
2018-08-12 11:37:00 +08:00
|
|
|
case PARAM_TYPE_CARD: {
|
|
|
|
|
int32 result = FALSE;
|
|
|
|
|
if(lua_isuserdata(L, index) && lua_getmetatable(L, index)) {
|
|
|
|
|
result = check_data_type(L, -1, "Card");
|
|
|
|
|
lua_pop(L, 1);
|
2015-09-25 21:07:16 +08:00
|
|
|
}
|
2018-08-12 11:37:00 +08:00
|
|
|
if(result)
|
|
|
|
|
return TRUE;
|
2015-09-25 21:07:16 +08:00
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
|
|
|
|
luaL_error(L, "Parameter %d should be \"Card\".", index);
|
|
|
|
|
break;
|
2018-08-12 11:37:00 +08:00
|
|
|
}
|
|
|
|
|
case PARAM_TYPE_GROUP: {
|
|
|
|
|
if(lua_isuserdata(L, index) && check_data_type(L, index, "Group"))
|
|
|
|
|
return TRUE;
|
2015-09-25 21:07:16 +08:00
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
|
|
|
|
luaL_error(L, "Parameter %d should be \"Group\".", index);
|
|
|
|
|
break;
|
2018-08-12 11:37:00 +08:00
|
|
|
}
|
|
|
|
|
case PARAM_TYPE_EFFECT: {
|
|
|
|
|
if(lua_isuserdata(L, index) && check_data_type(L, index, "Effect"))
|
|
|
|
|
return TRUE;
|
2015-09-25 21:07:16 +08:00
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
|
|
|
|
luaL_error(L, "Parameter %d should be \"Effect\".", index);
|
|
|
|
|
break;
|
2018-08-12 11:37:00 +08:00
|
|
|
}
|
|
|
|
|
case PARAM_TYPE_FUNCTION: {
|
|
|
|
|
if(lua_isfunction(L, index))
|
2015-09-25 21:07:16 +08:00
|
|
|
return TRUE;
|
|
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
|
|
|
|
luaL_error(L, "Parameter %d should be \"Function\".", index);
|
|
|
|
|
break;
|
2018-08-12 11:37:00 +08:00
|
|
|
}
|
|
|
|
|
case PARAM_TYPE_STRING: {
|
|
|
|
|
if(lua_isstring(L, index))
|
2015-09-25 21:07:16 +08:00
|
|
|
return TRUE;
|
|
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
|
|
|
|
luaL_error(L, "Parameter %d should be \"String\".", index);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-08-12 11:37:00 +08:00
|
|
|
}
|
2015-09-25 21:07:16 +08:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32 scriptlib::check_param_count(lua_State* L, int32 count) {
|
|
|
|
|
if (lua_gettop(L) < count)
|
|
|
|
|
luaL_error(L, "%d Parameters are needed.", count);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
int32 scriptlib::check_action_permission(lua_State* L) {
|
|
|
|
|
duel* pduel = interpreter::get_duel_info(L);
|
|
|
|
|
if(pduel->lua->no_action)
|
|
|
|
|
luaL_error(L, "Action is not allowed here.");
|
|
|
|
|
return TRUE;
|
2018-08-13 19:02:22 +02:00
|
|
|
}
|