Commit 19bb8c48 authored by wind2009's avatar wind2009

Merge remote-tracking branch 'upstream/master' into develop

parents 0631e6b0 fd48b607
......@@ -455,11 +455,6 @@ uint32_t card::get_another_code() {
return otcode;
return 0;
}
inline bool check_setcode(uint16_t setcode, uint32_t value) {
const uint32_t settype = value & 0x0fffU;
const uint32_t setsubtype = value & 0xf000U;
return (setcode & 0x0fffU) == settype && (setcode & setsubtype) == setsubtype;
}
int32_t card::is_set_card(uint32_t set_code) {
uint32_t code1 = get_code();
card_data dat1;
......
......@@ -24,6 +24,33 @@ const std::unordered_map<uint32_t, uint32_t> second_code = {
{CARD_HERMOS, 10000070u},
};
inline bool check_setcode(uint16_t setcode, uint32_t value) {
const uint32_t settype = value & 0x0fffU;
const uint32_t setsubtype = value & 0xf000U;
return setcode && (setcode & 0x0fffU) == settype && (setcode & setsubtype) == setsubtype;
}
inline void write_setcode(uint16_t list[], uint64_t value) {
if (!list)
return;
int len = 0;
while (value) {
if (value & 0xffff) {
list[len] = value & 0xffff;
++len;
}
value >>= 16;
}
if (len < SIZE_SETCODE)
std::memset(list + len, 0, (SIZE_SETCODE - len) * sizeof(uint16_t));
}
inline bool is_alternative(uint32_t code, uint32_t alias) {
if (code == CARD_BLACK_LUSTER_SOLDIER2)
return false;
return alias && (alias < code + CARD_ARTWORK_VERSIONS_OFFSET) && (code < alias + CARD_ARTWORK_VERSIONS_OFFSET);
}
struct card_data {
uint32_t code{};
uint32_t alias{};
......@@ -43,38 +70,17 @@ struct card_data {
}
bool is_setcode(uint32_t value) const {
const uint16_t settype = value & 0x0fff;
const uint16_t setsubtype = value & 0xf000;
for (auto& x : setcode) {
if ((x & 0x0fff) == settype && (x & setsubtype) == setsubtype)
return true;
if (!x)
return false;
if (check_setcode(x, value))
return true;
}
return false;
}
bool is_alternative() const {
if (code == CARD_BLACK_LUSTER_SOLDIER2)
return false;
return alias && (alias < code + CARD_ARTWORK_VERSIONS_OFFSET) && (code < alias + CARD_ARTWORK_VERSIONS_OFFSET);
}
void set_setcode(uint64_t value) {
int ctr = 0;
while (value) {
if (value & 0xffff) {
setcode[ctr] = value & 0xffff;
++ctr;
}
value >>= 16;
}
if (ctr < SIZE_SETCODE)
std::memset(setcode + ctr, 0, (SIZE_SETCODE - ctr) * sizeof(uint16_t));
}
uint32_t get_original_code() const {
return is_alternative() ? alias : code;
return is_alternative(code, alias) ? alias : code;
}
};
......
......@@ -16,7 +16,7 @@
#include "buffer.h"
duel::duel() {
lua = new interpreter(this);
lua = new interpreter(this, false);
game_field = new field(this);
game_field->temp_card = new_card(TEMP_CARD_ID);
message_buffer.reserve(SIZE_MESSAGE_BUFFER);
......
......@@ -531,6 +531,7 @@ const std::map<uint64_t, uint64_t> category_checklist{
#define EFFECT_XYZ_MIN_COUNT 372
#define EFFECT_SYNCHRO_LEVEL_EX 373
#define EFFECT_RITUAL_LEVEL_EX 374
#define EFFECT_DOUBLE_XMATERIAL 375
//#define EVENT_STARTUP 1000
#define EVENT_FLIP 1001
......
......@@ -14,7 +14,7 @@
#include "ocgapi.h"
#include "interpreter.h"
interpreter::interpreter(duel* pd): coroutines(256) {
interpreter::interpreter(duel* pd, bool enable_unsafe_libraries): coroutines(256) {
lua_state = luaL_newstate();
current_state = lua_state;
pduel = pd;
......@@ -35,15 +35,20 @@ interpreter::interpreter(duel* pd): coroutines(256) {
lua_pop(lua_state, 1);
luaL_requiref(lua_state, "math", luaopen_math, 1);
lua_pop(lua_state, 1);
if (enable_unsafe_libraries) {
luaL_requiref(lua_state, "io", luaopen_io, 1);
lua_pop(lua_state, 1);
}
auto nil_out = [&](const char* name) {
lua_pushnil(lua_state);
lua_setglobal(lua_state, name);
};
nil_out("collectgarbage");
#ifndef ENABLE_UNSAFE_LIBRARIES
nil_out("dofile");
nil_out("loadfile");
#endif // ENABLE_UNSAFE_LIBRARIES
if (!enable_unsafe_libraries) {
nil_out("dofile");
nil_out("loadfile");
}
#endif
//open all libs
scriptlib::open_cardlib(lua_state);
......
......@@ -53,7 +53,7 @@ public:
int32_t no_action;
int32_t call_depth;
explicit interpreter(duel* pd);
explicit interpreter(duel* pd, bool enable_unsafe_libraries);
~interpreter();
void register_card(card* pcard);
......
......@@ -176,10 +176,12 @@ int32_t scriptlib::debug_set_ai_name(lua_State *L) {
}
int32_t scriptlib::debug_show_hint(lua_State *L) {
check_param_count(L, 1);
check_param(L, PARAM_TYPE_STRING, 1);
duel* pduel = interpreter::get_duel_info(L);
lua_getglobal(L, "tostring");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 1, 0);
pduel->write_buffer8(MSG_SHOW_HINT);
const char* pstr = lua_tostring(L, 1);
const char* pstr = lua_tostring(L, -1);
int len = (int)std::strlen(pstr);
if (len > SIZE_HINT_MSG - 1)
len = SIZE_HINT_MSG - 1;
......
......@@ -4446,7 +4446,7 @@ int32_t scriptlib::duel_is_player_can_spsummon_monster(lua_State * L) {
dat.code = code;
dat.alias = 0;
if(lua_gettop(L) >= 3 && !lua_isnil(L, 3))
dat.set_setcode((uint64_t)lua_tointeger(L, 3));
write_setcode(dat.setcode, lua_tointeger(L, 3));
if(lua_gettop(L) >= 4 && !lua_isnil(L, 4))
dat.type = (uint32_t)lua_tointeger(L, 4);
if(lua_gettop(L) >= 5 && !lua_isnil(L, 5))
......
......@@ -59,24 +59,25 @@ public:
return l + (int)(x % range);
}
// N % k == (N - k) % k, discard the leftmost numbers
// N % k = (N - k) % k = (-k) % k
// discard (N % range) numbers from the left end so that it is a multiple of range
#pragma warning(disable:4146)
int get_random_integer_v2(int l, int h) {
uint32_t range = (h - l + 1);
#pragma warning(disable:4146)
uint32_t bound = -range % range;
#pragma warning(default:4146)
auto x = rng();
while (x < bound) {
x = rng();
}
return l + (int)(x % range);
}
#pragma warning(default:4146)
// Fisher-Yates shuffle [first, last)
template<typename T>
void shuffle_vector(std::vector<T>& v, int first = 0, int last = INT32_MAX, int version = 2) {
if ((size_t)last > v.size())
last = v.size();
last = (int)v.size();
auto distribution = &mtrandom::get_random_integer_v2;
if (version == 1)
distribution = &mtrandom::get_random_integer_v1;
......
......@@ -12,7 +12,6 @@ end
workspace "ocgcoredll"
location "build"
language "C++"
cppdialect "C++14"
configurations { "Release", "Debug" }
platforms { "x32", "x64" }
......@@ -62,7 +61,6 @@ include(LUA_DIR)
project "ocgcore"
kind "SharedLib"
cppdialect "C++14"
files { "*.cpp", "*.h" }
links { "lua" }
......
project "ocgcore"
kind "StaticLib"
cppdialect "C++14"
files { "*.cpp", "*.h" }
......@@ -10,6 +9,9 @@ project "ocgcore"
includedirs { LUA_INCLUDE_DIR }
end
filter "not action:vs*"
cppdialect "C++14"
filter "system:bsd"
defines { "LUA_USE_POSIX" }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment