From 26f9baf9b475a71bbab46a93fce6d6c3c83b8f9b Mon Sep 17 00:00:00 2001 From: donnyjang Date: Wed, 14 Oct 2026 20:58:16 -0700 Subject: [PATCH] core: import the rules core (ygopro-core 973672dc + fork patches) The vendored ocgcore from the Unity repo (Tools/YGOLegacy/ocgcore at dev HEAD). Upstream is nanahira/ygopro-core at 973672dc9ce7cb49f47173277ab1ad0e318b7593; the seven files that differ from it carry the fork's core patches, already applied: 0001 face-down Token setcode, 0004 effect damage step outside a chain, 0008 Sandbox god mode, 0015 tag first seat holds the hand, 0022 attack preview, 0024 attack preview legality. Generated build/ output is left out. Co-Authored-By: Claude Fable 5.1 --- core/.github/workflows/build.yml | 121 + core/.gitignore | 15 + core/.gitlab-ci.yml | 269 ++ core/CMakeLists.txt | 18 + core/LICENSE | 339 ++ core/LuaMemTracker.cpp | 120 + core/LuaMemTracker.h | 38 + core/README.md | 79 + core/buffer.h | 46 + core/card.cpp | 4302 +++++++++++++++++++ core/card.h | 473 +++ core/card_data.h | 87 + core/common.h | 485 +++ core/duel.cpp | 160 + core/duel.h | 78 + core/effect.cpp | 891 ++++ core/effect.h | 668 +++ core/effectset.h | 21 + core/emscripten_shutdown.cpp | 7 + core/field.cpp | 3711 +++++++++++++++++ core/field.h | 858 ++++ core/group.cpp | 24 + core/group.h | 46 + core/interpreter.cpp | 831 ++++ core/interpreter.h | 102 + core/libcard.cpp | 3946 ++++++++++++++++++ core/libdebug.cpp | 228 ++ core/libduel.cpp | 5603 +++++++++++++++++++++++++ core/libeffect.cpp | 732 ++++ core/libgroup.cpp | 999 +++++ core/lua_coverage.cpp | 155 + core/lua_coverage.h | 35 + core/metadata.cpp | 231 ++ core/metadata.h | 44 + core/mtrandom.h | 108 + core/ocgapi.cpp | 830 ++++ core/ocgapi.h | 108 + core/operations.cpp | 6574 ++++++++++++++++++++++++++++++ core/playerop.cpp | 1109 +++++ core/premake/dll.lua | 232 ++ core/premake/lua.lua | 25 + core/premake/migrate.sql | 20 + core/premake5.lua | 38 + core/processor.cpp | 5272 ++++++++++++++++++++++++ core/scriptlib.cpp | 83 + core/scriptlib.h | 728 ++++ core/sort.h | 10 + 47 files changed, 40899 insertions(+) create mode 100644 core/.github/workflows/build.yml create mode 100644 core/.gitignore create mode 100644 core/.gitlab-ci.yml create mode 100644 core/CMakeLists.txt create mode 100644 core/LICENSE create mode 100644 core/LuaMemTracker.cpp create mode 100644 core/LuaMemTracker.h create mode 100644 core/README.md create mode 100644 core/buffer.h create mode 100644 core/card.cpp create mode 100644 core/card.h create mode 100644 core/card_data.h create mode 100644 core/common.h create mode 100644 core/duel.cpp create mode 100644 core/duel.h create mode 100644 core/effect.cpp create mode 100644 core/effect.h create mode 100644 core/effectset.h create mode 100644 core/emscripten_shutdown.cpp create mode 100644 core/field.cpp create mode 100644 core/field.h create mode 100644 core/group.cpp create mode 100644 core/group.h create mode 100644 core/interpreter.cpp create mode 100644 core/interpreter.h create mode 100644 core/libcard.cpp create mode 100644 core/libdebug.cpp create mode 100644 core/libduel.cpp create mode 100644 core/libeffect.cpp create mode 100644 core/libgroup.cpp create mode 100644 core/lua_coverage.cpp create mode 100644 core/lua_coverage.h create mode 100644 core/metadata.cpp create mode 100644 core/metadata.h create mode 100644 core/mtrandom.h create mode 100644 core/ocgapi.cpp create mode 100644 core/ocgapi.h create mode 100644 core/operations.cpp create mode 100644 core/playerop.cpp create mode 100644 core/premake/dll.lua create mode 100644 core/premake/lua.lua create mode 100644 core/premake/migrate.sql create mode 100644 core/premake5.lua create mode 100644 core/processor.cpp create mode 100644 core/scriptlib.cpp create mode 100644 core/scriptlib.h create mode 100644 core/sort.h diff --git a/core/.github/workflows/build.yml b/core/.github/workflows/build.yml new file mode 100644 index 0000000..1ad5838 --- /dev/null +++ b/core/.github/workflows/build.yml @@ -0,0 +1,121 @@ +name: Automated Test Build + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build-windows: + runs-on: windows-2022 + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Download lua + id: lua + uses: mercury233/action-cache-download-file@0e2b8bab566270adae95bd3cd298744402b667eb # v1.1.0 + with: + url: https://www.lua.org/ftp/lua-5.4.8.tar.gz + + - name: Lua + run: | + tar -xzf ${{ steps.lua.outputs.filepath }} + move lua-5.4.8 lua + + - name: Download premake + id: premake + uses: mercury233/action-cache-download-file@0e2b8bab566270adae95bd3cd298744402b667eb + with: + url: https://github.com/premake/premake-core/releases/download/v5.0.0-beta7/premake-5.0.0-beta7-windows.zip + + - name: Premake + run: | + mkdir premake-5 + tar -C premake-5 -xzf ${{ steps.premake.outputs.filepath }} + copy premake\lua.lua lua\premake5.lua + copy premake\dll.lua dll.lua + .\premake-5\premake5.exe vs2022 --file=dll.lua + + - name: Add msbuild to PATH + uses: microsoft/setup-msbuild@v3 + + - name: Build x32 + run: | + msbuild build\ocgcoredll.sln /m /t:Build /p:"Configuration=Release;Platform=Win32" + + - name: Build x64 + run: | + msbuild build\ocgcoredll.sln /m /t:Build /p:"Configuration=Release;Platform=x64" + + - name: Upload build artifacts + uses: actions/upload-artifact@v6 + with: + name: ocgcore-windows + path: | + build/bin/x32/Release/ocgcore.dll + build/bin/x64/Release/ocgcore.dll + + build-linux: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Install Dependencies + run: | + sudo apt-get update + sudo apt-get install -y gcc-multilib g++-multilib + + - name: Download lua + id: lua + uses: mercury233/action-cache-download-file@0e2b8bab566270adae95bd3cd298744402b667eb + with: + url: https://www.lua.org/ftp/lua-5.4.8.tar.gz + + - name: Lua + run: | + tar -xzf ${{ steps.lua.outputs.filepath }} + mv lua-5.4.8 lua + + - name: Download premake + id: premake + uses: mercury233/action-cache-download-file@0e2b8bab566270adae95bd3cd298744402b667eb + with: + url: https://github.com/premake/premake-core/releases/download/v5.0.0-beta7/premake-5.0.0-beta7-linux.tar.gz + + - name: Premake + run: | + mkdir premake-5 + tar -C premake-5 -xzf ${{ steps.premake.outputs.filepath }} + cp premake/lua.lua lua/premake5.lua + cp premake/dll.lua dll.lua + chmod +x premake-5/premake5 + ./premake-5/premake5 gmake --file=dll.lua + + - name: Build x32 + run: | + cd build + make -j $(nproc) config=release_x32 + cd .. + + - name: Build x64 + run: | + cd build + make -j $(nproc) config=release_x64 + cd .. + + - name: Upload build artifacts + uses: actions/upload-artifact@v6 + with: + name: ocgcore-linux + path: | + build/bin/x32/Release/libocgcore.so + build/bin/x64/Release/libocgcore.so diff --git a/core/.gitignore b/core/.gitignore new file mode 100644 index 0000000..d6761b8 --- /dev/null +++ b/core/.gitignore @@ -0,0 +1,15 @@ +/.vscode/ +/build/ +/lua +/temp/ +/.emsdk/ +/premake-5/ +/premake5.exe +/premake5 + +/dll.lua +/*.sh +/test-wasm +/wasm +/dist +/research diff --git a/core/.gitlab-ci.yml b/core/.gitlab-ci.yml new file mode 100644 index 0000000..540916b --- /dev/null +++ b/core/.gitlab-ci.yml @@ -0,0 +1,269 @@ +stages: + - prepare + - build + - combine + - deploy + +variables: + GIT_DEPTH: "1" + +.no_888: + except: + - '888' + +mat_common: + stage: prepare + tags: + - linux + script: + # lua + - wget -O - https://mat-cacher.moenext.com/https://www.lua.org/ftp/lua-5.4.8.tar.gz | tar zfx - + - mv lua-5.4.8 lua + - cp premake/lua.lua lua/premake5.lua + - wget -O - https://mat-cacher.moenext.com/https://www.sqlite.org/2026/sqlite-autoconf-3510300.tar.gz | tar zfx - + - mv sqlite-autoconf-3510300 sqlite3 + artifacts: + paths: + - lua + - sqlite3 + +mat_windows: + extends: .no_888 + stage: prepare + tags: + - linux + script: + # premake5.exe + - wget https://cdn02.moecube.com:444/premake5-built/premake-5.0.0-beta8-windows.zip + - 7z x -y premake-5.0.0-beta8-windows.zip + artifacts: + paths: + - premake5.exe + +mat_macos: + extends: .no_888 + stage: prepare + tags: + - linux + script: + - wget -O - https://cdn02.moecube.com:444/premake5-built/premake-5.0.0-beta8-macosx.tar.gz | tar zfx - + - chmod +x premake5 + artifacts: + paths: + - premake5 + +.build: + extends: .no_888 + stage: build + dependencies: + - mat_common + artifacts: + paths: + - dist + +.build_unix: + extends: .build + tags: + - noavx2 + image: git-registry.moenext.com/mycard/docker-ygopro-builder:debian11 + variables: + PREMAKE_OS: linux + BUILD_TYPE: x64 + PREMAKE5_PATH: premake5 + script: + - ln -sf premake/dll.lua . + - $PREMAKE5_PATH gmake --file=dll.lua --os=$PREMAKE_OS --sqlite3-dir=sqlite3 + - cd build + - make config=release_${BUILD_TYPE} -j$(nproc) + - cd .. + - mkdir -p dist/$BUILD_TYPE + - cp build/bin/$BUILD_TYPE/Release/libocgcore.* dist/$BUILD_TYPE/ + - cp build/bin/$BUILD_TYPE/Release/libsqlite3.* dist/$BUILD_TYPE/ || true + +build_linux_x64: + extends: .build_unix + +build_android: + extends: .build_unix + variables: + BUILD_TYPE: android_arm64 + NDK_DIR: android-ndk-r23b + ANDROID_API_LEVEL: '26' + before_script: + - wget -O android-ndk-r23b-linux.zip https://mat-cacher.moenext.com/https://dl.google.com/android/repository/android-ndk-r23b-linux.zip + - 7z x -y android-ndk-r23b-linux.zip + +build_linux_x64_no_longjmp: + extends: .build_unix + except: [] + only: + - '888' + variables: + NO_LONGJMP: '1' + +build_linux_x32: + extends: .build_unix + variables: + BUILD_TYPE: x32 + before_script: + - apt update; apt -y install gcc-multilib g++-multilib + +build_wasm_cjs: + extends: .build_unix + except: [] + variables: + PREMAKE_OS: emscripten + BUILD_TYPE: wasm_cjs + image: git-registry.moenext.com/mycard/docker-ygopro-builder:debian12 + +build_wasm_esm: + extends: .build_unix + variables: + PREMAKE_OS: emscripten + BUILD_TYPE: wasm_esm + image: git-registry.moenext.com/mycard/docker-ygopro-builder:debian12 + +.build_windows: + extends: .build + dependencies: + - mat_common + - mat_windows + tags: + - vs + before_script: + - copy premake\dll.lua dll.lua + - '.\premake5.exe vs2026 --file=dll.lua --sqlite3-dir=sqlite3' + - mkdir dist + +build_windows_x32: + extends: .build_windows + script: + - cmd /c '"C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Current\Bin\msbuild.exe" build\ocgcoredll.slnx /t:Build /p:Configuration=Release /p:Platform=Win32' + - mkdir dist\x32 + - copy build\bin\x32\Release\ocgcore.dll dist\x32\ocgcore.dll + - copy build\bin\x32\Release\sqlite3.dll dist\x32\sqlite3.dll + +build_windows_x64: + extends: .build_windows + script: + - cmd /c '"C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Current\Bin\msbuild.exe" build\ocgcoredll.slnx /t:Build /p:Configuration=Release /p:Platform=x64' + - mkdir dist\x64 + - copy build\bin\x64\Release\ocgcore.dll dist\x64\ocgcore.dll + - copy build\bin\x64\Release\sqlite3.dll dist\x64\sqlite3.dll + +.build_macos_arch: + extends: .build_unix + tags: + - macos + dependencies: + - mat_common + - mat_macos + variables: + PREMAKE_OS: macosx + PREMAKE5_PATH: ./premake5 + before_script: + - rm -rf sqlite3/VERSION sqlite3/version + +build_macos_x64: + extends: .build_macos_arch + variables: + BUILD_TYPE: x64 + +build_macos_m1: + extends: .build_macos_arch + tags: + - macos-m1 + variables: + BUILD_TYPE: arm64 + +build_macos: + extends: .no_888 + stage: combine + dependencies: + - build_macos_x64 + - build_macos_m1 + tags: + - macos + script: + - mkdir -p dist/macos + - lipo -create -output dist/macos/libocgcore.dylib dist/x64/libocgcore.dylib dist/arm64/libocgcore.dylib + - lipo -create -output dist/macos/libsqlite3.dylib dist/x64/libsqlite3.dylib dist/arm64/libsqlite3.dylib + - rm -rf dist/x64 dist/arm64 + artifacts: + paths: + - dist + +upload_to_minio: + stage: deploy + dependencies: + - build_android + - build_linux_x32 + - build_linux_x64 + - build_windows_x32 + - build_windows_x64 + - build_macos + - build_wasm_cjs + - build_wasm_esm + tags: + - linux + variables: + MINIO_DIR: 'libocgcore-koishi' + script: + - aws s3 --endpoint=https://minio.moenext.com:9000 sync --delete dist/ s3://mycard/$MINIO_DIR + only: + - master + +upload_to_minio_develop: + extends: upload_to_minio + variables: + MINIO_DIR: 'libocgcore-koishi-develop' + only: + - develop + +.deploy_to_server: + stage: deploy + tags: + - linux + variables: + SERVER_USER: nanahira + SERVER_HOST: koishi.momobako.com + FROM_PATH: '' + SERVER_PATH: '' + script: + - apt update && apt -y install openssh-client rsync coreutils + - mkdir -p ~/.ssh + - chmod 700 ~/.ssh + - ssh-keyscan $SERVER_HOST >> ~/.ssh/known_hosts + - echo $NANAHIRA_SSH_KEY | base64 --decode > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/* + - rsync -4cavzP $FROM_PATH $SERVER_USER@$SERVER_HOST:$SERVER_PATH + +deploy_888_so: + extends: .deploy_to_server + dependencies: + - build_linux_x64_no_longjmp + variables: + FROM_PATH: 'dist/x64/libocgcore.so' + SERVER_PATH: '~/ygopro-super-pre/bin/release/' + only: + - '888' + +deploy_888_wasm: + extends: .deploy_to_server + dependencies: + - build_wasm_cjs + variables: + FROM_PATH: 'dist/wasm_cjs/' + SERVER_PATH: '~/srvpro2/888/wasm/' + only: + - '888' + +deploy_8888_wasm: + extends: .deploy_to_server + dependencies: + - build_wasm_cjs + variables: + FROM_PATH: 'dist/wasm_cjs/' + SERVER_PATH: '~/srvpro2/8888/wasm/' + only: + - 'develop' diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt new file mode 100644 index 0000000..e85d226 --- /dev/null +++ b/core/CMakeLists.txt @@ -0,0 +1,18 @@ +project (ocgcore) + +set (AUTO_FILES_RESULT) +AutoFiles("." "src" "\\.(cpp|c|h)$") + +if (MSVC) + include_directories ( ../lua ) +else () + include_directories ( ${LUA_INCLUDE_DIR} ) +endif () + +add_library (ocgcore STATIC ${AUTO_FILES_RESULT}) + +if (MSVC) + target_link_libraries (ocgcore lua) +else () + target_link_libraries (ocgcore ${LUA_LIBRARIES}) +endif () diff --git a/core/LICENSE b/core/LICENSE new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/core/LICENSE @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/core/LuaMemTracker.cpp b/core/LuaMemTracker.cpp new file mode 100644 index 0000000..d50ccbd --- /dev/null +++ b/core/LuaMemTracker.cpp @@ -0,0 +1,120 @@ +#include "LuaMemTracker.h" +#include + +LuaMemTracker::LuaMemTracker(size_t mem_limit) + : limit(mem_limit) { + lua_State* tmp_L = luaL_newstate(); // get default alloc + real_alloc = lua_getallocf(tmp_L, &real_ud); + lua_close(tmp_L); + +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE + time_t now = time(nullptr); + char filename[64]; + std::snprintf(filename, sizeof(filename), "memtrace-%ld.log", static_cast(now)); + + log_file = std::fopen(filename, "a"); + if (log_file) { + std::fprintf(log_file, "---- Lua memory tracking started ----\n"); + } +#endif +} + +LuaMemTracker::~LuaMemTracker() { +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE + if (log_file) { + std::fprintf(log_file, "---- Lua memory tracking ended ----\n"); + std::fclose(log_file); + log_file = nullptr; + } +#endif +} + +void* LuaMemTracker::AllocThunk(void* ud, void* ptr, size_t osize, size_t nsize) { + return static_cast(ud)->Alloc(ptr, osize, nsize); +} + +void* LuaMemTracker::Alloc(void* ptr, size_t osize, size_t nsize) { + auto shrink = [&](size_t dec) { + // Clamp subtraction to avoid size_t underflow. + if (dec > total_allocated) { + total_allocated = 0; + } else { + total_allocated -= dec; + } + }; + + if (nsize == 0) { // free + if (ptr) { + shrink(osize); + } + return real_alloc(real_ud, ptr, osize, nsize); + } + + // Unified path: + // - grow > 0: check limit using safe inequality, then alloc; on success add 'grow' + // - grow = 0: just alloc; accounting unchanged + // - shrink > 0: shrink first, alloc; on failure, rollback the shrink + auto do_alloc = [&](size_t grow_amount, size_t shrink_amount) -> void* { + if (grow_amount > 0 && limit) { + if (total_allocated >= limit || grow_amount > (limit - total_allocated)) { + return nullptr; + } + } + + auto result = real_alloc(real_ud, ptr, osize, nsize); + if (result) { + if (shrink_amount > 0) + shrink(shrink_amount); + if (grow_amount > 0) + total_allocated += grow_amount; +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE + write_log(); +#endif + } + + return result; + }; + + if (ptr) { + // realloc path: osize is the real old size + if (nsize > osize) { + // growth + size_t grow = nsize - osize; + return do_alloc(grow, 0); + } else if (nsize < osize) { + // shrink + size_t dec = osize - nsize; + return do_alloc(0, dec); + } else { + // no net change + return do_alloc(0, 0); + } + } else { + // malloc path: osize is a type tag; only count nsize + return do_alloc(nsize, 0); + } +} + +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE +void LuaMemTracker::write_log() { + if (!log_file) return; + + time_t now = time(nullptr); + struct tm* tm_info = localtime(&now); + + char time_buf[32]; + std::strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info); + + if (total_allocated > max_used) + max_used = total_allocated; + + if (limit) + std::fprintf(log_file, "%s | used = %zu bytes | max_used = %zu bytes | limit = %zu\n", + time_buf, total_allocated, max_used, limit); + else + std::fprintf(log_file, "%s | used = %zu bytes | max_used = %zu bytes | limit = unlimited\n", + time_buf, total_allocated, max_used); + + std::fflush(log_file); // make it write instantly +} +#endif diff --git a/core/LuaMemTracker.h b/core/LuaMemTracker.h new file mode 100644 index 0000000..b6def03 --- /dev/null +++ b/core/LuaMemTracker.h @@ -0,0 +1,38 @@ +#ifndef LUA_MEM_TRACKER_H +#define LUA_MEM_TRACKER_H + +#include +#include +#include + +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE +#include // time_t +#include // FILE*, fopen, fprintf +#endif + +class LuaMemTracker { +public: + LuaMemTracker(size_t mem_limit = 0); + ~LuaMemTracker(); + static void* AllocThunk(void* ud, void* ptr, size_t osize, size_t nsize); + void* Alloc(void* ptr, size_t osize, size_t nsize); + + size_t get_total() const { return total_allocated; } + size_t get_limit() const { return limit; } + +private: +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE + FILE* log_file = nullptr; + void write_log(); +#endif + + lua_Alloc real_alloc; + void* real_ud; + size_t total_allocated = 0; + size_t limit = 0; +#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE + size_t max_used = 0; // for logging purposes, to track peak memory usage +#endif +}; + +#endif // LUA_MEM_TRACKER_H diff --git a/core/README.md b/core/README.md new file mode 100644 index 0000000..7ddf434 --- /dev/null +++ b/core/README.md @@ -0,0 +1,79 @@ +# YGOPro script engine + +## Introduction +The core logic and lua script processor of YGOPro. This library can be made external of the project and used to power server technologies. It maintains a state engine that is manipulated by Lua scripts using manipulation functions it exposes. + +## Compiling +In most cases, what you want to compile is the main program of YGOPro. You should refer to the main YGOPro project's [wiki](https://github.com/Fluorohydride/ygopro/wiki). +If you want to compile the dynamic link library of this repository (ocgcore.dll), you can refer to [this script](https://github.com/Fluorohydride/ygopro-core/blob/master/.github/workflows/build.yml). + +For WebAssembly builds, use Emscripten 3.1.1 through 3.1.7. + +## Exposed Functions + +The 3 functions need to be provided to the core so it can get card and database information. +- `void set_script_reader(script_reader f);` +Interface provided returns scripts based on number that corresponds to a lua file, send in a string. + +- `void set_card_reader(card_reader f);` +Interface provided function that provides database information from the `datas` table of `cards.cdb`. + +- `void set_message_handler(message_handler f);` +Interface provided function that handles error messages. + +These functions create the game itself and then manipulate it. +- `intptr_t create_duel(uint_fast32_t seed);` +(Deprecated) Create an instance of `duel` with 1 uint32 PRNG seed. +This function is reserved for replay mode only. + +- `intptr_t create_duel_v2(uint32_t seed_sequence[]);` +Create an instance of `duel` with PRNG seed `seed_sequence[]` of length `SEED_COUNT`. + +- `void start_duel(intptr_t pduel, uint32_t options);` +Start the duel. + +- `void end_duel(intptr_t pduel);` +End the duel. + +- `void set_player_info(intptr_t pduel, int32_t playerid, int32_t lp, int32_t startcount, int32_t drawcount);` +Set up the duel information. + +- `void get_log_message(intptr_t pduel, char* buf);` + +- `int32_t get_message(intptr_t pduel, byte* buf);` + +- `int32_t process(intptr_t pduel);` +Do a game tick. + +- `void new_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position);` +Add a card to the duel state. + +- `void new_tag_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t location);` +Add a new card to the tag pool. + +- `int32_t query_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint32_t query_flag, byte* buf, int32_t use_cache);` +Get a card in a specific location. + +- `int32_t query_field_count(intptr_t pduel, uint8_t playerid, uint8_t location);` +Get the number of cards in a specific location. + +- `int32_t query_field_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint32_t query_flag, byte* buf, int32_t use_cache);` +Get all cards in some location. + +- `int32_t query_field_info(intptr_t pduel, byte* buf);` + +- `void set_responsei(intptr_t pduel, int32_t value);` + +- `void set_responseb(intptr_t pduel, byte* buf);` + +- `int32_t preload_script(intptr_t pduel, const char* script_name);` + +- `byte* default_script_reader(const char* script_name, int* len);` +The default script reader using `fread`. + +# Lua functions +- `libcard.cpp` +- `libdebug.cpp` +- `libduel.cpp` +- `libeffect.cpp` +- `libgroup.cpp` diff --git a/core/buffer.h b/core/buffer.h new file mode 100644 index 0000000..434285f --- /dev/null +++ b/core/buffer.h @@ -0,0 +1,46 @@ +#ifndef CORE_BUFFER_H +#define CORE_BUFFER_H + +#include +#include +#include + +inline void buffer_read_block(unsigned char*& p, void* dest, size_t size) { + std::memcpy(dest, p, size); + p += size; +} +template +inline T buffer_read(unsigned char*& p) { + T ret{}; + std::memcpy(&ret, p, sizeof(T)); + p += sizeof(T); + return ret; +} + +inline void buffer_write_block(unsigned char*& p, const void* src, size_t size) { + std::memcpy(p, src, size); + p += size; +} +template +inline void buffer_write(unsigned char*& p, const T& value) { + std::memcpy(p, &value, sizeof(T)); + p += sizeof(T); +} + +inline void vector_write_block(std::vector& buffer, const void* src, size_t size) { + auto* bytes = static_cast(src); + buffer.insert(buffer.end(), bytes, bytes + size); +} +template +inline void vector_write(std::vector& buffer, const T& value) { + vector_write_block(buffer, &value, sizeof(T)); +} + +inline void vector_fread(std::vector& buffer, FILE* fp) { + unsigned char temp[4096]{}; + while (size_t len = std::fread(temp, 1, sizeof temp, fp)) + vector_write_block(buffer, temp, len); + std::fclose(fp); +} + +#endif // !CORE_BUFFER_H diff --git a/core/card.cpp b/core/card.cpp new file mode 100644 index 0000000..5cc6ae3 --- /dev/null +++ b/core/card.cpp @@ -0,0 +1,4302 @@ +/* + * card.cpp + * + * Created on: 2010-5-7 + * Author: Argon + */ + +#include "card.h" +#include "field.h" +#include "effect.h" +#include "duel.h" +#include "group.h" +#include "interpreter.h" +#include "ocgapi.h" +#include "buffer.h" +#include + +bool card_sort::operator()(card* const& c1, card* const& c2) const { + return c1->cardid < c2->cardid; +} + +//millux +uint32_t card::get_ritual_type() { + if(current.location == LOCATION_SZONE && (data.type & TYPE_MONSTER)) + return data.type; + return get_type(); +} +uint32_t card::set_entity_code(uint32_t entity_code, bool remove_alias) { + card_data dat; + ::read_card(entity_code, &dat); + uint32_t code = dat.code; + if (!code) + return 0; + if (remove_alias && dat.alias) + dat.alias = 0; + data = dat; + pduel->write_buffer8(MSG_UPDATE_CARD); + pduel->write_buffer8(current.controler); + pduel->write_buffer8(current.location); + pduel->write_buffer8(current.sequence); + unsigned char query_buffer[0x1000]; + auto query_len = get_infos(query_buffer, 0xe81fff, 0); + pduel->write_buffer(query_buffer, query_len); + return code; +} +uint32_t card::get_summon_info() { + effect_set effects; + effect_set effects2; + uint32_t res = summon_info; + filter_effect(EFFECT_ADD_SUMMON_TYPE_KOISHI, &effects, FALSE); + filter_effect(EFFECT_REMOVE_SUMMON_TYPE_KOISHI, &effects); + filter_effect(EFFECT_CHANGE_SUMMON_TYPE_KOISHI, &effects2, FALSE); + filter_effect(EFFECT_CHANGE_SUMMON_LOCATION_KOISHI, &effects2); + for (int32_t i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_ADD_SUMMON_TYPE_KOISHI) + res |= (effects[i]->get_value(this) & 0xff00ffff); + else + res &= ~(effects[i]->get_value(this) & 0xff00ffff); + } + for (int32_t i = 0; i < effects2.size(); ++i) { + if (effects2[i]->code == EFFECT_CHANGE_SUMMON_TYPE_KOISHI) + res = (res & 0xff0000) | (effects2[i]->get_value(this) & 0xff00ffff); + else + res = ((effects2[i]->get_value(this) & 0xff) << 16) | (res & 0xff00ffff); + } + return res; +} + +bool card_state::is_location(uint32_t loc) const { + if((loc & LOCATION_FZONE) && location == LOCATION_SZONE && sequence == 5) + return true; + if((loc & LOCATION_PZONE) && location == LOCATION_SZONE && pzone) + return true; + if(location & loc) + return true; + return false; +} +void card_state::init_state() { + code = UINT32_MAX; + code2 = UINT32_MAX; + type = UINT32_MAX; + level = UINT32_MAX; + rank = UINT32_MAX; + link = UINT32_MAX; + lscale = UINT32_MAX; + rscale = UINT32_MAX; + attribute = UINT32_MAX; + race = UINT32_MAX; + attack = -1; + defense = -1; + base_attack = -1; + base_defense = -1; + controler = UINT8_MAX; + location = UINT8_MAX; + sequence = UINT8_MAX; + position = UINT8_MAX; + reason = UINT32_MAX; + reason_player = UINT8_MAX; +} +void query_cache::clear_cache() { + info_location = UINT32_MAX; + current_code = UINT32_MAX; + type = UINT32_MAX; + level = UINT32_MAX; + rank = UINT32_MAX; + link = UINT32_MAX; + attribute = UINT32_MAX; + race = UINT32_MAX; + attack = -1; + defense = -1; + base_attack = -1; + base_defense = -1; + reason = UINT32_MAX; + status = UINT32_MAX; + lscale = UINT32_MAX; + rscale = UINT32_MAX; + link_marker = UINT32_MAX; +} +bool card::card_operation_sort(card* c1, card* c2) { + duel* pduel = c1->pduel; + int32_t cp1 = c1->overlay_target ? c1->overlay_target->current.controler : c1->current.controler; + int32_t cp2 = c2->overlay_target ? c2->overlay_target->current.controler : c2->current.controler; + if(cp1 != cp2) { + if(cp1 == PLAYER_NONE || cp2 == PLAYER_NONE) + return cp1 < cp2; + if(pduel->game_field->infos.turn_player == 0) + return cp1 < cp2; + else + return cp1 > cp2; + } + if(c1->current.location != c2->current.location) + return c1->current.location < c2->current.location; + if(c1->current.location == LOCATION_OVERLAY) { + if(c1->overlay_target && c2->overlay_target && c1->overlay_target->current.sequence != c2->overlay_target->current.sequence) + return c1->overlay_target->current.sequence < c2->overlay_target->current.sequence; + else + return c1->current.sequence < c2->current.sequence; + } else if (c1->current.location == LOCATION_DECK && pduel->game_field->is_select_hide_deck_sequence(cp1)) { + // if deck reversed and the card being at the top, it should go first + if(pduel->game_field->core.deck_reversed) { + if(c1 == pduel->game_field->player[cp1].list_main.back()) + return false; + if(c2 == pduel->game_field->player[cp2].list_main.back()) + return true; + } + // faceup deck cards should go at the very first + auto c1_faceup = c1->current.position & POS_FACEUP; + auto c2_faceup = c2->current.position & POS_FACEUP; + if(c1_faceup || c2_faceup) { + if(c1_faceup && c2_faceup) + return c1->current.sequence > c2->current.sequence; + else + return c2_faceup; + } + // sort deck as card property + auto c1_type = c1->data.type & (TYPE_MONSTER | TYPE_SPELL | TYPE_TRAP); + auto c2_type = c2->data.type & (TYPE_MONSTER | TYPE_SPELL | TYPE_TRAP); + // monster should go before spell, and then trap + if(c1_type != c2_type) + return c1_type > c2_type; + if(c1_type & TYPE_MONSTER) { + if (c1->data.level != c2->data.level) + return c1->data.level < c2->data.level; + // TODO: more sorts here + } + if(c1->data.code != c2->data.code) + return c1->data.code > c2->data.code; + return c1->current.sequence > c2->current.sequence; + } else { + if(c1->current.location & (LOCATION_DECK | LOCATION_EXTRA | LOCATION_GRAVE | LOCATION_REMOVED)) + return c1->current.sequence > c2->current.sequence; + else + return c1->current.sequence < c2->current.sequence; + } +} +bool card::check_card_setcode(uint32_t code, uint32_t value) { + card_data dat; + ::read_card(code, &dat); + return dat.is_setcode(value); +} +void card::attacker_map::addcard(card* pcard) { + auto fid = pcard ? pcard->fieldid_r : 0; + auto pr = emplace(fid, mapped_type(pcard, 0)); + ++pr.first->second.second; +} +uint32_t card::attacker_map::findcard(card* pcard) { + auto fid = pcard ? pcard->fieldid_r : 0; + auto it = find(fid); + if(it == end()) + return 0; + else + return it->second.second; +} +card::card(duel* pd) + :pduel(pd) { + temp.init_state(); + current.controler = PLAYER_NONE; + to_leave_fromex = FALSE; +} +inline void update_cache(uint32_t tdata, uint32_t& cache, byte*& p, uint32_t& query_flag, uint32_t flag) { + if (tdata != cache) { + cache = tdata; + buffer_write(p, tdata); + } + else + query_flag &= ~flag; +} +int32_t card::get_infos(byte* buf, uint32_t query_flag, int32_t use_cache) { + byte* p = buf; + std::pair atk_def(-10, -10); + std::pair base_atk_def(-10, -10); + if ((query_flag & QUERY_ATTACK) || (query_flag & QUERY_DEFENSE)) { + atk_def = get_atk_def(); + } + if ((query_flag & QUERY_BASE_ATTACK) || (query_flag & QUERY_BASE_DEFENSE)) { + base_atk_def = get_base_atk_def(); + } + //first 8 bytes: data length, query flag + p += 2 * sizeof(uint32_t); + if (query_flag & QUERY_CODE) { + buffer_write(p, data.code); + } + if (query_flag & QUERY_POSITION) { + uint32_t tdata = get_public_info_location(); + buffer_write(p, tdata); + if (q_cache.info_location != tdata) { + q_cache.clear_cache(); + q_cache.info_location = tdata; + use_cache = 0; + } + } + if(!use_cache) { + if (query_flag & QUERY_ALIAS) { + uint32_t tdata = get_code(); + buffer_write(p, tdata); + q_cache.current_code = tdata; + } + if (query_flag & QUERY_TYPE) { + uint32_t tdata = get_type(); + buffer_write(p, tdata); + q_cache.type = tdata; + } + if (query_flag & QUERY_LEVEL) { + uint32_t tdata = get_level(); + buffer_write(p, tdata); + q_cache.level = tdata; + } + if (query_flag & QUERY_RANK) { + uint32_t tdata = get_rank(); + buffer_write(p, tdata); + q_cache.rank = tdata; + } + if (query_flag & QUERY_ATTRIBUTE) { + uint32_t tdata = get_attribute(); + buffer_write(p, tdata); + q_cache.attribute = tdata; + } + if (query_flag & QUERY_RACE) { + uint32_t tdata = get_race(); + buffer_write(p, tdata); + q_cache.race = tdata; + } + if (query_flag & QUERY_ATTACK) { + buffer_write(p, atk_def.first); + q_cache.attack = atk_def.first; + } + if (query_flag & QUERY_DEFENSE) { + buffer_write(p, atk_def.second); + q_cache.defense = atk_def.second; + } + if (query_flag & QUERY_BASE_ATTACK) { + buffer_write(p, base_atk_def.first); + q_cache.base_attack = base_atk_def.first; + } + if (query_flag & QUERY_BASE_DEFENSE) { + buffer_write(p, base_atk_def.second); + q_cache.base_defense = base_atk_def.second; + } + if (query_flag & QUERY_REASON) { + buffer_write(p, current.reason); + q_cache.reason = current.reason; + } + } + else { + if((query_flag & QUERY_ALIAS)) { + uint32_t tdata = get_code(); + update_cache(tdata, q_cache.current_code, p, query_flag, QUERY_ALIAS); + } + if((query_flag & QUERY_TYPE)) { + uint32_t tdata = get_type(); + update_cache(tdata, q_cache.type, p, query_flag, QUERY_TYPE); + } + if((query_flag & QUERY_LEVEL)) { + uint32_t tdata = get_level(); + update_cache(tdata, q_cache.level, p, query_flag, QUERY_LEVEL); + } + if((query_flag & QUERY_RANK)) { + uint32_t tdata = get_rank(); + update_cache(tdata, q_cache.rank, p, query_flag, QUERY_RANK); + } + if((query_flag & QUERY_ATTRIBUTE)) { + uint32_t tdata = get_attribute(); + update_cache(tdata, q_cache.attribute, p, query_flag, QUERY_ATTRIBUTE); + } + if((query_flag & QUERY_RACE)) { + uint32_t tdata = get_race(); + update_cache(tdata, q_cache.race, p, query_flag, QUERY_RACE); + } + if((query_flag & QUERY_ATTACK)) { + if (atk_def.first != q_cache.attack) { + q_cache.attack = atk_def.first; + buffer_write(p, atk_def.first); + } + else + query_flag &= ~QUERY_ATTACK; + } + if((query_flag & QUERY_DEFENSE)) { + if (atk_def.second != q_cache.defense) { + q_cache.defense = atk_def.second; + buffer_write(p, atk_def.second); + } + else + query_flag &= ~QUERY_DEFENSE; + } + if((query_flag & QUERY_BASE_ATTACK)) { + if (base_atk_def.first != q_cache.base_attack) { + q_cache.base_attack = base_atk_def.first; + buffer_write(p, base_atk_def.first); + } + else + query_flag &= ~QUERY_BASE_ATTACK; + } + if((query_flag & QUERY_BASE_DEFENSE)) { + if (base_atk_def.second != q_cache.base_defense) { + q_cache.base_defense = base_atk_def.second; + buffer_write(p, base_atk_def.second); + } + else + query_flag &= ~QUERY_BASE_DEFENSE; + } + if((query_flag & QUERY_REASON)) { + uint32_t tdata = current.reason; + update_cache(tdata, q_cache.reason, p, query_flag, QUERY_REASON); + } + } + if (query_flag & QUERY_REASON_CARD) { + uint32_t tdata = current.reason_card ? current.reason_card->get_info_location() : 0; + buffer_write(p, tdata); + } + if(query_flag & QUERY_EQUIP_CARD) { + if (equiping_target) { + uint32_t tdata = equiping_target->get_info_location(); + buffer_write(p, tdata); + } + else + query_flag &= ~QUERY_EQUIP_CARD; + } + if(query_flag & QUERY_TARGET_CARD) { + buffer_write(p, (int32_t)effect_target_cards.size()); + for (auto& pcard : effect_target_cards) { + uint32_t tdata = pcard->get_info_location(); + buffer_write(p, tdata); + } + } + if(query_flag & QUERY_OVERLAY_CARD) { + buffer_write(p, (int32_t)xyz_materials.size()); + for (auto& xcard : xyz_materials) { + buffer_write(p, xcard->data.code); + } + } + if(query_flag & QUERY_COUNTERS) { + buffer_write(p, (int32_t)counters.size()); + for (const auto& cmit : counters) { + uint32_t tdata = cmit.first | ((uint32_t)cmit.second << 16); + buffer_write(p, tdata); + } + } + if (query_flag & QUERY_OWNER) { + int32_t tdata = owner; + buffer_write(p, tdata); + } + if(query_flag & QUERY_STATUS) { + uint32_t tdata = status & (STATUS_DISABLED | STATUS_FORBIDDEN | STATUS_PROC_COMPLETE); + if(!use_cache || (tdata != q_cache.status)) { + q_cache.status = tdata; + buffer_write(p, tdata); + } + else + query_flag &= ~QUERY_STATUS; + } + if(!use_cache) { + if (query_flag & QUERY_LSCALE) { + uint32_t tdata = get_lscale(); + buffer_write(p, tdata); + q_cache.lscale = tdata; + } + if (query_flag & QUERY_RSCALE) { + uint32_t tdata = get_rscale(); + buffer_write(p, tdata); + q_cache.rscale = tdata; + } + if(query_flag & QUERY_LINK) { + uint32_t tdata = get_link(); + buffer_write(p, tdata); + q_cache.link = tdata; + tdata = get_link_marker(); + buffer_write(p, tdata); + q_cache.link_marker = tdata; + } + } + else { + if((query_flag & QUERY_LSCALE)) { + uint32_t tdata = get_lscale(); + update_cache(tdata, q_cache.lscale, p, query_flag, QUERY_LSCALE); + } + if((query_flag & QUERY_RSCALE)) { + uint32_t tdata = get_rscale(); + update_cache(tdata, q_cache.rscale, p, query_flag, QUERY_RSCALE); + } + if(query_flag & QUERY_LINK) { + uint32_t link = get_link(); + uint32_t link_marker = get_link_marker(); + if((link != q_cache.link) || (link_marker != q_cache.link_marker)) { + q_cache.link = link; + buffer_write(p, link); + q_cache.link_marker = link_marker; + buffer_write(p, link_marker); + } + else + query_flag &= ~QUERY_LINK; + } + } + byte* finalize = buf; + buffer_write(finalize, (int32_t)(p - buf)); + buffer_write(finalize, query_flag); + return (int32_t)(p - buf); +} +uint32_t card::get_info_location() const { + if(overlay_target) { + uint32_t c = overlay_target->current.controler; + uint32_t l = overlay_target->current.location | LOCATION_OVERLAY; + uint32_t s = overlay_target->current.sequence; + uint32_t ss = current.sequence; + return c | (l << 8) | (s << 16) | (ss << 24); + } else { + uint32_t c = current.controler; + uint32_t l = current.location; + uint32_t s = current.sequence; + uint32_t ss = current.position; + return c | (l << 8) | (s << 16) | (ss << 24); + } +} +uint32_t card::get_public_info_location() { + uint32_t info = get_info_location(); + if((current.location & LOCATION_ONFIELD) && is_affected_by_effect(EFFECT_REVEAL_ONFIELD)) + info |= (static_cast(POS_REVEAL) << 24); + return info; +} +// get the printed code on card +uint32_t card::get_original_code() const { + return data.get_original_code(); +} +// get the original code in duel (can be different from printed code) +std::tuple card::get_original_code_rule() const { + auto it = second_code.find(data.get_original_code()); + if (it != second_code.end()) { + return std::make_tuple(data.get_original_code(), it->second); + } + return std::make_tuple(data.get_duel_code(), 0); +} +// return: the current card name +// for double-name cards, it returns printed name +uint32_t card::get_code() { + if(assume_type == ASSUME_CODE) + return assume_value; + if(temp.code != UINT32_MAX) // prevent recursion, return the former value + return temp.code; + effect_set effects; + uint32_t code = std::get<0>(get_original_code_rule()); + temp.code = code; + filter_effect(EFFECT_CHANGE_CODE, &effects); + if (effects.size()) + code = effects.back()->get_value(this); + temp.code = UINT32_MAX; + return code; +} +// return: the current second card name +// for double-name cards, it returns the name in description +uint32_t card::get_another_code() { + uint32_t code1 = get_code(); + if (is_affected_by_effect(EFFECT_CHANGE_CODE)) { + auto it = second_code.find(code1); + if (it != second_code.end()) + return it->second; + else + return 0; + } + uint32_t code2 = std::get<1>(get_original_code_rule()); + effect_set eset; + filter_effect(EFFECT_ADD_CODE, &eset); + if(!eset.size()) + return code2; + uint32_t otcode = eset.back()->get_value(this); + if(code1 != otcode) + return otcode; + return 0; +} +int32_t card::is_set_card(uint32_t set_code) { + uint32_t code1 = get_code(); + card_data dat1; + if (code1 == data.code) { + if (data.is_setcode(set_code)) + return TRUE; + } + else { + if (check_card_setcode(code1, set_code)) + return TRUE; + } + uint32_t code2 = get_another_code(); + if (code2 && check_card_setcode(code2, set_code)) + return TRUE; + //add set code + effect_set eset; + filter_effect(EFFECT_ADD_SETCODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t value = eset[i]->get_value(this); + uint16_t new_setcode = value & 0xffff; + if (check_setcode(new_setcode, set_code)) + return TRUE; + } + return FALSE; +} +int32_t card::is_origin_set_card(uint32_t set_code) { + if (data.is_setcode(set_code)) + return TRUE; + uint32_t code2 = std::get<1>(get_original_code_rule()); + if (code2 && check_card_setcode(code2, set_code)) + return TRUE; + return FALSE; +} +int32_t card::is_pre_set_card(uint32_t set_code) { + uint32_t code = previous.code; + if (code == data.code) { + if (data.is_setcode(set_code)) + return TRUE; + } + else { + if (check_card_setcode(code, set_code)) + return TRUE; + } + uint32_t code2 = previous.code2; + if (code2 && check_card_setcode(code2, set_code)) + return TRUE; + //add set code + for(auto& presetcode : previous.setcode) { + if (check_setcode(presetcode, set_code)) + return TRUE; + } + return FALSE; +} +int32_t card::is_fusion_set_card(uint32_t set_code) { + if(is_set_card(set_code)) + return TRUE; + if(pduel->game_field->core.not_material) + return FALSE; + effect_set eset; + filter_effect(EFFECT_ADD_FUSION_CODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t code = eset[i]->get_value(this); + if (check_card_setcode(code, set_code)) + return TRUE; + } + eset.clear(); + filter_effect(EFFECT_ADD_FUSION_SETCODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t value = eset[i]->get_value(this); + uint16_t new_setcode = value & 0xffff; + if (check_setcode(new_setcode, set_code)) + return TRUE; + } + return FALSE; +} +int32_t card::is_link_set_card(uint32_t set_code) { + if(is_set_card(set_code)) + return TRUE; + effect_set eset; + filter_effect(EFFECT_ADD_LINK_CODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t code = eset[i]->get_value(this); + if (check_card_setcode(code, set_code)) + return TRUE; + } + return FALSE; +} +int32_t card::is_special_summon_set_card(uint32_t set_code) { + uint32_t code = spsummon.code; + if (check_card_setcode(code, set_code)) + return TRUE; + uint32_t code2 = spsummon.code2; + if (code2 && check_card_setcode(code2, set_code)) + return TRUE; + //add set code + for(auto& spsetcode : spsummon.setcode) { + if (check_setcode(spsetcode, set_code)) + return TRUE; + } + return FALSE; +} +uint32_t card::get_type() { + if(assume_type == ASSUME_TYPE) + return assume_value; + if(!(current.location & (LOCATION_ONFIELD | LOCATION_HAND | LOCATION_GRAVE))) + return data.type; + if(current.is_location(LOCATION_PZONE)) + return TYPE_PENDULUM + TYPE_SPELL; + if(temp.type != UINT32_MAX) // prevent recursion, return the former value + return temp.type; + effect_set effects; + int32_t type = data.type; + temp.type = data.type; + filter_effect(EFFECT_ADD_TYPE, &effects, FALSE); + filter_effect(EFFECT_REMOVE_TYPE, &effects, FALSE); + filter_effect(EFFECT_CHANGE_TYPE, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_ADD_TYPE) + type |= effects[i]->get_value(this); + else if (effects[i]->code == EFFECT_REMOVE_TYPE) + type &= ~(effects[i]->get_value(this)); + else + type = effects[i]->get_value(this); + temp.type = type; + } + temp.type = UINT32_MAX; + if (data.type & TYPE_TOKEN) + type |= TYPE_TOKEN; + return type; +} +uint32_t card::get_fusion_type() { + if(current.location == LOCATION_SZONE && (data.type & TYPE_MONSTER) && !pduel->game_field->core.not_material) + return data.type; + return get_type(); +} +uint32_t card::get_synchro_type() { + if(current.location == LOCATION_SZONE && (data.type & TYPE_MONSTER)) + return data.type; + return get_type(); +} +uint32_t card::get_xyz_type() { + if(current.location == LOCATION_SZONE && (data.type & TYPE_MONSTER)) + return data.type; + return get_type(); +} +uint32_t card::get_link_type() { + if(current.location == LOCATION_SZONE) + return data.type | TYPE_MONSTER; + return get_type(); +} +std::pair card::get_base_atk_def() { + if (!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER) && !is_affected_by_effect(EFFECT_PRE_MONSTER)) + return std::pair(0, 0); + std::pair ret(data.attack, 0); + if (!(data.type & TYPE_LINK)) + ret.second = data.defense; + if (current.location != LOCATION_MZONE || get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP)) + return ret; + if (temp.base_attack != -1){ + ret.first = temp.base_attack; + ret.second = temp.base_defense; + return ret; + } + int32_t batk = data.attack; + if (batk < 0) + batk = 0; + int32_t bdef = 0; + if (!(data.type & TYPE_LINK)) { + bdef = data.defense; + if (bdef < 0) + bdef = 0; + } + temp.base_attack = batk; + temp.base_defense = bdef; + effect_set eset; + filter_effect(EFFECT_SET_BASE_ATTACK, &eset, FALSE); + filter_effect(EFFECT_SET_BASE_ATTACK_FINAL, &eset, FALSE); + if (!(data.type & TYPE_LINK)) { + filter_effect(EFFECT_SWAP_BASE_AD, &eset, FALSE); + filter_effect(EFFECT_SET_BASE_DEFENSE, &eset, FALSE); + filter_effect(EFFECT_SET_BASE_DEFENSE_FINAL, &eset, FALSE); + } + std::sort(eset.begin(), eset.end(), effect_sort_id); + // calculate single effects of this first + for(effect_set::size_type i = 0; i < eset.size();) { + if (eset[i]->type & EFFECT_TYPE_SINGLE) { + switch(eset[i]->code) { + case EFFECT_SET_BASE_ATTACK: + batk = eset[i]->get_value(this); + if(batk < 0) + batk = 0; + temp.base_attack = batk; + eset.erase(eset.begin() + i); + continue; + case EFFECT_SET_BASE_DEFENSE: + bdef = eset[i]->get_value(this); + if(bdef < 0) + bdef = 0; + temp.base_defense = bdef; + eset.erase(eset.begin() + i); + continue; + } + } + ++i; + } + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + switch(eset[i]->code) { + case EFFECT_SET_BASE_ATTACK: + case EFFECT_SET_BASE_ATTACK_FINAL: + batk = eset[i]->get_value(this); + if(batk < 0) + batk = 0; + break; + case EFFECT_SET_BASE_DEFENSE: + case EFFECT_SET_BASE_DEFENSE_FINAL: + bdef = eset[i]->get_value(this); + if(bdef < 0) + bdef = 0; + break; + case EFFECT_SWAP_BASE_AD: + std::swap(batk, bdef); + break; + } + temp.base_attack = batk; + temp.base_defense = bdef; + } + ret.first = batk; + ret.second = bdef; + temp.base_attack = -1; + temp.base_defense = -1; + return ret; +} +std::pair card::get_atk_def() { + if (assume_type == ASSUME_ATTACK) + return std::pair(assume_value, 0); + if (assume_type == ASSUME_DEFENSE) { + if (data.type & TYPE_LINK) + return std::pair(0, 0); + else + return std::pair(0, assume_value); + } + if (!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER) && !is_affected_by_effect(EFFECT_PRE_MONSTER)) + return std::pair(0, 0); + std::pair ret(data.attack, 0); + if (!(data.type & TYPE_LINK)) + ret.second = data.defense; + if (current.location != LOCATION_MZONE || get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP)) + return ret; + if (temp.attack != -1) { + ret.first = temp.attack; + ret.second = temp.defense; + return ret; + } + int32_t atk = -1, def = -1; + int32_t up_atk = 0, upc_atk = 0; + int32_t up_def = 0, upc_def = 0; + bool swap_final = false; + effect_set eset; + effect_set effects_atk_final, effects_atk_wicked, effects_atk_option; + effect_set effects_def_final, effects_def_wicked, effects_def_option; + effect_set effects_repeat_update_atk, effects_repeat_update_def; + int32_t batk = data.attack; + if (batk < 0) + batk = 0; + int32_t bdef = 0; + if (!(data.type & TYPE_LINK)) { + bdef = data.defense; + if (bdef < 0) + bdef = 0; + } + temp.attack = batk; + temp.defense = bdef; + std::pair base_val = get_base_atk_def(); + atk = base_val.first; + def = base_val.second; + temp.attack = atk; + temp.defense = def; + filter_effect(EFFECT_UPDATE_ATTACK, &eset, FALSE); + filter_effect(EFFECT_SET_ATTACK, &eset, FALSE); + filter_effect(EFFECT_SET_ATTACK_FINAL, &eset, FALSE); + if (!(data.type & TYPE_LINK)) { + filter_effect(EFFECT_SWAP_AD, &eset, FALSE); + filter_effect(EFFECT_UPDATE_DEFENSE, &eset, FALSE); + filter_effect(EFFECT_SET_DEFENSE, &eset, FALSE); + filter_effect(EFFECT_SET_DEFENSE_FINAL, &eset, FALSE); + } + std::sort(eset.begin(), eset.end(), effect_sort_id); + bool rev = false; + if (is_affected_by_effect(EFFECT_REVERSE_UPDATE)) + rev = true; + for (effect_set::size_type i = 0; i < eset.size();) { + if (eset[i]->type & EFFECT_TYPE_SINGLE) { + switch (eset[i]->code) { + case EFFECT_SET_ATTACK: + atk = eset[i]->get_value(this); + if (atk < 0) + atk = 0; + temp.attack = atk; + eset.erase(eset.begin() + i); + continue; + case EFFECT_SET_DEFENSE: + def = eset[i]->get_value(this); + if (def < 0) + def = 0; + temp.defense = def; + eset.erase(eset.begin() + i); + continue; + } + } + ++i; + } + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + switch (eset[i]->code) { + case EFFECT_UPDATE_ATTACK: + if (eset[i]->is_flag(EFFECT_FLAG2_REPEAT_UPDATE)) + effects_repeat_update_atk.push_back(eset[i]); + else if ((eset[i]->type & EFFECT_TYPE_SINGLE) && !eset[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + up_atk += eset[i]->get_value(this); + else + upc_atk += eset[i]->get_value(this); + break; + case EFFECT_SET_ATTACK: + atk = eset[i]->get_value(this); + if (atk < 0) + atk = 0; + up_atk = 0; + break; + case EFFECT_SET_ATTACK_FINAL: + if ((eset[i]->type & EFFECT_TYPE_SINGLE) && !eset[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + atk = eset[i]->get_value(this); + if (atk < 0) + atk = 0; + up_atk = 0; + upc_atk = 0; + } + else if (eset[i]->is_flag(EFFECT_FLAG2_OPTION)) { + effects_atk_option.push_back(eset[i]); + } + else if (eset[i]->is_flag(EFFECT_FLAG2_WICKED)) { + effects_atk_wicked.push_back(eset[i]); + } + else { + effects_atk_final.push_back(eset[i]); + } + break; + // def + case EFFECT_UPDATE_DEFENSE: + if (eset[i]->is_flag(EFFECT_FLAG2_REPEAT_UPDATE)) + effects_repeat_update_def.push_back(eset[i]); + else if ((eset[i]->type & EFFECT_TYPE_SINGLE) && !eset[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + up_def += eset[i]->get_value(this); + else + upc_def += eset[i]->get_value(this); + break; + case EFFECT_SET_DEFENSE: + def = eset[i]->get_value(this); + if (def < 0) + def = 0; + up_def = 0; + break; + case EFFECT_SET_DEFENSE_FINAL: + if ((eset[i]->type & EFFECT_TYPE_SINGLE) && !eset[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + def = eset[i]->get_value(this); + if (def < 0) + def = 0; + up_def = 0; + upc_def = 0; + } + else if (eset[i]->is_flag(EFFECT_FLAG2_OPTION)) { + effects_def_option.push_back(eset[i]); + } + else if (eset[i]->is_flag(EFFECT_FLAG2_WICKED)) { + effects_def_wicked.push_back(eset[i]); + } + else { + effects_def_final.push_back(eset[i]); + } + break; + case EFFECT_SWAP_AD: + swap_final = !swap_final; + break; + } + if (!rev) { + temp.attack = atk + up_atk + upc_atk; + temp.defense = def + up_def + upc_def; + } + else { + temp.attack = atk - up_atk - upc_atk; + temp.defense = def - up_def - upc_def; + } + if (temp.attack < 0) + temp.attack = 0; + if (temp.defense < 0) + temp.defense = 0; + } + for (effect_set::size_type i = 0; i < effects_repeat_update_atk.size(); ++i) { + temp.attack += effects_repeat_update_atk[i]->get_value(this); + if (temp.attack < 0) + temp.attack = 0; + } + for (effect_set::size_type i = 0; i < effects_repeat_update_def.size(); ++i) { + temp.defense += effects_repeat_update_def[i]->get_value(this); + if (temp.defense < 0) + temp.defense = 0; + } + for (effect_set::size_type i = 0; i < effects_atk_final.size(); ++i) { + atk = effects_atk_final[i]->get_value(this); + if (atk < 0) + atk = 0; + temp.attack = atk; + } + for (effect_set::size_type i = 0; i < effects_def_final.size(); ++i){ + def = effects_def_final[i]->get_value(this); + if (def < 0) + def = 0; + temp.defense = def; + } + if (swap_final) + std::swap(temp.attack, temp.defense); + // The Wicked + for (effect_set::size_type i = 0; i < effects_atk_wicked.size(); ++i) { + atk = effects_atk_wicked[i]->get_value(this); + if (atk < 0) + atk = 0; + temp.attack = atk; + } + for (effect_set::size_type i = 0; i < effects_def_wicked.size(); ++i) { + def = effects_def_wicked[i]->get_value(this); + if (def < 0) + def = 0; + temp.defense = def; + } + // Gradius' Option + for (effect_set::size_type i = 0; i < effects_atk_option.size(); ++i) { + atk = effects_atk_option[i]->get_value(this); + if (atk < 0) + atk = 0; + temp.attack = atk; + } + for (effect_set::size_type i = 0; i < effects_def_option.size(); ++i) { + def = effects_def_option[i]->get_value(this); + if (def < 0) + def = 0; + temp.defense = def; + } + ret.first = temp.attack; + ret.second = temp.defense; + temp.attack = -1; + temp.defense = -1; + return ret; +} +int32_t card::get_base_attack() { + return get_base_atk_def().first; +} +int32_t card::get_attack() { + return get_atk_def().first; +} +int32_t card::get_base_defense() { + return get_base_atk_def().second; +} +int32_t card::get_defense() { + return get_atk_def().second; +} +int32_t card::get_battle_attack() { + effect_set eset; + filter_effect(EFFECT_SET_BATTLE_ATTACK, &eset); + if (eset.size()) { + int32_t atk = eset.back()->get_value(this); + if (atk < 0) + atk = 0; + return atk; + } + else + return get_atk_def().first; +} +int32_t card::get_battle_defense() { + effect_set eset; + filter_effect(EFFECT_SET_BATTLE_DEFENSE, &eset); + if (eset.size()) { + int32_t def = eset.back()->get_value(this); + if (def < 0) + def = 0; + return def; + } + else + return get_atk_def().second; +} +uint32_t card::get_level() { + if((data.type & (TYPE_XYZ | TYPE_LINK)) || (status & STATUS_NO_LEVEL) + || (!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER) && !is_affected_by_effect(EFFECT_PRE_MONSTER))) + return 0; + if(assume_type == ASSUME_LEVEL) + return assume_value; + if(temp.level != UINT32_MAX) // prevent recursion, return the former value + return temp.level; + effect_set effects; + int32_t level = data.level; + temp.level = level; + int32_t up = 0; + filter_effect(EFFECT_UPDATE_LEVEL, &effects, FALSE); + filter_effect(EFFECT_CHANGE_LEVEL, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + switch (effects[i]->code) { + case EFFECT_UPDATE_LEVEL: + up += effects[i]->get_value(this); + break; + case EFFECT_CHANGE_LEVEL: + level = effects[i]->get_value(this); + up = 0; + break; + } + temp.level = level + up; + } + level += up; + if (level < 1) + level = 1; + temp.level = UINT32_MAX; + return level; +} +uint32_t card::get_rank() { + if (!(data.type & TYPE_XYZ)) + return 0; + if(assume_type == ASSUME_RANK) + return assume_value; + //if(!(current.location & LOCATION_MZONE)) + // return data.level; + if(temp.level != UINT32_MAX) // prevent recursion, return the former value + return temp.level; + effect_set effects; + int32_t rank = data.level; + temp.level = rank; + int32_t up = 0; + filter_effect(EFFECT_UPDATE_RANK, &effects, FALSE); + filter_effect(EFFECT_CHANGE_RANK, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + switch (effects[i]->code) { + case EFFECT_UPDATE_RANK: + up += effects[i]->get_value(this); + break; + case EFFECT_CHANGE_RANK: + rank = effects[i]->get_value(this); + up = 0; + break; + } + temp.level = rank + up; + } + rank += up; + if (rank < 1) + rank = 1; + temp.level = UINT32_MAX; + return rank; +} +uint32_t card::get_link() { + if (!(data.type & TYPE_LINK)) + return 0; + return data.level; +} + +uint32_t card::get_mat_level_from_effect(card* pcard, uint32_t effect_code) { + if(!effect_code) + return 0; + effect_set eset; + filter_effect(effect_code, &eset); + for (auto& peffect : eset) { + uint32_t lev = peffect->get_value(pcard); + if (lev) + return lev; + } + return 0; +} +uint32_t card::get_mat_level(card* pcard, uint32_t level_effect_code, uint32_t allow_effect_code) { + if((data.type & (TYPE_XYZ | TYPE_LINK)) || (status & STATUS_NO_LEVEL)) + return get_mat_level_from_effect(pcard, allow_effect_code); + auto lv = get_mat_level_from_effect(pcard, level_effect_code); + if(lv) + return lv; + return get_level(); +} +uint32_t card::get_synchro_level(card* pcard) { + return get_mat_level(pcard, EFFECT_SYNCHRO_LEVEL, EFFECT_SYNCHRO_LEVEL_EX); +} +uint32_t card::get_ritual_level(card* pcard) { + return get_mat_level(pcard, EFFECT_RITUAL_LEVEL, EFFECT_RITUAL_LEVEL_EX); +} +uint32_t card::check_xyz_level(card* pcard, uint32_t lv) { + if(status & STATUS_NO_LEVEL) + return 0; + card* rcard = pduel->game_field->rose_card; + uint32_t rlv = pduel->game_field->rose_level; + if(rcard == this && rlv == lv) + return rlv; + int32_t min_count = 0; + effect_set mset; + filter_effect(EFFECT_XYZ_MIN_COUNT, &mset); + for (auto& peffect: mset) { + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + int32_t count = peffect->get_value(2); + if (count > min_count) + min_count = count; + } + if (min_count > 0xf) + min_count = 0xf; + effect_set eset; + filter_effect(EFFECT_XYZ_LEVEL, &eset); + if(!eset.size()) { + uint32_t card_lv = get_level(); + if (card_lv == lv) + return (card_lv & MAX_XYZ_LEVEL) | ((uint32_t)min_count << 12); + return 0; + } + for (auto& peffect: eset) { + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + uint32_t lev = peffect->get_value(2); + uint16_t lv1 = lev & MAX_XYZ_LEVEL; + uint16_t count1 = (lev & 0xf000) >> 12; + if (count1 < min_count) + count1 = min_count; + if (lv1 == lv) + return lv1 | ((uint32_t)count1 << 12); + lev >>= 16; + uint16_t lv2 = lev & MAX_XYZ_LEVEL; + uint16_t count2 = (lev & 0xf000) >> 12; + if (count2 < min_count) + count2 = min_count; + if (lv2 == lv) + return lv2 | ((uint32_t)count2 << 12); + } + return 0; +} +uint32_t card::get_attribute() { + if(assume_type == ASSUME_ATTRIBUTE) + return assume_value; + if(!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER) && !is_affected_by_effect(EFFECT_PRE_MONSTER)) + return 0; + if(temp.attribute != UINT32_MAX) // prevent recursion, return the former value + return temp.attribute; + effect_set effects; + auto attribute = data.attribute; + temp.attribute = data.attribute; + filter_effect(EFFECT_ADD_ATTRIBUTE, &effects, FALSE); + filter_effect(EFFECT_REMOVE_ATTRIBUTE, &effects, FALSE); + filter_effect(EFFECT_CHANGE_ATTRIBUTE, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_ADD_ATTRIBUTE) + attribute |= effects[i]->get_value(this); + else if (effects[i]->code == EFFECT_REMOVE_ATTRIBUTE) + attribute &= ~(effects[i]->get_value(this)); + else if (effects[i]->code == EFFECT_CHANGE_ATTRIBUTE) + attribute = effects[i]->get_value(this); + temp.attribute = attribute; + } + temp.attribute = UINT32_MAX; + return attribute; +} +uint32_t card::get_fusion_attribute(uint8_t playerid) { + effect_set effects; + filter_effect(EFFECT_CHANGE_FUSION_ATTRIBUTE, &effects); + if(!effects.size() || pduel->game_field->core.not_material) + return get_attribute(); + uint32_t attribute = 0; + for(effect_set::size_type i = 0; i < effects.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + attribute = effects[i]->get_value(this, 1); + } + return attribute; +} +uint32_t card::get_link_attribute(uint8_t playerid) { + effect_set effects; + filter_effect(EFFECT_ADD_LINK_ATTRIBUTE, &effects); + uint32_t attribute = get_attribute(); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + attribute |= effects[i]->get_value(this, 1); + } + return attribute; +} +uint32_t card::get_grave_attribute(uint8_t playerid) { + if(!(data.type & TYPE_MONSTER)) + return 0; + if(current.is_location(LOCATION_GRAVE)) + return get_attribute(); + uint32_t attribute = data.attribute; + effect_set eset; + pduel->game_field->filter_player_effect(playerid, EFFECT_CHANGE_GRAVE_ATTRIBUTE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + attribute = eset[i]->get_value(this); + else { + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(pduel->lua->check_condition(eset[i]->target, 2)) + attribute = eset[i]->get_value(this); + } + } + return attribute; +} +uint32_t card::get_race() { + if(assume_type == ASSUME_RACE) + return assume_value; + if(!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER) && !is_affected_by_effect(EFFECT_PRE_MONSTER)) + return 0; + if(temp.race != UINT32_MAX) // prevent recursion, return the former value + return temp.race; + effect_set effects; + auto race = data.race; + temp.race = data.race; + filter_effect(EFFECT_ADD_RACE, &effects, FALSE); + filter_effect(EFFECT_REMOVE_RACE, &effects, FALSE); + filter_effect(EFFECT_CHANGE_RACE, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_ADD_RACE) + race |= effects[i]->get_value(this); + else if (effects[i]->code == EFFECT_REMOVE_RACE) + race &= ~(effects[i]->get_value(this)); + else if (effects[i]->code == EFFECT_CHANGE_RACE) + race = effects[i]->get_value(this); + temp.race = race; + } + temp.race = UINT32_MAX; + return race; +} +uint32_t card::get_link_race(uint8_t playerid) { + effect_set effects; + filter_effect(EFFECT_ADD_LINK_RACE, &effects); + uint32_t race = get_race(); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + race |= effects[i]->get_value(this, 1); + } + return race; +} +uint32_t card::get_grave_race(uint8_t playerid) { + if(!(data.type & TYPE_MONSTER)) + return 0; + if(current.is_location(LOCATION_GRAVE)) + return get_race(); + uint32_t race = data.race; + effect_set eset; + pduel->game_field->filter_player_effect(playerid, EFFECT_CHANGE_GRAVE_RACE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + race = eset[i]->get_value(this); + else { + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(pduel->lua->check_condition(eset[i]->target, 2)) + race = eset[i]->get_value(this); + } + } + return race; +} +uint32_t card::get_lscale() { + if(!current.is_location(LOCATION_PZONE)) + return data.lscale; + if(temp.lscale != UINT32_MAX) // prevent recursion, return the former value + return temp.lscale; + effect_set effects; + int32_t lscale = data.lscale; + temp.lscale = data.lscale; + int32_t up = 0, upc = 0; + filter_effect(EFFECT_UPDATE_LSCALE, &effects, FALSE); + filter_effect(EFFECT_CHANGE_LSCALE, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_UPDATE_LSCALE) { + if ((effects[i]->type & EFFECT_TYPE_SINGLE) && !effects[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + up += effects[i]->get_value(this); + else + upc += effects[i]->get_value(this); + } else { + lscale = effects[i]->get_value(this); + up = 0; + } + temp.lscale = lscale; + } + lscale += up + upc; + if(lscale < 0 && current.pzone) + lscale = 0; + temp.lscale = UINT32_MAX; + return lscale; +} +uint32_t card::get_rscale() { + if(!current.is_location(LOCATION_PZONE)) + return data.rscale; + if(temp.rscale != UINT32_MAX) // prevent recursion, return the former value + return temp.rscale; + effect_set effects; + int32_t rscale = data.rscale; + temp.rscale = data.rscale; + int32_t up = 0, upc = 0; + filter_effect(EFFECT_UPDATE_RSCALE, &effects, FALSE); + filter_effect(EFFECT_CHANGE_RSCALE, &effects); + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + if (effects[i]->code == EFFECT_UPDATE_RSCALE) { + if ((effects[i]->type & EFFECT_TYPE_SINGLE) && !effects[i]->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + up += effects[i]->get_value(this); + else + upc += effects[i]->get_value(this); + } else { + rscale = effects[i]->get_value(this); + up = 0; + } + temp.rscale = rscale; + } + rscale += up + upc; + if(rscale < 0 && current.pzone) + rscale = 0; + temp.rscale = UINT32_MAX; + return rscale; +} +uint32_t card::get_link_marker() { + effect_set effects; + effect_set effects2; + uint32_t link_marker = data.link_marker; + if(!(data.type & TYPE_LINK) || current.location == LOCATION_SZONE) { + link_marker = 0; + effect_set effects3; + filter_effect(EFFECT_LINK_SPELL_KOISHI, &effects3); + if(!effects3.size()) + return 0; + for (int32_t i = 0; i < effects3.size(); ++i) { + card* ocard = effects3[i]->get_handler(); + if (!(effects3[i]->type & EFFECT_TYPE_FIELD) || !(ocard && ocard->to_leave_fromex)) + link_marker = effects3[i]->get_value(this); + } + } + filter_effect(EFFECT_ADD_LINK_MARKER_KOISHI, &effects, FALSE); + filter_effect(EFFECT_REMOVE_LINK_MARKER_KOISHI, &effects); + filter_effect(EFFECT_CHANGE_LINK_MARKER_KOISHI, &effects2); + for (int32_t i = 0; i < effects.size(); ++i) { + card* ocard = effects[i]->get_handler(); + if (effects[i]->code == EFFECT_ADD_LINK_MARKER_KOISHI && (!(effects[i]->type & EFFECT_TYPE_FIELD) || !(ocard && ocard->to_leave_fromex))) + link_marker |= effects[i]->get_value(this); + else if (effects[i]->code == EFFECT_REMOVE_LINK_MARKER_KOISHI && (!(effects[i]->type & EFFECT_TYPE_FIELD) || !(ocard && ocard->to_leave_fromex))) + link_marker &= ~(effects[i]->get_value(this)); + } + for (int32_t i = 0; i < effects2.size(); ++i) { + card* ocard = effects2[i]->get_handler(); + if (!(effects2[i]->type & EFFECT_TYPE_FIELD) || !(ocard && ocard->to_leave_fromex)) + link_marker = effects2[i]->get_value(this); + } + return link_marker; +} +uint32_t card::is_link_marker(uint32_t dir) { + return get_link_marker() & dir; +} +uint32_t card::get_linked_zone() { + if((!(data.type & TYPE_LINK) || current.location != LOCATION_MZONE || is_treated_as_not_on_field()) + && (!is_affected_by_effect(EFFECT_LINK_SPELL_KOISHI) || current.location != LOCATION_SZONE)) + return 0; + uint32_t zones = 0; + int32_t s = current.sequence; + if(current.location == LOCATION_SZONE) { + if(s > 4) + return 0; + if(is_link_marker(LINK_MARKER_TOP_LEFT) && s != 0) + zones |= 1u << (s - 1); + if(is_link_marker(LINK_MARKER_TOP)) + zones |= 1u << s; + if(is_link_marker(LINK_MARKER_TOP_RIGHT) && s != 4) + zones |= 1u << (s + 1); + } + if(s > 0 && s <= 4 && is_link_marker(LINK_MARKER_LEFT)) + zones |= 1u << (s - 1); + if(s <= 3 && is_link_marker(LINK_MARKER_RIGHT)) + zones |= 1u << (s + 1); + if((s == 0 && is_link_marker(LINK_MARKER_TOP_RIGHT)) + || (s == 1 && is_link_marker(LINK_MARKER_TOP)) + || (s == 2 && is_link_marker(LINK_MARKER_TOP_LEFT))) + zones |= (1u << 5) | (1u << (16 + 6)); + if((s == 2 && is_link_marker(LINK_MARKER_TOP_RIGHT)) + || (s == 3 && is_link_marker(LINK_MARKER_TOP)) + || (s == 4 && is_link_marker(LINK_MARKER_TOP_LEFT))) + zones |= (1u << 6) | (1u << (16 + 5)); + if(s == 5) { + if(is_link_marker(LINK_MARKER_BOTTOM_LEFT)) + zones |= 1u << 0; + if(is_link_marker(LINK_MARKER_BOTTOM)) + zones |= 1u << 1; + if(is_link_marker(LINK_MARKER_BOTTOM_RIGHT)) + zones |= 1u << 2; + if(is_link_marker(LINK_MARKER_TOP_LEFT)) + zones |= 1u << (16 + 4); + if(is_link_marker(LINK_MARKER_TOP)) + zones |= 1u << (16 + 3); + if(is_link_marker(LINK_MARKER_TOP_RIGHT)) + zones |= 1u << (16 + 2); + } + if(s == 6) { + if(is_link_marker(LINK_MARKER_BOTTOM_LEFT)) + zones |= 1u << 2; + if(is_link_marker(LINK_MARKER_BOTTOM)) + zones |= 1u << 3; + if(is_link_marker(LINK_MARKER_BOTTOM_RIGHT)) + zones |= 1u << 4; + if(is_link_marker(LINK_MARKER_TOP_LEFT)) + zones |= 1u << (16 + 2); + if(is_link_marker(LINK_MARKER_TOP)) + zones |= 1u << (16 + 1); + if(is_link_marker(LINK_MARKER_TOP_RIGHT)) + zones |= 1u << (16 + 0); + } + return zones; +} +void card::get_linked_cards(card_set* cset) { + cset->clear(); + if((!(data.type & TYPE_LINK) || current.location != LOCATION_MZONE) && (!is_affected_by_effect(EFFECT_LINK_SPELL_KOISHI) || current.location != LOCATION_SZONE)) + return; + int32_t p = current.controler; + uint32_t linked_zone = get_linked_zone(); + pduel->game_field->get_cards_in_zone(cset, linked_zone, p, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(cset, linked_zone >> 16, 1 - p, LOCATION_MZONE); +} +uint32_t card::get_mutual_linked_zone() { + if(!(data.type & TYPE_LINK) || current.location != LOCATION_MZONE || is_treated_as_not_on_field()) + return 0; + uint32_t zones = 0; + int32_t p = current.controler; + int32_t s = current.sequence; + uint32_t linked_zone = get_linked_zone(); + uint32_t icheck = 0x1U; + for(int32_t i = 0; i < 7; ++i, icheck <<= 1) { + if(icheck & linked_zone) { + card* pcard = pduel->game_field->player[p].list_mzone[i]; + if(pcard && (pcard->get_linked_zone() & (0x1u << s))) + zones |= icheck; + } + } + icheck = 0x10000U; + for(uint32_t i = 0; i < 7; ++i, icheck <<= 1) { + if(icheck & linked_zone) { + card* pcard = pduel->game_field->player[1 - p].list_mzone[i]; + if(pcard && (pcard->get_linked_zone() & (0x1u << (s + 16)))) + zones |= icheck; + } + } + return zones; +} +void card::get_mutual_linked_cards(card_set* cset) { + cset->clear(); + if(!(data.type & TYPE_LINK) || current.location != LOCATION_MZONE) + return; + int32_t p = current.controler; + uint32_t mutual_linked_zone = get_mutual_linked_zone(); + pduel->game_field->get_cards_in_zone(cset, mutual_linked_zone, p, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(cset, mutual_linked_zone >> 16, 1 - p, LOCATION_MZONE); +} +int32_t card::is_link_state() { + if(current.location != LOCATION_MZONE) + return FALSE; + card_set cset; + get_linked_cards(&cset); + if(cset.size()) + return TRUE; + int32_t p = current.controler; + uint32_t linked_zone = pduel->game_field->get_linked_zone(p); + if((linked_zone >> current.sequence) & 0x1U) + return TRUE; + return FALSE; +} +int32_t card::is_extra_link_state() { + if(current.location != LOCATION_MZONE) + return FALSE; + uint32_t checked = 0x1U << current.sequence; + uint32_t linked_zone = get_mutual_linked_zone(); + const auto& list_mzone0 = pduel->game_field->player[current.controler].list_mzone; + const auto& list_mzone1 = pduel->game_field->player[1 - current.controler].list_mzone; + while(true) { + if(((linked_zone >> 5) | (linked_zone >> (16 + 6))) & ((linked_zone >> 6) | (linked_zone >> (16 + 5))) & 0x1U) + return TRUE; + uint32_t checking = linked_zone & ~checked; + if(!checking) + return FALSE; + uint32_t rightmost = checking & (~checking + 1); + checked |= rightmost; + if(rightmost < 0x10000U) { + for(int32_t i = 0; i < 7; ++i) { + if(rightmost & 0x1U) { + card* pcard = list_mzone0[i]; + linked_zone |= pcard->get_mutual_linked_zone(); + break; + } + rightmost >>= 1; + } + } else { + rightmost >>= 16; + for(int32_t i = 0; i < 7; ++i) { + if(rightmost & 0x1U) { + card* pcard = list_mzone1[i]; + uint32_t zone = pcard->get_mutual_linked_zone(); + linked_zone |= (zone << 16) | (zone >> 16); + break; + } + rightmost >>= 1; + } + } + } + return FALSE; +} +int32_t card::is_position(uint32_t pos) const { + return current.position & pos; +} +void card::set_status(uint32_t x, int32_t enabled) { + if (enabled) + status |= x; + else + status &= ~x; +} +// get match status +int32_t card::get_status(uint32_t x) const { + return status & x; +} +// match all status +int32_t card::is_status(uint32_t x) const { + if ((status & x) == x) + return TRUE; + return FALSE; +} +uint32_t card::get_column_zone(int32_t location) { + int32_t zones = 0; + uint8_t seq = current.sequence; + if(!(location & LOCATION_ONFIELD) || !(current.location & LOCATION_ONFIELD) || current.location == LOCATION_SZONE && seq >= 5) + return 0; + if(seq <= 4) { + if(location & LOCATION_MZONE) { + if(!(current.location & LOCATION_MZONE)) + zones |= 1u << seq; + zones |= 1u << (16 + (4 - seq)); + if(seq == 1) + zones |= (1u << 5) | (1u << (16 + 6)); + if(seq == 3) + zones |= (1u << 6) | (1u << (16 + 5)); + } + if(location & LOCATION_SZONE) { + if(!(current.location & LOCATION_SZONE)) + zones |= 1u << (seq + 8); + zones |= 1u << (16 + 8 + (4 - seq)); + } + } + if(seq == 5) { + if(location & LOCATION_MZONE) + zones |= (1u << 1) | (1u << (16 + 3)); + if(location & LOCATION_SZONE) + zones |= (1u << (8 + 1)) | (1u << (16 + 8 + 3)); + } + if(seq == 6) { + if(location & LOCATION_MZONE) + zones |= (1u << 3) | (1u << (16 + 1)); + if(location & LOCATION_SZONE) + zones |= (1u << (8 + 3)) | (1u << (16 + 8 + 1)); + } + return zones; +} +void card::get_column_cards(card_set* cset) { + cset->clear(); + if(!(current.location & LOCATION_ONFIELD)) + return; + int32_t p = current.controler; + uint32_t column_mzone = get_column_zone(LOCATION_MZONE); + uint32_t column_szone = get_column_zone(LOCATION_SZONE); + pduel->game_field->get_cards_in_zone(cset, column_mzone, p, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(cset, column_mzone >> 16, 1 - p, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(cset, column_szone >> 8, p, LOCATION_SZONE); + pduel->game_field->get_cards_in_zone(cset, column_szone >> 24, 1 - p, LOCATION_SZONE); +} +int32_t card::is_all_column() { + if(!(current.location & LOCATION_ONFIELD)) + return FALSE; + card_set cset; + get_column_cards(&cset); + int32_t full = 3; + if(pduel->game_field->core.duel_rule >= NEW_MASTER_RULE && (current.sequence == 1 || current.sequence == 3)) + ++full; + if(cset.size() == full) + return TRUE; + return FALSE; +} +uint8_t card::get_select_sequence(uint8_t *deck_seq_pointer) { + if(current.location == LOCATION_DECK && pduel->game_field->is_select_hide_deck_sequence(current.controler)) { + return (*deck_seq_pointer)++; + } else { + return current.sequence; + } +} +uint32_t card::get_select_info_location(uint8_t *deck_seq_pointer) { + if(current.location == LOCATION_DECK) { + uint32_t c = current.controler; + uint32_t l = current.location; + uint32_t s = get_select_sequence(deck_seq_pointer); + uint32_t ss = current.position; + return c + (l << 8) + (s << 16) + (ss << 24); + } else { + return get_info_location(); + } +} +int32_t card::is_treated_as_not_on_field() const { + return get_status(STATUS_SUMMONING | STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED | STATUS_SPSUMMON_STEP); +} +void card::equip(card* target, uint32_t send_msg) { + if (equiping_target) + return; + target->equiping_cards.insert(this); + equiping_target = target; + for (auto& it : equip_effect) { + if (it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(equiping_target); + } + if(send_msg) { + pduel->write_buffer8(MSG_EQUIP); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer32(target->get_info_location()); + } + return; +} +void card::unequip() { + if (!equiping_target) + return; + for (auto& it : equip_effect) { + if (it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(equiping_target); + } + equiping_target->equiping_cards.erase(this); + pre_equip_target = equiping_target; + equiping_target = 0; + return; +} +int32_t card::get_union_count() { + int32_t count = 0; + for(auto& pcard : equiping_cards) { + if((pcard->data.type & TYPE_UNION) && pcard->is_affected_by_effect(EFFECT_UNION_STATUS)) + ++count; + } + return count; +} +int32_t card::get_old_union_count() { + int32_t count = 0; + for(auto& pcard : equiping_cards) { + if((pcard->data.type & TYPE_UNION) && pcard->is_affected_by_effect(EFFECT_OLDUNION_STATUS)) + ++count; + } + return count; +} +void card::xyz_overlay(const card_set& materials) { + if(materials.empty()) + return; + card_set des, leave_grave, leave_deck; + card_vector cv(materials.begin(), materials.end()); + std::sort(cv.begin(), cv.end(), card::card_operation_sort); + if(pduel->game_field->core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + int32_t d0 = (int32_t)pduel->game_field->player[0].list_main.size() - 1, s0 = d0; + int32_t d1 = (int32_t)pduel->game_field->player[1].list_main.size() - 1, s1 = d1; + for(auto& pcard : cv) { + if(pcard->current.location != LOCATION_DECK) + continue; + if((pcard->current.controler == 0) && (pcard->current.sequence == s0)) + --s0; + if((pcard->current.controler == 1) && (pcard->current.sequence == s1)) + --s1; + } + if((s0 != d0) && (s0 > 0)) { + card* ptop = pduel->game_field->player[0].list_main[s0]; + if(pduel->game_field->core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(0); + pduel->write_buffer8(d0 - s0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + if((s1 != d1) && (s1 > 0)) { + card* ptop = pduel->game_field->player[1].list_main[s1]; + if(pduel->game_field->core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(1); + pduel->write_buffer8(d1 - s1); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + for(auto& pcard : cv) { + if(pcard->overlay_target == this) + continue; + pcard->current.reason = REASON_XYZ + REASON_MATERIAL; + pcard->reset(RESET_LEAVE + RESET_OVERLAY, RESET_EVENT); + if(pcard->unique_code) + pduel->game_field->remove_unique_card(pcard); + if(pcard->equiping_target) + pcard->unequip(); + des.insert(pcard->equiping_cards.begin(), pcard->equiping_cards.end()); + for(auto cit = pcard->equiping_cards.begin(); cit != pcard->equiping_cards.end();) { + card* equipc = *cit++; + equipc->unequip(); + } + pcard->clear_card_target(); + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + if(pcard->overlay_target) { + pcard->overlay_target->xyz_remove(pcard); + } else { + pcard->enable_field_effect(false); + pduel->game_field->remove_card(pcard); + pduel->game_field->add_to_disable_check_list(pcard); + } + xyz_add(pcard); + if(pcard->previous.location == LOCATION_GRAVE) { + leave_grave.insert(pcard); + pduel->game_field->raise_single_event(pcard, 0, EVENT_LEAVE_GRAVE, pduel->game_field->core.reason_effect, pcard->current.reason, pduel->game_field->core.reason_player, 0, 0); + } else if(pcard->previous.location == LOCATION_DECK || pcard->previous.location == LOCATION_EXTRA) { + leave_deck.insert(pcard); + pduel->game_field->raise_single_event(pcard, 0, EVENT_LEAVE_DECK, pduel->game_field->core.reason_effect, pcard->current.reason, pduel->game_field->core.reason_player, 0, 0); + } + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->current.reason); + } + if(leave_grave.size() || leave_deck.size()) { + if(leave_grave.size()) { + pduel->game_field->raise_event(leave_grave, EVENT_LEAVE_GRAVE, pduel->game_field->core.reason_effect, REASON_XYZ + REASON_MATERIAL, pduel->game_field->core.reason_player, 0, 0); + } + if(leave_deck.size()) { + pduel->game_field->raise_event(leave_deck, EVENT_LEAVE_DECK, pduel->game_field->core.reason_effect, REASON_XYZ + REASON_MATERIAL, pduel->game_field->core.reason_player, 0, 0); + } + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + } + if(des.size()) + pduel->game_field->destroy(des, 0, REASON_LOST_TARGET + REASON_RULE, PLAYER_NONE); + else + pduel->game_field->adjust_instant(); +} +void card::xyz_add(card* mat) { + if(mat->current.location != 0) + return; + xyz_materials.push_back(mat); + mat->overlay_target = this; + mat->current.controler = PLAYER_NONE; + mat->current.location = LOCATION_OVERLAY; + mat->current.sequence = (uint8_t)xyz_materials.size() - 1; + for(auto& eit : mat->xmaterial_effect) { + effect* peffect = eit.second; + if(peffect->type & EFFECT_TYPE_FIELD) + pduel->game_field->add_effect(peffect); + } +} +void card::xyz_remove(card* mat) { + if(mat->overlay_target != this) + return; + if (std::find(xyz_materials.begin(), xyz_materials.end(), mat) == xyz_materials.end()) + return; + xyz_materials.erase(std::remove(xyz_materials.begin(), xyz_materials.end(), mat), xyz_materials.end()); + mat->previous.controler = mat->current.controler; + mat->previous.location = mat->current.location; + mat->previous.sequence = mat->current.sequence; + mat->previous.pzone = mat->current.pzone; + mat->current.controler = PLAYER_NONE; + mat->current.location = 0; + mat->current.sequence = 0; + mat->overlay_target = 0; + for(auto clit = xyz_materials.begin(); clit != xyz_materials.end(); ++clit) + (*clit)->current.sequence = (uint8_t)(clit - xyz_materials.begin()); + for(auto& eit : mat->xmaterial_effect) { + effect* peffect = eit.second; + if(peffect->type & EFFECT_TYPE_FIELD) + pduel->game_field->remove_effect(peffect); + } +} +void card::apply_field_effect() { + if (current.controler == PLAYER_NONE) + return; + for (auto& it : field_effect) { + if (it.second->in_range(this) || it.second->is_hand_trigger()) { + pduel->game_field->add_effect(it.second); + } + } + if(unique_code && (current.location & unique_location)) + pduel->game_field->add_unique_card(this); + spsummon_counter[0] = spsummon_counter[1] = 0; +} +void card::cancel_field_effect() { + if (current.controler == PLAYER_NONE) + return; + for (auto& it : field_effect) { + pduel->game_field->remove_effect(it.second); + } + if(unique_code && (current.location & unique_location)) + pduel->game_field->remove_unique_card(this); +} +// STATUS_EFFECT_ENABLED: the card is ready to use +// false: before moving, summoning, chaining +// true: ready +void card::enable_field_effect(bool enabled) { + if (current.location == 0) + return; + if ((enabled && get_status(STATUS_EFFECT_ENABLED)) || (!enabled && !get_status(STATUS_EFFECT_ENABLED))) + return; + refresh_disable_status(); + if (enabled) { + set_status(STATUS_EFFECT_ENABLED, TRUE); + for (auto& it : single_effect) { + if (it.second->is_flag(EFFECT_FLAG_SINGLE_RANGE) && it.second->in_range(this)) + it.second->id = pduel->game_field->infos.field_id++; + } + for (auto& it : field_effect) { + if (it.second->in_range(this)) + it.second->id = pduel->game_field->infos.field_id++; + } + if(current.location == LOCATION_SZONE) { + for (auto& it : equip_effect) + it.second->id = pduel->game_field->infos.field_id++; + } + for (auto& it : target_effect) { + if (it.second->in_range(this)) + it.second->id = pduel->game_field->infos.field_id++; + } + if (get_status(STATUS_DISABLED)) + reset(RESET_DISABLE, RESET_EVENT); + } else + set_status(STATUS_EFFECT_ENABLED, FALSE); + if (get_status(STATUS_DISABLED | STATUS_FORBIDDEN)) + return; + filter_disable_related_cards(); +} +int32_t card::add_effect(effect* peffect) { + if (!peffect) + return 0; + if (get_status(STATUS_COPYING_EFFECT) && peffect->is_flag(EFFECT_FLAG_UNCOPYABLE)) { + pduel->uncopy.insert(peffect); + return 0; + } + if (indexer.find(peffect) != indexer.end()) + return 0; + if (peffect->type & EFFECT_TYPE_SINGLE && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE) && peffect->owner == this + && get_status(STATUS_DISABLED) && (peffect->reset_flag & RESET_DISABLE)) + return 0; + if (peffect->type & EFFECT_TYPES_TRIGGER_LIKE && is_continuous_event(peffect->code)) + return 0; + if (peffect->type & EFFECT_TYPES_CHAIN_LINK && peffect->owner == pduel->game_field->temp_card) + return 0; + // the trigger effect in phase is "once per turn" by default + if (peffect->get_code_type() == CODE_PHASE && peffect->code & (PHASE_DRAW | PHASE_STANDBY | PHASE_END) + && peffect->type & (EFFECT_TYPE_TRIGGER_O | EFFECT_TYPE_TRIGGER_F) && !peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + peffect->flag[0] |= EFFECT_FLAG_COUNT_LIMIT; + peffect->count_limit = 1; + peffect->count_limit_max = 1; + } + // add EFFECT_FLAG_IGNORE_IMMUNE to EFFECT_CANNOT_TRIGGER by default + if (peffect->code == EFFECT_CANNOT_TRIGGER) { + peffect->flag[0] |= EFFECT_FLAG_IGNORE_IMMUNE; + } + card_set check_target = { this }; + effect_container::iterator eit; + if (peffect->type & EFFECT_TYPE_SINGLE) { + // assign atk/def + if(peffect->code == EFFECT_SET_ATTACK && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for(auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + if(rm->second->code == EFFECT_SET_ATTACK && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if(peffect->code == EFFECT_SET_ATTACK_FINAL && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for(auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + auto code = rm->second->code; + if((code == EFFECT_UPDATE_ATTACK || code == EFFECT_SET_ATTACK || code == EFFECT_SET_ATTACK_FINAL) + && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if(peffect->code == EFFECT_SET_DEFENSE && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for(auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + if(rm->second->code == EFFECT_SET_DEFENSE && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if(peffect->code == EFFECT_SET_DEFENSE_FINAL && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for(auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + auto code = rm->second->code; + if((code == EFFECT_UPDATE_DEFENSE || code == EFFECT_SET_DEFENSE || code == EFFECT_SET_DEFENSE_FINAL) + && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + // assign base atk/def + if (peffect->code == EFFECT_SET_BASE_ATTACK && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for (auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + if (rm->second->code == EFFECT_SET_BASE_ATTACK && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if (peffect->code == EFFECT_SET_BASE_ATTACK_FINAL && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for (auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + auto code = rm->second->code; + if ((code == EFFECT_SET_BASE_ATTACK || code == EFFECT_SET_BASE_ATTACK_FINAL || code == EFFECT_SET_ATTACK || code == EFFECT_SET_ATTACK_FINAL) + && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if (peffect->code == EFFECT_SET_BASE_DEFENSE && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for (auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + if (rm->second->code == EFFECT_SET_BASE_DEFENSE && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + if (peffect->code == EFFECT_SET_BASE_DEFENSE_FINAL && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + for (auto it = single_effect.begin(); it != single_effect.end();) { + auto rm = it++; + auto code = rm->second->code; + if ((code == EFFECT_SET_BASE_DEFENSE || code == EFFECT_SET_BASE_DEFENSE_FINAL || code == EFFECT_SET_DEFENSE || code == EFFECT_SET_DEFENSE_FINAL) + && !rm->second->is_flag(EFFECT_FLAG_SINGLE_RANGE)) + remove_effect(rm->second); + } + } + eit = single_effect.emplace(peffect->code, peffect); + } else if (peffect->type & EFFECT_TYPE_EQUIP) { + eit = equip_effect.emplace(peffect->code, peffect); + if (equiping_target) + check_target = { equiping_target }; + else + check_target.clear(); + } else if(peffect->type & EFFECT_TYPE_TARGET) { + eit = target_effect.emplace(peffect->code, peffect); + if(!effect_target_cards.empty()) + check_target = effect_target_cards; + else + check_target.clear(); + } else if (peffect->type & EFFECT_TYPE_XMATERIAL) { + eit = xmaterial_effect.emplace(peffect->code, peffect); + if (overlay_target) + check_target = { overlay_target }; + else + check_target.clear(); + } else if (peffect->type & EFFECT_TYPE_FIELD) { + eit = field_effect.emplace(peffect->code, peffect); + } else + return 0; + peffect->id = pduel->game_field->infos.field_id++; + peffect->card_type = data.type; + if (get_status(STATUS_INITIALIZING)) + peffect->flag[0] |= EFFECT_FLAG_INITIAL; + else if (get_status(STATUS_COPYING_EFFECT)) + peffect->flag[0] |= EFFECT_FLAG_COPY; + if (get_status(STATUS_COPYING_EFFECT)) { + peffect->copy_id = pduel->game_field->infos.copy_id; + peffect->reset_flag |= pduel->game_field->core.copy_reset; + peffect->reset_count = pduel->game_field->core.copy_reset_count; + } + effect* reason_effect = pduel->game_field->core.reason_effect; + indexer.emplace(peffect, eit); + peffect->handler = this; + if (peffect->is_flag(EFFECT_FLAG_INITIAL)) + initial_effect.insert(peffect); + else if (peffect->is_flag(EFFECT_FLAG_COPY)) + owning_effect.insert(peffect); + if((peffect->type & EFFECT_TYPE_FIELD)) { + if(peffect->in_range(this) || current.controler != PLAYER_NONE && peffect->is_hand_trigger()) + pduel->game_field->add_effect(peffect); + } + if (current.controler != PLAYER_NONE && !check_target.empty()) { + if (peffect->is_disable_related()) { + for (auto& target : check_target) + pduel->game_field->add_to_disable_check_list(target); + } + } + if(peffect->is_flag(EFFECT_FLAG_OATH)) { + pduel->game_field->effects.oath.emplace(peffect, reason_effect); + } + if(peffect->reset_flag & RESET_PHASE) { + pduel->game_field->effects.pheff.insert(peffect); + if(peffect->reset_count == 0) + peffect->reset_count = 1; + } + if(peffect->reset_flag & RESET_CHAIN) + pduel->game_field->effects.cheff.insert(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + pduel->game_field->effects.rechargeable.insert(peffect); + if(peffect->is_flag(EFFECT_FLAG_CLIENT_HINT)) { + pduel->write_buffer8(MSG_CARD_HINT); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer8(CHINT_DESC_ADD); + pduel->write_buffer32(peffect->description); + } + if(peffect->type & EFFECT_TYPE_SINGLE && peffect->code == EFFECT_UPDATE_LEVEL && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE)) { + int32_t val = peffect->get_value(this); + if(val > 0) { + pduel->game_field->raise_single_event(this, 0, EVENT_LEVEL_UP, peffect, 0, 0, 0, val); + pduel->game_field->process_single_event(); + } + } + if(peffect->type & EFFECT_TYPE_QUICK_O && peffect->code == EVENT_FREE_CHAIN && peffect->hint_timing[0] == 0 && peffect->hint_timing[1] == 0) { + peffect->hint_timing[1] = TIMING_END_PHASE; + } + return peffect->id; +} +effect_indexer::iterator card::remove_effect(effect* peffect) { + auto index = indexer.find(peffect); + if (index == indexer.end()) + return index; + auto& it = index->second; + card_set check_target = { this }; + if (peffect->type & EFFECT_TYPE_SINGLE) { + single_effect.erase(it); + } else if (peffect->type & EFFECT_TYPE_EQUIP) { + equip_effect.erase(it); + if (equiping_target) + check_target = { equiping_target }; + else + check_target.clear(); + } else if(peffect->type & EFFECT_TYPE_TARGET) { + target_effect.erase(it); + if(!effect_target_cards.empty()) + check_target = effect_target_cards; + else + check_target.clear(); + } else if (peffect->type & EFFECT_TYPE_XMATERIAL) { + xmaterial_effect.erase(it); + if (overlay_target) + check_target = { overlay_target }; + else + check_target.clear(); + } else if (peffect->type & EFFECT_TYPE_FIELD) { + check_target.clear(); + if (peffect->is_available() && peffect->is_disable_related()) { + pduel->game_field->update_disable_check_list(peffect); + } + field_effect.erase(it); + pduel->game_field->remove_effect(peffect); + } + if ((current.controler != PLAYER_NONE) && !get_status(STATUS_DISABLED | STATUS_FORBIDDEN) && !check_target.empty()) { + if (peffect->is_disable_related()) { + for (auto& target : check_target) + pduel->game_field->add_to_disable_check_list(target); + } + } + auto ret = indexer.erase(index); + if (peffect->is_flag(EFFECT_FLAG_INITIAL)) + initial_effect.erase(peffect); + else if (peffect->is_flag(EFFECT_FLAG_COPY)) + owning_effect.erase(peffect); + if(peffect->is_flag(EFFECT_FLAG_OATH)) + pduel->game_field->effects.oath.erase(peffect); + if(peffect->reset_flag & RESET_PHASE) + pduel->game_field->effects.pheff.erase(peffect); + if(peffect->reset_flag & RESET_CHAIN) + pduel->game_field->effects.cheff.erase(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + pduel->game_field->effects.rechargeable.erase(peffect); + if(peffect->get_code_type() == CODE_COUNTER && (peffect->code & 0xf0000) == EFFECT_COUNTER_PERMIT && (peffect->type & EFFECT_TYPE_SINGLE)) { + auto cmit = counters.find(peffect->code & 0xffff); + if(cmit != counters.end()) { + pduel->write_buffer8(MSG_REMOVE_COUNTER); + pduel->write_buffer16(cmit->first); + pduel->write_buffer8(current.controler); + pduel->write_buffer8(current.location); + pduel->write_buffer8(current.sequence); + pduel->write_buffer16(cmit->second); + counters.erase(cmit); + } + } + if(peffect->is_flag(EFFECT_FLAG_CLIENT_HINT)) { + pduel->write_buffer8(MSG_CARD_HINT); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer8(CHINT_DESC_REMOVE); + pduel->write_buffer32(peffect->description); + } + if(peffect->code == EFFECT_UNIQUE_CHECK) { + pduel->game_field->remove_unique_card(this); + unique_pos[0] = unique_pos[1] = 0; + unique_code = 0; + } + pduel->game_field->core.reseted_effects.insert(peffect); + return ret; +} +int32_t card::copy_effect(uint32_t code, uint32_t reset, int32_t count) { + card_data cdata; + ::read_card(code, &cdata); + if(cdata.type & TYPE_NORMAL) + return -1; + if (!reset) + reset = RESETS_STANDARD; + set_status(STATUS_COPYING_EFFECT, TRUE); + auto cr = pduel->game_field->core.copy_reset; + auto crc = pduel->game_field->core.copy_reset_count; + pduel->game_field->core.copy_reset = reset; + pduel->game_field->core.copy_reset_count = count; + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->call_code_function(code, "initial_effect", 1, 0); + ++pduel->game_field->infos.copy_id; + set_status(STATUS_COPYING_EFFECT, FALSE); + pduel->game_field->core.copy_reset = cr; + pduel->game_field->core.copy_reset_count = crc; + for(auto& peffect : pduel->uncopy) + pduel->delete_effect(peffect); + pduel->uncopy.clear(); + if((data.type & TYPE_MONSTER) && !(data.type & TYPE_EFFECT)) { + effect* peffect = pduel->new_effect(); + if(pduel->game_field->core.reason_effect) + peffect->owner = pduel->game_field->core.reason_effect->get_handler(); + else + peffect->owner = this; + peffect->handler = this; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_ADD_TYPE; + peffect->value = TYPE_EFFECT; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = reset; + peffect->reset_count = count; + this->add_effect(peffect); + } + return pduel->game_field->infos.copy_id - 1; +} +int32_t card::replace_effect(uint32_t code, uint32_t reset, int32_t count) { + card_data cdata; + ::read_card(code, &cdata); + if(cdata.type & TYPE_NORMAL) + return -1; + if (!reset) + reset = RESETS_STANDARD; + if(is_status(STATUS_EFFECT_REPLACED)) + set_status(STATUS_EFFECT_REPLACED, FALSE); + for(auto it = indexer.begin(); it != indexer.end();) { + effect* const& peffect = it->first; + if (peffect->is_flag(EFFECT_FLAG_INITIAL)) + it = remove_effect(peffect); + else + ++it; + } + auto cr = pduel->game_field->core.copy_reset; + auto crc = pduel->game_field->core.copy_reset_count; + pduel->game_field->core.copy_reset = reset; + pduel->game_field->core.copy_reset_count = count; + set_status(STATUS_INITIALIZING | STATUS_COPYING_EFFECT, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->call_code_function(code, "initial_effect", 1, 0); + set_status(STATUS_INITIALIZING | STATUS_COPYING_EFFECT, FALSE); + ++pduel->game_field->infos.copy_id; + pduel->game_field->core.copy_reset = cr; + pduel->game_field->core.copy_reset_count = crc; + set_status(STATUS_EFFECT_REPLACED, TRUE); + for(auto& peffect : pduel->uncopy) + pduel->delete_effect(peffect); + pduel->uncopy.clear(); + if((data.type & TYPE_MONSTER) && !(data.type & TYPE_EFFECT)) { + effect* peffect = pduel->new_effect(); + if(pduel->game_field->core.reason_effect) + peffect->owner = pduel->game_field->core.reason_effect->get_handler(); + else + peffect->owner = this; + peffect->handler = this; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_ADD_TYPE; + peffect->value = TYPE_EFFECT; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = reset; + peffect->reset_count = count; + this->add_effect(peffect); + } + return pduel->game_field->infos.copy_id - 1; +} +void card::reset(uint32_t id, uint32_t reset_type) { + if (reset_type != RESET_EVENT && reset_type != RESET_PHASE && reset_type != RESET_CODE && reset_type != RESET_COPY && reset_type != RESET_CARD) + return; + if (reset_type == RESET_EVENT) { + for (auto rit = relations.begin(); rit != relations.end();) { + if (rit->second & 0xffff0000 & id) + rit = relations.erase(rit); + else + ++rit; + } + if(id & (RESET_TODECK | RESET_TOHAND | RESET_TOGRAVE | RESET_REMOVE | RESET_TEMP_REMOVE + | RESET_OVERLAY | RESET_MSCHANGE)) + clear_relate_effect(); + if(id & (RESET_TODECK | RESET_TOHAND | RESET_TOGRAVE | RESET_REMOVE | RESET_TEMP_REMOVE + | RESET_OVERLAY | RESET_MSCHANGE | RESET_LEAVE | RESET_TOFIELD)) { + indestructable_effects.clear(); + announced_cards.clear(); + attacked_cards.clear(); + attack_announce_count = 0; + announce_count = 0; + attacked_count = 0; + removed_overlay_count = 0; + attack_all_target = TRUE; + } + if(id & (RESET_TODECK | RESET_TOHAND | RESET_TOGRAVE | RESET_REMOVE | RESET_TEMP_REMOVE + | RESET_OVERLAY | RESET_MSCHANGE | RESET_LEAVE | RESET_TOFIELD | RESET_TURN_SET)) { + battled_cards.clear(); + reset_effect_count(); + auto pr = field_effect.equal_range(EFFECT_DISABLE_FIELD); + for(; pr.first != pr.second; ++pr.first){ + if(!pr.first->second->is_flag(EFFECT_FLAG_FUNC_VALUE)) + pr.first->second->value = 0; + } + } + if(id & (RESET_TODECK | RESET_TOHAND | RESET_TOGRAVE | RESET_REMOVE | RESET_TEMP_REMOVE + | RESET_OVERLAY | RESET_MSCHANGE | RESET_TOFIELD | RESET_TURN_SET)) { + counters.clear(); + } + if(id & (RESET_TODECK | RESET_TOHAND | RESET_TOGRAVE | RESET_REMOVE | RESET_TEMP_REMOVE + | RESET_LEAVE | RESET_TOFIELD | RESET_TURN_SET | RESET_CONTROL)) { + auto pr = field_effect.equal_range(EFFECT_USE_EXTRA_MZONE); + for(; pr.first != pr.second; ++pr.first) + pr.first->second->value = pr.first->second->value & 0xffff; + pr = field_effect.equal_range(EFFECT_USE_EXTRA_SZONE); + for(; pr.first != pr.second; ++pr.first) + pr.first->second->value = pr.first->second->value & 0xffff; + } + if(id & RESET_TOFIELD) { + pre_equip_target = 0; + } + if(id & RESET_DISABLE) { + for(auto cmit = counters.begin(); cmit != counters.end();) { + if (cmit->first & COUNTER_WITHOUT_PERMIT) { + ++cmit; + continue; + } + pduel->write_buffer8(MSG_REMOVE_COUNTER); + pduel->write_buffer16(cmit->first); + pduel->write_buffer8(current.controler); + pduel->write_buffer8(current.location); + pduel->write_buffer8(current.sequence); + pduel->write_buffer16(cmit->second); + cmit = counters.erase(cmit); + } + } + } + else if (reset_type == RESET_COPY) { + delete_card_target(TRUE); + effect_target_cards.clear(); + } + bool reload = false; + for (auto it = indexer.begin(); it != indexer.end();) { + effect* const& peffect = it->first; + if (peffect->reset(id, reset_type)) { + if (is_status(STATUS_EFFECT_REPLACED) && peffect->is_flag(EFFECT_FLAG_INITIAL) && peffect->copy_id) + reload = true; + it = remove_effect(peffect); + } + else + ++it; + } + if (reload) { + set_status(STATUS_EFFECT_REPLACED, FALSE); + if (interpreter::is_load_script(data)) { + set_status(STATUS_INITIALIZING, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->call_card_function(this, "initial_effect", 1, 0); + set_status(STATUS_INITIALIZING, FALSE); + } + } +} +void card::reset_effect_count() { + for (auto& i : indexer) { + effect* const& peffect = i.first; + if (peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + peffect->recharge(); + } +} +// refresh STATUS_DISABLED based on EFFECT_DISABLE and EFFECT_CANNOT_DISABLE +// refresh STATUS_FORBIDDEN based on EFFECT_FORBIDDEN +void card::refresh_disable_status() { + filter_immune_effect(); + // forbidden + if (is_affected_by_effect(EFFECT_FORBIDDEN)) + set_status(STATUS_FORBIDDEN, TRUE); + else + set_status(STATUS_FORBIDDEN, FALSE); + // disabled + if (!is_affected_by_effect(EFFECT_CANNOT_DISABLE) && is_affected_by_effect(EFFECT_DISABLE)) + set_status(STATUS_DISABLED, TRUE); + else + set_status(STATUS_DISABLED, FALSE); +} +std::tuple card::refresh_control_status() { + uint8_t final = owner; + effect* ceffect = nullptr; + uint32_t last_id = 0; + if(pduel->game_field->core.remove_brainwashing && is_affected_by_effect(EFFECT_REMOVE_BRAINWASHING)) + last_id = pduel->game_field->core.last_control_changed_id; + effect_set eset; + filter_effect(EFFECT_SET_CONTROL, &eset); + if(eset.size()) { + effect* peffect = eset.back(); + if(peffect->id >= last_id) { + final = (uint8_t)peffect->get_value(this); + ceffect = peffect; + } + } + return std::make_tuple(final, ceffect); +} +void card::count_turn(uint16_t ct) { + turn_counter = ct; + pduel->write_buffer8(MSG_CARD_HINT); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer8(CHINT_TURN); + pduel->write_buffer32(ct); +} +void card::create_relation(card* target, uint32_t reset) { + if (relations.find(target) != relations.end()) + return; + relations[target] = reset; +} +int32_t card::is_has_relation(card* target) { + if (relations.find(target) != relations.end()) + return TRUE; + return FALSE; +} +void card::release_relation(card* target) { + if (relations.find(target) == relations.end()) + return; + relations.erase(target); +} +void card::create_relation(const chain& ch) { + relate_effect.emplace(ch.triggering_effect, ch.chain_id); +} +int32_t card::is_has_relation(const chain& ch) { + if (relate_effect.find(effect_relation::value_type(ch.triggering_effect, ch.chain_id)) != relate_effect.end()) + return TRUE; + return FALSE; +} +void card::release_relation(const chain& ch) { + relate_effect.erase(effect_relation::value_type(ch.triggering_effect, ch.chain_id)); +} +void card::clear_relate_effect() { + relate_effect.clear(); +} +void card::create_relation(effect* peffect) { + for(auto it = pduel->game_field->core.current_chain.rbegin(); it != pduel->game_field->core.current_chain.rend(); ++it) { + if(it->triggering_effect == peffect) { + create_relation(*it); + return; + } + } + relate_effect.emplace(peffect, (uint16_t)0); +} +int32_t card::is_has_relation(effect* peffect) { + for(auto& it : relate_effect) { + if(it.first == peffect) + return TRUE; + } + return FALSE; +} +void card::release_relation(effect* peffect) { + for(auto it = pduel->game_field->core.current_chain.rbegin(); it != pduel->game_field->core.current_chain.rend(); ++it) { + if(it->triggering_effect == peffect) { + release_relation(*it); + return; + } + } + relate_effect.erase(effect_relation::value_type(peffect, 0)); +} +int32_t card::leave_field_redirect(uint32_t reason) { + effect_set es; + uint32_t redirect; + uint32_t redirects = 0; + if(data.type & TYPE_TOKEN) + return 0; + filter_effect(EFFECT_LEAVE_FIELD_REDIRECT, &es); + for(effect_set::size_type i = 0; i < es.size(); ++i) { + effect* peffect = es[i]; + redirect = peffect->get_value(this, 0); + if((redirect & LOCATION_HAND) && !is_affected_by_effect(EFFECT_CANNOT_TO_HAND) && pduel->game_field->is_player_can_send_to_hand(es[i]->get_handler_player(), this)) + redirects |= redirect; + else if((redirect & LOCATION_DECK) && !is_affected_by_effect(EFFECT_CANNOT_TO_DECK) && pduel->game_field->is_player_can_send_to_deck(es[i]->get_handler_player(), this)) + redirects |= redirect; + else if((redirect & LOCATION_REMOVED) && !is_affected_by_effect(EFFECT_CANNOT_REMOVE) && pduel->game_field->is_player_can_remove(es[i]->get_handler_player(), this, REASON_EFFECT | REASON_REDIRECT, peffect)) + redirects |= redirect; + } + if(redirects & LOCATION_REMOVED) + return LOCATION_REMOVED; + // the ruling for the priority of the following redirects can't be confirmed for now + if(redirects & LOCATION_DECK) { + if((redirects & LOCATION_DECKBOT) == LOCATION_DECKBOT) + return LOCATION_DECKBOT; + if((redirects & LOCATION_DECKSHF) == LOCATION_DECKSHF) + return LOCATION_DECKSHF; + return LOCATION_DECK; + } + if(redirects & LOCATION_HAND) + return LOCATION_HAND; + return 0; +} +int32_t card::destination_redirect(uint8_t destination, uint32_t reason) { + effect_set es; + uint32_t redirect; + if(data.type & TYPE_TOKEN) + return 0; + if(destination == LOCATION_HAND) + filter_effect(EFFECT_TO_HAND_REDIRECT, &es); + else if(destination == LOCATION_DECK) + filter_effect(EFFECT_TO_DECK_REDIRECT, &es); + else if(destination == LOCATION_GRAVE) + filter_effect(EFFECT_TO_GRAVE_REDIRECT, &es); + else if(destination == LOCATION_REMOVED) + filter_effect(EFFECT_REMOVE_REDIRECT, &es); + else + return 0; + for(effect_set::size_type i = 0; i < es.size(); ++i) { + effect* peffect = es[i]; + redirect = peffect->get_value(this, 0); + if((redirect & LOCATION_HAND) && !is_affected_by_effect(EFFECT_CANNOT_TO_HAND) && pduel->game_field->is_player_can_send_to_hand(es[i]->get_handler_player(), this)) + return redirect; + if((redirect & LOCATION_DECK) && !is_affected_by_effect(EFFECT_CANNOT_TO_DECK) && pduel->game_field->is_player_can_send_to_deck(es[i]->get_handler_player(), this)) + return redirect; + if((redirect & LOCATION_REMOVED) && !is_affected_by_effect(EFFECT_CANNOT_REMOVE) && pduel->game_field->is_player_can_remove(es[i]->get_handler_player(), this, REASON_EFFECT | REASON_REDIRECT, peffect)) + return redirect; + if((redirect & LOCATION_GRAVE) && !is_affected_by_effect(EFFECT_CANNOT_TO_GRAVE) && pduel->game_field->is_player_can_send_to_grave(es[i]->get_handler_player(), this)) + return redirect; + } + return 0; +} +int32_t card::add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly) { + if(!is_can_add_counter(playerid, countertype, count, singly, 0)) + return FALSE; + uint16_t cttype = countertype; + auto pr = counters.emplace(cttype, 0); + auto cmit = pr.first; + auto pcount = count; + if(singly) { + effect_set eset; + int32_t limit = 0; + filter_effect(EFFECT_COUNTER_LIMIT + cttype, &eset); + if (eset.size()) + limit = eset.back()->get_value(); + if(limit) { + int32_t mcount = limit - get_counter(cttype); + if (mcount < 0) + mcount = 0; + if (pcount > mcount) + pcount = (uint16_t)mcount; + } + } + cmit->second += pcount; + pduel->write_buffer8(MSG_ADD_COUNTER); + pduel->write_buffer16(cttype); + pduel->write_buffer8(current.controler); + pduel->write_buffer8(current.location); + pduel->write_buffer8(current.sequence); + pduel->write_buffer16(pcount); + pduel->game_field->raise_single_event(this, 0, EVENT_ADD_COUNTER + countertype, pduel->game_field->core.reason_effect, REASON_EFFECT, playerid, playerid, pcount); + pduel->game_field->process_single_event(); + return TRUE; +} +int32_t card::remove_counter(uint16_t countertype, uint16_t count) { + auto cmit = counters.find(countertype); + if(cmit == counters.end()) + return FALSE; + auto remove_count = count; + if (cmit->second <= count) { + remove_count = cmit->second; + counters.erase(cmit); + } + else { + cmit->second -= count; + } + pduel->write_buffer8(MSG_REMOVE_COUNTER); + pduel->write_buffer16(countertype); + pduel->write_buffer8(current.controler); + pduel->write_buffer8(current.location); + pduel->write_buffer8(current.sequence); + pduel->write_buffer16(remove_count); + return TRUE; +} +// return: the player can put a counter on this or not +int32_t card::is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly, uint32_t loc) { + effect_set eset; + if (!pduel->game_field->is_player_can_place_counter(playerid, this, countertype, count)) + return FALSE; + if (!loc && (!(current.location & LOCATION_ONFIELD) || !is_position(POS_FACEUP))) + return FALSE; + uint32_t check = countertype & COUNTER_WITHOUT_PERMIT; + if(!check) { + filter_effect(EFFECT_COUNTER_PERMIT + countertype, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t prange = eset[i]->range; + if(loc) + check = loc & prange; + else + check = TRUE; + if(check) + break; + } + eset.clear(); + } + if(!check) + return FALSE; + int32_t limit = -1; + int32_t cur = 0; + auto cmit = counters.find(countertype); + if (cmit != counters.end()) + cur = cmit->second; + filter_effect(EFFECT_COUNTER_LIMIT + countertype, &eset); + if (eset.size()) + limit = eset.back()->get_value(); + if(limit > 0 && (cur + (singly ? 1 : count) > limit)) + return FALSE; + return TRUE; +} +// return: this have a EFFECT_COUNTER_PERMIT of countertype or not +int32_t card::is_can_have_counter(uint16_t countertype) { + effect_set eset; + if (countertype & COUNTER_WITHOUT_PERMIT) + return FALSE; + else { + filter_self_effect(EFFECT_COUNTER_PERMIT + countertype, &eset); + if (current.is_location(LOCATION_ONFIELD)) { + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + if (eset[i]->is_single_ready()) + return TRUE; + } + return FALSE; + } + else if (eset.size()) + return TRUE; + else + return FALSE; + } + return FALSE; +} +int32_t card::get_counter(uint16_t countertype) { + auto cmit = counters.find(countertype); + if(cmit == counters.end()) + return 0; + return cmit->second; +} +void card::set_material(card_set* materials) { + if(!materials) { + material_cards.clear(); + } else + material_cards = *materials; + for(auto& pcard : material_cards) + pcard->current.reason_card = this; + effect_set eset; + filter_effect(EFFECT_MATERIAL_CHECK, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + eset[i]->get_value(this); + } +} +void card::add_card_target(card* pcard) { + effect_target_cards.insert(pcard); + pcard->effect_target_owner.insert(this); + for(auto& it : target_effect) { + if(it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(pcard); + } + pduel->write_buffer8(MSG_CARD_TARGET); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer32(pcard->get_info_location()); +} +void card::cancel_card_target(card* pcard) { + auto cit = effect_target_cards.find(pcard); + if(cit != effect_target_cards.end()) { + effect_target_cards.erase(cit); + pcard->effect_target_owner.erase(this); + for(auto& it : target_effect) { + if(it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(pcard); + } + pduel->write_buffer8(MSG_CANCEL_TARGET); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer32(pcard->get_info_location()); + } +} +void card::delete_card_target(uint32_t send_msg) { + for (auto& pcard : effect_target_cards) { + pcard->effect_target_owner.erase(this); + for (auto& it : target_effect) { + if (it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(pcard); + } + for (auto it = pcard->single_effect.begin(); it != pcard->single_effect.end();) { + auto rm = it++; + effect* const& peffect = rm->second; + if ((peffect->owner == this) && peffect->is_flag(EFFECT_FLAG_OWNER_RELATE)) + pcard->remove_effect(peffect); + } + if (send_msg) { + pduel->write_buffer8(MSG_CANCEL_TARGET); + pduel->write_buffer32(get_info_location()); + pduel->write_buffer32(pcard->get_info_location()); + } + } +} +void card::clear_card_target() { + for(auto& pcard : effect_target_owner) { + pcard->effect_target_cards.erase(this); + for(auto& it : pcard->target_effect) { + if(it.second->is_disable_related()) + pduel->game_field->add_to_disable_check_list(this); + } + } + delete_card_target(FALSE); + effect_target_owner.clear(); + effect_target_cards.clear(); +} +void card::set_special_summon_status(effect* peffect) { + if((peffect->code == EFFECT_SPSUMMON_PROC || peffect->code == EFFECT_SPSUMMON_PROC_G) + && peffect->is_flag(EFFECT_FLAG_CANNOT_DISABLE) && peffect->is_flag(EFFECT_FLAG_UNCOPYABLE)) { + spsummon.code = 0; + spsummon.code2 = 0; + spsummon.type = 0; + spsummon.level = 0; + spsummon.rank = 0; + spsummon.attribute = 0; + spsummon.race = 0; + spsummon.attack = 0; + spsummon.defense = 0; + spsummon.setcode.clear(); + spsummon.reason_effect = nullptr; + spsummon.reason_player = PLAYER_NONE; + return; + } + card* pcard = peffect->get_handler(); + auto cait = pduel->game_field->core.current_chain.rbegin(); + if(!(peffect->type & EFFECT_TYPES_CHAIN_LINK) || (pcard->is_has_relation(*cait) && !(pcard->get_type() & TYPE_TRAPMONSTER))) { + spsummon.code = pcard->get_code(); + spsummon.code2 = pcard->get_another_code(); + spsummon.type = pcard->get_type(); + spsummon.level = pcard->get_level(); + spsummon.rank = pcard->get_rank(); + spsummon.attribute = pcard->get_attribute(); + spsummon.race = pcard->get_race(); + std::pair atk_def = pcard->get_atk_def(); + spsummon.attack = atk_def.first; + spsummon.defense = atk_def.second; + spsummon.setcode.clear(); + effect_set eset; + pcard->filter_effect(EFFECT_ADD_SETCODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + spsummon.setcode.push_back(eset[i]->get_value(pcard) & 0xffffU); + } + spsummon.reason_effect = peffect; + if(pduel->game_field->core.current_chain.size()) + spsummon.reason_player = cait->triggering_player; + else + spsummon.reason_player = summon_player; + } else { + pcard = cait->triggering_effect->get_handler(); + spsummon.code = cait->triggering_state.code; + spsummon.code2 = cait->triggering_state.code2; + spsummon.type = cait->triggering_effect->card_type; + spsummon.level = cait->triggering_state.level; + spsummon.rank = cait->triggering_state.rank; + spsummon.attribute = cait->triggering_state.attribute; + spsummon.race = cait->triggering_state.race; + spsummon.attack = cait->triggering_state.attack; + spsummon.defense = cait->triggering_state.defense; + spsummon.setcode.clear(); + effect_set eset; + pcard->filter_effect(EFFECT_ADD_SETCODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + spsummon.setcode.push_back(eset[i]->get_value(pcard) & 0xffffU); + } + spsummon.reason_effect = cait->triggering_effect; + spsummon.reason_player = cait->triggering_player; + } +} +auto default_single_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_available() && (!peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE) || c->is_affect_by_effect(peffect)); +}; +auto default_equip_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_available() && c->is_affect_by_effect(peffect); +}; +auto default_target_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_available() && peffect->is_target(c) && c->is_affect_by_effect(peffect); +}; +auto default_xmaterial_filter = [](card* c, effect* peffect) -> bool { + return !(peffect->type & EFFECT_TYPE_FIELD) && peffect->is_available() && c->is_affect_by_effect(peffect); +}; +auto default_aura_filter = [](card* c, effect* peffect) -> bool { + return !peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET) && peffect->is_available() && peffect->is_target(c) && c->is_affect_by_effect(peffect); +}; +auto accept_filter = [](card* c, effect* peffect) -> bool { + return true; +}; +template +void card::filter_effect_container(const effect_container& container, uint32_t code, effect_filter f, T& eset) { + auto rg = container.equal_range(code); + for (auto it = rg.first; it != rg.second; ++it) { + if (f(this, it->second)) + eset.push_back(it->second); + } +} +void card::filter_effect_container(const effect_container& container, uint32_t code, effect_filter f, effect_collection& eset) { + auto rg = container.equal_range(code); + for (auto it = rg.first; it != rg.second; ++it) { + if (f(this, it->second)) + eset.insert(it->second); + } +} +void card::filter_effect(uint32_t code, effect_set* eset, uint8_t sort) { + filter_effect_container(single_effect, code, default_single_filter, *eset); + for (auto& pcard : equiping_cards) + filter_effect_container(pcard->equip_effect, code, default_equip_filter, *eset); + for (auto& pcard : effect_target_owner) + filter_effect_container(pcard->target_effect, code, default_target_filter, *eset); + for (auto& pcard : xyz_materials) + filter_effect_container(pcard->xmaterial_effect, code, default_xmaterial_filter, *eset); + filter_effect_container(pduel->game_field->effects.aura_effect, code, default_aura_filter, *eset); + if(sort) + std::sort(eset->begin(), eset->end(), effect_sort_id); +} +void card::filter_single_continuous_effect(uint32_t code, effect_set* eset, uint8_t sort) { + filter_effect_container(single_effect, code, accept_filter, *eset); + for (auto& pcard : equiping_cards) + filter_effect_container(pcard->equip_effect, code, accept_filter, *eset); + auto target_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_target(c); + }; + for (auto& pcard : effect_target_owner) + filter_effect_container(pcard->target_effect, code, target_filter, *eset); + auto xmaterial_filter = [](card* c, effect* peffect) -> bool { + return !(peffect->type & EFFECT_TYPE_FIELD); + }; + for (auto& pcard : xyz_materials) + filter_effect_container(pcard->xmaterial_effect, code, xmaterial_filter, *eset); + if(sort) + std::sort(eset->begin(), eset->end(), effect_sort_id); +} +void card::filter_self_effect(uint32_t code, effect_set* eset, uint8_t sort) { + auto single_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE); + }; + filter_effect_container(single_effect, code, single_filter, *eset); + auto xmaterial_filter = [](card* c, effect* peffect) -> bool { + return !(peffect->type & EFFECT_TYPE_FIELD); + }; + for (auto& pcard : xyz_materials) + filter_effect_container(pcard->xmaterial_effect, code, xmaterial_filter, *eset); + if (sort) + std::sort(eset->begin(), eset->end(), effect_sort_id); +} +// refresh this->immune_effect +void card::filter_immune_effect() { + immune_effect.clear(); + filter_effect_container(single_effect, EFFECT_IMMUNE_EFFECT, accept_filter, immune_effect); + for (auto& pcard : equiping_cards) + filter_effect_container(pcard->equip_effect, EFFECT_IMMUNE_EFFECT, accept_filter, immune_effect); + auto target_filter = [](card* c, effect* peffect) -> bool { + return peffect->is_target(c); + }; + for (auto& pcard : effect_target_owner) + filter_effect_container(pcard->target_effect, EFFECT_IMMUNE_EFFECT, target_filter, immune_effect); + auto xmaterial_filter = [](card* c, effect* peffect) -> bool { + return !(peffect->type & EFFECT_TYPE_FIELD); + }; + for (auto& pcard : xyz_materials) + filter_effect_container(pcard->xmaterial_effect, EFFECT_IMMUNE_EFFECT, xmaterial_filter, immune_effect); + filter_effect_container(pduel->game_field->effects.aura_effect, EFFECT_IMMUNE_EFFECT, target_filter, immune_effect); + std::sort(immune_effect.begin(), immune_effect.end(), effect_sort_id); +} +// for all disable-related peffect of this, +// 1. Insert all cards in the target of peffect into effects.disable_check_list. +// 2. Insert equiping_target of peffect into it. +// 3. Insert overlay_target of peffect into it. +// 4. Insert continuous target of this into it. +void card::filter_disable_related_cards() { + for (auto& it : indexer) { + effect* const& peffect = it.first; + if (peffect->is_disable_related()) { + if (peffect->type & EFFECT_TYPE_FIELD) + pduel->game_field->update_disable_check_list(peffect); + else if ((peffect->type & EFFECT_TYPE_EQUIP) && equiping_target) + pduel->game_field->add_to_disable_check_list(equiping_target); + else if((peffect->type & EFFECT_TYPE_TARGET) && !effect_target_cards.empty()) { + for(auto& target : effect_target_cards) + pduel->game_field->add_to_disable_check_list(target); + } else if((peffect->type & EFFECT_TYPE_XMATERIAL) && overlay_target) + pduel->game_field->add_to_disable_check_list(overlay_target); + } + } +} +// put all summon procedures except ordinay summon in peset (see is_can_be_summoned()) +// return value: +// -2 = this has a EFFECT_LIMIT_SUMMON_PROC, 0 available +// -1 = this has a EFFECT_LIMIT_SUMMON_PROC, at least 1 available +// 0 = no EFFECT_LIMIT_SUMMON_PROC, and ordinary summon is not available +// 1 = no EFFECT_LIMIT_SUMMON_PROC, and ordinary summon is available +int32_t card::filter_summon_procedure(uint8_t playerid, effect_set* peset, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone) { + effect_set eset; + filter_effect(EFFECT_LIMIT_SUMMON_PROC, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(check_summon_procedure(eset[i], playerid, ignore_count, min_tribute, zone)) + peset->push_back(eset[i]); + } + if(peset->size()) + return -1; + return -2; + } + eset.clear(); + filter_effect(EFFECT_SUMMON_PROC, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(check_summon_procedure(eset[i], playerid, ignore_count, min_tribute, zone)) + peset->push_back(eset[i]); + } + // ordinary summon + if(!pduel->game_field->is_player_can_summon(SUMMON_TYPE_NORMAL, playerid, this, playerid)) + return FALSE; + if(pduel->game_field->check_unique_onfield(this, playerid, LOCATION_MZONE)) + return FALSE; + int32_t rcount = get_summon_tribute_count(); + int32_t min = rcount & 0xffff; + int32_t max = (rcount >> 16) & 0xffff; + if(!pduel->game_field->is_player_can_summon(SUMMON_TYPE_ADVANCE, playerid, this, playerid)) + max = 0; + if(min < min_tribute) + min = min_tribute; + if(max < min) + return FALSE; + if(!ignore_count && !pduel->game_field->core.extra_summon[playerid] + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) { + effect_set extra_count; + filter_effect(EFFECT_EXTRA_SUMMON_COUNT, &extra_count); + for(effect_set::size_type i = 0; i < extra_count.size(); ++i) { + std::vector retval; + extra_count[i]->get_value(this, 0, retval); + int32_t new_min = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if(new_min < min) + new_min = min; + new_zone &= zone; + if(pduel->game_field->check_tribute(this, new_min, max, 0, current.controler, new_zone, releasable)) + return TRUE; + } + } else + return pduel->game_field->check_tribute(this, min, max, 0, current.controler, zone); + return FALSE; +} +int32_t card::check_summon_procedure(effect* proc, uint8_t playerid, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone) { + if(!proc->check_count_limit(playerid)) + return FALSE; + uint8_t toplayer = playerid; + if(proc->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(proc->o_range) + toplayer = 1 - playerid; + } + if(!pduel->game_field->is_player_can_summon(proc->get_value(this), playerid, this, toplayer)) + return FALSE; + if(pduel->game_field->check_unique_onfield(this, toplayer, LOCATION_MZONE)) + return FALSE; + // the script will check min_tribute, Duel.CheckTribute() + if(!ignore_count && !pduel->game_field->core.extra_summon[playerid] + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) { + effect_set eset; + filter_effect(EFFECT_EXTRA_SUMMON_COUNT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + std::vector retval; + eset[i]->get_value(this, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if(new_min_tribute < min_tribute) + new_min_tribute = min_tribute; + new_zone &= zone; + if(is_summonable(proc, new_min_tribute, new_zone, releasable)) + return TRUE; + } + } else + return is_summonable(proc, min_tribute, zone); + return FALSE; +} +// put all set procedures except ordinay set in peset (see is_can_be_summoned()) +int32_t card::filter_set_procedure(uint8_t playerid, effect_set* peset, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone) { + effect_set eset; + filter_effect(EFFECT_LIMIT_SET_PROC, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(check_set_procedure(eset[i], playerid, ignore_count, min_tribute, zone)) + peset->push_back(eset[i]); + } + if(peset->size()) + return -1; + return -2; + } + eset.clear(); + filter_effect(EFFECT_SET_PROC, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(check_set_procedure(eset[i], playerid, ignore_count, min_tribute, zone)) + peset->push_back(eset[i]); + } + if(!pduel->game_field->is_player_can_mset(SUMMON_TYPE_NORMAL, playerid, this, playerid)) + return FALSE; + int32_t rcount = get_set_tribute_count(); + int32_t min = rcount & 0xffff; + int32_t max = (rcount >> 16) & 0xffff; + if(!pduel->game_field->is_player_can_mset(SUMMON_TYPE_ADVANCE, playerid, this, playerid)) + max = 0; + if(min < min_tribute) + min = min_tribute; + if(max < min) + return FALSE; + if(!ignore_count && !pduel->game_field->core.extra_summon[playerid] + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) { + effect_set extra_count; + filter_effect(EFFECT_EXTRA_SET_COUNT, &extra_count); + for(effect_set::size_type i = 0; i < extra_count.size(); ++i) { + std::vector retval; + extra_count[i]->get_value(this, 0, retval); + int32_t new_min = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if(new_min < min) + new_min = min; + new_zone &= zone; + if(pduel->game_field->check_tribute(this, new_min, max, 0, current.controler, new_zone, releasable, POS_FACEDOWN_DEFENSE)) + return TRUE; + } + } else + return pduel->game_field->check_tribute(this, min, max, 0, current.controler, zone, 0xff00ff, POS_FACEDOWN_DEFENSE); + return FALSE; +} +int32_t card::check_set_procedure(effect* proc, uint8_t playerid, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone) { + if(!proc->check_count_limit(playerid)) + return FALSE; + uint8_t toplayer = playerid; + if(proc->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(proc->o_range) + toplayer = 1 - playerid; + } + if(!pduel->game_field->is_player_can_mset(proc->get_value(this), playerid, this, toplayer)) + return FALSE; + if(!ignore_count && !pduel->game_field->core.extra_summon[playerid] + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) { + effect_set eset; + filter_effect(EFFECT_EXTRA_SET_COUNT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + std::vector retval; + eset[i]->get_value(this, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if(new_min_tribute < min_tribute) + new_min_tribute = min_tribute; + new_zone &= zone; + if(is_summonable(proc, new_min_tribute, new_zone, releasable)) + return TRUE; + } + } else + return is_summonable(proc, min_tribute, zone); + return FALSE; +} +void card::filter_spsummon_procedure(uint8_t playerid, effect_set* peset, uint32_t summon_type, material_info info) { + effect_collection proc_set; + filter_effect_container(field_effect, EFFECT_SPSUMMON_PROC, accept_filter, proc_set); + for (auto& peffect : proc_set) { + uint8_t toplayer{}; + uint8_t topos{}; + if(peffect->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + topos = (uint8_t)peffect->s_range; + if(peffect->o_range == 0) + toplayer = playerid; + else + toplayer = 1 - playerid; + } else { + topos = POS_FACEUP; + toplayer = playerid; + } + if(peffect->is_available() && peffect->check_count_limit(playerid) && is_spsummonable(peffect, info) + && ((topos & POS_FACEDOWN) || !pduel->game_field->check_unique_onfield(this, toplayer, LOCATION_MZONE))) { + effect* sumeffect = pduel->game_field->core.reason_effect; + if(!sumeffect) + sumeffect = peffect; + uint32_t sumtype = peffect->get_value(this); + if((!summon_type || summon_type == sumtype) + && pduel->game_field->is_player_can_spsummon(sumeffect, sumtype, topos, playerid, toplayer, this)) + peset->push_back(peffect); + } + } +} +void card::filter_spsummon_procedure_g(uint8_t playerid, effect_set* peset) { + effect_collection proc_set; + filter_effect_container(field_effect, EFFECT_SPSUMMON_PROC_G, accept_filter, proc_set); + for (auto& peffect : proc_set) { + if(!peffect->is_available() || !peffect->check_count_limit(playerid)) + continue; + if(current.controler != playerid && !peffect->is_flag(EFFECT_FLAG_BOTH_SIDE)) + continue; + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = peffect; + pduel->game_field->core.reason_player = this->current.controler; + pduel->game_field->save_lp_cost(); + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(pduel->lua->check_condition(peffect->condition, 2)) + peset->push_back(peffect); + pduel->game_field->restore_lp_cost(); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + } +} +effect* card::find_effect(const effect_container& container, uint32_t code, effect_filter f) { + auto rg = container.equal_range(code); + for (auto it = rg.first; it != rg.second; ++it) { + if (f(this, it->second)) + return it->second; + } + return nullptr; +} +effect* card::find_effect_with_target(const effect_container& container, uint32_t code, effect_filter_target f, card* target) { + auto rg = container.equal_range(code); + for (auto it = rg.first; it != rg.second; ++it) { + if (f(this, it->second, target)) + return it->second; + } + return nullptr; +} +// find an effect with code which affects this +effect* card::is_affected_by_effect(uint32_t code) { + effect* peffect = find_effect(single_effect, code, default_single_filter); + if (peffect) + return peffect; + for (auto& pcard : equiping_cards) { + peffect = find_effect(pcard->equip_effect, code, default_equip_filter); + if (peffect) + return peffect; + } + for (auto& pcard : effect_target_owner) { + peffect = find_effect(pcard->target_effect, code, default_target_filter); + if (peffect) + return peffect; + } + for (auto& pcard : xyz_materials) { + peffect = find_effect(pcard->xmaterial_effect, code, default_xmaterial_filter); + if (peffect) + return peffect; + } + peffect = find_effect(pduel->game_field->effects.aura_effect, code, default_aura_filter); + if (peffect) + return peffect; + return nullptr; +} +effect* card::is_affected_by_effect(int32_t code, card* target) { + auto single_filter = [](card* c, effect* peffect, card* target) -> bool { + return default_single_filter(c, peffect) && peffect->get_value(target); + }; + effect* peffect = find_effect_with_target(single_effect, code, single_filter, target); + if (peffect) + return peffect; + auto equip_filter = [](card* c, effect* peffect, card* target) -> bool { + return default_equip_filter(c, peffect) && peffect->get_value(target); + }; + for (auto& pcard : equiping_cards) { + peffect = find_effect_with_target(pcard->equip_effect, code, equip_filter, target); + if (peffect) + return peffect; + } + auto target_filter = [](card* c, effect* peffect, card* target) -> bool { + return default_target_filter(c, peffect) && peffect->get_value(target); + }; + for (auto& pcard : effect_target_owner) { + peffect = find_effect_with_target(pcard->target_effect, code, target_filter, target); + if (peffect) + return peffect; + } + auto xmaterial_filter = [](card* c, effect* peffect, card* target) -> bool { + return default_xmaterial_filter(c, peffect) && peffect->get_value(target); + }; + for (auto& pcard : xyz_materials) { + peffect = find_effect_with_target(pcard->xmaterial_effect, code, xmaterial_filter, target); + if (peffect) + return peffect; + } + auto aura_filter = [](card* c, effect* peffect, card* target) -> bool { + return default_aura_filter(c, peffect) && peffect->get_value(target); + }; + peffect = find_effect_with_target(pduel->game_field->effects.aura_effect, code, aura_filter, target); + if (peffect) + return peffect; + return nullptr; +} +int32_t card::fusion_check(group* fusion_m, card* cg, uint32_t chkf, uint8_t not_material) { + group* matgroup = nullptr; + if(fusion_m && !not_material) { + matgroup = pduel->new_group(fusion_m->container); + uint32_t summon_type = SUMMON_TYPE_FUSION; + if((chkf & 0x200) > 0) + summon_type = SUMMON_TYPE_SPECIAL; + effect_set eset; + filter_effect(EFFECT_MATERIAL_LIMIT, &eset); + for(auto cit = matgroup->container.begin(); cit != matgroup->container.end();) { + card* pcard = *cit; + bool is_erase = false; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(summon_type, PARAM_TYPE_INT); + if (!eset[i]->check_value_condition(3)) { + is_erase = true; + break; + } + } + if (is_erase) + cit = matgroup->container.erase(cit); + else + ++cit; + } + } else if(fusion_m) { + matgroup = pduel->new_group(fusion_m->container); + } + auto ecit = single_effect.find(EFFECT_FUSION_MATERIAL); + if(ecit == single_effect.end()) + return FALSE; + effect* peffect = ecit->second; + if(!peffect->condition) + return FALSE; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(matgroup, PARAM_TYPE_GROUP); + pduel->lua->add_param(cg, PARAM_TYPE_CARD); + pduel->lua->add_param(chkf, PARAM_TYPE_INT); + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = peffect; + pduel->game_field->core.reason_player = peffect->get_handler_player(); + pduel->game_field->core.not_material = not_material; + int32_t res = pduel->lua->check_condition(peffect->condition, 4); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + pduel->game_field->core.not_material = 0; + return res; +} +void card::fusion_select(uint8_t playerid, group* fusion_m, card* cg, uint32_t chkf, uint8_t not_material) { + group* matgroup = nullptr; + if(fusion_m && !not_material) { + matgroup = pduel->new_group(fusion_m->container); + uint32_t summon_type = SUMMON_TYPE_FUSION; + if((chkf & 0x200) > 0) + summon_type = SUMMON_TYPE_SPECIAL; + effect_set eset; + filter_effect(EFFECT_MATERIAL_LIMIT, &eset); + for(auto cit = matgroup->container.begin(); cit != matgroup->container.end();) { + card* pcard = *cit; + bool is_erase = false; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(summon_type, PARAM_TYPE_INT); + if (!eset[i]->check_value_condition(3)) { + is_erase = true; + break; + } + } + if (is_erase) + cit = matgroup->container.erase(cit); + else + ++cit; + } + } else if(fusion_m) { + matgroup = pduel->new_group(fusion_m->container); + } + effect* peffect = nullptr; + auto ecit = single_effect.find(EFFECT_FUSION_MATERIAL); + if(ecit != single_effect.end()) + peffect = ecit->second; + pduel->game_field->add_process(PROCESSOR_SELECT_FUSION, 0, peffect, matgroup, playerid + (chkf << 16), not_material, 0, 0, cg); +} +int32_t card::check_fusion_substitute(card* fcard) { + effect_set eset; + filter_effect(EFFECT_FUSION_SUBSTITUTE, &eset); + if(eset.size() == 0) + return FALSE; + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(!eset[i]->value || eset[i]->get_value(fcard)) + return TRUE; + return FALSE; +} +int32_t card::is_not_tuner(card* scard) { + if(!(get_synchro_type() & TYPE_TUNER)) + return TRUE; + effect_set eset; + filter_effect(EFFECT_NONTUNER, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(!eset[i]->value || eset[i]->get_value(scard)) + return TRUE; + return FALSE; +} +int32_t card::is_tuner(card* scard) { + if (get_synchro_type() & TYPE_TUNER) + return TRUE; + effect_set eset; + filter_effect(EFFECT_TUNER, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) + if (!eset[i]->value || eset[i]->get_value(scard)) + return TRUE; + return FALSE; +} +int32_t card::check_unique_code(card* pcard) { + if(!unique_code) + return FALSE; + if(unique_code == 1) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + return pduel->lua->get_function_value(unique_function, 1); + } + uint32_t code1 = pcard->get_code(); + uint32_t code2 = pcard->get_another_code(); + if(code1 == unique_code || (code2 && code2 == unique_code)) + return TRUE; + return FALSE; +} +void card::get_unique_target(card_set* cset, int32_t controler, card* icard) { + cset->clear(); + for(int32_t p = 0; p < 2; ++p) { + if(!unique_pos[p]) + continue; + const auto& player = pduel->game_field->player[controler ^ p]; + if(unique_location & LOCATION_MZONE) { + for(auto& pcard : player.list_mzone) { + if(pcard && (pcard != icard) && pcard->is_position(POS_FACEUP) && !pcard->get_status(STATUS_SPSUMMON_STEP) + && check_unique_code(pcard)) + cset->insert(pcard); + } + } + if(unique_location & LOCATION_SZONE) { + for(auto& pcard : player.list_szone) { + if(pcard && (pcard != icard) && pcard->is_position(POS_FACEUP) && check_unique_code(pcard)) + cset->insert(pcard); + } + } + } +} +int32_t card::check_cost_condition(int32_t ecode, int32_t playerid) { + effect_set eset; + pduel->game_field->filter_player_effect(playerid, ecode, &eset, FALSE); + filter_effect(ecode, &eset); + int32_t res = TRUE; + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + effect* peffect = eset[i]; + pduel->game_field->core.reason_effect = peffect; + pduel->game_field->core.reason_player = playerid; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(peffect->cost, 3)) { + res = FALSE; + break; + } + } + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return res; +} +int32_t card::check_cost_condition(int32_t ecode, int32_t playerid, int32_t sumtype) { + effect_set eset; + pduel->game_field->filter_player_effect(playerid, ecode, &eset, FALSE); + filter_effect(ecode, &eset); + int32_t res = TRUE; + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + effect* peffect = eset[i]; + pduel->game_field->core.reason_effect = peffect; + pduel->game_field->core.reason_player = playerid; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(peffect->cost, 4)) { + res = FALSE; + break; + } + } + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return res; +} +int32_t card::is_summonable_card() const { + if(!(data.type & TYPE_MONSTER)) + return FALSE; + if (data.type & TYPES_EXTRA_DECK) + return FALSE; + return !(data.type & (TYPE_RITUAL | TYPE_SPSUMMON | TYPE_TOKEN)); +} +int32_t card::is_spsummonable_card() { + if(!(data.type & TYPE_MONSTER)) + return FALSE; + if(is_affected_by_effect(EFFECT_REVIVE_LIMIT) && !is_status(STATUS_PROC_COMPLETE)) { + if(current.location & (LOCATION_GRAVE | LOCATION_REMOVED | LOCATION_SZONE)) + return FALSE; + if((data.type & TYPE_PENDULUM) && current.location == LOCATION_EXTRA && (current.position & POS_FACEUP)) + return FALSE; + } + effect_set eset; + filter_effect(EFFECT_SPSUMMON_CONDITION, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pduel->game_field->core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pduel->game_field->core.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(SUMMON_TYPE_SPECIAL, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + if(!eset[i]->check_value_condition(5)) + return FALSE; + } + return TRUE; +} +int32_t card::is_fusion_summonable_card(uint32_t summon_type) { + if(!(data.type & TYPE_FUSION)) + return FALSE; + if((data.type & TYPE_PENDULUM) && current.location == LOCATION_EXTRA && (current.position & POS_FACEUP)) + return FALSE; + summon_type |= SUMMON_TYPE_FUSION; + effect_set eset; + filter_effect(EFFECT_SPSUMMON_CONDITION, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pduel->game_field->core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pduel->game_field->core.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(summon_type, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + if(!eset[i]->check_value_condition(5)) + return FALSE; + } + return TRUE; +} +// check the condition function of proc +int32_t card::is_spsummonable(effect* proc, material_info info) { + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = proc; + pduel->game_field->core.reason_player = this->current.controler; + pduel->game_field->save_lp_cost(); + pduel->lua->add_param(proc, PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + uint32_t result = FALSE; + if(info.limit_tuner || info.limit_syn) { + pduel->lua->add_param(info.limit_tuner, PARAM_TYPE_CARD); + pduel->lua->add_param(info.limit_syn, PARAM_TYPE_GROUP); + uint32_t param_count = 4; + if(info.limit_syn_minc) { + pduel->lua->add_param(info.limit_syn_minc, PARAM_TYPE_INT); + pduel->lua->add_param(info.limit_syn_maxc, PARAM_TYPE_INT); + param_count = 6; + } + if(pduel->lua->check_condition(proc->condition, param_count)) + result = TRUE; + } else if(info.limit_xyz) { + pduel->lua->add_param(info.limit_xyz, PARAM_TYPE_GROUP); + uint32_t param_count = 3; + if(info.limit_xyz_minc) { + pduel->lua->add_param(info.limit_xyz_minc, PARAM_TYPE_INT); + pduel->lua->add_param(info.limit_xyz_maxc, PARAM_TYPE_INT); + param_count = 5; + } + if(pduel->lua->check_condition(proc->condition, param_count)) + result = TRUE; + } else if(info.limit_link || info.limit_link_card) { + pduel->lua->add_param(info.limit_link, PARAM_TYPE_GROUP); + pduel->lua->add_param(info.limit_link_card, PARAM_TYPE_CARD); + uint32_t param_count = 4; + if(info.limit_link_minc) { + pduel->lua->add_param(info.limit_link_minc, PARAM_TYPE_INT); + pduel->lua->add_param(info.limit_link_maxc, PARAM_TYPE_INT); + param_count = 6; + } + if(pduel->lua->check_condition(proc->condition, param_count)) + result = TRUE; + } else { + if(pduel->lua->check_condition(proc->condition, 2)) + result = TRUE; + } + pduel->game_field->restore_lp_cost(); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return result; +} +int32_t card::is_summonable(effect* proc, uint8_t min_tribute, uint32_t zone, uint32_t releasable) { + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = proc; + pduel->game_field->core.reason_player = this->current.controler; + pduel->game_field->save_lp_cost(); + uint32_t result = FALSE; + pduel->lua->add_param(proc, PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(min_tribute, PARAM_TYPE_INT); + pduel->lua->add_param(zone, PARAM_TYPE_INT); + pduel->lua->add_param(releasable, PARAM_TYPE_INT); + pduel->game_field->core.limit_extra_summon_zone = zone; + pduel->game_field->core.limit_extra_summon_releasable = releasable; + if(pduel->lua->check_condition(proc->condition, 5)) + result = TRUE; + pduel->game_field->core.limit_extra_summon_zone = 0; + pduel->game_field->core.limit_extra_summon_releasable = 0; + pduel->game_field->restore_lp_cost(); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return result; +} +int32_t card::is_can_be_summoned(uint8_t playerid, uint8_t ignore_count, effect* peffect, uint8_t min_tribute, uint32_t zone) { + if(!is_summonable_card()) + return FALSE; + if(!ignore_count && (pduel->game_field->core.extra_summon[playerid] || !is_affected_by_effect(EFFECT_EXTRA_SUMMON_COUNT)) + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_SUMMON_COST, playerid)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + if(current.location == LOCATION_MZONE) { + if(is_position(POS_FACEDOWN) + || !is_affected_by_effect(EFFECT_DUAL_SUMMONABLE) + || is_affected_by_effect(EFFECT_DUAL_STATUS) + || !pduel->game_field->is_player_can_summon(SUMMON_TYPE_DUAL, playerid, this, playerid) + || is_affected_by_effect(EFFECT_CANNOT_SUMMON)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } else if(current.location == LOCATION_HAND) { + if(is_affected_by_effect(EFFECT_CANNOT_SUMMON)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + effect_set proc; + int32_t res = filter_summon_procedure(playerid, &proc, ignore_count, min_tribute, zone); + if(peffect) { + if(res < 0 || !check_summon_procedure(peffect, playerid, ignore_count, min_tribute, zone)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } else { + if(!proc.size() && (!res || res == -2)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +int32_t card::is_summon_negatable(uint32_t sumtype, effect* reason_effect) { + uint32_t code = 0; + if (sumtype & SUMMON_TYPE_NORMAL) + code = EFFECT_CANNOT_DISABLE_SUMMON; + else if (sumtype & SUMMON_TYPE_FLIP) + code = EFFECT_CANNOT_DISABLE_FLIP_SUMMON; + else if (sumtype & SUMMON_TYPE_SPECIAL) + code = EFFECT_CANNOT_DISABLE_SPSUMMON; + else + return FALSE; + if (is_affected_by_effect(code)) + return FALSE; + if (sumtype == SUMMON_TYPE_DUAL || sumtype & SUMMON_TYPE_FLIP) { + if (!is_status(STATUS_FLIP_SUMMONING)) + return FALSE; + if (!is_affect_by_effect(reason_effect)) + return FALSE; + if (sumtype == SUMMON_TYPE_DUAL && (!is_affected_by_effect(EFFECT_DUAL_SUMMONABLE) || is_affected_by_effect(EFFECT_DUAL_STATUS))) + return FALSE; + } + return TRUE; +} +int32_t card::get_summon_tribute_count() { + int32_t min = 0, max = 0; + int32_t level = get_level(); + if(level < 5) + return 0; + else if(level < 7) + min = max = 1; + else + min = max = 2; + std::vector duplicate; + effect_set eset; + filter_effect(EFFECT_DECREASE_TRIBUTE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) && eset[i]->count_limit == 0) + continue; + std::vector retval; + eset[i]->get_value(this, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + max -= dec >> 16; + } + if(min < 0) min = 0; + if(max < min) max = min; + return min + (max << 16); +} +int32_t card::get_set_tribute_count() { + int32_t min = 0, max = 0; + int32_t level = get_level(); + if(level < 5) + return 0; + else if(level < 7) + min = max = 1; + else + min = max = 2; + std::vector duplicate; + effect_set eset; + filter_effect(EFFECT_DECREASE_TRIBUTE_SET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) && eset[i]->count_limit == 0) + continue; + std::vector retval; + eset[i]->get_value(this, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + max -= dec >> 16; + } + if(min < 0) min = 0; + if(max < min) max = min; + return min + (max << 16); +} +int32_t card::is_can_be_flip_summoned(uint8_t playerid) { + if(is_status(STATUS_CANNOT_CHANGE_FORM)) + return FALSE; + if(current.location != LOCATION_MZONE) + return FALSE; + if(!(current.position & POS_FACEDOWN)) + return FALSE; + if(pduel->game_field->check_unique_onfield(this, playerid, LOCATION_MZONE)) + return FALSE; + if(!pduel->game_field->is_player_can_flipsummon(playerid, this)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_FLIP_SUMMON)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_CHANGE_POSITION)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_FLIPSUMMON_COST, playerid)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +// check if this can be sp_summoned by EFFECT_SPSUMMON_PROC +// call filter_spsummon_procedure() +int32_t card::is_special_summonable(uint8_t playerid, uint32_t summon_type, material_info info) { + if(!(data.type & TYPE_MONSTER)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_SPECIAL_SUMMON)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_SPSUMMON_COST, playerid, summon_type)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + effect_set eset; + filter_spsummon_procedure(playerid, &eset, summon_type, info); + pduel->game_field->restore_lp_cost(); + return !!eset.size(); +} +int32_t card::is_can_be_special_summoned(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t sumplayer, uint8_t toplayer, uint8_t nocheck, uint8_t nolimit, uint32_t zone) { + if(reason_effect->get_handler() == this) + reason_effect->status |= EFFECT_STATUS_SPSELF; + if(current.location == LOCATION_MZONE) + return FALSE; + if(current.location == LOCATION_REMOVED && (current.position & POS_FACEDOWN)) + return FALSE; + if(is_affected_by_effect(EFFECT_REVIVE_LIMIT) && !is_status(STATUS_PROC_COMPLETE)) { + if((!nolimit && (current.location & (LOCATION_GRAVE | LOCATION_REMOVED | LOCATION_SZONE))) + || (!nocheck && !nolimit && (current.location & (LOCATION_DECK | LOCATION_HAND)))) + return FALSE; + if(!nolimit && (data.type & TYPE_PENDULUM) && current.location == LOCATION_EXTRA && (current.position & POS_FACEUP)) + return FALSE; + } + if((data.type & TYPE_PENDULUM) && current.location == LOCATION_EXTRA && (current.position & POS_FACEUP) + && (sumtype == SUMMON_TYPE_FUSION || sumtype == SUMMON_TYPE_SYNCHRO || sumtype == SUMMON_TYPE_XYZ)) + return FALSE; + if((sumpos & POS_FACEDOWN) && pduel->game_field->is_player_affected_by_effect(sumplayer, EFFECT_DIVINE_LIGHT)) + sumpos = (sumpos & POS_FACEUP) | ((sumpos & POS_FACEDOWN) >> 1); + if(!(sumpos & POS_FACEDOWN) && pduel->game_field->check_unique_onfield(this, toplayer, LOCATION_MZONE)) + return FALSE; + sumtype |= SUMMON_TYPE_SPECIAL; + if((sumplayer == 0 || sumplayer == 1) && !pduel->game_field->is_player_can_spsummon(reason_effect, sumtype, sumpos, sumplayer, toplayer, this)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_SPECIAL_SUMMON)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(zone != 0xff) { + if(pduel->game_field->get_useable_count(this, toplayer, LOCATION_MZONE, sumplayer, LOCATION_REASON_TOFIELD, zone, 0) <= 0) + return FALSE; + } + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_SPSUMMON_COST, sumplayer, sumtype)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + if(!nocheck) { + effect_set eset; + if(!(data.type & TYPE_MONSTER)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + filter_effect(EFFECT_SPSUMMON_CONDITION, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(sumplayer, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + pduel->lua->add_param(sumpos, PARAM_TYPE_INT); + pduel->lua->add_param(toplayer, PARAM_TYPE_INT); + if(!eset[i]->check_value_condition(5)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +uint8_t card::get_spsummonable_position(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t sumplayer, uint8_t toplayer) { + uint8_t position = 0; + uint8_t positions[4] = { POS_FACEUP_ATTACK, POS_FACEDOWN_ATTACK, POS_FACEUP_DEFENSE, POS_FACEDOWN_DEFENSE }; + effect_set eset; + for(int32_t p = 0; p < 4; ++p) { + bool poscheck = true; + if(!(positions[p] & sumpos)) + continue; + //[YGOL] A Token opted in with SETCODE_FACEDOWN_TOKEN may be Summoned face-down. + //A Link monster never can, so it stays blocked even if it carries the setcode. + if((data.type & (TYPE_TOKEN | TYPE_LINK)) && (positions[p] & POS_FACEDOWN) + && !((data.type & TYPE_TOKEN) && !(data.type & TYPE_LINK) && data.is_setcode(SETCODE_FACEDOWN_TOKEN))) + continue; + pduel->game_field->filter_player_effect(sumplayer, EFFECT_LIMIT_SPECIAL_SUMMON_POSITION, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + continue; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + pduel->lua->add_param(sumplayer, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + pduel->lua->add_param(positions[p], PARAM_TYPE_INT); + pduel->lua->add_param(toplayer, PARAM_TYPE_INT); + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + if(pduel->lua->check_condition(eset[i]->target, 7)) + poscheck = false; + } + eset.clear(); + if(poscheck) + position |= positions[p]; + } + return position; +} +int32_t card::is_setable_mzone(uint8_t playerid, uint8_t ignore_count, effect* peffect, uint8_t min_tribute, uint32_t zone) { + if(!is_summonable_card()) + return FALSE; + if(current.location != LOCATION_HAND) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_MSET)) + return FALSE; + if(!ignore_count && (pduel->game_field->core.extra_summon[playerid] || !is_affected_by_effect(EFFECT_EXTRA_SET_COUNT)) + && pduel->game_field->core.summon_count[playerid] >= pduel->game_field->get_summon_count_limit(playerid)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_MSET_COST, playerid)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + effect_set eset; + int32_t res = filter_set_procedure(playerid, &eset, ignore_count, min_tribute, zone); + if(peffect) { + if(res < 0 || !check_set_procedure(peffect, playerid, ignore_count, min_tribute, zone)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } else { + if(!eset.size() && (!res || res == -2)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +int32_t card::is_setable_szone(uint8_t playerid, uint8_t ignore_fd, uint8_t toplayer, uint32_t zone) { + uint32_t tp = current.controler; + if(toplayer < 2) + tp = toplayer; + if(!(data.type & TYPE_FIELD) && !ignore_fd && pduel->game_field->get_useable_count(this, tp, LOCATION_SZONE, tp, LOCATION_REASON_TOFIELD, zone) <= 0) + return FALSE; + if(data.type & TYPE_MONSTER && !is_affected_by_effect(EFFECT_MONSTER_SSET)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_SSET)) + return FALSE; + if(!pduel->game_field->is_player_can_sset(playerid, this)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_SSET_COST, playerid)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +int32_t card::is_affect_by_effect(effect* reason_effect) { + if (is_status(STATUS_SUMMONING)) + return reason_effect && affect_summoning_effect.find(reason_effect->code) != affect_summoning_effect.end(); + if(!reason_effect || reason_effect->is_flag(EFFECT_FLAG_IGNORE_IMMUNE)) + return TRUE; + if(reason_effect->is_immuned(this)) + return FALSE; + return TRUE; +} +int32_t card::is_can_be_disabled_by_effect(effect* reason_effect, bool is_monster_effect) { + if (is_monster_effect && is_status(STATUS_DISABLED)) + return FALSE; + if(!is_monster_effect && !(get_type() & TYPE_TRAPMONSTER) && is_status(STATUS_DISABLED)) + return FALSE; + if (is_affected_by_effect(EFFECT_CANNOT_DISABLE)) + return FALSE; + if (!is_affect_by_effect(reason_effect)) + return FALSE; + return TRUE; +} +int32_t card::is_destructable() { + if(overlay_target) + return FALSE; + if(current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) + return FALSE; + return TRUE; +} +int32_t card::is_destructable_by_battle(card * pcard) { + if(is_affected_by_effect(EFFECT_INDESTRUCTABLE_BATTLE, pcard)) + return FALSE; + return TRUE; +} +effect* card::check_indestructable_by_effect(effect* reason_effect, uint8_t playerid) { + if(!reason_effect) + return nullptr; + effect_set eset; + filter_effect(EFFECT_INDESTRUCTABLE_EFFECT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(eset[i]->check_value_condition(3)) + return eset[i]; + } + return nullptr; +} +int32_t card::is_destructable_by_effect(effect* reason_effect, uint8_t playerid) { + if(!is_affect_by_effect(reason_effect)) + return FALSE; + if(check_indestructable_by_effect(reason_effect, playerid)) + return FALSE; + effect_set eset; + eset.clear(); + filter_effect(EFFECT_INDESTRUCTABLE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(REASON_EFFECT, PARAM_TYPE_INT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + return FALSE; + break; + } + } + eset.clear(); + filter_effect(EFFECT_INDESTRUCTABLE_COUNT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + if(eset[i]->count_limit == 0) + continue; + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(REASON_EFFECT, PARAM_TYPE_INT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + return FALSE; + break; + } + } else { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(REASON_EFFECT, PARAM_TYPE_INT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + int32_t ct = eset[i]->get_value(3); + if(ct) { + auto it = indestructable_effects.emplace(eset[i]->id, 0); + if(it.first->second + 1 <= ct) { + return FALSE; + break; + } + } + } + } + return TRUE; +} +int32_t card::is_removeable(uint8_t playerid, uint8_t pos, uint32_t reason) { + if(!pduel->game_field->is_player_can_remove(playerid, this, reason)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_REMOVE)) + return FALSE; + //[YGOL] Same opt-in as get_spsummonable_position: without this, a card that banishes + //face-down silently does nothing to a Token that is legitimately face-down. + if((data.type & TYPE_TOKEN) && (pos & POS_FACEDOWN) && !data.is_setcode(SETCODE_FACEDOWN_TOKEN)) + return FALSE; + return TRUE; +} +int32_t card::is_removeable_as_cost(uint8_t playerid, uint8_t pos) { + uint32_t redirect = 0; + uint32_t dest = LOCATION_REMOVED; + if(current.location == LOCATION_REMOVED) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + return FALSE; + if(!is_removeable(playerid, pos, REASON_COST)) + return FALSE; + int32_t redirchk = FALSE; + auto op_param = sendto_param; + sendto_param.location = dest; + if(current.location & LOCATION_ONFIELD) + redirect = leave_field_redirect(REASON_COST) & 0xffff; + if(redirect) { + redirchk = TRUE; + dest = redirect; + } + redirect = destination_redirect(dest, REASON_COST) & 0xffff; + if(redirect) { + redirchk = TRUE; + dest = redirect; + } + sendto_param = op_param; + if(dest != LOCATION_REMOVED || (redirchk && (pos & POS_FACEDOWN))) + return FALSE; + return TRUE; +} +int32_t card::is_attack_decreasable_as_cost(uint8_t playerid, int32_t val) { + if(!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER)) + return FALSE; + if(!(current.location & LOCATION_MZONE) || is_position(POS_FACEDOWN)) + return FALSE; + if(is_affected_by_effect(EFFECT_SET_ATTACK_FINAL) || is_affected_by_effect(EFFECT_REVERSE_UPDATE)) + return FALSE; + if(val && get_attack() < val) + return FALSE; + return TRUE; +} +int32_t card::is_defense_decreasable_as_cost(uint8_t playerid, int32_t val) { + if(!(data.type & TYPE_MONSTER) && !(get_type() & TYPE_MONSTER)) + return FALSE; + if(!(current.location & LOCATION_MZONE) || is_position(POS_FACEDOWN) || (data.type & TYPE_LINK)) + return FALSE; + if(is_affected_by_effect(EFFECT_SET_DEFENSE_FINAL) || is_affected_by_effect(EFFECT_REVERSE_UPDATE)) + return FALSE; + if(val && get_defense() < val) + return FALSE; + return TRUE; +} +int32_t card::is_releasable_by_summon(uint8_t playerid, card *pcard) { + if(is_status(STATUS_SUMMONING)) + return FALSE; + if(overlay_target) + return FALSE; + if(current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) + return FALSE; + if(!pduel->game_field->is_player_can_release(playerid, this, REASON_SUMMON)) + return FALSE; + if(is_affected_by_effect(EFFECT_UNRELEASABLE_SUM, pcard)) + return FALSE; + if(pcard->is_affected_by_effect(EFFECT_TRIBUTE_LIMIT, this)) + return FALSE; + return TRUE; +} +int32_t card::is_releasable_by_nonsummon(uint8_t playerid, uint32_t reason) { + if(is_status(STATUS_SUMMONING)) + return FALSE; + if(overlay_target) + return FALSE; + if(current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) + return FALSE; + if((current.location == LOCATION_HAND) && (data.type & (TYPE_SPELL | TYPE_TRAP))) + return FALSE; + if(!pduel->game_field->is_player_can_release(playerid, this, reason)) + return FALSE; + if(is_affected_by_effect(EFFECT_UNRELEASABLE_NONSUM)) + return FALSE; + return TRUE; +} +int32_t card::is_releasable_by_effect(uint8_t playerid, effect* reason_effect) { + if(!reason_effect) + return TRUE; + effect_set eset; + filter_effect(EFFECT_UNRELEASABLE_EFFECT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(eset[i]->check_value_condition(3)) + return FALSE; + } + return TRUE; +} +int32_t card::is_capable_send_to_grave(uint8_t playerid) { + if(is_affected_by_effect(EFFECT_CANNOT_TO_GRAVE)) + return FALSE; + if(!pduel->game_field->is_player_can_send_to_grave(playerid, this)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_send_to_hand(uint8_t playerid) { + if(is_status(STATUS_LEAVE_CONFIRMED)) + return FALSE; + if((current.location == LOCATION_EXTRA) && is_extra_deck_monster()) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_TO_HAND)) + return FALSE; + if(is_extra_deck_monster() && !is_capable_send_to_deck(playerid)) + return FALSE; + if(!pduel->game_field->is_player_can_send_to_hand(playerid, this)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_send_to_deck(uint8_t playerid, uint8_t send_activating) { + if(!send_activating && is_status(STATUS_LEAVE_CONFIRMED)) + return FALSE; + if((current.location == LOCATION_EXTRA) && is_extra_deck_monster()) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_TO_DECK)) + return FALSE; + if(!pduel->game_field->is_player_can_send_to_deck(playerid, this)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_send_to_extra(uint8_t playerid) { + if(!is_extra_deck_monster() && !(data.type & TYPE_PENDULUM)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_TO_DECK)) + return FALSE; + if(!pduel->game_field->is_player_can_send_to_deck(playerid, this)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_cost_to_grave(uint8_t playerid) { + uint32_t redirect = 0; + uint32_t dest = LOCATION_GRAVE; + if(data.type & TYPE_TOKEN) + return FALSE; + if((data.type & TYPE_PENDULUM) && (current.location & LOCATION_ONFIELD) && is_capable_send_to_extra(playerid)) + return FALSE; + if(current.location == LOCATION_GRAVE) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_TO_GRAVE_AS_COST)) + return FALSE; + if(!is_capable_send_to_grave(playerid)) + return FALSE; + auto op_param = sendto_param; + sendto_param.location = dest; + if(current.location & LOCATION_ONFIELD) + redirect = leave_field_redirect(REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + redirect = destination_redirect(dest, REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + sendto_param = op_param; + if(dest != LOCATION_GRAVE) + return FALSE; + return TRUE; +} +int32_t card::is_capable_cost_to_hand(uint8_t playerid) { + uint32_t redirect = 0; + uint32_t dest = LOCATION_HAND; + if(data.type & (TYPE_TOKEN) || is_extra_deck_monster()) + return FALSE; + if(current.location == LOCATION_HAND) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + return FALSE; + if(!is_capable_send_to_hand(playerid)) + return FALSE; + auto op_param = sendto_param; + sendto_param.location = dest; + if(current.location & LOCATION_ONFIELD) + redirect = leave_field_redirect(REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + redirect = destination_redirect(dest, REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + sendto_param = op_param; + if(dest != LOCATION_HAND) + return FALSE; + return TRUE; +} +int32_t card::is_capable_cost_to_deck(uint8_t playerid) { + uint32_t redirect = 0; + uint32_t dest = LOCATION_DECK; + if(data.type & (TYPE_TOKEN) || is_extra_deck_monster()) + return FALSE; + if(current.location == LOCATION_DECK) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + return FALSE; + if(!is_capable_send_to_deck(playerid)) + return FALSE; + auto op_param = sendto_param; + sendto_param.location = dest; + if(current.location & LOCATION_ONFIELD) + redirect = leave_field_redirect(REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + redirect = destination_redirect(dest, REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + sendto_param = op_param; + if(dest != LOCATION_DECK) + return FALSE; + return TRUE; +} +int32_t card::is_capable_cost_to_extra(uint8_t playerid) { + uint32_t redirect = 0; + uint32_t dest = LOCATION_DECK; + if(!is_extra_deck_monster()) + return FALSE; + if(current.location == LOCATION_EXTRA) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + return FALSE; + if(!is_capable_send_to_deck(playerid)) + return FALSE; + auto op_param = sendto_param; + sendto_param.location = dest; + if(current.location & LOCATION_ONFIELD) + redirect = leave_field_redirect(REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + redirect = destination_redirect(dest, REASON_COST) & 0xffff; + if(redirect) + dest = redirect; + sendto_param = op_param; + if(dest != LOCATION_DECK) + return FALSE; + return TRUE; +} +int32_t card::is_capable_attack() { + if(!is_position(POS_FACEUP_ATTACK) && !(is_position(POS_FACEUP_DEFENSE) && is_affected_by_effect(EFFECT_DEFENSE_ATTACK))) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_ATTACK)) + return FALSE; + if(is_affected_by_effect(EFFECT_ATTACK_DISABLED)) + return FALSE; + if(pduel->game_field->is_player_affected_by_effect(pduel->game_field->infos.turn_player, EFFECT_SKIP_BP)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_attack_announce(uint8_t playerid) { + if(!is_capable_attack()) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_ATTACK_ANNOUNCE)) + return FALSE; + pduel->game_field->save_lp_cost(); + if(!check_cost_condition(EFFECT_ATTACK_COST, playerid)) { + pduel->game_field->restore_lp_cost(); + return FALSE; + } + pduel->game_field->restore_lp_cost(); + return TRUE; +} +int32_t card::is_capable_change_position(uint8_t playerid) { + if(is_status(STATUS_CANNOT_CHANGE_FORM)) + return FALSE; + if(data.type & TYPE_LINK) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_CHANGE_POSITION)) + return FALSE; + if(pduel->game_field->is_player_affected_by_effect(playerid, EFFECT_CANNOT_CHANGE_POSITION)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_change_position_by_effect(uint8_t playerid) { + if(data.type & TYPE_LINK) + return FALSE; + return TRUE; +} +int32_t card::is_capable_turn_set(uint8_t playerid) { + if(data.type & (TYPE_LINK | TYPE_TOKEN)) + return FALSE; + if(is_position(POS_FACEDOWN)) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_TURN_SET)) + return FALSE; + if(pduel->game_field->is_player_affected_by_effect(playerid, EFFECT_CANNOT_TURN_SET)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_change_control() { + if(is_affected_by_effect(EFFECT_CANNOT_CHANGE_CONTROL)) + return FALSE; + return TRUE; +} +int32_t card::is_control_can_be_changed(int32_t ignore_mzone, uint32_t zone) { + if(current.controler == PLAYER_NONE) + return FALSE; + if(current.location != LOCATION_MZONE) + return FALSE; + if(!ignore_mzone && pduel->game_field->get_useable_count(this, 1 - current.controler, LOCATION_MZONE, current.controler, LOCATION_REASON_CONTROL, zone) <= 0) + return FALSE; + if(pduel->game_field->core.duel_rule <= 4 && (get_type() & TYPE_TRAPMONSTER) + && pduel->game_field->get_useable_count(this, 1 - current.controler, LOCATION_SZONE, current.controler, LOCATION_REASON_CONTROL) <= 0) + return FALSE; + if(is_affected_by_effect(EFFECT_CANNOT_CHANGE_CONTROL)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_be_battle_target(card* pcard) { + if(is_affected_by_effect(EFFECT_CANNOT_BE_BATTLE_TARGET, pcard)) + return FALSE; + if(pcard->is_affected_by_effect(EFFECT_CANNOT_SELECT_BATTLE_TARGET, this)) + return FALSE; + if(is_affected_by_effect(EFFECT_IGNORE_BATTLE_TARGET, pcard)) + return FALSE; + return TRUE; +} +int32_t card::is_capable_be_effect_target(effect* reason_effect, uint8_t playerid) { + if(is_status(STATUS_SUMMONING) || is_status(STATUS_BATTLE_DESTROYED)) + return FALSE; + if(current.location & (LOCATION_DECK | LOCATION_EXTRA | LOCATION_HAND)) + return FALSE; + effect_set eset; + filter_effect(EFFECT_CANNOT_BE_EFFECT_TARGET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(eset[i]->get_value(reason_effect, 1)) + return FALSE; + } + eset.clear(); + reason_effect->get_handler()->filter_effect(EFFECT_CANNOT_SELECT_EFFECT_TARGET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(this, PARAM_TYPE_CARD); + if(eset[i]->get_value(reason_effect, 1)) + return FALSE; + } + return TRUE; +} +int32_t card::is_capable_overlay(uint8_t playerid) { + if(data.type & TYPE_TOKEN) + return FALSE; + if(!(current.location & LOCATION_ONFIELD) && is_status(STATUS_FORBIDDEN)) + return FALSE; + if(current.controler != playerid && !is_capable_change_control()) + return FALSE; + return TRUE; +} +int32_t card::is_can_be_fusion_material(card* fcard, uint32_t summon_type) { + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + if(pduel->game_field->core.not_material) + return TRUE; + effect_set eset; + filter_effect(EFFECT_CANNOT_BE_FUSION_MATERIAL, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(summon_type, PARAM_TYPE_INT); + if(eset[i]->get_value(fcard, 1)) + return FALSE; + } + eset.clear(); + filter_effect(EFFECT_EXTRA_FUSION_MATERIAL, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(eset[i]->get_value(fcard)) + return TRUE; + return FALSE; + } + return TRUE; +} +int32_t card::is_can_be_synchro_material(card* scard, card* tuner) { + if((data.type & (TYPE_XYZ | TYPE_LINK)) && !get_synchro_level(scard)) + return FALSE; + if(!(get_synchro_type() & TYPE_MONSTER)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + //special fix for scrap chimera, not perfect yet + if(tuner && (pduel->game_field->core.global_flag & GLOBALFLAG_SCRAP_CHIMERA)) { + if(is_affected_by_effect(EFFECT_SCRAP_CHIMERA, tuner)) + return false; + } + effect_set eset; + filter_effect(EFFECT_CANNOT_BE_SYNCHRO_MATERIAL, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(eset[i]->get_value(scard)) + return FALSE; + return TRUE; +} +int32_t card::is_can_be_ritual_material(card* scard) { + if(!(get_type() & TYPE_MONSTER)) + return FALSE; + if(current.location == LOCATION_GRAVE) { + effect_set eset; + filter_effect(EFFECT_EXTRA_RITUAL_MATERIAL, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(eset[i]->get_value(scard)) + return TRUE; + return FALSE; + } + if(current.location == LOCATION_EXTRA && is_affected_by_effect(EFFECT_MAP_OF_HEAVEN) && scard && scard->get_level() < 7) + return FALSE; + return TRUE; +} +int32_t card::is_can_be_xyz_material(card* scard) { + if(data.type & TYPE_TOKEN) + return FALSE; + if(!(get_xyz_type() & TYPE_MONSTER)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + effect_set eset; + filter_effect(EFFECT_CANNOT_BE_XYZ_MATERIAL, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(eset[i]->get_value(scard)) + return FALSE; + return TRUE; +} +int32_t card::is_can_be_link_material(card* scard) { + if(!(get_link_type() & TYPE_MONSTER)) + return FALSE; + if(is_status(STATUS_FORBIDDEN)) + return FALSE; + effect_set eset; + filter_effect(EFFECT_CANNOT_BE_LINK_MATERIAL, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + if(eset[i]->get_value(scard)) + return FALSE; + return TRUE; +} +/** +* @param filter Lua function filter(e) +*/ +int32_t card::is_original_effect_property(int32_t filter) { + for (auto& peffect : initial_effect) { + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + if (pduel->lua->check_condition(filter, 1)) + return TRUE; + } + return FALSE; +} +/** +* @param filter Lua function filter(e) +*/ +int32_t card::is_effect_property(int32_t filter) { + for (auto& peffect : initial_effect) { + if (current.is_location(LOCATION_MZONE) && !peffect->is_monster_effect()) + continue; + if (current.is_location(LOCATION_SZONE) && !peffect->in_range(this)) + continue; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + if(pduel->lua->check_condition(filter, 1)) + return TRUE; + } + for (auto& peffect : owning_effect) { + if (current.is_location(LOCATION_MZONE) && !peffect->is_monster_effect()) + continue; + if (current.is_location(LOCATION_SZONE) && !peffect->in_range(this)) + continue; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + if (pduel->lua->check_condition(filter, 1)) + return TRUE; + } + return FALSE; +} diff --git a/core/card.h b/core/card.h new file mode 100644 index 0000000..cd4dbb5 --- /dev/null +++ b/core/card.h @@ -0,0 +1,473 @@ +/* + * card.h + * + * Created on: 2010-4-8 + * Author: Argon + */ + +#ifndef CARD_H_ +#define CARD_H_ + +#include "common.h" +#include "effectset.h" +#include "card_data.h" +#include "sort.h" +#include "metadata.h" +#include +#include +#include +#include +#include + +class card; +class duel; +class effect; +class group; +struct chain; + +using card_set = std::set; +using card_vector = std::vector; +using effect_container = std::multimap; +using effect_indexer = std::unordered_map; +using effect_collection = std::unordered_set; + +using effect_filter = bool(*)(card* self, effect* peffect); +using effect_filter_target = bool(*)(card* self, effect* peffect, card* target); + +struct card_state { + uint32_t code{ 0 }; + uint32_t code2{ 0 }; + std::vector setcode; + uint32_t type{ 0 }; + uint32_t level{ 0 }; + uint32_t rank{ 0 }; + uint32_t link{ 0 }; + uint32_t lscale{ 0 }; + uint32_t rscale{ 0 }; + uint32_t attribute{ 0 }; + uint32_t race{ 0 }; + int32_t attack{ 0 }; + int32_t defense{ 0 }; + int32_t base_attack{ 0 }; + int32_t base_defense{ 0 }; + uint8_t controler{ PLAYER_NONE }; + uint8_t location{ 0 }; + uint8_t sequence{ 0 }; + uint8_t position{ 0 }; + uint32_t reason{ 0 }; + card* reason_card{ nullptr }; + effect* reason_effect{ nullptr }; + uint8_t reason_player{ PLAYER_NONE }; + bool pzone{ false }; + + bool is_location(uint32_t loc) const; + bool is_main_mzone() const { + return location == LOCATION_MZONE && sequence >= 0 && sequence <= 4; + } + bool is_stzone() const { + return location == LOCATION_SZONE && sequence >= 0 && sequence <= 4; + } + void init_state(); +}; + +struct query_cache { + uint32_t info_location{ UINT32_MAX }; + uint32_t current_code{ UINT32_MAX }; + uint32_t type{ UINT32_MAX }; + uint32_t level{ UINT32_MAX }; + uint32_t rank{ UINT32_MAX }; + uint32_t link{ UINT32_MAX }; + uint32_t attribute{ UINT32_MAX }; + uint32_t race{ UINT32_MAX }; + int32_t attack{ -1 }; + int32_t defense{ -1 }; + int32_t base_attack{ -1 }; + int32_t base_defense{ -1 }; + uint32_t reason{ UINT32_MAX }; + uint32_t status{ UINT32_MAX }; + uint32_t lscale{ UINT32_MAX }; + uint32_t rscale{ UINT32_MAX }; + uint32_t link_marker{ UINT32_MAX }; + + void clear_cache(); +}; + +struct material_info { + // Synchron + card* limit_tuner{ nullptr }; + group* limit_syn{ nullptr }; + int32_t limit_syn_minc{ 0 }; + int32_t limit_syn_maxc{ 0 }; + // Xyz + group* limit_xyz{ nullptr }; + int32_t limit_xyz_minc{ 0 }; + int32_t limit_xyz_maxc{ 0 }; + // Link + group* limit_link{ nullptr }; + card* limit_link_card{ nullptr }; + int32_t limit_link_minc{ 0 }; + int32_t limit_link_maxc{ 0 }; +}; +const material_info null_info; + +class card { +public: + struct effect_relation_hash { + std::size_t operator()(const std::pair& v) const { + std::size_t h1 = std::hash()(v.first); + std::size_t h2 = std::hash()(v.second); + return h1 ^ (h2 << 1); + } + }; + using effect_relation = std::unordered_set, effect_relation_hash>; + using relation_map = std::unordered_map; + using counter_map = std::map; + using effect_count = std::map; + class attacker_map : public std::unordered_map> { + public: + void addcard(card* pcard); + uint32_t findcard(card* pcard); + }; + + //millux + + uint32_t get_ritual_type(); + uint32_t set_entity_code(uint32_t entity_code, bool remove_alias = false); + uint32_t get_summon_info(); + + struct sendto_param_t { + void set(uint8_t p, uint8_t pos, uint8_t loc, uint8_t seq = 0) { + playerid = p; + position = pos; + location = loc; + sequence = seq; + } + void clear() { + playerid = 0; + position = 0; + location = 0; + sequence = 0; + } + uint8_t playerid{ 0 }; + uint8_t position{ 0 }; + uint8_t location{ 0 }; + uint8_t sequence{ 0 }; + }; + + int32_t ref_handle{}; + duel* pduel{}; + metadata meta; + card_data data; + card_state previous; + card_state temp; + card_state current; + card_state spsummon; + query_cache q_cache; + uint8_t owner{ PLAYER_NONE }; + uint8_t summon_player{ PLAYER_NONE }; + uint32_t summon_info{}; + uint32_t status{}; + sendto_param_t sendto_param; + uint32_t release_param{}; + uint32_t sum_param{}; + uint32_t position_param{}; + uint32_t spsummon_param{}; + uint32_t to_field_param{}; + uint8_t attack_announce_count{}; + uint8_t direct_attackable{}; + uint8_t announce_count{}; + uint8_t attacked_count{}; + uint8_t removed_overlay_count{}; + uint8_t attack_all_target{}; + uint8_t attack_controler{}; + uint64_t cardid{}; + uint32_t fieldid{}; + uint32_t fieldid_r{}; //real field id, updated when moving to new location + uint32_t activate_count_id{}; //updated when moving to new location or flipping + uint16_t turnid{}; + uint16_t turn_counter{}; + uint8_t unique_pos[2]{}; + uint32_t unique_fieldid{}; + uint32_t unique_code{}; + uint32_t unique_location{}; + int32_t unique_function{}; + effect* unique_effect{}; + uint32_t spsummon_code{}; + uint16_t spsummon_counter[2]{}; + uint8_t assume_type{}; + uint32_t assume_value{}; + card* equiping_target{}; + card* pre_equip_target{}; + card* overlay_target{}; + relation_map relations; + counter_map counters; + effect_count indestructable_effects; + attacker_map announced_cards; + attacker_map attacked_cards; + attacker_map battled_cards; + card_set equiping_cards; + card_set material_cards; + card_set effect_target_owner; + card_set effect_target_cards; + card_vector xyz_materials; + int32_t xyz_materials_previous_count_onfield{}; + effect_container single_effect; + effect_container field_effect; + effect_container equip_effect; + effect_container target_effect; + effect_container xmaterial_effect; + effect_indexer indexer; + effect_relation relate_effect; + effect_set_v immune_effect; + effect_collection initial_effect; + effect_collection owning_effect; + + uint8_t to_leave_fromex; + + explicit card(duel* pd); + ~card() = default; + static bool card_operation_sort(card* c1, card* c2); + static bool check_card_setcode(uint32_t code, uint32_t value); + bool is_extra_deck_monster() const { return !!(data.type & TYPES_EXTRA_DECK); } + + int32_t get_infos(byte* buf, uint32_t query_flag, int32_t use_cache = TRUE); + uint32_t get_info_location() const; + uint32_t get_public_info_location(); + uint32_t get_original_code() const; + std::tuple get_original_code_rule() const; + uint32_t get_code(); + uint32_t get_another_code(); + int32_t is_set_card(uint32_t set_code); + int32_t is_origin_set_card(uint32_t set_code); + int32_t is_pre_set_card(uint32_t set_code); + int32_t is_fusion_set_card(uint32_t set_code); + int32_t is_link_set_card(uint32_t set_code); + int32_t is_special_summon_set_card(uint32_t set_code); + uint32_t get_type(); + uint32_t get_fusion_type(); + uint32_t get_synchro_type(); + uint32_t get_xyz_type(); + uint32_t get_link_type(); + std::pair get_base_atk_def(); + std::pair get_atk_def(); + int32_t get_base_attack(); + int32_t get_attack(); + int32_t get_base_defense(); + int32_t get_defense(); + int32_t get_battle_attack(); + int32_t get_battle_defense(); + uint32_t get_level(); + uint32_t get_rank(); + uint32_t get_link(); + uint32_t get_mat_level_from_effect(card* pcard, uint32_t effect_code); + uint32_t get_mat_level(card* pcard, uint32_t level_effect_code, uint32_t allow_effect_code = 0); + uint32_t get_synchro_level(card* pcard); + uint32_t get_ritual_level(card* pcard); + uint32_t check_xyz_level(card* pcard, uint32_t lv); + uint32_t get_attribute(); + uint32_t get_fusion_attribute(uint8_t playerid); + uint32_t get_link_attribute(uint8_t playerid); + uint32_t get_grave_attribute(uint8_t playerid); + uint32_t get_race(); + uint32_t get_link_race(uint8_t playerid); + uint32_t get_grave_race(uint8_t playerid); + uint32_t get_lscale(); + uint32_t get_rscale(); + uint32_t get_link_marker(); + uint32_t is_link_marker(uint32_t dir); + uint32_t get_linked_zone(); + void get_linked_cards(card_set* cset); + uint32_t get_mutual_linked_zone(); + void get_mutual_linked_cards(card_set * cset); + int32_t is_link_state(); + int32_t is_extra_link_state(); + int32_t is_position(uint32_t pos) const; + void set_status(uint32_t x, int32_t enabled); + int32_t get_status(uint32_t x) const; + int32_t is_status(uint32_t x) const; + uint32_t get_column_zone(int32_t location); + void get_column_cards(card_set* cset); + int32_t is_all_column(); + uint8_t get_select_sequence(uint8_t *deck_seq_pointer); + uint32_t get_select_info_location(uint8_t *deck_seq_pointer); + int32_t is_treated_as_not_on_field() const; + + void equip(card* target, uint32_t send_msg = TRUE); + void unequip(); + int32_t get_union_count(); + int32_t get_old_union_count(); + void xyz_overlay(const card_set& materials); + void xyz_add(card* mat); + void xyz_remove(card* mat); + void apply_field_effect(); + void cancel_field_effect(); + void enable_field_effect(bool enabled); + int32_t add_effect(effect* peffect); + effect_indexer::iterator remove_effect(effect* peffect); + int32_t copy_effect(uint32_t code, uint32_t reset, int32_t count); + int32_t replace_effect(uint32_t code, uint32_t reset, int32_t count); + void reset(uint32_t id, uint32_t reset_type); + void reset_effect_count(); + void refresh_disable_status(); + std::tuple refresh_control_status(); + + void count_turn(uint16_t ct); + void create_relation(card* target, uint32_t reset); + int32_t is_has_relation(card* target); + void release_relation(card* target); + void create_relation(const chain& ch); + int32_t is_has_relation(const chain& ch); + void release_relation(const chain& ch); + void clear_relate_effect(); + void create_relation(effect* peffect); + int32_t is_has_relation(effect* peffect); + void release_relation(effect* peffect); + int32_t leave_field_redirect(uint32_t reason); + int32_t destination_redirect(uint8_t destination, uint32_t reason); + int32_t add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly); + int32_t remove_counter(uint16_t countertype, uint16_t count); + int32_t is_can_add_counter(uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t singly, uint32_t loc); + int32_t is_can_have_counter(uint16_t countertype); + int32_t get_counter(uint16_t countertype); + void set_material(card_set* materials); + void add_card_target(card* pcard); + void cancel_card_target(card* pcard); + void delete_card_target(uint32_t send_msg); + void clear_card_target(); + void set_special_summon_status(effect* peffect); + + template + void filter_effect_container(const effect_container& container, uint32_t code, effect_filter f, T& eset); + void filter_effect_container(const effect_container& container, uint32_t code, effect_filter f, effect_collection& eset); + void filter_effect(uint32_t code, effect_set* eset, uint8_t sort = TRUE); + void filter_single_continuous_effect(uint32_t code, effect_set* eset, uint8_t sort = TRUE); + void filter_self_effect(uint32_t code, effect_set* eset, uint8_t sort = TRUE); + void filter_immune_effect(); + void filter_disable_related_cards(); + int32_t filter_summon_procedure(uint8_t playerid, effect_set* eset, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone); + int32_t check_summon_procedure(effect* proc, uint8_t playerid, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone); + int32_t filter_set_procedure(uint8_t playerid, effect_set* eset, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone); + int32_t check_set_procedure(effect* proc, uint8_t playerid, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone); + void filter_spsummon_procedure(uint8_t playerid, effect_set* eset, uint32_t summon_type, material_info info = null_info); + void filter_spsummon_procedure_g(uint8_t playerid, effect_set* eset); + effect* find_effect(const effect_container& container, uint32_t code, effect_filter f); + effect* find_effect_with_target(const effect_container& container, uint32_t code, effect_filter_target f, card* target); + effect* is_affected_by_effect(uint32_t code); + effect* is_affected_by_effect(int32_t code, card* target); + int32_t fusion_check(group* fusion_m, card* cg, uint32_t chkf, uint8_t not_material); + void fusion_select(uint8_t playerid, group* fusion_m, card* cg, uint32_t chkf, uint8_t not_material); + int32_t check_fusion_substitute(card* fcard); + int32_t is_not_tuner(card* scard); + int32_t is_tuner(card* scard); + + int32_t check_unique_code(card* pcard); + void get_unique_target(card_set* cset, int32_t controler, card* icard = nullptr); + int32_t check_cost_condition(int32_t ecode, int32_t playerid); + int32_t check_cost_condition(int32_t ecode, int32_t playerid, int32_t sumtype); + int32_t is_summonable_card() const; + int32_t is_spsummonable_card(); + int32_t is_fusion_summonable_card(uint32_t summon_type); + int32_t is_spsummonable(effect* proc, material_info info = null_info); + int32_t is_summonable(effect* proc, uint8_t min_tribute, uint32_t zone = 0x1f, uint32_t releasable = 0xff00ff); + int32_t is_can_be_summoned(uint8_t playerid, uint8_t ingore_count, effect* peffect, uint8_t min_tribute, uint32_t zone = 0x1f); + int32_t is_summon_negatable(uint32_t sumtype, effect* reason_effect); + int32_t get_summon_tribute_count(); + int32_t get_set_tribute_count(); + int32_t is_can_be_flip_summoned(uint8_t playerid); + int32_t is_special_summonable(uint8_t playerid, uint32_t summon_type, material_info info = null_info); + int32_t is_can_be_special_summoned(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t sumplayer, uint8_t toplayer, uint8_t nocheck, uint8_t nolimit, uint32_t zone); + uint8_t get_spsummonable_position(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t sumplayer, uint8_t toplayer); + int32_t is_setable_mzone(uint8_t playerid, uint8_t ignore_count, effect* peffect, uint8_t min_tribute, uint32_t zone = 0x1f); + int32_t is_setable_szone(uint8_t playerid, uint8_t ignore_fd = 0, uint8_t toplayer = 2, uint32_t zone = 0xff); + int32_t is_affect_by_effect(effect* reason_effect); + int32_t is_can_be_disabled_by_effect(effect* reason_effect, bool is_monster_effect); + int32_t is_destructable(); + int32_t is_destructable_by_battle(card* pcard); + effect* check_indestructable_by_effect(effect* reason_effect, uint8_t playerid); + int32_t is_destructable_by_effect(effect* reason_effect, uint8_t playerid); + int32_t is_removeable(uint8_t playerid, uint8_t pos, uint32_t reason); + int32_t is_removeable_as_cost(uint8_t playerid, uint8_t pos); + int32_t is_attack_decreasable_as_cost(uint8_t playerid, int32_t val); + int32_t is_defense_decreasable_as_cost(uint8_t playerid, int32_t val); + int32_t is_releasable_by_summon(uint8_t playerid, card* pcard); + int32_t is_releasable_by_nonsummon(uint8_t playerid, uint32_t reason); + int32_t is_releasable_by_effect(uint8_t playerid, effect* reason_effect); + int32_t is_capable_send_to_grave(uint8_t playerid); + int32_t is_capable_send_to_hand(uint8_t playerid); + int32_t is_capable_send_to_deck(uint8_t playerid, uint8_t send_activating = FALSE); + int32_t is_capable_send_to_extra(uint8_t playerid); + int32_t is_capable_cost_to_grave(uint8_t playerid); + int32_t is_capable_cost_to_hand(uint8_t playerid); + int32_t is_capable_cost_to_deck(uint8_t playerid); + int32_t is_capable_cost_to_extra(uint8_t playerid); + int32_t is_capable_attack(); + int32_t is_capable_attack_announce(uint8_t playerid); + int32_t is_capable_change_position(uint8_t playerid); + int32_t is_capable_change_position_by_effect(uint8_t playerid); + int32_t is_capable_turn_set(uint8_t playerid); + int32_t is_capable_change_control(); + int32_t is_control_can_be_changed(int32_t ignore_mzone, uint32_t zone); + int32_t is_capable_be_battle_target(card* pcard); + int32_t is_capable_be_effect_target(effect* reason_effect, uint8_t playerid); + int32_t is_capable_overlay(uint8_t playerid); + int32_t is_can_be_fusion_material(card* fcard, uint32_t summon_type); + int32_t is_can_be_synchro_material(card* scard, card* tuner = nullptr); + int32_t is_can_be_ritual_material(card* scard); + int32_t is_can_be_xyz_material(card* scard); + int32_t is_can_be_link_material(card* scard); + int32_t is_original_effect_property(int32_t filter); + int32_t is_effect_property(int32_t filter); +}; + +//Summon Type in summon_info +#define SUMMON_TYPE_NORMAL 0x10000000U +#define SUMMON_TYPE_ADVANCE 0x11000000U +#define SUMMON_TYPE_FLIP 0x20000000U +#define SUMMON_TYPE_SPECIAL 0x40000000U +#define SUMMON_TYPE_FUSION 0x43000000U +#define SUMMON_TYPE_RITUAL 0x45000000U +#define SUMMON_TYPE_SYNCHRO 0x46000000U +#define SUMMON_TYPE_XYZ 0x49000000U +#define SUMMON_TYPE_PENDULUM 0x4a000000U +#define SUMMON_TYPE_LINK 0x4c000000U + +//Gemini Summon +#define SUMMON_TYPE_DUAL 0x12000000U + +//bitfield blocks +#define SUMMON_VALUE_MAIN_TYPE 0xf0000000U +#define SUMMON_VALUE_SUB_TYPE 0x0f000000U +#define SUMMON_VALUE_LOCATION 0x00ff0000U +#define SUMMON_VALUE_CUSTOM_TYPE 0x0000ffffU +constexpr uint32_t DEFAULT_SUMMON_TYPE = SUMMON_VALUE_MAIN_TYPE | SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE; + +#define SUMMON_VALUE_FUTURE_FUSION 0x18 + +//Counter +#define COUNTER_WITHOUT_PERMIT 0x1000U +//#define COUNTER_NEED_ENABLE 0x2000 + +//Assume +#define ASSUME_CODE 1 +#define ASSUME_TYPE 2 +#define ASSUME_LEVEL 3 +#define ASSUME_RANK 4 +#define ASSUME_ATTRIBUTE 5 +#define ASSUME_RACE 6 +#define ASSUME_ATTACK 7 +#define ASSUME_DEFENSE 8 + +//Special Summon effect info +#define SUMMON_INFO_CODE 0x01 +#define SUMMON_INFO_CODE2 0x02 +#define SUMMON_INFO_TYPE 0x04 +#define SUMMON_INFO_LEVEL 0x08 +#define SUMMON_INFO_RANK 0x10 +#define SUMMON_INFO_ATTRIBUTE 0x20 +#define SUMMON_INFO_RACE 0x40 +#define SUMMON_INFO_ATTACK 0x80 +#define SUMMON_INFO_DEFENSE 0x100 +#define SUMMON_INFO_REASON_EFFECT 0x200 +#define SUMMON_INFO_REASON_PLAYER 0x400 + +#endif /* CARD_H_ */ diff --git a/core/card_data.h b/core/card_data.h new file mode 100644 index 0000000..ae8d413 --- /dev/null +++ b/core/card_data.h @@ -0,0 +1,87 @@ +#ifndef CARD_DATA_H_ +#define CARD_DATA_H_ + +#include +#include +#include "common.h" + +constexpr int SIZE_SETCODE = 16; + +//double name +constexpr uint32_t CARD_MARINE_DOLPHIN = 78734254; +constexpr uint32_t CARD_TWINKLE_MOSS = 13857930; +constexpr uint32_t CARD_TIMAEUS = 1784686; +constexpr uint32_t CARD_CRITIAS = 11082056; +constexpr uint32_t CARD_HERMOS = 46232525; + +const std::unordered_map second_code = { + {CARD_MARINE_DOLPHIN, 17955766u}, + {CARD_TWINKLE_MOSS, 17732278u}, + {CARD_TIMAEUS, 10000050u}, + {CARD_CRITIAS, 10000060u}, + {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)); +} + +struct card_data { + uint32_t code{}; + uint32_t alias{}; + uint16_t setcode[SIZE_SETCODE]{}; + uint32_t type{}; + uint32_t level{}; + uint32_t attribute{}; + uint32_t race{}; + int32_t attack{}; + int32_t defense{}; + uint32_t lscale{}; + uint32_t rscale{}; + uint32_t link_marker{}; + uint32_t rule_code{}; + + void clear() { + std::memset(this, 0, sizeof(card_data)); + } + + bool is_setcode(uint32_t value) const { + for (auto& x : setcode) { + if (!x) + return false; + if (check_setcode(x, value)) + return true; + } + return false; + } + + // get the printed code on card + uint32_t get_original_code() const { + return alias ? alias : code; + } + + // get the code used in duel + uint32_t get_duel_code() const { + return rule_code ? rule_code : get_original_code(); + } +}; + +static_assert(sizeof(card_data) == 80, "card_data size error"); +#endif /* CARD_DATA_H_ */ diff --git a/core/common.h b/core/common.h new file mode 100644 index 0000000..522fca5 --- /dev/null +++ b/core/common.h @@ -0,0 +1,485 @@ +/* + * common.h + * + * Created on: 2009-1-7 + * Author: Argon.Sun + */ + +#ifndef COMMON_H_ +#define COMMON_H_ + +#include +#include +typedef unsigned char byte; + +inline bool check_playerid(int32_t playerid) { + return playerid >= 0 && playerid <= 1; +} + +enum LuaParamType : int32_t { + PARAM_TYPE_INT = 0x01, + PARAM_TYPE_STRING = 0x02, + PARAM_TYPE_CARD = 0x04, + PARAM_TYPE_GROUP = 0x08, + PARAM_TYPE_EFFECT = 0x10, + PARAM_TYPE_FUNCTION = 0x20, + PARAM_TYPE_BOOLEAN = 0x40, + PARAM_TYPE_INDEX = 0x80, +}; + +#define MATCH_ALL(x,y) (((x)&(y))==(y)) +#define MATCH_ANY(x,y) ((x)&(y)) +#define ADD_BIT(x,y) ((x)|=(y)) +#define REMOVE_BIT(x,y) ((x)&=~(y)) + +#define OPERATION_SUCCESS 1 +#define OPERATION_FAIL 0 +#define OPERATION_CANCELED -1 +#define TRUE 1 +#define FALSE 0 + +#define SIZE_MESSAGE_BUFFER 0x2000u +#define SIZE_RETURN_VALUE 256u +#define SIZE_AI_NAME 128u +#define SIZE_HINT_MSG 1024u + +#define PROCESSOR_BUFFER_LEN 0x0fffffff +#define PROCESSOR_FLAG 0xf0000000 +#define PROCESSOR_NONE 0 +#define PROCESSOR_WAITING 0x10000000 +#define PROCESSOR_END 0x20000000 + +#define MASTER_RULE3 3 //Master Rule 3 (2014) +#define NEW_MASTER_RULE 4 //New Master Rule (2017) +#define MASTER_RULE_2020 5 //Master Rule 2020 +#define CURRENT_RULE 5 + +// Duel creation flags +#define OCGCORE_CREATE_ENABLE_LUA_COVERAGE 0x00000001u + +//Locations +#define LOCATION_DECK 0x01U +#define LOCATION_HAND 0x02U +#define LOCATION_MZONE 0x04U +#define LOCATION_SZONE 0x08U +#define LOCATION_GRAVE 0x10U +#define LOCATION_REMOVED 0x20U +#define LOCATION_EXTRA 0x40U +#define LOCATION_OVERLAY 0x80U +#define LOCATION_ONFIELD (LOCATION_MZONE | LOCATION_SZONE) +#define LOCATION_FZONE 0x100U +#define LOCATION_PZONE 0x200U +//For redirect +#define LOCATION_DECKBOT 0x10001 //Return to deck bottom +#define LOCATION_DECKSHF 0x20001 //Return to deck and shuffle +//For Duel.SendtoDeck +#define SEQ_DECKTOP 0 //Return to deck top +#define SEQ_DECKBOTTOM 1 //Return to deck bottom +#define SEQ_DECKSHUFFLE 2 //Return to deck and shuffle + +//Positions +#define POS_FACEUP_ATTACK 0x1 +#define POS_FACEDOWN_ATTACK 0x2 +#define POS_FACEUP_DEFENSE 0x4 +#define POS_FACEDOWN_DEFENSE 0x8 +#define POS_FACEUP 0x5 +#define POS_FACEDOWN 0xa +#define POS_ATTACK 0x3 +#define POS_DEFENSE 0xc +#define POS_REVEAL 0x80 +//Flip effect flags +#define NO_FLIP_EFFECT 0x10000 + +//Move to field flags +#define RETURN_TEMP_REMOVE_TO_FIELD 1 +#define RETURN_TRAP_MONSTER_TO_SZONE 2 + +//Types +#define TYPE_MONSTER 0x1 // +#define TYPE_SPELL 0x2 // +#define TYPE_TRAP 0x4 // +#define TYPE_NORMAL 0x10 // +#define TYPE_EFFECT 0x20 // +#define TYPE_FUSION 0x40 // +#define TYPE_RITUAL 0x80 // +#define TYPE_TRAPMONSTER 0x100 // +#define TYPE_SPIRIT 0x200 // +#define TYPE_UNION 0x400 // +#define TYPE_DUAL 0x800 // +#define TYPE_TUNER 0x1000 // +#define TYPE_SYNCHRO 0x2000 // +#define TYPE_TOKEN 0x4000 // +#define TYPE_QUICKPLAY 0x10000 // +#define TYPE_CONTINUOUS 0x20000 // +#define TYPE_EQUIP 0x40000 // +#define TYPE_FIELD 0x80000 // +#define TYPE_COUNTER 0x100000 // +#define TYPE_FLIP 0x200000 // +#define TYPE_TOON 0x400000 // +#define TYPE_XYZ 0x800000 // +#define TYPE_PENDULUM 0x1000000 // +#define TYPE_SPSUMMON 0x2000000 // +#define TYPE_LINK 0x4000000 // + +//[YGOL] Reserved setcode: "this Token is allowed to exist face-down". +// The OCG rule is that a Token is never face-down; the core hard-codes that in +// card::get_spsummonable_position and card::is_removeable. Rather than lift the rule +// for every Token, a Token opts in by carrying this setcode on its cdb row. +// A setcode is the ONLY carrier that works: field::is_player_can_spsummon_monster +// probes a scratch temp_card built from card_data alone (no script, no registered +// effects), so an effect-based opt-in is invisible to the legality check and the card +// ends up unactivatable. A correctly typed Token is also TYPE_NORMAL, and +// interpreter::is_load_script skips Normal monsters, so a Token cannot register +// anything from its own Lua either. +// card_data::is_setcode matches on the low 12 bits, so this permanently burns +// archetype id 0xFDF -- never reuse it for a real archetype. +// First user: "WANTED! Rushing Roulette" (810000327) and its two Tokens. +#define SETCODE_FACEDOWN_TOKEN 0xFDF + +#define TYPES_EXTRA_DECK (TYPE_FUSION | TYPE_SYNCHRO | TYPE_XYZ | TYPE_LINK) + +//Attributes +#define ATTRIBUTES_COUNT 7 +#define ATTRIBUTE_ALL 0x7f // +#define ATTRIBUTE_EARTH 0x01 // +#define ATTRIBUTE_WATER 0x02 // +#define ATTRIBUTE_FIRE 0x04 // +#define ATTRIBUTE_WIND 0x08 // +#define ATTRIBUTE_LIGHT 0x10 // +#define ATTRIBUTE_DARK 0x20 // +#define ATTRIBUTE_DEVINE 0x40 // + +//Races +#define RACES_COUNT 26 +#define RACE_ALL 0x3ffffff +#define RACE_WARRIOR 0x1 // +#define RACE_SPELLCASTER 0x2 // +#define RACE_FAIRY 0x4 // +#define RACE_FIEND 0x8 // +#define RACE_ZOMBIE 0x10 // +#define RACE_MACHINE 0x20 // +#define RACE_AQUA 0x40 // +#define RACE_PYRO 0x80 // +#define RACE_ROCK 0x100 // +#define RACE_WINDBEAST 0x200 // +#define RACE_PLANT 0x400 // +#define RACE_INSECT 0x800 // +#define RACE_THUNDER 0x1000 // +#define RACE_DRAGON 0x2000 // +#define RACE_BEAST 0x4000 // +#define RACE_BEASTWARRIOR 0x8000 // +#define RACE_DINOSAUR 0x10000 // +#define RACE_FISH 0x20000 // +#define RACE_SEASERPENT 0x40000 // +#define RACE_REPTILE 0x80000 // +#define RACE_PSYCHO 0x100000 // +#define RACE_DEVINE 0x200000 // +#define RACE_CREATORGOD 0x400000 // +#define RACE_WYRM 0x800000 // +#define RACE_CYBERSE 0x1000000 // +#define RACE_ILLUSION 0x2000000 // + +//Reason +#define REASON_DESTROY 0x1 // +#define REASON_RELEASE 0x2 // +#define REASON_TEMPORARY 0x4 // +#define REASON_MATERIAL 0x8 // +#define REASON_SUMMON 0x10 // +#define REASON_BATTLE 0x20 // +#define REASON_EFFECT 0x40 // +#define REASON_COST 0x80 // +#define REASON_ADJUST 0x100 // +#define REASON_LOST_TARGET 0x200 // +#define REASON_RULE 0x400 // +#define REASON_SPSUMMON 0x800 // +#define REASON_DISSUMMON 0x1000 // +#define REASON_FLIP 0x2000 // +#define REASON_DISCARD 0x4000 // +#define REASON_RDAMAGE 0x8000 // +#define REASON_RRECOVER 0x10000 // +#define REASON_RETURN 0x20000 // +#define REASON_FUSION 0x40000 // +#define REASON_SYNCHRO 0x80000 // +#define REASON_RITUAL 0x100000 // +#define REASON_XYZ 0x200000 // +#define REASON_REPLACE 0x1000000 // +#define REASON_DRAW 0x2000000 // +#define REASON_REDIRECT 0x4000000 // +#define REASON_REVEAL 0x8000000 // +#define REASON_LINK 0x10000000 // +#define REASON_LOST_OVERLAY 0x20000000 // +#define REASON_MAINTENANCE 0x40000000 // +#define REASON_ACTION 0x80000000 // + +#define REASONS_PROCEDURE (REASON_SYNCHRO | REASON_XYZ | REASON_LINK) + +//Status +#define STATUS_DISABLED 0x0001 +#define STATUS_TO_ENABLE 0x0002 +#define STATUS_TO_DISABLE 0x0004 +#define STATUS_PROC_COMPLETE 0x0008 +#define STATUS_SET_TURN 0x0010 +#define STATUS_NO_LEVEL 0x0020 +#define STATUS_BATTLE_RESULT 0x0040 +#define STATUS_SPSUMMON_STEP 0x0080 +#define STATUS_CANNOT_CHANGE_FORM 0x0100 +#define STATUS_SUMMONING 0x0200 +#define STATUS_EFFECT_ENABLED 0x0400 +#define STATUS_SUMMON_TURN 0x0800 +#define STATUS_DESTROY_CONFIRMED 0x1000 +#define STATUS_LEAVE_CONFIRMED 0x2000 +#define STATUS_BATTLE_DESTROYED 0x4000 +#define STATUS_COPYING_EFFECT 0x8000 +#define STATUS_CHAINING 0x10000 +#define STATUS_SUMMON_DISABLED 0x20000 +#define STATUS_ACTIVATE_DISABLED 0x40000 +#define STATUS_EFFECT_REPLACED 0x80000 +#define STATUS_FLIP_SUMMONING 0x100000 +#define STATUS_ATTACK_CANCELED 0x200000 +#define STATUS_INITIALIZING 0x400000 +#define STATUS_TO_HAND_WITHOUT_CONFIRM 0x800000 +#define STATUS_JUST_POS 0x1000000 +#define STATUS_CONTINUOUS_POS 0x2000000 +#define STATUS_FORBIDDEN 0x4000000 +#define STATUS_ACT_FROM_HAND 0x8000000 +#define STATUS_OPPO_BATTLE 0x10000000 +#define STATUS_FLIP_SUMMON_TURN 0x20000000 +#define STATUS_SPSUMMON_TURN 0x40000000 +#define STATUS_FLIP_SUMMON_DISABLED 0x80000000 + +//Query list +#define QUERY_CODE 0x1 +#define QUERY_POSITION 0x2 +#define QUERY_ALIAS 0x4 +#define QUERY_TYPE 0x8 +#define QUERY_LEVEL 0x10 +#define QUERY_RANK 0x20 +#define QUERY_ATTRIBUTE 0x40 +#define QUERY_RACE 0x80 +#define QUERY_ATTACK 0x100 +#define QUERY_DEFENSE 0x200 +#define QUERY_BASE_ATTACK 0x400 +#define QUERY_BASE_DEFENSE 0x800 +#define QUERY_REASON 0x1000 +#define QUERY_REASON_CARD 0x2000 +#define QUERY_EQUIP_CARD 0x4000 +#define QUERY_TARGET_CARD 0x8000 +#define QUERY_OVERLAY_CARD 0x10000 +#define QUERY_COUNTERS 0x20000 +#define QUERY_OWNER 0x40000 +#define QUERY_STATUS 0x80000 +#define QUERY_LSCALE 0x200000 +#define QUERY_RSCALE 0x400000 +#define QUERY_LINK 0x800000 + +//Link markers +#define LINK_MARKER_BOTTOM_LEFT 0x001 +#define LINK_MARKER_BOTTOM 0x002 +#define LINK_MARKER_BOTTOM_RIGHT 0x004 +#define LINK_MARKER_LEFT 0x008 +#define LINK_MARKER_RIGHT 0x020 +#define LINK_MARKER_TOP_LEFT 0x040 +#define LINK_MARKER_TOP 0x080 +#define LINK_MARKER_TOP_RIGHT 0x100 + +//Messages +#define MSG_RETRY 1 +#define MSG_HINT 2 +//#define MSG_WAITING 3 +//#define MSG_START 4 +#define MSG_WIN 5 +//#define MSG_UPDATE_DATA 6 +#define MSG_UPDATE_CARD 7 +//#define MSG_REQUEST_DECK 8 +#define MSG_SELECT_BATTLECMD 10 +#define MSG_SELECT_IDLECMD 11 +#define MSG_SELECT_EFFECTYN 12 +#define MSG_SELECT_YESNO 13 +#define MSG_SELECT_OPTION 14 +#define MSG_SELECT_CARD 15 +#define MSG_SELECT_CHAIN 16 +#define MSG_SELECT_PLACE 18 +#define MSG_SELECT_POSITION 19 +#define MSG_SELECT_TRIBUTE 20 +#define MSG_SELECT_COUNTER 22 +#define MSG_SELECT_SUM 23 +#define MSG_SELECT_DISFIELD 24 +#define MSG_SORT_CARD 25 +#define MSG_SELECT_UNSELECT_CARD 26 +#define MSG_CONFIRM_DECKTOP 30 +#define MSG_CONFIRM_CARDS 31 +#define MSG_SHUFFLE_DECK 32 +#define MSG_SHUFFLE_HAND 33 +//#define MSG_REFRESH_DECK 34 +#define MSG_SWAP_GRAVE_DECK 35 +#define MSG_SHUFFLE_SET_CARD 36 +#define MSG_REVERSE_DECK 37 +#define MSG_DECK_TOP 38 +#define MSG_SHUFFLE_EXTRA 39 +#define MSG_NEW_TURN 40 +#define MSG_NEW_PHASE 41 +#define MSG_CONFIRM_EXTRATOP 42 +#define MSG_MOVE 50 +#define MSG_POS_CHANGE 53 +#define MSG_SET 54 +#define MSG_SWAP 55 +#define MSG_FIELD_DISABLED 56 +#define MSG_SUMMONING 60 +#define MSG_SUMMONED 61 +#define MSG_SPSUMMONING 62 +#define MSG_SPSUMMONED 63 +#define MSG_FLIPSUMMONING 64 +#define MSG_FLIPSUMMONED 65 +#define MSG_CHAINING 70 +#define MSG_CHAINED 71 +#define MSG_CHAIN_SOLVING 72 +#define MSG_CHAIN_SOLVED 73 +#define MSG_CHAIN_END 74 +#define MSG_CHAIN_NEGATED 75 +#define MSG_CHAIN_DISABLED 76 +//#define MSG_CARD_SELECTED 80 +#define MSG_RANDOM_SELECTED 81 +#define MSG_BECOME_TARGET 83 +#define MSG_DRAW 90 +#define MSG_DAMAGE 91 +#define MSG_RECOVER 92 +#define MSG_EQUIP 93 +#define MSG_LPUPDATE 94 +//#define MSG_UNEQUIP 95 +#define MSG_CARD_TARGET 96 +#define MSG_CANCEL_TARGET 97 +#define MSG_PAY_LPCOST 100 +#define MSG_ADD_COUNTER 101 +#define MSG_REMOVE_COUNTER 102 +#define MSG_ATTACK 110 +#define MSG_BATTLE 111 +#define MSG_ATTACK_DISABLED 112 +#define MSG_DAMAGE_STEP_START 113 +#define MSG_DAMAGE_STEP_END 114 +#define MSG_MISSED_EFFECT 120 +//#define MSG_BE_CHAIN_TARGET 121 +//#define MSG_CREATE_RELATION 122 +//#define MSG_RELEASE_RELATION 123 +#define MSG_TOSS_COIN 130 +#define MSG_TOSS_DICE 131 +#define MSG_ROCK_PAPER_SCISSORS 132 +#define MSG_HAND_RES 133 +#define MSG_ANNOUNCE_RACE 140 +#define MSG_ANNOUNCE_ATTRIB 141 +#define MSG_ANNOUNCE_CARD 142 +#define MSG_ANNOUNCE_NUMBER 143 +#define MSG_CARD_HINT 160 +#define MSG_TAG_SWAP 161 +#define MSG_RELOAD_FIELD 162 // Debug.ReloadFieldEnd() or query_field_info() +#define MSG_AI_NAME 163 // Debug.AIName() +#define MSG_SHOW_HINT 164 // Debug.ShowHint() +#define MSG_PLAYER_HINT 165 +#define MSG_MATCH_KILL 170 +#define MSG_CUSTOM_MSG 180 + +#define MSG_RESET_TIME 221 + +//Hints +#define HINT_EVENT 1 +#define HINT_MESSAGE 2 +#define HINT_SELECTMSG 3 +#define HINT_OPSELECTED 4 +#define HINT_EFFECT 5 +#define HINT_RACE 6 +#define HINT_ATTRIB 7 +#define HINT_CODE 8 +#define HINT_NUMBER 9 +#define HINT_CARD 10 +#define HINT_ZONE 11 +//custom hints in KoishiPro for custom sound +#define HINT_MUSIC 21 +#define HINT_SOUND 22 +#define HINT_MUSIC_OGG 23 +// +#define CHINT_TURN 1 +#define CHINT_CARD 2 +#define CHINT_RACE 3 +#define CHINT_ATTRIBUTE 4 +#define CHINT_NUMBER 5 +#define CHINT_DESC_ADD 6 +#define CHINT_DESC_REMOVE 7 +// +#define PHINT_DESC_ADD 6 +#define PHINT_DESC_REMOVE 7 +// +#define EDESC_OPERATION 1 +#define EDESC_RESET 2 + +// +#define OPCODE_ADD 0x40000000 +#define OPCODE_SUB 0x40000001 +#define OPCODE_MUL 0x40000002 +#define OPCODE_DIV 0x40000003 +#define OPCODE_AND 0x40000004 +#define OPCODE_OR 0x40000005 +#define OPCODE_NEG 0x40000006 +#define OPCODE_NOT 0x40000007 +#define OPCODE_ISCODE 0x40000100 +#define OPCODE_ISSETCARD 0x40000101 +#define OPCODE_ISTYPE 0x40000102 +#define OPCODE_ISRACE 0x40000103 +#define OPCODE_ISATTRIBUTE 0x40000104 + +//Player +#define PLAYER_NONE 2 // +#define PLAYER_ALL 3 // +#define PLAYER_SELFDES 5 // + +//Phase +#define PHASE_DRAW 0x01 +#define PHASE_STANDBY 0x02 +#define PHASE_MAIN1 0x04 +#define PHASE_BATTLE_START 0x08 +#define PHASE_BATTLE_STEP 0x10 +#define PHASE_DAMAGE 0x20 +#define PHASE_DAMAGE_CAL 0x40 +#define PHASE_BATTLE 0x80 +#define PHASE_MAIN2 0x100 +#define PHASE_END 0x200 + +//Options +#define DUEL_TEST_MODE 0x01 +#define DUEL_ATTACK_FIRST_TURN 0x02 +//#define DUEL_OLD_REPLAY 0x04 +#define DUEL_OBSOLETE_RULING 0x08 +#define DUEL_PSEUDO_SHUFFLE 0x10 +#define DUEL_TAG_MODE 0x20 +#define DUEL_SIMPLE_AI 0x40 +#define DUEL_RETURN_DECK_TOP 0x80 +#define DUEL_REVEAL_DECK_SEQ 0x100 + +//Activity +#define ACTIVITY_SUMMON 1 +#define ACTIVITY_NORMALSUMMON 2 +#define ACTIVITY_SPSUMMON 3 +#define ACTIVITY_FLIPSUMMON 4 +#define ACTIVITY_ATTACK 5 +#define ACTIVITY_BATTLE_PHASE 6 +#define ACTIVITY_CHAIN 7 +//card datas for Duel.ReadCard / Card.SetCardData, arranged by database format +#define CARDDATA_CODE 1 +#define CARDDATA_ALIAS 2 +#define CARDDATA_SETCODE 3 +#define CARDDATA_TYPE 4 +#define CARDDATA_LEVEL 5 +#define CARDDATA_ATTRIBUTE 6 +#define CARDDATA_RACE 7 +#define CARDDATA_ATTACK 8 +#define CARDDATA_DEFENSE 9 +#define CARDDATA_LSCALE 10 +#define CARDDATA_RSCALE 11 +#define CARDDATA_LINK_MARKER 12 + +//get effects for Card.GetCardRegistered +#define GETEFFECT_INITIAL 0x1 +#define GETEFFECT_GAIN 0x2 +#define GETEFFECT_COPY 0x4 +#define GETEFFECT_GRANT 0x8 +#define GETEFFECT_ALL 0xf + +#endif /* COMMON_H_ */ diff --git a/core/duel.cpp b/core/duel.cpp new file mode 100644 index 0000000..a3f5fc2 --- /dev/null +++ b/core/duel.cpp @@ -0,0 +1,160 @@ +/* + * duel.cpp + * + * Created on: 2010-5-2 + * Author: Argon + */ + +#include +#include "duel.h" +#include "interpreter.h" +#include "field.h" +#include "card.h" +#include "effect.h" +#include "group.h" +#include "ocgapi.h" +#include "buffer.h" + +duel::duel(uint32_t create_flags) { + lua = new interpreter(this, false, create_flags); + game_field = new field(this); + game_field->temp_card = new_card(TEMP_CARD_ID); + game_field->rose_card = 0; + game_field->rose_level = 0; + message_buffer.reserve(SIZE_MESSAGE_BUFFER); +#ifdef _WIN32 + _set_error_mode(_OUT_TO_MSGBOX); +#endif // _WIN32 +} +duel::~duel() { + for(auto& pcard : cards) + delete pcard; + for(auto& pgroup : groups) + delete pgroup; + for(auto& peffect : effects) + delete peffect; + delete game_field; + delete lua; +} +void duel::clear() { + for(auto& pcard : cards) { + lua->unregister_card(pcard); + delete pcard; + } + for(auto& pgroup : groups) { + lua->unregister_group(pgroup); + delete pgroup; + } + for(auto& peffect : effects) { + lua->unregister_effect(peffect); + delete peffect; + } + delete game_field; + cards.clear(); + groups.clear(); + effects.clear(); + assumes.clear(); + sgroups.clear(); + uncopy.clear(); + game_field = new field(this); + game_field->temp_card = new_card(TEMP_CARD_ID); + game_field->rose_card = 0; + game_field->rose_level = 0; +} +card* duel::new_card(uint32_t code) { + card* pcard = new card(this); + cards.insert(pcard); + if (code != TEMP_CARD_ID) + ::read_card(code, &(pcard->data)); + pcard->data.code = code; + lua->register_card(pcard); + return pcard; +} +group* duel::register_group(group* pgroup) { + groups.insert(pgroup); + if(lua->call_depth) + sgroups.insert(pgroup); + lua->register_group(pgroup); + return pgroup; +} +group* duel::new_group() { + group* pgroup = new group(this); + return register_group(pgroup); +} +group* duel::new_group(card* pcard) { + group* pgroup = new group(this, pcard); + return register_group(pgroup); +} +group* duel::new_group(const card_set& cset) { + group* pgroup = new group(this, cset); + return register_group(pgroup); +} +effect* duel::new_effect() { + effect* peffect = new effect(this); + effects.insert(peffect); + lua->register_effect(peffect); + return peffect; +} +void duel::delete_card(card* pcard) { + lua->unregister_card(pcard); + cards.erase(pcard); + delete pcard; +} +void duel::delete_group(group* pgroup) { + lua->unregister_group(pgroup); + groups.erase(pgroup); + sgroups.erase(pgroup); + delete pgroup; +} +void duel::delete_effect(effect* peffect) { + lua->unregister_effect(peffect); + effects.erase(peffect); + delete peffect; +} +int32_t duel::read_buffer(byte* buf) { + auto size = buffer_size(); + if (size) + std::memcpy(buf, message_buffer.data(), size); + return (int32_t)size; +} +void duel::release_script_group() { + for(auto& pgroup : sgroups) { + if(pgroup->is_readonly == GTYPE_DEFAULT) { + lua->unregister_group(pgroup); + groups.erase(pgroup); + delete pgroup; + } + } + sgroups.clear(); +} +void duel::restore_assumes() { + for(auto& pcard : assumes) + pcard->assume_type = 0; + assumes.clear(); +} +void duel::write_buffer(const void* data, size_t size) { + vector_write_block(message_buffer, data, size); +} +void duel::write_buffer32(uint32_t value) { + vector_write(message_buffer, value); +} +void duel::write_buffer16(uint16_t value) { + vector_write(message_buffer, value); +} +void duel::write_buffer8(uint8_t value) { + vector_write(message_buffer, value); +} +void duel::clear_buffer() { + message_buffer.clear(); +} +void duel::set_responsei(int32_t resp) { + game_field->returns.ivalue[0] = resp; +} +void duel::set_responseb(byte* resp) { + std::memcpy(game_field->returns.bvalue, resp, SIZE_RETURN_VALUE); +} +int32_t duel::get_next_integer(int32_t l, int32_t h) { + if (rng_version == 1) + return random.get_random_integer_v1(l, h); + return random.get_random_integer_v2(l, h); +} diff --git a/core/duel.h b/core/duel.h new file mode 100644 index 0000000..3182261 --- /dev/null +++ b/core/duel.h @@ -0,0 +1,78 @@ +/* + * duel.h + * + * Created on: 2010-4-8 + * Author: Argon + */ + +#ifndef DUEL_H_ +#define DUEL_H_ + +#include "common.h" +#include "sort.h" +#include "mtrandom.h" +#include "metadata.h" +#include +#include +#include +#include +#include + +class card; +class group; +class effect; +class field; +class interpreter; + +using card_set = std::set; + +class duel { +public: + char strbuffer[256]{}; + int32_t rng_version{ 2 }; + interpreter* lua; + field* game_field; + mtrandom random; + metadata meta; + + std::vector message_buffer; + std::unordered_set cards; + std::unordered_set assumes; + std::unordered_set groups; + std::unordered_set sgroups; + std::unordered_set effects; + std::unordered_set uncopy; + + std::unordered_map registry; + + explicit duel(uint32_t create_flags = 0); + ~duel(); + void clear(); + + uint32_t buffer_size() const { + return (uint32_t)message_buffer.size() & PROCESSOR_BUFFER_LEN; + } + card* new_card(uint32_t code); + group* new_group(); + group* new_group(card* pcard); + group* new_group(const card_set& cset); + effect* new_effect(); + void delete_card(card* pcard); + void delete_group(group* pgroup); + void delete_effect(effect* peffect); + void release_script_group(); + void restore_assumes(); + int32_t read_buffer(byte* buf); + void write_buffer(const void* data, size_t size); + void write_buffer32(uint32_t value); + void write_buffer16(uint16_t value); + void write_buffer8(uint8_t value); + void clear_buffer(); + void set_responsei(int32_t resp); + void set_responseb(byte* resp); + int32_t get_next_integer(int32_t l, int32_t h); +private: + group* register_group(group* pgroup); +}; + +#endif /* DUEL_H_ */ diff --git a/core/effect.cpp b/core/effect.cpp new file mode 100644 index 0000000..926acae --- /dev/null +++ b/core/effect.cpp @@ -0,0 +1,891 @@ +/* + * effect.cpp + * + * Created on: 2010-5-7 + * Author: Argon + */ + +#include "effect.h" +#include "card.h" +#include "duel.h" +#include "group.h" +#include "interpreter.h" + +bool effect_sort_id(const effect* e1, const effect* e2) { + int32_t is_single1 = e1->is_initial_single(); + int32_t is_single2 = e2->is_initial_single(); + if (is_single1 != is_single2) + return is_single1 > is_single2; + return e1->id < e2->id; +} +// return: code is an event reserved for EFFECT_TYPE_CONTINUOUS or not +bool is_continuous_event(uint32_t code) { + if (code & EVENT_CUSTOM) + return false; + else if (code & 0xf0000) // EVENT_ADD_COUNTER, EVENT_REMOVE_COUNTER + return false; + else if (code & 0xf000) // EVENT_PHASE_START must be continuous, but other EVENT_PHASE must not be + return !!(code & EVENT_PHASE_START); + else + return continuous_event.find(code) != continuous_event.end(); +} + +effect::effect(duel* pd) + : pduel(pd) { + label.reserve(4); +} +int32_t effect::is_disable_related() const { + if (code == EFFECT_IMMUNE_EFFECT || code == EFFECT_DISABLE || code == EFFECT_CANNOT_DISABLE || code == EFFECT_FORBIDDEN) + return TRUE; + return FALSE; +} +int32_t effect::is_self_destroy_related() const { + if(code == EFFECT_UNIQUE_CHECK || code == EFFECT_SELF_DESTROY || code == EFFECT_SELF_TOGRAVE) + return TRUE; + return FALSE; +} +int32_t effect::is_can_be_forbidden() const { + if (is_flag(EFFECT_FLAG_CANNOT_DISABLE) && !is_flag(EFFECT_FLAG_CANNOT_NEGATE)) + return FALSE; + return TRUE; +} +// check if a single/field/equip effect is available +// check properties: range, EFFECT_FLAG_OWNER_RELATE, STATUS_BATTLE_DESTROYED, STATUS_EFFECT_ENABLED, disabled/forbidden +// check fucntions: condition +int32_t effect::is_available(int32_t neglect_disabled) { + if (type & EFFECT_TYPE_ACTIONS) + return FALSE; + if ((type & (EFFECT_TYPE_SINGLE | EFFECT_TYPE_XMATERIAL)) && !(type & EFFECT_TYPE_FIELD)) { + card* phandler = get_handler(); + card* powner = get_owner(); + if (phandler->current.controler == PLAYER_NONE) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && !in_range(phandler)) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && !phandler->get_status(STATUS_EFFECT_ENABLED) && !is_flag(EFFECT_FLAG_IMMEDIATELY_APPLY)) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && (phandler->current.location & LOCATION_ONFIELD) && !phandler->is_position(POS_FACEUP)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && is_can_be_forbidden() && powner->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(powner == phandler && is_can_be_forbidden() && phandler->get_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && powner->is_status(STATUS_DISABLED)) + return FALSE; + if(powner == phandler && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && phandler->get_status(STATUS_DISABLED)) + return FALSE; + } + if (type & EFFECT_TYPE_EQUIP) { + if(handler->current.controler == PLAYER_NONE) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && is_can_be_forbidden() && owner->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(owner == handler && is_can_be_forbidden() && handler->get_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && owner->is_status(STATUS_DISABLED)) + return FALSE; + if(owner == handler && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && handler->get_status(STATUS_DISABLED)) + return FALSE; + if(!is_flag(EFFECT_FLAG_SET_AVAILABLE)) { + if(!(handler->get_status(STATUS_EFFECT_ENABLED))) + return FALSE; + if(!handler->is_position(POS_FACEUP)) + return FALSE; + } + } + if (type & (EFFECT_TYPE_FIELD | EFFECT_TYPE_TARGET)) { + card* phandler = get_handler(); + card* powner = get_owner(); + if (!is_flag(EFFECT_FLAG_FIELD_ONLY)) { + if(phandler->current.controler == PLAYER_NONE) + return FALSE; + if(!in_range(phandler)) + return FALSE; + if(!phandler->get_status(STATUS_EFFECT_ENABLED) && !is_flag(EFFECT_FLAG_IMMEDIATELY_APPLY)) + return FALSE; + if (phandler->current.is_location(LOCATION_ONFIELD) && !phandler->is_position(POS_FACEUP)) + return FALSE; + if (phandler->current.is_stzone() && (phandler->data.type & (TYPE_SPELL | TYPE_TRAP)) && phandler->is_affected_by_effect(EFFECT_CHANGE_TYPE)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && is_can_be_forbidden() && powner->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(powner == phandler && is_can_be_forbidden() && phandler->get_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && powner->is_status(STATUS_DISABLED)) + return FALSE; + if(powner == phandler && !(is_flag(EFFECT_FLAG_CANNOT_DISABLE) || neglect_disabled) && phandler->get_status(STATUS_DISABLED)) + return FALSE; + if(phandler->is_status(STATUS_BATTLE_DESTROYED) && !is_flag(EFFECT_FLAG2_AVAILABLE_BD)) + return FALSE; + } + } + if (!condition) + return TRUE; + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + int32_t res = pduel->lua->check_condition(condition, 1); + if(res) { + if(!(status & EFFECT_STATUS_AVAILABLE)) + id = pduel->game_field->infos.field_id++; + status |= EFFECT_STATUS_AVAILABLE; + } else + status &= ~EFFECT_STATUS_AVAILABLE; + return res; +} +// check if a count limit effect counter is available, which should be available even if the effect is disabled +int32_t effect::limit_counter_is_available() { + return is_available(TRUE); +} +// check if a effect is EFFECT_TYPE_SINGLE and is ready +// check: range, enabled, condition +int32_t effect::is_single_ready() { + if(type & EFFECT_TYPE_ACTIONS) + return FALSE; + if((type & (EFFECT_TYPE_SINGLE | EFFECT_TYPE_XMATERIAL)) && !(type & EFFECT_TYPE_FIELD)) { + card* phandler = get_handler(); + if(phandler->current.controler == PLAYER_NONE) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && !in_range(phandler)) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && !phandler->get_status(STATUS_EFFECT_ENABLED) && !is_flag(EFFECT_FLAG_IMMEDIATELY_APPLY)) + return FALSE; + if(is_flag(EFFECT_FLAG_SINGLE_RANGE) && (phandler->current.location & LOCATION_ONFIELD) && !phandler->is_position(POS_FACEUP)) + return FALSE; + } + else + return FALSE; + if(!condition) + return TRUE; + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + int32_t res = pduel->lua->check_condition(condition, 1); + return res; +} +int32_t effect::check_count_limit(uint8_t playerid) { + if(is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + if(count_limit == 0) + return FALSE; + if(count_code) { + uint32_t limit_code = count_code & MAX_CARD_ID; + uint32_t limit_type = count_code & 0xf0000000U; + int32_t count = count_limit_max; + if(limit_code == EFFECT_COUNT_CODE_SINGLE) { + if(pduel->game_field->get_effect_code(limit_type | get_handler()->activate_count_id, PLAYER_NONE) >= count) + return FALSE; + } else { + if(pduel->game_field->get_effect_code(count_code, playerid) >= count) + return FALSE; + } + } + } + return TRUE; +} +// check activate in hand/in set turn +int32_t effect::get_required_handorset_effects(effect_set* eset, uint8_t playerid, const tevent& e, int32_t neglect_loc) { + eset->clear(); + if(!(type & EFFECT_TYPE_ACTIVATE)) + return 1; + int32_t ecode = 0; + if (handler->current.location == LOCATION_HAND && !neglect_loc) + { + if(handler->data.type & TYPE_TRAP) + ecode = EFFECT_TRAP_ACT_IN_HAND; + else if((handler->data.type & TYPE_SPELL) && pduel->game_field->infos.turn_player != playerid) { + if(handler->data.type & TYPE_QUICKPLAY) + ecode = EFFECT_QP_ACT_IN_NTPHAND; + else + return FALSE; + } + else if((handler->data.type & TYPE_PENDULUM) && pduel->game_field->infos.turn_player != playerid && is_flag(EFFECT_FLAG2_SPOSITCH)) { + ecode = EFFECT_QP_ACT_IN_NTPHAND; + } + } + else if (handler->current.location == LOCATION_SZONE) + { + if((handler->data.type & TYPE_TRAP) && handler->get_status(STATUS_SET_TURN)) + ecode = EFFECT_TRAP_ACT_IN_SET_TURN; + if((handler->data.type & TYPE_SPELL) && (handler->data.type & TYPE_QUICKPLAY) && handler->get_status(STATUS_SET_TURN)) + ecode = EFFECT_QP_ACT_IN_SET_TURN; + } + if (!ecode) + return 1; + int32_t available = 0; + effect_set tmp_eset; + handler->filter_effect(ecode, &tmp_eset); + if(!tmp_eset.size()) + return available; + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_player = playerid; + pduel->game_field->save_lp_cost(); + for(effect_set::size_type i = 0; i < tmp_eset.size(); ++i) { + auto peffect = tmp_eset[i]; + if(peffect->check_count_limit(playerid)) { + pduel->game_field->core.reason_effect = peffect; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + if(pduel->lua->check_condition(peffect->cost, 10)) { + available = 2; + eset->push_back(peffect); + } + } + } + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + pduel->game_field->restore_lp_cost(); + return available; +} +// check if an EFFECT_TYPE_ACTIONS effect can be activated +// for triggering effects, it checks EFFECT_FLAG_DAMAGE_STEP, EFFECT_FLAG_SET_AVAILABLE +int32_t effect::is_activateable(uint8_t playerid, const tevent& e, int32_t neglect_cond, int32_t neglect_cost, int32_t neglect_target, int32_t neglect_loc, int32_t neglect_faceup) { + if(!(type & EFFECT_TYPE_ACTIONS)) + return FALSE; + if(!check_count_limit(playerid)) + return FALSE; + if (!is_flag(EFFECT_FLAG_FIELD_ONLY)) { + if (type & EFFECT_TYPE_ACTIVATE) { + if(handler->current.controler != playerid) + return FALSE; + if(pduel->game_field->check_unique_onfield(handler, playerid, LOCATION_SZONE)) + return FALSE; + if(!(handler->data.type & TYPE_COUNTER)) { + if((code < 1132 || code > 1149) && pduel->game_field->infos.phase == PHASE_DAMAGE && !is_flag(EFFECT_FLAG_DAMAGE_STEP) + && !pduel->game_field->get_cteffect(this, playerid, FALSE)) + return FALSE; + if((code < 1134 || code > 1136) && pduel->game_field->infos.phase == PHASE_DAMAGE_CAL && !is_flag(EFFECT_FLAG_DAMAGE_CAL) + && !pduel->game_field->get_cteffect(this, playerid, FALSE)) + return FALSE; + } + uint32_t zone = 0xff; + if(!(handler->data.type & (TYPE_FIELD | TYPE_PENDULUM)) && is_flag(EFFECT_FLAG_LIMIT_ZONE)) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + zone = get_value(7); + if(!zone) + zone = 0xff; + } + if(handler->current.location == LOCATION_SZONE) { + if(handler->is_position(POS_FACEUP)) + return FALSE; + if(handler->equiping_target) + return FALSE; + if(!(handler->data.type & (TYPE_FIELD | TYPE_PENDULUM)) && is_flag(EFFECT_FLAG_LIMIT_ZONE) && !(zone & (1u << handler->current.sequence))) + return FALSE; + } else { + if((handler->data.type & TYPE_MONSTER) && !is_flag(EFFECT_FLAG2_ACTIVATE_MONSTER_SZONE)) { + if(!(handler->data.type & TYPE_PENDULUM)) + return FALSE; + if(!pduel->game_field->is_location_useable(playerid, LOCATION_PZONE, 0) + && !pduel->game_field->is_location_useable(playerid, LOCATION_PZONE, 1)) + return FALSE; + } else if(!(handler->data.type & TYPE_FIELD) + && pduel->game_field->get_useable_count(handler, playerid, LOCATION_SZONE, playerid, LOCATION_REASON_TOFIELD, zone) <= 0) + return FALSE; + } + // check activate in hand/in set turn + effect_set eset; + if(!get_required_handorset_effects(&eset, playerid, e, neglect_loc)) + return FALSE; + if(handler->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(handler->is_affected_by_effect(EFFECT_CANNOT_TRIGGER)) + return FALSE; + } else if(!(type & EFFECT_TYPE_CONTINUOUS)) { + card* phandler = get_handler(); + if(!(phandler->get_type() & TYPE_MONSTER) && (get_active_type(FALSE) & TYPE_MONSTER)) + return FALSE; + if((type & EFFECT_TYPE_QUICK_O) && is_flag(EFFECT_FLAG_DELAY) && !in_range(phandler)) + return FALSE; + if(!neglect_faceup && (phandler->current.location & (LOCATION_ONFIELD | LOCATION_REMOVED))) { + if(!phandler->is_position(POS_FACEUP) && !is_flag(EFFECT_FLAG_SET_AVAILABLE)) + return FALSE; + if(phandler->is_position(POS_FACEUP) && !phandler->is_status(STATUS_EFFECT_ENABLED)) + return FALSE; + } + if(!(type & (EFFECT_TYPE_FLIP | EFFECT_TYPE_TRIGGER_F)) && !((type & EFFECT_TYPE_TRIGGER_O) && (type & EFFECT_TYPE_SINGLE))) { + if((code < 1132 || code > 1149) && pduel->game_field->infos.phase == PHASE_DAMAGE && !is_flag(EFFECT_FLAG_DAMAGE_STEP)) + return FALSE; + if((code < 1134 || code > 1136) && pduel->game_field->infos.phase == PHASE_DAMAGE_CAL && !is_flag(EFFECT_FLAG_DAMAGE_CAL)) + return FALSE; + } + if(phandler->current.location == LOCATION_OVERLAY) + return FALSE; + if(phandler->current.location == LOCATION_DECK + || pduel->game_field->core.duel_rule >= MASTER_RULE_2020 && phandler->current.location == LOCATION_EXTRA && (phandler->current.position & POS_FACEDOWN)) { + if((type & EFFECT_TYPE_SINGLE) && code != EVENT_TO_DECK) + return FALSE; + if((type & EFFECT_TYPE_FIELD) && !(range & (LOCATION_DECK | LOCATION_EXTRA))) + return FALSE; + } + if (phandler->current.is_stzone() && (phandler->data.type & (TYPE_SPELL|TYPE_TRAP)) && phandler->is_affected_by_effect(EFFECT_CHANGE_TYPE)) + return FALSE; + if((type & EFFECT_TYPE_FIELD) && (phandler->current.controler != playerid) && !is_flag(EFFECT_FLAG_BOTH_SIDE | EFFECT_FLAG_EVENT_PLAYER)) + return FALSE; + if(phandler->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(phandler->is_affected_by_effect(EFFECT_CANNOT_TRIGGER)) + return FALSE; + } else { + card* phandler = get_handler(); + if(!is_flag(EFFECT_FLAG2_AVAILABLE_BD) && (type & EFFECT_TYPE_FIELD) && phandler->is_status(STATUS_BATTLE_DESTROYED)) + return FALSE; + if(((type & EFFECT_TYPE_FIELD) || ((type & EFFECT_TYPE_SINGLE) && is_flag(EFFECT_FLAG_SINGLE_RANGE))) + && (phandler->current.location & LOCATION_ONFIELD) + && (!phandler->is_position(POS_FACEUP) || !phandler->is_status(STATUS_EFFECT_ENABLED))) + return FALSE; + if((type & EFFECT_TYPE_SINGLE) && is_flag(EFFECT_FLAG_SINGLE_RANGE) && !in_range(phandler)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && is_can_be_forbidden() && owner->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(phandler == owner && is_can_be_forbidden() && phandler->is_status(STATUS_FORBIDDEN)) + return FALSE; + if(is_flag(EFFECT_FLAG_OWNER_RELATE) && !is_flag(EFFECT_FLAG_CANNOT_DISABLE) && owner->is_status(STATUS_DISABLED)) + return FALSE; + if(phandler == owner && !is_flag(EFFECT_FLAG_CANNOT_DISABLE) && phandler->is_status(STATUS_DISABLED)) + return FALSE; + } + } else { + if((get_owner_player() != playerid) && !is_flag(EFFECT_FLAG_BOTH_SIDE)) + return FALSE; + } + pduel->game_field->save_lp_cost(); + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = this; + pduel->game_field->core.reason_player = playerid; + int32_t result = TRUE; + if(!(type & EFFECT_TYPE_CONTINUOUS)) + result = is_action_check(playerid); + if(result) + result = is_activate_ready(playerid, e, neglect_cond, neglect_cost, neglect_target); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + pduel->game_field->restore_lp_cost(); + return result; +} +// check functions: value of EFFECT_CANNOT_ACTIVATE, target, cost of EFFECT_ACTIVATE_COST +int32_t effect::is_action_check(uint8_t playerid) { + effect_set eset; + pduel->game_field->filter_player_effect(playerid, EFFECT_CANNOT_ACTIVATE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(2)) + return FALSE; + } + eset.clear(); + pduel->game_field->filter_player_effect(playerid, EFFECT_ACTIVATE_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(eset[i]->target, 3)) + continue; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(eset[i]->cost, 3)) + return FALSE; + } + return TRUE; +} +// check functions: condition, cost(chk=0), target(chk=0) +int32_t effect::is_activate_ready(effect* reason_effect, uint8_t playerid, const tevent& e, int32_t neglect_cond, int32_t neglect_cost, int32_t neglect_target) { + if(!neglect_cond && condition) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards, PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(condition, 8)) { + return FALSE; + } + } + if(!neglect_cost && !(type & EFFECT_TYPE_CONTINUOUS)) { + reason_effect->cost_checked = TRUE; + if(cost) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards, PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(cost, 9)) { + reason_effect->cost_checked = FALSE; + return FALSE; + } + } + } else { + reason_effect->cost_checked = FALSE; + } + if(!neglect_target && target) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards, PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(target, 9)) { + reason_effect->cost_checked = FALSE; + return FALSE; + } + } + reason_effect->cost_checked = FALSE; + return TRUE; +} +int32_t effect::is_activate_ready(uint8_t playerid, const tevent& e, int32_t neglect_cond, int32_t neglect_cost, int32_t neglect_target) { + return is_activate_ready(this, playerid, e, neglect_cond, neglect_cost, neglect_target); +} +// check functions: condition +int32_t effect::is_condition_check(uint8_t playerid, const tevent& e) { + card* phandler = get_handler(); + if(!(type & EFFECT_TYPE_ACTIVATE) && (phandler->current.location & (LOCATION_ONFIELD | LOCATION_REMOVED)) && !phandler->is_position(POS_FACEUP)) + return FALSE; + if(!condition) + return TRUE; + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = this; + pduel->game_field->core.reason_player = playerid; + pduel->game_field->save_lp_cost(); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(condition, 8)) { + pduel->game_field->restore_lp_cost(); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return FALSE; + } + pduel->game_field->restore_lp_cost(); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + return TRUE; +} +int32_t effect::is_activate_check(uint8_t playerid, const tevent& e, int32_t neglect_cond, int32_t neglect_cost, int32_t neglect_target) { + pduel->game_field->save_lp_cost(); + effect* oreason = pduel->game_field->core.reason_effect; + uint8_t op = pduel->game_field->core.reason_player; + pduel->game_field->core.reason_effect = this; + pduel->game_field->core.reason_player = playerid; + int32_t result = is_activate_ready(playerid, e, neglect_cond, neglect_cost, neglect_target); + pduel->game_field->core.reason_effect = oreason; + pduel->game_field->core.reason_player = op; + pduel->game_field->restore_lp_cost(); + return result; +} +int32_t effect::is_target(card* pcard) { + if(type & EFFECT_TYPE_ACTIONS) + return FALSE; + if(type & (EFFECT_TYPE_SINGLE | EFFECT_TYPE_EQUIP | EFFECT_TYPE_XMATERIAL) && !(type & EFFECT_TYPE_FIELD)) + return TRUE; + if((type & EFFECT_TYPE_TARGET) && !(type & EFFECT_TYPE_FIELD)) { + return is_fit_target_function(pcard); + } + if(!is_flag(EFFECT_FLAG_SET_AVAILABLE) && code != EFFECT_REVEAL_ONFIELD + && (pcard->current.location & LOCATION_ONFIELD) && !pcard->is_position(POS_FACEUP)) + return FALSE; + if(!is_flag(EFFECT_FLAG_IGNORE_RANGE)) { + if(pcard->is_treated_as_not_on_field()) + return FALSE; + if(is_flag(EFFECT_FLAG_SPSUM_PARAM)) + return FALSE; + if(is_flag(EFFECT_FLAG_ABSOLUTE_TARGET)) { + if(pcard->current.controler == 0) { + if(!pcard->current.is_location(s_range)) + return FALSE; + } else { + if(!pcard->current.is_location(o_range)) + return FALSE; + } + } else { + if(pcard->current.controler == get_handler_player()) { + if(!pcard->current.is_location(s_range)) + return FALSE; + } else { + if(!pcard->current.is_location(o_range)) + return FALSE; + } + } + } + return is_fit_target_function(pcard); +} +int32_t effect::is_fit_target_function(card* pcard) { + if(target) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if (!pduel->lua->check_condition(target, 2)) { + return FALSE; + } + } + return TRUE; +} +int32_t effect::is_target_player(uint8_t playerid) { + if(!is_flag(EFFECT_FLAG_PLAYER_TARGET)) + return FALSE; + uint8_t self = get_handler_player(); + if(is_flag(EFFECT_FLAG_ABSOLUTE_TARGET)) { + if(s_range && playerid == 0) + return TRUE; + if(o_range && playerid == 1) + return TRUE; + } else { + if(s_range && self == playerid) + return TRUE; + if(o_range && self != playerid) + return TRUE; + } + return FALSE; +} +int32_t effect::is_player_effect_target(card* pcard) { + if(target) { + handler->pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + handler->pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if(!handler->pduel->lua->check_condition(target, 2)) { + return FALSE; + } + } + return TRUE; +} +int32_t effect::is_immuned(card* pcard) { + const effect_set_v& effects = pcard->immune_effect; + for (effect_set::size_type i = 0; i < effects.size(); ++i) { + effect* peffect = effects[i]; + if(peffect->is_available() && peffect->value) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if(peffect->check_value_condition(2)) + return TRUE; + } + } + return FALSE; +} +int32_t effect::is_chainable(uint8_t tp) { + if(!(type & EFFECT_TYPE_ACTIONS)) + return FALSE; + int32_t sp = get_speed(); + // Curse of Field is the exception + if((type & EFFECT_TYPE_ACTIVATE) && (sp <= 1) && !is_flag(EFFECT_FLAG2_COF)) + return FALSE; + if(pduel->game_field->core.current_chain.size()) { + if(!is_flag(EFFECT_FLAG_FIELD_ONLY) && (type & EFFECT_TYPE_TRIGGER_O) + && (get_handler()->current.location == LOCATION_HAND)) { + if(pduel->game_field->core.current_chain.rbegin()->triggering_effect->get_speed() > 2) + return FALSE; + } else if(sp < pduel->game_field->core.current_chain.rbegin()->triggering_effect->get_speed()) + return FALSE; + } + for(const auto& ch_lim : pduel->game_field->core.chain_limit) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(ch_lim.player, PARAM_TYPE_INT); + pduel->lua->add_param(tp, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(ch_lim.function, 3)) + return FALSE; + } + for(const auto& ch_lim_p : pduel->game_field->core.chain_limit_p) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT); + pduel->lua->add_param(ch_lim_p.player, PARAM_TYPE_INT); + pduel->lua->add_param(tp, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(ch_lim_p.function, 3)) + return FALSE; + } + return TRUE; +} +int32_t effect::is_hand_trigger() const { + return (range & LOCATION_HAND) && (type & EFFECT_TYPE_TRIGGER_O) && get_code_type() != CODE_PHASE; +} +int32_t effect::is_initial_single() const { + return (type & EFFECT_TYPE_SINGLE) && is_flag(EFFECT_FLAG_SINGLE_RANGE) && is_flag(EFFECT_FLAG_INITIAL); +} +int32_t effect::is_monster_effect() const { + if (range & (LOCATION_SZONE | LOCATION_FZONE | LOCATION_PZONE)) + return FALSE; + return TRUE; +} +//return: this can be reset by reset_level or not +//RESET_DISABLE is valid only when owner == handler +int32_t effect::reset(uint32_t reset_level, uint32_t reset_type) { + switch (reset_type) { + case RESET_EVENT: { + if(!(reset_flag & RESET_EVENT)) + return FALSE; + if(owner != handler) + reset_level &= ~RESET_DISABLE; + if(reset_level & 0xffff0000 & reset_flag) + return TRUE; + return FALSE; + break; + } + case RESET_CARD: { + return owner && (owner->data.code == reset_level); + break; + } + case RESET_PHASE: { + if(!(reset_flag & RESET_PHASE)) + return FALSE; + uint8_t pid = get_owner_player(); + uint8_t tp = handler->pduel->game_field->infos.turn_player; + if((((reset_flag & RESET_SELF_TURN) && pid == tp) || ((reset_flag & RESET_OPPO_TURN) && pid != tp)) + && (reset_level & 0x3ff & reset_flag)) + --reset_count; + if(reset_count == 0) + return TRUE; + return FALSE; + break; + } + case RESET_CODE: { + return (code == reset_level) && (type & EFFECT_TYPE_SINGLE) && !(type & EFFECT_TYPE_ACTIONS); + break; + } + case RESET_COPY: { + return copy_id == reset_level; + break; + } + } + return FALSE; +} +void effect::dec_count(uint8_t playerid) { + if(!is_flag(EFFECT_FLAG_COUNT_LIMIT)) + return; + if(count_limit == 0) + return; + if(count_code == 0 || is_flag(EFFECT_FLAG_NO_TURN_RESET)) + count_limit -= 1; + if(count_code) { + uint32_t limit_code = count_code & MAX_CARD_ID; + uint32_t limit_type = count_code & 0xf0000000; + if(limit_code == EFFECT_COUNT_CODE_SINGLE) + pduel->game_field->add_effect_code(limit_type | get_handler()->activate_count_id, PLAYER_NONE); + else + pduel->game_field->add_effect_code(count_code, playerid); + } +} +void effect::recharge() { + if(is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + count_limit = count_limit_max; + } +} +int32_t effect::get_value(uint32_t extraargs) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + int32_t res = pduel->lua->get_function_value(value, 1 + extraargs); + return res; + } else { + pduel->lua->params.clear(); + return value; + } +} +int32_t effect::get_value(card* pcard, uint32_t extraargs) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + int32_t res = pduel->lua->get_function_value(value, 2 + extraargs); + return res; + } else { + pduel->lua->params.clear(); + return value; + } +} +int32_t effect::get_value(effect* peffect, uint32_t extraargs) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + int32_t res = pduel->lua->get_function_value(value, 2 + extraargs); + return res; + } else { + pduel->lua->params.clear(); + return value; + } +} +void effect::get_value(uint32_t extraargs, std::vector& result) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + pduel->lua->get_function_value(value, 1 + extraargs, result); + } else { + pduel->lua->params.clear(); + result.push_back(value); + } +} +void effect::get_value(card* pcard, uint32_t extraargs, std::vector& result) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + pduel->lua->get_function_value(value, 2 + extraargs, result); + } else { + pduel->lua->params.clear(); + result.push_back(value); + } +} +void effect::get_value(effect* peffect, uint32_t extraargs, std::vector& result) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT, TRUE); + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + pduel->lua->get_function_value(value, 2 + extraargs, result); + } else { + pduel->lua->params.clear(); + result.push_back(value); + } +} +int32_t effect::get_integer_value() { + return is_flag(EFFECT_FLAG_FUNC_VALUE) ? 0 : value; +} +int32_t effect::check_value_condition(uint32_t extraargs) { + if(is_flag(EFFECT_FLAG_FUNC_VALUE)) { + pduel->lua->add_param(this, PARAM_TYPE_EFFECT, TRUE); + int32_t res = pduel->lua->check_condition(value, 1 + extraargs); + return res; + } else { + pduel->lua->params.clear(); + return value; + } +} +void* effect::get_label_object() { + return pduel->lua->get_ref_object(label_object); +} +int32_t effect::get_speed() { + if(!(type & EFFECT_TYPE_ACTIONS)) + return 0; + if(type & (EFFECT_TYPE_TRIGGER_O | EFFECT_TYPE_TRIGGER_F | EFFECT_TYPE_IGNITION)) + return 1; + else if(type & (EFFECT_TYPE_QUICK_O | EFFECT_TYPE_QUICK_F)) + return 2; + else if(type & EFFECT_TYPE_ACTIVATE) { + if(handler->data.type & TYPE_MONSTER) + return 0; + else if(handler->data.type & TYPE_SPELL) { + if(handler->data.type & TYPE_QUICKPLAY) + return 2; + return 1; + } else { + if (handler->data.type & TYPE_COUNTER) + return 3; + return 2; + } + } + return 0; +} +effect* effect::clone() { + effect* ceffect = pduel->new_effect(); + int32_t ref = ceffect->ref_handle; + *ceffect = *this; + ceffect->ref_handle = ref; + ceffect->handler = 0; + if(condition) + ceffect->condition = pduel->lua->clone_function_ref(condition); + if(cost) + ceffect->cost = pduel->lua->clone_function_ref(cost); + if(target) + ceffect->target = pduel->lua->clone_function_ref(target); + if(operation) + ceffect->operation = pduel->lua->clone_function_ref(operation); + if(value && is_flag(EFFECT_FLAG_FUNC_VALUE)) + ceffect->value = pduel->lua->clone_function_ref(value); + return ceffect; +} +card* effect::get_owner() const { + if (type & EFFECT_TYPE_XMATERIAL) { + if (active_handler) + return active_handler; + if (handler->overlay_target) + return handler->overlay_target; + if (last_handler) + return last_handler; + } + return owner; +} +uint8_t effect::get_owner_player() const { + if(effect_owner != PLAYER_NONE) + return effect_owner; + return get_owner()->current.controler; +} +card* effect::get_handler() const { + if (type & EFFECT_TYPE_XMATERIAL) { + if (active_handler) + return active_handler; + if (handler->overlay_target) + return handler->overlay_target; + if (last_handler) + return last_handler; + } + return handler; +} +uint8_t effect::get_handler_player() const { + if(is_flag(EFFECT_FLAG_FIELD_ONLY)) + return effect_owner; + return get_handler()->current.controler; +} +int32_t effect::in_range(card* pcard) const { + if(type & EFFECT_TYPE_XMATERIAL) + return handler->overlay_target ? TRUE : FALSE; + return pcard->current.is_location(range); +} +int32_t effect::in_range(const chain& ch) const { + if(type & EFFECT_TYPE_XMATERIAL) + return handler->overlay_target ? TRUE : FALSE; + return range & ch.triggering_location; +} +void effect::set_activate_location() { + card* phandler = get_handler(); + active_location = phandler->current.location; + active_sequence = phandler->current.sequence; +} +void effect::set_active_type() { + card* phandler = get_handler(); + active_type = phandler->get_type(); + if(active_type & TYPE_TRAPMONSTER) + active_type &= ~TYPE_TRAP; + if((type & EFFECT_TYPE_ACTIVATE) && is_flag(EFFECT_FLAG2_SPOSITCH)) + active_type |= TYPE_QUICKPLAY; +} +uint32_t effect::get_active_type(uint8_t uselast) { + if(type & EFFECT_TYPES_CHAIN_LINK) { + if(active_type && uselast) + return active_type; + else if((type & EFFECT_TYPE_ACTIVATE) && (get_handler()->data.type & TYPE_PENDULUM)) + { + if(is_flag(EFFECT_FLAG2_SPOSITCH)) + return TYPE_PENDULUM + TYPE_SPELL + TYPE_QUICKPLAY; + else + return TYPE_PENDULUM + TYPE_SPELL; + } + else + return get_handler()->get_type(); + } else + return owner->get_type(); +} +code_type effect::get_code_type() const { + // start from the highest bit + if (code & 0xf0000000) + return CODE_CUSTOM; + else if (code & 0xf0000) + return CODE_COUNTER; + else if (code & 0xf000) + return CODE_PHASE; + else + return CODE_VALUE; +} diff --git a/core/effect.h b/core/effect.h new file mode 100644 index 0000000..566c612 --- /dev/null +++ b/core/effect.h @@ -0,0 +1,668 @@ +/* + * effect.h + * + * Created on: 2010-3-13 + * Author: Argon + */ + +#ifndef EFFECT_H_ +#define EFFECT_H_ + +#include "common.h" +#include "field.h" +#include "effectset.h" +#include "metadata.h" +#include + +class card; +class duel; +class group; +class effect; +struct tevent; +enum effect_flag : uint64_t; +enum effect_flag2 : uint64_t; +enum effect_category :uint64_t; +enum code_type : int32_t; + +bool is_continuous_event(uint32_t code); + +class effect { +public: + int32_t ref_handle{ 0 }; + duel* pduel{ nullptr }; + card* owner{ nullptr }; + card* handler{ nullptr }; + uint8_t effect_owner{ PLAYER_NONE }; + uint32_t description{ 0 }; + uint32_t code{ 0 }; + uint32_t id{ 0 }; + uint32_t type{ 0 }; + uint16_t copy_id{ 0 }; + uint16_t range{ 0 }; + uint16_t s_range{ 0 }; + uint16_t o_range{ 0 }; + uint8_t count_limit{ 0 }; //left count of activation + uint8_t count_limit_max{ 0 }; //max count of activation + uint16_t status{ 0 }; + int32_t reset_count{ 0 }; + uint32_t reset_flag{ 0 }; + uint32_t count_code{ 0 }; + uint64_t category{ 0 }; + uint64_t flag[2]{}; + uint32_t hint_timing[2]{}; + uint32_t card_type{ 0 }; + uint32_t active_type{ 0 }; + uint16_t active_location{ 0 }; + uint16_t active_sequence{ 0 }; + card* active_handler{ nullptr }; + card* last_handler{ nullptr }; + std::vector label; + int32_t label_object{ 0 }; + int32_t condition{ 0 }; + int32_t cost{ 0 }; + int32_t target{ 0 }; + int32_t value{ 0 }; + int32_t operation{ 0 }; + uint8_t cost_checked{ FALSE }; + uint8_t is_granted{ 0 }; + + effect_set required_handorset_effects; + LuaParamType object_type{ PARAM_TYPE_INT }; + metadata meta; + + explicit effect(duel* pd); + ~effect() = default; + + int32_t is_disable_related() const; + int32_t is_self_destroy_related() const; + int32_t is_can_be_forbidden() const; + int32_t is_available(int32_t neglect_disabled = FALSE); + int32_t limit_counter_is_available(); + int32_t is_single_ready(); + int32_t check_count_limit(uint8_t playerid); + int32_t get_required_handorset_effects(effect_set* eset, uint8_t playerid, const tevent& e, int32_t neglect_loc = FALSE); + int32_t is_activateable(uint8_t playerid, const tevent &e, int32_t neglect_cond = FALSE, int32_t neglect_cost = FALSE, int32_t neglect_target = FALSE, int32_t neglect_loc = FALSE, int32_t neglect_faceup = FALSE); + int32_t is_action_check(uint8_t playerid); + int32_t is_activate_ready(effect* reason_effect, uint8_t playerid, const tevent& e, int32_t neglect_cond = FALSE, int32_t neglect_cost = FALSE, int32_t neglect_target = FALSE); + int32_t is_activate_ready(uint8_t playerid, const tevent& e, int32_t neglect_cond = FALSE, int32_t neglect_cost = FALSE, int32_t neglect_target = FALSE); + int32_t is_condition_check(uint8_t playerid, const tevent& e); + int32_t is_activate_check(uint8_t playerid, const tevent& e, int32_t neglect_cond = FALSE, int32_t neglect_cost = FALSE, int32_t neglect_target = FALSE); + int32_t is_target(card* pcard); + int32_t is_fit_target_function(card* pcard); + int32_t is_target_player(uint8_t playerid); + int32_t is_player_effect_target(card* pcard); + int32_t is_immuned(card* pcard); + int32_t is_chainable(uint8_t tp); + int32_t is_hand_trigger() const; + int32_t is_initial_single() const; + int32_t is_monster_effect() const; + int32_t reset(uint32_t reset_level, uint32_t reset_type); + void dec_count(uint8_t playerid = PLAYER_NONE); + void recharge(); + int32_t get_value(uint32_t extraargs = 0); + int32_t get_value(card* pcard, uint32_t extraargs = 0); + int32_t get_value(effect* peffect, uint32_t extraargs = 0); + void get_value(uint32_t extraargs, std::vector& result); + void get_value(card* pcard, uint32_t extraargs, std::vector& result); + void get_value(effect* peffect, uint32_t extraargs, std::vector& result); + int32_t get_integer_value(); + int32_t check_value_condition(uint32_t extraargs = 0); + void* get_label_object(); + int32_t get_speed(); + effect* clone(); + card* get_owner() const; + uint8_t get_owner_player() const; + card* get_handler() const; + uint8_t get_handler_player() const; + int32_t in_range(card* pcard) const; + int32_t in_range(const chain& ch) const; + void set_activate_location(); + void set_active_type(); + uint32_t get_active_type(uint8_t uselast = TRUE); + code_type get_code_type() const; + + bool is_flag(effect_flag x) const { + return !!(flag[0] & x); + } + bool is_flag(effect_flag2 x) const { + return !!(flag[1] & x); + } +}; + +// KoishiPro effects +#define EFFECT_CHANGE_LINK_MARKER_KOISHI 710253 +#define EFFECT_ADD_LINK_MARKER_KOISHI 37564151 +#define EFFECT_REMOVE_LINK_MARKER_KOISHI 37564152 +#define EFFECT_CANNOT_LOSE_KOISHI 37564153 +#define EFFECT_EXTRA_TOMAIN_KOISHI 37564154 +#define EFFECT_OVERLAY_REMOVE_COST_CHANGE_KOISHI 37564155 +#define EFFECT_ADD_SUMMON_TYPE_KOISHI 37564158 +#define EFFECT_REMOVE_SUMMON_TYPE_KOISHI 37564159 +#define EFFECT_CHANGE_SUMMON_TYPE_KOISHI 37564160 +#define EFFECT_CHANGE_SUMMON_LOCATION_KOISHI 37564161 +#define EFFECT_LINK_SPELL_KOISHI 37564162 +#define EFFECT_MAP_OF_HEAVEN 77702007 + +//status +#define EFFECT_STATUS_AVAILABLE 0x0001 +//#define EFFECT_STATUS_ACTIVATED 0x0002 +#define EFFECT_STATUS_SPSELF 0x0004 + +#define EFFECT_COUNT_CODE_OATH 0x10000000U +#define EFFECT_COUNT_CODE_DUEL 0x20000000U +#define EFFECT_COUNT_CODE_CHAIN 0x40000000U +#define EFFECT_COUNT_CODE_SINGLE 0x1U + +//========== Reset ========== +#define RESET_SELF_TURN 0x10000000 +#define RESET_OPPO_TURN 0x20000000 +#define RESET_PHASE 0x40000000 +#define RESET_CHAIN 0x80000000 +#define RESET_EVENT 0x1000 +#define RESET_CARD 0x2000 +#define RESET_CODE 0x4000 +#define RESET_COPY 0x8000 + +#define RESET_DISABLE 0x00010000 +#define RESET_TURN_SET 0x00020000 +#define RESET_TOGRAVE 0x00040000 +#define RESET_REMOVE 0x00080000 +#define RESET_TEMP_REMOVE 0x00100000 +#define RESET_TOHAND 0x00200000 +#define RESET_TODECK 0x00400000 +#define RESET_LEAVE 0x00800000 +#define RESET_TOFIELD 0x01000000 +#define RESET_CONTROL 0x02000000 +#define RESET_OVERLAY 0x04000000 +#define RESET_MSCHANGE 0x08000000 + +constexpr uint32_t RESETS_STANDARD = RESET_TOFIELD | RESET_LEAVE | RESET_TODECK | RESET_TOHAND | RESET_TEMP_REMOVE | RESET_REMOVE | RESET_TOGRAVE | RESET_TURN_SET; + +//========== Types ========== +#define EFFECT_TYPE_SINGLE 0x0001 // +#define EFFECT_TYPE_FIELD 0x0002 // +#define EFFECT_TYPE_EQUIP 0x0004 // +#define EFFECT_TYPE_ACTIONS 0x0008 // +#define EFFECT_TYPE_ACTIVATE 0x0010 // +#define EFFECT_TYPE_FLIP 0x0020 // +#define EFFECT_TYPE_IGNITION 0x0040 // +#define EFFECT_TYPE_TRIGGER_O 0x0080 // +#define EFFECT_TYPE_QUICK_O 0x0100 // +#define EFFECT_TYPE_TRIGGER_F 0x0200 // +#define EFFECT_TYPE_QUICK_F 0x0400 // +#define EFFECT_TYPE_CONTINUOUS 0x0800 // +#define EFFECT_TYPE_XMATERIAL 0x1000 // +#define EFFECT_TYPE_GRANT 0x2000 // +#define EFFECT_TYPE_TARGET 0x4000 // + +constexpr uint32_t EFFECT_TYPES_TRIGGER_LIKE = EFFECT_TYPE_ACTIVATE | EFFECT_TYPE_TRIGGER_O | EFFECT_TYPE_TRIGGER_F | EFFECT_TYPE_QUICK_O | EFFECT_TYPE_QUICK_F; +constexpr uint32_t EFFECT_TYPES_CHAIN_LINK = EFFECT_TYPES_TRIGGER_LIKE | EFFECT_TYPE_FLIP | EFFECT_TYPE_IGNITION; + +//========== Flags ========== +enum effect_flag : uint64_t { + EFFECT_FLAG_INITIAL = 0x0001, + EFFECT_FLAG_FUNC_VALUE = 0x0002, + EFFECT_FLAG_COUNT_LIMIT = 0x0004, + EFFECT_FLAG_FIELD_ONLY = 0x0008, + EFFECT_FLAG_CARD_TARGET = 0x0010, + EFFECT_FLAG_IGNORE_RANGE = 0x0020, + EFFECT_FLAG_ABSOLUTE_TARGET = 0x0040, + EFFECT_FLAG_IGNORE_IMMUNE = 0x0080, + EFFECT_FLAG_SET_AVAILABLE = 0x0100, + EFFECT_FLAG_CANNOT_NEGATE = 0x0200, + EFFECT_FLAG_CANNOT_DISABLE = 0x0400, + EFFECT_FLAG_PLAYER_TARGET = 0x0800, + EFFECT_FLAG_BOTH_SIDE = 0x1000, + EFFECT_FLAG_COPY = 0x2000, + EFFECT_FLAG_DAMAGE_STEP = 0x4000, + EFFECT_FLAG_DAMAGE_CAL = 0x8000, + EFFECT_FLAG_DELAY = 0x10000, + EFFECT_FLAG_SINGLE_RANGE = 0x20000, + EFFECT_FLAG_UNCOPYABLE = 0x40000, + EFFECT_FLAG_OATH = 0x80000, + EFFECT_FLAG_SPSUM_PARAM = 0x100000, +// EFFECT_FLAG_REPEAT = 0x200000, + EFFECT_FLAG_NO_TURN_RESET = 0x400000, + EFFECT_FLAG_EVENT_PLAYER = 0x800000, + EFFECT_FLAG_OWNER_RELATE = 0x1000000, + EFFECT_FLAG_CANNOT_INACTIVATE = 0x2000000, + EFFECT_FLAG_CLIENT_HINT = 0x4000000, + EFFECT_FLAG_CONTINUOUS_TARGET = 0x8000000, + EFFECT_FLAG_LIMIT_ZONE = 0x10000000, + EFFECT_FLAG_ACTIVATE_CONDITION = 0x20000000, +// EFFECT_FLAG_CVAL_CHECK = 0x40000000, + EFFECT_FLAG_IMMEDIATELY_APPLY = 0x80000000, + EFFECT_FLAG_COIN = 0x100000000, + EFFECT_FLAG_DICE = 0x200000000, + EFFECT_FLAG_FUSION_SUMMON = 0x400000000, +}; +enum effect_flag2 : uint64_t { + EFFECT_FLAG2_REPEAT_UPDATE = 0x0001, + EFFECT_FLAG2_COF = 0x0002, + EFFECT_FLAG2_WICKED = 0x0004, + EFFECT_FLAG2_OPTION = 0x0008, + EFFECT_FLAG2_SPOSITCH = 0x0100, // flag2 from 0x0100 are koishipro use + EFFECT_FLAG2_AVAILABLE_BD = 0x0200, + EFFECT_FLAG2_ACTIVATE_MONSTER_SZONE = 0x0400, +}; +constexpr effect_flag operator|(effect_flag flag1, effect_flag flag2) { + return static_cast(static_cast(flag1) | static_cast(flag2)); +} +constexpr uint64_t INTERNAL_FLAGS = EFFECT_FLAG_INITIAL | EFFECT_FLAG_COPY | EFFECT_FLAG_FUNC_VALUE | EFFECT_FLAG_COUNT_LIMIT | EFFECT_FLAG_FIELD_ONLY | EFFECT_FLAG_ABSOLUTE_TARGET; + +//Category +enum effect_category : uint64_t { + CATEGORY_DESTROY = 0x1, + CATEGORY_RELEASE = 0x2, + CATEGORY_REMOVE = 0x4, + CATEGORY_TOHAND = 0x8, + CATEGORY_TODECK = 0x10, + CATEGORY_TOGRAVE = 0x20, + CATEGORY_DECKDES = 0x40, + CATEGORY_HANDES = 0x80, + CATEGORY_SUMMON = 0x100, + CATEGORY_SPECIAL_SUMMON = 0x200, + CATEGORY_TOKEN = 0x400, + CATEGORY_GRAVE_ACTION = 0x800, + CATEGORY_POSITION = 0x1000, + CATEGORY_CONTROL = 0x2000, + CATEGORY_DISABLE = 0x4000, + CATEGORY_DISABLE_SUMMON = 0x8000, + CATEGORY_DRAW = 0x10000, + CATEGORY_SEARCH = 0x20000, + CATEGORY_EQUIP = 0x40000, + CATEGORY_DAMAGE = 0x80000, + CATEGORY_RECOVER = 0x100000, + CATEGORY_ATKCHANGE = 0x200000, + CATEGORY_DEFCHANGE = 0x400000, + CATEGORY_COUNTER = 0x800000, + CATEGORY_COIN = 0x1000000, + CATEGORY_DICE = 0x2000000, + CATEGORY_LEAVE_GRAVE = 0x4000000, + CATEGORY_GRAVE_SPSUMMON = 0x8000000, + CATEGORY_NEGATE = 0x10000000, + CATEGORY_ANNOUNCE = 0x20000000, + CATEGORY_FUSION_SUMMON = 0x40000000, + CATEGORY_TOEXTRA = 0x80000000, + CATEGORY_MSET = 0x100000000, + CATEGORY_SSET = 0x200000000, +}; + +const std::map category_checklist{ + {CATEGORY_COIN, EFFECT_FLAG_COIN}, + {CATEGORY_DICE, EFFECT_FLAG_DICE}, + {CATEGORY_FUSION_SUMMON, EFFECT_FLAG_FUSION_SUMMON}, +}; + +//========== Codes ========== +#define EFFECT_IMMUNE_EFFECT 1 // +#define EFFECT_DISABLE 2 // +#define EFFECT_CANNOT_DISABLE 3 // +#define EFFECT_SET_CONTROL 4 // +#define EFFECT_CANNOT_CHANGE_CONTROL 5 // +#define EFFECT_CANNOT_ACTIVATE 6 // +#define EFFECT_CANNOT_TRIGGER 7 // +#define EFFECT_DISABLE_EFFECT 8 // +#define EFFECT_DISABLE_CHAIN 9 // +#define EFFECT_DISABLE_TRAPMONSTER 10 // +#define EFFECT_CANNOT_INACTIVATE 12 // +#define EFFECT_CANNOT_DISEFFECT 13 // +#define EFFECT_CANNOT_CHANGE_POSITION 14 // +#define EFFECT_TRAP_ACT_IN_HAND 15 // +#define EFFECT_TRAP_ACT_IN_SET_TURN 16 // +#define EFFECT_REMAIN_FIELD 17 // +#define EFFECT_MONSTER_SSET 18 // +#define EFFECT_CANNOT_SUMMON 20 // +#define EFFECT_CANNOT_FLIP_SUMMON 21 // +#define EFFECT_CANNOT_SPECIAL_SUMMON 22 // +#define EFFECT_CANNOT_MSET 23 // +#define EFFECT_CANNOT_SSET 24 // +#define EFFECT_CANNOT_DRAW 25 // +#define EFFECT_CANNOT_DISABLE_SUMMON 26 // +#define EFFECT_CANNOT_DISABLE_SPSUMMON 27 // +#define EFFECT_SET_SUMMON_COUNT_LIMIT 28 // +#define EFFECT_EXTRA_SUMMON_COUNT 29 // +#define EFFECT_SPSUMMON_CONDITION 30 // +#define EFFECT_REVIVE_LIMIT 31 // +#define EFFECT_SUMMON_PROC 32 // +#define EFFECT_LIMIT_SUMMON_PROC 33 // +#define EFFECT_SPSUMMON_PROC 34 // +#define EFFECT_EXTRA_SET_COUNT 35 // +#define EFFECT_SET_PROC 36 // +#define EFFECT_LIMIT_SET_PROC 37 // +#define EFFECT_DIVINE_LIGHT 38 // +#define EFFECT_CANNOT_DISABLE_FLIP_SUMMON 39 // +#define EFFECT_INDESTRUCTABLE 40 // +#define EFFECT_INDESTRUCTABLE_EFFECT 41 // +#define EFFECT_INDESTRUCTABLE_BATTLE 42 // +#define EFFECT_UNRELEASABLE_SUM 43 // +#define EFFECT_UNRELEASABLE_NONSUM 44 // +#define EFFECT_DESTROY_SUBSTITUTE 45 // +#define EFFECT_CANNOT_RELEASE 46 // +#define EFFECT_INDESTRUCTABLE_COUNT 47 // +#define EFFECT_UNRELEASABLE_EFFECT 48 // +#define EFFECT_DESTROY_REPLACE 50 // +#define EFFECT_RELEASE_REPLACE 51 // +#define EFFECT_SEND_REPLACE 52 // +#define EFFECT_CANNOT_DISCARD_HAND 55 // +#define EFFECT_CANNOT_DISCARD_DECK 56 // +#define EFFECT_CANNOT_USE_AS_COST 57 // +#define EFFECT_CANNOT_PLACE_COUNTER 58 // +#define EFFECT_CANNOT_TO_GRAVE_AS_COST 59 // +#define EFFECT_LEAVE_FIELD_REDIRECT 60 // +#define EFFECT_TO_HAND_REDIRECT 61 // +#define EFFECT_TO_DECK_REDIRECT 62 // +#define EFFECT_TO_GRAVE_REDIRECT 63 // +#define EFFECT_REMOVE_REDIRECT 64 // +#define EFFECT_CANNOT_TO_HAND 65 // +#define EFFECT_CANNOT_TO_DECK 66 // +#define EFFECT_CANNOT_REMOVE 67 // +#define EFFECT_CANNOT_TO_GRAVE 68 // +#define EFFECT_CANNOT_TURN_SET 69 // +#define EFFECT_CANNOT_BE_BATTLE_TARGET 70 // +#define EFFECT_CANNOT_BE_EFFECT_TARGET 71 // +#define EFFECT_IGNORE_BATTLE_TARGET 72 // +#define EFFECT_CANNOT_DIRECT_ATTACK 73 // +#define EFFECT_DIRECT_ATTACK 74 // +#define EFFECT_DUAL_STATUS 75 // +#define EFFECT_EQUIP_LIMIT 76 // +#define EFFECT_DUAL_SUMMONABLE 77 // +#define EFFECT_UNION_LIMIT 78 // +#define EFFECT_REVERSE_DAMAGE 80 // +#define EFFECT_REVERSE_RECOVER 81 // +#define EFFECT_CHANGE_DAMAGE 82 // +#define EFFECT_REFLECT_DAMAGE 83 // +#define EFFECT_CANNOT_ATTACK 85 // +#define EFFECT_CANNOT_ATTACK_ANNOUNCE 86 // +#define EFFECT_CANNOT_CHANGE_POS_E 87 // +#define EFFECT_ACTIVATE_COST 90 // +#define EFFECT_SUMMON_COST 91 // +#define EFFECT_SPSUMMON_COST 92 // +#define EFFECT_FLIPSUMMON_COST 93 // +#define EFFECT_MSET_COST 94 // +#define EFFECT_SSET_COST 95 // +#define EFFECT_ATTACK_COST 96 // + +#define EFFECT_UPDATE_ATTACK 100 // +#define EFFECT_SET_ATTACK 101 // +#define EFFECT_SET_ATTACK_FINAL 102 // +#define EFFECT_SET_BASE_ATTACK 103 // +#define EFFECT_UPDATE_DEFENSE 104 // +#define EFFECT_SET_DEFENSE 105 // +#define EFFECT_SET_DEFENSE_FINAL 106 // +#define EFFECT_SET_BASE_DEFENSE 107 // +#define EFFECT_REVERSE_UPDATE 108 // +#define EFFECT_SWAP_AD 109 // +#define EFFECT_SWAP_BASE_AD 110 // +#define EFFECT_SET_BASE_ATTACK_FINAL 111 // +#define EFFECT_SET_BASE_DEFENSE_FINAL 112 // +#define EFFECT_ADD_CODE 113 // +#define EFFECT_CHANGE_CODE 114 // +#define EFFECT_ADD_TYPE 115 // +#define EFFECT_REMOVE_TYPE 116 // +#define EFFECT_CHANGE_TYPE 117 // +#define EFFECT_ADD_RACE 120 // +#define EFFECT_REMOVE_RACE 121 // +#define EFFECT_CHANGE_RACE 122 // +#define EFFECT_ADD_ATTRIBUTE 125 // +#define EFFECT_REMOVE_ATTRIBUTE 126 // +#define EFFECT_CHANGE_ATTRIBUTE 127 // +#define EFFECT_UPDATE_LEVEL 130 // +#define EFFECT_CHANGE_LEVEL 131 // +#define EFFECT_UPDATE_RANK 132 // +#define EFFECT_CHANGE_RANK 133 // +#define EFFECT_UPDATE_LSCALE 134 // +#define EFFECT_CHANGE_LSCALE 135 // +#define EFFECT_UPDATE_RSCALE 136 // +#define EFFECT_CHANGE_RSCALE 137 // +#define EFFECT_SET_POSITION 140 // +#define EFFECT_SELF_DESTROY 141 // +#define EFFECT_SELF_TOGRAVE 142 +#define EFFECT_DOUBLE_TRIBUTE 150 +#define EFFECT_DECREASE_TRIBUTE 151 +#define EFFECT_DECREASE_TRIBUTE_SET 152 +#define EFFECT_EXTRA_RELEASE 153 +#define EFFECT_TRIBUTE_LIMIT 154 +#define EFFECT_EXTRA_RELEASE_SUM 155 +//#define EFFECT_TRIPLE_TRIBUTE 156 +#define EFFECT_ADD_EXTRA_TRIBUTE 157 +#define EFFECT_EXTRA_RELEASE_NONSUM 158 +#define EFFECT_PUBLIC 160 +#define EFFECT_COUNTER_PERMIT 0x10000 +#define EFFECT_COUNTER_LIMIT 0x20000 +#define EFFECT_RCOUNTER_REPLACE 0x30000 +#define EFFECT_LPCOST_CHANGE 170 +#define EFFECT_LPCOST_REPLACE 171 +#define EFFECT_SKIP_DP 180 +#define EFFECT_SKIP_SP 181 +#define EFFECT_SKIP_M1 182 +#define EFFECT_SKIP_BP 183 +#define EFFECT_SKIP_M2 184 +#define EFFECT_CANNOT_BP 185 +#define EFFECT_CANNOT_M2 186 +#define EFFECT_CANNOT_EP 187 +#define EFFECT_SKIP_TURN 188 +#define EFFECT_SKIP_EP 189 +#define EFFECT_DEFENSE_ATTACK 190 +#define EFFECT_MUST_ATTACK 191 +#define EFFECT_FIRST_ATTACK 192 +#define EFFECT_ATTACK_ALL 193 +#define EFFECT_EXTRA_ATTACK 194 +//#define EFFECT_MUST_BE_ATTACKED 195 +#define EFFECT_ONLY_BE_ATTACKED 196 +#define EFFECT_ATTACK_DISABLED 197 +#define EFFECT_NO_BATTLE_DAMAGE 200 +#define EFFECT_AVOID_BATTLE_DAMAGE 201 +#define EFFECT_REFLECT_BATTLE_DAMAGE 202 +#define EFFECT_PIERCE 203 +#define EFFECT_BATTLE_DESTROY_REDIRECT 204 +#define EFFECT_BATTLE_DAMAGE_TO_EFFECT 205 +#define EFFECT_BOTH_BATTLE_DAMAGE 206 +#define EFFECT_ALSO_BATTLE_DAMAGE 207 +#define EFFECT_CHANGE_BATTLE_DAMAGE 208 +#define EFFECT_TOSS_COIN_REPLACE 220 +#define EFFECT_TOSS_DICE_REPLACE 221 +#define EFFECT_FUSION_MATERIAL 230 +#define EFFECT_CHAIN_MATERIAL 231 +#define EFFECT_EXTRA_SYNCHRO_MATERIAL 232 +#define EFFECT_XYZ_MATERIAL 233 +#define EFFECT_FUSION_SUBSTITUTE 234 +#define EFFECT_CANNOT_BE_FUSION_MATERIAL 235 +#define EFFECT_CANNOT_BE_SYNCHRO_MATERIAL 236 +#define EFFECT_SYNCHRO_MATERIAL_CUSTOM 237 +#define EFFECT_CANNOT_BE_XYZ_MATERIAL 238 +#define EFFECT_CANNOT_BE_LINK_MATERIAL 239 +#define EFFECT_SYNCHRO_LEVEL 240 +#define EFFECT_RITUAL_LEVEL 241 +#define EFFECT_XYZ_LEVEL 242 +#define EFFECT_EXTRA_RITUAL_MATERIAL 243 +#define EFFECT_NONTUNER 244 +#define EFFECT_OVERLAY_REMOVE_REPLACE 245 +#define EFFECT_SCRAP_CHIMERA 246 +#define EFFECT_TUNE_MAGICIAN_X 247 +#define EFFECT_PRE_MONSTER 250 +#define EFFECT_MATERIAL_CHECK 251 +#define EFFECT_DISABLE_FIELD 260 +#define EFFECT_USE_EXTRA_MZONE 261 +#define EFFECT_USE_EXTRA_SZONE 262 +#define EFFECT_MAX_MZONE 263 +#define EFFECT_MAX_SZONE 264 +#define EFFECT_MUST_USE_MZONE 265 +#define EFFECT_HAND_LIMIT 270 +#define EFFECT_DRAW_COUNT 271 +#define EFFECT_SPIRIT_DONOT_RETURN 280 +#define EFFECT_SPIRIT_MAYNOT_RETURN 281 +#define EFFECT_CHANGE_ENVIRONMENT 290 +#define EFFECT_NECRO_VALLEY 291 +#define EFFECT_FORBIDDEN 292 +#define EFFECT_NECRO_VALLEY_IM 293 +#define EFFECT_REVERSE_DECK 294 +#define EFFECT_REMOVE_BRAINWASHING 295 +#define EFFECT_BP_TWICE 296 +#define EFFECT_UNIQUE_CHECK 297 +#define EFFECT_MATCH_KILL 300 +#define EFFECT_SYNCHRO_CHECK 310 +#define EFFECT_QP_ACT_IN_NTPHAND 311 +#define EFFECT_MUST_BE_SMATERIAL 312 +#define EFFECT_TO_GRAVE_REDIRECT_CB 313 +#define EFFECT_CHANGE_INVOLVING_BATTLE_DAMAGE 314 +//#define EFFECT_CHANGE_RANK_FINAL 315 +#define EFFECT_MUST_BE_FMATERIAL 316 +#define EFFECT_MUST_BE_XMATERIAL 317 +#define EFFECT_MUST_BE_LMATERIAL 318 +#define EFFECT_SPSUMMON_PROC_G 320 +#define EFFECT_SPSUMMON_COUNT_LIMIT 330 +#define EFFECT_LEFT_SPSUMMON_COUNT 331 +#define EFFECT_CANNOT_SELECT_BATTLE_TARGET 332 +#define EFFECT_CANNOT_SELECT_EFFECT_TARGET 333 +#define EFFECT_ADD_SETCODE 334 +#define EFFECT_NO_EFFECT_DAMAGE 335 +//#define EFFECT_UNSUMMONABLE_CARD 336 +#define EFFECT_DISCARD_COST_CHANGE 338 +#define EFFECT_HAND_SYNCHRO 339 +#define EFFECT_ADD_FUSION_CODE 340 +#define EFFECT_ADD_FUSION_SETCODE 341 +#define EFFECT_ONLY_ATTACK_MONSTER 343 +#define EFFECT_MUST_ATTACK_MONSTER 344 +#define EFFECT_PATRICIAN_OF_DARKNESS 345 +#define EFFECT_EXTRA_ATTACK_MONSTER 346 +#define EFFECT_UNION_STATUS 347 +#define EFFECT_OLDUNION_STATUS 348 +//#define EFFECT_ADD_FUSION_ATTRIBUTE 349 +//#define EFFECT_REMOVE_FUSION_ATTRIBUTE 350 +#define EFFECT_CHANGE_FUSION_ATTRIBUTE 351 +#define EFFECT_EXTRA_FUSION_MATERIAL 352 +#define EFFECT_TUNER_MATERIAL_LIMIT 353 +#define EFFECT_ADD_LINK_CODE 354 +//#define EFFECT_ADD_LINK_SETCODE 355 +#define EFFECT_ADD_LINK_ATTRIBUTE 356 +#define EFFECT_ADD_LINK_RACE 357 +#define EFFECT_EXTRA_LINK_MATERIAL 358 +#define EFFECT_QP_ACT_IN_SET_TURN 359 +#define EFFECT_EXTRA_PENDULUM_SUMMON 360 +#define EFFECT_MATERIAL_LIMIT 361 +#define EFFECT_SET_BATTLE_ATTACK 362 +#define EFFECT_SET_BATTLE_DEFENSE 363 +#define EFFECT_OVERLAY_RITUAL_MATERIAL 364 +#define EFFECT_CHANGE_GRAVE_ATTRIBUTE 365 +#define EFFECT_CHANGE_GRAVE_RACE 366 +#define EFFECT_ACTIVATION_COUNT_LIMIT 367 +#define EFFECT_LIMIT_SPECIAL_SUMMON_POSITION 368 +#define EFFECT_TUNER 369 +#define EFFECT_KAISER_COLOSSEUM 370 +#define EFFECT_REPLACE_DAMAGE 371 +#define EFFECT_XYZ_MIN_COUNT 372 +#define EFFECT_SYNCHRO_LEVEL_EX 373 +#define EFFECT_RITUAL_LEVEL_EX 374 +#define EFFECT_DOUBLE_XMATERIAL 375 +#define EFFECT_REVEAL_ONFIELD 376 + +//#define EVENT_STARTUP 1000 +#define EVENT_FLIP 1001 +#define EVENT_FREE_CHAIN 1002 +#define EVENT_DESTROY 1010 +#define EVENT_REMOVE 1011 +#define EVENT_TO_HAND 1012 +#define EVENT_TO_DECK 1013 +#define EVENT_TO_GRAVE 1014 +#define EVENT_LEAVE_FIELD 1015 +#define EVENT_CHANGE_POS 1016 +#define EVENT_RELEASE 1017 +#define EVENT_DISCARD 1018 +#define EVENT_LEAVE_FIELD_P 1019 +#define EVENT_CHAIN_SOLVING 1020 +#define EVENT_CHAIN_ACTIVATING 1021 +#define EVENT_CHAIN_SOLVED 1022 +//#define EVENT_CHAIN_ACTIVATED 1023 +#define EVENT_CHAIN_NEGATED 1024 +#define EVENT_CHAIN_DISABLED 1025 +#define EVENT_CHAIN_END 1026 +#define EVENT_CHAINING 1027 +#define EVENT_BECOME_TARGET 1028 +#define EVENT_DESTROYED 1029 +#define EVENT_MOVE 1030 +#define EVENT_LEAVE_GRAVE 1031 +#define EVENT_LEAVE_DECK 1032 +#define EVENT_ADJUST 1040 +#define EVENT_BREAK_EFFECT 1050 +#define EVENT_SUMMON_SUCCESS 1100 +#define EVENT_FLIP_SUMMON_SUCCESS 1101 +#define EVENT_SPSUMMON_SUCCESS 1102 +#define EVENT_SUMMON 1103 +#define EVENT_FLIP_SUMMON 1104 +#define EVENT_SPSUMMON 1105 +#define EVENT_MSET 1106 +#define EVENT_SSET 1107 +#define EVENT_BE_MATERIAL 1108 +#define EVENT_BE_PRE_MATERIAL 1109 +#define EVENT_DRAW 1110 +#define EVENT_DAMAGE 1111 +#define EVENT_RECOVER 1112 +#define EVENT_PREDRAW 1113 +#define EVENT_SUMMON_NEGATED 1114 +#define EVENT_FLIP_SUMMON_NEGATED 1115 +#define EVENT_SPSUMMON_NEGATED 1116 +#define EVENT_SPSUMMON_SUCCESS_G_P 1117 +#define EVENT_CONTROL_CHANGED 1120 +#define EVENT_EQUIP 1121 +#define EVENT_ATTACK_ANNOUNCE 1130 +#define EVENT_BE_BATTLE_TARGET 1131 +#define EVENT_BATTLE_START 1132 +#define EVENT_BATTLE_CONFIRM 1133 +#define EVENT_PRE_DAMAGE_CALCULATE 1134 +//#define EVENT_DAMAGE_CALCULATING 1135 +#define EVENT_PRE_BATTLE_DAMAGE 1136 +//#define EVENT_BATTLE_END 1137 +#define EVENT_BATTLED 1138 +#define EVENT_BATTLE_DESTROYING 1139 +#define EVENT_BATTLE_DESTROYED 1140 +#define EVENT_DAMAGE_STEP_END 1141 +#define EVENT_ATTACK_DISABLED 1142 +#define EVENT_BATTLE_DAMAGE 1143 +#define EVENT_TOSS_DICE 1150 +#define EVENT_TOSS_COIN 1151 +#define EVENT_TOSS_COIN_NEGATE 1152 +#define EVENT_TOSS_DICE_NEGATE 1153 +#define EVENT_LEVEL_UP 1200 +#define EVENT_PAY_LPCOST 1201 +#define EVENT_DETACH_MATERIAL 1202 +#define EVENT_TURN_END 1210 +#define EVENT_PHASE 0x1000 +#define EVENT_PHASE_START 0x2000 +#define EVENT_ADD_COUNTER 0x10000 +#define EVENT_REMOVE_COUNTER 0x20000 +#define EVENT_CUSTOM 0x10000000 + +constexpr int32_t DOUBLE_DAMAGE = INT32_MIN; +constexpr int32_t HALF_DAMAGE = INT32_MIN + 1; + +constexpr uint32_t MAX_PARAMETER = 0xffffU; +constexpr uint32_t MAX_XYZ_LEVEL = 0x0fffU; + +// flag effect +#define EFFECT_FLAG_EFFECT 0x20000000U +#define MAX_CARD_ID 0x0fffffffU + +// The type of effect code +enum code_type : int32_t { + CODE_CUSTOM = 1, // header + id (28 bits) + CODE_COUNTER, // header + counter_id (16 bits) + CODE_PHASE, // header + phase_id (12 bits) + CODE_VALUE, // numeric value, max = 4095 +}; + +const std::unordered_set continuous_event{ + EVENT_ADJUST, + EVENT_BREAK_EFFECT, + EVENT_TURN_END, + EVENT_PRE_BATTLE_DAMAGE, + EVENT_SPSUMMON_SUCCESS_G_P, +}; + +const std::unordered_set affect_summoning_effect{ + EFFECT_CANNOT_DISABLE_SUMMON, + EFFECT_CANNOT_DISABLE_SPSUMMON, + EVENT_BE_PRE_MATERIAL, +}; + +#endif /* EFFECT_H_ */ diff --git a/core/effectset.h b/core/effectset.h new file mode 100644 index 0000000..7ecd5b0 --- /dev/null +++ b/core/effectset.h @@ -0,0 +1,21 @@ +/* + * effectset.h + * + * Created on: 2011-10-8 + * Author: Argon + */ + +#ifndef EFFECTSET_H_ +#define EFFECTSET_H_ + +#include +#include + +class effect; + +bool effect_sort_id(const effect* e1, const effect* e2); + +using effect_set = std::vector; +using effect_set_v = effect_set; + +#endif //EFFECTSET_H_ diff --git a/core/emscripten_shutdown.cpp b/core/emscripten_shutdown.cpp new file mode 100644 index 0000000..4bece84 --- /dev/null +++ b/core/emscripten_shutdown.cpp @@ -0,0 +1,7 @@ +#ifdef __EMSCRIPTEN__ +#include + +extern "C" EMSCRIPTEN_KEEPALIVE void ocgcore_shutdown(int code) { + emscripten_force_exit(code); +} +#endif diff --git a/core/field.cpp b/core/field.cpp new file mode 100644 index 0000000..f181c0d --- /dev/null +++ b/core/field.cpp @@ -0,0 +1,3711 @@ +/* + * field.cpp + * + * Created on: 2010-7-21 + * Author: Argon + */ + +#include "field.h" +#include "duel.h" +#include "card.h" +#include "group.h" +#include "effect.h" +#include "interpreter.h" +#include +#include + +int32_t field::field_used_count[32] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5}; + +bool chain::chain_operation_sort(const chain& c1, const chain& c2) { + auto e1 = c1.triggering_effect; + auto e2 = c2.triggering_effect; + if (e1 && e2) { + if (e1->handler == e2->handler && e1->description != e2->description) + return e1->description < e2->description; + if (e1->id != e2->id) + return e1->id < e2->id; + } + return c1.chain_id < c2.chain_id; +} +void chain::set_triggering_state(card* pcard) { + triggering_controler = pcard->current.controler; + if(pcard->current.is_location(LOCATION_FZONE)) + triggering_location = LOCATION_SZONE | LOCATION_FZONE; + else if(pcard->current.is_location(LOCATION_PZONE)) + triggering_location = LOCATION_SZONE | LOCATION_PZONE; + else + triggering_location = pcard->current.location; + triggering_sequence = pcard->current.sequence; + triggering_position = pcard->current.position; + triggering_state.code = pcard->get_code(); + triggering_state.code2 = pcard->get_another_code(); + triggering_state.level = pcard->get_level(); + triggering_state.rank = pcard->get_rank(); + triggering_state.attribute = pcard->get_attribute(); + triggering_state.race = pcard->get_race(); + std::pair atk_def = pcard->get_atk_def(); + triggering_state.attack = atk_def.first; + triggering_state.defense = atk_def.second; +} +bool tevent::operator< (const tevent& v) const { + if(trigger_card != v.trigger_card) return std::less{}(trigger_card, v.trigger_card); + if(event_cards != v.event_cards) return std::less{}(event_cards, v.event_cards); + if(reason_effect != v.reason_effect) return std::less{}(reason_effect, v.reason_effect); + if(event_code != v.event_code) return event_code < v.event_code; + if(event_value != v.event_value) return event_value < v.event_value; + if(reason != v.reason) return reason < v.reason; + if(event_player != v.event_player) return event_player < v.event_player; + return reason_player < v.reason_player; +} +bool delayed_effect_sort::operator()(const std::pair& lhs, const std::pair& rhs) const { + if(lhs.first != rhs.first) + return std::less{}(lhs.first, rhs.first); + return lhs.second < rhs.second; +} +field::field(duel* pd) + : pduel(pd) { + for (int32_t i = 0; i < 2; ++i) { + player[i].list_mzone.resize(7, 0); + player[i].list_szone.resize(8, 0); + player[i].list_main.reserve(60); + player[i].list_hand.reserve(60); + player[i].list_grave.reserve(75); + player[i].list_remove.reserve(75); + player[i].list_extra.reserve(30); + } +} +void field::reload_field_info() { + pduel->write_buffer8(MSG_RELOAD_FIELD); + pduel->write_buffer8(core.duel_rule); + for(int32_t playerid = 0; playerid < 2; ++playerid) { + pduel->write_buffer32(player[playerid].lp); + for(auto& pcard : player[playerid].list_mzone) { + if(pcard) { + pduel->write_buffer8(1); + pduel->write_buffer8(pcard->current.position); + pduel->write_buffer8((uint8_t)pcard->xyz_materials.size()); + } else { + pduel->write_buffer8(0); + } + } + for(auto& pcard : player[playerid].list_szone) { + if(pcard) { + pduel->write_buffer8(1); + pduel->write_buffer8(pcard->current.position); + } else { + pduel->write_buffer8(0); + } + } + pduel->write_buffer8((uint8_t)player[playerid].list_main.size()); + pduel->write_buffer8((uint8_t)player[playerid].list_hand.size()); + pduel->write_buffer8((uint8_t)player[playerid].list_grave.size()); + pduel->write_buffer8((uint8_t)player[playerid].list_remove.size()); + pduel->write_buffer8((uint8_t)player[playerid].list_extra.size()); + pduel->write_buffer8((uint8_t)player[playerid].extra_p_count); + } + pduel->write_buffer8((uint8_t)core.current_chain.size()); + for(const auto& ch : core.current_chain) { + effect* peffect = ch.triggering_effect; + pduel->write_buffer32(peffect->get_handler()->data.code); + pduel->write_buffer32(peffect->get_handler()->get_info_location()); + pduel->write_buffer8(ch.triggering_controler); + pduel->write_buffer8((uint8_t)ch.triggering_location); + pduel->write_buffer8(ch.triggering_sequence); + pduel->write_buffer32(peffect->description); + } +} +void field::add_card(uint8_t playerid, card* pcard, uint8_t location, uint8_t sequence, uint8_t pzone) { + if (pcard->current.location != 0) + return; + if (!is_location_useable(playerid, location, sequence)) + return; + if(pcard->is_extra_deck_monster() && (location & (LOCATION_HAND | LOCATION_DECK))) { + location = LOCATION_EXTRA; + pcard->sendto_param.position = POS_FACEDOWN_DEFENSE; + } + pcard->current.controler = playerid; + pcard->current.location = location; + switch (location) { + case LOCATION_MZONE: { + player[playerid].list_mzone[sequence] = pcard; + pcard->current.sequence = sequence; + break; + } + case LOCATION_SZONE: { + player[playerid].list_szone[sequence] = pcard; + pcard->current.sequence = sequence; + break; + } + case LOCATION_DECK: { + if (sequence == SEQ_DECKTOP) { + player[playerid].list_main.push_back(pcard); + pcard->current.sequence = (uint8_t)player[playerid].list_main.size() - 1; + } else if (sequence == SEQ_DECKBOTTOM) { + player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard); + reset_sequence(playerid, LOCATION_DECK); + } else { // SEQ_DECKSHUFFLE + if(core.duel_options & DUEL_RETURN_DECK_TOP) { + player[playerid].list_main.push_back(pcard); + pcard->current.sequence = (uint8_t)player[playerid].list_main.size() - 1; + } + else { + player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard); + reset_sequence(playerid, LOCATION_DECK); + } + if(!core.shuffle_check_disabled) + core.shuffle_deck_check[playerid] = TRUE; + } + pcard->sendto_param.position = POS_FACEDOWN; + break; + } + case LOCATION_HAND: { + player[playerid].list_hand.push_back(pcard); + pcard->current.sequence = (uint8_t)player[playerid].list_hand.size() - 1; + uint32_t pos = pcard->is_affected_by_effect(EFFECT_PUBLIC) ? POS_FACEUP : POS_FACEDOWN; + pcard->sendto_param.position = pos; + if(!(pcard->current.reason & REASON_DRAW) && !core.shuffle_check_disabled) + core.shuffle_hand_check[playerid] = TRUE; + break; + } + case LOCATION_GRAVE: { + player[playerid].list_grave.push_back(pcard); + pcard->current.sequence = (uint8_t)player[playerid].list_grave.size() - 1; + break; + } + case LOCATION_REMOVED: { + player[playerid].list_remove.push_back(pcard); + pcard->current.sequence = (uint8_t)player[playerid].list_remove.size() - 1; + break; + } + case LOCATION_EXTRA: { + if(player[playerid].extra_p_count == 0 || (pcard->data.type & TYPE_PENDULUM) && (pcard->sendto_param.position & POS_FACEUP)) + player[playerid].list_extra.push_back(pcard); + else + player[playerid].list_extra.insert(player[playerid].list_extra.end() - player[playerid].extra_p_count, pcard); + if((pcard->data.type & TYPE_PENDULUM) && (pcard->sendto_param.position & POS_FACEUP)) + ++player[playerid].extra_p_count; + reset_sequence(playerid, LOCATION_EXTRA); + break; + } + } + if(pzone) + pcard->current.pzone = true; + else + pcard->current.pzone = false; + pcard->apply_field_effect(); + pcard->fieldid = infos.field_id++; + pcard->fieldid_r = pcard->fieldid; + pcard->activate_count_id = pcard->fieldid; + if(check_unique_onfield(pcard, pcard->current.controler, pcard->current.location)) + pcard->unique_fieldid = UINT_MAX; + pcard->turnid = infos.turn_id; + refresh_player_info(playerid); +} +void field::remove_card(card* pcard) { + if (!check_playerid(pcard->current.controler) || pcard->current.location == 0) + return; + uint8_t playerid = pcard->current.controler; + switch (pcard->current.location) { + case LOCATION_MZONE: + player[playerid].list_mzone[pcard->current.sequence] = 0; + break; + case LOCATION_SZONE: + player[playerid].list_szone[pcard->current.sequence] = 0; + break; + case LOCATION_DECK: + player[playerid].list_main.erase(player[playerid].list_main.begin() + pcard->current.sequence); + reset_sequence(playerid, LOCATION_DECK); + if(!core.shuffle_check_disabled) + core.shuffle_deck_check[playerid] = TRUE; + break; + case LOCATION_HAND: + pcard->set_status(STATUS_TO_HAND_WITHOUT_CONFIRM, FALSE); + player[playerid].list_hand.erase(player[playerid].list_hand.begin() + pcard->current.sequence); + reset_sequence(playerid, LOCATION_HAND); + break; + case LOCATION_GRAVE: + player[playerid].list_grave.erase(player[playerid].list_grave.begin() + pcard->current.sequence); + reset_sequence(playerid, LOCATION_GRAVE); + break; + case LOCATION_REMOVED: + player[playerid].list_remove.erase(player[playerid].list_remove.begin() + pcard->current.sequence); + reset_sequence(playerid, LOCATION_REMOVED); + break; + case LOCATION_EXTRA: + player[playerid].list_extra.erase(player[playerid].list_extra.begin() + pcard->current.sequence); + reset_sequence(playerid, LOCATION_EXTRA); + if((pcard->data.type & TYPE_PENDULUM) && (pcard->current.position & POS_FACEUP)) + --player[playerid].extra_p_count; + break; + } + pcard->cancel_field_effect(); + refresh_player_info(playerid); + pcard->previous.controler = pcard->current.controler; + pcard->previous.location = pcard->current.location; + pcard->previous.sequence = pcard->current.sequence; + pcard->previous.position = pcard->current.position; + pcard->previous.pzone = pcard->current.pzone; + pcard->current.controler = PLAYER_NONE; + pcard->current.location = 0; + pcard->current.sequence = 0; +} +void field::move_card(uint8_t playerid, card* pcard, uint8_t location, uint8_t sequence, uint8_t pzone) { + if (!is_location_useable(playerid, location, sequence)) + return; + uint8_t preplayer = pcard->current.controler; + uint8_t presequence = pcard->current.sequence; + if(pcard->is_extra_deck_monster() && (location & (LOCATION_HAND | LOCATION_DECK))) { + location = LOCATION_EXTRA; + pcard->sendto_param.position = POS_FACEDOWN_DEFENSE; + } + if (pcard->current.location) { + if (pcard->current.location == location && pcard->current.pzone == !!pzone) { + if (pcard->current.location == LOCATION_DECK) { + if(preplayer == playerid) { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + player[preplayer].list_main.erase(player[preplayer].list_main.begin() + pcard->current.sequence); + if (sequence == SEQ_DECKTOP) { + player[playerid].list_main.push_back(pcard); + } else if (sequence == SEQ_DECKBOTTOM) { + player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard); + } else { // SEQ_DECKSHUFFLE + if(core.duel_options & DUEL_RETURN_DECK_TOP) + player[playerid].list_main.push_back(pcard); + else + player[playerid].list_main.insert(player[playerid].list_main.begin(), pcard); + if(!core.shuffle_check_disabled) + core.shuffle_deck_check[playerid] = TRUE; + } + reset_sequence(playerid, LOCATION_DECK); + pcard->previous.controler = preplayer; + pcard->current.controler = playerid; + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->current.reason); + return; + } else + remove_card(pcard); + } else if(location & LOCATION_ONFIELD) { + if (playerid == preplayer && sequence == presequence) + return; + if(location == LOCATION_MZONE) { + if(sequence >= (int32_t)player[playerid].list_mzone.size() || player[playerid].list_mzone[sequence]) + return; + } else { + if(sequence >= player[playerid].szone_size || player[playerid].list_szone[sequence]) + return; + } + if(preplayer == playerid) { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + } + pcard->previous.controler = pcard->current.controler; + pcard->previous.location = pcard->current.location; + pcard->previous.sequence = pcard->current.sequence; + pcard->previous.position = pcard->current.position; + pcard->previous.pzone = pcard->current.pzone; + if (location == LOCATION_MZONE) { + player[preplayer].list_mzone[presequence] = 0; + player[playerid].list_mzone[sequence] = pcard; + pcard->current.controler = playerid; + pcard->current.sequence = sequence; + } else { + player[preplayer].list_szone[presequence] = 0; + player[playerid].list_szone[sequence] = pcard; + pcard->current.controler = playerid; + pcard->current.sequence = sequence; + } + if(preplayer == playerid) { + refresh_player_info(playerid); + pduel->write_buffer32(pcard->get_public_info_location()); + pduel->write_buffer32(pcard->current.reason); + } else { + refresh_player_info(preplayer); + refresh_player_info(playerid); + pcard->fieldid = infos.field_id++; + if(check_unique_onfield(pcard, pcard->current.controler, pcard->current.location)) + pcard->unique_fieldid = UINT_MAX; + } + return; + } else if(location == LOCATION_HAND) { + if(preplayer == playerid) + return; + remove_card(pcard); + } else { + if(location == LOCATION_GRAVE) { + if(pcard->current.sequence == player[pcard->current.controler].list_grave.size() - 1) + return; + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + player[pcard->current.controler].list_grave.erase(player[pcard->current.controler].list_grave.begin() + pcard->current.sequence); + player[pcard->current.controler].list_grave.push_back(pcard); + reset_sequence(pcard->current.controler, LOCATION_GRAVE); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->current.reason); + } else if(location == LOCATION_REMOVED) { + if(pcard->current.sequence == player[pcard->current.controler].list_remove.size() - 1) + return; + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + player[pcard->current.controler].list_remove.erase(player[pcard->current.controler].list_remove.begin() + pcard->current.sequence); + player[pcard->current.controler].list_remove.push_back(pcard); + reset_sequence(pcard->current.controler, LOCATION_REMOVED); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->current.reason); + } else { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + player[pcard->current.controler].list_extra.erase(player[pcard->current.controler].list_extra.begin() + pcard->current.sequence); + player[pcard->current.controler].list_extra.push_back(pcard); + reset_sequence(pcard->current.controler, LOCATION_EXTRA); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->current.reason); + } + return; + } + } else { + if((pcard->data.type & TYPE_PENDULUM) && (location == LOCATION_GRAVE) + && pcard->is_capable_send_to_extra(playerid) + && (((pcard->current.location == LOCATION_MZONE) && !pcard->is_status(STATUS_SUMMON_DISABLED)) + || ((pcard->current.location == LOCATION_SZONE) && !pcard->is_status(STATUS_ACTIVATE_DISABLED)))) { + location = LOCATION_EXTRA; + pcard->sendto_param.position = POS_FACEUP_DEFENSE; + } + remove_card(pcard); + } + } + add_card(playerid, pcard, location, sequence, pzone); +} +void field::swap_card(card* pcard1, card* pcard2, uint8_t new_sequence1, uint8_t new_sequence2) { + uint8_t p1 = pcard1->current.controler, p2 = pcard2->current.controler; + uint8_t l1 = pcard1->current.location, l2 = pcard2->current.location; + uint8_t s1 = pcard1->current.sequence, s2 = pcard2->current.sequence; + uint32_t info1 = pcard1->get_info_location(), info2 = pcard2->get_info_location(); + if(!(l1 & LOCATION_ONFIELD) || !(l2 & LOCATION_ONFIELD)) + return; + if(new_sequence1 != s1 && !is_location_useable(p1, l1, new_sequence1) + || new_sequence2 != s2 && !is_location_useable(p2, l2, new_sequence2)) + return; + if(p1 == p2 && l1 == l2 && (new_sequence1 == s2 || new_sequence2 == s1)) + return; + if(l1 == l2) { + pcard1->previous.controler = p1; + pcard1->previous.location = l1; + pcard1->previous.sequence = s1; + pcard1->previous.position = pcard1->current.position; + pcard1->previous.pzone = pcard1->current.pzone; + pcard1->current.controler = p2; + pcard1->current.location = l2; + pcard1->current.sequence = new_sequence2; + pcard2->previous.controler = p2; + pcard2->previous.location = l2; + pcard2->previous.sequence = s2; + pcard2->previous.position = pcard2->current.position; + pcard2->previous.pzone = pcard2->current.pzone; + pcard2->current.controler = p1; + pcard2->current.location = l1; + pcard2->current.sequence = new_sequence1; + if(p1 != p2) { + pcard1->fieldid = infos.field_id++; + pcard2->fieldid = infos.field_id++; + if(check_unique_onfield(pcard1, pcard1->current.controler, pcard1->current.location)) + pcard1->unique_fieldid = UINT_MAX; + if(check_unique_onfield(pcard2, pcard2->current.controler, pcard2->current.location)) + pcard2->unique_fieldid = UINT_MAX; + } + if(l1 == LOCATION_MZONE) { + player[p1].list_mzone[s1] = 0; + player[p2].list_mzone[s2] = 0; + player[p2].list_mzone[new_sequence2] = pcard1; + player[p1].list_mzone[new_sequence1] = pcard2; + refresh_player_info(p1); + refresh_player_info(p2); + } else if(l1 == LOCATION_SZONE) { + player[p1].list_szone[s1] = 0; + player[p2].list_szone[s2] = 0; + player[p2].list_szone[new_sequence2] = pcard1; + player[p1].list_szone[new_sequence1] = pcard2; + refresh_player_info(p1); + refresh_player_info(p2); + } + } else { + remove_card(pcard1); + remove_card(pcard2); + add_card(p2, pcard1, l2, new_sequence2); + add_card(p1, pcard2, l1, new_sequence1); + } + if(s1 == new_sequence1 && s2 == new_sequence2) { + pduel->write_buffer8(MSG_SWAP); + pduel->write_buffer32(pcard1->data.code); + pduel->write_buffer32(info1); + pduel->write_buffer32(pcard2->data.code); + pduel->write_buffer32(info2); + } else if(s1 == new_sequence1) { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard1->data.code); + pduel->write_buffer32(info1); + pduel->write_buffer32(pcard1->get_public_info_location()); + pduel->write_buffer32(0); + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard2->data.code); + pduel->write_buffer32(info2); + pduel->write_buffer32(pcard2->get_public_info_location()); + pduel->write_buffer32(0); + } else { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard2->data.code); + pduel->write_buffer32(info2); + pduel->write_buffer32(pcard2->get_public_info_location()); + pduel->write_buffer32(0); + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard1->data.code); + pduel->write_buffer32(info1); + pduel->write_buffer32(pcard1->get_public_info_location()); + pduel->write_buffer32(0); + } +} +void field::swap_card(card* pcard1, card* pcard2) { + return swap_card(pcard1, pcard2, pcard1->current.sequence, pcard2->current.sequence); +} +void field::set_control(card* pcard, uint8_t playerid, uint16_t reset_phase, uint8_t reset_count) { + if((core.remove_brainwashing && pcard->is_affected_by_effect(EFFECT_REMOVE_BRAINWASHING)) || +#ifdef _IRR_ANDROID_PLATFORM_ + std::get<0>(pcard->refresh_control_status()) +#else + std::get(pcard->refresh_control_status()) +#endif + == playerid) + return; + effect* peffect = pduel->new_effect(); + if(core.reason_effect) + peffect->owner = core.reason_effect->get_handler(); + else + peffect->owner = pcard; + peffect->handler = pcard; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_SET_CONTROL; + peffect->value = playerid; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = RESET_EVENT | 0xc6c0000; + if(reset_count) { + peffect->reset_flag |= RESET_PHASE | reset_phase; + if(!(peffect->reset_flag & (RESET_SELF_TURN | RESET_OPPO_TURN))) + peffect->reset_flag |= (RESET_SELF_TURN | RESET_OPPO_TURN); + peffect->reset_count = reset_count; + } + pcard->add_effect(peffect); + pcard->current.controler = playerid; +} + +int32_t field::get_pzone_sequence(uint8_t pseq) const { + if (core.duel_rule >= NEW_MASTER_RULE) { + if(!pseq) + return 0; + else + return 4; + } + else { + if (!pseq) + return 6; + else + return 7; + } +} +const card_vector* field::get_field_vector(uint8_t playerid, uint8_t location) const { + if (!check_playerid(playerid)) + return nullptr; + switch (location) { + case LOCATION_MZONE: + return &player[playerid].list_mzone; + case LOCATION_SZONE: + return &player[playerid].list_szone; + case LOCATION_DECK: + return &player[playerid].list_main; + case LOCATION_HAND: + return &player[playerid].list_hand; + case LOCATION_GRAVE: + return &player[playerid].list_grave; + case LOCATION_REMOVED: + return &player[playerid].list_remove; + case LOCATION_EXTRA: + return &player[playerid].list_extra; + default: + return nullptr; + } +} +card* field::get_field_card(uint8_t playerid, uint32_t general_location, uint8_t sequence) const { + if (!check_playerid(playerid)) + return nullptr; + switch(general_location) { + case LOCATION_MZONE: + case LOCATION_DECK: + case LOCATION_HAND: + case LOCATION_GRAVE: + case LOCATION_REMOVED: + case LOCATION_EXTRA: { + auto ptr = get_field_vector(playerid, general_location); + if (!ptr) + return nullptr; + auto& container = *ptr; + if (sequence < container.size()) + return container[sequence]; + else + return nullptr; + break; + } + case LOCATION_SZONE: { + if(sequence < player[playerid].szone_size) + return player[playerid].list_szone[sequence]; + else + return nullptr; + break; + } + case LOCATION_FZONE: { + if(sequence == 0) + return player[playerid].list_szone[5]; + else + return nullptr; + break; + } + case LOCATION_PZONE: { + if(sequence == 0 || sequence == 1) { + card* pcard = player[playerid].list_szone[get_pzone_sequence(sequence)]; + return (pcard && pcard->current.pzone) ? pcard : nullptr; + } else + return nullptr; + break; + } + } + return nullptr; +} +int32_t field::is_location_useable(uint8_t playerid, uint32_t general_location, uint8_t sequence) const { + if (!check_playerid(playerid)) + return FALSE; + uint32_t flag = player[playerid].disabled_location | player[playerid].used_location; + if (general_location == LOCATION_MZONE) { + if (sequence >= (int32_t)player[playerid].list_mzone.size()) + return FALSE; + if(flag & (0x1U << sequence)) + return FALSE; + if(sequence >= 5) { + uint32_t oppo = player[1 - playerid].disabled_location | player[1 - playerid].used_location; + if(oppo & (0x1U << (11 - sequence))) + return FALSE; + } + } else if (general_location == LOCATION_SZONE) { + if (sequence >= player[playerid].szone_size) + return FALSE; + if(flag & (0x100U << sequence)) + return FALSE; + } else if (general_location == LOCATION_FZONE) { + if (sequence >= 1) + return FALSE; + if(flag & (0x100U << (5 + sequence))) + return FALSE; + } else if (general_location == LOCATION_PZONE) { + if (sequence >= 2) + return FALSE; + if (flag & (0x100U << get_pzone_sequence(sequence))) + return FALSE; + } + return TRUE; +} +/** +* Return usable count in `zone` when `uplayer` moves `pcard` to the MZONE or SZONE(0~4) of `playerid` (can be negative). +* For LOCATION_MZONE, "usable" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE, satisfying EFFECT_MAX_MZONE. +* For LOCATION_SZONE, "usable" means not used, not disabled, satisfying EFFECT_MAX_SZONE. +* +* @param pcard the card about to move +* @param playerid target player +* @param location LOCATION_MZONE or LOCATION_SZONE +* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer, ignoring EFFECT_MAX_MZONE, EFFECT_MAX_SZONE of playerid +* @param reason location reason +* @param zone specified zones, default: 0xff +* @param list storing unavailable or unspecified zones +* +* @return usable count in the MZONE or SZONE(0~4) of `playerid` (can be negative) +*/ +int32_t field::get_useable_count(card* pcard, uint8_t playerid, uint8_t location, uint8_t uplayer, uint32_t reason, uint32_t zone, uint32_t* list) { + if(location == LOCATION_MZONE && pcard && pcard->current.location == LOCATION_EXTRA) + return get_useable_count_fromex(pcard, playerid, uplayer, zone, list); + else + return get_useable_count_other(pcard, playerid, location, uplayer, reason, zone, list); +} +/** +* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck) +* @return usable count in the MZONE of `playerid` +*/ +int32_t field::get_useable_count_fromex(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone, uint32_t* list) { + bool use_temp_card = false; + if(!pcard) { + use_temp_card = true; + pcard = temp_card; + pcard->current.location = LOCATION_EXTRA; + } + int32_t useable_count = 0; + if(core.duel_rule >= NEW_MASTER_RULE) + useable_count = get_useable_count_fromex_rule4(pcard, playerid, uplayer, zone, list); + else + useable_count = get_useable_count_other(pcard, playerid, LOCATION_MZONE, uplayer, LOCATION_REASON_TOFIELD, zone, list); + if(use_temp_card) + pcard->current.location = 0; + return useable_count; +} +/** +* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid` +* For LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE. +*/ +int32_t field::get_spsummonable_count(card* pcard, uint8_t playerid, uint32_t zone, uint32_t* list) { + if(pcard->current.location == LOCATION_EXTRA) + return get_spsummonable_count_fromex(pcard, playerid, playerid, zone, list); + else + return get_tofield_count(pcard, playerid, LOCATION_MZONE, playerid, LOCATION_REASON_TOFIELD, zone, list); +} +/** +* @param pcard the card about to move from Extra Deck (nullptr means any card in Extra Deck) +* @return the number of available slots in `zone` when `pcard` is sp_summoned to the MZONE of `playerid` by `uplayer` +*/ +int32_t field::get_spsummonable_count_fromex(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone, uint32_t* list) { + bool use_temp_card = false; + if(!pcard) { + use_temp_card = true; + pcard = temp_card; + pcard->current.location = LOCATION_EXTRA; + } + int32_t spsummonable_count = 0; + if(core.duel_rule >= NEW_MASTER_RULE && !is_player_affected_by_effect(playerid, EFFECT_EXTRA_TOMAIN_KOISHI) && !pcard->is_affected_by_effect(EFFECT_EXTRA_TOMAIN_KOISHI)) + spsummonable_count = get_spsummonable_count_fromex_rule4(pcard, playerid, uplayer, zone, list); + else + { + spsummonable_count = get_tofield_count(pcard, playerid, LOCATION_MZONE, uplayer, LOCATION_REASON_TOFIELD, zone, list); + if(core.duel_rule >= 4) { + uint32_t temp_list = 0; + get_spsummonable_count_fromex_rule4(pcard, playerid, uplayer, zone, &temp_list); + if(~temp_list & ((1u << 5) | (1u << 6))) + spsummonable_count++; + if(list) + *list &= temp_list; + } + } + if(use_temp_card) + pcard->current.location = 0; + return spsummonable_count; +} +/** +* @return usable count in `zone` of Main MZONE or SZONE(0~4) +*/ +int32_t field::get_useable_count_other(card* pcard, uint8_t playerid, uint8_t location, uint8_t uplayer, uint32_t reason, uint32_t zone, uint32_t* list) { + int32_t count = get_tofield_count(pcard, playerid, location, uplayer, reason, zone, list); + int32_t limit; + if(location == LOCATION_MZONE) + limit = get_mzone_limit(playerid, uplayer, reason); + else + limit = get_szone_limit(playerid, uplayer, reason); + if(count > limit) + count = limit; + return count; +} +/** +* @return the number of available slots in `zone` of Main MZONE or SZONE(0~4) +* for LOCATION_MZONE, "available" means not used, not disabled, satisfying EFFECT_MUST_USE_MZONE +* for LOCATION_SZONE, "available" means not used, not disabled +*/ +int32_t field::get_tofield_count(card* pcard, uint8_t playerid, uint8_t location, uint32_t uplayer, uint32_t reason, uint32_t zone, uint32_t* list) { + if (location != LOCATION_MZONE && location != LOCATION_SZONE) + return 0; + uint32_t flag = player[playerid].disabled_location | player[playerid].used_location; + if(location == LOCATION_MZONE && (reason & LOCATION_REASON_TOFIELD)) { + filter_must_use_mzone(playerid, uplayer, reason, pcard, &flag); + } + if (location == LOCATION_MZONE) + flag = (flag | ~zone) & 0x1f; + else + flag = ((flag >> 8) | ~zone) & 0x1f; + int32_t count = 5 - field_used_count[flag]; + if(location == LOCATION_MZONE) + flag |= (1u << 5) | (1u << 6); + if(list) + *list = flag; + return count; +} +int32_t field::get_useable_count_fromex_rule4(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone, uint32_t* list) { + int32_t count = get_spsummonable_count_fromex_rule4(pcard, playerid, uplayer, zone, list); + int32_t limit = get_mzone_limit(playerid, uplayer, LOCATION_REASON_TOFIELD); + if(count > limit) + count = limit; + return count; +} +int32_t field::get_spsummonable_count_fromex_rule4(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone, uint32_t* list) { + uint32_t flag = player[playerid].disabled_location | player[playerid].used_location; + filter_must_use_mzone(playerid, uplayer, LOCATION_REASON_TOFIELD, pcard, &flag); + if(player[playerid].list_mzone[5] && is_location_useable(playerid, LOCATION_MZONE, 6) + && check_extra_link(playerid, pcard, 6)) { + flag |= 1u << 5; + } else if(player[playerid].list_mzone[6] && is_location_useable(playerid, LOCATION_MZONE, 5) + && check_extra_link(playerid, pcard, 5)) { + flag |= 1u << 6; + } else if(player[playerid].list_mzone[5] || player[playerid].list_mzone[6]) { + flag |= (1u << 5) | (1u << 6); + } else { + if(!is_location_useable(playerid, LOCATION_MZONE, 5)) + flag |= 1u << 5; + if(!is_location_useable(playerid, LOCATION_MZONE, 6)) + flag |= 1u << 6; + } + uint32_t rule_zone = get_rule_zone_fromex(playerid, pcard); + flag = flag | ~zone | ~rule_zone; + if(list) + *list = flag & 0x7f; + int32_t count = 5 - field_used_count[flag & 0x1f]; + if(~flag & ((1u << 5) | (1u << 6))) + ++count; + return count; +} +/** +* @param playerid target player +* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_MZONE +* @param reason location reason +* +* @return the remaining count in the MZONE of `playerid` after applying EFFECT_MAX_MZONE (can be negative). +*/ +int32_t field::get_mzone_limit(uint8_t playerid, uint8_t uplayer, uint32_t reason) { + uint32_t used_flag = player[playerid].used_location; + used_flag = used_flag & 0x1f; + int32_t max = 5; + int32_t used_count = field_used_count[used_flag]; + if(core.duel_rule >= NEW_MASTER_RULE) { + max = 7; + if(player[playerid].list_mzone[5]) + ++used_count; + if(player[playerid].list_mzone[6]) + ++used_count; + } + effect_set eset; + if(uplayer < 2) + filter_player_effect(playerid, EFFECT_MAX_MZONE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(uplayer, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + int32_t v = eset[i]->get_value(3); + if(max > v) + max = v; + } + int32_t limit = max - used_count; + return limit; +} +/** +* @param playerid target player +* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MAX_SZONE +* @param reason location reason +* +* @return the remaining count in the SZONE(0~4) of `playerid` after applying EFFECT_MAX_SZONE. +*/ +int32_t field::get_szone_limit(uint8_t playerid, uint8_t uplayer, uint32_t reason) { + uint32_t used_flag = player[playerid].used_location; + used_flag = (used_flag >> 8) & 0x1f; + effect_set eset; + if(uplayer < 2) + filter_player_effect(playerid, EFFECT_MAX_SZONE, &eset); + int32_t max = 5; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(uplayer, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + int32_t v = eset[i]->get_value(3); + if(max > v) + max = v; + } + int32_t limit = max - field_used_count[used_flag]; + return limit; +} +uint32_t field::get_linked_zone(int32_t playerid) { + uint32_t zones = 0; + for(auto& pcard : player[playerid].list_mzone) { + if(pcard) + zones |= pcard->get_linked_zone() & 0xffff; + } + for(auto& pcard : player[1 - playerid].list_mzone) { + if(pcard) + zones |= pcard->get_linked_zone() >> 16; + } + for(uint32_t i = 0; i < 5; ++i) { + if(i > 0 && player[playerid].list_szone[i] && player[playerid].list_szone[i]->is_link_marker(LINK_MARKER_TOP_LEFT)) + zones |= 1u << (i - 1); + if(player[playerid].list_szone[i] && player[playerid].list_szone[i]->is_link_marker(LINK_MARKER_TOP)) + zones |= 1u << i; + if(i < 4 && player[playerid].list_szone[i] && player[playerid].list_szone[i]->is_link_marker(LINK_MARKER_TOP_RIGHT)) + zones |= 1u << (i + 1); + } + return zones; +} +uint32_t field::get_rule_zone_fromex(int32_t playerid, card* pcard) { + if(core.duel_rule >= NEW_MASTER_RULE) { + if(core.duel_rule >= MASTER_RULE_2020 && pcard && (pcard->data.type & (TYPE_FUSION | TYPE_SYNCHRO | TYPE_XYZ)) + && (pcard->is_position(POS_FACEDOWN) || !(pcard->data.type & TYPE_PENDULUM))) + return 0x7f; + if(is_player_affected_by_effect(playerid, EFFECT_EXTRA_TOMAIN_KOISHI) || pcard && pcard->is_affected_by_effect(EFFECT_EXTRA_TOMAIN_KOISHI)) + return 0x7f; + else + return get_linked_zone(playerid) | (1u << 5) | (1u << 6); + } else { + return 0x1f; + } +} +/** +* @param playerid target player +* @param uplayer request player, PLAYER_NONE means ignoring EFFECT_MUST_USE_MZONE of uplayer +* @param reason location reason +* @param pcard the card about to move +* @param flag storing the zones in MZONE blocked by EFFECT_MUST_USE_MZONE +*/ +void field::filter_must_use_mzone(uint8_t playerid, uint8_t uplayer, uint32_t reason, card* pcard, uint32_t* flag) { + effect_set eset; + if(uplayer < 2) + filter_player_effect(uplayer, EFFECT_MUST_USE_MZONE, &eset); + if(pcard) + pcard->filter_effect(EFFECT_MUST_USE_MZONE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) && eset[i]->count_limit == 0) + continue; + uint32_t value = 0x1f; + if(eset[i]->is_flag(EFFECT_FLAG_PLAYER_TARGET)) + value = eset[i]->get_value(); + else { + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(uplayer, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + value = eset[i]->get_value(pcard, 3); + } + if(eset[i]->get_handler_player() == playerid) + *flag |= ~value & 0x7f; + else + *flag |= ~(value >> 16) & 0x7f; + } +} +void field::get_linked_cards(uint8_t self, uint8_t s, uint8_t o, card_set* cset) { + cset->clear(); + uint8_t c = s; + for(int32_t p = 0; p < 2; ++p) { + if(c) { + uint32_t linked_zone = get_linked_zone(self); + get_cards_in_zone(cset, linked_zone, self, LOCATION_MZONE); + } + self = 1 - self; + c = o; + } +} +int32_t field::check_extra_link(int32_t playerid) { + if(!player[playerid].list_mzone[5] || !player[playerid].list_mzone[6]) + return FALSE; + card* pcard = player[playerid].list_mzone[5]; + uint32_t checked = 0x1u << 5; + uint32_t linked_zone = pcard->get_mutual_linked_zone(); + while(true) { + if((linked_zone >> 6) & 0x1U) + return TRUE; + uint32_t checking = linked_zone & ~checked; + if(!checking) + return FALSE; + uint32_t rightmost = checking & (~checking + 1); + checked |= rightmost; + if(rightmost < 0x10000U) { + for(int32_t i = 0; i < 7; ++i) { + if(rightmost & 0x1U) { + pcard = player[playerid].list_mzone[i]; + linked_zone |= pcard->get_mutual_linked_zone(); + break; + } + rightmost >>= 1; + } + } else { + rightmost >>= 16; + for(int32_t i = 0; i < 7; ++i) { + if(rightmost & 0x1U) { + pcard = player[1 - playerid].list_mzone[i]; + uint32_t zone = pcard->get_mutual_linked_zone(); + linked_zone |= (zone << 16) | (zone >> 16); + break; + } + rightmost >>= 1; + } + } + } + return FALSE; +} +int32_t field::check_extra_link(int32_t playerid, card* pcard, int32_t sequence) { + if(!pcard) + return FALSE; + if(player[playerid].list_mzone[sequence]) + return FALSE; + uint8_t cur_controler = pcard->current.controler; + uint8_t cur_location = pcard->current.location; + uint8_t cur_sequence = pcard->current.sequence; + player[playerid].list_mzone[sequence] = pcard; + pcard->current.controler = playerid; + pcard->current.location = LOCATION_MZONE; + pcard->current.sequence = sequence; + int32_t ret = check_extra_link(playerid); + player[playerid].list_mzone[sequence] = 0; + pcard->current.controler = cur_controler; + pcard->current.location = cur_location; + pcard->current.sequence = cur_sequence; + return ret; +} +void field::get_cards_in_zone(card_set* cset, uint32_t zone, int32_t playerid, int32_t location) { + if(!(location & LOCATION_ONFIELD)) + return; + card_vector& svector = (location == LOCATION_MZONE) ? player[playerid].list_mzone : player[playerid].list_szone; + uint32_t icheck = 0x1; + for(auto& pcard : svector) { + if(zone & icheck) { + if(pcard) + cset->insert(pcard); + } + icheck <<= 1; + } +} +void field::shuffle(uint8_t playerid, uint8_t location) { + if(!(location & (LOCATION_HAND | LOCATION_DECK | LOCATION_EXTRA))) + return; + card_vector& svector = (location == LOCATION_HAND) ? player[playerid].list_hand : (location == LOCATION_DECK) ? player[playerid].list_main : player[playerid].list_extra; + if(svector.size() == 0) + return; + if(location == LOCATION_HAND) { + bool shuffle = false; + for(auto& pcard : svector) + if(!pcard->is_position(POS_FACEUP)) + shuffle = true; + if(!shuffle) { + core.shuffle_hand_check[playerid] = FALSE; + return; + } + } + if(location == LOCATION_HAND || !(core.duel_options & DUEL_PSEUDO_SHUFFLE)) { + int32_t s = (int32_t)svector.size(); + if(location == LOCATION_EXTRA) + s = s - (int32_t)player[playerid].extra_p_count; + if(s > 1) { + pduel->random.shuffle_vector(svector, 0, s, pduel->rng_version); + reset_sequence(playerid, location); + } + } + if(location == LOCATION_HAND || location == LOCATION_EXTRA) { + pduel->write_buffer8((location == LOCATION_HAND) ? MSG_SHUFFLE_HAND : MSG_SHUFFLE_EXTRA); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)svector.size()); + for(auto& pcard : svector) + pduel->write_buffer32(pcard->data.code); + if(location == LOCATION_HAND) { + core.shuffle_hand_check[playerid] = FALSE; + for(auto& pcard : svector) { + for(auto& i : pcard->indexer) { + effect* peffect = i.first; + if(peffect->is_flag(EFFECT_FLAG_CLIENT_HINT) && !peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET)) { + pduel->write_buffer8(MSG_CARD_HINT); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer8(CHINT_DESC_ADD); + pduel->write_buffer32(peffect->description); + } + } + } + } + } else { + pduel->write_buffer8(MSG_SHUFFLE_DECK); + pduel->write_buffer8(playerid); + core.shuffle_deck_check[playerid] = FALSE; + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + card* ptop = svector.back(); + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8(0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } +} +void field::reset_sequence(uint8_t playerid, uint8_t location) { + if(location & (LOCATION_ONFIELD)) + return; + int32_t i = 0; + switch(location) { + case LOCATION_DECK: + for(auto& pcard : player[playerid].list_main) + pcard->current.sequence = i++; + break; + case LOCATION_HAND: + for(auto& pcard : player[playerid].list_hand) + pcard->current.sequence = i++; + break; + case LOCATION_EXTRA: + for(auto& pcard : player[playerid].list_extra) + pcard->current.sequence = i++; + break; + case LOCATION_GRAVE: + for(auto& pcard : player[playerid].list_grave) + pcard->current.sequence = i++; + break; + case LOCATION_REMOVED: + for(auto& pcard : player[playerid].list_remove) + pcard->current.sequence = i++; + break; + } +} +void field::swap_deck_and_grave(uint8_t playerid) { + for(auto& pcard : player[playerid].list_grave) { + pcard->previous.location = LOCATION_GRAVE; + pcard->previous.sequence = pcard->current.sequence; + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + } + for(auto& pcard : player[playerid].list_main) { + pcard->previous.location = LOCATION_DECK; + pcard->previous.sequence = pcard->current.sequence; + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + } + player[playerid].list_grave.swap(player[playerid].list_main); + card_vector ex; + for(auto clit = player[playerid].list_main.begin(); clit != player[playerid].list_main.end(); ) { + if((*clit)->is_extra_deck_monster()) { + ex.push_back(*clit); + clit = player[playerid].list_main.erase(clit); + } else + ++clit; + } + for(auto& pcard : player[playerid].list_grave) { + pcard->current.position = POS_FACEUP; + pcard->current.location = LOCATION_GRAVE; + pcard->current.reason = REASON_EFFECT; + pcard->current.reason_effect = core.reason_effect; + pcard->current.reason_player = core.reason_player; + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + pcard->reset(RESET_TOGRAVE, RESET_EVENT); + } + for(auto& pcard : player[playerid].list_main) { + pcard->current.position = POS_FACEDOWN_DEFENSE; + pcard->current.location = LOCATION_DECK; + pcard->current.reason = REASON_EFFECT; + pcard->current.reason_effect = core.reason_effect; + pcard->current.reason_player = core.reason_player; + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + pcard->reset(RESET_TODECK, RESET_EVENT); + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + } + for(auto& pcard : ex) { + pcard->current.position = POS_FACEDOWN_DEFENSE; + pcard->current.location = LOCATION_EXTRA; + pcard->current.reason = REASON_EFFECT; + pcard->current.reason_effect = core.reason_effect; + pcard->current.reason_player = core.reason_player; + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + pcard->reset(RESET_TODECK, RESET_EVENT); + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + } + player[playerid].list_extra.insert(player[playerid].list_extra.end() - player[playerid].extra_p_count, ex.begin(), ex.end()); + reset_sequence(playerid, LOCATION_GRAVE); + reset_sequence(playerid, LOCATION_DECK); + reset_sequence(playerid, LOCATION_EXTRA); + pduel->write_buffer8(MSG_SWAP_GRAVE_DECK); + pduel->write_buffer8(playerid); + shuffle(playerid, LOCATION_DECK); +} +void field::reverse_deck(uint8_t playerid) { + int32_t count = (int32_t)player[playerid].list_main.size(); + if(count == 0) + return; + for(int32_t i = 0; i < count / 2; ++i) { + player[playerid].list_main[i]->current.sequence = count - 1 - i; + player[playerid].list_main[count - 1 - i]->current.sequence = i; + std::swap(player[playerid].list_main[i], player[playerid].list_main[count - 1 - i]); + } +} +void field::refresh_player_info(uint8_t playerid) { + if (!check_playerid(playerid)) + return; + uint32_t used_flag = 0; + for (int32_t i = 0; i < (int32_t)player[playerid].list_mzone.size(); ++i) { + if (player[playerid].list_mzone[i]) + used_flag |= 0x1U << i; + } + for (int32_t i = 0; i < player[playerid].szone_size; ++i) { + if (player[playerid].list_szone[i]) + used_flag |= 0x100U << i; + } + player[playerid].used_location = used_flag; +} +void field::tag_swap(uint8_t playerid) { + //main + for(auto& pcard : player[playerid].list_main) { + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + } + std::swap(player[playerid].list_main, player[playerid].tag_list_main); + for(auto& pcard : player[playerid].list_main) { + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + } + //hand + for(auto& pcard : player[playerid].list_hand) { + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + } + std::swap(player[playerid].list_hand, player[playerid].tag_list_hand); + for(auto& pcard : player[playerid].list_hand) { + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + } + //extra + for(auto& pcard : player[playerid].list_extra) { + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + } + std::swap(player[playerid].list_extra, player[playerid].tag_list_extra); + std::swap(player[playerid].extra_p_count, player[playerid].tag_extra_p_count); + for(auto& pcard : player[playerid].list_extra) { + pcard->apply_field_effect(); + pcard->enable_field_effect(true); + } + pduel->write_buffer8(MSG_TAG_SWAP); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)player[playerid].list_main.size()); + pduel->write_buffer8((uint8_t)player[playerid].list_extra.size()); + pduel->write_buffer8((uint8_t)player[playerid].extra_p_count); + pduel->write_buffer8((uint8_t)player[playerid].list_hand.size()); + if(core.deck_reversed && player[playerid].list_main.size()) + pduel->write_buffer32(player[playerid].list_main.back()->data.code); + else + pduel->write_buffer32(0); + for(auto& pcard : player[playerid].list_hand) + pduel->write_buffer32(pcard->data.code | (pcard->is_position(POS_FACEUP) ? 0x80000000 : 0)); + for(auto& pcard : player[playerid].list_extra) + pduel->write_buffer32(pcard->data.code | (pcard->is_position(POS_FACEUP) ? 0x80000000 : 0)); +} +void field::add_effect(effect* peffect, uint8_t owner_player) { + if (!peffect) + return; + if (effects.indexer.find(peffect) != effects.indexer.end()) + return; + if (peffect->type & EFFECT_TYPES_CHAIN_LINK && peffect->owner == temp_card) + return; + effect_container::iterator it; + if (!(peffect->type & EFFECT_TYPE_ACTIONS)) { + it = effects.aura_effect.emplace(peffect->code, peffect); + if(peffect->code == EFFECT_SPSUMMON_COUNT_LIMIT) + effects.spsummon_count_eff.insert(peffect); + if(peffect->type & EFFECT_TYPE_GRANT) + effects.grant_effect.emplace(peffect, field_effect::gain_effects()); + } else { + if (peffect->type & EFFECT_TYPE_IGNITION) + it = effects.ignition_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_TRIGGER_O) + it = effects.trigger_o_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_TRIGGER_F) + it = effects.trigger_f_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_QUICK_O) + it = effects.quick_o_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_QUICK_F) + it = effects.quick_f_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_ACTIVATE) + it = effects.activate_effect.emplace(peffect->code, peffect); + else if (peffect->type & EFFECT_TYPE_CONTINUOUS) + it = effects.continuous_effect.emplace(peffect->code, peffect); + else + return; + } + if (!peffect->handler) { + peffect->flag[0] |= EFFECT_FLAG_FIELD_ONLY; + peffect->handler = peffect->owner; + peffect->effect_owner = owner_player; + peffect->id = infos.field_id++; + } + peffect->card_type = peffect->owner->data.type; + effects.indexer.emplace(peffect, it); + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) { + if(peffect->is_disable_related()) + update_disable_check_list(peffect); + if(peffect->is_flag(EFFECT_FLAG_OATH)) + effects.oath.emplace(peffect, core.reason_effect); + if(peffect->reset_flag & RESET_PHASE) + effects.pheff.insert(peffect); + if(peffect->reset_flag & RESET_CHAIN) + effects.cheff.insert(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + effects.rechargeable.insert(peffect); + if(peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET) && peffect->is_flag(EFFECT_FLAG_CLIENT_HINT)) { + bool target_player[2] = {false, false}; + if(peffect->s_range) + target_player[owner_player] = true; + if(peffect->o_range) + target_player[1 - owner_player] = true; + if(target_player[0]) { + pduel->write_buffer8(MSG_PLAYER_HINT); + pduel->write_buffer8(0); + pduel->write_buffer8(PHINT_DESC_ADD); + pduel->write_buffer32(peffect->description); + } + if(target_player[1]) { + pduel->write_buffer8(MSG_PLAYER_HINT); + pduel->write_buffer8(1); + pduel->write_buffer8(PHINT_DESC_ADD); + pduel->write_buffer32(peffect->description); + } + } + } +} +void field::remove_effect(effect* peffect) { + auto eit = effects.indexer.find(peffect); + if (eit == effects.indexer.end()) + return; + auto& it = eit->second; + if (!(peffect->type & EFFECT_TYPE_ACTIONS)) { + effects.aura_effect.erase(it); + if(peffect->code == EFFECT_SPSUMMON_COUNT_LIMIT) + effects.spsummon_count_eff.erase(peffect); + if(peffect->type & EFFECT_TYPE_GRANT) + erase_grant_effect(peffect); + } else { + if (peffect->type & EFFECT_TYPE_IGNITION) + effects.ignition_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_TRIGGER_O) + effects.trigger_o_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_TRIGGER_F) + effects.trigger_f_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_QUICK_O) + effects.quick_o_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_QUICK_F) + effects.quick_f_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_ACTIVATE) + effects.activate_effect.erase(it); + else if (peffect->type & EFFECT_TYPE_CONTINUOUS) + effects.continuous_effect.erase(it); + } + effects.indexer.erase(peffect); + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) { + if(peffect->is_disable_related()) + update_disable_check_list(peffect); + if(peffect->is_flag(EFFECT_FLAG_OATH)) + effects.oath.erase(peffect); + if(peffect->reset_flag & RESET_PHASE) + effects.pheff.erase(peffect); + if(peffect->reset_flag & RESET_CHAIN) + effects.cheff.erase(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + effects.rechargeable.erase(peffect); + core.reseted_effects.insert(peffect); + if(peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET) && peffect->is_flag(EFFECT_FLAG_CLIENT_HINT)) { + bool target_player[2] = {false, false}; + if(peffect->s_range) + target_player[peffect->effect_owner] = true; + if(peffect->o_range) + target_player[1 - peffect->effect_owner] = true; + if(target_player[0]) { + pduel->write_buffer8(MSG_PLAYER_HINT); + pduel->write_buffer8(0); + pduel->write_buffer8(PHINT_DESC_REMOVE); + pduel->write_buffer32(peffect->description); + } + if(target_player[1]) { + pduel->write_buffer8(MSG_PLAYER_HINT); + pduel->write_buffer8(1); + pduel->write_buffer8(PHINT_DESC_REMOVE); + pduel->write_buffer32(peffect->description); + } + } + } +} +void field::remove_oath_effect(effect* reason_effect) { + for(auto oeit = effects.oath.begin(); oeit != effects.oath.end();) { + auto rm = oeit++; + if(rm->second == reason_effect) { + effect* peffect = rm->first; + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + remove_effect(peffect); + else + peffect->handler->remove_effect(peffect); + } + } +} +void field::release_oath_relation(effect* reason_effect) { + for(auto& oeit : effects.oath) + if(oeit.second == reason_effect) + oeit.second = 0; +} +void field::reset_phase(uint32_t phase) { + for(auto eit = effects.pheff.begin(); eit != effects.pheff.end();) { + auto rm = eit++; + if((*rm)->reset(phase, RESET_PHASE)) { + if((*rm)->is_flag(EFFECT_FLAG_FIELD_ONLY)) + remove_effect(*rm); + else + (*rm)->handler->remove_effect((*rm)); + } + } +} +void field::reset_chain() { + for(auto eit = effects.cheff.begin(); eit != effects.cheff.end();) { + auto rm = eit++; + if((*rm)->is_flag(EFFECT_FLAG_FIELD_ONLY)) + remove_effect(*rm); + else + (*rm)->handler->remove_effect((*rm)); + } +} +void field::add_effect_code(uint32_t code, int32_t playerid) { + if (playerid < 0 || playerid > PLAYER_NONE) + return; + auto count_map = &core.effect_count_code[playerid]; + if (code & EFFECT_COUNT_CODE_DUEL) + count_map = &core.effect_count_code_duel[playerid]; + else if (code & EFFECT_COUNT_CODE_CHAIN) + count_map = &core.effect_count_code_chain[playerid]; + (*count_map)[code]++; +} +int32_t field::get_effect_code(uint32_t code, int32_t playerid) { + if (playerid < 0 || playerid > PLAYER_NONE) + return 0; + auto count_map = &core.effect_count_code[playerid]; + if(code & EFFECT_COUNT_CODE_DUEL) + count_map = &core.effect_count_code_duel[playerid]; + else if(code & EFFECT_COUNT_CODE_CHAIN) + count_map = &core.effect_count_code_chain[playerid]; + auto iter = count_map->find(code); + if(iter == count_map->end()) + return 0; + return iter->second; +} +void field::dec_effect_code(uint32_t code, int32_t playerid) { + if (playerid < 0 || playerid > PLAYER_NONE) + return; + auto count_map = &core.effect_count_code[playerid]; + if (code & EFFECT_COUNT_CODE_DUEL) + count_map = &core.effect_count_code_duel[playerid]; + else if (code & EFFECT_COUNT_CODE_CHAIN) + count_map = &core.effect_count_code_chain[playerid]; + auto iter = count_map->find(code); + if(iter == count_map->end()) + return; + if (iter->second > 0) + iter->second--; +} +void field::filter_field_effect(uint32_t code, effect_set* eset, uint8_t sort) { + auto rg = effects.aura_effect.equal_range(code); + for (; rg.first != rg.second; ) { + effect* peffect = rg.first->second; + ++rg.first; + if (peffect->is_available()) + eset->push_back(peffect); + } + if(sort) + std::sort(eset->begin(), eset->end(), effect_sort_id); +} +//Get all cards in the target range of a EFFECT_TYPE_FIELD effect +void field::filter_affected_cards(effect* peffect, card_set* cset) { + if ((peffect->type & EFFECT_TYPE_ACTIONS) || !(peffect->type & EFFECT_TYPE_FIELD)) + return; + if (peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET | EFFECT_FLAG_SPSUM_PARAM)) + return; + uint8_t self = peffect->get_handler_player(); + if (!check_playerid(self)) + return; + std::vector cvec; + uint16_t range = peffect->s_range; + for(uint32_t p = 0; p < 2; ++p) { + if(range & LOCATION_MZONE) + cvec.push_back(&player[self].list_mzone); + if(range & LOCATION_SZONE) + cvec.push_back(&player[self].list_szone); + if(range & LOCATION_GRAVE) + cvec.push_back(&player[self].list_grave); + if(range & LOCATION_REMOVED) + cvec.push_back(&player[self].list_remove); + if(range & LOCATION_HAND) + cvec.push_back(&player[self].list_hand); + if(range & LOCATION_DECK) + cvec.push_back(&player[self].list_main); + if(range & LOCATION_EXTRA) + cvec.push_back(&player[self].list_extra); + range = peffect->o_range; + self = 1 - self; + } + for(auto& cvit : cvec) { + for(auto& pcard : *cvit) { + if(pcard && peffect->is_target(pcard)) + cset->insert(pcard); + } + } +} +void field::filter_inrange_cards(effect* peffect, card_set* cset) { + if(peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET | EFFECT_FLAG_SPSUM_PARAM)) + return; + uint8_t self = peffect->get_handler_player(); + if (!check_playerid(self)) + return; + uint16_t range = peffect->s_range; + std::vector cvec; + for(int32_t p = 0; p < 2; ++p) { + if(range & LOCATION_MZONE) + cvec.push_back(&player[self].list_mzone); + if(range & LOCATION_SZONE) + cvec.push_back(&player[self].list_szone); + if(range & LOCATION_GRAVE) + cvec.push_back(&player[self].list_grave); + if(range & LOCATION_REMOVED) + cvec.push_back(&player[self].list_remove); + if(range & LOCATION_HAND) + cvec.push_back(&player[self].list_hand); + if(range & LOCATION_DECK) + cvec.push_back(&player[self].list_main); + if(range & LOCATION_EXTRA) + cvec.push_back(&player[self].list_extra); + range = peffect->o_range; + self = 1 - self; + } + for(auto& cvit : cvec) { + for(auto& pcard : *cvit) { + if (pcard && (!(pcard->current.location & LOCATION_ONFIELD) || !pcard->is_treated_as_not_on_field()) && peffect->is_fit_target_function(pcard)) + cset->insert(pcard); + } + } +} +void field::filter_player_effect(uint8_t playerid, uint32_t code, effect_set* eset, uint8_t sort) { + auto rg = effects.aura_effect.equal_range(code); + for (; rg.first != rg.second; ++rg.first) { + effect* peffect = rg.first->second; + if (peffect->is_target_player(playerid) && peffect->is_available()) + eset->push_back(peffect); + } + if(sort) + std::sort(eset->begin(), eset->end(), effect_sort_id); +} +int32_t field::filter_matching_card(lua_State* L, int32_t findex, uint8_t self, uint32_t location1, uint32_t location2, group* pgroup, card* pexception, group* pexgroup, uint32_t extraargs, card** pret, int32_t fcount, int32_t is_target) { + if(self != 0 && self != 1) + return FALSE; + card_set result; + uint32_t location = location1; + for(uint32_t p = 0; p < 2; ++p) { + if(location & LOCATION_MZONE) { + for(auto& pcard : player[self].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field() + && pcard != pexception && !(pexgroup && pexgroup->has_card(pcard)) + && pduel->lua->check_filter(L, pcard, findex, extraargs) + && (!is_target || pcard->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = pcard; + return TRUE; + } + result.insert(pcard); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_SZONE) { + for(auto& pcard : player[self].list_szone) { + if(pcard && !pcard->is_treated_as_not_on_field() + && pcard != pexception && !(pexgroup && pexgroup->has_card(pcard)) + && pduel->lua->check_filter(L, pcard, findex, extraargs) + && (!is_target || pcard->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = pcard; + return TRUE; + } + result.insert(pcard); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_FZONE) { + card* pcard = player[self].list_szone[5]; + if(pcard && !pcard->is_treated_as_not_on_field() + && pcard != pexception && !(pexgroup && pexgroup->has_card(pcard)) + && pduel->lua->check_filter(L, pcard, findex, extraargs) + && (!is_target || pcard->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = pcard; + return TRUE; + } + result.insert(pcard); + if (fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + if(location & LOCATION_PZONE) { + for(int32_t i = 0; i < 2; ++i) { + card* pcard = player[self].list_szone[get_pzone_sequence(i)]; + if(pcard && pcard->current.pzone && !pcard->is_treated_as_not_on_field() + && pcard != pexception && !(pexgroup && pexgroup->has_card(pcard)) + && pduel->lua->check_filter(L, pcard, findex, extraargs) + && (!is_target || pcard->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = pcard; + return TRUE; + } + result.insert(pcard); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_DECK) { + for(auto cit = player[self].list_main.rbegin(); cit != player[self].list_main.rend(); ++cit) { + if(*cit != pexception && !(pexgroup && pexgroup->has_card(*cit)) + && pduel->lua->check_filter(L, *cit, findex, extraargs) + && (!is_target || (*cit)->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = *cit; + return TRUE; + } + result.insert(*cit); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_EXTRA) { + for(auto cit = player[self].list_extra.rbegin(); cit != player[self].list_extra.rend(); ++cit) { + if(*cit != pexception && !(pexgroup && pexgroup->has_card(*cit)) + && pduel->lua->check_filter(L, *cit, findex, extraargs) + && (!is_target || (*cit)->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = *cit; + return TRUE; + } + result.insert(*cit); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_HAND) { + for(auto& pcard : player[self].list_hand) { + if(pcard != pexception && !(pexgroup && pexgroup->has_card(pcard)) + && pduel->lua->check_filter(L, pcard, findex, extraargs) + && (!is_target || pcard->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = pcard; + return TRUE; + } + result.insert(pcard); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_GRAVE) { + for(auto cit = player[self].list_grave.rbegin(); cit != player[self].list_grave.rend(); ++cit) { + if(*cit != pexception && !(pexgroup && pexgroup->has_card(*cit)) + && pduel->lua->check_filter(L, *cit, findex, extraargs) + && (!is_target || (*cit)->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = *cit; + return TRUE; + } + result.insert(*cit); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + if(location & LOCATION_REMOVED) { + for(auto cit = player[self].list_remove.rbegin(); cit != player[self].list_remove.rend(); ++cit) { + if(*cit != pexception && !(pexgroup && pexgroup->has_card(*cit)) + && pduel->lua->check_filter(L, *cit, findex, extraargs) + && (!is_target || (*cit)->is_capable_be_effect_target(core.reason_effect, core.reason_player))) { + if(pret) { + *pret = *cit; + return TRUE; + } + result.insert(*cit); + if(fcount && (int32_t)result.size() >= fcount) + return TRUE; + } + } + } + location = location2; + self = 1 - self; + } + if (pgroup) + pgroup->container.insert(result.begin(), result.end()); + return FALSE; +} +// Duel.GetFieldGroup(), Duel.GetFieldGroupCount() +int32_t field::filter_field_card(uint8_t self, uint32_t location1, uint32_t location2, group* pgroup) { + if(self != 0 && self != 1) + return 0; + uint32_t location = location1; + card_set result; + for(uint32_t p = 0; p < 2; ++p) { + if(location & LOCATION_MZONE) { + for(auto& pcard : player[self].list_mzone) { + if(pcard && !pcard->get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP)) { + result.insert(pcard); + } + } + } + if(location & LOCATION_SZONE) { + for(auto& pcard : player[self].list_szone) { + if(pcard) { + result.insert(pcard); + } + } + } + if(location & LOCATION_FZONE) { + card* pcard = player[self].list_szone[5]; + if(pcard) { + result.insert(pcard); + } + } + if(location & LOCATION_PZONE) { + for(int32_t i = 0; i < 2; ++i) { + card* pcard = player[self].list_szone[get_pzone_sequence(i)]; + if(pcard && pcard->current.pzone) { + result.insert(pcard); + } + } + } + if(location & LOCATION_HAND) { + result.insert(player[self].list_hand.begin(), player[self].list_hand.end()); + } + if(location & LOCATION_DECK) { + result.insert(player[self].list_main.rbegin(), player[self].list_main.rend()); + } + if(location & LOCATION_EXTRA) { + result.insert(player[self].list_extra.rbegin(), player[self].list_extra.rend()); + } + if(location & LOCATION_GRAVE) { + result.insert(player[self].list_grave.rbegin(), player[self].list_grave.rend()); + } + if(location & LOCATION_REMOVED) { + result.insert(player[self].list_remove.rbegin(), player[self].list_remove.rend()); + } + location = location2; + self = 1 - self; + } + if (pgroup) + pgroup->container.insert(result.begin(), result.end()); + return (int32_t)result.size(); +} +effect* field::is_player_affected_by_effect(uint8_t playerid, uint32_t code) { + auto rg = effects.aura_effect.equal_range(code); + for (; rg.first != rg.second; ++rg.first) { + effect* peffect = rg.first->second; + if (peffect->is_target_player(playerid) && peffect->is_available()) + return peffect; + } + return nullptr; +} +int32_t field::get_release_list(lua_State* L, uint8_t playerid, card_set* release_list, card_set* ex_list, card_set* ex_list_oneof, int32_t use_con, int32_t use_hand, int32_t fun, int32_t exarg, card* exc, group* exg, uint32_t reason) { + uint32_t rcount = 0; + effect* re = core.reason_effect; + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && pcard != exc && !(exg && exg->has_card(pcard)) && pcard->is_releasable_by_nonsummon(playerid, reason) + && (reason != REASON_EFFECT || pcard->is_releasable_by_effect(playerid, re)) + && (!use_con || pduel->lua->check_filter(L, pcard, fun, exarg))) { + if(release_list) + release_list->insert(pcard); + pcard->release_param = 1; + ++rcount; + } + } + if(use_hand) { + for(auto& pcard : player[playerid].list_hand) { + if(pcard && pcard != exc && !(exg && exg->has_card(pcard)) && pcard->is_releasable_by_nonsummon(playerid, reason) + && (reason != REASON_EFFECT || pcard->is_releasable_by_effect(playerid, re)) + && (!use_con || pduel->lua->check_filter(L, pcard, fun, exarg))) { + if(release_list) + release_list->insert(pcard); + pcard->release_param = 1; + ++rcount; + } + } + } + int32_t ex_oneof_max = 0; + for(auto& pcard : player[1 - playerid].list_mzone) { + if(pcard && pcard != exc && !(exg && exg->has_card(pcard)) && (pcard->is_position(POS_FACEUP) || !use_con) + && pcard->is_releasable_by_nonsummon(playerid, reason) + && (reason != REASON_EFFECT || pcard->is_releasable_by_effect(playerid, re)) + && (!use_con || pduel->lua->check_filter(L, pcard, fun, exarg))) { + pcard->release_param = 1; + if(pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE)) { + if(ex_list) + ex_list->insert(pcard); + ++rcount; + } else { + effect* peffect = pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE_NONSUM); + if(!peffect || (peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && peffect->count_limit == 0)) + continue; + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(core.reason_player, PARAM_TYPE_INT); + if(!peffect->check_value_condition(3)) + continue; + if(ex_list_oneof) + ex_list_oneof->insert(pcard); + ex_oneof_max = 1; + } + } + } + return rcount + ex_oneof_max; +} +int32_t field::check_release_list(lua_State* L, uint8_t playerid, int32_t count, int32_t use_con, int32_t use_hand, int32_t fun, int32_t exarg, card* exc, group* exg, uint32_t reason) { + card_set relcard; + card_set relcard_oneof; + get_release_list(L, playerid, &relcard, &relcard, &relcard_oneof, use_con, use_hand, fun, exarg, exc, exg, reason); + bool has_oneof = false; + for(auto& pcard : core.must_select_cards) { + auto it = relcard.find(pcard); + if(it != relcard.end()) + relcard.erase(it); + else if(!has_oneof && relcard_oneof.find(pcard) != relcard_oneof.end()) + has_oneof = true; + else + return FALSE; + } + int32_t rcount = (int32_t)relcard.size(); + if(!has_oneof && !relcard_oneof.empty()) + ++rcount; + return (rcount >= count) ? TRUE : FALSE; +} +// return: the max release count of mg or all monsters on field +int32_t field::get_summon_release_list(card* target, card_set* release_list, card_set* ex_list, card_set* ex_list_oneof, group* mg, uint32_t ex, uint32_t releasable, uint32_t pos) { + uint8_t p = target->current.controler; + card_set ex_tribute; + effect_set eset; + target->filter_effect(EFFECT_ADD_EXTRA_TRIBUTE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->get_value() & pos) + filter_inrange_cards(eset[i], &ex_tribute); + } + uint32_t rcount = 0; + for(auto& pcard : player[p].list_mzone) { + if(pcard && ((releasable >> pcard->current.sequence) & 1) && pcard->is_releasable_by_summon(p, target)) { + if(mg && !mg->has_card(pcard)) + continue; + if(release_list) + release_list->insert(pcard); + if(pcard->is_affected_by_effect(EFFECT_DOUBLE_TRIBUTE, target)) + pcard->release_param = 2; + else + pcard->release_param = 1; + rcount += pcard->release_param; + } + } + uint32_t ex_oneof_max = 0; + for(auto& pcard : player[1 - p].list_mzone) { + if(!pcard || !((releasable >> (pcard->current.sequence + 16)) & 1) || !pcard->is_releasable_by_summon(p, target)) + continue; + if(mg && !mg->has_card(pcard)) + continue; + if(pcard->is_affected_by_effect(EFFECT_DOUBLE_TRIBUTE, target)) + pcard->release_param = 2; + else + pcard->release_param = 1; + if(ex || ex_tribute.find(pcard) != ex_tribute.end()) { + if(release_list) + release_list->insert(pcard); + rcount += pcard->release_param; + } else if(pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE)) { + if(ex_list) + ex_list->insert(pcard); + rcount += pcard->release_param; + } else { + effect* peffect = pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE_SUM); + if(!peffect || (peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && peffect->count_limit == 0)) + continue; + if(ex_list_oneof) + ex_list_oneof->insert(pcard); + if(ex_oneof_max < pcard->release_param) + ex_oneof_max = pcard->release_param; + } + } + for(auto& pcard : ex_tribute) { + if(pcard->current.location == LOCATION_MZONE || !pcard->is_releasable_by_summon(p, target)) + continue; + if(release_list) + release_list->insert(pcard); + if(pcard->is_affected_by_effect(EFFECT_DOUBLE_TRIBUTE, target)) + pcard->release_param = 2; + else + pcard->release_param = 1; + rcount += pcard->release_param; + } + return rcount + ex_oneof_max; +} +int32_t field::get_summon_count_limit(uint8_t playerid) { + effect_set eset; + filter_player_effect(playerid, EFFECT_SET_SUMMON_COUNT_LIMIT, &eset); + int32_t count = 1; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + int32_t c = eset[i]->get_value(); + if(c > count) + count = c; + } + return count; +} +int32_t field::get_draw_count(uint8_t playerid) { + if ((core.duel_rule >= 3) && (infos.turn_id == 1) && (infos.turn_player == playerid)) { + return 0; + } + effect_set eset; + filter_player_effect(playerid, EFFECT_DRAW_COUNT, &eset); + int32_t count = player[playerid].draw_count; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + int32_t c = eset[i]->get_value(); + if(c == 0) + return 0; + if(c > count) + count = c; + } + return count; +} +void field::get_ritual_material(uint8_t playerid, effect* peffect, card_set* material, uint8_t no_level) { + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && pcard->is_affect_by_effect(peffect) + && pcard->is_releasable_by_nonsummon(playerid, REASON_EFFECT) && pcard->is_releasable_by_effect(playerid, peffect) + && (no_level || pcard->get_level() > 0 || pcard->is_affected_by_effect(EFFECT_RITUAL_LEVEL_EX))) + material->insert(pcard); + if(pcard && pcard->is_affected_by_effect(EFFECT_OVERLAY_RITUAL_MATERIAL)) + for(auto& mcard : pcard->xyz_materials) + if (no_level || mcard->get_level() > 0 || pcard->is_affected_by_effect(EFFECT_RITUAL_LEVEL_EX)) + material->insert(mcard); + } + for(auto& pcard : player[1 - playerid].list_mzone) { + if(pcard && pcard->is_affect_by_effect(peffect) + && pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE) && pcard->is_position(POS_FACEUP) + && pcard->is_releasable_by_nonsummon(playerid, REASON_EFFECT) && pcard->is_releasable_by_effect(playerid, peffect) + && (no_level || pcard->get_level() > 0 || pcard->is_affected_by_effect(EFFECT_RITUAL_LEVEL_EX))) + material->insert(pcard); + } + for(auto& pcard : player[playerid].list_hand) + if((pcard->data.type & TYPE_MONSTER) && pcard->is_releasable_by_nonsummon(playerid, REASON_EFFECT)) + material->insert(pcard); + for(auto& pcard : player[playerid].list_grave) + if((pcard->data.type & TYPE_MONSTER) + && pcard->is_affected_by_effect(EFFECT_EXTRA_RITUAL_MATERIAL) && pcard->is_removeable(playerid, POS_FACEUP, REASON_EFFECT) + && (no_level || pcard->get_level() > 0 || pcard->is_affected_by_effect(EFFECT_RITUAL_LEVEL_EX))) + material->insert(pcard); + for(auto& pcard : player[playerid].list_extra) + if(((pcard->is_affected_by_effect(EFFECT_EXTRA_RITUAL_MATERIAL) || pcard->data.type & TYPE_MONSTER) && pcard->is_affected_by_effect(EFFECT_MAP_OF_HEAVEN) && pcard->is_capable_send_to_grave(playerid)) + && (no_level || pcard->get_level() > 0 || pcard->is_affected_by_effect(EFFECT_RITUAL_LEVEL_EX))) + material->insert(pcard); +} +void field::get_fusion_material(uint8_t playerid, card_set* material_all, card_set* material_base, uint32_t location) { + if(location & LOCATION_MZONE) { + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field()) + material_base->insert(pcard); + } + } + if(location & LOCATION_HAND) { + for(auto& pcard : player[playerid].list_hand) { + if(pcard->data.type & TYPE_MONSTER) + material_base->insert(pcard); + } + } + if(location & LOCATION_GRAVE) { + for(auto& pcard : player[playerid].list_grave) { + if(pcard->data.type & TYPE_MONSTER) + material_base->insert(pcard); + } + } + if(location & LOCATION_REMOVED) { + for(auto& pcard : player[playerid].list_remove) { + if(pcard->data.type & TYPE_MONSTER) + material_base->insert(pcard); + } + } + if(location & LOCATION_DECK) { + for(auto& pcard : player[playerid].list_main) { + if(pcard->data.type & TYPE_MONSTER) + material_base->insert(pcard); + } + } + if(location & LOCATION_EXTRA) { + for(auto& pcard : player[playerid].list_extra) { + if(pcard->data.type & TYPE_MONSTER) + material_base->insert(pcard); + } + } + if(location & LOCATION_SZONE) { + for(auto& pcard : player[playerid].list_szone) { + if(pcard && pcard->data.type & TYPE_MONSTER && !pcard->is_treated_as_not_on_field()) + material_base->insert(pcard); + } + } + if(location & LOCATION_PZONE) { + for(auto& pcard : player[playerid].list_szone) { + if(pcard && pcard->current.pzone && pcard->data.type & TYPE_MONSTER && !pcard->is_treated_as_not_on_field()) + material_base->insert(pcard); + } + } + for(auto& pcard : player[playerid].list_szone) { + if(pcard && pcard->is_affected_by_effect(EFFECT_EXTRA_FUSION_MATERIAL)) + material_all->insert(pcard); + } + for(auto& pcard : player[playerid].list_grave) { + if(pcard->is_affected_by_effect(EFFECT_EXTRA_FUSION_MATERIAL)) + material_all->insert(pcard); + } + material_all->insert(material_base->begin(), material_base->end()); +} +void field::ritual_release(const card_set& material) { + card_set rel; + card_set rem; + card_set tgy; + for(auto& pcard : material) { + if(pcard->current.location == LOCATION_GRAVE) + rem.insert(pcard); + else if(pcard->current.location == LOCATION_OVERLAY || pcard->current.location == LOCATION_EXTRA) + tgy.insert(pcard); + else + rel.insert(pcard); + } + send_to(tgy, core.reason_effect, REASON_RITUAL + REASON_EFFECT + REASON_MATERIAL, core.reason_player, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + release(rel, core.reason_effect, REASON_RITUAL + REASON_EFFECT + REASON_MATERIAL, core.reason_player); + send_to(rem, core.reason_effect, REASON_RITUAL + REASON_EFFECT + REASON_MATERIAL, core.reason_player, PLAYER_NONE, LOCATION_REMOVED, 0, POS_FACEUP); +} +void field::get_xyz_material(lua_State* L, card* scard, int32_t findex, uint32_t lv, int32_t maxc, group* mg) { + core.xmaterial_lst.clear(); + uint32_t xyz_level; + if(mg) { + for (auto& pcard : mg->container) { + if(pcard->is_can_be_xyz_material(scard) && (xyz_level = pcard->check_xyz_level(scard, lv)) + && (findex == 0 || pduel->lua->check_filter(L, pcard, findex, 0))) + core.xmaterial_lst.emplace((xyz_level >> 12) & 0xf, pcard); + } + } else { + int32_t playerid = scard->current.controler; + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && pcard->is_position(POS_FACEUP) && !pcard->is_treated_as_not_on_field() + && pcard->is_can_be_xyz_material(scard) && (xyz_level = pcard->check_xyz_level(scard, lv)) + && (findex == 0 || pduel->lua->check_filter(L, pcard, findex, 0))) + core.xmaterial_lst.emplace((xyz_level >> 12) & 0xf, pcard); + } + for(auto& pcard : player[1 - playerid].list_mzone) { + if(pcard && pcard->is_position(POS_FACEUP) && !pcard->is_treated_as_not_on_field() + && pcard->is_can_be_xyz_material(scard) && (xyz_level = pcard->check_xyz_level(scard, lv)) + && pcard->is_affected_by_effect(EFFECT_XYZ_MATERIAL) && (findex == 0 || pduel->lua->check_filter(L, pcard, findex, 0))) + core.xmaterial_lst.emplace((xyz_level >> 12) & 0xf, pcard); + } + } + if(core.global_flag & GLOBALFLAG_XMAT_COUNT_LIMIT) { + if(maxc > (int32_t)core.xmaterial_lst.size()) + maxc = (int32_t)core.xmaterial_lst.size(); + auto iter = core.xmaterial_lst.lower_bound(maxc); + core.xmaterial_lst.erase(core.xmaterial_lst.begin(), iter); + } +} +void field::get_overlay_group(uint8_t self, uint8_t s, uint8_t o, card_set* pset) { + if (!check_playerid(self)) + return; + uint8_t c = s; + for(int32_t p = 0; p < 2; ++p) { + if(c) { + for(auto& pcard : player[self].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field() && pcard->xyz_materials.size()) + pset->insert(pcard->xyz_materials.begin(), pcard->xyz_materials.end()); + } + } + self = 1 - self; + c = o; + } +} +int32_t field::get_overlay_count(uint8_t self, uint8_t s, uint8_t o) { + if (!check_playerid(self)) + return 0; + uint8_t c = s; + int32_t count = 0; + for(int32_t p = 0; p < 2; ++p) { + if(c) { + for(auto& pcard : player[self].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field()) + count += (int32_t)pcard->xyz_materials.size(); + } + } + self = 1 - self; + c = o; + } + return count; +} +// put all cards in the target of peffect into effects.disable_check_list +void field::update_disable_check_list(effect* peffect) { + card_set cset; + filter_affected_cards(peffect, &cset); + for (auto& pcard : cset) + add_to_disable_check_list(pcard); +} +void field::add_to_disable_check_list(card* pcard) { + auto result = effects.disable_check_set.insert(pcard); + if(!result.second) + return; + effects.disable_check_list.push_back(pcard); +} +void field::adjust_disable_check_list() { + if(!effects.disable_check_list.size()) + return; + do { + card_set checked; + while(effects.disable_check_list.size()) { + card* checking = effects.disable_check_list.front(); + effects.disable_check_list.pop_front(); + effects.disable_check_set.erase(checking); + checked.insert(checking); + if(checking->is_status(STATUS_TO_ENABLE | STATUS_TO_DISABLE)) // prevent loop + continue; + int32_t pre_disable = checking->get_status(STATUS_DISABLED | STATUS_FORBIDDEN); + checking->refresh_disable_status(); + int32_t new_disable = checking->get_status(STATUS_DISABLED | STATUS_FORBIDDEN); + if(pre_disable != new_disable && checking->is_status(STATUS_EFFECT_ENABLED)) { + checking->filter_disable_related_cards(); // change effects.disable_check_list + if(pre_disable) + checking->set_status(STATUS_TO_ENABLE, TRUE); + else + checking->set_status(STATUS_TO_DISABLE, TRUE); + } + } + for(auto& pcard : checked) { + if(pcard->is_status(STATUS_DISABLED) && pcard->is_status(STATUS_TO_DISABLE) && !pcard->is_status(STATUS_TO_ENABLE)) + pcard->reset(RESET_DISABLE, RESET_EVENT); + pcard->set_status(STATUS_TO_ENABLE | STATUS_TO_DISABLE, FALSE); + } + } while(effects.disable_check_list.size()); +} +// adjust SetUniqueOnField(), EFFECT_SELF_DESTROY, EFFECT_SELF_TOGRAVE +void field::adjust_self_destroy_set() { + if(core.selfdes_disabled || !core.unique_destroy_set.empty() || !core.self_destroy_set.empty() || !core.self_tograve_set.empty()) + return; + int32_t check_player = infos.turn_player; + for(int32_t i = 0; i < 2; ++i) { + std::vector uniq_set; + for(auto& ucard : core.unique_cards[check_player]) { + if(ucard->is_position(POS_FACEUP) && ucard->get_status(STATUS_EFFECT_ENABLED) + && !ucard->get_status(STATUS_DISABLED | STATUS_FORBIDDEN)) { + card_set cset; + ucard->get_unique_target(&cset, check_player); + if(cset.size() == 0) + ucard->unique_fieldid = 0; + else if(cset.size() == 1) { + auto cit = cset.begin(); + ucard->unique_fieldid = (*cit)->fieldid; + } else + uniq_set.push_back(ucard); + } + } + std::sort(uniq_set.begin(), uniq_set.end(), [](card* lhs, card* rhs) { return lhs->fieldid < rhs->fieldid; }); + for(auto& pcard : uniq_set) { + add_process(PROCESSOR_SELF_DESTROY, 0, 0, 0, check_player, 0, 0, 0, pcard); + core.unique_destroy_set.insert(pcard); + } + check_player = 1 - check_player; + } + card_set cset; + for(int32_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(pcard && pcard->is_position(POS_FACEUP) && !pcard->is_status(STATUS_BATTLE_DESTROYED)) + cset.insert(pcard); + } + for(auto& pcard : player[p].list_szone) { + if(pcard && pcard->is_position(POS_FACEUP)) + cset.insert(pcard); + } + } + for(auto& pcard : cset) { + effect* peffect = pcard->is_affected_by_effect(EFFECT_SELF_DESTROY); + if(peffect) { + core.self_destroy_set.insert(pcard); + } + } + if(core.global_flag & GLOBALFLAG_SELF_TOGRAVE) { + for(auto& pcard : cset) { + effect* peffect = pcard->is_affected_by_effect(EFFECT_SELF_TOGRAVE); + if(peffect) { + core.self_tograve_set.insert(pcard); + } + } + } + if(!core.self_destroy_set.empty()) + add_process(PROCESSOR_SELF_DESTROY, 10, 0, 0, 0, 0); + if(!core.self_tograve_set.empty()) + add_process(PROCESSOR_SELF_DESTROY, 20, 0, 0, 0, 0); +} +void field::erase_grant_effect(effect* peffect) { + auto eit = effects.grant_effect.find(peffect); + if (eit == effects.grant_effect.end()) + return; + for(auto& it : eit->second) + it.first->remove_effect(it.second); + effects.grant_effect.erase(eit); +} +int32_t field::adjust_grant_effect() { + int32_t adjusted = FALSE; + for(auto& eit : effects.grant_effect) { + effect* peffect = eit.first; + if (peffect->object_type != PARAM_TYPE_EFFECT) + continue; + effect* geffect = (effect*)peffect->get_label_object(); + if (geffect->type & EFFECT_TYPE_GRANT) + continue; + if (geffect->code == EFFECT_UNIQUE_CHECK) + continue; + card_set cset; + if(peffect->is_available()) + filter_affected_cards(peffect, &cset); + card_set add_set; + for(auto& pcard : cset) { + if(pcard->is_affect_by_effect(peffect) && !eit.second.count(pcard)) + add_set.insert(pcard); + } + card_set remove_set; + for(auto& cit : eit.second) { + card* pcard = cit.first; + if(!pcard->is_affect_by_effect(peffect) || !cset.count(pcard)) + remove_set.insert(pcard); + } + for(auto& pcard : add_set) { + effect* ceffect = geffect->clone(); + ceffect->owner = pcard; + ceffect->is_granted = 1; + pcard->add_effect(ceffect); + eit.second.emplace(pcard, ceffect); + } + for(auto& pcard : remove_set) { + auto it = eit.second.find(pcard); + pcard->remove_effect(it->second); + eit.second.erase(it); + } + if(!add_set.empty() || !remove_set.empty()) + adjusted = TRUE; + } + return adjusted; +} +void field::add_unique_card(card* pcard) { + uint8_t con = pcard->current.controler; + if(pcard->unique_pos[0]) + core.unique_cards[con].insert(pcard); + if(pcard->unique_pos[1]) + core.unique_cards[1 - con].insert(pcard); + pcard->unique_fieldid = 0; +} +void field::remove_unique_card(card* pcard) { + uint8_t con = pcard->current.controler; + if (!check_playerid(con)) + return; + if(pcard->unique_pos[0]) + core.unique_cards[con].erase(pcard); + if(pcard->unique_pos[1]) + core.unique_cards[1 - con].erase(pcard); +} +// return: pcard->unique_effect or 0 +effect* field::check_unique_onfield(card* pcard, uint8_t controler, uint8_t location, card* icard) { + for(auto& ucard : core.unique_cards[controler]) { + if((ucard != pcard) && (ucard != icard) && ucard->is_position(POS_FACEUP) && ucard->get_status(STATUS_EFFECT_ENABLED) + && !ucard->get_status(STATUS_DISABLED | STATUS_FORBIDDEN) + && ucard->unique_fieldid && ucard->check_unique_code(pcard) && (ucard->unique_location & location)) + return ucard->unique_effect; + } + if(!pcard->unique_code || !(pcard->unique_location & location) || pcard->get_status(STATUS_DISABLED | STATUS_FORBIDDEN)) + return 0; + card_set cset; + pcard->get_unique_target(&cset, controler, icard); + if(pcard->check_unique_code(pcard)) + cset.insert(pcard); + if(cset.size() >= 2) + return pcard->unique_effect; + return nullptr; +} +int32_t field::check_spsummon_once(card* pcard, uint8_t playerid) { + if(pcard->spsummon_code == 0) + return TRUE; + auto iter = core.spsummon_once_map[playerid].find(pcard->spsummon_code); + return (iter == core.spsummon_once_map[playerid].end()) || (iter->second == 0); +} +// increase the binary custom counter 1~5 +void field::check_card_counter(card* pcard, int32_t counter_type, int32_t playerid) { + auto& counter_map = (counter_type == ACTIVITY_SUMMON) ? core.summon_counter : + (counter_type == ACTIVITY_NORMALSUMMON) ? core.normalsummon_counter : + (counter_type == ACTIVITY_SPSUMMON) ? core.spsummon_counter : + (counter_type == ACTIVITY_FLIPSUMMON) ? core.flipsummon_counter : core.attack_counter; + for(auto& iter : counter_map) { + auto& info = iter.second; + if((playerid == 0) && (info.second & 0xffff) != 0) + continue; + if((playerid == 1) && (info.second & 0xffff0000) != 0) + continue; + if(info.first) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if(!pduel->lua->check_condition(info.first, 1)) { + if(playerid == 0) + info.second += 0x1; + else + info.second += 0x10000; + } + } + } +} +void field::check_card_counter(group* pgroup, int32_t counter_type, int32_t playerid) { + auto& counter_map = (counter_type == ACTIVITY_SUMMON) ? core.summon_counter : + (counter_type == ACTIVITY_NORMALSUMMON) ? core.normalsummon_counter : + (counter_type == ACTIVITY_SPSUMMON) ? core.spsummon_counter : + (counter_type == ACTIVITY_FLIPSUMMON) ? core.flipsummon_counter : core.attack_counter; + for(auto& iter : counter_map) { + auto& info = iter.second; + if((playerid == 0) && (info.second & 0xffff) != 0) + continue; + if((playerid == 1) && (info.second & 0xffff0000) != 0) + continue; + if(info.first) { + for(auto& pcard : pgroup->container) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if(!pduel->lua->check_condition(info.first, 1)) { + if(playerid == 0) + info.second += 0x1; + else + info.second += 0x10000; + break; + } + } + } + } +} + +void field::check_chain_counter(effect* peffect, int32_t playerid, int32_t chainid, bool cancel) { + for(auto& iter : core.chain_counter) { + auto& info = iter.second; + if(info.first) { + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(chainid, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(info.first, 3)) { + if(playerid == 0) { + if(!cancel) + info.second += 0x1; + else if(info.second & 0xffff) + info.second -= 0x1; + } else { + if(!cancel) + info.second += 0x10000; + else if(info.second & 0xffff0000) + info.second -= 0x10000; + } + } + } + } +} +void field::set_spsummon_counter(uint8_t playerid) { + ++core.spsummon_state_count[playerid]; + if(core.global_flag & GLOBALFLAG_SPSUMMON_COUNT) { + for(auto& peffect : effects.spsummon_count_eff) { + card* pcard = peffect->get_handler(); + if(peffect->limit_counter_is_available()) { + if(((playerid == pcard->current.controler) && peffect->s_range) || ((playerid != pcard->current.controler) && peffect->o_range)) { + ++pcard->spsummon_counter[playerid]; + } + } + } + } +} +int32_t field::check_spsummon_counter(uint8_t playerid, uint8_t ct) { + if(core.global_flag & GLOBALFLAG_SPSUMMON_COUNT) { + for(auto& peffect : effects.spsummon_count_eff) { + card* pcard = peffect->get_handler(); + uint16_t val = (uint16_t)peffect->value; + if(peffect->is_available()) { + if(pcard->spsummon_counter[playerid] + ct > val) + return FALSE; + } + } + } + return TRUE; +} +bool field::is_select_hide_deck_sequence(uint8_t playerid) { + return !core.select_deck_sequence_revealed && !(core.duel_options & DUEL_REVEAL_DECK_SEQ) && playerid == core.selecting_player; +} +int32_t field::check_lp_cost(uint8_t playerid, int32_t cost, uint32_t must_pay) { + effect_set eset; + int32_t val = cost; + filter_player_effect(playerid, EFFECT_LPCOST_CHANGE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(val, PARAM_TYPE_INT); + val = eset[i]->get_value(3); + } + if(val <= 0) { + if(must_pay) + return FALSE; + return TRUE; + } + if(!must_pay) { + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = cost; + e.reason = 0; + e.reason_effect = core.reason_effect; + e.reason_player = playerid; + if(effect_replace_check(EFFECT_LPCOST_REPLACE, e)) + return TRUE; + } + //cost[playerid].amount += val; + if(val <= player[playerid].lp) + return TRUE; + return FALSE; +} +/* +void field::save_lp_cost() { + for(uint8_t playerid = 0; playerid < 2; ++playerid) { + if(cost[playerid].count < 8) + cost[playerid].lpstack[cost[playerid].count] = cost[playerid].amount; + ++cost[playerid].count; + } +} +void field::restore_lp_cost() { + for(uint8_t playerid = 0; playerid < 2; ++playerid) { + --cost[playerid].count; + if(cost[playerid].count < 8) + cost[playerid].amount = cost[playerid].lpstack[cost[playerid].count]; + } +} +*/ +uint32_t field::get_field_counter(uint8_t self, uint8_t s, uint8_t o, uint16_t countertype) { + uint8_t c = s; + uint32_t count = 0; + for(int32_t p = 0; p < 2; ++p) { + if(c) { + for(auto& pcard : player[self].list_mzone) { + if(pcard) + count += pcard->get_counter(countertype); + } + for(auto& pcard : player[self].list_szone) { + if(pcard) + count += pcard->get_counter(countertype); + } + } + self = 1 - self; + c = o; + } + return count; +} +int32_t field::effect_replace_check(uint32_t code, const tevent& e) { + auto pr = effects.continuous_effect.equal_range(code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) + return TRUE; + } + return FALSE; +} +int32_t field::get_attack_target(card* pcard, card_vector* v, uint8_t chain_attack, bool select_target) { + pcard->direct_attackable = 0; + uint8_t p = pcard->current.controler; + card_vector auto_attack, only_attack, must_attack; + for(auto& atarget : player[1 - p].list_mzone) { + if(atarget) { + if(atarget->is_affected_by_effect(EFFECT_ONLY_BE_ATTACKED)) + auto_attack.push_back(atarget); + if(pcard->is_affected_by_effect(EFFECT_ONLY_ATTACK_MONSTER, atarget)) + only_attack.push_back(atarget); + if(pcard->is_affected_by_effect(EFFECT_MUST_ATTACK_MONSTER, atarget)) + must_attack.push_back(atarget); + } + } + card_vector* pv = nullptr; + int32_t atype = 0; + if(auto_attack.size()) { + atype = 1; + if(auto_attack.size() == 1) + pv = &auto_attack; + else + return atype; + } else if(pcard->is_affected_by_effect(EFFECT_ONLY_ATTACK_MONSTER)) { + atype = 2; + if(only_attack.size() == 1) + pv = &only_attack; + else + return atype; + } else if(pcard->is_affected_by_effect(EFFECT_MUST_ATTACK_MONSTER)) { + atype = 3; + if(must_attack.size()) + pv = &must_attack; + else + return atype; + } else { + atype = 4; + pv = &player[1 - p].list_mzone; + } + int32_t extra_count = 0; + effect_set eset; + pcard->filter_effect(EFFECT_EXTRA_ATTACK, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + int32_t val = eset[i]->get_value(pcard); + if(val > extra_count) + extra_count = val; + } + int32_t extra_count_m = 0; + eset.clear(); + pcard->filter_effect(EFFECT_EXTRA_ATTACK_MONSTER, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + int32_t val = eset[i]->get_value(pcard); + if(val > extra_count_m) + extra_count_m = val; + } + if(!chain_attack && pcard->announce_count > extra_count + && (!extra_count_m || pcard->announce_count > extra_count_m || pcard->announced_cards.findcard(0))) { + effect* peffect; + if((peffect = pcard->is_affected_by_effect(EFFECT_ATTACK_ALL)) && pcard->attack_all_target) { + for(auto& atarget : *pv) { + if(!atarget) + continue; + pduel->lua->add_param(atarget, PARAM_TYPE_CARD); + if(!peffect->check_value_condition(1)) + continue; + if(pcard->announced_cards.findcard(atarget) >= (uint32_t)peffect->get_value(atarget)) + continue; + if(atype >= 2 && atarget->is_affected_by_effect(EFFECT_IGNORE_BATTLE_TARGET, pcard)) + continue; + if(select_target && (atype == 2 || atype == 4)) { + if(atarget->is_affected_by_effect(EFFECT_CANNOT_BE_BATTLE_TARGET, pcard)) + continue; + if(pcard->is_affected_by_effect(EFFECT_CANNOT_SELECT_BATTLE_TARGET, atarget)) + continue; + } + v->push_back(atarget); + } + } + return atype; + } + //chain attack or announce count check passed + uint32_t mcount = 0; + for(auto& atarget : *pv) { + if(!atarget) + continue; + if(atype >= 2 && atarget->is_affected_by_effect(EFFECT_IGNORE_BATTLE_TARGET, pcard)) + continue; + ++mcount; + if(chain_attack && core.chain_attack_target && atarget != core.chain_attack_target) + continue; + if(select_target && (atype == 2 || atype == 4)) { + if(atarget->is_affected_by_effect(EFFECT_CANNOT_BE_BATTLE_TARGET, pcard)) + continue; + if(pcard->is_affected_by_effect(EFFECT_CANNOT_SELECT_BATTLE_TARGET, atarget)) + continue; + } + v->push_back(atarget); + } + if(atype <= 3) + return atype; + if((mcount == 0 || pcard->is_affected_by_effect(EFFECT_DIRECT_ATTACK) || core.attack_player) + && !pcard->is_affected_by_effect(EFFECT_CANNOT_DIRECT_ATTACK) + && !(!chain_attack && extra_count_m && pcard->announce_count > extra_count) + && !(chain_attack && core.chain_attack_target)) + pcard->direct_attackable = 1; + return atype; +} +bool field::confirm_attack_target() { + card_vector cv; + get_attack_target(core.attacker, &cv, core.chain_attack, false); + return core.attack_target && std::find(cv.begin(), cv.end(), core.attack_target) != cv.end() + || !core.attack_target && core.attacker->direct_attackable; +} +// update the validity for EFFECT_ATTACK_ALL (approximate solution) +void field::attack_all_target_check() { + if(!core.attacker) + return; + if(!core.attack_target) { + core.attacker->attack_all_target = FALSE; + return; + } + effect* peffect = core.attacker->is_affected_by_effect(EFFECT_ATTACK_ALL); + if(!peffect) + return; + if(!peffect->get_value(core.attack_target)) + core.attacker->attack_all_target = FALSE; +} +int32_t field::get_must_material_list(uint8_t playerid, uint32_t limit, card_set* must_list) { + effect_set eset; + filter_player_effect(playerid, limit, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + must_list->insert(eset[i]->handler); + return (int32_t)must_list->size(); +} +int32_t field::check_must_material(group* mg, uint8_t playerid, uint32_t limit) { + card_set must_list; + int32_t m = get_must_material_list(playerid, limit, &must_list); + if(m <= 0) + return TRUE; + if(!mg) + return FALSE; + for(auto& pcard : must_list) + if(!mg->has_card(pcard)) + return FALSE; + return TRUE; +} +void field::get_synchro_material(uint8_t playerid, card_set* material, effect* tuner_limit) { + if(tuner_limit && tuner_limit->value) { + int32_t location = tuner_limit->value; + if(location & LOCATION_MZONE) { + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field()) + material->insert(pcard); + } + } + if(location & LOCATION_HAND) { + for(auto& pcard : player[playerid].list_hand) { + if(pcard) + material->insert(pcard); + } + } + } else { + for(auto& pcard : player[playerid].list_mzone) { + if(pcard && !pcard->is_treated_as_not_on_field()) + material->insert(pcard); + } + for(auto& pcard : player[1 - playerid].list_mzone) { + if(pcard && pcard->is_affected_by_effect(EFFECT_EXTRA_SYNCHRO_MATERIAL) && !pcard->is_treated_as_not_on_field()) + material->insert(pcard); + } + for(auto& pcard : player[playerid].list_hand) { + if(pcard && pcard->is_affected_by_effect(EFFECT_EXTRA_SYNCHRO_MATERIAL)) + material->insert(pcard); + } + } +} +int32_t field::check_synchro_material(lua_State* L, card* pcard, int32_t findex1, int32_t findex2, int32_t min, int32_t max, card* smat, group* mg) { + if(mg) { + for(auto& tuner : mg->container) { + if(check_tuner_material(L, pcard, tuner, findex1, findex2, min, max, nullptr, mg)) + return TRUE; + } + } else { + card_set material; + get_synchro_material(pcard->current.controler, &material); + for(auto& tuner : material) { + if(check_tuner_material(L, pcard, tuner, findex1, findex2, min, max, smat, nullptr)) + return TRUE; + } + } + return FALSE; +} +int32_t field::check_tuner_material(lua_State* L, card* pcard, card* tuner, int32_t findex1, int32_t findex2, int32_t min, int32_t max, card* smat, group* mg) { + if(!tuner || (tuner->current.location == LOCATION_MZONE && !tuner->is_position(POS_FACEUP)) || !tuner->is_tuner(pcard) || !tuner->is_can_be_synchro_material(pcard)) + return FALSE; + effect* pcheck = tuner->is_affected_by_effect(EFFECT_SYNCHRO_CHECK); + if(pcheck) + pcheck->get_value(tuner); + if((mg && !mg->has_card(tuner)) || !pduel->lua->check_filter(L, tuner, findex1, 0)) { + pduel->restore_assumes(); + return FALSE; + } + effect* pcustom = tuner->is_affected_by_effect(EFFECT_SYNCHRO_MATERIAL_CUSTOM, pcard); + if(pcustom) { + if(!pcustom->target) { + pduel->restore_assumes(); + return FALSE; + } + pduel->lua->add_param(pcustom, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(interpreter::get_function_handle(L, findex2), PARAM_TYPE_FUNCTION); + pduel->lua->add_param(min, PARAM_TYPE_INT); + pduel->lua->add_param(max, PARAM_TYPE_INT); + if(pduel->lua->check_condition(pcustom->target, 5)) { + pduel->restore_assumes(); + return TRUE; + } + pduel->restore_assumes(); + return FALSE; + } + int32_t playerid = pcard->current.controler; + card_set must_list; + get_must_material_list(playerid, EFFECT_MUST_BE_SMATERIAL, &must_list); + must_list.erase(tuner); + int32_t location_count = get_spsummonable_count(pcard, playerid); + card_set handover_zone_cards; + if(location_count <= 0) { + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, pcard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, pcard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + if(handover_zone_cards.find(tuner) != handover_zone_cards.end()) + ++location_count; + } + int32_t location = LOCATION_MZONE; + effect* tuner_limit = tuner->is_affected_by_effect(EFFECT_TUNER_MATERIAL_LIMIT); + if(tuner_limit) { + if(tuner_limit->get_integer_value()) + location = tuner_limit->get_integer_value(); + if(tuner_limit->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(tuner_limit->s_range && tuner_limit->s_range > min) + min = tuner_limit->s_range; + if(tuner_limit->o_range && tuner_limit->o_range < max) + max = tuner_limit->o_range; + } + if(min > max) { + pduel->restore_assumes(); + return FALSE; + } + } + effect* extra_synchro_material_effect = tuner->is_affected_by_effect(EFFECT_EXTRA_SYNCHRO_MATERIAL); + if (extra_synchro_material_effect) { + if (!extra_synchro_material_effect->check_count_limit(playerid)) { + return FALSE; + } + int32_t value = extra_synchro_material_effect->get_value(pcard); + if (value <= 0) { + return FALSE; + } + } + int32_t mzone_limit = get_mzone_limit(playerid, playerid, LOCATION_REASON_TOFIELD); + if(mzone_limit < 0) { + if(location == LOCATION_HAND) { + pduel->restore_assumes(); + return FALSE; + } + int32_t ft = -mzone_limit; + if(ft > min) + min = ft; + if(min > max) { + pduel->restore_assumes(); + return FALSE; + } + } + int32_t lv = pcard->get_level(); + card_vector nsyn; + int32_t mcount = 1; + nsyn.push_back(tuner); + tuner->sum_param = tuner->get_synchro_level(pcard); + if(smat) { + if(pcheck) + pcheck->get_value(smat); + if((smat->current.location == LOCATION_MZONE && !smat->is_position(POS_FACEUP)) || !smat->is_can_be_synchro_material(pcard, tuner)) { + pduel->restore_assumes(); + return FALSE; + } + interpreter::card2value(L, pcard); + auto res = pduel->lua->check_filter(L, smat, findex2, 1); + lua_pop(L, 1); + if (!res) { + pduel->restore_assumes(); + return FALSE; + } + if(tuner_limit) { + if(tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(smat, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) { + pduel->restore_assumes(); + return FALSE; + } + } + if(tuner_limit->get_integer_value() && !((uint32_t)smat->current.location & tuner_limit->get_integer_value())) { + pduel->restore_assumes(); + return FALSE; + } + } + --min; + --max; + nsyn.push_back(smat); + smat->sum_param = smat->get_synchro_level(pcard); + ++mcount; + must_list.erase(smat); + if(location_count <= 0) { + if(handover_zone_cards.find(smat) != handover_zone_cards.end()) + ++location_count; + } + if(min == 0) { + if(location_count > 0 && must_list.size() == 0 && check_with_sum_limit_m(nsyn, lv, 0, 0, 0, 0xffff, 2)) { + pduel->restore_assumes(); + return TRUE; + } + if(max == 0) { + pduel->restore_assumes(); + return FALSE; + } + } + } + if(must_list.size()) { + for(auto& mcard : must_list) { + if(mg) { + if(!mg->has_card(mcard)) { + pduel->restore_assumes(); + return FALSE; + } + } + if(pcheck) + pcheck->get_value(mcard); + if((mcard->current.location == LOCATION_MZONE && !mcard->is_position(POS_FACEUP)) || !mcard->is_can_be_synchro_material(pcard, tuner)) { + pduel->restore_assumes(); + return FALSE; + } + interpreter::card2value(L, pcard); + auto res = pduel->lua->check_filter(L, mcard, findex2, 1); + lua_pop(L, 1); + if (!res) { + pduel->restore_assumes(); + return FALSE; + } + if(tuner_limit) { + if(tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(mcard, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) { + pduel->restore_assumes(); + return FALSE; + } + } + if(tuner_limit->value && !(mcard->current.location & location)) { + pduel->restore_assumes(); + return FALSE; + } + } + --min; + --max; + nsyn.push_back(mcard); + mcard->sum_param = mcard->get_synchro_level(pcard); + ++mcount; + } + } + if(mg) { + for(auto& pm : mg->container) { + if(pm == tuner || pm == smat || must_list.find(pm) != must_list.end() || !pm->is_can_be_synchro_material(pcard, tuner)) + continue; + if(tuner_limit) { + if(tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) + continue; + } + if(tuner_limit->get_integer_value() && !((uint32_t)pm->current.location & tuner_limit->get_integer_value())) + continue; + } + if(pcheck) + pcheck->get_value(pm); + if(pm->current.location == LOCATION_MZONE && !pm->is_position(POS_FACEUP)) + continue; + interpreter::card2value(L, pcard); + auto res = pduel->lua->check_filter(L, pm, findex2, 1); + lua_pop(L, 1); + if (!res) + continue; + nsyn.push_back(pm); + pm->sum_param = pm->get_synchro_level(pcard); + } + } else { + card_set material_set; + get_synchro_material(playerid, &material_set, tuner_limit); + for(auto& pm : material_set) { + if(!pm || pm == tuner || pm == smat || must_list.find(pm) != must_list.end() || !pm->is_can_be_synchro_material(pcard, tuner)) + continue; + if(tuner_limit && tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) + continue; + } + if(pcheck) + pcheck->get_value(pm); + if(pm->current.location == LOCATION_MZONE && !pm->is_position(POS_FACEUP)) + continue; + interpreter::card2value(L, pcard); + auto res = pduel->lua->check_filter(L, pm, findex2, 1); + lua_pop(L, 1); + if (!res) + continue; + nsyn.push_back(pm); + pm->sum_param = pm->get_synchro_level(pcard); + } + } + if(location_count > 0) { + int32_t ret = check_other_synchro_material(nsyn, lv, min, max, mcount); + pduel->restore_assumes(); + return ret; + } + auto start = nsyn.begin() + mcount; + for(auto cit = start; cit != nsyn.end(); ++cit) { + card* pm = *cit; + if(handover_zone_cards.find(pm) == handover_zone_cards.end()) + continue; + if(start != cit) + std::iter_swap(start, cit); + if(check_other_synchro_material(nsyn, lv, min - 1, max - 1, mcount + 1)) { + pduel->restore_assumes(); + return TRUE; + } + if(start != cit) + std::iter_swap(start, cit); + } + pduel->restore_assumes(); + return FALSE; +} +int32_t field::check_other_synchro_material(const card_vector& nsyn, int32_t lv, int32_t min, int32_t max, int32_t mcount) { + if(!(core.global_flag & GLOBALFLAG_SCRAP_CHIMERA)) { + if(check_with_sum_limit_m(nsyn, lv, 0, min, max, 0xffff, mcount)) { + return TRUE; + } + return FALSE; + } + effect* pscrap = nullptr; + for(auto& pm : nsyn) { + pscrap = pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA); + if(pscrap) + break; + } + if(!pscrap) { + if(check_with_sum_limit_m(nsyn, lv, 0, min, max, 0xffff, mcount)) { + return TRUE; + } + return FALSE; + } + card_vector nsyn_filtered; + for(auto& pm : nsyn) { + if(!pscrap->get_value(pm)) + nsyn_filtered.push_back(pm); + } + if(nsyn_filtered.size() == nsyn.size()) { + if(check_with_sum_limit_m(nsyn, lv, 0, min, max, 0xffff, mcount)) { + return TRUE; + } + } else { + bool mfiltered = true; + for(int32_t i = 0; i < mcount; ++i) { + if(pscrap->get_value(nsyn[i])) + mfiltered = false; + } + if(mfiltered && check_with_sum_limit_m(nsyn_filtered, lv, 0, min, max, 0xffff, mcount)) { + return TRUE; + } + for(int32_t i = 0; i < mcount; ++i) { + if(nsyn[i]->is_affected_by_effect(EFFECT_SCRAP_CHIMERA)) { + return FALSE; + } + } + card_vector nsyn_removed; + for(auto& pm : nsyn) { + if(!pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA)) + nsyn_removed.push_back(pm); + } + if(check_with_sum_limit_m(nsyn_removed, lv, 0, min, max, 0xffff, mcount)) { + return TRUE; + } + } + return FALSE; +} +int32_t field::check_tribute(card* pcard, int32_t min, int32_t max, group* mg, uint8_t toplayer, uint32_t zone, uint32_t releasable, uint32_t pos) { + int32_t ex = FALSE; + uint32_t sumplayer = pcard->current.controler; + if(toplayer == 1 - sumplayer) + ex = TRUE; + card_set release_list, ex_list; + int32_t m = get_summon_release_list(pcard, &release_list, &ex_list, 0, mg, ex, releasable, pos); + if(max > m) + max = m; + if(min > max) + return FALSE; + zone &= 0x1f; + int32_t s = 0; + int32_t ct = get_tofield_count(pcard, toplayer, LOCATION_MZONE, sumplayer, LOCATION_REASON_TOFIELD, zone); + if(ct <= 0 && max <= 0) + return FALSE; + for(auto& release_card : release_list) { + if(release_card->current.location == LOCATION_MZONE && release_card->current.controler == toplayer) { + ++s; + if((zone >> release_card->current.sequence) & 1) + ++ct; + } + } + if(ct <= 0) + return FALSE; + max -= (int32_t)ex_list.size(); + int32_t fcount = get_mzone_limit(toplayer, sumplayer, LOCATION_REASON_TOFIELD); + if(s < -fcount + 1) + return FALSE; + if(max < 0) + max = 0; + if(max < -fcount + 1) + return FALSE; + return TRUE; +} +void field::get_sum_params(uint32_t sum_param, int32_t& op1, int32_t& op2) { + op1 = sum_param & 0xffff; + op2 = (sum_param >> 16) & 0xffff; + if(op2 & 0x8000) { + op1 = sum_param & 0x7fffffff; + op2 = 0; + } +} +int32_t field::check_with_sum_limit(const card_vector& mats, int32_t acc, int32_t index, int32_t count, int32_t min, int32_t max, int32_t opmin) { + if(count > max) + return FALSE; + while(index < (int32_t)mats.size()) { + int32_t op1, op2; + get_sum_params(mats[index]->sum_param, op1, op2); + if(((op1 == acc && acc + opmin > op1) || (op2 && op2 == acc && acc + opmin > op2)) && count >= min) + return TRUE; + ++index; + if(acc > op1 && check_with_sum_limit(mats, acc - op1, index, count + 1, min, max, std::min(opmin, op1))) + return TRUE; + if(op2 && acc > op2 && check_with_sum_limit(mats, acc - op2, index, count + 1, min, max, std::min(opmin, op2))) + return TRUE; + } + return FALSE; +} +int32_t field::check_with_sum_limit_m(const card_vector& mats, int32_t acc, int32_t index, int32_t min, int32_t max, int32_t opmin, int32_t must_count) { + if(acc == 0) + return index == must_count && 0 >= min && 0 <= max; + if(index == must_count) + return check_with_sum_limit(mats, acc, index, 1, min, max, opmin); + if(index >= (int32_t)mats.size()) + return FALSE; + int32_t op1; + int32_t op2; + get_sum_params(mats[index]->sum_param, op1, op2); + if(acc >= op1 && check_with_sum_limit_m(mats, acc - op1, index + 1, min, max, std::min(opmin, op1), must_count)) + return TRUE; + if(op2 && acc >= op2 && check_with_sum_limit_m(mats, acc - op2, index + 1, min, max, std::min(opmin, op2), must_count)) + return TRUE; + return FALSE; +} +int32_t field::check_with_sum_greater_limit(const card_vector& mats, int32_t acc, int32_t index, int32_t opmin) { + while(index < (int32_t)mats.size()) { + int32_t op1; + int32_t op2; + get_sum_params(mats[index]->sum_param, op1, op2); + if((acc <= op1 && acc + opmin > op1) || (op2 && acc <= op2 && acc + opmin > op2)) + return TRUE; + ++index; + if(check_with_sum_greater_limit(mats, acc - op1, index, std::min(opmin, op1))) + return TRUE; + if(op2 && check_with_sum_greater_limit(mats, acc - op2, index, std::min(opmin, op2))) + return TRUE; + } + return FALSE; +} +int32_t field::check_with_sum_greater_limit_m(const card_vector& mats, int32_t acc, int32_t index, int32_t opmin, int32_t must_count) { + if(acc <= 0) + return index == must_count && acc + opmin > 0; + if(index == must_count) + return check_with_sum_greater_limit(mats, acc, index, opmin); + if(index >= (int32_t)mats.size()) + return FALSE; + int32_t op1; + int32_t op2; + get_sum_params(mats[index]->sum_param, op1, op2); + if(check_with_sum_greater_limit_m(mats, acc - op1, index + 1, std::min(opmin, op1), must_count)) + return TRUE; + if(op2 && check_with_sum_greater_limit_m(mats, acc - op2, index + 1, std::min(opmin, op2), must_count)) + return TRUE; + return FALSE; +} +int32_t field::check_xyz_material(lua_State* L, card* scard, int32_t findex, int32_t lv, int32_t min, int32_t max, group* mg) { + get_xyz_material(L, scard, findex, lv, max, mg); + int32_t playerid = scard->current.controler; + int32_t ct = get_spsummonable_count(scard, playerid); + card_set handover_zone_cards; + if(ct <= 0) { + int32_t ft = ct; + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, scard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, scard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end(); ++cit) { + card* pcard = cit->second; + if(handover_zone_cards.find(pcard) != handover_zone_cards.end()) + ++ft; + } + if(ft <= 0) + return FALSE; + } + int32_t mzone_limit = get_mzone_limit(playerid, playerid, LOCATION_REASON_TOFIELD); + if(mzone_limit <= 0) { + int32_t ft = -mzone_limit + 1; + if(ft > min) + min = ft; + if(min > max) + return FALSE; + } + card_set mcset; + int32_t mct = get_must_material_list(playerid, EFFECT_MUST_BE_XMATERIAL, &mcset); + if(mct > 0) { + if(ct == 0 && std::none_of(mcset.begin(), mcset.end(), + [=](card* pcard) { return handover_zone_cards.find(pcard) != handover_zone_cards.end(); })) + ++mct; + if(mct > max) + return FALSE; + } + if(!(core.global_flag & GLOBALFLAG_TUNE_MAGICIAN)) { + if(std::any_of(mcset.begin(), mcset.end(), + [=](card* pcard) { return std::find_if(core.xmaterial_lst.begin(), core.xmaterial_lst.end(), + [=](const std::pair& v) { return v.second == pcard; }) == core.xmaterial_lst.end(); })) + return FALSE; + return (int32_t)core.xmaterial_lst.size() >= min; + } + for(auto& pcard : mcset) { + effect* peffect = pcard->is_affected_by_effect(EFFECT_TUNE_MAGICIAN_X); + if(peffect) { + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end();) { + if(pcard != cit->second && peffect->get_value(cit->second)) + cit = core.xmaterial_lst.erase(cit); + else + ++cit; + } + } + } + for(auto& pcard : mcset) { + auto it = std::find_if(core.xmaterial_lst.begin(), core.xmaterial_lst.end(), + [=](const std::pair& v) { return v.second == pcard; }); + if(it == core.xmaterial_lst.end()) + return FALSE; + if(min < it->first) + min = it->first; + core.xmaterial_lst.erase(it); + } + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end(); ++cit) + cit->second->sum_param = 0; + int32_t digit = 1; + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end();) { + card* pcard = cit->second; + effect* peffect = pcard->is_affected_by_effect(EFFECT_TUNE_MAGICIAN_X); + if(peffect) { + if(std::any_of(mcset.begin(), mcset.end(), + [=](card* mcard) { return !!peffect->get_value(mcard); })) { + cit = core.xmaterial_lst.erase(cit); + continue; + } + digit <<= 1; + for(auto mit = core.xmaterial_lst.begin(); mit != core.xmaterial_lst.end(); ++mit) { + if(!peffect->get_value(mit->second)) + mit->second->sum_param |= digit; + } + pcard->sum_param |= digit; + } else + pcard->sum_param |= 1; + ++cit; + } + ct += (int32_t)std::count_if(mcset.begin(), mcset.end(), + [=](card* pcard) { return handover_zone_cards.find(pcard) != handover_zone_cards.end(); }); + std::multimap> mat; + for(int32_t icheck = 1; icheck <= digit; icheck <<= 1) { + mat.clear(); + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end(); ++cit) { + if(cit->second->sum_param & icheck) + mat.insert(*cit); + } + if(core.global_flag & GLOBALFLAG_XMAT_COUNT_LIMIT) { + int32_t maxc = std::min(max, (int32_t)mat.size() + mct); + auto iter = mat.lower_bound(maxc); + mat.erase(mat.begin(), iter); + } + if(ct <= 0) { + int32_t ft = ct; + for(auto cit = mat.begin(); cit != mat.end(); ++cit) { + card* pcard = cit->second; + if(handover_zone_cards.find(pcard) != handover_zone_cards.end()) + ++ft; + } + if(ft <= 0) + continue; + } + if((int32_t)mat.size() + mct >= min) + return TRUE; + } + return FALSE; +} +int32_t field::is_player_can_draw(uint8_t playerid) { + return !is_player_affected_by_effect(playerid, EFFECT_CANNOT_DRAW); +} +int32_t field::is_player_can_discard_deck(uint8_t playerid, int32_t count) { + if(count < 0 || (int32_t)player[playerid].list_main.size() < count) + return FALSE; + return !is_player_affected_by_effect(playerid, EFFECT_CANNOT_DISCARD_DECK); +} +int32_t field::is_player_can_discard_deck_as_cost(uint8_t playerid, int32_t count) { + if(count < 0 || (int32_t)player[playerid].list_main.size() < count) + return FALSE; + if(is_player_affected_by_effect(playerid, EFFECT_CANNOT_DISCARD_DECK)) + return FALSE; + card* topcard = player[playerid].list_main.back(); + if((count == 1) && topcard->is_position(POS_FACEUP)) + return topcard->is_capable_cost_to_grave(playerid); + bool cant_remove_s = !is_player_can_action(playerid, EFFECT_CANNOT_REMOVE); + bool cant_remove_o = !is_player_can_action(1 - playerid, EFFECT_CANNOT_REMOVE); + effect_set eset; + filter_field_effect(EFFECT_TO_GRAVE_REDIRECT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t redirect = eset[i]->get_value(); + uint8_t p = eset[i]->get_handler_player(); + if(redirect & LOCATION_REMOVED) { + if(topcard->is_affected_by_effect(EFFECT_CANNOT_REMOVE)) + continue; + if(cant_remove_s && (p == playerid) || cant_remove_o && (p != playerid)) + continue; + } + if((p == playerid && eset[i]->s_range & LOCATION_DECK) || (p != playerid && eset[i]->o_range & LOCATION_DECK)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_discard_hand(uint8_t playerid, card * pcard, effect * reason_effect, uint32_t reason) { + if(pcard->current.location != LOCATION_HAND) + return FALSE; + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_DISCARD_HAND, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 4)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_action(uint8_t playerid, uint32_t actionlimit) { + effect_set eset; + filter_player_effect(playerid, actionlimit, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_summon(uint32_t sumtype, uint8_t playerid, card * pcard, uint8_t toplayer) { + effect_set eset; + sumtype |= SUMMON_TYPE_NORMAL; + filter_player_effect(playerid, EFFECT_CANNOT_SUMMON, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + pduel->lua->add_param(POS_FACEUP, PARAM_TYPE_INT); + pduel->lua->add_param(toplayer, PARAM_TYPE_INT); + if(pduel->lua->check_condition(eset[i]->target, 6)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_mset(uint32_t sumtype, uint8_t playerid, card * pcard, uint8_t toplayer) { + effect_set eset; + sumtype |= SUMMON_TYPE_NORMAL; + filter_player_effect(playerid, EFFECT_CANNOT_MSET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + pduel->lua->add_param(POS_FACEDOWN, PARAM_TYPE_INT); + pduel->lua->add_param(toplayer, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 6)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_sset(uint8_t playerid, card * pcard) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_SSET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 3)) + return FALSE; + } + return TRUE; +} +// check player-effect EFFECT_CANNOT_SPECIAL_SUMMON without target +int32_t field::is_player_can_spsummon(uint8_t playerid) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_SPECIAL_SUMMON, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + } + return is_player_can_spsummon_count(playerid, 1); +} +int32_t field::is_player_can_spsummon(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t playerid, uint8_t toplayer, card* pcard) { + if(pcard->is_affected_by_effect(EFFECT_CANNOT_SPECIAL_SUMMON)) + return FALSE; + if(pcard->is_status(STATUS_FORBIDDEN)) + return FALSE; + if((pcard->data.type & TYPE_TOKEN) && (pcard->current.location & LOCATION_ONFIELD)) + return FALSE; + if(pcard->data.type & TYPE_LINK) + sumpos &= POS_FACEUP_ATTACK; + uint8_t position = pcard->get_spsummonable_position(reason_effect, sumtype, sumpos, playerid, toplayer); + if(position == 0) + return FALSE; + sumtype |= SUMMON_TYPE_SPECIAL; + save_lp_cost(); + if(!pcard->check_cost_condition(EFFECT_SPSUMMON_COST, playerid, sumtype)) { + restore_lp_cost(); + return FALSE; + } + restore_lp_cost(); + if(position & POS_FACEDOWN && is_player_affected_by_effect(playerid, EFFECT_DIVINE_LIGHT)) + position = (position & POS_FACEUP) | ((position & POS_FACEDOWN) >> 1); + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_SPECIAL_SUMMON, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(sumtype, PARAM_TYPE_INT); + pduel->lua->add_param(position, PARAM_TYPE_INT); + pduel->lua->add_param(toplayer, PARAM_TYPE_INT); + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + if (pduel->lua->check_condition(eset[i]->target, 7)) + return FALSE; + } + if(!check_spsummon_once(pcard, playerid)) + return FALSE; + if(!check_spsummon_counter(playerid)) + return FALSE; + return TRUE; +} +int32_t field::is_player_can_flipsummon(uint8_t playerid, card * pcard) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_FLIP_SUMMON, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 3)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_spsummon_monster(uint8_t playerid, uint8_t toplayer, uint8_t sumpos, uint32_t sumtype, card_data* pdata) { + temp_card->data = *pdata; + int32_t result = is_player_can_spsummon(core.reason_effect, sumtype, sumpos, playerid, toplayer, temp_card); + temp_card->data.clear(); + return result; +} +int32_t field::is_player_can_release(uint8_t playerid, card* pcard, uint32_t reason) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_RELEASE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 4)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_spsummon_count(uint8_t playerid, uint32_t count) { + effect_set eset; + filter_player_effect(playerid, EFFECT_LEFT_SPSUMMON_COUNT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + int32_t v = eset[i]->get_value(2); + if(v < (int32_t)count) + return FALSE; + } + return check_spsummon_counter(playerid, count); +} +int32_t field::is_player_can_place_counter(uint8_t playerid, card * pcard, uint16_t countertype, uint16_t count) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_PLACE_COUNTER, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 3)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_remove_counter(uint8_t playerid, card * pcard, uint8_t s, uint8_t o, uint16_t countertype, uint16_t count, uint32_t reason) { + if((pcard && pcard->get_counter(countertype) >= count) || (!pcard && get_field_counter(playerid, s, o, countertype) >= count)) + return TRUE; + auto pr = effects.continuous_effect.equal_range(EFFECT_RCOUNTER_REPLACE + countertype); + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = count; + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = playerid; + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) + return TRUE; + } + return FALSE; +} +int32_t field::is_player_can_remove_overlay_card(uint8_t playerid, card * pcard, uint8_t s, uint8_t o, uint16_t min, uint32_t reason) { + int32_t minc = min; + effect_set eset; + filter_player_effect(playerid, EFFECT_OVERLAY_REMOVE_COST_CHANGE_KOISHI, &eset); + for(int32_t i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(minc, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + int32_t param_count; + if(pcard) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + param_count = 5; + } else { + pduel->lua->add_param(s, PARAM_TYPE_INT); + pduel->lua->add_param(o, PARAM_TYPE_INT); + param_count = 6; + } + minc = eset[i]->get_value(param_count); + } + if((pcard && (int32_t)pcard->xyz_materials.size() >= minc) || (!pcard && get_overlay_count(playerid, s, o) >= minc)) + return TRUE; + auto pr = effects.continuous_effect.equal_range(EFFECT_OVERLAY_REMOVE_REPLACE); + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = minc; + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = playerid; + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) + return TRUE; + } + return FALSE; +} +int32_t field::is_player_can_send_to_grave(uint8_t playerid, card * pcard) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_TO_GRAVE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 3)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_send_to_hand(uint8_t playerid, card * pcard) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_TO_HAND, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + if (pduel->lua->check_condition(eset[i]->target, 4)) + return FALSE; + } + if(pcard->is_extra_deck_monster() && !is_player_can_send_to_deck(playerid, pcard)) + return FALSE; + return TRUE; +} +int32_t field::is_player_can_send_to_deck(uint8_t playerid, card * pcard) { + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_TO_DECK, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if (pduel->lua->check_condition(eset[i]->target, 3)) + return FALSE; + } + return TRUE; +} +int32_t field::is_player_can_remove(uint8_t playerid, card* pcard, uint32_t reason, effect* reason_effect) { + if(!reason_effect) + reason_effect = core.reason_effect; + effect_set eset; + filter_player_effect(playerid, EFFECT_CANNOT_REMOVE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(!eset[i]->target) + return FALSE; + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + if(pduel->lua->check_condition(eset[i]->target, 5)) + return FALSE; + } + return TRUE; +} +int32_t field::is_chain_negatable(uint8_t chaincount) { + effect_set eset; + if(chaincount < 0 || chaincount > core.current_chain.size()) + return FALSE; + effect* peffect; + if(chaincount == 0) + peffect = core.current_chain.back().triggering_effect; + else + peffect = core.current_chain[chaincount - 1].triggering_effect; + if(peffect->is_flag(EFFECT_FLAG_CANNOT_INACTIVATE)) + return FALSE; + filter_field_effect(EFFECT_CANNOT_INACTIVATE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(chaincount, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(1)) + return FALSE; + } + return TRUE; +} +int32_t field::is_chain_disablable(uint8_t chaincount) { + effect_set eset; + if(chaincount < 0 || chaincount > core.current_chain.size()) + return FALSE; + effect* peffect; + if(chaincount == 0) + peffect = core.current_chain.back().triggering_effect; + else + peffect = core.current_chain[chaincount - 1].triggering_effect; + if(!peffect->get_handler()->get_status(STATUS_FORBIDDEN)) { + if(peffect->is_flag(EFFECT_FLAG_CANNOT_DISABLE)) + return FALSE; + filter_field_effect(EFFECT_CANNOT_DISEFFECT, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(chaincount, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(1)) + return FALSE; + } + } + return TRUE; +} +int32_t field::is_chain_disabled(uint8_t chaincount) { + if(chaincount < 0 || chaincount > core.current_chain.size()) + return FALSE; + chain* pchain; + if(chaincount == 0) + pchain = &core.current_chain.back(); + else + pchain = &core.current_chain[chaincount - 1]; + if(pchain->flag & CHAIN_DISABLE_EFFECT) + return TRUE; + card* pcard = pchain->triggering_effect->get_handler(); + auto rg = pcard->single_effect.equal_range(EFFECT_DISABLE_CHAIN); + for(; rg.first != rg.second; ++rg.first) { + effect* peffect = rg.first->second; + if(peffect->get_value() == pchain->chain_id) { + peffect->reset_flag |= RESET_CHAIN; + return TRUE; + } + } + return FALSE; +} +int32_t field::check_chain_target(uint8_t chaincount, card * pcard) { + if(chaincount < 0 || chaincount > core.current_chain.size()) + return FALSE; + chain* pchain; + if(chaincount == 0) + pchain = &core.current_chain.back(); + else + pchain = &core.current_chain[chaincount - 1]; + effect* peffect = pchain->triggering_effect; + uint8_t tp = pchain->triggering_player; + if(!peffect->is_flag(EFFECT_FLAG_CARD_TARGET) || !peffect->target) + return FALSE; + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(tp, PARAM_TYPE_INT); + pduel->lua->add_param(pchain->evt.event_cards , PARAM_TYPE_GROUP); + pduel->lua->add_param(pchain->evt.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(pchain->evt.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(pchain->evt.reason_effect , PARAM_TYPE_EFFECT); + pduel->lua->add_param(pchain->evt.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pchain->evt.reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(0, PARAM_TYPE_INT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + return pduel->lua->check_condition(peffect->target, 10); +} +chain* field::get_chain(uint32_t chaincount) { + if(chaincount == 0 && core.continuous_chain.size() && (core.reason_effect->type & EFFECT_TYPE_CONTINUOUS)) + return &core.continuous_chain.back(); + if(chaincount == 0 || chaincount > core.current_chain.size()) { + chaincount = (uint32_t)core.current_chain.size(); + if(chaincount == 0) + return 0; + } + return &core.current_chain[chaincount - 1]; +} +int32_t field::get_cteffect(effect* peffect, int32_t playerid, int32_t store) { + card* phandler = peffect->get_handler(); + if(phandler->data.type != (TYPE_TRAP | TYPE_CONTINUOUS)) + return FALSE; + if(!(peffect->type & EFFECT_TYPE_ACTIVATE)) + return FALSE; + if(peffect->code != EVENT_FREE_CHAIN) + return FALSE; + if(peffect->cost || peffect->target || peffect->operation) + return FALSE; + if(store) { + core.select_chains.clear(); + core.select_options.clear(); + } + for(auto& efit : phandler->field_effect) { + effect* feffect = efit.second; + if(!(feffect->type & (EFFECT_TYPE_TRIGGER_F | EFFECT_TYPE_TRIGGER_O | EFFECT_TYPE_QUICK_O))) + continue; + if(!feffect->in_range(phandler)) + continue; + uint32_t code = efit.first; + if(code == EVENT_FREE_CHAIN || code == (EVENT_PHASE | infos.phase)) { + tevent test_event; + test_event.event_code = code; + if(get_cteffect_evt(feffect, playerid, test_event, store) && !store) + return TRUE; + } else { + for(const auto& ev : core.point_event) { + if(code != ev.event_code) + continue; + if(get_cteffect_evt(feffect, playerid, ev, store) && !store) + return TRUE; + } + for(const auto& ev : core.instant_event) { + if(code != ev.event_code) + continue; + if(get_cteffect_evt(feffect, playerid, ev, store) && !store) + return TRUE; + } + } + } + return (store && !core.select_chains.empty()) ? TRUE : FALSE; +} +int32_t field::get_cteffect_evt(effect* feffect, int32_t playerid, const tevent& e, int32_t store) { + if(!feffect->is_activateable(playerid, e, FALSE, FALSE, FALSE, FALSE, TRUE)) + return FALSE; + if(store) { + chain newchain; + newchain.evt = e; + newchain.triggering_effect = feffect; + core.select_chains.push_back(newchain); + core.select_options.push_back(feffect->description); + } + return TRUE; +} +int32_t field::check_cteffect_hint(effect* peffect, uint8_t playerid) { + card* phandler = peffect->get_handler(); + if(phandler->data.type != (TYPE_TRAP | TYPE_CONTINUOUS)) + return FALSE; + if(!(peffect->type & EFFECT_TYPE_ACTIVATE)) + return FALSE; + if(peffect->code != EVENT_FREE_CHAIN) + return FALSE; + if(peffect->cost || peffect->target || peffect->operation) + return FALSE; + for(auto& efit : phandler->field_effect) { + effect* feffect = efit.second; + if(!(feffect->type & (EFFECT_TYPE_TRIGGER_F | EFFECT_TYPE_TRIGGER_O | EFFECT_TYPE_QUICK_O))) + continue; + if(!feffect->in_range(phandler)) + continue; + uint32_t code = efit.first; + if(code == EVENT_FREE_CHAIN || code == EVENT_PHASE + infos.phase) { + tevent test_event; + test_event.event_code = code; + if(get_cteffect_evt(feffect, playerid, test_event, FALSE) + && (code != EVENT_FREE_CHAIN || check_hint_timing(feffect))) + return TRUE; + } else { + for(const auto& ev : core.point_event) { + if(code != ev.event_code) + continue; + if(get_cteffect_evt(feffect, playerid, ev, FALSE)) + return TRUE; + } + for(const auto& ev : core.instant_event) { + if(code != ev.event_code) + continue; + if(get_cteffect_evt(feffect, playerid, ev, FALSE)) + return TRUE; + } + } + } + return FALSE; +} +int32_t field::check_nonpublic_trigger(chain& ch) { + effect* peffect = ch.triggering_effect; + card* phandler = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) + && ((peffect->type & EFFECT_TYPE_SINGLE) && !peffect->is_flag(EFFECT_FLAG_SINGLE_RANGE) + && phandler->is_has_relation(ch) && (ch.triggering_location & (LOCATION_HAND | LOCATION_DECK)) + || (peffect->range & (LOCATION_HAND | LOCATION_DECK)))) { + ch.flag |= CHAIN_HAND_TRIGGER; + core.new_ochain_h.push_back(ch); + if(ch.triggering_location == LOCATION_HAND && phandler->is_position(POS_FACEDOWN) + || ch.triggering_location == LOCATION_DECK + || peffect->range && !peffect->in_range(ch)) + return FALSE; + } + return TRUE; +} +int32_t field::check_trigger_effect(const chain& ch) const { + effect* peffect = ch.triggering_effect; + card* phandler = peffect->get_handler(); + if(core.duel_rule >= MASTER_RULE_2020) + return phandler->is_has_relation(ch); + else { + if(peffect->type & EFFECT_TYPE_FIELD) + return phandler->is_has_relation(ch); + else { + if((ch.triggering_location & (LOCATION_DECK | LOCATION_HAND | LOCATION_EXTRA)) + && (ch.triggering_position & POS_FACEDOWN)) + return TRUE; + if(!(phandler->current.location & (LOCATION_DECK | LOCATION_HAND | LOCATION_EXTRA)) + || phandler->is_position(POS_FACEUP)) + return TRUE; + return FALSE; + } + } +} +int32_t field::check_spself_from_hand_trigger(const chain& ch) const { + effect* peffect = ch.triggering_effect; + uint8_t tp = ch.triggering_player; + if((peffect->status & EFFECT_STATUS_SPSELF) && (ch.flag & CHAIN_HAND_TRIGGER)) { + return std::none_of(core.current_chain.begin(), core.current_chain.end(), [tp](chain ch) { + return ch.triggering_player == tp + && (ch.triggering_effect->status & EFFECT_STATUS_SPSELF) && (ch.flag & CHAIN_HAND_TRIGGER); + }); + } + return TRUE; +} +int32_t field::is_able_to_enter_bp() { + return ((core.duel_options & DUEL_ATTACK_FIRST_TURN) || infos.turn_id != 1) + && infos.phase < PHASE_BATTLE_START + && !is_player_affected_by_effect(infos.turn_player, EFFECT_CANNOT_BP); +} diff --git a/core/field.h b/core/field.h new file mode 100644 index 0000000..98b9a81 --- /dev/null +++ b/core/field.h @@ -0,0 +1,858 @@ +/* + * field.h + * + * Created on: 2010-5-8 + * Author: Argon + */ + +#ifndef FIELD_H_ +#define FIELD_H_ + +#include "common.h" +#include "card.h" +#include "effectset.h" +#include "interpreter.h" +#include +#include +#include +#include +#include +#include + +#define MAX_COIN_COUNT 20 + +//summon action type +#define SUMMON_IN_IDLE 0 +#define SUMMON_IN_CHAIN 1 + +class card; +struct card_data; +class duel; +class group; +class effect; + +using effect_vector = std::vector; + +struct tevent { + card* trigger_card{ nullptr }; + group* event_cards{ nullptr }; + effect* reason_effect{ nullptr }; + uint32_t event_code{ 0 }; + uint32_t event_value{ 0 }; + uint32_t reason{ 0 }; + uint8_t event_player{ PLAYER_NONE }; + uint8_t reason_player{ PLAYER_NONE }; + + bool operator< (const tevent& v) const; +}; +struct optarget { + group* op_cards{ nullptr }; + uint8_t op_count{ 0 }; + uint8_t op_player{ PLAYER_NONE }; + int32_t op_param{ 0 }; +}; +struct chain { + using opmap = std::unordered_map; + uint16_t chain_id{ 0 }; + uint8_t chain_count{ 0 }; + uint8_t triggering_player{ PLAYER_NONE }; + uint8_t triggering_controler{ PLAYER_NONE }; + uint16_t triggering_location{ 0 }; + uint8_t triggering_sequence{ 0 }; + uint8_t triggering_position{ 0 }; + card_state triggering_state; + effect* triggering_effect{ nullptr }; + group* target_cards{ nullptr }; + int32_t replace_op{ 0 }; + uint8_t target_player{ PLAYER_NONE }; + int32_t target_param{ 0 }; + effect* disable_reason{ nullptr }; + uint8_t disable_player{ PLAYER_NONE }; + tevent evt; + opmap opinfos; + uint32_t flag{ 0 }; + effect_set required_handorset_effects; + metadata meta; + + static bool chain_operation_sort(const chain& c1, const chain& c2); + void set_triggering_state(card* pcard); +}; + +struct player_info { + int32_t lp{ 8000 }; + int32_t start_count{ 5 }; + int32_t draw_count{ 1 }; + uint32_t used_location{ 0 }; + uint32_t disabled_location{ 0 }; + uint32_t extra_p_count{ 0 }; + uint32_t tag_extra_p_count{ 0 }; + int32_t szone_size{ 6 }; + card_vector list_mzone; + card_vector list_szone; + card_vector list_main; + card_vector list_grave; + card_vector list_hand; + card_vector list_remove; + card_vector list_extra; + card_vector tag_list_main; + card_vector tag_list_hand; + card_vector tag_list_extra; +}; +struct field_effect { + using oath_effects = std::unordered_map; + using gain_effects = std::unordered_map; + using grant_effect_container = std::unordered_map; + + effect_container aura_effect; + effect_container ignition_effect; + effect_container activate_effect; + effect_container trigger_o_effect; + effect_container trigger_f_effect; + effect_container quick_o_effect; + effect_container quick_f_effect; + effect_container continuous_effect; + effect_indexer indexer; + oath_effects oath; + effect_collection pheff; + effect_collection cheff; + effect_collection rechargeable; + effect_collection spsummon_count_eff; + + std::list disable_check_list; + std::unordered_set disable_check_set; + + grant_effect_container grant_effect; +}; +struct field_info { + uint64_t card_id{ 1 }; + int32_t field_id{ 1 }; + uint16_t copy_id{ 1 }; + uint16_t turn_id{}; + uint16_t turn_id_by_player[2]{}; + uint16_t phase{}; + uint8_t turn_player{}; + uint8_t priorities[2]{}; + uint8_t can_shuffle{ TRUE }; +}; +struct lpcost { + int32_t count{}; + int32_t amount{}; + int32_t lpstack[8]{}; +}; +struct processor_unit { + uint16_t type{ 0 }; + uint16_t step{ 0 }; + effect* peffect{ nullptr }; + group* ptarget{ nullptr }; + uint32_t arg1{ 0 }; + uint32_t arg2{ 0 }; + uint32_t arg3{ 0 }; + uint32_t arg4{ 0 }; + int32_t value1{ 0 }; + int32_t value2{ 0 }; + int32_t value3{ 0 }; + int32_t value4{ 0 }; + void* ptr1{ nullptr }; + void* ptr2{ nullptr }; + void* ptr3{ nullptr }; + void* ptr4{ nullptr }; +}; +constexpr size_t SIZE_SVALUE = SIZE_RETURN_VALUE / 2; +constexpr size_t SIZE_IVALUE = SIZE_RETURN_VALUE / 4; +constexpr size_t SIZE_LVALUE = SIZE_RETURN_VALUE / 8; +union return_value { + uint8_t bvalue[SIZE_RETURN_VALUE]; + uint16_t svalue[SIZE_SVALUE]; + int32_t ivalue[SIZE_IVALUE]; + int64_t lvalue[SIZE_LVALUE]; +}; + +using option_vector = std::vector; +using card_list = std::list; +using event_list = std::list; +using chain_list = std::list; +using instant_f_list = std::map; +using chain_array = std::vector; +using processor_list = std::list; +struct delayed_effect_sort { + bool operator()(const std::pair& lhs, const std::pair& rhs) const; +}; +using delayed_effect_collection = std::set, delayed_effect_sort>; +using activity_map = std::unordered_map>; // (counter_id, (counter_filter, count[1]|count[0])) +// [YGOL] patch 0022 (attack preview): what field::preview_battle answers for one attacker and one target. +struct battle_preview { + uint8_t attacker_destroyed{ FALSE }; + uint8_t target_destroyed{ FALSE }; + uint8_t pierce{ FALSE }; + int32_t attacker_value{ 0 }; + int32_t target_value{ 0 }; + int32_t damage[2]{}; +}; +struct processor { + struct chain_limit_t { + chain_limit_t(int32_t f, int32_t p): function(f), player(p) {} + int32_t function{ 0 }; + int32_t player{ PLAYER_NONE }; + }; + using chain_limit_list = std::vector; + + processor_list units; + processor_list subunits; + processor_unit damage_step_reserved; + processor_unit summon_reserved; + card_vector select_cards; + card_vector unselect_cards; + card_vector summonable_cards; + card_vector spsummonable_cards; + card_vector repositionable_cards; + card_vector msetable_cards; + card_vector ssetable_cards; + card_vector attackable_cards; + effect_vector select_effects; + option_vector select_options; + card_vector must_select_cards; + event_list point_event; + event_list instant_event; + event_list queue_event; + event_list delayed_activate_event; + event_list full_event; + event_list used_event; + event_list single_event; + event_list solving_event; + event_list sub_solving_event; + chain_array select_chains; + chain_array current_chain; + chain_array ignition_priority_chains; + chain_list continuous_chain; + chain_list solving_continuous; + chain_list sub_solving_continuous; + chain_list delayed_continuous_tp; + chain_list delayed_continuous_ntp; + chain_list desrep_chain; + chain_list new_fchain; + chain_list new_fchain_s; + chain_list new_ochain; + chain_list new_ochain_s; + chain_list new_fchain_b; + chain_list new_ochain_b; + chain_list new_ochain_h; + chain_list new_chains; + delayed_effect_collection delayed_quick_tmp; + delayed_effect_collection delayed_quick; + instant_f_list quick_f_chain; + card_set leave_confirmed; + card_set special_summoning; + card_set unable_tofield_set; + card_set equiping_cards; + card_set control_adjust_set[2]; + card_set unique_destroy_set; + card_set self_destroy_set; + card_set self_tograve_set; + card_set trap_monster_adjust_set[2]; + card_set release_cards; + card_set release_cards_ex; + card_set release_cards_ex_oneof; + card_set battle_destroy_rep; + card_set fusion_materials; + card_set synchro_materials; + card_set operated_set; + card_set discarded_set; + card_set destroy_canceled; + card_set indestructable_count_set; + card_set delayed_enable_set; + card_set set_group_pre_set; + card_set set_group_set; + effect_set_v disfield_effects; + effect_set_v extra_mzone_effects; + effect_set_v extra_szone_effects; + std::set reseted_effects; + std::unordered_map readjust_map; + std::unordered_set unique_cards[2]; + std::unordered_map effect_count_code[3]; + std::unordered_map effect_count_code_duel[3]; + std::unordered_map effect_count_code_chain[3]; + std::unordered_map spsummon_once_map[2]; + std::multimap> xmaterial_lst; + + int32_t temp_var[4]{}; + uint32_t global_flag{ 0 }; + uint16_t pre_field[2]{}; + std::set opp_mzone; + chain_limit_list chain_limit; + chain_limit_list chain_limit_p; + uint8_t chain_solving{ FALSE }; + uint8_t conti_solving{ FALSE }; + uint8_t win_player{ 5 }; + uint8_t win_reason{ 0 }; + uint8_t re_adjust{ FALSE }; + effect* reason_effect{ nullptr }; + uint8_t reason_player{ PLAYER_NONE }; + card* summoning_card{ nullptr }; + uint8_t summon_depth{ 0 }; + uint8_t summon_cancelable{ FALSE }; + card* attacker{ nullptr }; + card* attack_target{ nullptr }; + uint8_t attacker_player{ PLAYER_NONE }; + uint8_t attack_target_player{ PLAYER_NONE }; + uint32_t limit_extra_summon_zone{ 0 }; + uint32_t limit_extra_summon_releasable{ 0 }; + card* limit_tuner{ nullptr }; + group* limit_syn{ nullptr }; + int32_t limit_syn_minc{ 0 }; + int32_t limit_syn_maxc{ 0 }; + group* limit_xyz{ nullptr }; + int32_t limit_xyz_minc{ 0 }; + int32_t limit_xyz_maxc{ 0 }; + group* limit_link{ nullptr }; + card* limit_link_card{ nullptr }; + int32_t limit_link_minc{ 0 }; + int32_t limit_link_maxc{ 0 }; + uint8_t not_material{ FALSE }; + uint8_t attack_cancelable{ FALSE }; + uint8_t attack_rollback{ FALSE }; + uint8_t effect_damage_step{ 0 }; + int32_t battle_damage[2]{}; + int32_t summon_count[2]{}; + uint8_t extra_summon[2]{}; + int32_t spe_effect[2]{}; + int32_t last_select_hint[2]{ 0 }; + uint32_t duel_options{ 0 }; + int32_t duel_rule{ CURRENT_RULE }; + uint32_t copy_reset{ 0 }; + int32_t copy_reset_count{ 0 }; + uint32_t last_control_changed_id{ 0 }; + uint32_t set_group_used_zones{ 0 }; + uint8_t set_group_seq[7]{}; + uint8_t dice_result[5]{}; + uint32_t coin_result{ 0 }; + int32_t coin_count{ 0 }; + bool is_target_ready{ false }; + bool is_gemini_summoning{ false }; + bool is_summon_negated{ false }; + + uint8_t to_bp{ FALSE }; + uint8_t to_m2{ FALSE }; + uint8_t to_ep{ FALSE }; + uint8_t skip_m2{ FALSE }; + uint8_t chain_attack{ FALSE }; + uint32_t chain_attacker_id{ 0 }; + card* chain_attack_target{ nullptr }; + uint8_t attack_player{ PLAYER_NONE }; + uint8_t selfdes_disabled{ FALSE }; + uint8_t overdraw[2]{}; + int32_t check_level{ 0 }; + uint8_t shuffle_check_disabled{ FALSE }; + uint8_t shuffle_hand_check[2]{}; + uint8_t shuffle_deck_check[2]{}; + uint8_t deck_reversed{ FALSE }; + uint8_t remove_brainwashing{ FALSE }; + uint8_t flip_delayed{ FALSE }; + uint8_t damage_calculated{ FALSE }; + uint8_t hand_adjusted{ FALSE }; + uint8_t summon_state_count[2]{}; + uint8_t normalsummon_state_count[2]{}; + uint8_t flipsummon_state_count[2]{}; + uint8_t spsummon_state_count[2]{}; + uint8_t attack_state_count[2]{}; + uint8_t battle_phase_count[2]{}; + uint8_t battled_count[2]{}; + uint8_t phase_action{ FALSE }; + uint32_t hint_timing[2]{}; + uint8_t current_player{ PLAYER_NONE }; + uint8_t conti_player{ PLAYER_NONE }; + uint8_t select_deck_sequence_revealed{ FALSE }; + uint8_t selecting_player{ 0 }; + activity_map summon_counter; + activity_map normalsummon_counter; + activity_map spsummon_counter; + activity_map flipsummon_counter; + activity_map attack_counter; + activity_map chain_counter; + processor_list recover_damage_reserve; + effect_vector dec_count_reserve; + uint8_t announce_card_silent{ FALSE }; +}; +class field { +public: + duel* pduel{}; + player_info player[2]; + card* temp_card{}; + field_info infos; + //lpcost cost[2]; + field_effect effects; + processor core; + return_value returns{}; + tevent nil_event; + card* rose_card{}; + uint32_t rose_level{0}; + + static int32_t field_used_count[32]; + explicit field(duel* pd); + ~field() = default; + void reload_field_info(); + + void add_card(uint8_t playerid, card* pcard, uint8_t location, uint8_t sequence, uint8_t pzone = FALSE); + void remove_card(card* pcard); + void move_card(uint8_t playerid, card* pcard, uint8_t location, uint8_t sequence, uint8_t pzone = FALSE); + void swap_card(card* pcard1, card* pcard2, uint8_t new_sequence1, uint8_t new_sequence2); + void swap_card(card* pcard1, card* pcard2); + void set_control(card* pcard, uint8_t playerid, uint16_t reset_phase, uint8_t reset_count); + + int32_t get_pzone_sequence(uint8_t pseq) const; + const card_vector* get_field_vector(uint8_t playerid, uint8_t location) const; + card* get_field_card(uint8_t playerid, uint32_t general_location, uint8_t sequence) const; + int32_t is_location_useable(uint8_t playerid, uint32_t general_location, uint8_t sequence) const; + int32_t get_useable_count(card* pcard, uint8_t playerid, uint8_t location, uint8_t uplayer, uint32_t reason, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_useable_count_fromex(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_spsummonable_count(card* pcard, uint8_t playerid, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_spsummonable_count_fromex(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_useable_count_other(card* pcard, uint8_t playerid, uint8_t location, uint8_t uplayer, uint32_t reason, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_tofield_count(card* pcard, uint8_t playerid, uint8_t location, uint32_t uplayer, uint32_t reason, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_useable_count_fromex_rule4(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_spsummonable_count_fromex_rule4(card* pcard, uint8_t playerid, uint8_t uplayer, uint32_t zone = 0xff, uint32_t* list = nullptr); + int32_t get_mzone_limit(uint8_t playerid, uint8_t uplayer, uint32_t reason); + int32_t get_szone_limit(uint8_t playerid, uint8_t uplayer, uint32_t reason); + uint32_t get_linked_zone(int32_t playerid); + uint32_t get_rule_zone_fromex(int32_t playerid, card* pcard); + void filter_must_use_mzone(uint8_t playerid, uint8_t uplayer, uint32_t reason, card* pcard, uint32_t* flag); + void get_linked_cards(uint8_t self, uint8_t s, uint8_t o, card_set* cset); + int32_t check_extra_link(int32_t playerid); + int32_t check_extra_link(int32_t playerid, card* pcard, int32_t sequence); + void get_cards_in_zone(card_set* cset, uint32_t zone, int32_t playerid, int32_t location); + void shuffle(uint8_t playerid, uint8_t location); + void reset_sequence(uint8_t playerid, uint8_t location); + void swap_deck_and_grave(uint8_t playerid); + void reverse_deck(uint8_t playerid); + void refresh_player_info(uint8_t playerid); + void tag_swap(uint8_t playerid); + + void add_effect(effect* peffect, uint8_t owner_player = PLAYER_NONE); + void remove_effect(effect* peffect); + void remove_oath_effect(effect* reason_effect); + void release_oath_relation(effect* reason_effect); + void reset_phase(uint32_t phase); + void reset_chain(); + void add_effect_code(uint32_t code, int32_t playerid); + int32_t get_effect_code(uint32_t code, int32_t playerid); + void dec_effect_code(uint32_t code, int32_t playerid); + + void filter_field_effect(uint32_t code, effect_set* eset, uint8_t sort = TRUE); + void filter_affected_cards(effect* peffect, card_set* cset); + void filter_inrange_cards(effect* peffect, card_set* cset); + void filter_player_effect(uint8_t playerid, uint32_t code, effect_set* eset, uint8_t sort = TRUE); + int32_t filter_matching_card(lua_State* L, int32_t findex, uint8_t self, uint32_t location1, uint32_t location2, group* pgroup, card* pexception, group* pexgroup, uint32_t extraargs, card** pret = nullptr, int32_t fcount = 0, int32_t is_target = FALSE); + int32_t filter_field_card(uint8_t self, uint32_t location, uint32_t location2, group* pgroup); + effect* is_player_affected_by_effect(uint8_t playerid, uint32_t code); + + int32_t get_release_list(lua_State* L, uint8_t playerid, card_set* release_list, card_set* ex_list, card_set* ex_list_oneof, int32_t use_con, int32_t use_hand, int32_t fun, int32_t exarg, card* exc, group* exg, uint32_t reason); + int32_t check_release_list(lua_State* L, uint8_t playerid, int32_t count, int32_t use_con, int32_t use_hand, int32_t fun, int32_t exarg, card* exc, group* exg, uint32_t reason); + int32_t get_summon_release_list(card* target, card_set* release_list, card_set* ex_list, card_set* ex_list_oneof, group* mg = nullptr, uint32_t ex = 0, uint32_t releasable = 0xff00ff, uint32_t pos = 0x1); + int32_t get_summon_count_limit(uint8_t playerid); + int32_t get_draw_count(uint8_t playerid); + void get_ritual_material(uint8_t playerid, effect* peffect, card_set* material, uint8_t no_level = FALSE); + void get_fusion_material(uint8_t playerid, card_set* material_all, card_set* material_base, uint32_t location); + void ritual_release(const card_set& material); + void get_xyz_material(lua_State* L, card* scard, int32_t findex, uint32_t lv, int32_t maxc, group* mg); + void get_overlay_group(uint8_t self, uint8_t s, uint8_t o, card_set* pset); + int32_t get_overlay_count(uint8_t self, uint8_t s, uint8_t o); + void update_disable_check_list(effect* peffect); + void add_to_disable_check_list(card* pcard); + void adjust_disable_check_list(); + void adjust_self_destroy_set(); + void erase_grant_effect(effect* peffect); + int32_t adjust_grant_effect(); + void add_unique_card(card* pcard); + void remove_unique_card(card* pcard); + effect* check_unique_onfield(card* pcard, uint8_t controler, uint8_t location, card* icard = nullptr); + int32_t check_spsummon_once(card* pcard, uint8_t playerid); + void check_card_counter(card* pcard, int32_t counter_type, int32_t playerid); + void check_card_counter(group* pgroup, int32_t counter_type, int32_t playerid); + void check_chain_counter(effect* peffect, int32_t playerid, int32_t chainid, bool cancel = false); + void set_spsummon_counter(uint8_t playerid); + int32_t check_spsummon_counter(uint8_t playerid, uint8_t ct = 1); + bool is_select_hide_deck_sequence(uint8_t playerid); + + int32_t check_lp_cost(uint8_t playerid, int32_t cost, uint32_t must_pay); + void save_lp_cost() {} + void restore_lp_cost() {} + int32_t pay_lp_cost(uint32_t step, uint8_t playerid, uint32_t cost, uint32_t must_pay); + + uint32_t get_field_counter(uint8_t self, uint8_t s, uint8_t o, uint16_t countertype); + int32_t effect_replace_check(uint32_t code, const tevent& e); + int32_t get_attack_target(card* pcard, card_vector* v, uint8_t chain_attack = FALSE, bool select_target = true); + bool confirm_attack_target(); + void attack_all_target_check(); + int32_t get_must_material_list(uint8_t playerid, uint32_t limit, card_set* must_list); + int32_t check_must_material(group* mg, uint8_t playerid, uint32_t limit); + void get_synchro_material(uint8_t playerid, card_set* material, effect* tuner_limit = nullptr); + + //check material + int32_t check_synchro_material(lua_State* L, card* pcard, int32_t findex1, int32_t findex2, int32_t min, int32_t max, card* smat, group* mg); + int32_t check_tuner_material(lua_State* L, card* pcard, card* tuner, int32_t findex1, int32_t findex2, int32_t min, int32_t max, card* smat, group* mg); + int32_t check_other_synchro_material(const card_vector& nsyn, int32_t lv, int32_t min, int32_t max, int32_t mcount); + int32_t check_tribute(card* pcard, int32_t min, int32_t max, group* mg, uint8_t toplayer, uint32_t zone = 0x1f, uint32_t releasable = 0xff00ff, uint32_t pos = 0x1); + static void get_sum_params(uint32_t sum_param, int32_t& op1, int32_t& op2); + static int32_t check_with_sum_limit(const card_vector& mats, int32_t acc, int32_t index, int32_t count, int32_t min, int32_t max, int32_t opmin); + static int32_t check_with_sum_limit_m(const card_vector& mats, int32_t acc, int32_t index, int32_t min, int32_t max, int32_t opmin, int32_t must_count); + static int32_t check_with_sum_greater_limit(const card_vector& mats, int32_t acc, int32_t index, int32_t opmin); + static int32_t check_with_sum_greater_limit_m(const card_vector& mats, int32_t acc, int32_t index, int32_t opmin, int32_t must_count); + int32_t check_xyz_material(lua_State* L, card* pcard, int32_t findex, int32_t lv, int32_t min, int32_t max, group* mg); + + int32_t is_player_can_draw(uint8_t playerid); + int32_t is_player_can_discard_deck(uint8_t playerid, int32_t count); + int32_t is_player_can_discard_deck_as_cost(uint8_t playerid, int32_t count); + int32_t is_player_can_discard_hand(uint8_t playerid, card* pcard, effect* reason_effect, uint32_t reason); + int32_t is_player_can_action(uint8_t playerid, uint32_t actionlimit); + int32_t is_player_can_summon(uint32_t sumtype, uint8_t playerid, card* pcard, uint8_t toplayer); + int32_t is_player_can_mset(uint32_t sumtype, uint8_t playerid, card* pcard, uint8_t toplayer); + int32_t is_player_can_sset(uint8_t playerid, card* pcard); + int32_t is_player_can_spsummon(uint8_t playerid); + int32_t is_player_can_spsummon(effect* reason_effect, uint32_t sumtype, uint8_t sumpos, uint8_t playerid, uint8_t toplayer, card* pcard); + int32_t is_player_can_flipsummon(uint8_t playerid, card* pcard); + int32_t is_player_can_spsummon_monster(uint8_t playerid, uint8_t toplayer, uint8_t sumpos, uint32_t sumtype, card_data* pdata); + int32_t is_player_can_spsummon_count(uint8_t playerid, uint32_t count); + int32_t is_player_can_release(uint8_t playerid, card* pcard, uint32_t reason); + int32_t is_player_can_place_counter(uint8_t playerid, card* pcard, uint16_t countertype, uint16_t count); + int32_t is_player_can_remove_counter(uint8_t playerid, card* pcard, uint8_t s, uint8_t o, uint16_t countertype, uint16_t count, uint32_t reason); + int32_t is_player_can_remove_overlay_card(uint8_t playerid, card* pcard, uint8_t s, uint8_t o, uint16_t count, uint32_t reason); + int32_t is_player_can_send_to_grave(uint8_t playerid, card* pcard); + int32_t is_player_can_send_to_hand(uint8_t playerid, card* pcard); + int32_t is_player_can_send_to_deck(uint8_t playerid, card* pcard); + int32_t is_player_can_remove(uint8_t playerid, card* pcard, uint32_t reason, effect* reason_effect = nullptr); + int32_t is_chain_negatable(uint8_t chaincount); + int32_t is_chain_disablable(uint8_t chaincount); + int32_t is_chain_disabled(uint8_t chaincount); + int32_t check_chain_target(uint8_t chaincount, card* pcard); + chain* get_chain(uint32_t chaincount); + int32_t get_cteffect(effect* peffect, int32_t playerid, int32_t store); + int32_t get_cteffect_evt(effect* feffect, int32_t playerid, const tevent& e, int32_t store); + int32_t check_cteffect_hint(effect* peffect, uint8_t playerid); + int32_t check_nonpublic_trigger(chain& ch); + int32_t check_trigger_effect(const chain& ch) const; + int32_t check_spself_from_hand_trigger(const chain& ch) const; + int32_t is_able_to_enter_bp(); + + void add_process(uint16_t type, uint16_t step, effect* peffect, group* target, int32_t arg1, int32_t arg2, int32_t arg3 = 0, int32_t arg4 = 0, void* ptr1 = nullptr, void* ptr2 = nullptr); + uint32_t process(); + int32_t execute_cost(uint16_t step, effect* peffect, uint8_t triggering_player); + int32_t execute_operation(uint16_t step, effect* peffect, uint8_t triggering_player); + int32_t execute_target(uint16_t step, effect* peffect, uint8_t triggering_player); + void raise_event(card* event_card, uint32_t event_code, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value); + void raise_event(const card_set& event_cards, uint32_t event_code, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value); + void raise_single_event(card* trigger_card, card_set* event_cards, uint32_t event_code, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value); + int32_t check_event(uint32_t code, tevent* pe = nullptr); + int32_t check_event_c(effect* peffect, uint8_t playerid, int32_t neglect_con, int32_t neglect_cost, int32_t copy_info, tevent* pe = nullptr); + int32_t check_hint_timing(effect* peffect); + int32_t process_phase_event(int16_t step, int32_t phase_event); + int32_t process_point_event(int16_t step, int32_t skip_trigger, int32_t skip_freechain, int32_t skip_new); + int32_t process_quick_effect(int16_t step, int32_t skip_freechain, uint8_t priority); + int32_t process_instant_event(); + int32_t process_single_event(); + int32_t process_single_event(effect* peffect, const tevent& e, chain_list& tp, chain_list& ntp); + int32_t process_idle_command(uint16_t step); + int32_t process_battle_command(uint16_t step); + int32_t process_damage_step(uint16_t step, uint32_t new_attack); + void calculate_battle_damage(effect** pdamchange, card** preason_card, uint8_t* battle_destroyed); + void preview_battle(card* attacker, card* target, battle_preview* out); + int32_t process_turn(uint16_t step, uint8_t turn_player); + + int32_t add_chain(uint16_t step); + void solve_continuous(uint8_t playerid, effect* peffect, const tevent& e); + int32_t solve_continuous(uint16_t step); + int32_t solve_chain(uint16_t step, uint32_t chainend_arg1, uint32_t chainend_arg2); + int32_t break_effect(); + void adjust_instant(); + void adjust_all(); + void refresh_location_info_instant(); + int32_t refresh_location_info(uint16_t step); + int32_t adjust_step(uint16_t step); + + //operations + int32_t negate_chain(uint8_t chaincount); + int32_t disable_chain(uint8_t chaincount, uint8_t forced); + void change_chain_effect(uint8_t chaincount, int32_t replace_op); + void change_target(uint8_t chaincount, group* targets); + void change_target_player(uint8_t chaincount, uint8_t playerid); + void change_target_param(uint8_t chaincount, int32_t param); + void remove_counter(uint32_t reason, card* pcard, uint32_t rplayer, uint32_t s, uint32_t o, uint32_t countertype, uint32_t count); + void remove_overlay_card(uint32_t reason, card* pcard, uint32_t rplayer, uint32_t s, uint32_t o, uint16_t min, uint16_t max); + void get_control(const card_set& targets, effect* reason_effect, uint32_t reason_player, uint32_t playerid, uint32_t reset_phase, uint32_t reset_count, uint32_t zone); + void get_control(card* target, effect* reason_effect, uint32_t reason_player, uint32_t playerid, uint32_t reset_phase, uint32_t reset_count, uint32_t zone); + void swap_control(effect* reason_effect, uint32_t reason_player, const card_set& targets1, const card_set& targets2, uint32_t reset_phase, uint32_t reset_count); + void swap_control(effect* reason_effect, uint32_t reason_player, card* pcard1, card* pcard2, uint32_t reset_phase, uint32_t reset_count); + void equip(uint32_t equip_player, card* equip_card, card* target, uint32_t up, uint32_t is_step); + void draw(effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, int32_t count); + void damage(effect* reason_effect, uint32_t reason, uint32_t reason_player, card* reason_card, uint32_t playerid, int32_t amount, uint32_t is_step = FALSE); + void recover(effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, int32_t amount, uint32_t is_step = FALSE); + void summon(uint32_t sumplayer, card* target, effect* proc, uint32_t ignore_count, uint32_t min_tribute, uint32_t zone = 0x1f, uint32_t action_type = SUMMON_IN_IDLE); + void mset(uint32_t setplayer, card* target, effect* proc, uint32_t ignore_count, uint32_t min_tribute, uint32_t zone = 0x1f, uint32_t action_type = SUMMON_IN_IDLE); + void special_summon_rule(uint32_t sumplayer, card* target, uint32_t summon_type, uint32_t action_type = SUMMON_IN_IDLE); + void special_summon(const card_set& target, uint32_t sumtype, uint32_t sumplayer, uint32_t playerid, uint32_t nocheck, uint32_t nolimit, uint32_t positions, uint32_t zone); + void special_summon_step(card* target, uint32_t sumtype, uint32_t sumplayer, uint32_t playerid, uint32_t nocheck, uint32_t nolimit, uint32_t positions, uint32_t zone); + void special_summon_complete(effect* reason_effect, uint8_t reason_player); + void destroy(card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid = 2, uint32_t destination = 0, uint32_t sequence = 0); + void destroy(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid = 2, uint32_t destination = 0, uint32_t sequence = 0); + void release(const card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player); + void release(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player); + void send_to(const card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence, uint32_t position, uint8_t send_activating = FALSE); + void send_to(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence, uint32_t position, uint8_t send_activating = FALSE); + void move_to_field(card* target, uint32_t move_player, uint32_t playerid, uint32_t destination, uint32_t positions, uint32_t enable = FALSE, uint32_t ret = 0, uint32_t pzone = FALSE, uint32_t zone = 0xff); + void change_position(const card_set& targets, effect* reason_effect, uint32_t reason_player, uint32_t au, uint32_t ad, uint32_t du, uint32_t dd, uint32_t flag, uint32_t enable = FALSE); + void change_position(card* target, effect* reason_effect, uint32_t reason_player, uint32_t npos, uint32_t flag, uint32_t enable = FALSE); + void operation_replace(int32_t type, int32_t step, group* targets); + void select_tribute_cards(card* target, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max, uint8_t toplayer, uint32_t zone); + + // summon + int32_t summon(uint16_t step, uint8_t sumplayer, card* target, effect* proc, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone, uint32_t action_type); + int32_t mset(uint16_t step, uint8_t setplayer, card* ptarget, effect* proc, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone, uint32_t action_type); + int32_t flip_summon(uint16_t step, uint8_t sumplayer, card* target, uint32_t action_type); + int32_t special_summon_rule(uint16_t step, uint8_t sumplayer, card* target, uint32_t summon_type, uint32_t action_type); + + int32_t remove_counter(uint16_t step, uint32_t reason, card* pcard, uint8_t rplayer, uint8_t s, uint8_t o, uint16_t countertype, uint16_t count); + int32_t remove_overlay_card(uint16_t step, uint32_t reason, card* pcard, uint8_t rplayer, uint8_t s, uint8_t o, uint16_t min, uint16_t max); + int32_t get_control(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets, uint8_t playerid, uint16_t reset_phase, uint8_t reset_count, uint32_t zone); + int32_t swap_control(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets1, group* targets2, uint16_t reset_phase, uint8_t reset_count); + int32_t self_destroy(uint16_t step, card* ucard, int32_t p); + int32_t trap_monster_adjust(uint16_t step); + int32_t equip(uint16_t step, uint8_t equip_player, card* equip_card, card* target, uint32_t up, uint32_t is_step); + int32_t draw(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t playerid, int32_t count); + int32_t damage(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, card* reason_card, uint8_t playerid, int32_t amount, uint32_t is_step); + int32_t recover(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t playerid, int32_t amount, uint32_t is_step); + int32_t sset(uint16_t step, uint8_t setplayer, uint8_t toplayer, card* ptarget, effect* reason_effect, uint32_t zone = 0xff); + int32_t sset_g(uint16_t step, uint8_t setplayer, uint8_t toplayer, group* ptarget, uint8_t confirm, effect* reason_effect, uint32_t zone = 0xff); + int32_t special_summon_step(uint16_t step, group* targets, card* target, uint32_t zone); + int32_t special_summon(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets, uint32_t zone); + int32_t destroy_replace(uint16_t step, group* targets, card* target, uint8_t battle); + int32_t destroy(uint16_t step, group* targets, effect* reason_effect, uint32_t reason, uint8_t reason_player); + int32_t release_replace(uint16_t step, group* targets, card* target); + int32_t release(uint16_t step, group* targets, effect* reason_effect, uint32_t reason, uint8_t reason_player); + int32_t send_replace(uint16_t step, group* targets, card* target); + int32_t send_to(uint16_t step, group* targets, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t send_activating); + int32_t discard_deck(uint16_t step, uint8_t playerid, uint8_t count, uint32_t reason); + int32_t move_to_field(uint16_t step, card* target, uint32_t enable, uint32_t ret, uint32_t pzone, uint32_t zone); + int32_t change_position(uint16_t step, group* targets, effect* reason_effect, uint8_t reason_player, uint32_t enable); + int32_t operation_replace(uint16_t step, effect* replace_effect, group* targets, card* target, int32_t is_destroy); + int32_t activate_effect(uint16_t step, effect* peffect); + int32_t select_synchro_material(int16_t step, uint8_t playerid, card* pcard, int32_t min, int32_t max, card* smat, group* mg, int32_t filter1, int32_t filter2); + int32_t select_xyz_material(int16_t step, uint8_t playerid, uint32_t lv, card* pcard, int32_t min, int32_t max); + int32_t select_release_cards(int16_t step, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max); + int32_t select_tribute_cards(int16_t step, card* target, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max, uint8_t toplayer, uint32_t zone); + int32_t toss_coin(uint16_t step, effect* reason_effect, uint8_t reason_player, uint8_t playerid, int32_t count); + int32_t toss_dice(uint16_t step, effect* reason_effect, uint8_t reason_player, uint8_t playerid, uint8_t count1, uint8_t count2); + int32_t rock_paper_scissors(uint16_t step, uint8_t repeat); + + bool check_response(size_t vector_size, int32_t min_len, int32_t max_len) const; + int32_t select_battle_command(uint16_t step, uint8_t playerid); + int32_t select_idle_command(uint16_t step, uint8_t playerid); + int32_t select_effect_yes_no(uint16_t step, uint8_t playerid, uint32_t description, card* pcard); + int32_t select_yes_no(uint16_t step, uint8_t playerid, uint32_t description); + int32_t select_option(uint16_t step, uint8_t playerid); + int32_t select_card(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max); + int32_t select_unselect_card(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max, uint8_t finishable); + int32_t select_chain(uint16_t step, uint8_t playerid, uint8_t spe_count); + int32_t select_place(uint16_t step, uint8_t playerid, uint32_t flag, uint8_t count); + int32_t select_position(uint16_t step, uint8_t playerid, uint32_t code, uint8_t positions); + int32_t select_tribute(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max); + int32_t select_counter(uint16_t step, uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t s, uint8_t o); + int32_t select_with_sum_limit(int16_t step, uint8_t playerid, int32_t acc, int32_t min, int32_t max); + int32_t sort_card(int16_t step, uint8_t playerid); + int32_t announce_race(int16_t step, uint8_t playerid, int32_t count, int32_t available); + int32_t announce_attribute(int16_t step, uint8_t playerid, int32_t count, int32_t available); + int32_t announce_card(int16_t step, uint8_t playerid); + int32_t announce_number(int16_t step, uint8_t playerid); +}; + +//Location Use Reason +#define LOCATION_REASON_TOFIELD 0x1 +#define LOCATION_REASON_CONTROL 0x2 +//Chain Info +#define CHAIN_DISABLE_ACTIVATE 0x01 +#define CHAIN_DISABLE_EFFECT 0x02 +#define CHAIN_HAND_EFFECT 0x04 +#define CHAIN_CONTINUOUS_CARD 0x08 +#define CHAIN_ACTIVATING 0x10 +#define CHAIN_HAND_TRIGGER 0x20 +#define CHAIN_FORCED 0x40 +#define CHAININFO_CHAIN_COUNT 0x01 +#define CHAININFO_TRIGGERING_EFFECT 0x02 +#define CHAININFO_TRIGGERING_PLAYER 0x04 +#define CHAININFO_TRIGGERING_CONTROLER 0x08 +#define CHAININFO_TRIGGERING_LOCATION 0x10 +#define CHAININFO_TRIGGERING_SEQUENCE 0x20 +#define CHAININFO_TARGET_CARDS 0x40 +#define CHAININFO_TARGET_PLAYER 0x80 +#define CHAININFO_TARGET_PARAM 0x100 +#define CHAININFO_DISABLE_REASON 0x200 +#define CHAININFO_DISABLE_PLAYER 0x400 +#define CHAININFO_CHAIN_ID 0x800 +#define CHAININFO_TYPE 0x1000 +#define CHAININFO_EXTTYPE 0x2000 +#define CHAININFO_TRIGGERING_POSITION 0x4000 +#define CHAININFO_TRIGGERING_CODE 0x8000 +#define CHAININFO_TRIGGERING_CODE2 0x10000 +//#define CHAININFO_TRIGGERING_TYPE 0x20000 +#define CHAININFO_TRIGGERING_LEVEL 0x40000 +#define CHAININFO_TRIGGERING_RANK 0x80000 +#define CHAININFO_TRIGGERING_ATTRIBUTE 0x100000 +#define CHAININFO_TRIGGERING_RACE 0x200000 +#define CHAININFO_TRIGGERING_ATTACK 0x400000 +#define CHAININFO_TRIGGERING_DEFENSE 0x800000 +//Timing +#define TIMING_DRAW_PHASE 0x1 +#define TIMING_STANDBY_PHASE 0x2 +#define TIMING_MAIN_END 0x4 +#define TIMING_BATTLE_START 0x8 +#define TIMING_BATTLE_END 0x10 +#define TIMING_END_PHASE 0x20 +#define TIMING_SUMMON 0x40 +#define TIMING_SPSUMMON 0x80 +#define TIMING_FLIPSUMMON 0x100 +#define TIMING_MSET 0x200 +#define TIMING_SSET 0x400 +#define TIMING_POS_CHANGE 0x800 +#define TIMING_ATTACK 0x1000 +#define TIMING_DAMAGE_STEP 0x2000 +#define TIMING_DAMAGE_CAL 0x4000 +#define TIMING_CHAIN_END 0x8000 +#define TIMING_DRAW 0x10000 +#define TIMING_DAMAGE 0x20000 +#define TIMING_RECOVER 0x40000 +#define TIMING_DESTROY 0x80000 +#define TIMING_REMOVE 0x100000 +#define TIMING_TOHAND 0x200000 +#define TIMING_TODECK 0x400000 +#define TIMING_TOGRAVE 0x800000 +#define TIMING_BATTLE_PHASE 0x1000000 +#define TIMING_EQUIP 0x2000000 +#define TIMING_BATTLE_STEP_END 0x4000000 +#define TIMING_BATTLED 0x8000000 + +#define GLOBALFLAG_DECK_REVERSE_CHECK 0x1 +#define GLOBALFLAG_BRAINWASHING_CHECK 0x2 +#define GLOBALFLAG_SCRAP_CHIMERA 0x4 +//#define GLOBALFLAG_DELAYED_QUICKEFFECT 0x8 +#define GLOBALFLAG_DETACH_EVENT 0x10 +//#define GLOBALFLAG_MUST_BE_SMATERIAL 0x20 +#define GLOBALFLAG_SPSUMMON_COUNT 0x40 +#define GLOBALFLAG_XMAT_COUNT_LIMIT 0x80 +#define GLOBALFLAG_SELF_TOGRAVE 0x100 +#define GLOBALFLAG_SPSUMMON_ONCE 0x200 +#define GLOBALFLAG_TUNE_MAGICIAN 0x400 +#define GLOBALFLAG_ACTIVATION_COUNT 0x800 +// + +#define PROCESSOR_ADJUST 1 +#define PROCESSOR_HINT 2 +#define PROCESSOR_TURN 3 +#define PROCESSOR_WAIT 4 +#define PROCESSOR_REFRESH_LOC 5 +#define PROCESSOR_SELECT_IDLECMD 10 +#define PROCESSOR_SELECT_EFFECTYN 11 +#define PROCESSOR_SELECT_BATTLECMD 12 +#define PROCESSOR_SELECT_YESNO 13 +#define PROCESSOR_SELECT_OPTION 14 +#define PROCESSOR_SELECT_CARD 15 +#define PROCESSOR_SELECT_CHAIN 16 +#define PROCESSOR_SELECT_UNSELECT_CARD 17 +#define PROCESSOR_SELECT_PLACE 18 +#define PROCESSOR_SELECT_POSITION 19 +#define PROCESSOR_SELECT_TRIBUTE_P 20 +#define PROCESSOR_SELECT_COUNTER 22 +#define PROCESSOR_SELECT_SUM 23 +#define PROCESSOR_SELECT_DISFIELD 24 +#define PROCESSOR_SORT_CARD 25 +#define PROCESSOR_SELECT_RELEASE 26 +#define PROCESSOR_SELECT_TRIBUTE 27 +#define PROCESSOR_POINT_EVENT 30 +#define PROCESSOR_QUICK_EFFECT 31 +#define PROCESSOR_IDLE_COMMAND 32 +#define PROCESSOR_PHASE_EVENT 33 +#define PROCESSOR_BATTLE_COMMAND 34 +#define PROCESSOR_DAMAGE_STEP 35 +#define PROCESSOR_ADD_CHAIN 40 +#define PROCESSOR_SOLVE_CHAIN 42 +#define PROCESSOR_SOLVE_CONTINUOUS 43 +#define PROCESSOR_EXECUTE_COST 44 +#define PROCESSOR_EXECUTE_OPERATION 45 +#define PROCESSOR_EXECUTE_TARGET 46 +#define PROCESSOR_DESTROY 50 +#define PROCESSOR_RELEASE 51 +#define PROCESSOR_SENDTO 52 +#define PROCESSOR_MOVETOFIELD 53 +#define PROCESSOR_CHANGEPOS 54 +#define PROCESSOR_OPERATION_REPLACE 55 +#define PROCESSOR_DESTROY_REPLACE 56 +#define PROCESSOR_RELEASE_REPLACE 57 +#define PROCESSOR_SENDTO_REPLACE 58 +#define PROCESSOR_SUMMON_RULE 60 +#define PROCESSOR_SPSUMMON_RULE 61 +#define PROCESSOR_SPSUMMON 62 +#define PROCESSOR_FLIP_SUMMON 63 +#define PROCESSOR_MSET 64 +#define PROCESSOR_SSET 65 +#define PROCESSOR_SPSUMMON_STEP 66 +#define PROCESSOR_SSET_G 67 +#define PROCESSOR_DRAW 70 +#define PROCESSOR_DAMAGE 71 +#define PROCESSOR_RECOVER 72 +#define PROCESSOR_EQUIP 73 +#define PROCESSOR_GET_CONTROL 74 +#define PROCESSOR_SWAP_CONTROL 75 +//#define PROCESSOR_CONTROL_ADJUST 76 +#define PROCESSOR_SELF_DESTROY 77 +#define PROCESSOR_TRAP_MONSTER_ADJUST 78 +#define PROCESSOR_PAY_LPCOST 80 +#define PROCESSOR_REMOVE_COUNTER 81 +#define PROCESSOR_ATTACK_DISABLE 82 +#define PROCESSOR_ACTIVATE_EFFECT 83 + +#define PROCESSOR_ANNOUNCE_RACE 110 +#define PROCESSOR_ANNOUNCE_ATTRIB 111 +#define PROCESSOR_ANNOUNCE_LEVEL 112 +#define PROCESSOR_ANNOUNCE_CARD 113 +#define PROCESSOR_ANNOUNCE_TYPE 114 +#define PROCESSOR_ANNOUNCE_NUMBER 115 +#define PROCESSOR_ANNOUNCE_COIN 116 +#define PROCESSOR_TOSS_DICE 117 +#define PROCESSOR_TOSS_COIN 118 +#define PROCESSOR_ROCK_PAPER_SCISSORS 119 + +#define PROCESSOR_SELECT_FUSION 131 +#define PROCESSOR_SELECT_SYNCHRO 132 +#define PROCESSOR_SELECT_XMATERIAL 139 +#define PROCESSOR_DISCARD_HAND 150 +#define PROCESSOR_DISCARD_DECK 151 +#define PROCESSOR_SORT_DECK 152 +#define PROCESSOR_REMOVE_OVERLAY 160 + +//#define PROCESSOR_SORT_CHAIN 21 +//#define PROCESSOR_DESTROY_S 100 +//#define PROCESSOR_RELEASE_S 101 +//#define PROCESSOR_SENDTO_S 102 +//#define PROCESSOR_CHANGEPOS_S 103 +//#define PROCESSOR_SELECT_YESNO_S 120 +//#define PROCESSOR_SELECT_OPTION_S 121 +//#define PROCESSOR_SELECT_CARD_S 122 +//#define PROCESSOR_SELECT_EFFECTYN_S 123 +//#define PROCESSOR_SELECT_UNSELECT_CARD_S 124 +//#define PROCESSOR_SELECT_PLACE_S 125 +//#define PROCESSOR_SELECT_POSITION_S 126 +//#define PROCESSOR_SELECT_TRIBUTE_S 127 +//#define PROCESSOR_SORT_CARDS_S 128 +//#define PROCESSOR_SELECT_RELEASE_S 129 +//#define PROCESSOR_SELECT_TARGET 130 +//#define PROCESSOR_SELECT_SUM_S 133 +//#define PROCESSOR_SELECT_DISFIELD_S 134 +//#define PROCESSOR_SPSUMMON_S 135 +//#define PROCESSOR_SPSUMMON_STEP_S 136 +//#define PROCESSOR_SPSUMMON_COMP_S 137 +//#define PROCESSOR_RANDOM_SELECT_S 138 +//#define PROCESSOR_DRAW_S 140 +//#define PROCESSOR_DAMAGE_S 141 +//#define PROCESSOR_RECOVER_S 142 +//#define PROCESSOR_EQUIP_S 143 +//#define PROCESSOR_GET_CONTROL_S 144 +//#define PROCESSOR_SWAP_CONTROL_S 145 +//#define PROCESSOR_MOVETOFIELD_S 161 + +#endif /* FIELD_H_ */ diff --git a/core/group.cpp b/core/group.cpp new file mode 100644 index 0000000..d32b632 --- /dev/null +++ b/core/group.cpp @@ -0,0 +1,24 @@ +/* + * group.cpp + * + * Created on: 2010-8-3 + * Author: Argon + */ + +#include "group.h" +#include "card.h" +#include "duel.h" + +group::group(duel* pd) { + pduel = pd; + it = container.begin(); +} +group::group(duel* pd, card* pcard) { + container.insert(pcard); + pduel = pd; + it = container.begin(); +} +group::group(duel* pd, const card_set& cset): container(cset) { + pduel = pd; + it = container.begin(); +} diff --git a/core/group.h b/core/group.h new file mode 100644 index 0000000..551f4c6 --- /dev/null +++ b/core/group.h @@ -0,0 +1,46 @@ +/* + * group.h + * + * Created on: 2010-5-6 + * Author: Argon + */ + +#ifndef GROUP_H_ +#define GROUP_H_ + +#include "common.h" +#include "sort.h" +#include "metadata.h" +#include +#include + +class card; +class duel; + +using card_set = std::set; + +constexpr uint32_t GTYPE_DEFAULT = 0; +constexpr uint32_t GTYPE_READ_ONLY = 1; +constexpr uint32_t GTYPE_KEEP_ALIVE = 2; + +class alignas(8) group { +public: + int32_t ref_handle{ 0 }; + uint32_t is_readonly{ GTYPE_DEFAULT }; + duel* pduel; + metadata meta; + card_set container; + card_set::iterator it; + bool is_iterator_dirty{ true }; + + bool has_card(card* c) { + return container.find(c) != container.end(); + } + + explicit group(duel* pd); + group(duel* pd, card* pcard); + group(duel* pd, const card_set& cset); + ~group() = default; +}; + +#endif /* GROUP_H_ */ diff --git a/core/interpreter.cpp b/core/interpreter.cpp new file mode 100644 index 0000000..0741079 --- /dev/null +++ b/core/interpreter.cpp @@ -0,0 +1,831 @@ +/* + * interpreter.cpp + * + * Created on: 2010-4-28 + * Author: Argon + */ + +#include +#include "duel.h" +#include "group.h" +#include "card.h" +#include "effect.h" +#include "scriptlib.h" +#include "ocgapi.h" +#include "interpreter.h" + +interpreter::interpreter(duel* pd, bool enable_unsafe_libraries, uint32_t create_flags) + : coroutines(256), pduel(pd), enable_unsafe_feature(enable_unsafe_libraries) { + mem_tracker = new LuaMemTracker(YGOPRO_LUA_MEMORY_SIZE); + lua_state = lua_newstate(LuaMemTracker::AllocThunk, mem_tracker); + current_state = lua_state; + std::memcpy(lua_getextraspace(lua_state), &pd, LUA_EXTRASPACE); //set_duel_info + if (create_flags & OCGCORE_CREATE_ENABLE_LUA_COVERAGE) + lua_coverage.enable(lua_state); + //Initial +#ifdef YGOPRO_NO_LUA_SAFE + luaL_openlibs(lua_state); +#else + luaL_requiref(lua_state, "base", luaopen_base, 0); + lua_pop(lua_state, 1); + luaL_requiref(lua_state, "string", luaopen_string, 1); + lua_pop(lua_state, 1); + luaL_requiref(lua_state, "utf8", luaopen_utf8, 1); + lua_pop(lua_state, 1); + luaL_requiref(lua_state, "table", luaopen_table, 1); + lua_pop(lua_state, 1); + luaL_requiref(lua_state, "math", luaopen_math, 1); + lua_pop(lua_state, 1); + luaL_requiref(lua_state, "coroutine", luaopen_coroutine, 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"); + if (!enable_unsafe_libraries) { + nil_out("dofile"); + nil_out("loadfile"); + } +#endif + //open all libs + scriptlib::open_cardlib(lua_state); + scriptlib::open_effectlib(lua_state); + scriptlib::open_grouplib(lua_state); + scriptlib::open_duellib(lua_state); + scriptlib::open_debuglib(lua_state); + //extra scripts + load_script("./script/constant.lua"); + load_script("./script/utility.lua"); + load_script("./script/procedure.lua"); + //load kpro constant + //card data constants + lua_pushinteger(lua_state, CARDDATA_CODE); + lua_setglobal(lua_state, "CARDDATA_CODE"); + lua_pushinteger(lua_state, CARDDATA_ALIAS); + lua_setglobal(lua_state, "CARDDATA_ALIAS"); + lua_pushinteger(lua_state, CARDDATA_SETCODE); + lua_setglobal(lua_state, "CARDDATA_SETCODE"); + lua_pushinteger(lua_state, CARDDATA_TYPE); + lua_setglobal(lua_state, "CARDDATA_TYPE"); + lua_pushinteger(lua_state, CARDDATA_LEVEL); + lua_setglobal(lua_state, "CARDDATA_LEVEL"); + lua_pushinteger(lua_state, CARDDATA_ATTRIBUTE); + lua_setglobal(lua_state, "CARDDATA_ATTRIBUTE"); + lua_pushinteger(lua_state, CARDDATA_ATTRIBUTE); + lua_setglobal(lua_state, "CARDDATA_ATTRIBUTE"); + lua_pushinteger(lua_state, CARDDATA_RACE); + lua_setglobal(lua_state, "CARDDATA_RACE"); + lua_pushinteger(lua_state, CARDDATA_ATTACK); + lua_setglobal(lua_state, "CARDDATA_ATTACK"); + lua_pushinteger(lua_state, CARDDATA_DEFENSE); + lua_setglobal(lua_state, "CARDDATA_DEFENSE"); + lua_pushinteger(lua_state, CARDDATA_LSCALE); + lua_setglobal(lua_state, "CARDDATA_LSCALE"); + lua_pushinteger(lua_state, CARDDATA_RSCALE); + lua_setglobal(lua_state, "CARDDATA_RSCALE"); + lua_pushinteger(lua_state, CARDDATA_LINK_MARKER); + lua_setglobal(lua_state, "CARDDATA_LINK_MARKER"); + //effect flag2s + lua_pushinteger(lua_state, EFFECT_FLAG2_SPOSITCH); + lua_setglobal(lua_state, "EFFECT_FLAG2_SPOSITCH"); + lua_pushinteger(lua_state, EFFECT_FLAG2_AVAILABLE_BD); + lua_setglobal(lua_state, "EFFECT_FLAG2_AVAILABLE_BD"); + lua_pushinteger(lua_state, EFFECT_FLAG2_ACTIVATE_MONSTER_SZONE); + lua_setglobal(lua_state, "EFFECT_FLAG2_ACTIVATE_MONSTER_SZONE"); + //effects + lua_pushinteger(lua_state, EFFECT_CHANGE_LINK_MARKER_KOISHI); + lua_setglobal(lua_state, "EFFECT_CHANGE_LINK_MARKER_KOISHI"); + lua_pushinteger(lua_state, EFFECT_ADD_LINK_MARKER_KOISHI); + lua_setglobal(lua_state, "EFFECT_ADD_LINK_MARKER_KOISHI"); + lua_pushinteger(lua_state, EFFECT_REMOVE_LINK_MARKER_KOISHI); + lua_setglobal(lua_state, "EFFECT_REMOVE_LINK_MARKER_KOISHI"); + lua_pushinteger(lua_state, EFFECT_CANNOT_LOSE_KOISHI); + lua_setglobal(lua_state, "EFFECT_CANNOT_LOSE_KOISHI"); + lua_pushinteger(lua_state, EFFECT_EXTRA_TOMAIN_KOISHI); + lua_setglobal(lua_state, "EFFECT_EXTRA_TOMAIN_KOISHI"); + lua_pushinteger(lua_state, EFFECT_OVERLAY_REMOVE_COST_CHANGE_KOISHI); + lua_setglobal(lua_state, "EFFECT_OVERLAY_REMOVE_COST_CHANGE_KOISHI"); + lua_pushinteger(lua_state, EFFECT_SYNCHRO_LEVEL_EX); + lua_setglobal(lua_state, "EFFECT_ALLOW_SYNCHRO_KOISHI"); // for compat only + // lua_pushinteger(lua_state, EFFECT_MINIATURE_GARDEN_GIRL); + // lua_setglobal(lua_state, "EFFECT_MINIATURE_GARDEN_GIRL"); + lua_pushinteger(lua_state, EFFECT_ADD_SUMMON_TYPE_KOISHI); + lua_setglobal(lua_state, "EFFECT_ADD_SUMMON_TYPE_KOISHI"); + lua_pushinteger(lua_state, EFFECT_REMOVE_SUMMON_TYPE_KOISHI); + lua_setglobal(lua_state, "EFFECT_REMOVE_SUMMON_TYPE_KOISHI"); + lua_pushinteger(lua_state, EFFECT_CHANGE_SUMMON_TYPE_KOISHI); + lua_setglobal(lua_state, "EFFECT_CHANGE_SUMMON_TYPE_KOISHI"); + lua_pushinteger(lua_state, EFFECT_CHANGE_SUMMON_LOCATION_KOISHI); + lua_setglobal(lua_state, "EFFECT_CHANGE_SUMMON_LOCATION_KOISHI"); + lua_pushinteger(lua_state, EFFECT_LINK_SPELL_KOISHI); + lua_setglobal(lua_state, "EFFECT_LINK_SPELL_KOISHI"); + + lua_pushinteger(lua_state, GETEFFECT_ALL); + lua_setglobal(lua_state, "GETEFFECT_ALL"); + lua_pushinteger(lua_state, GETEFFECT_INITIAL); + lua_setglobal(lua_state, "GETEFFECT_INITIAL"); + lua_pushinteger(lua_state, GETEFFECT_COPY); + lua_setglobal(lua_state, "GETEFFECT_COPY"); + lua_pushinteger(lua_state, GETEFFECT_GAIN); + lua_setglobal(lua_state, "GETEFFECT_GAIN"); + lua_pushinteger(lua_state, GETEFFECT_GRANT); + lua_setglobal(lua_state, "GETEFFECT_GRANT"); + + // lua_pushinteger(lua_state, EFFECT_SEA_PULSE); + // lua_setglobal(lua_state, "EFFECT_SEA_PULSE"); + lua_pushinteger(lua_state, EFFECT_MAP_OF_HEAVEN); + lua_setglobal(lua_state, "EFFECT_MAP_OF_HEAVEN"); + + //music hints + lua_pushinteger(lua_state, HINT_MUSIC); + lua_setglobal(lua_state, "HINT_MUSIC"); + lua_pushinteger(lua_state, HINT_SOUND); + lua_setglobal(lua_state, "HINT_SOUND"); + lua_pushinteger(lua_state, HINT_MUSIC_OGG); + lua_setglobal(lua_state, "HINT_MUSIC_OGG"); + //detect operating system +#ifdef _WIN32 + lua_pushboolean(lua_state, 1); + lua_setglobal(lua_state, "_WIN32"); +#endif +} +interpreter::~interpreter() { + lua_close(lua_state); + delete mem_tracker; +} +void interpreter::register_card(card *pcard) { + if (!pcard) + return; + //create a card in by userdata + luaL_checkstack(lua_state, 1, nullptr); + card ** ppcard = (card**) lua_newuserdata(lua_state, sizeof(card*)); //+1 userdata + *ppcard = pcard; + pcard->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); //-1 + //some userdata may be created in script like token so use current_state + luaL_checkstack(current_state, 1, nullptr); + lua_rawgeti(current_state, LUA_REGISTRYINDEX, pcard->ref_handle); //+1 userdata + load_card_script(pcard->data.get_original_code()); + //stack: table cxxx, userdata + //set metatable of pointer to base script + lua_setmetatable(current_state, -2); //-1 + lua_pop(current_state, 1); //-1 + //Initial + if(is_load_script(pcard->data)) { + pcard->set_status(STATUS_INITIALIZING, TRUE); + add_param(pcard, PARAM_TYPE_CARD); + call_card_function(pcard, "initial_effect", 1, 0); + pcard->set_status(STATUS_INITIALIZING, FALSE); + } + pcard->cardid = pduel->game_field->infos.card_id++; +} +void interpreter::unregister_card(card *pcard) { + if (!pcard) + return; + luaL_unref(lua_state, LUA_REGISTRYINDEX, pcard->ref_handle); + pcard->ref_handle = 0; +} +void interpreter::register_effect(effect *peffect) { + if (!peffect) + return; + //create a effect in userdata + luaL_checkstack(lua_state, 3, nullptr); + effect ** ppeffect = (effect**) lua_newuserdata(lua_state, sizeof(effect*)); + *ppeffect = peffect; + peffect->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); + //set metatable of pointer to base script + lua_rawgeti(lua_state, LUA_REGISTRYINDEX, peffect->ref_handle); + lua_getglobal(lua_state, "Effect"); + lua_setmetatable(lua_state, -2); + lua_pop(lua_state, 1); +} +void interpreter::unregister_effect(effect *peffect) { + if (!peffect) + return; + if(peffect->condition) + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->condition); + if(peffect->cost) + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->cost); + if(peffect->target) + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->target); + if(peffect->operation) + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->operation); + if(peffect->value && peffect->is_flag(EFFECT_FLAG_FUNC_VALUE)) + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->value); + luaL_unref(lua_state, LUA_REGISTRYINDEX, peffect->ref_handle); + peffect->ref_handle = 0; +} +void interpreter::register_group(group *pgroup) { + if (!pgroup) + return; + //create a group in by userdata + luaL_checkstack(lua_state, 3, nullptr); + group ** ppgroup = (group**) lua_newuserdata(lua_state, sizeof(group*)); + *ppgroup = pgroup; + pgroup->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); + //set metatable of pointer to base script + lua_rawgeti(lua_state, LUA_REGISTRYINDEX, pgroup->ref_handle); + lua_getglobal(lua_state, "Group"); + lua_setmetatable(lua_state, -2); + lua_pop(lua_state, 1); +} +void interpreter::unregister_group(group *pgroup) { + if (!pgroup) + return; + luaL_unref(lua_state, LUA_REGISTRYINDEX, pgroup->ref_handle); + pgroup->ref_handle = 0; +} +int32_t interpreter::is_effect_check(lua_State* L, effect* peffect, int32_t findex, int32_t extraargs) { + if (!findex || lua_isnil(L, findex)) + return TRUE; + luaL_checkstack(L, 1 + extraargs, nullptr); + lua_pushvalue(L, findex); + interpreter::effect2value(L, peffect); + for (int32_t i = 0; i < extraargs; ++i) + lua_pushvalue(L, (int32_t)(-extraargs - 2)); + if (lua_pcall(L, 1 + extraargs, 1, 0)) { + sprintf(pduel->strbuffer, "%s", lua_tostring(L, -1)); + handle_message(pduel, 1); + lua_pop(L, 1); + return OPERATION_FAIL; + } + int32_t result = lua_toboolean(L, -1); + lua_pop(L, 1); + return result; +} +int32_t interpreter::load_script(const char* script_name) { + int len = 0; + byte* buffer = ::read_script(script_name, &len); + if (!buffer) + return OPERATION_FAIL; + ++no_action; + luaL_checkstack(current_state, 2, nullptr); + int32_t error = 0; + if (enable_unsafe_feature && true) + error = luaL_loadbuffer(current_state, (const char*)buffer, len, script_name) || lua_pcall(current_state, 0, 0, 0); + else + error = luaL_loadbufferx(current_state, (const char*)buffer, len, script_name, "t") || lua_pcall(current_state, 0, 0, 0); + if (error) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1)); + handle_message(pduel, 1); + lua_pop(current_state, 1); + --no_action; + return OPERATION_FAIL; + } + --no_action; + return OPERATION_SUCCESS; +} +//push table cxxx onto the stack of current_state +int32_t interpreter::load_card_script(uint32_t code) { + char class_name[20]; + interpreter::sprintf(class_name, "c%d", code); + luaL_checkstack(current_state, 1, nullptr); + lua_getglobal(current_state, class_name); //+1 table cxxx + if (lua_isnil(current_state, -1)) { + luaL_checkstack(current_state, 5, nullptr); + lua_pop(current_state, 1); //-1 + //create a table & set metatable + lua_createtable(current_state, 0, 0); //+1, {} + lua_setglobal(current_state, class_name); //-1 + lua_getglobal(current_state, class_name); //+1 table cxxx + lua_getglobal(current_state, "Card"); //+1 Card, table cxxx + lua_setmetatable(current_state, -2); //-1 table cxxx + lua_pushstring(current_state, "__index"); //+1 "__index", table cxxx + lua_pushvalue(current_state, -2); //+1 table cxxx, "__index", table cxxx + lua_rawset(current_state, -3); //-2 table cxxx + card_data cdata; + int32_t res = OPERATION_SUCCESS; + ::read_card(code, &cdata); + if (is_load_script(cdata)) { + lua_getglobal(current_state, class_name); //+1 + lua_setglobal(current_state, "self_table"); //-1 + lua_pushinteger(current_state, code); //+1 + lua_setglobal(current_state, "self_code"); //-1 + char script_name[64]; + interpreter::sprintf(script_name, "./script/c%d.lua", code); + res = load_script(script_name); + lua_pushnil(current_state); //+1 + lua_setglobal(current_state, "self_table"); //-1 + lua_pushnil(current_state); //+1 + lua_setglobal(current_state, "self_code"); //-1 table cxxx {__index: cxxx } + } + if(!res) { + return OPERATION_FAIL; + } + } + return OPERATION_SUCCESS; +} +void interpreter::add_param(void* param, LuaParamType type, bool front) { + lua_param p; + p.ptr = param; + if(front) + params.emplace_front(p, type); + else + params.emplace_back(p, type); +} +void interpreter::add_param(int32_t param, LuaParamType type, bool front) { + lua_param p; + p.integer = param; + if(front) + params.emplace_front(p, type); + else + params.emplace_back(p, type); +} +void interpreter::push_param(lua_State* L, bool is_coroutine) { + int32_t pushed = 0; + for (const auto& it : params) { + luaL_checkstack(L, 1, nullptr); + auto type = it.second; + switch(type) { + case PARAM_TYPE_INT: + lua_pushinteger(L, it.first.integer); + break; + case PARAM_TYPE_STRING: + lua_pushstring(L, (const char*)it.first.ptr); + break; + case PARAM_TYPE_BOOLEAN: + lua_pushboolean(L, it.first.integer); + break; + case PARAM_TYPE_CARD: { + if (it.first.ptr) + lua_rawgeti(L, LUA_REGISTRYINDEX, ((card*)it.first.ptr)->ref_handle); + else + lua_pushnil(L); + break; + } + case PARAM_TYPE_EFFECT: { + if (it.first.ptr) + lua_rawgeti(L, LUA_REGISTRYINDEX, ((effect*)it.first.ptr)->ref_handle); + else + lua_pushnil(L); + break; + } + case PARAM_TYPE_GROUP: { + if (it.first.ptr) + lua_rawgeti(L, LUA_REGISTRYINDEX, ((group*)it.first.ptr)->ref_handle); + else + lua_pushnil(L); + break; + } + case PARAM_TYPE_FUNCTION: { + function2value(L, it.first.integer); + break; + } + case PARAM_TYPE_INDEX: { + int32_t index = it.first.integer; + if(index > 0) + lua_pushvalue(L, index); + else if(is_coroutine) { + //copy value from current_state to new stack + lua_pushvalue(current_state, index); + lua_xmove(current_state, L, 1); + } else { + //the calling function is pushed before the params, so the actual index is: index - pushed -1 + lua_pushvalue(L, index - pushed - 1); + } + break; + } + } + ++pushed; + } + params.clear(); +} +int32_t interpreter::call_function(int32_t f, uint32_t param_count, int32_t ret_count) { + if (!f) { + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallFunction\": attempt to call a null function."); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + if (param_count != params.size()) { + interpreter::sprintf(pduel->strbuffer, "\"CallFunction\": incorrect parameter count (%d expected, %zu pushed)", param_count, params.size()); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + function2value(current_state, f); + if (!lua_isfunction(current_state, -1)) { + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallFunction\": attempt to call an error function"); + handle_message(pduel, 1); + lua_pop(current_state, 1); + params.clear(); + return OPERATION_FAIL; + } + ++no_action; + ++call_depth; + push_param(current_state); + if (lua_pcall(current_state, param_count, ret_count, 0)) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1)); + handle_message(pduel, 1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_SUCCESS; +} +int32_t interpreter::call_card_function(card* pcard, const char* f, uint32_t param_count, int32_t ret_count) { + if (param_count != params.size()) { + interpreter::sprintf(pduel->strbuffer, "\"CallCardFunction\"(c%d.%s): incorrect parameter count", pcard->data.get_original_code(), f); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + card2value(current_state, pcard); + luaL_checkstack(current_state, 1, nullptr); + lua_getfield(current_state, -1, f); + if (!lua_isfunction(current_state, -1)) { + interpreter::sprintf(pduel->strbuffer, "\"CallCardFunction\"(c%d.%s): attempt to call an error function", pcard->data.get_original_code(), f); + handle_message(pduel, 1); + lua_pop(current_state, 2); + params.clear(); + return OPERATION_FAIL; + } + ++no_action; + ++call_depth; + lua_remove(current_state, -2); + push_param(current_state); + if (lua_pcall(current_state, param_count, ret_count, 0)) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1)); + handle_message(pduel, 1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_SUCCESS; +} +int32_t interpreter::call_code_function(uint32_t code, const char* f, uint32_t param_count, int32_t ret_count) { + if (param_count != params.size()) { + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallCodeFunction\": incorrect parameter count"); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + if (code) + load_card_script(code); + else + lua_getglobal(current_state, "Auxiliary"); + luaL_checkstack(current_state, 1, nullptr); + lua_getfield(current_state, -1, f); + if (!lua_isfunction(current_state, -1)) { + if(!code) { + params.clear(); + return OPERATION_FAIL; + } + interpreter::sprintf(pduel->strbuffer, "\"CallCodeFunction\": attempt to call an error function"); + handle_message(pduel, 1); + lua_pop(current_state, 2); + params.clear(); + return OPERATION_FAIL; + } + lua_remove(current_state, -2); + ++no_action; + ++call_depth; + push_param(current_state); + if (lua_pcall(current_state, param_count, ret_count, 0)) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1)); + handle_message(pduel, 1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_SUCCESS; +} +int32_t interpreter::check_condition(int32_t f, uint32_t param_count) { + if(!f) { + params.clear(); + return TRUE; + } + ++no_action; + ++call_depth; + if (call_function(f, param_count, 1)) { + int32_t result = lua_toboolean(current_state, -1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return result; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; +} +int32_t interpreter::check_filter(lua_State* L, card* pcard, int32_t findex, int32_t extraargs) { + if (!findex || lua_isnil(L, findex)) + return TRUE; + ++no_action; + ++call_depth; + luaL_checkstack(L, 1 + extraargs, nullptr); + lua_pushvalue(L, findex); + card2value(L, pcard); + for (int32_t i = 0; i < extraargs; ++i) + lua_pushvalue(L, (int32_t)(-extraargs - 2)); + if (lua_pcall(L, 1 + extraargs, 1, 0)) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(L, -1)); + handle_message(pduel, 1); + lua_pop(L, 1); + --no_action; + --call_depth; + if (call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + int32_t result = lua_toboolean(L, -1); + lua_pop(L, 1); + --no_action; + --call_depth; + if (call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return result; +} +int32_t interpreter::get_operation_value(card* pcard, int32_t findex, int32_t extraargs) { + if(!findex || lua_isnil(current_state, findex)) + return 0; + ++no_action; + ++call_depth; + luaL_checkstack(current_state, 1 + extraargs, nullptr); + lua_pushvalue(current_state, findex); + interpreter::card2value(current_state, pcard); + for(int32_t i = 0; i < extraargs; ++i) + lua_pushvalue(current_state, (int32_t)(-extraargs - 2)); + if (lua_pcall(current_state, 1 + extraargs, 1, 0)) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1)); + handle_message(pduel, 1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + int32_t result = lua_isinteger(current_state, -1) ? (int32_t)lua_tointeger(current_state, -1) : (int32_t)lua_tonumber(current_state, -1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return result; +} +int32_t interpreter::get_function_value(int32_t f, uint32_t param_count) { + if(!f) { + params.clear(); + return 0; + } + ++no_action; + ++call_depth; + if (call_function(f, param_count, 1)) { + int32_t result = 0; + if(lua_isboolean(current_state, -1)) + result = lua_toboolean(current_state, -1); + else if(lua_isinteger(current_state, -1)) + result = (int32_t)lua_tointeger(current_state, -1); + else + result = (int32_t)lua_tonumber(current_state, -1); + lua_pop(current_state, 1); + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return result; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; +} +int32_t interpreter::get_function_value(int32_t f, uint32_t param_count, std::vector& result) { + int32_t is_success = OPERATION_FAIL; + if(!f) { + params.clear(); + return is_success; + } + int32_t stack_top = lua_gettop(current_state); + ++no_action; + ++call_depth; + if (call_function(f, param_count, LUA_MULTRET)) { + int32_t stack_newtop = lua_gettop(current_state); + for (int32_t index = stack_top + 1; index <= stack_newtop; ++index) { + lua_Integer return_value = 0; + if(lua_isboolean(current_state, index)) + return_value = lua_toboolean(current_state, index); + else if(lua_isinteger(current_state, index)) + return_value = lua_tointeger(current_state, index); + else + return_value = static_cast(lua_tonumber(current_state, index)); + result.push_back(return_value); + } + lua_settop(current_state, stack_top); + is_success = OPERATION_SUCCESS; + } + --no_action; + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return is_success; +} +int32_t interpreter::call_coroutine(int32_t f, uint32_t param_count, int32_t* yield_value, uint16_t step) { + *yield_value = 0; + if (!f) { + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallCoroutine\": attempt to call a null function"); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + if (param_count != params.size()) { + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallCoroutine\": incorrect parameter count"); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + auto it = coroutines.find(f); + lua_State* rthread; + if (it == coroutines.end()) { + rthread = lua_newthread(lua_state); + std::memcpy(lua_getextraspace(rthread), lua_getextraspace(lua_state), LUA_EXTRASPACE); + lua_coverage.install_hook(rthread); + const auto threadref = luaL_ref(lua_state, LUA_REGISTRYINDEX); + function2value(rthread, f); + if(!lua_isfunction(rthread, -1)) { + luaL_unref(lua_state, LUA_REGISTRYINDEX, threadref); + interpreter::sprintf(pduel->strbuffer, "%s", "\"CallCoroutine\": attempt to call an error function"); + handle_message(pduel, 1); + params.clear(); + return OPERATION_FAIL; + } + ++call_depth; + auto ret = coroutines.emplace(f, coroutine_map::mapped_type(rthread, threadref)); + it = ret.first; + } else { + if(step == 0) { + auto threadref = it->second.second; + coroutines.erase(it); + luaL_unref(lua_state, LUA_REGISTRYINDEX, threadref); + interpreter::sprintf(pduel->strbuffer, "%s", "recursive event trigger detected"); + handle_message(pduel, 1); + params.clear(); + --call_depth; + if(call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return OPERATION_FAIL; + } + rthread = it->second.first; + } + push_param(rthread, true); + int32_t result = 0, nresults = 0; + { + auto prev_state = current_state; + current_state = rthread; +#if (LUA_VERSION_NUM >= 504) + result = lua_resume(rthread, prev_state, param_count, &nresults); +#else + result = lua_resume(rthread, prev_state, param_count); + nresults = lua_gettop(rthread); +#endif + current_state = prev_state; + } + if (result == LUA_YIELD) + return COROUTINE_YIELD; + if (result != LUA_OK) { + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(rthread, -1)); + handle_message(pduel, 1); + lua_pop(rthread, 1); + } + else if (yield_value) { + if (nresults == 0) + *yield_value = 0; + else if (lua_isboolean(rthread, -1)) + *yield_value = lua_toboolean(rthread, -1); + else + *yield_value = (int32_t)lua_tointeger(rthread, -1); + } + auto threadref = it->second.second; + coroutines.erase(it); + luaL_unref(lua_state, LUA_REGISTRYINDEX, threadref); + --call_depth; + if (call_depth == 0) { + pduel->release_script_group(); + pduel->restore_assumes(); + } + return (result == LUA_OK) ? COROUTINE_FINISH : COROUTINE_ERROR; +} +int32_t interpreter::clone_function_ref(int32_t func_ref) { + luaL_checkstack(current_state, 1, nullptr); + lua_rawgeti(current_state, LUA_REGISTRYINDEX, func_ref); + int32_t ref = luaL_ref(current_state, LUA_REGISTRYINDEX); + return ref; +} +void* interpreter::get_ref_object(int32_t ref_handler) { + if(ref_handler == 0) + return nullptr; + luaL_checkstack(current_state, 1, nullptr); + lua_rawgeti(current_state, LUA_REGISTRYINDEX, ref_handler); + void* p = *(void**)lua_touserdata(current_state, -1); + lua_pop(current_state, 1); + return p; +} +//push the object onto the stack of L, +1 +void interpreter::card2value(lua_State* L, card* pcard) { + luaL_checkstack(L, 1, nullptr); + if (!pcard || pcard->ref_handle == 0) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, pcard->ref_handle); +} +void interpreter::group2value(lua_State* L, group* pgroup) { + luaL_checkstack(L, 1, nullptr); + if (!pgroup || pgroup->ref_handle == 0) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, pgroup->ref_handle); +} +void interpreter::effect2value(lua_State* L, effect* peffect) { + luaL_checkstack(L, 1, nullptr); + if (!peffect || peffect->ref_handle == 0) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, peffect->ref_handle); +} +void interpreter::function2value(lua_State* L, int32_t func_ref) { + luaL_checkstack(L, 1, nullptr); + if (!func_ref) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref); +} +int32_t interpreter::get_function_handle(lua_State* L, int32_t index) { + luaL_checkstack(L, 1, nullptr); + lua_pushvalue(L, index); + int32_t ref = luaL_ref(L, LUA_REGISTRYINDEX); + return ref; +} +duel* interpreter::get_duel_info(lua_State* L) { + duel* pduel; + std::memcpy(&pduel, lua_getextraspace(L), LUA_EXTRASPACE); + return pduel; +} +bool interpreter::is_load_script(const card_data& data) { + if(data.code == TEMP_CARD_ID) + return false; + return !(data.type & TYPE_NORMAL) || (data.type & TYPE_PENDULUM); +} diff --git a/core/interpreter.h b/core/interpreter.h new file mode 100644 index 0000000..b891d70 --- /dev/null +++ b/core/interpreter.h @@ -0,0 +1,102 @@ +/* + * interpreter.h + * + * Created on: 2010-4-28 + * Author: Argon + */ + +#ifndef INTERPRETER_H_ +#define INTERPRETER_H_ + +#include +#include +#include +#include "common.h" +#include +#include +#include +#include +#include "LuaMemTracker.h" +#include "lua_coverage.h" + +class card; +struct card_data; +class effect; +class group; +class duel; + +class interpreter { +public: + union lua_param { + void* ptr; + int32_t integer; + }; + using coroutine_map = std::unordered_map>; + using param_list = std::list>; + + duel* pduel; + char msgbuf[64]{}; + lua_State* lua_state; + lua_State* current_state; + LuaMemTracker* mem_tracker; + param_list params; + param_list resumes; + coroutine_map coroutines; + int32_t no_action{}; + int32_t call_depth{}; + bool enable_unsafe_feature{}; + int32_t disable_action_check{}; + int32_t preloaded{}; + lua_coverage_state lua_coverage; + + explicit interpreter(duel* pd, bool enable_unsafe_libraries, uint32_t create_flags = 0); + ~interpreter(); + + void register_card(card* pcard); + void unregister_card(card* pcard); + void register_effect(effect* peffect); + void unregister_effect(effect* peffect); + void register_group(group* pgroup); + void unregister_group(group* pgroup); + + int32_t is_effect_check(lua_State *L, effect *peffect, int32_t findex, int32_t extraargs); + int32_t load_script(const char* script_name); + int32_t load_card_script(uint32_t code); + void add_param(void* param, LuaParamType type, bool front = false); + void add_param(int32_t param, LuaParamType type, bool front = false); + void push_param(lua_State* L, bool is_coroutine = false); + int32_t call_function(int32_t f, uint32_t param_count, int32_t ret_count); + int32_t call_card_function(card* pcard, const char* f, uint32_t param_count, int32_t ret_count); + int32_t call_code_function(uint32_t code, const char* f, uint32_t param_count, int32_t ret_count); + int32_t check_condition(int32_t f, uint32_t param_count); + int32_t check_filter(lua_State* L, card* pcard, int32_t findex, int32_t extraargs); + int32_t get_operation_value(card* pcard, int32_t findex, int32_t extraargs); + int32_t get_function_value(int32_t f, uint32_t param_count); + int32_t get_function_value(int32_t f, uint32_t param_count, std::vector& result); + int32_t call_coroutine(int32_t f, uint32_t param_count, int32_t* yield_value, uint16_t step); + int32_t clone_function_ref(int32_t func_ref); + void* get_ref_object(int32_t ref_handler); + + static void card2value(lua_State* L, card* pcard); + static void group2value(lua_State* L, group* pgroup); + static void effect2value(lua_State* L, effect* peffect); + static void function2value(lua_State* L, int32_t func_ref); + static int32_t get_function_handle(lua_State* L, int32_t index); + static duel* get_duel_info(lua_State* L); + static bool is_load_script(const card_data& data); + + template + static int sprintf(char (&buffer)[N], const char* format, TR... args) { + return std::snprintf(buffer, N, format, args...); + } +}; + +#define COROUTINE_FINISH 1 +#define COROUTINE_YIELD 2 +#define COROUTINE_ERROR 3 + +#ifndef YGOPRO_LUA_MEMORY_SIZE +#define YGOPRO_LUA_MEMORY_SIZE 0 // disable memory limit by default +#endif + +#endif /* INTERPRETER_H_ */ diff --git a/core/libcard.cpp b/core/libcard.cpp new file mode 100644 index 0000000..027b04a --- /dev/null +++ b/core/libcard.cpp @@ -0,0 +1,3946 @@ +/* + * libcard.cpp + * + * Created on: 2010-5-6 + * Author: Argon + */ + +#include "scriptlib.h" +#include "duel.h" +#include "field.h" +#include "card.h" +#include "effect.h" +#include "group.h" + +int32_t scriptlib::card_get_card_registered(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + if(!lua_isnil(L, 2)) + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + int type = GETEFFECT_ALL; + if (!pcard) { + lua_pushnil(L); + return 1; + } + if (lua_gettop(L) > 2) { + check_param(L, PARAM_TYPE_INT, 3); + type = lua_tointeger(L, 3); + } + effect_set eset; + duel* pduel = pcard->pduel; + int32_t extraargs = lua_gettop(L) - 3; + for(auto& eit : pcard->single_effect) { + if(pduel->lua->is_effect_check(L, eit.second, 2, extraargs)) { + eset.push_back(eit.second); + } + } + for(auto& eit : pcard->field_effect) { + if(pduel->lua->is_effect_check(L, eit.second, 2, extraargs)) { + eset.push_back(eit.second); + } + } + int size = 0; + for (int32_t i = 0; i < eset.size(); ++i) { + if (eset[i]->is_granted) { + if (type & GETEFFECT_GRANT) { + interpreter::effect2value(L, eset[i]); + ++size; + } + } else if ((type & GETEFFECT_COPY && eset[i]->copy_id != 0) + || (type & GETEFFECT_INITIAL && eset[i]->reset_flag == 0 && eset[i]->copy_id == 0) + || (type & GETEFFECT_GAIN && eset[i]->reset_flag != 0 && eset[i]->copy_id == 0)) { + interpreter::effect2value(L, eset[i]); + ++size; + } + } + if (!size) { + lua_pushnil(L); + return 1; + } + return size; +} + +int32_t scriptlib::card_is_ritual_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = lua_tointeger(L, 2); + if(pcard->get_ritual_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_set_entity_code(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t trace = lua_tointeger(L, 2); + bool remove_alias = false; + int32_t enable = lua_toboolean(L, 3); + if (enable) + remove_alias = true; + lua_pushinteger(L, pcard->set_entity_code(trace, remove_alias)); + return 1; +} +int32_t scriptlib::card_set_card_data(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + int32_t stype = lua_tointeger(L, 2); + switch(stype) { + case CARDDATA_CODE: + pcard->data.code = lua_tointeger(L, 3); + break; + case CARDDATA_ALIAS: + pcard->data.alias = lua_tointeger(L, 3); + break; + case CARDDATA_SETCODE: { + unsigned long setcode = lua_tointeger(L, 3); + for (int i = 0 ;; ++i) + { + if(setcode == 0) + break; + pcard->data.setcode[i] = setcode & 0xffff; + setcode >>= 16; + } + break; + } + case CARDDATA_TYPE: + pcard->data.type = lua_tointeger(L, 3); + break; + case CARDDATA_LEVEL: + pcard->data.level = lua_tointeger(L, 3); + break; + case CARDDATA_ATTRIBUTE: + pcard->data.attribute = lua_tointeger(L, 3); + break; + case CARDDATA_RACE: + pcard->data.race = lua_tointeger(L, 3); + break; + case CARDDATA_ATTACK: + pcard->data.attack = lua_tointeger(L, 3); + break; + case CARDDATA_DEFENSE: + pcard->data.defense = lua_tointeger(L, 3); + break; + case CARDDATA_LSCALE: + pcard->data.lscale = lua_tointeger(L, 3); + break; + case CARDDATA_RSCALE: + pcard->data.rscale = lua_tointeger(L, 3); + break; + case CARDDATA_LINK_MARKER: + pcard->data.link_marker = lua_tointeger(L, 3); + break; + } + pduel->write_buffer8(MSG_UPDATE_CARD); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + unsigned char query_buffer[0x1000]; + auto query_len = pcard->get_infos(query_buffer, 0xe81fff, 0); + pduel->write_buffer(query_buffer, query_len); + return 0; +} +int32_t scriptlib::card_get_link_marker(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_link_marker()); + return 1; +} +int32_t scriptlib::card_get_origin_link_marker(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_NO_LEVEL) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.link_marker); + return 1; +} +int32_t scriptlib::card_is_xyz_summonable_by_rose(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + check_param(L, PARAM_TYPE_CARD, 3); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_XYZ)) + return 0; + card* rcard = *(card**) lua_touserdata(L, 2); + card* mcard = *(card**) lua_touserdata(L, 3); + group* materials = pcard->pduel->new_group(rcard); + materials->container.insert(mcard); + uint32_t p = pcard->pduel->game_field->core.reason_player; + pcard->pduel->game_field->core.limit_xyz = materials; + pcard->pduel->game_field->core.limit_xyz_minc = 2; + pcard->pduel->game_field->core.limit_xyz_maxc = 2; + pcard->pduel->game_field->rose_card = rcard; + pcard->pduel->game_field->rose_level = mcard->get_level(); + int32_t result = pcard->is_special_summonable(p, SUMMON_TYPE_XYZ); + pcard->pduel->game_field->rose_card = 0; + pcard->pduel->game_field->rose_level = 0; + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_get_removed_overlay_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->removed_overlay_count); + return 1; +} +int32_t scriptlib::card_check_spsummon_once(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + auto playerid = (int32_t)lua_tointeger(L, 2); + auto pduel = pcard->pduel; + lua_pushboolean(L, pduel->game_field->check_spsummon_once(pcard, playerid)); + return 1; +} + +int32_t scriptlib::card_get_code(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_code()); + uint32_t otcode = pcard->get_another_code(); + if(otcode) { + lua_pushinteger(L, otcode); + return 2; + } + return 1; +} +int32_t scriptlib::card_get_origin_code(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_original_code()); + return 1; +} +int32_t scriptlib::card_get_origin_code_rule(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + auto codes = pcard->get_original_code_rule(); + uint32_t code1 = std::get<0>(codes); + uint32_t code2 = std::get<1>(codes); + if (code2) { + lua_pushinteger(L, code1); + lua_pushinteger(L, code2); + return 2; + } + else { + lua_pushinteger(L, code1); + return 1; + } +} +int32_t scriptlib::card_get_fusion_code(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_code()); + int32_t count = 1; + uint32_t otcode = pcard->get_another_code(); + if(otcode) { + lua_pushinteger(L, otcode); + ++count; + } + if(pcard->pduel->game_field->core.not_material) + return count; + effect_set eset; + pcard->filter_effect(EFFECT_ADD_FUSION_CODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + lua_pushinteger(L, eset[i]->get_value(pcard)); + return count + (int32_t)eset.size(); +} +int32_t scriptlib::card_get_link_code(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_code()); + int32_t count = 1; + uint32_t otcode = pcard->get_another_code(); + if(otcode) { + lua_pushinteger(L, otcode); + ++count; + } + effect_set eset; + pcard->filter_effect(EFFECT_ADD_LINK_CODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + lua_pushinteger(L, eset[i]->get_value(pcard)); + return count + (int32_t)eset.size(); +} +int32_t scriptlib::card_is_fusion_code(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + effect_set eset; + pcard->filter_effect(EFFECT_ADD_FUSION_CODE, &eset); + if(!eset.size() || pcard->pduel->game_field->core.not_material) + return card_is_code(L); + uint32_t code1 = pcard->get_code(); + uint32_t code2 = pcard->get_another_code(); + std::unordered_set fcode; + fcode.insert(code1); + if(code2) + fcode.insert(code2); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + fcode.insert(eset[i]->get_value(pcard)); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tcode = (uint32_t)lua_tointeger(L, i + 2); + if(fcode.find(tcode) != fcode.end()) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_link_code(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + effect_set eset; + pcard->filter_effect(EFFECT_ADD_LINK_CODE, &eset); + if(!eset.size()) + return card_is_code(L); + uint32_t code1 = pcard->get_code(); + uint32_t code2 = pcard->get_another_code(); + std::unordered_set fcode; + fcode.insert(code1); + if(code2) + fcode.insert(code2); + for(effect_set::size_type i = 0; i < eset.size(); ++i) + fcode.insert(eset[i]->get_value(pcard)); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tcode = (uint32_t)lua_tointeger(L, i + 2); + if(fcode.find(tcode) != fcode.end()) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_origin_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_origin_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_pre_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_pre_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_fusion_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_fusion_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_link_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_link_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_special_summon_set_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t set_code = (uint32_t)lua_tointeger(L, i + 2); + if(pcard->is_special_summon_set_card(set_code)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_get_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_type()); + return 1; +} +int32_t scriptlib::card_get_origin_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->data.type); + return 1; +} +int32_t scriptlib::card_get_fusion_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_fusion_type()); + return 1; +} +int32_t scriptlib::card_get_synchro_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_synchro_type()); + return 1; +} +int32_t scriptlib::card_get_xyz_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_xyz_type()); + return 1; +} +int32_t scriptlib::card_get_link_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_link_type()); + return 1; +} +int32_t scriptlib::card_get_level(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_level()); + return 1; +} +int32_t scriptlib::card_get_rank(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_rank()); + return 1; +} +int32_t scriptlib::card_get_link(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_link()); + return 1; +} +int32_t scriptlib::card_get_synchro_level(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = *(card**) lua_touserdata(L, 2); + lua_pushinteger(L, pcard->get_synchro_level(scard)); + return 1; +} +int32_t scriptlib::card_get_ritual_level(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = *(card**) lua_touserdata(L, 2); + lua_pushinteger(L, pcard->get_ritual_level(scard)); + return 1; +} +int32_t scriptlib::card_get_origin_level(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if((pcard->data.type & (TYPE_XYZ | TYPE_LINK)) || (pcard->status & STATUS_NO_LEVEL)) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.level); + return 1; +} +int32_t scriptlib::card_get_origin_rank(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_XYZ)) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.level); + return 1; +} +int32_t scriptlib::card_is_xyz_level(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* xyzcard = *(card**) lua_touserdata(L, 2); + uint32_t lv = (uint32_t)lua_tointeger(L, 3); + lua_pushboolean(L, pcard->check_xyz_level(xyzcard, lv)); + return 1; +} +int32_t scriptlib::card_get_lscale(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_lscale()); + return 1; +} +int32_t scriptlib::card_get_origin_lscale(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->data.lscale); + return 1; +} +int32_t scriptlib::card_get_rscale(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_rscale()); + return 1; +} +int32_t scriptlib::card_get_origin_rscale(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->data.rscale); + return 1; +} +int32_t scriptlib::card_get_current_scale(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + if (pcard->current.pzone) { + if (pcard->current.sequence == pcard->pduel->game_field->get_pzone_sequence(0)) + lua_pushinteger(L, pcard->get_lscale()); + else + lua_pushinteger(L, pcard->get_rscale()); + } + else + lua_pushinteger(L, pcard->data.lscale); + return 1; +} +int32_t scriptlib::card_is_link_marker(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t dir = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->is_link_marker(dir)); + return 1; +} +int32_t scriptlib::card_get_linked_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card_set cset; + pcard->get_linked_cards(&cset); + group* pgroup = pcard->pduel->new_group(cset); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_linked_group_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card_set cset; + pcard->get_linked_cards(&cset); + lua_pushinteger(L, cset.size()); + return 1; +} +int32_t scriptlib::card_get_linked_zone(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t zone = pcard->get_linked_zone(); + int32_t cp = pcard->current.controler; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) + cp = (int32_t)lua_tointeger(L, 2); + if(cp == 1 - pcard->current.controler) + lua_pushinteger(L, (((zone & 0xffff) << 16) | (zone >> 16))); + else + lua_pushinteger(L, zone); + return 1; +} +int32_t scriptlib::card_get_mutual_linked_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + card_set cset; + pcard->get_mutual_linked_cards(&cset); + group* pgroup = pcard->pduel->new_group(cset); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_mutual_linked_group_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + card_set cset; + pcard->get_mutual_linked_cards(&cset); + lua_pushinteger(L, cset.size()); + return 1; +} +int32_t scriptlib::card_get_mutual_linked_zone(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t zone = pcard->get_mutual_linked_zone(); + int32_t cp = pcard->current.controler; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) + cp = (int32_t)lua_tointeger(L, 2); + if(cp == 1 - pcard->current.controler) + lua_pushinteger(L, (((zone & 0xffff) << 16) | (zone >> 16))); + else + lua_pushinteger(L, zone); + return 1; +} +int32_t scriptlib::card_is_link_state(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_link_state()); + return 1; +} +int32_t scriptlib::card_is_extra_link_state(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_extra_link_state()); + return 1; +} +int32_t scriptlib::card_get_column_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card_set cset; + pcard->get_column_cards(&cset); + group* pgroup = pcard->pduel->new_group(cset); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_column_group_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card_set cset; + pcard->get_column_cards(&cset); + lua_pushinteger(L, cset.size()); + return 1; +} +int32_t scriptlib::card_get_column_zone(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t loc = (int32_t)lua_tointeger(L, 2); + int32_t cp = pcard->current.controler; + if(lua_gettop(L) >= 3 && !lua_isnil(L, 3)) + cp = (int32_t)lua_tointeger(L, 3); + uint32_t zone = pcard->get_column_zone(loc); + if(cp == 1 - pcard->current.controler) + lua_pushinteger(L, (((zone & 0xffff) << 16) | (zone >> 16))); + else + lua_pushinteger(L, zone); + return 1; +} +int32_t scriptlib::card_is_all_column(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_all_column()); + return 1; +} +int32_t scriptlib::card_get_attribute(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_attribute()); + return 1; +} +int32_t scriptlib::card_get_origin_attribute(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_NO_LEVEL) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.attribute); + return 1; +} +int32_t scriptlib::card_get_fusion_attribute(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (int32_t)lua_tointeger(L, 2); + else + playerid = pcard->pduel->game_field->core.reason_player; + lua_pushinteger(L, pcard->get_fusion_attribute(playerid)); + return 1; +} +int32_t scriptlib::card_get_link_attribute(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (int32_t)lua_tointeger(L, 2); + else + playerid = pcard->pduel->game_field->core.reason_player; + lua_pushinteger(L, pcard->get_link_attribute(playerid)); + return 1; +} +int32_t scriptlib::card_get_attribute_in_grave(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (int32_t)lua_tointeger(L, 2); + else + playerid = pcard->pduel->game_field->core.reason_player; + lua_pushinteger(L, pcard->get_grave_attribute(playerid)); + return 1; +} +int32_t scriptlib::card_get_race(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_race()); + return 1; +} +int32_t scriptlib::card_get_origin_race(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_NO_LEVEL) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.race); + return 1; +} +int32_t scriptlib::card_get_link_race(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (int32_t)lua_tointeger(L, 2); + else + playerid = pcard->pduel->game_field->core.reason_player; + lua_pushinteger(L, pcard->get_link_race(playerid)); + return 1; +} +int32_t scriptlib::card_get_race_in_grave(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (int32_t)lua_tointeger(L, 2); + else + playerid = pcard->pduel->game_field->core.reason_player; + lua_pushinteger(L, pcard->get_grave_race(playerid)); + return 1; +} +int32_t scriptlib::card_get_attack(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t atk = pcard->get_attack(); + if(atk < 0) + atk = 0; + lua_pushinteger(L, atk); + return 1; +} +int32_t scriptlib::card_get_origin_attack(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t atk = pcard->get_base_attack(); + if(atk < 0) + atk = 0; + lua_pushinteger(L, atk); + return 1; +} +int32_t scriptlib::card_get_text_attack(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_NO_LEVEL) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.attack); + return 1; +} +int32_t scriptlib::card_get_defense(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t def = pcard->get_defense(); + if(def < 0) + def = 0; + lua_pushinteger(L, def); + return 1; +} +int32_t scriptlib::card_get_origin_defense(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t def = pcard->get_base_defense(); + if(def < 0) + def = 0; + lua_pushinteger(L, def); + return 1; +} +int32_t scriptlib::card_get_text_defense(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_NO_LEVEL) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->data.defense); + return 1; +} +int32_t scriptlib::card_get_previous_code_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.code); + if(pcard->previous.code2) { + lua_pushinteger(L, pcard->previous.code2); + return 2; + } + return 1; +} +int32_t scriptlib::card_get_previous_type_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.type); + return 1; +} +int32_t scriptlib::card_get_previous_level_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.level); + return 1; +} +int32_t scriptlib::card_get_previous_rank_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.rank); + return 1; +} +int32_t scriptlib::card_get_previous_attribute_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.attribute); + return 1; +} +int32_t scriptlib::card_get_previous_race_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.race); + return 1; +} +int32_t scriptlib::card_get_previous_attack_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.attack); + return 1; +} +int32_t scriptlib::card_get_previous_defense_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.defense); + return 1; +} +int32_t scriptlib::card_get_previous_overlay_count_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->xyz_materials_previous_count_onfield); + return 1; +} +int32_t scriptlib::card_get_owner(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->owner); + return 1; +} +int32_t scriptlib::card_get_controler(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->current.controler); + return 1; +} +int32_t scriptlib::card_get_previous_controler(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.controler); + return 1; +} +int32_t scriptlib::card_set_reason(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t reason = (uint32_t)lua_tointeger(L, 2); + pcard->current.reason = reason; + return 0; +} +int32_t scriptlib::card_get_reason(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->current.reason); + return 1; +} +int32_t scriptlib::card_get_reason_card(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + interpreter::card2value(L, pcard->current.reason_card); + return 1; +} +int32_t scriptlib::card_get_reason_player(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->current.reason_player); + return 1; +} +int32_t scriptlib::card_get_reason_effect(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + interpreter::effect2value(L, pcard->current.reason_effect); + return 1; +} +int32_t scriptlib::card_get_position(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->current.position); + return 1; +} +int32_t scriptlib::card_get_previous_position(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.position); + return 1; +} +int32_t scriptlib::card_get_battle_position(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->temp.position); + return 1; +} +int32_t scriptlib::card_get_location(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->is_treated_as_not_on_field()) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, pcard->current.location); + return 1; +} +int32_t scriptlib::card_get_previous_location(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.location); + return 1; +} +int32_t scriptlib::card_get_sequence(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->current.sequence); + return 1; +} +int32_t scriptlib::card_get_previous_sequence(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->previous.sequence); + return 1; +} +int32_t scriptlib::card_get_summon_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_summon_info() & DEFAULT_SUMMON_TYPE); + return 1; +} +int32_t scriptlib::card_get_summon_location(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, (pcard->get_summon_info() >> 16) & 0xff); + return 1; +} +int32_t scriptlib::card_get_summon_player(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->summon_player); + return 1; +} +int32_t scriptlib::card_get_special_summon_info(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t args = lua_gettop(L) - 1; + for(uint32_t i = 0; i < args; ++i) { + uint32_t flag = (uint32_t)lua_tointeger(L, 2 + i); + switch(flag) { + case SUMMON_INFO_CODE: + lua_pushinteger(L, pcard->spsummon.code); + break; + case SUMMON_INFO_CODE2: + lua_pushinteger(L, pcard->spsummon.code2); + break; + case SUMMON_INFO_TYPE: + lua_pushinteger(L, pcard->spsummon.type); + break; + case SUMMON_INFO_LEVEL: + lua_pushinteger(L, pcard->spsummon.level); + break; + case SUMMON_INFO_RANK: + lua_pushinteger(L, pcard->spsummon.rank); + break; + case SUMMON_INFO_ATTRIBUTE: + lua_pushinteger(L, pcard->spsummon.attribute); + break; + case SUMMON_INFO_RACE: + lua_pushinteger(L, pcard->spsummon.race); + break; + case SUMMON_INFO_ATTACK: + lua_pushinteger(L, pcard->spsummon.attack); + break; + case SUMMON_INFO_DEFENSE: + lua_pushinteger(L, pcard->spsummon.defense); + break; + case SUMMON_INFO_REASON_EFFECT: + interpreter::effect2value(L, pcard->spsummon.reason_effect); + break; + case SUMMON_INFO_REASON_PLAYER: + lua_pushinteger(L, pcard->spsummon.reason_player); + break; + default: + lua_pushnil(L); + break; + } + } + return args; +} +int32_t scriptlib::card_get_destination(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->sendto_param.location); + return 1; +} +int32_t scriptlib::card_get_leave_field_dest(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->leave_field_redirect(REASON_EFFECT)); + return 1; +} +int32_t scriptlib::card_get_turnid(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->turnid); + return 1; +} +int32_t scriptlib::card_get_fieldid(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->fieldid); + return 1; +} +int32_t scriptlib::card_get_fieldidr(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->fieldid_r); + return 1; +} +int32_t scriptlib::card_is_origin_code_rule(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + auto codes = pcard->get_original_code_rule(); + uint32_t code1 = std::get<0>(codes); + uint32_t code2 = std::get<1>(codes); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tcode = (uint32_t)lua_tointeger(L, i + 2); + if(code1 == tcode || (code2 && code2 == tcode)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_code(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code1 = pcard->get_code(); + uint32_t code2 = pcard->get_another_code(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tcode = (uint32_t)lua_tointeger(L, i + 2); + if(code1 == tcode || (code2 && code2 == tcode)) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_all_types(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if((pcard->get_type() & ttype) == ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_fusion_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_fusion_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_synchro_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_synchro_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_xyz_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_xyz_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_link_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_link_type() & ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_level(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t level = pcard->get_level(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tlevel = (uint32_t)lua_tointeger(L, i + 2); + if(level == tlevel) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_rank(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t rank = pcard->get_rank(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t trank = (uint32_t)lua_tointeger(L, i + 2); + if(rank == trank) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_link(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t link = pcard->get_link(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tlink = (uint32_t)lua_tointeger(L, i + 2); + if(link == tlink) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::card_is_attack(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER)) + lua_pushboolean(L, 0); + else { + uint32_t atk = pcard->get_attack(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tatk = (uint32_t)lua_tointeger(L, i + 2); + if(atk == tatk) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + } + return 1; +} +int32_t scriptlib::card_is_defense(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if((pcard->data.type & TYPE_LINK) + || (!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER))) + lua_pushboolean(L, 0); + else { + uint32_t def = pcard->get_defense(); + uint32_t count = lua_gettop(L) - 1; + uint32_t result = FALSE; + for(uint32_t i = 0; i < count; ++i) { + if(lua_isnil(L, i + 2)) + continue; + uint32_t tdef = (uint32_t)lua_tointeger(L, i + 2); + if(def == tdef) { + result = TRUE; + break; + } + } + lua_pushboolean(L, result); + } + return 1; +} +int32_t scriptlib::card_is_race(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t trace = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_race() & trace) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_link_race(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t trace = (uint32_t)lua_tointeger(L, 2); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) + playerid = (int32_t)lua_tointeger(L, 3); + else + playerid = pcard->pduel->game_field->core.reason_player; + if(pcard->get_link_race(playerid) & trace) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_attribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t tattrib = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_attribute() & tattrib) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_fusion_attribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t tattrib = (uint32_t)lua_tointeger(L, 2); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) + playerid = (int32_t)lua_tointeger(L, 3); + else + playerid = pcard->pduel->game_field->core.reason_player; + if(pcard->get_fusion_attribute(playerid) & tattrib) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_link_attribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t tattrib = (uint32_t)lua_tointeger(L, 2); + int32_t playerid = PLAYER_NONE; + if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) + playerid = (int32_t)lua_tointeger(L, 3); + else + playerid = pcard->pduel->game_field->core.reason_player; + if(pcard->get_link_attribute(playerid) & tattrib) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_non_attribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t tattrib = (uint32_t)lua_tointeger(L, 2); + if(pcard->get_attribute() & (ATTRIBUTE_ALL & ~tattrib)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_extra_deck_monster(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_extra_deck_monster()); + return 1; +} +int32_t scriptlib::card_is_reason(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t treason = (uint32_t)lua_tointeger(L, 2); + if(pcard->current.reason & treason) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_all_reasons(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t treason = (uint32_t)lua_tointeger(L, 2); + if((pcard->current.reason & treason) == treason) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_summon_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t ttype = (uint32_t)lua_tointeger(L, 2); + if(((pcard->get_summon_info() & DEFAULT_SUMMON_TYPE) & ttype) == ttype) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_summon_location(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t tloc = (uint32_t)lua_tointeger(L, 2); + if(((pcard->summon_info >> 16) & 0xff) & tloc) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_summon_player(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t con = (uint32_t)lua_tointeger(L, 2); + if(pcard->summon_player == con) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_status(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t tstatus = (uint32_t)lua_tointeger(L, 2); + if(pcard->status & tstatus) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_not_tuner(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pcard->is_not_tuner(scard)); + return 1; +} +int32_t scriptlib::card_is_tuner(lua_State* L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**)lua_touserdata(L, 1); + card* scard = *(card**)lua_touserdata(L, 2); + lua_pushboolean(L, pcard->is_tuner(scard)); + return 1; +} +int32_t scriptlib::card_is_original_effect_property(lua_State* L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t filter = interpreter::get_function_handle(L, 2); + lua_pushboolean(L, pcard->is_original_effect_property(filter)); + return 1; +} +int32_t scriptlib::card_is_effect_property(lua_State* L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pcard = *(card**)lua_touserdata(L, 1); + int32_t filter = interpreter::get_function_handle(L, 2); + lua_pushboolean(L, pcard->is_effect_property(filter)); + return 1; +} +int32_t scriptlib::card_set_status(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_COPYING_EFFECT) + return 0; + uint32_t tstatus = (uint32_t)lua_tointeger(L, 2); + int32_t enable = lua_toboolean(L, 3); + pcard->set_status(tstatus, enable); + return 0; +} +int32_t scriptlib::card_is_dual_state(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t ret = 0; + if(pcard->is_affected_by_effect(EFFECT_DUAL_STATUS)) + ret = 1; + else + ret = 0; + lua_pushboolean(L, ret); + return 1; +} +int32_t scriptlib::card_enable_dual_state(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + effect* deffect = pduel->new_effect(); + deffect->owner = pcard; + deffect->code = EFFECT_DUAL_STATUS; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 64; + deffect->reset_flag = RESET_EVENT + 0x1fe0000; + pcard->add_effect(deffect); + return 0; +} +int32_t scriptlib::card_set_turn_counter(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t ct = (int32_t)lua_tointeger(L, 2); + pcard->count_turn(ct); + return 0; +} +int32_t scriptlib::card_get_turn_counter(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->turn_counter); + return 1; +} +int32_t scriptlib::card_set_material(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_GROUP, 2); + group* pgroup = *(group**) lua_touserdata(L, 2); + pcard->set_material(&pgroup->container); + } else + pcard->set_material(0); + return 0; +} +int32_t scriptlib::card_get_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(pcard->material_cards); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_material_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->material_cards.size()); + return 1; +} +int32_t scriptlib::card_get_equip_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(pcard->equiping_cards); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_equip_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->equiping_cards.size()); + return 1; +} +int32_t scriptlib::card_get_equip_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + interpreter::card2value(L, pcard->equiping_target); + return 1; +} +int32_t scriptlib::card_get_pre_equip_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + interpreter::card2value(L, pcard->pre_equip_target); + return 1; +} +int32_t scriptlib::card_check_equip_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* target = *(card**) lua_touserdata(L, 2); + if(pcard->is_affected_by_effect(EFFECT_EQUIP_LIMIT, target) + && ((!pcard->is_affected_by_effect(EFFECT_OLDUNION_STATUS) || target->get_union_count() == 0) + && (!pcard->is_affected_by_effect(EFFECT_UNION_STATUS) || target->get_old_union_count() == 0))) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_check_union_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* target = *(card**) lua_touserdata(L, 2); + if(pcard->is_affected_by_effect(EFFECT_UNION_LIMIT, target) + && ((!pcard->is_affected_by_effect(EFFECT_OLDUNION_STATUS) || target->get_union_count() == 0) + && (!pcard->is_affected_by_effect(EFFECT_UNION_STATUS) || target->get_old_union_count() == 0))) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_get_union_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->get_union_count()); + lua_pushinteger(L, pcard->get_old_union_count()); + return 2; +} +int32_t scriptlib::card_get_overlay_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(); + pgroup->container.insert(pcard->xyz_materials.begin(), pcard->xyz_materials.end()); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_overlay_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->xyz_materials.size()); + return 1; +} +int32_t scriptlib::card_get_overlay_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + interpreter::card2value(L, pcard->overlay_target); + return 1; +} +int32_t scriptlib::card_check_remove_overlay_card(lua_State *L) { + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t count = (int32_t)lua_tointeger(L, 3); + int32_t reason = (int32_t)lua_tointeger(L, 4); + duel* pduel = pcard->pduel; + lua_pushboolean(L, pduel->game_field->is_player_can_remove_overlay_card(playerid, pcard, 0, 0, count, reason)); + return 1; +} +int32_t scriptlib::card_remove_overlay_card(lua_State *L) { + check_action_permission(L); + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t min = (int32_t)lua_tointeger(L, 3); + int32_t max = (int32_t)lua_tointeger(L, 4); + int32_t reason = (int32_t)lua_tointeger(L, 5); + duel* pduel = pcard->pduel; + pduel->game_field->remove_overlay_card(reason, pcard, playerid, 0, 0, min, max); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::card_get_attacked_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(); + for(auto& cit : pcard->attacked_cards) { + if(cit.second.first) + pgroup->container.insert(cit.second.first); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_attacked_group_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->attacked_cards.size()); + return 1; +} +int32_t scriptlib::card_get_attacked_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->attacked_count); + return 1; +} +int32_t scriptlib::card_get_battled_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(); + for(auto& cit : pcard->battled_cards) { + if(cit.second.first) + pgroup->container.insert(cit.second.first); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_battled_group_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->battled_cards.size()); + return 1; +} +int32_t scriptlib::card_get_attack_announced_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->attack_announce_count); + return 1; +} +int32_t scriptlib::card_is_direct_attacked(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->attacked_cards.findcard(0)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_set_card_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* ocard = *(card**) lua_touserdata(L, 2); + pcard->add_card_target(ocard); + return 0; +} +int32_t scriptlib::card_get_card_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(pcard->effect_target_cards); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_first_card_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->effect_target_cards.size()) + interpreter::card2value(L, *pcard->effect_target_cards.begin()); + else lua_pushnil(L); + return 1; +} +int32_t scriptlib::card_get_card_target_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->effect_target_cards.size()); + return 1; +} +int32_t scriptlib::card_is_has_card_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* rcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, (int32_t)pcard->effect_target_cards.count(rcard)); + return 1; +} +int32_t scriptlib::card_cancel_card_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* rcard = *(card**) lua_touserdata(L, 2); + pcard->cancel_card_target(rcard); + return 0; +} +int32_t scriptlib::card_get_owner_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + group* pgroup = pcard->pduel->new_group(pcard->effect_target_owner); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::card_get_owner_target_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushinteger(L, pcard->effect_target_owner.size()); + return 1; +} +int32_t scriptlib::card_get_activate_effect(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t count = 0; + for(auto& eit : pcard->field_effect) { + if(eit.second->type & EFFECT_TYPE_ACTIVATE) { + interpreter::effect2value(L, eit.second); + ++count; + } + } + return count; +} +int32_t scriptlib::card_check_activate_effect(lua_State *L) { + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t neglect_con = lua_toboolean(L, 2); + int32_t neglect_cost = lua_toboolean(L, 3); + int32_t copy_info = lua_toboolean(L, 4); + duel* pduel = pcard->pduel; + tevent pe; + for(auto eit = pcard->field_effect.begin(); eit != pcard->field_effect.end();) { + effect* peffect = eit->second; + ++eit; + if((peffect->type & EFFECT_TYPE_ACTIVATE) + && pduel->game_field->check_event_c(peffect, pduel->game_field->core.reason_player, neglect_con, neglect_cost, copy_info, &pe)) { + if(!copy_info || (peffect->code == EVENT_FREE_CHAIN)) { + interpreter::effect2value(L, peffect); + return 1; + } else { + interpreter::effect2value(L, peffect); + interpreter::group2value(L, pe.event_cards); + lua_pushinteger(L, pe.event_player); + lua_pushinteger(L, pe.event_value); + interpreter::effect2value(L, pe.reason_effect); + lua_pushinteger(L, pe.reason); + lua_pushinteger(L, pe.reason_player); + return 7; + } + } + } + return 0; +} +int32_t scriptlib::card_get_tuner_limit(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + effect* peffect = pcard->is_affected_by_effect(EFFECT_TUNER_MATERIAL_LIMIT); + if(peffect) { + interpreter::effect2value(L, peffect); + if(peffect->target) + interpreter::function2value(L, peffect->target); + else + lua_pushnil(L); + if(peffect->value) + lua_pushinteger(L, peffect->value); + else + lua_pushnil(L); + uint16_t min = 0; + uint16_t max = 99; + if(peffect->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(peffect->s_range && peffect->s_range > min) + min = peffect->s_range; + if(peffect->o_range && peffect->o_range < max) + max = peffect->o_range; + } + lua_pushinteger(L, min); + lua_pushinteger(L, max); + return 5; + } + return 0; +} +int32_t scriptlib::card_get_hand_synchro(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + if(!(pcard->current.location & LOCATION_MZONE)) + return 0; + effect* peffect = pcard->is_affected_by_effect(EFFECT_HAND_SYNCHRO); + if(peffect) { + interpreter::effect2value(L, peffect); + if(peffect->target) + interpreter::function2value(L, peffect->target); + else + lua_pushnil(L); + uint16_t min = 0; + uint16_t max = 99; + if(peffect->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(peffect->s_range && peffect->s_range > min) + min = peffect->s_range; + if(peffect->o_range && peffect->o_range < max) + max = peffect->o_range; + } + lua_pushinteger(L, min); + lua_pushinteger(L, max); + return 4; + } + peffect = pcard->is_affected_by_effect(EFFECT_TUNER_MATERIAL_LIMIT); + if(peffect && (peffect->value & LOCATION_HAND)) { + interpreter::effect2value(L, peffect); + if(peffect->target) + interpreter::function2value(L, peffect->target); + else + lua_pushnil(L); + uint16_t min = 0; + uint16_t max = 99; + if(peffect->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(peffect->s_range && peffect->s_range > min) + min = peffect->s_range; + if(peffect->o_range && peffect->o_range < max) + max = peffect->o_range; + } + lua_pushinteger(L, min); + lua_pushinteger(L, max); + return 4; + } + return 0; +} +int32_t scriptlib::card_register_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + int32_t forced = lua_toboolean(L, 3); + duel* pduel = pcard->pduel; + if(peffect->owner == pduel->game_field->temp_card) + return 0; + if(!forced && pduel->game_field->core.reason_effect && !pcard->is_affect_by_effect(pduel->game_field->core.reason_effect)) { + pduel->game_field->core.reseted_effects.insert(peffect); + return 0; + } + for (auto& entry : category_checklist) { + if (peffect->category & entry.first) + peffect->flag[0] |= entry.second; + } + int32_t id; + if (peffect->handler) + id = -1; + else + id = pcard->add_effect(peffect); + lua_pushinteger(L, id); + return 1; +} +int32_t scriptlib::card_is_has_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + if(!pcard) { + lua_pushnil(L); + return 1; + } + effect_set eset; + pcard->filter_effect(code, &eset); + int32_t check_player = PLAYER_NONE; + if(lua_gettop(L) >= 3) { + check_player = (int32_t)lua_tointeger(L, 3); + if(check_player > PLAYER_NONE) + check_player = PLAYER_NONE; + } + int32_t size = 0; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(check_player == PLAYER_NONE || eset[i]->check_count_limit(check_player)) { + interpreter::effect2value(L, eset[i]); + ++size; + } + } + if(!size) { + lua_pushnil(L); + return 1; + } + return size; +} +int32_t scriptlib::card_reset_effect(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + uint32_t type = (uint32_t)lua_tointeger(L, 3); + pcard->reset(code, type); + return 0; +} +int32_t scriptlib::card_get_effect_count(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + effect_set eset; + pcard->filter_effect(code, &eset); + lua_pushinteger(L, eset.size()); + return 1; +} +int32_t scriptlib::card_register_flag_effect(lua_State *L) { + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + int32_t reset = (int32_t)lua_tointeger(L, 3); + uint64_t flag = lua_tointeger(L, 4); + int32_t count = (int32_t)lua_tointeger(L, 5); + lua_Integer lab = 0; + int32_t desc = 0; + if(lua_gettop(L) >= 6) + lab = lua_tointeger(L, 6); + if(lua_gettop(L) >= 7) + desc = (int32_t)lua_tointeger(L, 7); + if(count == 0) + count = 1; + if(reset & (RESET_PHASE) && !(reset & (RESET_SELF_TURN | RESET_OPPO_TURN))) + reset |= (RESET_SELF_TURN | RESET_OPPO_TURN); + duel* pduel = pcard->pduel; + effect* peffect = pduel->new_effect(); + peffect->effect_owner = pduel->game_field->core.reason_player; + peffect->owner = pcard; + peffect->handler = 0; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = code; + peffect->reset_flag = reset; + peffect->flag[0] = flag | EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_count = count; + peffect->label.push_back(lab); + peffect->description = desc; + pcard->add_effect(peffect); + interpreter::effect2value(L, peffect); + return 1; +} +int32_t scriptlib::card_get_flag_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + lua_pushinteger(L, pcard->single_effect.count(code)); + return 1; +} +int32_t scriptlib::card_reset_flag_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + pcard->reset(code, RESET_CODE); + return 0; +} +int32_t scriptlib::card_set_flag_effect_label(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + auto lab = lua_tointeger(L, 3); + auto eit = pcard->single_effect.find(code); + if(eit == pcard->single_effect.end()) + lua_pushboolean(L, FALSE); + else { + eit->second->label.clear(); + eit->second->label.push_back(lab); + lua_pushboolean(L, TRUE); + } + return 1; +} +int32_t scriptlib::card_get_flag_effect_label(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + auto rg = pcard->single_effect.equal_range(code); + int32_t count = 0; + for(; rg.first != rg.second; ++rg.first, ++count) + lua_pushinteger(L, rg.first->second->label.size() ? rg.first->second->label[0] : 0); + if(!count) { + lua_pushnil(L); + return 1; + } + return count; +} +int32_t scriptlib::card_create_relation(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* rcard = *(card**) lua_touserdata(L, 2); + uint32_t reset = (uint32_t)lua_tointeger(L, 3); + pcard->create_relation(rcard, reset); + return 0; +} +int32_t scriptlib::card_release_relation(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* rcard = *(card**) lua_touserdata(L, 2); + pcard->release_relation(rcard); + return 0; +} +int32_t scriptlib::card_create_effect_relation(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + pcard->create_relation(peffect); + return 0; +} +int32_t scriptlib::card_release_effect_relation(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + pcard->release_relation(peffect); + return 0; +} +int32_t scriptlib::card_clear_effect_relation(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + pcard->clear_relate_effect(); + return 0; +} +int32_t scriptlib::card_is_relate_to_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + if(pcard && pcard->is_has_relation(peffect)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_relate_to_chain(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t chain_count = 0; + if(lua_gettop(L) >= 2) + chain_count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = pcard->pduel; + if(chain_count > pduel->game_field->core.current_chain.size() || chain_count < 1) + chain_count = (uint32_t)pduel->game_field->core.current_chain.size(); + if(pcard && pcard->is_has_relation(pduel->game_field->core.current_chain[chain_count - 1])) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_relate_to_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* rcard = *(card**) lua_touserdata(L, 2); + if(pcard && pcard->is_has_relation(rcard)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_relate_to_battle(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + if(pcard->fieldid_r == pduel->game_field->core.pre_field[0] || pcard->fieldid_r == pduel->game_field->core.pre_field[1]) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_copy_effect(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + uint32_t reset = (uint32_t)lua_tointeger(L, 3); + int32_t count = (int32_t)lua_tointeger(L, 4); + if(count == 0) + count = 1; + if(reset & RESET_PHASE && !(reset & (RESET_SELF_TURN | RESET_OPPO_TURN))) + reset |= (RESET_SELF_TURN | RESET_OPPO_TURN); + lua_pushinteger(L, pcard->copy_effect(code, reset, count)); + return 1; +} +int32_t scriptlib::card_replace_effect(lua_State * L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + uint32_t reset = (uint32_t)lua_tointeger(L, 3); + int32_t count = (int32_t)lua_tointeger(L, 4); + if(count == 0) + count = 1; + if(reset & RESET_PHASE && !(reset & (RESET_SELF_TURN | RESET_OPPO_TURN))) + reset |= (RESET_SELF_TURN | RESET_OPPO_TURN); + lua_pushinteger(L, pcard->replace_effect(code, reset, count)); + return 1; +} +int32_t scriptlib::card_enable_revive_limit(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + if(!pcard->is_status(STATUS_COPYING_EFFECT)) { + effect* peffect = pduel->new_effect(); + peffect->owner = pcard; + peffect->code = EFFECT_REVIVE_LIMIT; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_UNCOPYABLE; + pcard->add_effect(peffect); + } + return 0; +} +int32_t scriptlib::card_complete_procedure(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + return 0; +} +int32_t scriptlib::card_is_disabled(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_status(STATUS_DISABLED)); + return 1; +} +int32_t scriptlib::card_is_destructable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + effect* peffect = nullptr; + if(lua_gettop(L) > 1) { + check_param(L, PARAM_TYPE_EFFECT, 2); + peffect = *(effect**) lua_touserdata(L, 2); + } + card* pcard = *(card**) lua_touserdata(L, 1); + if(peffect) + lua_pushboolean(L, pcard->is_destructable_by_effect(peffect, pcard->pduel->game_field->core.reason_player)); + else + lua_pushboolean(L, pcard->is_destructable()); + return 1; +} +int32_t scriptlib::card_is_summonable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_summonable_card()); + return 1; +} +int32_t scriptlib::card_is_special_summonable_card(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_spsummonable_card()); + return 1; +} +int32_t scriptlib::card_is_fusion_summonable_card(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t summon_type = 0; + if(lua_gettop(L) > 1) + summon_type = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->is_fusion_summonable_card(summon_type)); + return 1; +} +int32_t scriptlib::card_is_msetable(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->pduel->game_field->core.effect_damage_step) { + lua_pushboolean(L, FALSE); + return 1; + } + uint32_t p = pcard->pduel->game_field->core.reason_player; + uint32_t ign = lua_toboolean(L, 2); + effect* peffect = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_EFFECT, 3); + peffect = *(effect**)lua_touserdata(L, 3); + } + uint32_t minc = 0; + if(lua_gettop(L) >= 4) + minc = (uint32_t)lua_tointeger(L, 4); + uint32_t zone = 0x1f; + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pcard->is_setable_mzone(p, ign, peffect, minc, zone)); + return 1; +} +int32_t scriptlib::card_is_ssetable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + uint32_t ign = FALSE; + uint32_t tp = 2; + uint32_t zone = 0xff; + if(lua_gettop(L) >= 2) + ign = lua_toboolean(L, 2); + if(lua_gettop(L) >= 3) + tp = lua_tointeger(L, 3); + if(lua_gettop(L) >= 4) + zone = lua_tointeger(L, 4); + lua_pushboolean(L, pcard->is_setable_szone(p, ign, tp, zone)); + return 1; +} +int32_t scriptlib::card_is_special_summonable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t sumtype = 0; + if(lua_gettop(L) >= 2) + sumtype = (uint32_t)lua_tointeger(L, 2); + uint32_t p = pcard->pduel->game_field->core.reason_player; + lua_pushboolean(L, pcard->is_special_summonable(p, sumtype)); + return 1; +} +int32_t scriptlib::card_is_synchro_summonable(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_SYNCHRO) || pcard->pduel->game_field->core.effect_damage_step) { + lua_pushboolean(L, FALSE); + return 1; + } + card* tuner = nullptr; + group* mg = nullptr; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_CARD, 2); + tuner = *(card**) lua_touserdata(L, 2); + } + if(lua_gettop(L) >= 3) { + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_GROUP, 3); + mg = *(group**) lua_touserdata(L, 3); + } + } + int32_t minc = 0; + if(lua_gettop(L) >= 4) + minc = (int32_t)lua_tointeger(L, 4); + int32_t maxc = 0; + if(lua_gettop(L) >= 5) + maxc = (int32_t)lua_tointeger(L, 5); + uint32_t p = pcard->pduel->game_field->core.reason_player; + material_info info; + info.limit_tuner = tuner; + info.limit_syn = mg; + info.limit_syn_minc = minc; + info.limit_syn_maxc = maxc; + int32_t res = pcard->is_special_summonable(p, SUMMON_TYPE_SYNCHRO, info); + lua_pushboolean(L, res); + return 1; +} +int32_t scriptlib::card_is_xyz_summonable(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_XYZ) || pcard->pduel->game_field->core.effect_damage_step) { + lua_pushboolean(L, FALSE); + return 1; + } + group* materials = nullptr; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_GROUP, 2); + materials = *(group**) lua_touserdata(L, 2); + } + int32_t minc = 0; + if(lua_gettop(L) >= 3) + minc = (int32_t)lua_tointeger(L, 3); + int32_t maxc = 0; + if(lua_gettop(L) >= 4) + maxc = (int32_t)lua_tointeger(L, 4); + uint32_t p = pcard->pduel->game_field->core.reason_player; + material_info info; + info.limit_xyz = materials; + info.limit_xyz_minc = minc; + info.limit_xyz_maxc = maxc; + int32_t res = pcard->is_special_summonable(p, SUMMON_TYPE_XYZ, info); + lua_pushboolean(L, res); + return 1; +} +int32_t scriptlib::card_is_link_summonable(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + if(!(pcard->data.type & TYPE_LINK) || pcard->pduel->game_field->core.effect_damage_step) { + lua_pushboolean(L, FALSE); + return 1; + } + group* materials = nullptr; + card* lcard = nullptr; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_GROUP, 2); + materials = *(group**)lua_touserdata(L, 2); + } + if(lua_gettop(L) >= 3) { + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_CARD, 3); + lcard = *(card**)lua_touserdata(L, 3); + } + } + int32_t minc = 0; + if(lua_gettop(L) >= 4) + minc = (int32_t)lua_tointeger(L, 4); + int32_t maxc = 0; + if(lua_gettop(L) >= 5) + maxc = (int32_t)lua_tointeger(L, 5); + uint32_t p = pcard->pduel->game_field->core.reason_player; + material_info info; + info.limit_link = materials; + info.limit_link_card = lcard; + info.limit_link_minc = minc; + info.limit_link_maxc = maxc; + int32_t res = pcard->is_special_summonable(p, SUMMON_TYPE_LINK, info); + lua_pushboolean(L, res); + return 1; +} +int32_t scriptlib::card_is_can_be_summoned(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->pduel->game_field->core.effect_damage_step) { + lua_pushboolean(L, FALSE); + return 1; + } + uint32_t p = pcard->pduel->game_field->core.reason_player; + uint32_t ign = lua_toboolean(L, 2); + effect* peffect = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_EFFECT, 3); + peffect = *(effect**)lua_touserdata(L, 3); + } + uint32_t minc = 0; + if(lua_gettop(L) >= 4) + minc = (uint32_t)lua_tointeger(L, 4); + uint32_t zone = 0x1f; + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pcard->is_can_be_summoned(p, ign, peffect, minc, zone)); + return 1; +} +int32_t scriptlib::card_is_can_be_special_summoned(lua_State *L) { + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + uint32_t sumtype = (uint32_t)lua_tointeger(L, 3); + uint32_t sumplayer = (uint32_t)lua_tointeger(L, 4); + uint32_t nocheck = lua_toboolean(L, 5); + uint32_t nolimit = lua_toboolean(L, 6); + uint32_t sumpos = POS_FACEUP; + uint32_t toplayer = sumplayer; + uint32_t zone = 0xff; + if(lua_gettop(L) >= 7) + sumpos = (uint32_t)lua_tointeger(L, 7); + if(lua_gettop(L) >= 8) + toplayer = (uint32_t)lua_tointeger(L, 8); + if(lua_gettop(L) >= 9) + zone = (uint32_t)lua_tointeger(L, 9); + if(pcard->is_can_be_special_summoned(peffect, sumtype, sumpos, sumplayer, toplayer, nocheck, nolimit, zone)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_can_be_placed_on_field(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t toplayer = pcard->pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 2) + toplayer = (uint32_t)lua_tointeger(L, 2); + if(toplayer != 0 && toplayer != 1) { + lua_pushboolean(L, 0); + return 1; + } + uint32_t tolocation = LOCATION_SZONE; + if(lua_gettop(L) >= 3) + tolocation = (uint32_t)lua_tointeger(L, 3); + if(pcard->is_status(STATUS_FORBIDDEN) + || pcard->pduel->game_field->check_unique_onfield(pcard, toplayer, tolocation, nullptr)) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::card_is_able_to_hand(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 2) + p = (uint32_t)lua_tointeger(L, 2); + if(pcard->is_capable_send_to_hand(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_grave(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_send_to_grave(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_deck(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_send_to_deck(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_extra(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_send_to_extra(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_remove(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 2) + p = (uint32_t)lua_tointeger(L, 2); + uint8_t pos = POS_FACEUP; + if(lua_gettop(L) >= 3) + pos = (uint8_t)lua_tointeger(L, 3); + uint32_t reason = REASON_EFFECT; + if(lua_gettop(L) >= 4) + reason = (uint32_t)lua_tointeger(L, 4); + if(pcard->is_removeable(p, pos, reason)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_hand_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_cost_to_hand(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_grave_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_cost_to_grave(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_deck_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_cost_to_deck(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_extra_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + if(pcard->is_capable_cost_to_extra(p)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_deck_or_extra_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + int32_t val = pcard->is_extra_deck_monster() ? pcard->is_capable_cost_to_extra(p) : pcard->is_capable_cost_to_deck(p); + if(val) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_remove_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + uint8_t pos = POS_FACEUP; + if(lua_gettop(L) >= 2) + pos = (uint8_t)lua_tointeger(L, 2); + if(pcard->is_removeable_as_cost(p, pos)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_decrease_attack_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + int32_t val = 0; + if(lua_gettop(L) > 1) + val = lua_tointeger(L, 2); + if(pcard->is_attack_decreasable_as_cost(p, val)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_able_to_decrease_defense_as_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + int32_t val = 0; + if(lua_gettop(L) > 1) + val = lua_tointeger(L, 2); + if(pcard->is_defense_decreasable_as_cost(p, val)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_releasable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + uint32_t reason = REASON_COST; + if (lua_gettop(L) >= 2) + reason = (uint32_t)lua_tointeger(L, 2); + if(pcard->is_releasable_by_nonsummon(p, reason)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_releasable_by_effect(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + effect* re = pcard->pduel->game_field->core.reason_effect; + if(pcard->is_releasable_by_nonsummon(p, REASON_EFFECT) && pcard->is_releasable_by_effect(p, re)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_discardable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t p = pcard->pduel->game_field->core.reason_player; + effect* pe = pcard->pduel->game_field->core.reason_effect; + uint32_t reason = REASON_COST; + if(lua_gettop(L) > 1) + reason = (uint32_t)lua_tointeger(L, 2); + if((reason != REASON_COST || !pcard->is_affected_by_effect(EFFECT_CANNOT_USE_AS_COST)) + && pcard->pduel->game_field->is_player_can_discard_hand(p, pcard, pe, reason)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_attackable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_capable_attack()); + return 1; +} +int32_t scriptlib::card_is_chain_attackable(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + int32_t monsteronly = FALSE; + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + int32_t ac = 2; + if(lua_gettop(L) > 1) + ac = (int32_t)lua_tointeger(L, 2); + if(lua_gettop(L) > 2) + monsteronly = lua_toboolean(L, 3); + card* attacker = pduel->game_field->core.attacker; + if(attacker->is_status(STATUS_BATTLE_DESTROYED) + || attacker->current.controler != pduel->game_field->infos.turn_player + || attacker->fieldid_r != pduel->game_field->core.pre_field[0] + || !attacker->is_capable_attack_announce(pduel->game_field->infos.turn_player) + || (ac != 0 && attacker->announce_count >= ac) + || (ac == 2 && attacker->is_affected_by_effect(EFFECT_EXTRA_ATTACK))) { + lua_pushboolean(L, 0); + return 1; + } + card_vector cv; + pduel->game_field->get_attack_target(attacker, &cv, TRUE); + if(cv.size() == 0 && (monsteronly || attacker->direct_attackable == 0)) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::card_is_faceup(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_position(POS_FACEUP)); + return 1; +} +int32_t scriptlib::card_is_faceup_ex(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_position(POS_FACEUP) | (pcard->current.location & (LOCATION_HAND | LOCATION_GRAVE | LOCATION_DECK))); + return 1; +} +int32_t scriptlib::card_is_attack_pos(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_position(POS_ATTACK)); + return 1; +} +int32_t scriptlib::card_is_facedown(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_position(POS_FACEDOWN)); + return 1; +} +int32_t scriptlib::card_is_defense_pos(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_position(POS_DEFENSE)); + return 1; +} +int32_t scriptlib::card_is_position(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t pos = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->is_position(pos)); + return 1; +} +int32_t scriptlib::card_is_pre_position(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t pos = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->previous.position & pos); + return 1; +} +int32_t scriptlib::card_is_controler(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t con = (uint32_t)lua_tointeger(L, 2); + if(pcard->current.controler == con) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_pre_controler(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t con = (uint32_t)lua_tointeger(L, 2); + if(pcard->previous.controler == con) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_onfield(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if((pcard->current.location & LOCATION_ONFIELD) && !pcard->is_treated_as_not_on_field()) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_location(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t loc = (uint32_t)lua_tointeger(L, 2); + if(pcard->is_treated_as_not_on_field()) { + lua_pushboolean(L, 0); + } else if(pcard->current.location == LOCATION_MZONE) { + if(loc & LOCATION_MZONE) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + } else if(pcard->current.location == LOCATION_SZONE) { + if(pcard->current.is_location(loc)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + } else + lua_pushboolean(L, pcard->current.location & loc); + return 1; +} +int32_t scriptlib::card_is_pre_location(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t loc = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->previous.is_location(loc)); + return 1; +} +int32_t scriptlib::card_is_level_below(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t lvl = (uint32_t)lua_tointeger(L, 2); + uint32_t plvl = pcard->get_level(); + if(plvl == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, plvl <= lvl); + return 1; +} +int32_t scriptlib::card_is_level_above(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t lvl = (uint32_t)lua_tointeger(L, 2); + uint32_t plvl = pcard->get_level(); + if(plvl == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, plvl >= lvl); + return 1; +} +int32_t scriptlib::card_is_rank_below(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t rnk = (uint32_t)lua_tointeger(L, 2); + uint32_t prnk = pcard->get_rank(); + if(prnk == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, prnk <= rnk); + return 1; +} +int32_t scriptlib::card_is_rank_above(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t rnk = (uint32_t)lua_tointeger(L, 2); + uint32_t prnk = pcard->get_rank(); + if(prnk == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, prnk >= rnk); + return 1; +} +int32_t scriptlib::card_is_link_below(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t lnk = (uint32_t)lua_tointeger(L, 2); + uint32_t plnk = pcard->get_link(); + if(plnk == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, plnk <= lnk); + return 1; +} +int32_t scriptlib::card_is_link_above(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t lnk = (uint32_t)lua_tointeger(L, 2); + uint32_t plnk = pcard->get_link(); + if(plnk == 0) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, plnk >= lnk); + return 1; +} +int32_t scriptlib::card_is_attack_below(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t atk = (int32_t)lua_tointeger(L, 2); + if(!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER)) + lua_pushboolean(L, 0); + else { + int32_t _atk = pcard->get_attack(); + lua_pushboolean(L, _atk >= 0 && _atk <= atk); + } + return 1; +} +int32_t scriptlib::card_is_attack_above(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t atk = (int32_t)lua_tointeger(L, 2); + if(!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER)) + lua_pushboolean(L, 0); + else { + int32_t _atk = pcard->get_attack(); + lua_pushboolean(L, _atk >= atk); + } + return 1; +} +int32_t scriptlib::card_is_defense_below(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t def = (int32_t)lua_tointeger(L, 2); + if((pcard->data.type & TYPE_LINK) + || (!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER))) + lua_pushboolean(L, 0); + else { + int32_t _def = pcard->get_defense(); + lua_pushboolean(L, _def >= 0 && _def <= def); + } + return 1; +} +int32_t scriptlib::card_is_defense_above(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t def = (int32_t)lua_tointeger(L, 2); + if((pcard->data.type & TYPE_LINK) + || (!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER))) + lua_pushboolean(L, 0); + else { + int32_t _def = pcard->get_defense(); + lua_pushboolean(L, _def >= def); + } + return 1; +} +int32_t scriptlib::card_is_has_level(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if((pcard->data.type & (TYPE_XYZ | TYPE_LINK)) + || (pcard->status & STATUS_NO_LEVEL) + || (!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER))) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::card_is_has_defense(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if((pcard->data.type & TYPE_LINK) + || (!(pcard->data.type & TYPE_MONSTER) && !(pcard->get_type() & TYPE_MONSTER) && !pcard->is_affected_by_effect(EFFECT_PRE_MONSTER))) + lua_pushboolean(L, 0); + else + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::card_is_public(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->is_position(POS_FACEUP)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_forbidden(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_status(STATUS_FORBIDDEN)); + return 1; +} +int32_t scriptlib::card_is_able_to_change_controler(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->is_capable_change_control()) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_is_controler_can_be_changed(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t ign = FALSE; + if(lua_gettop(L) >= 2) + ign = lua_toboolean(L, 2); + uint32_t zone = 0xff; + if(lua_gettop(L) >= 3) + zone = (uint32_t)lua_tointeger(L, 3); + if(pcard->is_control_can_be_changed(ign, zone)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_add_counter(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t countertype = (uint32_t)lua_tointeger(L, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 3); + uint8_t singly = FALSE; + if(lua_gettop(L) > 3) + singly = lua_toboolean(L, 4); + if(pcard->is_affect_by_effect(pcard->pduel->game_field->core.reason_effect)) + lua_pushboolean(L, pcard->add_counter(pcard->pduel->game_field->core.reason_player, countertype, count, singly)); + else lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::card_remove_counter(lua_State *L) { + check_action_permission(L); + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 2); + uint32_t countertype = (uint32_t)lua_tointeger(L, 3); + uint32_t count = (uint32_t)lua_tointeger(L, 4); + uint32_t reason = (uint32_t)lua_tointeger(L, 5); + duel* pduel = pcard->pduel; + if(countertype == 0) { + // c38834303: remove all counters + int32_t result = FALSE; + for(const auto& cmit : pcard->counters) { + result = TRUE; + pduel->write_buffer8(MSG_REMOVE_COUNTER); + pduel->write_buffer16(cmit.first); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer16(cmit.second); + } + pcard->counters.clear(); + lua_pushboolean(L, result); + return 1; + } else { + pduel->game_field->remove_counter(reason, pcard, rplayer, 0, 0, countertype, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); + } +} +int32_t scriptlib::card_get_counter(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t countertype = (uint32_t)lua_tointeger(L, 2); + if(countertype == 0) + lua_pushinteger(L, pcard->counters.size()); + else + lua_pushinteger(L, pcard->get_counter(countertype)); + return 1; +} +int32_t scriptlib::card_enable_counter_permit(lua_State *L) { + check_param_count(L, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t countertype = (int32_t)lua_tointeger(L, 2); + uint32_t prange; + if(lua_gettop(L) > 2) + prange = (uint32_t)lua_tointeger(L, 3); + else if(pcard->data.type & TYPE_MONSTER) + prange = LOCATION_MZONE; + else + prange = LOCATION_SZONE | LOCATION_FZONE; + effect* peffect = pcard->pduel->new_effect(); + peffect->owner = pcard; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_COUNTER_PERMIT | countertype; + peffect->flag[0] = EFFECT_FLAG_SINGLE_RANGE; + peffect->range = prange; + if(lua_gettop(L) > 3 && lua_isfunction(L, 4)) + peffect->condition = interpreter::get_function_handle(L, 4); + pcard->add_effect(peffect); + return 0; +} +int32_t scriptlib::card_set_counter_limit(lua_State *L) { + check_param_count(L, 3); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t countertype = (int32_t)lua_tointeger(L, 2); + int32_t limit = (int32_t)lua_tointeger(L, 3); + effect* peffect = pcard->pduel->new_effect(); + peffect->owner = pcard; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_COUNTER_LIMIT | countertype; + peffect->value = limit; + pcard->add_effect(peffect); + return 0; +} +int32_t scriptlib::card_is_can_change_position(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_capable_change_position_by_effect(pcard->pduel->game_field->core.reason_player)); + return 1; +} +int32_t scriptlib::card_is_can_turn_set(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + lua_pushboolean(L, pcard->is_capable_turn_set(pcard->pduel->game_field->core.reason_player)); + return 1; +} +int32_t scriptlib::card_is_can_add_counter(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t countertype = (uint32_t)lua_tointeger(L, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 3); + uint8_t singly = FALSE; + if(lua_gettop(L) > 3) + singly = lua_toboolean(L, 4); + uint32_t loc = 0; + if(lua_gettop(L) > 4) + loc = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pcard->is_can_add_counter(pcard->pduel->game_field->core.reason_player, countertype, count, singly, loc)); + return 1; +} +int32_t scriptlib::card_is_can_remove_counter(lua_State *L) { + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t countertype = (uint32_t)lua_tointeger(L, 3); + uint32_t count = (uint32_t)lua_tointeger(L, 4); + uint32_t reason = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pcard->pduel->game_field->is_player_can_remove_counter(playerid, pcard, 0, 0, countertype, count, reason)); + return 1; +} +int32_t scriptlib::card_is_can_have_counter(lua_State* L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t countertype = (uint32_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->is_can_have_counter(countertype)); + return 1; +} +int32_t scriptlib::card_is_can_overlay(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint8_t playerid = pcard->pduel->game_field->core.reason_player; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) + playerid = (uint8_t)lua_tointeger(L, 2); + lua_pushboolean(L, pcard->is_capable_overlay(playerid)); + return 1; +} +int32_t scriptlib::card_is_can_be_fusion_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card* fcard = nullptr; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_CARD, 2); + fcard = *(card**)lua_touserdata(L, 2); + } + uint32_t summon_type = SUMMON_TYPE_FUSION; + if(lua_gettop(L) >= 3) + summon_type = (uint32_t)lua_tointeger(L, 3); + lua_pushboolean(L, pcard->is_can_be_fusion_material(fcard, summon_type)); + return 1; +} +int32_t scriptlib::card_is_can_be_synchro_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = nullptr; + card* tuner = nullptr; + if(lua_gettop(L) >= 2) { + check_param(L, PARAM_TYPE_CARD, 2); + scard = *(card**) lua_touserdata(L, 2); + } + if(lua_gettop(L) >= 3) { + check_param(L, PARAM_TYPE_CARD, 3); + tuner = *(card**) lua_touserdata(L, 3); + } + lua_pushboolean(L, pcard->is_can_be_synchro_material(scard, tuner)); + return 1; +} +int32_t scriptlib::card_is_can_be_ritual_material(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = nullptr; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_CARD, 2); + scard = *(card**) lua_touserdata(L, 2); + } + lua_pushboolean(L, pcard->is_can_be_ritual_material(scard)); + return 1; +} +int32_t scriptlib::card_is_can_be_xyz_material(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = nullptr; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_CARD, 2); + scard = *(card**) lua_touserdata(L, 2); + } + lua_pushboolean(L, pcard->is_can_be_xyz_material(scard)); + return 1; +} +int32_t scriptlib::card_is_can_be_link_material(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + card* scard = nullptr; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_CARD, 2); + scard = *(card**) lua_touserdata(L, 2); + } + lua_pushboolean(L, pcard->is_can_be_link_material(scard)); + return 1; +} +int32_t scriptlib::card_check_fusion_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t chkf = PLAYER_NONE; + group* pgroup = nullptr; + if(lua_gettop(L) > 1 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_GROUP, 2); + pgroup = *(group**) lua_touserdata(L, 2); + } + card* cg = nullptr; + if(lua_gettop(L) > 2 && !lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_CARD, 3); + cg = *(card**) lua_touserdata(L, 3); + } + if(lua_gettop(L) > 3) + chkf = (uint32_t)lua_tointeger(L, 4); + uint8_t not_material = FALSE; + if(lua_gettop(L) > 4) + not_material = lua_toboolean(L, 5); + lua_pushboolean(L, pcard->fusion_check(pgroup, cg, chkf, not_material)); + return 1; +} +int32_t scriptlib::card_check_fusion_substitute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* fcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pcard->check_fusion_substitute(fcard)); + return 1; +} +int32_t scriptlib::card_is_immune_to_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + effect* peffect = *(effect**) lua_touserdata(L, 2); + lua_pushboolean(L, (int)!pcard->is_affect_by_effect(peffect)); + return 1; +} +int32_t scriptlib::card_is_can_be_disabled_by_effect(lua_State* L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_EFFECT, 2); + card* pcard = *(card**)lua_touserdata(L, 1); + effect* peffect = *(effect**)lua_touserdata(L, 2); + bool is_monster_effect = true; + if (lua_gettop(L) > 2) + is_monster_effect = lua_toboolean(L, 3); + lua_pushboolean(L, pcard->is_can_be_disabled_by_effect(peffect, is_monster_effect)); + return 1; +} +int32_t scriptlib::card_is_can_be_effect_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + effect* peffect = pduel->game_field->core.reason_effect; + if(lua_gettop(L) > 1) { + check_param(L, PARAM_TYPE_EFFECT, 2); + peffect = *(effect**) lua_touserdata(L, 2); + } + lua_pushboolean(L, pcard->is_capable_be_effect_target(peffect, pduel->game_field->core.reason_player)); + return 1; +} +int32_t scriptlib::card_is_can_be_battle_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* bcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pcard->is_capable_be_battle_target(bcard)); + return 1; +} +int32_t scriptlib::card_add_monster_attribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + int32_t type = (int32_t)lua_tointeger(L, 2); + int32_t attribute = (int32_t)lua_tointeger(L, 3); + int32_t race = (int32_t)lua_tointeger(L, 4); + int32_t level = (int32_t)lua_tointeger(L, 5); + int32_t atk = (int32_t)lua_tointeger(L, 6); + int32_t def = (int32_t)lua_tointeger(L, 7); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + pcard->set_status(STATUS_NO_LEVEL, FALSE); + effect* peffect; + auto add_temp_effect = [&](uint32_t code, int32_t value, uint32_t extra_reset_flag = 0) { + peffect = pduel->new_effect(); + peffect->owner = pcard; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = code; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = RESET_EVENT | 0x47e0000 | extra_reset_flag; + peffect->value = value; + pcard->add_effect(peffect); + }; + add_temp_effect(EFFECT_PRE_MONSTER, type, RESET_CHAIN); + if(attribute) { + add_temp_effect(EFFECT_CHANGE_ATTRIBUTE, attribute); + } + if(race) { + add_temp_effect(EFFECT_CHANGE_RACE, race); + } + if(level) { + add_temp_effect(EFFECT_CHANGE_LEVEL, level); + } + if(atk) { + add_temp_effect(EFFECT_SET_BASE_ATTACK, atk); + } + if(def) { + add_temp_effect(EFFECT_SET_BASE_DEFENSE, def); + } + return 0; +} +int32_t scriptlib::card_cancel_to_grave(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + bool cancel = true; + if(lua_gettop(L) > 1) + cancel = lua_toboolean(L, 2) != 0; + if(cancel) + pcard->set_status(STATUS_LEAVE_CONFIRMED, FALSE); + else { + pcard->pduel->game_field->core.leave_confirmed.insert(pcard); + pcard->set_status(STATUS_LEAVE_CONFIRMED, TRUE); + } + return 0; +} +int32_t scriptlib::card_get_tribute_requirement(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t rcount = pcard->get_summon_tribute_count(); + lua_pushinteger(L, rcount & 0xffff); + lua_pushinteger(L, (rcount >> 16) & 0xffff); + return 2; +} +int32_t scriptlib::card_get_battle_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.attacker == pcard) + interpreter::card2value(L, pduel->game_field->core.attack_target); + else if(pduel->game_field->core.attack_target == pcard) + interpreter::card2value(L, pduel->game_field->core.attacker); + else lua_pushnil(L); + return 1; +} +int32_t scriptlib::card_get_attackable_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + card_vector targets; + uint8_t chain_attack = FALSE; + if(pduel->game_field->core.chain_attacker_id == pcard->fieldid) + chain_attack = TRUE; + pduel->game_field->get_attack_target(pcard, &targets, chain_attack); + group* newgroup = pduel->new_group(); + newgroup->container.insert(targets.begin(), targets.end()); + interpreter::group2value(L, newgroup); + lua_pushboolean(L, (int32_t)pcard->direct_attackable); + return 2; +} +int32_t scriptlib::card_set_hint(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + uint32_t type = (uint32_t)lua_tointeger(L, 2); + uint32_t value = (uint32_t)lua_tointeger(L, 3); + if(type >= CHINT_DESC_ADD) + return 0; + pduel->write_buffer8(MSG_CARD_HINT); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer8(type); + pduel->write_buffer32(value); + return 0; +} +int32_t scriptlib::card_reverse_in_deck(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->current.location != LOCATION_DECK) + return 0; + pcard->current.position = POS_FACEUP_DEFENSE; + duel* pduel = pcard->pduel; + if(pcard->current.sequence == pduel->game_field->player[pcard->current.controler].list_main.size() - 1) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(0); + pduel->write_buffer32(pcard->data.code | 0x80000000); + } + return 0; +} +int32_t scriptlib::card_set_unique_onfield(lua_State *L) { + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + pcard->unique_pos[0] = (uint8_t)lua_tointeger(L, 2); + pcard->unique_pos[1] = (uint8_t)lua_tointeger(L, 3); + if(lua_isfunction(L, 4)) { + pcard->unique_code = 1; + pcard->unique_function = interpreter::get_function_handle(L, 4); + } else + pcard->unique_code = (uint32_t)lua_tointeger(L, 4); + uint32_t location = LOCATION_ONFIELD; + if(lua_gettop(L) > 4) + location = lua_tointeger(L, 5) & LOCATION_ONFIELD; + pcard->unique_location = location; + effect* peffect = pcard->pduel->new_effect(); + peffect->owner = pcard; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_UNIQUE_CHECK; + pcard->add_effect(peffect); + pcard->unique_effect = peffect; + if(pcard->current.location & location) + pcard->pduel->game_field->add_unique_card(pcard); + return 0; +} +int32_t scriptlib::card_check_unique_onfield(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t check_player = (uint32_t)lua_tointeger(L, 2); + uint32_t check_location = LOCATION_ONFIELD; + if(lua_gettop(L) > 2) + check_location = lua_tointeger(L, 3) & LOCATION_ONFIELD; + card* icard = nullptr; + if(lua_gettop(L) > 3) { + if(check_param(L, PARAM_TYPE_CARD, 4, TRUE)) + icard = *(card**)lua_touserdata(L, 4); + } + lua_pushboolean(L, pcard->pduel->game_field->check_unique_onfield(pcard, check_player, check_location, icard) ? 0 : 1); + return 1; +} +int32_t scriptlib::card_reset_negate_effect(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t count = lua_gettop(L) - 1; + for(int32_t i = 0; i < count; ++i) + pcard->reset((uint32_t)lua_tointeger(L, i + 2), RESET_CARD); + return 0; +} +int32_t scriptlib::card_assume_prop(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + pcard->assume_type = (uint8_t)lua_tointeger(L, 2); + pcard->assume_value = (uint32_t)lua_tointeger(L, 3); + pcard->pduel->assumes.insert(pcard); + return 0; +} +int32_t scriptlib::card_set_spsummon_once(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(pcard->status & STATUS_COPYING_EFFECT) + return 0; + pcard->spsummon_code = (uint32_t)lua_tointeger(L, 2); + pcard->pduel->game_field->core.global_flag |= GLOBALFLAG_SPSUMMON_ONCE; + return 0; +} +int32_t scriptlib::card_get_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + return pcard->meta.luaop_get(L, 1); +} +int32_t scriptlib::card_set_meta_value(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + return pcard->meta.luaop_set(L, 1); +} +int32_t scriptlib::card_has_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + return pcard->meta.luaop_has(L, 1); +} +int32_t scriptlib::card_get_meta_keys(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + return pcard->meta.luaop_keys(L); +} +int32_t scriptlib::card_clear_meta(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + return pcard->meta.luaop_clear(); +} + +static const struct luaL_Reg cardlib[] = { + //millux + { "GetCardRegistered", scriptlib::card_get_card_registered }, + { "IsRitualType", scriptlib::card_is_ritual_type }, + { "SetEntityCode", scriptlib::card_set_entity_code }, + { "SetCardData", scriptlib::card_set_card_data }, + { "GetLinkMarker", scriptlib::card_get_link_marker }, + { "GetOriginalLinkMarker", scriptlib::card_get_origin_link_marker }, + { "IsXyzSummonableByRose", scriptlib::card_is_xyz_summonable_by_rose }, + { "GetRemovedOverlayCount", scriptlib::card_get_removed_overlay_count }, + { "IsAbleToDecreaseAttackAsCost", scriptlib::card_is_able_to_decrease_attack_as_cost }, + { "IsAbleToDecreaseDefenseAsCost", scriptlib::card_is_able_to_decrease_defense_as_cost }, + { "CheckSPSummonOnce", scriptlib::card_check_spsummon_once }, + + { "GetCode", scriptlib::card_get_code }, + { "GetOriginalCode", scriptlib::card_get_origin_code }, + { "GetOriginalCodeRule", scriptlib::card_get_origin_code_rule }, + { "GetFusionCode", scriptlib::card_get_fusion_code }, + { "GetLinkCode", scriptlib::card_get_link_code }, + { "IsFusionCode", scriptlib::card_is_fusion_code }, + { "IsLinkCode", scriptlib::card_is_link_code }, + { "IsSetCard", scriptlib::card_is_set_card }, + { "IsOriginalSetCard", scriptlib::card_is_origin_set_card }, + { "IsPreviousSetCard", scriptlib::card_is_pre_set_card }, + { "IsFusionSetCard", scriptlib::card_is_fusion_set_card }, + { "IsLinkSetCard", scriptlib::card_is_link_set_card }, + { "IsSpecialSummonSetCard", scriptlib::card_is_special_summon_set_card }, + { "GetType", scriptlib::card_get_type }, + { "GetOriginalType", scriptlib::card_get_origin_type }, + { "GetFusionType", scriptlib::card_get_fusion_type }, + { "GetSynchroType", scriptlib::card_get_synchro_type }, + { "GetXyzType", scriptlib::card_get_xyz_type }, + { "GetLinkType", scriptlib::card_get_link_type }, + { "GetLevel", scriptlib::card_get_level }, + { "GetRank", scriptlib::card_get_rank }, + { "GetLink", scriptlib::card_get_link }, + { "GetSynchroLevel", scriptlib::card_get_synchro_level }, + { "GetRitualLevel", scriptlib::card_get_ritual_level }, + { "GetOriginalLevel", scriptlib::card_get_origin_level }, + { "GetOriginalRank", scriptlib::card_get_origin_rank }, + { "IsXyzLevel", scriptlib::card_is_xyz_level }, + { "GetLeftScale", scriptlib::card_get_lscale }, + { "GetOriginalLeftScale", scriptlib::card_get_origin_lscale }, + { "GetRightScale", scriptlib::card_get_rscale }, + { "GetOriginalRightScale", scriptlib::card_get_origin_rscale }, + { "GetCurrentScale", scriptlib::card_get_current_scale }, + { "IsLinkMarker", scriptlib::card_is_link_marker }, + { "GetLinkedGroup", scriptlib::card_get_linked_group }, + { "GetLinkedGroupCount", scriptlib::card_get_linked_group_count }, + { "GetLinkedZone", scriptlib::card_get_linked_zone }, + { "GetMutualLinkedGroup", scriptlib::card_get_mutual_linked_group }, + { "GetMutualLinkedGroupCount", scriptlib::card_get_mutual_linked_group_count }, + { "GetMutualLinkedZone", scriptlib::card_get_mutual_linked_zone }, + { "IsLinkState", scriptlib::card_is_link_state }, + { "IsExtraLinkState", scriptlib::card_is_extra_link_state }, + { "GetColumnGroup", scriptlib::card_get_column_group }, + { "GetColumnGroupCount", scriptlib::card_get_column_group_count }, + { "GetColumnZone", scriptlib::card_get_column_zone }, + { "IsAllColumn", scriptlib::card_is_all_column }, + { "GetAttribute", scriptlib::card_get_attribute }, + { "GetOriginalAttribute", scriptlib::card_get_origin_attribute }, + { "GetFusionAttribute", scriptlib::card_get_fusion_attribute }, + { "GetLinkAttribute", scriptlib::card_get_link_attribute }, + { "GetAttributeInGrave", scriptlib::card_get_attribute_in_grave }, + { "GetRace", scriptlib::card_get_race }, + { "GetOriginalRace", scriptlib::card_get_origin_race }, + { "GetLinkRace", scriptlib::card_get_link_race }, + { "GetRaceInGrave", scriptlib::card_get_race_in_grave }, + { "GetAttack", scriptlib::card_get_attack }, + { "GetBaseAttack", scriptlib::card_get_origin_attack }, + { "GetTextAttack", scriptlib::card_get_text_attack }, + { "GetDefense", scriptlib::card_get_defense }, + { "GetBaseDefense", scriptlib::card_get_origin_defense }, + { "GetTextDefense", scriptlib::card_get_text_defense }, + { "GetPreviousCodeOnField", scriptlib::card_get_previous_code_onfield }, + { "GetPreviousTypeOnField", scriptlib::card_get_previous_type_onfield }, + { "GetPreviousLevelOnField", scriptlib::card_get_previous_level_onfield }, + { "GetPreviousRankOnField", scriptlib::card_get_previous_rank_onfield }, + { "GetPreviousAttributeOnField", scriptlib::card_get_previous_attribute_onfield }, + { "GetPreviousRaceOnField", scriptlib::card_get_previous_race_onfield }, + { "GetPreviousAttackOnField", scriptlib::card_get_previous_attack_onfield }, + { "GetPreviousDefenseOnField", scriptlib::card_get_previous_defense_onfield }, + { "GetPreviousOverlayCountOnField", scriptlib::card_get_previous_overlay_count_onfield }, + { "GetOwner", scriptlib::card_get_owner }, + { "GetControler", scriptlib::card_get_controler }, + { "GetPreviousControler", scriptlib::card_get_previous_controler }, + { "SetReason", scriptlib::card_set_reason }, + { "GetReason", scriptlib::card_get_reason }, + { "GetReasonCard", scriptlib::card_get_reason_card }, + { "GetReasonPlayer", scriptlib::card_get_reason_player }, + { "GetReasonEffect", scriptlib::card_get_reason_effect }, + { "GetPosition", scriptlib::card_get_position }, + { "GetPreviousPosition", scriptlib::card_get_previous_position }, + { "GetBattlePosition", scriptlib::card_get_battle_position }, + { "GetLocation", scriptlib::card_get_location }, + { "GetPreviousLocation", scriptlib::card_get_previous_location }, + { "GetSequence", scriptlib::card_get_sequence }, + { "GetPreviousSequence", scriptlib::card_get_previous_sequence }, + { "GetSummonType", scriptlib::card_get_summon_type }, + { "GetSummonLocation", scriptlib::card_get_summon_location }, + { "GetSummonPlayer", scriptlib::card_get_summon_player }, + { "GetSpecialSummonInfo", scriptlib::card_get_special_summon_info }, + { "GetDestination", scriptlib::card_get_destination }, + { "GetLeaveFieldDest", scriptlib::card_get_leave_field_dest }, + { "GetTurnID", scriptlib::card_get_turnid }, + { "GetFieldID", scriptlib::card_get_fieldid }, + { "GetRealFieldID", scriptlib::card_get_fieldidr }, + { "IsOriginalCodeRule", scriptlib::card_is_origin_code_rule }, + { "IsCode", scriptlib::card_is_code }, + { "IsType", scriptlib::card_is_type }, + { "IsAllTypes", scriptlib::card_is_all_types }, + { "IsFusionType", scriptlib::card_is_fusion_type }, + { "IsSynchroType", scriptlib::card_is_synchro_type }, + { "IsXyzType", scriptlib::card_is_xyz_type }, + { "IsLinkType", scriptlib::card_is_link_type }, + { "IsLevel", scriptlib::card_is_level }, + { "IsRank", scriptlib::card_is_rank }, + { "IsLink", scriptlib::card_is_link }, + { "IsAttack", scriptlib::card_is_attack }, + { "IsDefense", scriptlib::card_is_defense }, + { "IsRace", scriptlib::card_is_race }, + { "IsLinkRace", scriptlib::card_is_link_race }, + { "IsAttribute", scriptlib::card_is_attribute }, + { "IsFusionAttribute", scriptlib::card_is_fusion_attribute }, + { "IsLinkAttribute", scriptlib::card_is_link_attribute }, + { "IsNonAttribute", scriptlib::card_is_non_attribute }, + { "IsExtraDeckMonster", scriptlib::card_is_extra_deck_monster }, + { "IsReason", scriptlib::card_is_reason }, + { "IsAllReasons", scriptlib::card_is_all_reasons }, + { "IsSummonType", scriptlib::card_is_summon_type }, + { "IsSummonLocation", scriptlib::card_is_summon_location }, + { "IsSummonPlayer", scriptlib::card_is_summon_player }, + { "IsStatus", scriptlib::card_is_status }, + { "IsNotTuner", scriptlib::card_is_not_tuner }, + { "IsTuner", scriptlib::card_is_tuner }, + { "IsOriginalEffectProperty", scriptlib::card_is_original_effect_property }, + { "IsEffectProperty", scriptlib::card_is_effect_property }, + { "SetStatus", scriptlib::card_set_status }, + { "IsDualState", scriptlib::card_is_dual_state }, + { "EnableDualState", scriptlib::card_enable_dual_state }, + { "SetTurnCounter", scriptlib::card_set_turn_counter }, + { "GetTurnCounter", scriptlib::card_get_turn_counter }, + { "SetMaterial", scriptlib::card_set_material }, + { "GetMaterial", scriptlib::card_get_material }, + { "GetMaterialCount", scriptlib::card_get_material_count }, + { "GetEquipGroup", scriptlib::card_get_equip_group }, + { "GetEquipCount", scriptlib::card_get_equip_count }, + { "GetEquipTarget", scriptlib::card_get_equip_target }, + { "GetPreviousEquipTarget", scriptlib::card_get_pre_equip_target }, + { "CheckEquipTarget", scriptlib::card_check_equip_target }, + { "CheckUnionTarget", scriptlib::card_check_union_target }, + { "GetUnionCount", scriptlib::card_get_union_count }, + { "GetOverlayGroup", scriptlib::card_get_overlay_group }, + { "GetOverlayCount", scriptlib::card_get_overlay_count }, + { "GetOverlayTarget", scriptlib::card_get_overlay_target }, + { "CheckRemoveOverlayCard", scriptlib::card_check_remove_overlay_card }, + { "RemoveOverlayCard", scriptlib::card_remove_overlay_card }, + { "GetAttackedGroup", scriptlib::card_get_attacked_group }, + { "GetAttackedGroupCount", scriptlib::card_get_attacked_group_count }, + { "GetAttackedCount", scriptlib::card_get_attacked_count }, + { "GetBattledGroup", scriptlib::card_get_battled_group }, + { "GetBattledGroupCount", scriptlib::card_get_battled_group_count }, + { "GetAttackAnnouncedCount", scriptlib::card_get_attack_announced_count }, + { "IsDirectAttacked", scriptlib::card_is_direct_attacked }, + { "SetCardTarget", scriptlib::card_set_card_target }, + { "GetCardTarget", scriptlib::card_get_card_target }, + { "GetFirstCardTarget", scriptlib::card_get_first_card_target }, + { "GetCardTargetCount", scriptlib::card_get_card_target_count }, + { "IsHasCardTarget", scriptlib::card_is_has_card_target }, + { "CancelCardTarget", scriptlib::card_cancel_card_target }, + { "GetOwnerTarget", scriptlib::card_get_owner_target }, + { "GetOwnerTargetCount", scriptlib::card_get_owner_target_count }, + { "GetActivateEffect", scriptlib::card_get_activate_effect }, + { "CheckActivateEffect", scriptlib::card_check_activate_effect }, + { "GetTunerLimit", scriptlib::card_get_tuner_limit }, + { "GetHandSynchro", scriptlib::card_get_hand_synchro }, + { "RegisterEffect", scriptlib::card_register_effect }, + { "IsHasEffect", scriptlib::card_is_has_effect }, + { "ResetEffect", scriptlib::card_reset_effect }, + { "GetEffectCount", scriptlib::card_get_effect_count }, + { "RegisterFlagEffect", scriptlib::card_register_flag_effect }, + { "GetFlagEffect", scriptlib::card_get_flag_effect }, + { "ResetFlagEffect", scriptlib::card_reset_flag_effect }, + { "SetFlagEffectLabel", scriptlib::card_set_flag_effect_label }, + { "GetFlagEffectLabel", scriptlib::card_get_flag_effect_label }, + { "CreateRelation", scriptlib::card_create_relation }, + { "ReleaseRelation", scriptlib::card_release_relation }, + { "CreateEffectRelation", scriptlib::card_create_effect_relation }, + { "ReleaseEffectRelation", scriptlib::card_release_effect_relation }, + { "ClearEffectRelation", scriptlib::card_clear_effect_relation }, + { "IsRelateToEffect", scriptlib::card_is_relate_to_effect }, + { "IsRelateToChain", scriptlib::card_is_relate_to_chain }, + { "IsRelateToCard", scriptlib::card_is_relate_to_card }, + { "IsRelateToBattle", scriptlib::card_is_relate_to_battle }, + { "CopyEffect", scriptlib::card_copy_effect }, + { "ReplaceEffect", scriptlib::card_replace_effect }, + { "EnableReviveLimit", scriptlib::card_enable_revive_limit }, + { "CompleteProcedure", scriptlib::card_complete_procedure }, + { "IsDisabled", scriptlib::card_is_disabled }, + { "IsDestructable", scriptlib::card_is_destructable }, + { "IsSummonableCard", scriptlib::card_is_summonable }, + { "IsSpecialSummonableCard", scriptlib::card_is_special_summonable_card }, + { "IsFusionSummonableCard", scriptlib::card_is_fusion_summonable_card }, + { "IsSpecialSummonable", scriptlib::card_is_special_summonable }, + { "IsSynchroSummonable", scriptlib::card_is_synchro_summonable }, + { "IsXyzSummonable", scriptlib::card_is_xyz_summonable }, + { "IsLinkSummonable", scriptlib::card_is_link_summonable }, + { "IsSummonable", scriptlib::card_is_can_be_summoned }, + { "IsMSetable", scriptlib::card_is_msetable }, + { "IsSSetable", scriptlib::card_is_ssetable }, + { "IsCanBeSpecialSummoned", scriptlib::card_is_can_be_special_summoned }, + { "IsCanBePlacedOnField", scriptlib::card_is_can_be_placed_on_field }, + { "IsAbleToHand", scriptlib::card_is_able_to_hand }, + { "IsAbleToDeck", scriptlib::card_is_able_to_deck }, + { "IsAbleToExtra", scriptlib::card_is_able_to_extra }, + { "IsAbleToGrave", scriptlib::card_is_able_to_grave }, + { "IsAbleToRemove", scriptlib::card_is_able_to_remove }, + { "IsAbleToHandAsCost", scriptlib::card_is_able_to_hand_as_cost }, + { "IsAbleToDeckAsCost", scriptlib::card_is_able_to_deck_as_cost }, + { "IsAbleToExtraAsCost", scriptlib::card_is_able_to_extra_as_cost }, + { "IsAbleToDeckOrExtraAsCost", scriptlib::card_is_able_to_deck_or_extra_as_cost }, + { "IsAbleToGraveAsCost", scriptlib::card_is_able_to_grave_as_cost }, + { "IsAbleToRemoveAsCost", scriptlib::card_is_able_to_remove_as_cost }, + { "IsReleasable", scriptlib::card_is_releasable }, + { "IsReleasableByEffect", scriptlib::card_is_releasable_by_effect }, + { "IsDiscardable", scriptlib::card_is_discardable }, + { "IsAttackable", scriptlib::card_is_attackable }, + { "IsChainAttackable", scriptlib::card_is_chain_attackable }, + { "IsFaceup", scriptlib::card_is_faceup }, + { "IsFaceupEx", scriptlib::card_is_faceup_ex }, + { "IsAttackPos", scriptlib::card_is_attack_pos }, + { "IsFacedown", scriptlib::card_is_facedown }, + { "IsDefensePos", scriptlib::card_is_defense_pos }, + { "IsPosition", scriptlib::card_is_position }, + { "IsPreviousPosition", scriptlib::card_is_pre_position }, + { "IsControler", scriptlib::card_is_controler }, + { "IsPreviousControler", scriptlib::card_is_pre_controler }, + { "IsOnField", scriptlib::card_is_onfield }, + { "IsLocation", scriptlib::card_is_location }, + { "IsPreviousLocation", scriptlib::card_is_pre_location }, + { "IsLevelBelow", scriptlib::card_is_level_below }, + { "IsLevelAbove", scriptlib::card_is_level_above }, + { "IsRankBelow", scriptlib::card_is_rank_below }, + { "IsRankAbove", scriptlib::card_is_rank_above }, + { "IsLinkBelow", scriptlib::card_is_link_below }, + { "IsLinkAbove", scriptlib::card_is_link_above }, + { "IsAttackBelow", scriptlib::card_is_attack_below }, + { "IsAttackAbove", scriptlib::card_is_attack_above }, + { "IsDefenseBelow", scriptlib::card_is_defense_below }, + { "IsDefenseAbove", scriptlib::card_is_defense_above }, + { "IsHasLevel", scriptlib::card_is_has_level }, + { "IsHasDefense", scriptlib::card_is_has_defense }, + { "IsPublic", scriptlib::card_is_public }, + { "IsForbidden", scriptlib::card_is_forbidden }, + { "IsAbleToChangeControler", scriptlib::card_is_able_to_change_controler }, + { "IsControlerCanBeChanged", scriptlib::card_is_controler_can_be_changed }, + { "AddCounter", scriptlib::card_add_counter }, + { "RemoveCounter", scriptlib::card_remove_counter }, + { "GetCounter", scriptlib::card_get_counter }, + { "EnableCounterPermit", scriptlib::card_enable_counter_permit }, + { "SetCounterLimit", scriptlib::card_set_counter_limit }, + { "IsCanChangePosition", scriptlib::card_is_can_change_position }, + { "IsCanTurnSet", scriptlib::card_is_can_turn_set }, + { "IsCanAddCounter", scriptlib::card_is_can_add_counter }, + { "IsCanRemoveCounter", scriptlib::card_is_can_remove_counter }, + { "IsCanHaveCounter", scriptlib::card_is_can_have_counter }, + { "IsCanOverlay", scriptlib::card_is_can_overlay }, + { "IsCanBeFusionMaterial", scriptlib::card_is_can_be_fusion_material }, + { "IsCanBeSynchroMaterial", scriptlib::card_is_can_be_synchro_material }, + { "IsCanBeRitualMaterial", scriptlib::card_is_can_be_ritual_material }, + { "IsCanBeXyzMaterial", scriptlib::card_is_can_be_xyz_material }, + { "IsCanBeLinkMaterial", scriptlib::card_is_can_be_link_material }, + { "CheckFusionMaterial", scriptlib::card_check_fusion_material }, + { "CheckFusionSubstitute", scriptlib::card_check_fusion_substitute }, + { "IsImmuneToEffect", scriptlib::card_is_immune_to_effect }, + { "IsCanBeDisabledByEffect", scriptlib::card_is_can_be_disabled_by_effect }, + { "IsCanBeEffectTarget", scriptlib::card_is_can_be_effect_target }, + { "IsCanBeBattleTarget", scriptlib::card_is_can_be_battle_target }, + { "AddMonsterAttribute", scriptlib::card_add_monster_attribute }, + { "CancelToGrave", scriptlib::card_cancel_to_grave }, + { "GetTributeRequirement", scriptlib::card_get_tribute_requirement }, + { "GetBattleTarget", scriptlib::card_get_battle_target }, + { "GetAttackableTarget", scriptlib::card_get_attackable_target }, + { "SetHint", scriptlib::card_set_hint }, + { "ReverseInDeck", scriptlib::card_reverse_in_deck }, + { "SetUniqueOnField", scriptlib::card_set_unique_onfield }, + { "CheckUniqueOnField", scriptlib::card_check_unique_onfield }, + { "ResetNegateEffect", scriptlib::card_reset_negate_effect }, + { "AssumeProperty", scriptlib::card_assume_prop }, + { "SetSPSummonOnce", scriptlib::card_set_spsummon_once }, + { "GetMetaValue", scriptlib::card_get_meta_value }, + { "SetMetaValue", scriptlib::card_set_meta_value }, + { "HasMetaValue", scriptlib::card_has_meta_value }, + { "ClearMeta", scriptlib::card_clear_meta }, + { "GetMetaKeys", scriptlib::card_get_meta_keys }, + { nullptr, nullptr } +}; +void scriptlib::open_cardlib(lua_State *L) { + luaL_newlib(L, cardlib); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + lua_setglobal(L, "Card"); +} diff --git a/core/libdebug.cpp b/core/libdebug.cpp new file mode 100644 index 0000000..26387ce --- /dev/null +++ b/core/libdebug.cpp @@ -0,0 +1,228 @@ +/* + * libdebug.cpp + * + * Created on: 2012-2-8 + * Author: Argon + */ + +#include +#include "scriptlib.h" +#include "duel.h" +#include "field.h" +#include "card.h" +#include "effect.h" +#include "ocgapi.h" + +int32_t scriptlib::debug_message(lua_State *L) { +#if !defined(YGOPRO_SERVER_MODE) || defined(YGOPRO_ENABLE_DEBUG_FUNC) + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + lua_getglobal(L, "tostring"); + lua_pushvalue(L, -2); + lua_pcall(L, 1, 1, 0); + interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(L, -1)); + handle_message(pduel, 2); +#endif + return 0; +} +int32_t scriptlib::debug_add_card(lua_State *L) { + check_param_count(L, 6); + duel* pduel = interpreter::get_duel_info(L); + int32_t code = (int32_t)lua_tointeger(L, 1); + int32_t owner = (int32_t)lua_tointeger(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 3); + int32_t location = (int32_t)lua_tointeger(L, 4); + int32_t sequence = (int32_t)lua_tointeger(L, 5); + int32_t position = (int32_t)lua_tointeger(L, 6); + int32_t proc = lua_toboolean(L, 7); + if (!check_playerid(owner)) + return 0; + if (!check_playerid(playerid)) + return 0; + if(!pduel->lua->preloaded) { + pduel->lua->preloaded = TRUE; + pduel->lua->call_code_function(0, (char*) "PreloadUds", 0, 0); + } + if(pduel->game_field->is_location_useable(playerid, location, sequence)) { + card* pcard = pduel->new_card(code); + pcard->owner = owner; + if(location == LOCATION_EXTRA && position == 0) + position = POS_FACEDOWN_DEFENSE; + pcard->sendto_param.position = position; + if(location == LOCATION_PZONE) { + int32_t seq = pduel->game_field->get_pzone_sequence(sequence); + pduel->game_field->add_card(playerid, pcard, LOCATION_SZONE, seq, TRUE); + } else { + pduel->game_field->add_card(playerid, pcard, location, sequence); + } + pcard->current.position = position; + if(!(location & (LOCATION_ONFIELD | LOCATION_PZONE)) || (position & POS_FACEUP)) { + pcard->enable_field_effect(true); + pduel->game_field->adjust_instant(); + } + if(proc) + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + interpreter::card2value(L, pcard); + return 1; + } else if(location == LOCATION_MZONE) { + card* fcard = pduel->game_field->get_field_card(playerid, location, sequence); + if (!fcard || !(fcard->data.type & TYPE_XYZ)) + return 0; + card* pcard = pduel->new_card(code); + pcard->owner = owner; + fcard->xyz_add(pcard); + if(proc) + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + interpreter::card2value(L, pcard); + return 1; + } + return 0; +} +int32_t scriptlib::debug_set_player_info(lua_State *L) { + check_param_count(L, 4); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t lp = (int32_t)lua_tointeger(L, 2); + int32_t startcount = (int32_t)lua_tointeger(L, 3); + int32_t drawcount = (int32_t)lua_tointeger(L, 4); + if(playerid != 0 && playerid != 1) + return 0; + pduel->game_field->player[playerid].lp = lp; + pduel->game_field->player[playerid].start_count = startcount; + pduel->game_field->player[playerid].draw_count = drawcount; + return 0; +} +int32_t scriptlib::debug_pre_summon(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t summon_type = (uint32_t)lua_tointeger(L, 2); + uint8_t summon_location = 0; + if(lua_gettop(L) > 2) + summon_location = (uint8_t)lua_tointeger(L, 3); + pcard->summon_info = summon_type | (summon_location << 16); + return 0; +} +int32_t scriptlib::debug_pre_equip(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* equip_card = *(card**) lua_touserdata(L, 1); + card* target = *(card**) lua_touserdata(L, 2); + if((equip_card->current.location != LOCATION_SZONE) + || (target->current.location != LOCATION_MZONE) + || (target->current.position & POS_FACEDOWN)) + lua_pushboolean(L, 0); + else { + equip_card->equip(target, FALSE); + equip_card->effect_target_cards.insert(target); + target->effect_target_owner.insert(equip_card); + lua_pushboolean(L, 1); + } + return 1; +} +int32_t scriptlib::debug_pre_set_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* t_card = *(card**) lua_touserdata(L, 1); + card* target = *(card**) lua_touserdata(L, 2); + t_card->add_card_target(target); + return 0; +} +int32_t scriptlib::debug_pre_add_counter(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint16_t countertype = (uint16_t)lua_tointeger(L, 2); + uint16_t count = 1; + if(lua_gettop(L) >= 3) + count = (uint16_t)lua_tointeger(L, 3); + auto pr = pcard->counters.emplace(countertype, 0); + auto cmit = pr.first; + cmit->second += count; + return 0; +} +int32_t scriptlib::debug_reload_field_begin(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + uint32_t flag = (uint32_t)lua_tointeger(L, 1); + int32_t rule = (int32_t)lua_tointeger(L, 2); + pduel->clear(); + pduel->game_field->core.duel_options |= flag; + if (rule) + pduel->game_field->core.duel_rule = rule; + else if (flag & DUEL_OBSOLETE_RULING) + pduel->game_field->core.duel_rule = 1; + else + pduel->game_field->core.duel_rule = CURRENT_RULE; + if (pduel->game_field->core.duel_rule == MASTER_RULE3) { + pduel->game_field->player[0].szone_size = 8; + pduel->game_field->player[1].szone_size = 8; + } + return 0; +} +int32_t scriptlib::debug_reload_field_end(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.shuffle_hand_check[0] = FALSE; + pduel->game_field->core.shuffle_hand_check[1] = FALSE; + pduel->game_field->core.shuffle_deck_check[0] = FALSE; + pduel->game_field->core.shuffle_deck_check[1] = FALSE; + pduel->game_field->reload_field_info(); + return 0; +} +int32_t scriptlib::debug_set_ai_name(lua_State *L) { +#ifndef YGOPRO_SERVER_MODE + check_param_count(L, 1); + check_param(L, PARAM_TYPE_STRING, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->write_buffer8(MSG_AI_NAME); + const char* pstr = lua_tostring(L, 1); + size_t length = std::strlen(pstr); + if(length > SIZE_AI_NAME - 1) + length = SIZE_AI_NAME - 1; + uint16_t len = (uint16_t)length; + pduel->write_buffer16(len); + pduel->write_buffer(pstr, len); + pduel->write_buffer8(0); +#endif + return 0; +} +int32_t scriptlib::debug_show_hint(lua_State *L) { +#if !defined(YGOPRO_SERVER_MODE) || defined(YGOPRO_ENABLE_DEBUG_FUNC) + check_param_count(L, 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); + size_t length = std::strlen(pstr); + if (length > SIZE_HINT_MSG - 1) + length = SIZE_HINT_MSG - 1; + uint16_t len = (uint16_t)length; + pduel->write_buffer16(len); + pduel->write_buffer(pstr, len); + pduel->write_buffer8(0); +#endif + return 0; +} + +static const struct luaL_Reg debuglib[] = { + { "Message", scriptlib::debug_message }, + { "AddCard", scriptlib::debug_add_card }, + { "SetPlayerInfo", scriptlib::debug_set_player_info }, + { "PreSummon", scriptlib::debug_pre_summon }, + { "PreEquip", scriptlib::debug_pre_equip }, + { "PreSetTarget", scriptlib::debug_pre_set_target }, + { "PreAddCounter", scriptlib::debug_pre_add_counter }, + { "ReloadFieldBegin", scriptlib::debug_reload_field_begin }, + { "ReloadFieldEnd", scriptlib::debug_reload_field_end }, + { "SetAIName", scriptlib::debug_set_ai_name }, + { "ShowHint", scriptlib::debug_show_hint }, + { nullptr, nullptr } +}; +void scriptlib::open_debuglib(lua_State *L) { + luaL_newlib(L, debuglib); + lua_setglobal(L, "Debug"); +} diff --git a/core/libduel.cpp b/core/libduel.cpp new file mode 100644 index 0000000..b6a97a6 --- /dev/null +++ b/core/libduel.cpp @@ -0,0 +1,5603 @@ +/* + * libduel.cpp + * + * Created on: 2010-5-6 + * Author: Argon + */ + +#include "scriptlib.h" +#include "duel.h" +#include "field.h" +#include "card.h" +#include "effect.h" +#include "group.h" +#include "ocgapi.h" + +int32_t scriptlib::duel_get_master_rule(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->core.duel_rule); + return 1; +} +int32_t scriptlib::duel_read_card(lua_State *L) { + check_param_count(L, 2); + card_data dat; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, 1); + dat = pcard->data; + } else { + int32_t code = lua_tointeger(L, 1); + ::read_card(code, &dat); + } + if(!dat.code) + return 0; + uint32_t args = lua_gettop(L) - 1; + for(uint32_t i = 0; i < args; ++i) { + int32_t flag = lua_tointeger(L, 2 + i); + switch(flag) { + case CARDDATA_CODE: + lua_pushinteger(L, dat.code); + break; + case CARDDATA_ALIAS: + lua_pushinteger(L, dat.alias); + break; + case CARDDATA_SETCODE: { + unsigned long setcode = 0; + for (int i = 0;; ++i) { + uint16_t sc = dat.setcode[i]; + if (!sc) + break; + setcode |= sc << (16 * i); + } + lua_pushinteger(L, setcode); + break; + } + case CARDDATA_TYPE: + lua_pushinteger(L, dat.type); + break; + case CARDDATA_LEVEL: + lua_pushinteger(L, dat.level); + break; + case CARDDATA_ATTRIBUTE: + lua_pushinteger(L, dat.attribute); + break; + case CARDDATA_RACE: + lua_pushinteger(L, dat.race); + break; + case CARDDATA_ATTACK: + lua_pushinteger(L, dat.attack); + break; + case CARDDATA_DEFENSE: + lua_pushinteger(L, dat.defense); + break; + case CARDDATA_LSCALE: + lua_pushinteger(L, dat.lscale); + break; + case CARDDATA_RSCALE: + lua_pushinteger(L, dat.rscale); + break; + case CARDDATA_LINK_MARKER: + lua_pushinteger(L, dat.link_marker); + break; + default: + lua_pushinteger(L, 0); + break; + } + } + return args; +} +int32_t scriptlib::duel_exile(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = 0; + group* pgroup = 0; + duel* pduel = 0; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t reason = lua_tointeger(L, 2); + if(pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, PLAYER_NONE, 0, 0, POS_FACEUP); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, PLAYER_NONE, 0, 0, POS_FACEUP); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_disable_action_check(lua_State *L) { + check_param_count(L, 1); + int32_t dis = lua_toboolean(L, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->lua->disable_action_check = dis; + return 0; +} +int32_t scriptlib::duel_setmetatable(lua_State *L) { + check_param_count(L, 2); + if(!lua_isuserdata(L, 1)) + luaL_error(L, "Parameter 1 should be \"Card\" or \"Effect\" or \"Group\"."); + if(!lua_istable(L, 2)) + luaL_error(L, "Parameter 2 should be \"Table\"."); + lua_settop(L, 2); + lua_setmetatable(L, 1); + return 0; +} +int32_t scriptlib::duel_move_turn_count(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + int32_t turn_player = pduel->game_field->infos.turn_player; + pduel->game_field->infos.turn_id++; + pduel->game_field->infos.turn_id_by_player[turn_player]++; + pduel->write_buffer8(MSG_NEW_TURN); + pduel->write_buffer8(turn_player | 0x2); + return 0; +} +int32_t scriptlib::duel_get_cards_in_zone(lua_State *L) { + check_param_count(L, 2); + uint32_t rplayer = lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t zone = lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + card_set cset; + pduel->game_field->get_cards_in_zone(&cset, zone, rplayer, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(&cset, zone >> 8, rplayer, LOCATION_SZONE); + pduel->game_field->get_cards_in_zone(&cset, zone >> 16, 1 - rplayer, LOCATION_MZONE); + pduel->game_field->get_cards_in_zone(&cset, zone >> 24, 1 - rplayer, LOCATION_SZONE); + group* pgroup = pduel->new_group(cset); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_xyz_summon_by_rose(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 2); + check_param(L, PARAM_TYPE_CARD, 3); + check_param(L, PARAM_TYPE_CARD, 4); + uint32_t playerid = lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + card* rcard = *(card**) lua_touserdata(L, 3); + card* mcard = *(card**) lua_touserdata(L, 4); + duel* pduel = pcard->pduel; + group* materials = pduel->new_group(rcard); + materials->container.insert(mcard); + pduel->game_field->core.limit_xyz = materials; + pduel->game_field->core.limit_xyz_minc = 0; + pduel->game_field->core.limit_xyz_maxc = 0; + pduel->game_field->core.summon_cancelable = FALSE; + pduel->game_field->rose_card = rcard; + pduel->game_field->rose_level = mcard->get_level(); + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_XYZ); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_load_script(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_STRING, 1); + duel* pduel = interpreter::get_duel_info(L); + const char* pstr = lua_tostring(L, 1); + char filename[64]; + sprintf(filename, "./script/%s", pstr); + lua_pushboolean(L, pduel->lua->load_script(filename)); + return 1; +} +int32_t scriptlib::duel_reset_time_limit(lua_State * L) { + check_param_count(L, 1); + int32_t p = lua_tointeger(L, 1); + int32_t time = 0; + if(p != 0 && p != 1) + luaL_error(L, "Parameter 1 should be 0 or 1.", 2); + if(lua_gettop(L) >= 2) + time = lua_tointeger(L, 2); + if(time < 0 || time > 0x7fff) + luaL_error(L, "Invalid time value.", 2); + duel* pduel = interpreter::get_duel_info(L); + pduel->write_buffer8(MSG_RESET_TIME); + pduel->write_buffer8(p); + pduel->write_buffer16(time); + return 0; +} +int32_t scriptlib::duel_set_summon_cancelable(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.summon_cancelable = lua_toboolean(L, 1); + return 0; +} +int32_t scriptlib::duel_get_random_number(lua_State * L) { + //check_action_permission(L); Don't check action permission + duel* pduel = interpreter::get_duel_info(L); + int32_t min = 0; + int32_t max = 2147483647; + uint32_t count = lua_gettop(L); + //Duel.GetRandomNumber() returns a number from [0,2147483647] + if(count > 1) { + //Duel.GetRandomNumber(n,m) returns a number from [n,m] + min = lua_tointeger(L, 1); + max = lua_tointeger(L, 2); + } + else if(count > 0) { + // Duel.GetRandomNumber(n) returns a number from [1,n] + min = 1; + max = lua_tointeger(L, 1); + } + lua_pushinteger(L, pduel->get_next_integer(min, max)); + return 1; +} +int32_t scriptlib::duel_get_registry_value(lua_State* L) { + check_param_count(L, 1); + + duel* pduel = interpreter::get_duel_info(L); + + // 对参数 1 调用 tostring + lua_getglobal(L, "tostring"); + lua_pushvalue(L, 1); + lua_call(L, 1, 1); // 调用 tostring(key) + const char* key = lua_tostring(L, -1); + if (!key) { + lua_pop(L, 1); // 清理 tostring 返回值 + lua_pushnil(L); // 无效 key,返回 nil + return 1; + } + + auto it = pduel->registry.find(key); + if (it != pduel->registry.end()) { + lua_pushstring(L, it->second.c_str()); + } else { + lua_pushnil(L); + } + + lua_remove(L, -2); // 移除 key 的 tostring 结果,保持栈只返回一个结果 + return 1; +} +int32_t scriptlib::duel_set_registry_value(lua_State* L) { + check_param_count(L, 2); + + duel* pduel = interpreter::get_duel_info(L); + + // 对参数 1(key)调用 tostring + lua_getglobal(L, "tostring"); + lua_pushvalue(L, 1); + lua_call(L, 1, 1); // 调用 tostring(key) + const char* key = lua_tostring(L, -1); // key 字符串结果在栈顶 + + if (!key) { + lua_pop(L, 1); // 清理 key tostring 结果 + return luaL_error(L, "invalid key for registry"); + } + + if (lua_isnil(L, 2)) { + // 删除 key + pduel->registry.erase(key); + lua_pop(L, 1); // 清理 key + return 0; + } + + // 对参数 2(value)调用 tostring + lua_getglobal(L, "tostring"); + lua_pushvalue(L, 2); + lua_call(L, 1, 1); // 调用 tostring(value) + const char* value = lua_tostring(L, -1); // value 字符串在栈顶 + + if (value) { + pduel->registry[key] = value; + } + + lua_pop(L, 2); // 清理 key 和 value 的 tostring 结果 + + return 0; +} +int32_t scriptlib::duel_get_registry_keys(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + + if (pduel->registry.empty()) { + lua_pushnil(L); + return 1; + } + + int count = 0; + for (const auto& pair : pduel->registry) { + lua_pushstring(L, pair.first.c_str()); + ++count; + } + + return count; // 返回 n 个字符串(key) +} +int32_t scriptlib::duel_get_registry(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + lua_newtable(L); // 创建返回表 + + for (const auto& pair : pduel->registry) { + lua_pushstring(L, pair.first.c_str()); + lua_pushstring(L, pair.second.c_str()); + lua_settable(L, -3); // table[key] = value + } + + return 1; // 返回这个 table +} +int32_t scriptlib::duel_set_registry(lua_State* L) { + check_param_count(L, 1); // 至少要 table 参数 + + if (!lua_istable(L, 1)) { + return luaL_error(L, "first argument must be a table"); + } + + bool override = false; + if (lua_gettop(L) >= 2 && lua_isboolean(L, 2)) { + override = lua_toboolean(L, 2); + } + + duel* pduel = interpreter::get_duel_info(L); + if (override) { + pduel->registry.clear(); + } + + // 遍历 Lua 表 + lua_pushnil(L); // 初始 key for lua_next + while (lua_next(L, 1)) { + // 栈: [-1]=value, [-2]=key + + // 对 key 调用 tostring + lua_getglobal(L, "tostring"); + lua_pushvalue(L, -3); // key + lua_call(L, 1, 1); // -> [tostring_key] + const char* kstr = lua_tostring(L, -1); + lua_remove(L, -1); // pop tostring_key + + // 对 value 调用 tostring + lua_getglobal(L, "tostring"); + lua_pushvalue(L, -2); // value + lua_call(L, 1, 1); // -> [tostring_value] + const char* vstr = lua_tostring(L, -1); + lua_remove(L, -1); // pop tostring_value + + if (kstr && vstr) { + pduel->registry[kstr] = vstr; + } + + lua_pop(L, 1); // 移除 value,保留 key 以继续迭代 + } + + return 0; +} +int32_t scriptlib::duel_clear_registry(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->registry.clear(); + return 0; +} + +int32_t scriptlib::duel_enable_global_flag(lua_State *L) { + check_param_count(L, 1); + uint32_t flag = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.global_flag |= flag; + return 0; +} + +int32_t scriptlib::duel_is_global_flag(lua_State *L) { + check_param_count(L, 1); + int32_t flag = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + + if((pduel->game_field->core.global_flag & flag) == flag) { + lua_pushboolean(L, 1); // true + } else { + lua_pushboolean(L, 0); // false + } + + return 1; // 返回一个结果值 +} + +int32_t scriptlib::duel_get_lp(lua_State *L) { + check_param_count(L, 1); + int32_t p = (int32_t)lua_tointeger(L, 1); + if(p != 0 && p != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->player[p].lp); + return 1; +} +int32_t scriptlib::duel_set_lp(lua_State *L) { + check_param_count(L, 2); + int32_t p = (int32_t)lua_tointeger(L, 1); + int32_t lp = (int32_t)lua_tonumber(L, 2); + if(lp < 0) lp = 0; + if(p != 0 && p != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->player[p].lp = lp; + pduel->write_buffer8(MSG_LPUPDATE); + pduel->write_buffer8(p); + pduel->write_buffer32(lp); + return 0; +} +int32_t scriptlib::duel_is_turn_player(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->infos.turn_player == playerid) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_get_turn_player(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->infos.turn_player); + return 1; +} +int32_t scriptlib::duel_get_turn_count(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) > 0) { + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + lua_pushinteger(L, pduel->game_field->infos.turn_id_by_player[playerid]); + } else + lua_pushinteger(L, pduel->game_field->infos.turn_id); + return 1; +} +int32_t scriptlib::duel_get_draw_count(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + lua_pushinteger(L, pduel->game_field->get_draw_count(playerid)); + return 1; +} +int32_t scriptlib::duel_register_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**)lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = peffect->pduel; + pduel->game_field->add_effect(peffect, playerid); + return 0; +} +int32_t scriptlib::duel_register_flag_effect(lua_State *L) { + check_param_count(L, 5); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + int32_t reset = (int32_t)lua_tointeger(L, 3); + uint64_t flag = lua_tointeger(L, 4); + int32_t count = (int32_t)lua_tointeger(L, 5); + lua_Integer lab = 0; + if(lua_gettop(L) >= 6) + lab = lua_tointeger(L, 6); + if(count == 0) + count = 1; + if(reset & (RESET_PHASE) && !(reset & (RESET_SELF_TURN | RESET_OPPO_TURN))) + reset |= (RESET_SELF_TURN | RESET_OPPO_TURN); + duel* pduel = interpreter::get_duel_info(L); + effect* peffect = pduel->new_effect(); + peffect->effect_owner = playerid; + peffect->owner = pduel->game_field->temp_card; + peffect->handler = 0; + peffect->type = EFFECT_TYPE_FIELD; + peffect->code = code; + peffect->reset_flag = reset; + peffect->flag[0] = flag | EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_PLAYER_TARGET | EFFECT_FLAG_FIELD_ONLY; + peffect->s_range = 1; + peffect->o_range = 0; + peffect->reset_count = count; + peffect->label.push_back(lab); + pduel->game_field->add_effect(peffect, playerid); + interpreter::effect2value(L, peffect); + return 1; +} +int32_t scriptlib::duel_get_flag_effect(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + duel* pduel = interpreter::get_duel_info(L); + effect_set eset; + pduel->game_field->filter_player_effect(playerid, code, &eset); + lua_pushinteger(L, eset.size()); + return 1; +} +int32_t scriptlib::duel_reset_flag_effect(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + duel* pduel = interpreter::get_duel_info(L); + auto pr = pduel->game_field->effects.aura_effect.equal_range(code); + for(; pr.first != pr.second; ) { + auto rm = pr.first++; + effect* peffect = rm->second; + if(peffect->code == code && peffect->is_target_player(playerid)) + pduel->game_field->remove_effect(peffect); + } + return 0; +} +int32_t scriptlib::duel_set_flag_effect_label(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + auto lab = lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + effect_set eset; + pduel->game_field->filter_player_effect(playerid, code, &eset); + if(!eset.size()) + lua_pushboolean(L, FALSE); + else { + eset[0]->label.clear(); + eset[0]->label.push_back(lab); + lua_pushboolean(L, TRUE); + } + return 1; +} +int32_t scriptlib::duel_get_flag_effect_label(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t code = (lua_tointeger(L, 2) & MAX_CARD_ID) | EFFECT_FLAG_EFFECT; + duel* pduel = interpreter::get_duel_info(L); + effect_set eset; + pduel->game_field->filter_player_effect(playerid, code, &eset); + if(!eset.size()) { + lua_pushnil(L); + return 1; + } + for(effect_set::size_type i = 0; i < eset.size(); ++i) + lua_pushinteger(L, eset[i]->label.size() ? eset[i]->label[0] : 0); + return (int32_t)eset.size(); +} +int32_t scriptlib::duel_destroy(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t reason = (uint32_t)lua_tointeger(L, 2); + uint32_t dest = LOCATION_GRAVE; + if(lua_gettop(L) >= 3) + dest = (uint32_t)lua_tointeger(L, 3); + uint32_t reason_player = pduel->game_field->core.reason_player; + if (lua_gettop(L) >= 4) + reason_player = (uint32_t)lua_tointeger(L, 4); + if(pcard) + pduel->game_field->destroy(pcard, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, dest, 0); + else + pduel->game_field->destroy(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, dest, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_remove(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t pos = (uint32_t)lua_tointeger(L, 2); + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + uint32_t reason_player = pduel->game_field->core.reason_player; + if (lua_gettop(L) >= 4) + reason_player = (uint32_t)lua_tointeger(L, 4); + if(pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, LOCATION_REMOVED, 0, pos); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, LOCATION_REMOVED, 0, pos); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_sendto_grave(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t reason = (uint32_t)lua_tointeger(L, 2); + uint32_t reason_player = pduel->game_field->core.reason_player; + if (lua_gettop(L) >= 3) + reason_player = (uint32_t)lua_tointeger(L, 3); + if(pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_summon(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + uint32_t ignore_count = lua_toboolean(L, 3); + effect* peffect = nullptr; + if(!lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_EFFECT, 4); + peffect = *(effect**)lua_touserdata(L, 4); + } + uint32_t min_tribute = 0; + if(lua_gettop(L) >= 5) + min_tribute = (uint32_t)lua_tointeger(L, 5); + uint32_t zone = 0x1f; + if(lua_gettop(L) >= 6) + zone = (uint32_t)lua_tointeger(L, 6); + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->summon(playerid, pcard, peffect, ignore_count, min_tribute, zone, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->summon(playerid, pcard, peffect, ignore_count, min_tribute, zone, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_special_summon_rule(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + uint32_t sumtype = 0; + if(lua_gettop(L) >= 3) + sumtype = (uint32_t)lua_tointeger(L, 3); + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->special_summon_rule(playerid, pcard, sumtype, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->special_summon_rule(playerid, pcard, sumtype, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_synchro_summon(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + card* tuner = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_CARD, 3); + tuner = *(card**)lua_touserdata(L, 3); + } + group* mg = nullptr; + if(lua_gettop(L) >= 4) { + if(!lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_GROUP, 4); + group* pgroup = *(group**) lua_touserdata(L, 4); + mg = pduel->new_group(pgroup->container); + mg->is_readonly = GTYPE_READ_ONLY; + } + } + int32_t minc = 0; + if(lua_gettop(L) >= 5) + minc = (int32_t)lua_tointeger(L, 5); + int32_t maxc = 0; + if(lua_gettop(L) >= 6) + maxc = (int32_t)lua_tointeger(L, 6); + pduel->game_field->core.limit_tuner = tuner; + pduel->game_field->core.limit_syn = mg; + pduel->game_field->core.limit_syn_minc = minc; + pduel->game_field->core.limit_syn_maxc = maxc; + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_SYNCHRO, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_SYNCHRO, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_xyz_summon(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + group* materials = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_GROUP, 3); + group* pgroup = *(group**)lua_touserdata(L, 3); + materials = pduel->new_group(pgroup->container); + materials->is_readonly = GTYPE_READ_ONLY; + } + int32_t minc = 0; + if(lua_gettop(L) >= 4) + minc = (int32_t)lua_tointeger(L, 4); + int32_t maxc = 0; + if(lua_gettop(L) >= 5) + maxc = (int32_t)lua_tointeger(L, 5); + pduel->game_field->core.limit_xyz = materials; + pduel->game_field->core.limit_xyz_minc = minc; + pduel->game_field->core.limit_xyz_maxc = maxc; + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_XYZ, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_XYZ, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_link_summon(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + group* materials = nullptr; + card* lcard = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_GROUP, 3); + group* pgroup = *(group**)lua_touserdata(L, 3); + materials = pduel->new_group(pgroup->container); + materials->is_readonly = GTYPE_READ_ONLY; + } + if(lua_gettop(L) >= 4) { + if(!lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_CARD, 4); + lcard = *(card**)lua_touserdata(L, 4); + } + } + int32_t minc = 0; + if(lua_gettop(L) >= 5) + minc = (int32_t)lua_tointeger(L, 5); + int32_t maxc = 0; + if(lua_gettop(L) >= 6) + maxc = (int32_t)lua_tointeger(L, 6); + pduel->game_field->core.limit_link = materials; + pduel->game_field->core.limit_link_card = lcard; + pduel->game_field->core.limit_link_minc = minc; + pduel->game_field->core.limit_link_maxc = maxc; + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_LINK, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->special_summon_rule(playerid, pcard, SUMMON_TYPE_LINK, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_setm(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**)lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.effect_damage_step) + return 0; + uint32_t ignore_count = lua_toboolean(L, 3); + effect* peffect = nullptr; + if(!lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_EFFECT, 4); + peffect = *(effect**)lua_touserdata(L, 4); + } + uint32_t min_tribute = 0; + if(lua_gettop(L) >= 5) + min_tribute = (uint32_t)lua_tointeger(L, 5); + uint32_t zone = 0x1f; + if(lua_gettop(L) >= 6) + zone = (uint32_t)lua_tointeger(L, 6); + pduel->game_field->core.summon_cancelable = FALSE; + if(pduel->game_field->core.current_chain.size()) { + pduel->game_field->mset(playerid, pcard, peffect, ignore_count, min_tribute, zone, SUMMON_IN_CHAIN); + pduel->game_field->core.summon_reserved = pduel->game_field->core.subunits.back(); + pduel->game_field->core.subunits.pop_back(); + pduel->game_field->core.summoning_card = pcard; + } + else { + pduel->game_field->mset(playerid, pcard, peffect, ignore_count, min_tribute, zone, SUMMON_IN_IDLE); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_sets(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t toplayer = playerid; + if(lua_gettop(L) > 2) + toplayer = (uint32_t)lua_tointeger(L, 3); + if(toplayer != 0 && toplayer != 1) + toplayer = playerid; + uint32_t confirm = TRUE; + uint32_t zone = 0xff; + if(lua_gettop(L) > 3) + confirm = lua_toboolean(L, 4); + if(lua_gettop(L) > 4) + zone = lua_tointeger(L, 5); + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, 2); + pduel = pcard->pduel; + pgroup = pduel->new_group(pcard); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 2); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + pduel->game_field->add_process(PROCESSOR_SSET_G, 0, pduel->game_field->core.reason_effect, pgroup, playerid, toplayer, confirm, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_create_token(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t code = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + card* pcard = pduel->new_card(code); + pcard->owner = playerid; + pcard->current.location = 0; + pcard->current.controler = playerid; + interpreter::card2value(L, pcard); + return 1; +} +int32_t scriptlib::duel_special_summon(lua_State *L) { + check_action_permission(L); + check_param_count(L, 7); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t sumtype = (uint32_t)lua_tointeger(L, 2); + uint32_t sumplayer = (uint32_t)lua_tointeger(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 4); + uint32_t nocheck = lua_toboolean(L, 5); + uint32_t nolimit = lua_toboolean(L, 6); + uint32_t positions = (uint32_t)lua_tointeger(L, 7); + uint32_t zone = 0xff; + if(lua_gettop(L) >= 8) + zone = (uint32_t)lua_tointeger(L, 8); + if(pcard) { + card_set cset{ pcard }; + pduel->game_field->special_summon(cset, sumtype, sumplayer, playerid, nocheck, nolimit, positions, zone); + } else + pduel->game_field->special_summon(pgroup->container, sumtype, sumplayer, playerid, nocheck, nolimit, positions, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_special_summon_step(lua_State *L) { + check_action_permission(L); + check_param_count(L, 7); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + uint32_t sumtype = (uint32_t)lua_tointeger(L, 2); + uint32_t sumplayer = (uint32_t)lua_tointeger(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 4); + uint32_t nocheck = lua_toboolean(L, 5); + uint32_t nolimit = lua_toboolean(L, 6); + uint32_t positions = (uint32_t)lua_tointeger(L, 7); + uint32_t zone = 0xff; + if(lua_gettop(L) >= 8) + zone = (uint32_t)lua_tointeger(L, 8); + pduel->game_field->special_summon_step(pcard, sumtype, sumplayer, playerid, nocheck, nolimit, positions, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_special_summon_complete(lua_State *L) { + check_action_permission(L); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->special_summon_complete(pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_sendto_hand(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(lua_isnil(L, 2) || (playerid != 0 && playerid != 1)) + playerid = PLAYER_NONE; + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + uint32_t reason_player = pduel->game_field->core.reason_player; + if (lua_gettop(L) >= 4) + reason_player = (uint32_t)lua_tointeger(L, 4); + if(pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, reason_player, playerid, LOCATION_HAND, 0, POS_FACEUP); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player, playerid, LOCATION_HAND, 0, POS_FACEUP); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_sendto_deck(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(lua_isnil(L, 2) || (playerid != 0 && playerid != 1)) + playerid = PLAYER_NONE; + uint32_t sequence = (uint32_t)lua_tointeger(L, 3); + uint32_t reason = (uint32_t)lua_tointeger(L, 4); + uint32_t reason_player = pduel->game_field->core.reason_player; + uint8_t send_activating = FALSE; + if (lua_gettop(L) >= 5) + reason_player = (uint32_t)lua_tointeger(L, 5); + if (lua_gettop(L) >= 6) + send_activating = (uint8_t)lua_toboolean(L, 6); + if (pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, reason_player, playerid, LOCATION_DECK, sequence, POS_FACEUP, send_activating); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player, playerid, LOCATION_DECK, sequence, POS_FACEUP, send_activating); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_sendto_extra(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(lua_isnil(L, 2) || (playerid != 0 && playerid != 1)) + playerid = PLAYER_NONE; + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + if(pcard) + pduel->game_field->send_to(pcard, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, LOCATION_EXTRA, 0, POS_FACEUP); + else + pduel->game_field->send_to(pgroup->container, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, LOCATION_EXTRA, 0, POS_FACEUP); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_get_operated_group(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(pduel->game_field->core.operated_set); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_is_can_add_counter(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_PLACE_COUNTER)); + else { + check_param_count(L, 4); + int32_t countertype = (int32_t)lua_tointeger(L, 2); + int32_t count = (int32_t)lua_tointeger(L, 3); + check_param(L, PARAM_TYPE_CARD, 4); + card* pcard = *(card**) lua_touserdata(L, 4); + lua_pushboolean(L, pduel->game_field->is_player_can_place_counter(playerid, pcard, countertype, count)); + } + return 1; +} +int32_t scriptlib::duel_remove_counter(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + uint32_t countertype = (uint32_t)lua_tointeger(L, 4); + uint32_t count = (uint32_t)lua_tointeger(L, 5); + uint32_t reason = (uint32_t)lua_tointeger(L, 6); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->remove_counter(reason, 0, rplayer, s, o, countertype, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_is_can_remove_counter(lua_State *L) { + check_param_count(L, 6); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + uint32_t countertype = (uint32_t)lua_tointeger(L, 4); + uint32_t count = (uint32_t)lua_tointeger(L, 5); + uint32_t reason = (uint32_t)lua_tointeger(L, 6); + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_remove_counter(rplayer, 0, s, o, countertype, count, reason)); + return 1; +} +int32_t scriptlib::duel_get_counter(lua_State *L) { + check_param_count(L, 4); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + uint32_t countertype = (uint32_t)lua_tointeger(L, 4); + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->get_field_counter(playerid, s, o, countertype)); + return 1; +} +int32_t scriptlib::duel_change_form(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t au = (uint32_t)lua_tointeger(L, 2); + uint32_t ad = au, du = au, dd = au, flag = 0; + uint32_t top = lua_gettop(L); + if(top > 2) ad = (uint32_t)lua_tointeger(L, 3); + if(top > 3) du = (uint32_t)lua_tointeger(L, 4); + if(top > 4) dd = (uint32_t)lua_tointeger(L, 5); + if(top > 5 && lua_toboolean(L, 6)) flag |= NO_FLIP_EFFECT; + if(pcard) { + card_set cset{ pcard }; + pduel->game_field->change_position(cset, pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, au, ad, du, dd, flag, TRUE); + } else + pduel->game_field->change_position(pgroup->container, pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, au, ad, du, dd, flag, TRUE); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_release(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t reason = (uint32_t)lua_tointeger(L, 2); + uint32_t reason_player = pduel->game_field->core.reason_player; + if (lua_gettop(L) >= 3) + reason_player = (uint32_t)lua_tointeger(L, 3); + if(pcard) + pduel->game_field->release(pcard, pduel->game_field->core.reason_effect, reason, reason_player); + else + pduel->game_field->release(pgroup->container, pduel->game_field->core.reason_effect, reason, reason_player); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_move_to_field(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t move_player = (uint32_t)lua_tointeger(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 3); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t destination = (uint32_t)lua_tointeger(L, 4); + uint32_t positions = (uint32_t)lua_tointeger(L, 5); + uint32_t enable = lua_toboolean(L, 6); + uint32_t zone = 0xff; + if(lua_gettop(L) > 6) + zone = (uint32_t)lua_tointeger(L, 7); + if(destination == LOCATION_FZONE) { + destination = LOCATION_SZONE; + zone = 0x1U << 5; + } + uint32_t pzone = FALSE; + if(destination == LOCATION_PZONE) { + destination = LOCATION_SZONE; + pzone = TRUE; + } + duel* pduel = pcard->pduel; + pcard->enable_field_effect(false); + pduel->game_field->adjust_instant(); + pduel->game_field->move_to_field(pcard, move_player, playerid, destination, positions, enable, 0, pzone, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_return_to_field(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + if(!(pcard->current.reason & REASON_TEMPORARY)) + return 0; + int32_t pos = pcard->previous.position; + if(lua_gettop(L) >= 2) + pos = (int32_t)lua_tointeger(L, 2); + uint32_t zone = 0xff; + if(lua_gettop(L) >= 3) + zone = (uint32_t)lua_tointeger(L, 3); + duel* pduel = pcard->pduel; + pcard->enable_field_effect(false); + pduel->game_field->adjust_instant(); + pduel->game_field->refresh_location_info_instant(); + pduel->game_field->move_to_field(pcard, pcard->previous.controler, pcard->previous.controler, pcard->previous.location, pos, TRUE, RETURN_TEMP_REMOVE_TO_FIELD, pcard->previous.pzone, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_move_sequence(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + int32_t seq = (int32_t)lua_tointeger(L, 2); + duel* pduel = pcard->pduel; + int32_t playerid = pcard->current.controler; + if(pcard->is_affect_by_effect(pduel->game_field->core.reason_effect)) { + pduel->game_field->move_card(playerid, pcard, pcard->current.location, seq); + pduel->game_field->raise_single_event(pcard, 0, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, playerid, 0); + pduel->game_field->raise_event(pcard, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, playerid, 0); + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + } + return 0; +} +int32_t scriptlib::duel_swap_sequence(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard1 = *(card**) lua_touserdata(L, 1); + card* pcard2 = *(card**) lua_touserdata(L, 2); + uint8_t player = pcard1->current.controler; + uint8_t location = pcard1->current.location; + duel* pduel = pcard1->pduel; + if(pcard2->current.controler == player + && location == LOCATION_MZONE && pcard2->current.location == location + && pcard1->is_affect_by_effect(pduel->game_field->core.reason_effect) + && pcard2->is_affect_by_effect(pduel->game_field->core.reason_effect)) { + pduel->game_field->swap_card(pcard1, pcard2); + card_set swapped; + swapped.insert(pcard1); + swapped.insert(pcard2); + pduel->game_field->raise_single_event(pcard1, 0, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, player, 0); + pduel->game_field->raise_single_event(pcard2, 0, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, player, 0); + pduel->game_field->raise_event(swapped, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, player, 0); + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + } + return 0; +} +int32_t scriptlib::duel_activate_effect(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**)lua_touserdata(L, 1); + duel* pduel = peffect->pduel; + pduel->game_field->add_process(PROCESSOR_ACTIVATE_EFFECT, 0, peffect, 0, 0, 0); + return 0; +} +int32_t scriptlib::duel_set_chain_limit(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_FUNCTION, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t f = interpreter::get_function_handle(L, 1); + pduel->game_field->core.chain_limit.emplace_back(f, pduel->game_field->core.reason_player); + return 0; +} +int32_t scriptlib::duel_set_chain_limit_p(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_FUNCTION, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t f = interpreter::get_function_handle(L, 1); + pduel->game_field->core.chain_limit_p.emplace_back(f, pduel->game_field->core.reason_player); + return 0; +} +int32_t scriptlib::duel_get_chain_material(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + effect_set eset; + pduel->game_field->filter_player_effect(playerid, EFFECT_CHAIN_MATERIAL, &eset); + if(!eset.size()) + return 0; + interpreter::effect2value(L, eset[0]); + return 1; +} +int32_t scriptlib::duel_confirm_decktop(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + if(count >= pduel->game_field->player[playerid].list_main.size()) + count = (uint32_t)pduel->game_field->player[playerid].list_main.size(); + else if(pduel->game_field->player[playerid].list_main.size() > count) { + if(pduel->game_field->core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + card* pcard = *(pduel->game_field->player[playerid].list_main.rbegin() + count); + if(pduel->game_field->core.deck_reversed) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + if(pcard->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(pcard->data.code); + else + pduel->write_buffer32(pcard->data.code | 0x80000000); + } + } + } + auto cit = pduel->game_field->player[playerid].list_main.rbegin(); + pduel->write_buffer8(MSG_CONFIRM_DECKTOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + card_set ccards; + uint32_t reason = 0; + if(lua_gettop(L) >= 3) + reason = lua_tointeger(L, 3); + for(uint32_t i = 0; i < count && cit != pduel->game_field->player[playerid].list_main.rend(); ++i, ++cit) { + pduel->write_buffer32((*cit)->data.code); + pduel->write_buffer8((*cit)->current.controler); + pduel->write_buffer8((*cit)->current.location); + pduel->write_buffer8((*cit)->current.sequence); + pduel->game_field->raise_single_event(*cit, 0, EVENT_CUSTOM+50000000, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, count); + ccards.insert(*cit); + } + pduel->game_field->raise_event(ccards, EVENT_CUSTOM+50000000, pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, count); + pduel->game_field->add_process(PROCESSOR_WAIT, 0, 0, 0, 0, 0); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_confirm_extratop(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + if(count >= pduel->game_field->player[playerid].list_extra.size() - pduel->game_field->player[playerid].extra_p_count) + count = (uint32_t)pduel->game_field->player[playerid].list_extra.size() - pduel->game_field->player[playerid].extra_p_count; + auto cit = pduel->game_field->player[playerid].list_extra.rbegin() + pduel->game_field->player[playerid].extra_p_count; + pduel->write_buffer8(MSG_CONFIRM_EXTRATOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + for(uint32_t i = 0; i < count && cit != pduel->game_field->player[playerid].list_extra.rend(); ++i, ++cit) { + pduel->write_buffer32((*cit)->data.code); + pduel->write_buffer8((*cit)->current.controler); + pduel->write_buffer8((*cit)->current.location); + pduel->write_buffer8((*cit)->current.sequence); + } + pduel->game_field->add_process(PROCESSOR_WAIT, 0, 0, 0, 0, 0); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_confirm_cards(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + pcard = *(card**) lua_touserdata(L, 2); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 2); + if(pgroup->container.size() == 0) + return 0; + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + uint8_t skip_panel = 0; + if(lua_gettop(L) >= 3) + skip_panel = lua_toboolean(L, 3); + pduel->write_buffer8(MSG_CONFIRM_CARDS); + pduel->write_buffer8(playerid); + pduel->write_buffer8(skip_panel); + if(pcard) { + pduel->write_buffer8(1); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } else { + pduel->write_buffer8((uint8_t)pgroup->container.size()); + for(auto& group_card : pgroup->container) { + pduel->write_buffer32(group_card->data.code); + pduel->write_buffer8(group_card->current.controler); + pduel->write_buffer8(group_card->current.location); + pduel->write_buffer8(group_card->current.sequence); + } + } + pduel->game_field->add_process(PROCESSOR_WAIT, 0, 0, 0, 0, 0); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_sort_decktop(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t sort_player = (int32_t)lua_tointeger(L, 1); + int32_t target_player = (int32_t)lua_tointeger(L, 2); + int32_t count = (int32_t)lua_tointeger(L, 3); + if (!check_playerid(sort_player)) + return 0; + if (!check_playerid(target_player)) + return 0; + if (count < 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_SORT_DECK, 0, 0, 0, sort_player + (target_player << 16), count); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_check_event(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t ev = (int32_t)lua_tointeger(L, 1); + int32_t get_info = lua_toboolean(L, 2); + if(!get_info) { + lua_pushboolean(L, pduel->game_field->check_event(ev)); + return 1; + } else { + tevent pe; + if(pduel->game_field->check_event(ev, &pe)) { + lua_pushboolean(L, 1); + interpreter::group2value(L, pe.event_cards); + lua_pushinteger(L, pe.event_player); + lua_pushinteger(L, pe.event_value); + interpreter::effect2value(L, pe.reason_effect); + lua_pushinteger(L, pe.reason); + lua_pushinteger(L, pe.reason_player); + return 7; + } else { + lua_pushboolean(L, 0); + return 1; + } + } +} +int32_t scriptlib::duel_raise_event(lua_State *L) { + check_action_permission(L); + check_param_count(L, 7); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + effect* peffect = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_EFFECT, 3); + peffect = *(effect**)lua_touserdata(L, 3); + } + uint32_t r = (uint32_t)lua_tointeger(L, 4); + uint32_t rp = (uint32_t)lua_tointeger(L, 5); + uint32_t ep = (uint32_t)lua_tointeger(L, 6); + uint32_t ev = (uint32_t)lua_tointeger(L, 7); + if(pcard) + pduel->game_field->raise_event(pcard, code, peffect, r, rp, ep, ev); + else + pduel->game_field->raise_event(pgroup->container, code, peffect, r, rp, ep, ev); + pduel->game_field->process_instant_event(); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_raise_single_event(lua_State *L) { + check_action_permission(L); + check_param_count(L, 7); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 2); + effect* peffect = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_EFFECT, 3); + peffect = *(effect**)lua_touserdata(L, 3); + } + uint32_t r = (uint32_t)lua_tointeger(L, 4); + uint32_t rp = (uint32_t)lua_tointeger(L, 5); + uint32_t ep = (uint32_t)lua_tointeger(L, 6); + uint32_t ev = (uint32_t)lua_tointeger(L, 7); + duel* pduel = pcard->pduel; + pduel->game_field->raise_single_event(pcard, 0, code, peffect, r, rp, ep, ev); + pduel->game_field->process_single_event(); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_check_timing(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t tm = (int32_t)lua_tointeger(L, 1); + lua_pushboolean(L, (pduel->game_field->core.hint_timing[0]&tm) || (pduel->game_field->core.hint_timing[1]&tm)); + return 1; +} +int32_t scriptlib::duel_is_environment(lua_State *L) { + check_param_count(L, 1); + uint32_t code = (uint32_t)lua_tointeger(L, 1); + int32_t playerid = PLAYER_ALL; + if(lua_gettop(L) >= 2) + playerid = (int32_t)lua_tointeger(L, 2); + uint32_t loc = LOCATION_ONFIELD; + if(lua_gettop(L) >= 3) + loc = (uint32_t)lua_tointeger(L, 3); + if(playerid != 0 && playerid != 1 && playerid != PLAYER_ALL) + return 0; + duel* pduel = interpreter::get_duel_info(L); + int32_t ret = 0, fc = 0; + if(loc & (LOCATION_FZONE + LOCATION_SZONE)) { + card* pcard = pduel->game_field->player[0].list_szone[5]; + if(pcard && pcard->is_position(POS_FACEUP)) { + fc = 1; + if(code == pcard->get_code() && (playerid == 0 || playerid == PLAYER_ALL)) + ret = 1; + } + pcard = pduel->game_field->player[1].list_szone[5]; + if(pcard && pcard->is_position(POS_FACEUP)) { + fc = 1; + if(code == pcard->get_code() && (playerid == 1 || playerid == PLAYER_ALL)) + ret = 1; + } + } + if(!ret && (loc & LOCATION_SZONE)) { + if(playerid == 0 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[0].list_szone) { + if(pcard && pcard->is_position(POS_FACEUP) && code == pcard->get_code()) + ret = 1; + } + } + if(playerid == 1 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[1].list_szone) { + if(pcard && pcard->is_position(POS_FACEUP) && code == pcard->get_code()) + ret = 1; + } + } + } + if(!ret && (loc & LOCATION_MZONE)) { + if(playerid == 0 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[0].list_mzone) { + if(pcard && pcard->is_position(POS_FACEUP) && code == pcard->get_code()) + ret = 1; + } + } + if(playerid == 1 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[1].list_mzone) { + if(pcard && pcard->is_position(POS_FACEUP) && code == pcard->get_code()) + ret = 1; + } + } + } + if(!ret && (loc & LOCATION_GRAVE)) { + if(playerid == 0 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[0].list_grave) { + if(code == pcard->get_code()) + ret = 1; + } + } + if(playerid == 1 || playerid == PLAYER_ALL) { + for(auto& pcard : pduel->game_field->player[1].list_grave) { + if(code == pcard->get_code()) + ret = 1; + } + } + } + if(!fc) { + effect_set eset; + pduel->game_field->filter_field_effect(EFFECT_CHANGE_ENVIRONMENT, &eset); + if(eset.size()) { + effect* peffect = eset.back(); + if(code == (uint32_t)peffect->get_value() && (playerid == peffect->get_handler_player() || playerid == PLAYER_ALL)) + ret = 1; + } + } + lua_pushboolean(L, ret); + return 1; +} +int32_t scriptlib::duel_win(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t reason = (uint32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1 && playerid != 2) + return 0; + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->core.win_player == 5) { + pduel->game_field->core.win_player = playerid; + pduel->game_field->core.win_reason = reason; + } + return 0; +} +int32_t scriptlib::duel_draw(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t count = (int32_t)lua_tointeger(L, 2); + if (count < 0) + count = 0; + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->draw(pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_damage(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t amount = (int32_t)lua_tonumber(L, 2); + if(amount < 0) + amount = 0; + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + uint32_t is_step = FALSE; + if(lua_gettop(L) >= 4) + is_step = lua_toboolean(L, 4); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->damage(pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, 0, playerid, amount, is_step); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_recover(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t amount = (int32_t)lua_tonumber(L, 2); + if(amount < 0) + amount = 0; + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + uint32_t is_step = FALSE; + if(lua_gettop(L) >= 4) + is_step = lua_toboolean(L, 4); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->recover(pduel->game_field->core.reason_effect, reason, pduel->game_field->core.reason_player, playerid, amount, is_step); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_rd_complete(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.subunits.splice(pduel->game_field->core.subunits.end(), pduel->game_field->core.recover_damage_reserve); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_equip(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 2); + check_param(L, PARAM_TYPE_CARD, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* equip_card = *(card**) lua_touserdata(L, 2); + card* target = *(card**) lua_touserdata(L, 3); + uint32_t up = TRUE; + if(lua_gettop(L) > 3) + up = lua_toboolean(L, 4); + uint32_t step = FALSE; + if(lua_gettop(L) > 4) + step = lua_toboolean(L, 5); + duel* pduel = target->pduel; + pduel->game_field->equip(playerid, equip_card, target, up, step); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_equip_complete(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + card_set etargets; + for(auto& equip_card : pduel->game_field->core.equiping_cards) { + if(equip_card->is_position(POS_FACEUP)) + equip_card->enable_field_effect(true); + etargets.insert(equip_card->equiping_target); + } + pduel->game_field->adjust_instant(); + for(auto& equip_target : etargets) + pduel->game_field->raise_single_event(equip_target, &pduel->game_field->core.equiping_cards, EVENT_EQUIP, + pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, PLAYER_NONE, 0); + pduel->game_field->raise_event(pduel->game_field->core.equiping_cards, EVENT_EQUIP, + pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, PLAYER_NONE, 0); + pduel->game_field->core.hint_timing[0] |= TIMING_EQUIP; + pduel->game_field->core.hint_timing[1] |= TIMING_EQUIP; + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_get_control(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t reset_phase = 0; + uint32_t reset_count = 0; + if(lua_gettop(L) >= 3) { + reset_phase = lua_tointeger(L, 3) & 0x3ff; + reset_count = lua_tointeger(L, 4) & 0xff; + } + uint32_t zone = 0xff; + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + if(pcard) + pduel->game_field->get_control(pcard, pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, playerid, reset_phase, reset_count, zone); + else + pduel->game_field->get_control(pgroup->container, pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, playerid, reset_phase, reset_count, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_swap_control(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + card* pcard1 = nullptr; + card* pcard2 = nullptr; + group* pgroup1 = nullptr; + group* pgroup2 = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE) && check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + pcard1 = *(card**) lua_touserdata(L, 1); + pcard2 = *(card**) lua_touserdata(L, 2); + pduel = pcard1->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE) && check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + pgroup1 = *(group**) lua_touserdata(L, 1); + pgroup2 = *(group**) lua_touserdata(L, 2); + pduel = pgroup1->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t reset_phase = 0; + uint32_t reset_count = 0; + if(lua_gettop(L) > 2) { + reset_phase = lua_tointeger(L, 3) & 0x3ff; + reset_count = lua_tointeger(L, 4) & 0xff; + } + if(pcard1) + pduel->game_field->swap_control(pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, pcard1, pcard2, reset_phase, reset_count); + else + pduel->game_field->swap_control(pduel->game_field->core.reason_effect, pduel->game_field->core.reason_player, pgroup1->container, pgroup2->container, reset_phase, reset_count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_check_lp_cost(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t cost = (int32_t)lua_tointeger(L, 2); + if(cost <= 0) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + uint32_t must_pay = FALSE; + if(lua_gettop(L) > 2) + must_pay = lua_toboolean(L, 3); + lua_pushboolean(L, pduel->game_field->check_lp_cost(playerid, cost, must_pay)); + return 1; +} +int32_t scriptlib::duel_pay_lp_cost(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t cost = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + uint32_t must_pay = FALSE; + if(lua_gettop(L) > 2) + must_pay = lua_toboolean(L, 3); + pduel->game_field->add_process(PROCESSOR_PAY_LPCOST, 0, 0, 0, playerid, cost, must_pay); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_discard_deck(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 2); + uint32_t reason = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_DISCARD_DECK, 0, 0, 0, playerid + (count << 16), reason); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_discard_hand(lua_State *L) { + check_action_permission(L); + check_param_count(L, 5); + if(!lua_isnil(L, 2)) + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pexception = nullptr; + group* pexgroup = nullptr; + uint32_t extraargs = 0; + if(lua_gettop(L) >= 6) { + if(check_param(L, PARAM_TYPE_CARD, 6, TRUE)) + pexception = *(card**) lua_touserdata(L, 6); + else if(check_param(L, PARAM_TYPE_GROUP, 6, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 6); + extraargs = lua_gettop(L) - 6; + } + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + uint32_t reason = (uint32_t)lua_tointeger(L, 5); + group* pgroup = pduel->new_group(); + pduel->game_field->filter_matching_card(L, 2, playerid, LOCATION_HAND, 0, pgroup, pexception, pexgroup, extraargs); + pduel->game_field->core.select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + if(pduel->game_field->core.select_cards.size() == 0) { + lua_pushinteger(L, 0); + return 1; + } + pduel->game_field->add_process(PROCESSOR_DISCARD_HAND, 0, nullptr, nullptr, playerid, min + (max << 16), reason); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_disable_shuffle_check(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + uint8_t disable = TRUE; + if(lua_gettop(L) > 0) + disable = lua_toboolean(L, 1); + pduel->game_field->core.shuffle_check_disabled = disable; + return 0; +} +int32_t scriptlib::duel_disable_self_destroy_check(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + uint8_t disable = TRUE; + if(lua_gettop(L) > 0) + disable = lua_toboolean(L, 1); + pduel->game_field->core.selfdes_disabled = disable; + return 0; +} +int32_t scriptlib::duel_reveal_select_deck_sequence(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + uint8_t reveal = TRUE; + if(lua_gettop(L) > 0) + reveal = lua_toboolean(L, 1); + pduel->game_field->core.select_deck_sequence_revealed = reveal; + return 0; +} +int32_t scriptlib::duel_shuffle_deck(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->shuffle(playerid, LOCATION_DECK); + return 0; +} +int32_t scriptlib::duel_shuffle_extra(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if (playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->shuffle(playerid, LOCATION_EXTRA); + return 0; +} +int32_t scriptlib::duel_shuffle_hand(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->shuffle(playerid, LOCATION_HAND); + return 0; +} +int32_t scriptlib::duel_shuffle_setcard(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**)lua_touserdata(L, 1); + if(pgroup->container.size() <= 1) + return 0; + duel* pduel = pgroup->pduel; + card* ms[5]{}; + uint8_t seq[5]{}; + uint8_t tp = 2; + uint8_t loc = 0; + uint8_t ct = 0; + for(auto& pcard : pgroup->container) { + if((loc != 0 && (pcard->current.location != loc)) || (pcard->current.location != LOCATION_MZONE && pcard->current.location != LOCATION_SZONE) + || (pcard->current.position & POS_FACEUP) || (pcard->current.sequence > 4) || (tp != 2 && (pcard->current.controler != tp))) + return 0; + tp = pcard->current.controler; + loc = pcard->current.location; + ms[ct] = pcard; + seq[ct] = pcard->current.sequence; + ++ct; + } + for(int32_t i = ct - 1; i > 0; --i) { + int32_t s = pduel->get_next_integer(0, i); + std::swap(ms[i], ms[s]); + } + pduel->write_buffer8(MSG_SHUFFLE_SET_CARD); + pduel->write_buffer8(loc); + pduel->write_buffer8(ct); + for(int32_t i = 0; i < ct; ++i) { + card* pcard = ms[i]; + pduel->write_buffer32(pcard->get_info_location()); + if(loc == LOCATION_MZONE) + pduel->game_field->player[tp].list_mzone[seq[i]] = pcard; + else + pduel->game_field->player[tp].list_szone[seq[i]] = pcard; + pcard->current.sequence = seq[i]; + pduel->game_field->raise_single_event(pcard, 0, EVENT_MOVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, tp, 0); + } + pduel->game_field->raise_event(pgroup->container, EVENT_MOVE, pduel->game_field->core.reason_effect, 0, pduel->game_field->core.reason_player, tp, 0); + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + for(int32_t i = 0; i < ct; ++i) { + if(ms[i]->xyz_materials.size()) + pduel->write_buffer32(ms[i]->get_info_location()); + else + pduel->write_buffer32(0); + } + return 0; +} +int32_t scriptlib::duel_change_attacker(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* attacker = *(card**) lua_touserdata(L, 1); + int32_t ignore_count = FALSE; + if(lua_gettop(L) >= 2) + ignore_count = lua_toboolean(L, 2); + duel* pduel = attacker->pduel; + if(pduel->game_field->core.attacker == attacker) + return 0; + card* attack_target = pduel->game_field->core.attack_target; + ++pduel->game_field->core.attacker->announce_count; + pduel->game_field->core.attacker->announced_cards.addcard(attack_target); + pduel->game_field->attack_all_target_check(); + pduel->game_field->core.attacker = attacker; + attacker->attack_controler = attacker->current.controler; + pduel->game_field->core.pre_field[0] = attacker->fieldid_r; + if(!ignore_count) { + ++attacker->attack_announce_count; + if(pduel->game_field->infos.phase == PHASE_DAMAGE) { + ++attacker->attacked_count; + attacker->attacked_cards.addcard(attack_target); + } + } + return 0; +} +int32_t scriptlib::duel_change_attack_target(lua_State *L) { + check_param_count(L, 1); + duel* pduel; + card* target; + if(lua_isnil(L, 1)) { + pduel = interpreter::get_duel_info(L); + target = 0; + } else { + check_param(L, PARAM_TYPE_CARD, 1); + target = *(card**)lua_touserdata(L, 1); + pduel = target->pduel; + } + card* attacker = pduel->game_field->core.attacker; + if(!attacker || !attacker->is_capable_attack() || attacker->is_status(STATUS_ATTACK_CANCELED)) { + lua_pushboolean(L, 0); + return 1; + } + card_vector cv; + pduel->game_field->get_attack_target(attacker, &cv, pduel->game_field->core.chain_attack); + if(target && std::find(cv.begin(), cv.end(), target) != cv.end() + || !target && !attacker->is_affected_by_effect(EFFECT_CANNOT_DIRECT_ATTACK)) { + pduel->game_field->core.attack_target = target; + pduel->game_field->core.attack_rollback = FALSE; + pduel->game_field->core.opp_mzone.clear(); + uint8_t turnp = pduel->game_field->infos.turn_player; + for(auto& pcard : pduel->game_field->player[1 - turnp].list_mzone) { + if(pcard) + pduel->game_field->core.opp_mzone.insert(pcard->fieldid_r); + } + pduel->write_buffer8(MSG_ATTACK); + pduel->write_buffer32(attacker->get_info_location()); + if(target) { + pduel->game_field->raise_single_event(target, 0, EVENT_BE_BATTLE_TARGET, 0, REASON_REPLACE, 0, 1 - turnp, 0); + pduel->game_field->raise_event(target, EVENT_BE_BATTLE_TARGET, 0, REASON_REPLACE, 0, 1 - turnp, 0); + pduel->game_field->process_single_event(); + pduel->game_field->process_instant_event(); + pduel->write_buffer32(target->get_info_location()); + } else { + pduel->game_field->core.attack_player = TRUE; + pduel->write_buffer32(0); + } + lua_pushboolean(L, 1); + } else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_calculate_damage(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* attacker = *(card**)lua_touserdata(L, 1); + card* attack_target; + if(lua_isnil(L, 2)) + attack_target = 0; + else { + check_param(L, PARAM_TYPE_CARD, 2); + attack_target = *(card**)lua_touserdata(L, 2); + } + int32_t new_attack = FALSE; + if(lua_gettop(L) >= 3) + new_attack = lua_toboolean(L, 3); + if(attacker == attack_target) + return 0; + attacker->pduel->game_field->add_process(PROCESSOR_DAMAGE_STEP, 0, (effect*)attacker, (group*)attack_target, 0, new_attack); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_get_battle_damage(lua_State *L) { + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + lua_pushinteger(L, pduel->game_field->core.battle_damage[playerid]); + return 1; +} +int32_t scriptlib::duel_change_battle_damage(lua_State *L) { + check_param_count(L, 2); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t dam = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t check = TRUE; + if(lua_gettop(L) >= 3) + check = lua_toboolean(L, 3); + if(check && pduel->game_field->core.battle_damage[playerid] == 0) + return 0; + pduel->game_field->core.battle_damage[playerid] = dam; + return 0; +} +int32_t scriptlib::duel_change_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 1); + group* pgroup = *(group**)lua_touserdata(L, 2); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->change_target(count, pgroup); + return 0; +} +int32_t scriptlib::duel_change_target_player(lua_State *L) { + check_param_count(L, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->change_target_player(count, playerid); + return 0; +} +int32_t scriptlib::duel_change_target_param(lua_State *L) { + check_param_count(L, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 1); + uint32_t param = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->change_target_param(count, param); + return 0; +} +int32_t scriptlib::duel_break_effect(lua_State *L) { + check_action_permission(L); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->break_effect(); + pduel->game_field->raise_event(nullptr, EVENT_BREAK_EFFECT, 0, 0, PLAYER_NONE, PLAYER_NONE, 0); + pduel->game_field->process_instant_event(); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_change_effect(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_FUNCTION, 2); + duel* pduel = interpreter::get_duel_info(L); + uint32_t count = (uint32_t)lua_tointeger(L, 1); + int32_t pf = interpreter::get_function_handle(L, 2); + pduel->game_field->change_chain_effect(count, pf); + return 0; +} +int32_t scriptlib::duel_negate_activate(lua_State *L) { + check_param_count(L, 1); + uint32_t c = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->negate_chain(c)); + return 1; +} +int32_t scriptlib::duel_negate_effect(lua_State *L) { + check_param_count(L, 1); + uint32_t c = (uint32_t)lua_tointeger(L, 1); + uint8_t forced = FALSE; + if(lua_gettop(L) > 1) + forced = lua_toboolean(L, 2); + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->disable_chain(c, forced)); + return 1; +} +int32_t scriptlib::duel_negate_related_chain(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**)lua_touserdata(L, 1); + uint32_t reset_flag = (uint32_t)lua_tointeger(L, 2); + duel* pduel = pcard->pduel; + if(pduel->game_field->core.current_chain.size() < 2) + return 0; + if(!pcard->is_affect_by_effect(pduel->game_field->core.reason_effect)) + return 0; + for(auto it = pduel->game_field->core.current_chain.rbegin(); it != pduel->game_field->core.current_chain.rend(); ++it) { + if(it->triggering_effect->get_handler() == pcard && pcard->is_has_relation(*it)) { + effect* negeff = pduel->new_effect(); + negeff->owner = pduel->game_field->core.reason_effect->get_handler(); + negeff->type = EFFECT_TYPE_SINGLE; + negeff->code = EFFECT_DISABLE_CHAIN; + negeff->value = it->chain_id; + negeff->reset_flag = RESET_CHAIN | RESET_EVENT | reset_flag; + pcard->add_effect(negeff); + } + } + return 0; +} +int32_t scriptlib::duel_disable_summon(lua_State *L) { + check_param_count(L, 1); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**)lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**)lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + uint32_t sumtype = 0; + if (pduel->game_field->check_event(EVENT_SUMMON)) { + if (pduel->game_field->core.is_gemini_summoning) + sumtype = SUMMON_TYPE_DUAL; + else + sumtype = SUMMON_TYPE_NORMAL; + } + else if (pduel->game_field->check_event(EVENT_FLIP_SUMMON)) + sumtype = SUMMON_TYPE_FLIP; + else if (pduel->game_field->check_event(EVENT_SPSUMMON)) + sumtype = SUMMON_TYPE_SPECIAL; + else + return 0; + if (sumtype & SUMMON_TYPE_NORMAL || sumtype & SUMMON_TYPE_FLIP) { + if (pgroup && pgroup->container.size() != 1) + return 0; + if (!pcard) + pcard = *pgroup->container.begin(); + } + uint8_t sumplayer = PLAYER_NONE; + effect* reason_effect = pduel->game_field->core.reason_effect; + card_set negated_cards; + if (sumtype == SUMMON_TYPE_DUAL || sumtype & SUMMON_TYPE_FLIP) { + if (!pcard->is_summon_negatable(sumtype, reason_effect)) + return 0; + sumplayer = pcard->summon_player; + pcard->set_status(STATUS_FLIP_SUMMONING, FALSE); + pcard->set_status(STATUS_FLIP_SUMMON_DISABLED, TRUE); + } + else { + if (pcard) { + if (!pcard->is_summon_negatable(sumtype, reason_effect)) + return 0; + sumplayer = pcard->summon_player; + pcard->set_status(STATUS_SUMMONING, FALSE); + pcard->set_status(STATUS_SUMMON_DISABLED, TRUE); + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + } + else { + for (auto& group_card : pgroup->container) { + if (!group_card->is_summon_negatable(sumtype, reason_effect)) + continue; + sumplayer = group_card->summon_player; + group_card->set_status(STATUS_SUMMONING, FALSE); + group_card->set_status(STATUS_SUMMON_DISABLED, TRUE); + group_card->set_status(STATUS_PROC_COMPLETE, FALSE); + negated_cards.insert(group_card); + } + if (!negated_cards.size()) + return 0; + } + } + pduel->game_field->core.is_summon_negated = true; + uint32_t event_code = 0; + if(sumtype & SUMMON_TYPE_NORMAL) + event_code = EVENT_SUMMON_NEGATED; + else if(sumtype & SUMMON_TYPE_FLIP) + event_code = EVENT_FLIP_SUMMON_NEGATED; + else if(sumtype & SUMMON_TYPE_SPECIAL) + event_code = EVENT_SPSUMMON_NEGATED; + uint8_t reason_player = pduel->game_field->core.reason_player; + if(pcard) + pduel->game_field->raise_event(pcard, event_code, reason_effect, REASON_EFFECT, reason_player, sumplayer, 0); + else + pduel->game_field->raise_event(pgroup->container, event_code, reason_effect, REASON_EFFECT, reason_player, sumplayer, 0); + pduel->game_field->process_instant_event(); + return 0; +} +int32_t scriptlib::duel_increase_summon_count(lua_State *L) { + card* pcard = nullptr; + effect* pextra = nullptr; + if(lua_gettop(L) > 0) { + check_param(L, PARAM_TYPE_CARD, 1); + pcard = *(card**) lua_touserdata(L, 1); + } + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = pduel->game_field->core.reason_player; + if(pcard && (pextra = pcard->is_affected_by_effect(EFFECT_EXTRA_SUMMON_COUNT))) + pextra->get_value(pcard); + else + ++pduel->game_field->core.summon_count[playerid]; + return 0; +} +int32_t scriptlib::duel_check_summon_count(lua_State *L) { + card* pcard = nullptr; + if(lua_gettop(L) > 0) { + check_param(L, PARAM_TYPE_CARD, 1); + pcard = *(card**) lua_touserdata(L, 1); + } + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = pduel->game_field->core.reason_player; + if((pcard && pcard->is_affected_by_effect(EFFECT_EXTRA_SUMMON_COUNT)) + || pduel->game_field->core.summon_count[playerid] < pduel->game_field->get_summon_count_limit(playerid)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +// Return usable count in zone of playerid's Main MZONE or SZONE(0~4) when uplayer moves a card to playerid's field (can be negative). +int32_t scriptlib::duel_get_location_count(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t location = (uint32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + uint32_t uplayer = pduel->game_field->core.reason_player; + uint32_t reason = LOCATION_REASON_TOFIELD; + if(lua_gettop(L) >= 3 && !lua_isnil(L,3)) + uplayer = (uint32_t)lua_tointeger(L, 3); + if(lua_gettop(L) >= 4) + reason = (uint32_t)lua_tointeger(L, 4); + uint32_t zone = 0xff; + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + uint32_t list = 0; + lua_pushinteger(L, pduel->game_field->get_useable_count(nullptr, playerid, location, uplayer, reason, zone, &list)); + lua_pushinteger(L, list); + return 2; +} +// Return usable count in zone of playerid's Main MZONE after mcard or mgroup leaves the field. +int32_t scriptlib::duel_get_mzone_count(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + bool swapped = false; + card* mcard = nullptr; + group* mgroup = nullptr; + uint32_t used_location[2] = { 0, 0 }; + card_vector list_mzone[2]; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + mcard = *(card**)lua_touserdata(L, 2); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + mgroup = *(group**)lua_touserdata(L, 2); + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + for(int32_t p = 0; p < 2; p++) { + uint32_t digit = 0x1U; + for(auto& pcard : pduel->game_field->player[p].list_mzone) { + if(pcard && pcard != mcard && !(mgroup && mgroup->container.find(pcard) != mgroup->container.end())) { + used_location[p] |= digit; + list_mzone[p].push_back(pcard); + } else + list_mzone[p].push_back(nullptr); + digit <<= 1; + } + used_location[p] |= pduel->game_field->player[p].used_location & 0xff00; + std::swap(used_location[p], pduel->game_field->player[p].used_location); + pduel->game_field->player[p].list_mzone.swap(list_mzone[p]); + } + swapped = true; + } + uint32_t uplayer = pduel->game_field->core.reason_player; + uint32_t reason = LOCATION_REASON_TOFIELD; + uint32_t zone = 0xff; + if(lua_gettop(L) >= 3) + uplayer = (uint32_t)lua_tointeger(L, 3); + if(lua_gettop(L) >= 4) + reason = (uint32_t)lua_tointeger(L, 4); + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + uint32_t list = 0; + lua_pushinteger(L, pduel->game_field->get_useable_count(nullptr, playerid, LOCATION_MZONE, uplayer, reason, zone, &list)); + lua_pushinteger(L, list); + if(swapped) { + pduel->game_field->player[0].used_location = used_location[0]; + pduel->game_field->player[1].used_location = used_location[1]; + pduel->game_field->player[0].list_mzone.swap(list_mzone[0]); + pduel->game_field->player[1].list_mzone.swap(list_mzone[1]); + } + return 2; +} +// Return usable count in zone of playerid's SZONE after card or group leaves the field. +int32_t scriptlib::duel_get_szone_count(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + bool swapped = false; + card* mcard = nullptr; + group* mgroup = nullptr; + uint32_t used_location[2] = { 0, 0 }; + card_vector list_szone[2]; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + mcard = *(card**)lua_touserdata(L, 2); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + mgroup = *(group**)lua_touserdata(L, 2); + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + for(int32_t p = 0; p < 2; p++) { + uint32_t digit = 0x100U; + for(auto& pcard : pduel->game_field->player[p].list_szone) { + if(pcard && pcard != mcard && !(mgroup && mgroup->container.find(pcard) != mgroup->container.end())) { + used_location[p] |= digit; + list_szone[p].push_back(pcard); + } else + list_szone[p].push_back(nullptr); + digit <<= 1; + } + used_location[p] |= pduel->game_field->player[p].used_location & 0xff; + std::swap(used_location[p], pduel->game_field->player[p].used_location); + pduel->game_field->player[p].list_szone.swap(list_szone[p]); + } + swapped = true; + } + uint32_t uplayer = pduel->game_field->core.reason_player; + uint32_t reason = LOCATION_REASON_TOFIELD; + uint32_t zone = 0xff; + if(lua_gettop(L) >= 3) + uplayer = (uint32_t)lua_tointeger(L, 3); + if(lua_gettop(L) >= 4) + reason = (uint32_t)lua_tointeger(L, 4); + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + uint32_t list = 0; + lua_pushinteger(L, pduel->game_field->get_useable_count(nullptr, playerid, LOCATION_SZONE, uplayer, reason, zone, &list)); + lua_pushinteger(L, list); + if(swapped) { + pduel->game_field->player[0].used_location = used_location[0]; + pduel->game_field->player[1].used_location = used_location[1]; + pduel->game_field->player[0].list_szone.swap(list_szone[0]); + pduel->game_field->player[1].list_szone.swap(list_szone[1]); + } + return 2; +} +// Condition: uplayer moves scard or any card with type from Extra Deck to playerid's field +// Return usable count in zone of playerid's MZONE after mcard or mgroup leaves the field +int32_t scriptlib::duel_get_location_count_fromex(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + uint32_t uplayer = pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) + uplayer = (uint32_t)lua_tointeger(L, 2); + bool swapped = false; + card* mcard = nullptr; + group* mgroup = nullptr; + uint32_t used_location[2] = {0, 0}; + card_vector list_mzone[2]; + if(lua_gettop(L) >= 3 && !lua_isnil(L, 3)) { + if(check_param(L, PARAM_TYPE_CARD, 3, TRUE)) { + mcard = *(card**) lua_touserdata(L, 3); + mcard->to_leave_fromex = TRUE; + } else if(check_param(L, PARAM_TYPE_GROUP, 3, TRUE)) { + mgroup = *(group**) lua_touserdata(L, 3); + for(auto cit = mgroup->container.begin(); cit != mgroup->container.end(); ++cit) { + card* gcard = *cit; + gcard->to_leave_fromex = TRUE; + } + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 3); + for(int32_t p = 0; p < 2; p++) { + uint32_t digit = 1; + for(auto& pcard : pduel->game_field->player[p].list_mzone) { + if(pcard && pcard != mcard && !(mgroup && mgroup->container.find(pcard) != mgroup->container.end())) { + used_location[p] |= digit; + list_mzone[p].push_back(pcard); + } else + list_mzone[p].push_back(nullptr); + digit <<= 1; + } + used_location[p] |= pduel->game_field->player[p].used_location & 0xff00; + std::swap(used_location[p], pduel->game_field->player[p].used_location); + pduel->game_field->player[p].list_mzone.swap(list_mzone[p]); + } + swapped = true; + } + bool use_temp_card = false; + card* scard = nullptr; + if(lua_gettop(L) >= 4 && !lua_isnil(L, 4)) { + if(check_param(L, PARAM_TYPE_CARD, 4, TRUE)) { + scard = *(card**)lua_touserdata(L, 4); + } else { + use_temp_card = true; + uint32_t type = (uint32_t)lua_tointeger(L, 4); + scard = pduel->game_field->temp_card; + scard->current.location = LOCATION_EXTRA; + scard->data.type = TYPE_MONSTER | type; + if(type & TYPE_PENDULUM) + scard->current.position = POS_FACEUP_DEFENSE; + else + scard->current.position = POS_FACEDOWN_DEFENSE; + } + } + uint32_t zone = 0xff; + if(lua_gettop(L) >= 5) + zone = (uint32_t)lua_tointeger(L, 5); + uint32_t list = 0; + lua_pushinteger(L, pduel->game_field->get_useable_count_fromex(scard, playerid, uplayer, zone, &list)); + lua_pushinteger(L, list); + if(mcard) + mcard->to_leave_fromex = FALSE; + if(mgroup){ + for(auto cit = mgroup->container.begin(); cit != mgroup->container.end(); ++cit) { + card* gcard = *cit; + gcard->to_leave_fromex = FALSE; + } + } + if(swapped) { + pduel->game_field->player[0].used_location = used_location[0]; + pduel->game_field->player[1].used_location = used_location[1]; + pduel->game_field->player[0].list_mzone.swap(list_mzone[0]); + pduel->game_field->player[1].list_mzone.swap(list_mzone[1]); + } + if(use_temp_card) { + scard->current.location = 0; + scard->data.type = 0; + scard->current.position = 0; + } + return 2; +} +// Return the number of available slots in the Main MZONE and Extra MZONE of `playerid`. +int32_t scriptlib::duel_get_usable_mzone_count(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + uint32_t uplayer = pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 2) + uplayer = (uint32_t)lua_tointeger(L, 2); + uint32_t zone = 0xff; + uint32_t flag1, flag2; + int32_t ct1 = pduel->game_field->get_tofield_count(nullptr, playerid, LOCATION_MZONE, uplayer, LOCATION_REASON_TOFIELD, zone, &flag1); + int32_t ct2 = pduel->game_field->get_spsummonable_count_fromex(nullptr, playerid, uplayer, zone, &flag2); + int32_t ct3 = field::field_used_count[~(flag1 | flag2) & 0x1f]; + int32_t count = ct1 + ct2 - ct3; + int32_t limit = pduel->game_field->get_mzone_limit(playerid, uplayer, LOCATION_REASON_TOFIELD); + if(count > limit) + count = limit; + lua_pushinteger(L, count); + return 1; +} +int32_t scriptlib::duel_get_linked_group(lua_State *L) { + check_param_count(L, 3); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + card_set cset; + pduel->game_field->get_linked_cards(rplayer, s, o, &cset); + group* pgroup = pduel->new_group(cset); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_get_linked_group_count(lua_State *L) { + check_param_count(L, 3); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + card_set cset; + pduel->game_field->get_linked_cards(rplayer, s, o, &cset); + lua_pushinteger(L, cset.size()); + return 1; +} +int32_t scriptlib::duel_get_linked_zone(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->get_linked_zone(playerid)); + return 1; +} +int32_t scriptlib::duel_get_field_card(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t location = (uint32_t)lua_tointeger(L, 2); + uint32_t sequence = (uint32_t)lua_tointeger(L, 3); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + card* pcard = pduel->game_field->get_field_card(playerid, location, sequence); + if(!pcard || pcard->get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP)) + return 0; + interpreter::card2value(L, pcard); + return 1; +} +int32_t scriptlib::duel_check_location(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t location = (uint32_t)lua_tointeger(L, 2); + uint32_t sequence = (uint32_t)lua_tointeger(L, 3); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_location_useable(playerid, location, sequence)); + return 1; +} +int32_t scriptlib::duel_get_current_chain(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->core.current_chain.size()); + return 1; +} +int32_t scriptlib::duel_get_ready_chain(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + int32_t size = (int32_t)pduel->game_field->core.current_chain.size(); + if (size && !pduel->game_field->core.is_target_ready) { + --size; + } + lua_pushinteger(L, size); + return 1; +} +int32_t scriptlib::duel_get_chain_info(lua_State *L) { + check_param_count(L, 1); + uint32_t c = (uint32_t)lua_tointeger(L, 1); + uint32_t args = lua_gettop(L) - 1; + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(c); + if(!ch) + return 0; + for(uint32_t i = 0; i < args; ++i) { + uint32_t flag = (uint32_t)lua_tointeger(L, 2 + i); + switch(flag) { + case CHAININFO_CHAIN_COUNT: + lua_pushinteger(L, ch->chain_count); + break; + case CHAININFO_TRIGGERING_EFFECT: + interpreter::effect2value(L, ch->triggering_effect); + break; + case CHAININFO_TRIGGERING_PLAYER: + lua_pushinteger(L, ch->triggering_player); + break; + case CHAININFO_TRIGGERING_CONTROLER: + lua_pushinteger(L, ch->triggering_controler); + break; + case CHAININFO_TRIGGERING_LOCATION: + lua_pushinteger(L, ch->triggering_location); + break; + case CHAININFO_TRIGGERING_SEQUENCE: + lua_pushinteger(L, ch->triggering_sequence); + break; + case CHAININFO_TRIGGERING_POSITION: + lua_pushinteger(L, ch->triggering_position); + break; + case CHAININFO_TRIGGERING_CODE: + lua_pushinteger(L, ch->triggering_state.code); + break; + case CHAININFO_TRIGGERING_CODE2: + lua_pushinteger(L, ch->triggering_state.code2); + break; + case CHAININFO_TRIGGERING_LEVEL: + lua_pushinteger(L, ch->triggering_state.level); + break; + case CHAININFO_TRIGGERING_RANK: + lua_pushinteger(L, ch->triggering_state.rank); + break; + case CHAININFO_TRIGGERING_ATTRIBUTE: + lua_pushinteger(L, ch->triggering_state.attribute); + break; + case CHAININFO_TRIGGERING_RACE: + lua_pushinteger(L, ch->triggering_state.race); + break; + case CHAININFO_TRIGGERING_ATTACK: + lua_pushinteger(L, ch->triggering_state.attack); + break; + case CHAININFO_TRIGGERING_DEFENSE: + lua_pushinteger(L, ch->triggering_state.defense); + break; + case CHAININFO_TARGET_CARDS: + interpreter::group2value(L, ch->target_cards); + break; + case CHAININFO_TARGET_PLAYER: + lua_pushinteger(L, ch->target_player); + break; + case CHAININFO_TARGET_PARAM: + lua_pushinteger(L, ch->target_param); + break; + case CHAININFO_DISABLE_REASON: + interpreter::effect2value(L, ch->disable_reason); + break; + case CHAININFO_DISABLE_PLAYER: + lua_pushinteger(L, ch->disable_player); + break; + case CHAININFO_CHAIN_ID: + lua_pushinteger(L, ch->chain_id); + break; + case CHAININFO_TYPE: + if((ch->triggering_effect->card_type & (TYPE_MONSTER | TYPE_SPELL | TYPE_TRAP)) == (TYPE_TRAP | TYPE_MONSTER)) + lua_pushinteger(L, TYPE_MONSTER); + else lua_pushinteger(L, (ch->triggering_effect->card_type & (TYPE_MONSTER | TYPE_SPELL | TYPE_TRAP))); + break; + case CHAININFO_EXTTYPE: + lua_pushinteger(L, ch->triggering_effect->card_type); + break; + default: + lua_pushnil(L); + break; + } + } + return args; +} +int32_t scriptlib::duel_get_chain_event(lua_State *L) { + check_param_count(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(count); + if(!ch) + return 0; + interpreter::group2value(L, ch->evt.event_cards); + lua_pushinteger(L, ch->evt.event_player); + lua_pushinteger(L, ch->evt.event_value); + interpreter::effect2value(L, ch->evt.reason_effect); + lua_pushinteger(L, ch->evt.reason); + lua_pushinteger(L, ch->evt.reason_player); + return 6; +} +int32_t scriptlib::duel_get_first_target(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(0); + if(!ch || !ch->target_cards || ch->target_cards->container.size() == 0) + return 0; + for(auto& pcard : ch->target_cards->container) + interpreter::card2value(L, pcard); + return (int32_t)ch->target_cards->container.size(); +} +int32_t scriptlib::duel_get_targets_relate_to_chain(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + chain* ch = pduel->game_field->get_chain(0); + if(ch && ch->target_cards && ch->target_cards->container.size() > 0) { + for(auto& pcard : ch->target_cards->container) { + if(pcard->is_has_relation(*ch)) { + pgroup->container.insert(pcard); + } + } + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_is_phase(lua_State *L) { + check_param_count(L, 1); + uint32_t pphase = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->infos.phase == pphase) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_is_main_phase(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + uint16_t phase = pduel->game_field->infos.phase; + if(phase == PHASE_MAIN1 || phase == PHASE_MAIN2) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_is_battle_phase(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + uint16_t phase = pduel->game_field->infos.phase; + if((phase >= PHASE_BATTLE_START) && (phase <= PHASE_BATTLE)) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_get_current_phase(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->infos.phase); + return 1; +} +int32_t scriptlib::duel_skip_phase(lua_State *L) { + check_param_count(L, 4); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t phase = (uint32_t)lua_tointeger(L, 2); + uint32_t reset = (uint32_t)lua_tointeger(L, 3); + int32_t count = (int32_t)lua_tointeger(L, 4); + int32_t value = (int32_t)lua_tointeger(L, 5); + if(count <= 0) + count = 1; + duel* pduel = interpreter::get_duel_info(L); + int32_t code = 0; + if(phase == PHASE_DRAW) + code = EFFECT_SKIP_DP; + else if(phase == PHASE_STANDBY) + code = EFFECT_SKIP_SP; + else if(phase == PHASE_MAIN1) + code = EFFECT_SKIP_M1; + else if(phase == PHASE_BATTLE) + code = EFFECT_SKIP_BP; + else if(phase == PHASE_MAIN2) + code = EFFECT_SKIP_M2; + else if(phase == PHASE_END) + code = EFFECT_SKIP_EP; + else + return 0; + effect* peffect = pduel->new_effect(); + peffect->owner = pduel->game_field->temp_card; + peffect->effect_owner = playerid; + peffect->type = EFFECT_TYPE_FIELD; + peffect->code = code; + peffect->reset_flag = (reset & 0x3ff) | RESET_PHASE | RESET_SELF_TURN; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_PLAYER_TARGET; + peffect->s_range = 1; + peffect->o_range = 0; + peffect->reset_count = count; + peffect->value = value; + pduel->game_field->add_effect(peffect, playerid); + return 0; +} +int32_t scriptlib::duel_is_damage_calculated(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->core.damage_calculated); + return 1; +} +int32_t scriptlib::duel_get_attacker(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + card* pcard = pduel->game_field->core.attacker; + interpreter::card2value(L, pcard); + return 1; +} +int32_t scriptlib::duel_get_attack_target(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + card* pcard = pduel->game_field->core.attack_target; + interpreter::card2value(L, pcard); + return 1; +} +int32_t scriptlib::duel_get_battle_monster(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_INT, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + card* attacker = pduel->game_field->core.attacker; + card* defender = pduel->game_field->core.attack_target; + for(int32_t i = 0; i < 2; i++) { + if(attacker && attacker->current.controler == playerid) + interpreter::card2value(L, attacker); + else if(defender && defender->current.controler == playerid) + interpreter::card2value(L, defender); + else + lua_pushnil(L); + playerid = 1 - playerid; + } + return 2; +} +int32_t scriptlib::duel_disable_attack(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_ATTACK_DISABLE, 0, 0, 0, 0, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_chain_attack(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + card* attacker = pduel->game_field->core.attacker; + if(!attacker || !attacker->is_affect_by_effect(pduel->game_field->core.reason_effect)) { + return 0; + } + pduel->game_field->core.chain_attack = TRUE; + pduel->game_field->core.chain_attacker_id = attacker->fieldid; + if(lua_gettop(L) > 0) { + check_param(L, PARAM_TYPE_CARD, 1); + pduel->game_field->core.chain_attack_target = *(card**) lua_touserdata(L, 1); + } + return 0; +} +int32_t scriptlib::duel_readjust(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + card* adjcard = pduel->game_field->core.reason_effect->get_handler(); + ++pduel->game_field->core.readjust_map[adjcard]; + if(pduel->game_field->core.readjust_map[adjcard] > 3) { + pduel->game_field->send_to(adjcard, 0, REASON_RULE, pduel->game_field->core.reason_player, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return lua_yield(L, 0); + } + pduel->game_field->core.re_adjust = TRUE; + return 0; +} +int32_t scriptlib::duel_adjust_instantly(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) > 0) { + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + pcard->filter_disable_related_cards(); + } + pduel->game_field->adjust_instant(); + return 0; +} +int32_t scriptlib::duel_adjust_all(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->adjust_all(); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + return 0; + }); +} +/** + * \brief Duel.GetFieldGroup + * \param playerid, location1, location2 + * \return Group + */ +int32_t scriptlib::duel_get_field_group(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t location1 = (uint32_t)lua_tointeger(L, 2); + uint32_t location2 = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->filter_field_card(playerid, location1, location2, pgroup); + interpreter::group2value(L, pgroup); + return 1; +} +/** + * \brief Duel.GetFieldGroupCount + * \param playerid, location1, location2 + * \return Integer + */ +int32_t scriptlib::duel_get_field_group_count(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t location1 = (uint32_t)lua_tointeger(L, 2); + uint32_t location2 = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + uint32_t count = pduel->game_field->filter_field_card(playerid, location1, location2, 0); + lua_pushinteger(L, count); + return 1; +} +/** + * \brief Duel.GetDeckTop + * \param playerid, count + * \return Group + */ +int32_t scriptlib::duel_get_decktop_group(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + auto cit = pduel->game_field->player[playerid].list_main.rbegin(); + for(uint32_t i = 0; i < count && cit != pduel->game_field->player[playerid].list_main.rend(); ++i, ++cit) + pgroup->container.insert(*cit); + interpreter::group2value(L, pgroup); + return 1; +} +/** + * \brief Duel.GetExtraTopGroup + * \param playerid, count + * \return Group + */ +int32_t scriptlib::duel_get_extratop_group(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + auto cit = pduel->game_field->player[playerid].list_extra.rbegin() + pduel->game_field->player[playerid].extra_p_count; + for(uint32_t i = 0; i < count && cit != pduel->game_field->player[playerid].list_extra.rend(); ++i, ++cit) + pgroup->container.insert(*cit); + interpreter::group2value(L, pgroup); + return 1; +} +/** +* \brief Duel.GetMatchingGroup +* \param filter_func, self, location1, location2, exception card, (extraargs...) +* \return Group +*/ +int32_t scriptlib::duel_get_matching_group(lua_State *L) { + check_param_count(L, 5); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) + pexception = *(card**) lua_touserdata(L, 5); + else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 5); + uint32_t extraargs = lua_gettop(L) - 5; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + group* pgroup = pduel->new_group(); + pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, pgroup, pexception, pexgroup, extraargs); + interpreter::group2value(L, pgroup); + return 1; +} +/** +* \brief Duel.GetMatchingGroupCount +* \param filter_func, self, location1, location2, exception card, (extraargs...) +* \return Integer +*/ +int32_t scriptlib::duel_get_matching_count(lua_State *L) { + check_param_count(L, 5); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) + pexception = *(card**) lua_touserdata(L, 5); + else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 5); + uint32_t extraargs = lua_gettop(L) - 5; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + group* pgroup = pduel->new_group(); + pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, pgroup, pexception, pexgroup, extraargs); + uint32_t count = (uint32_t)pgroup->container.size(); + lua_pushinteger(L, count); + return 1; +} +/** +* \brief Duel.GetFirstMatchingCard +* \param filter_func, self, location1, location2, exception card, (extraargs...) +* \return Card | nil +*/ +int32_t scriptlib::duel_get_first_matching_card(lua_State *L) { + check_param_count(L, 5); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) + pexception = *(card**) lua_touserdata(L, 5); + else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 5); + uint32_t extraargs = lua_gettop(L) - 5; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + card* pret = nullptr; + pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, 0, pexception, pexgroup, extraargs, &pret); + if(pret) + interpreter::card2value(L, pret); + else lua_pushnil(L); + return 1; +} +/** +* \brief Duel.IsExistingMatchingCard +* \param filter_func, self, location1, location2, count, exception card, (extraargs...) +* \return boolean +*/ +int32_t scriptlib::duel_is_existing_matching_card(lua_State *L) { + check_param_count(L, 6); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 6, TRUE)) + pexception = *(card**) lua_touserdata(L, 6); + else if(check_param(L, PARAM_TYPE_GROUP, 6, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 6); + uint32_t extraargs = lua_gettop(L) - 6; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + uint32_t fcount = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, 0, pexception, pexgroup, extraargs, nullptr, fcount)); + return 1; +} +/** +* \brief Duel.SelectMatchingCards +* \param playerid, filter_func, self, location1, location2, min, max, exception card, (extraargs...) +* \return Group +*/ +int32_t scriptlib::duel_select_matching_cards(lua_State *L) { + check_action_permission(L); + check_param_count(L, 8); + if(!lua_isnil(L, 2)) + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 8, TRUE)) + pexception = *(card**) lua_touserdata(L, 8); + else if(check_param(L, PARAM_TYPE_GROUP, 8, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 8); + uint32_t extraargs = lua_gettop(L) - 8; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 3); + uint32_t location1 = (uint32_t)lua_tointeger(L, 4); + uint32_t location2 = (uint32_t)lua_tointeger(L, 5); + uint32_t min = (uint32_t)lua_tointeger(L, 6); + uint32_t max = (uint32_t)lua_tointeger(L, 7); + group* pgroup = pduel->new_group(); + pduel->game_field->filter_matching_card(L, 2, (uint8_t)self, location1, location2, pgroup, pexception, pexgroup, extraargs); + pduel->game_field->core.select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (max << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +/** +* \brief Duel.GetReleaseGroup +* \param playerid +* \return Group +*/ +int32_t scriptlib::duel_get_release_group(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t hand = FALSE; + if(lua_gettop(L) > 1) + hand = lua_toboolean(L, 2); + uint32_t reason = REASON_COST; + if (lua_gettop(L) >= 3) + reason = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_release_list(L, playerid, &pgroup->container, &pgroup->container, &pgroup->container, FALSE, hand, 0, 0, nullptr, nullptr, reason); + interpreter::group2value(L, pgroup); + return 1; +} +/** +* \brief Duel.GetReleaseGroupCount +* \param playerid +* \return Integer +*/ +int32_t scriptlib::duel_get_release_group_count(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t hand = FALSE; + if(lua_gettop(L) > 1) + hand = lua_toboolean(L, 2); + uint32_t reason = REASON_COST; + if (lua_gettop(L) >= 3) + reason = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->get_release_list(L, playerid, nullptr, nullptr, nullptr, FALSE, hand, 0, 0, nullptr, nullptr, reason)); + return 1; +} +int32_t scriptlib::duel_check_release_group(lua_State *L) { + const int32_t must_param = 4; + check_param_count(L, must_param); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t use_con = FALSE; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + use_con = TRUE; + } + uint32_t fcount = (uint32_t)lua_tointeger(L, 3); + card* pexception = nullptr; + group *pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 4, TRUE)) + pexception = *(card**) lua_touserdata(L, 4); + else if(check_param(L, PARAM_TYPE_GROUP, 4, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 4); + uint32_t extraargs = lua_gettop(L) - must_param; + duel* pduel = interpreter::get_duel_info(L); + int32_t result = pduel->game_field->check_release_list(L, playerid, fcount, use_con, FALSE, 2, extraargs, pexception, pexgroup, REASON_COST); + pduel->game_field->core.must_select_cards.clear(); + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::duel_select_release_group(lua_State *L) { + const int32_t must_param = 5; + check_action_permission(L); + check_param_count(L, must_param); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t use_con = FALSE; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + use_con = TRUE; + } + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + card* pexception = 0; + group* pexgroup = 0; + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) + pexception = *(card**) lua_touserdata(L, 5); + else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 5); + uint32_t extraargs = lua_gettop(L) - must_param; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.release_cards.clear(); + pduel->game_field->core.release_cards_ex.clear(); + pduel->game_field->core.release_cards_ex_oneof.clear(); + pduel->game_field->get_release_list(L, playerid, &pduel->game_field->core.release_cards, &pduel->game_field->core.release_cards_ex, &pduel->game_field->core.release_cards_ex_oneof, use_con, FALSE, 2, extraargs, pexception, pexgroup, REASON_COST); + pduel->game_field->add_process(PROCESSOR_SELECT_RELEASE, 0, 0, 0, playerid, (max << 16) + min); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::duel_check_release_group_ex(lua_State *L) { + const int32_t must_param = 6; + check_param_count(L, must_param); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t use_con = FALSE; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + use_con = TRUE; + } + uint32_t fcount = (uint32_t)lua_tointeger(L, 3); + uint32_t reason = (uint32_t)lua_tointeger(L, 4); + int32_t use_hand = lua_toboolean(L, 5); + card* pexception = nullptr; + group *pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 6, TRUE)) + pexception = *(card**) lua_touserdata(L, 6); + else if(check_param(L, PARAM_TYPE_GROUP, 6, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 6); + uint32_t extraargs = lua_gettop(L) - must_param; + duel* pduel = interpreter::get_duel_info(L); + int32_t result = pduel->game_field->check_release_list(L, playerid, fcount, use_con, use_hand, 2, extraargs, pexception, pexgroup, reason); + pduel->game_field->core.must_select_cards.clear(); + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::duel_select_release_group_ex(lua_State *L) { + const int32_t must_param = 7; + check_action_permission(L); + check_param_count(L, must_param); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t use_con = FALSE; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + use_con = TRUE; + } + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + uint32_t reason = (uint32_t)lua_tointeger(L, 5); + int32_t use_hand = lua_toboolean(L, 6); + card* pexception = 0; + group* pexgroup = 0; + if(check_param(L, PARAM_TYPE_CARD, 7, TRUE)) + pexception = *(card**) lua_touserdata(L, 7); + else if(check_param(L, PARAM_TYPE_GROUP, 7, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 7); + uint32_t extraargs = lua_gettop(L) - must_param; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.release_cards.clear(); + pduel->game_field->core.release_cards_ex.clear(); + pduel->game_field->core.release_cards_ex_oneof.clear(); + pduel->game_field->get_release_list(L, playerid, &pduel->game_field->core.release_cards, &pduel->game_field->core.release_cards_ex, &pduel->game_field->core.release_cards_ex_oneof, use_con, use_hand, 2, extraargs, pexception, pexgroup, reason); + pduel->game_field->add_process(PROCESSOR_SELECT_RELEASE, 0, 0, 0, playerid, (max << 16) + min); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +/** +* \brief Duel.GetTributeGroup +* \param targetcard +* \return Group +*/ +int32_t scriptlib::duel_get_tribute_group(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* target = *(card**) lua_touserdata(L, 1); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_summon_release_list(target, &(pgroup->container), &(pgroup->container), nullptr); + interpreter::group2value(L, pgroup); + return 1; +} +/** +* \brief Duel.GetTributeCount +* \param targetcard +* \return Integer +*/ +int32_t scriptlib::duel_get_tribute_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* target = *(card**) lua_touserdata(L, 1); + group* mg = nullptr; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_GROUP, 2); + mg = *(group**) lua_touserdata(L, 2); + } + uint32_t ex = 0; + if(lua_gettop(L) >= 3) + ex = lua_toboolean(L, 3); + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->get_summon_release_list(target, nullptr, nullptr, nullptr, mg, ex)); + return 1; +} +int32_t scriptlib::duel_check_tribute(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* target = *(card**) lua_touserdata(L, 1); + uint32_t min = (uint32_t)lua_tointeger(L, 2); + uint32_t max = min; + if(lua_gettop(L) >= 3 && !lua_isnil(L, 3)) + max = (uint32_t)lua_tointeger(L, 3); + group* mg = nullptr; + if(lua_gettop(L) >= 4 && !lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_GROUP, 4); + mg = *(group**)lua_touserdata(L, 4); + } + uint8_t toplayer = target->current.controler; + if(lua_gettop(L) >= 5 && !lua_isnil(L, 5)) + toplayer = (uint8_t)lua_tointeger(L, 5); + duel* pduel = target->pduel; + uint32_t zone = 0x1f; + if(pduel->game_field->core.limit_extra_summon_zone) + zone= pduel->game_field->core.limit_extra_summon_zone; + if(lua_gettop(L) >= 6 && !lua_isnil(L, 6)) + zone = (uint32_t)lua_tointeger(L, 6); + uint32_t releasable = 0xff00ff; + if(pduel->game_field->core.limit_extra_summon_releasable) + releasable = pduel->game_field->core.limit_extra_summon_releasable; + lua_pushboolean(L, pduel->game_field->check_tribute(target, min, max, mg, toplayer, zone, releasable)); + return 1; +} +int32_t scriptlib::duel_select_tribute(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* target = *(card**) lua_touserdata(L, 2); + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + group* mg = nullptr; + if(lua_gettop(L) >= 5 && !lua_isnil(L, 5)) { + check_param(L, PARAM_TYPE_GROUP, 5); + mg = *(group**) lua_touserdata(L, 5); + } + uint8_t toplayer = playerid; + if(lua_gettop(L) >= 6) + toplayer = (uint8_t)lua_tointeger(L, 6); + if(toplayer != 0 && toplayer != 1) + return 0; + uint32_t ex = FALSE; + if(toplayer != playerid) + ex = TRUE; + duel* pduel = interpreter::get_duel_info(L); + uint32_t zone = 0x1f; + if(pduel->game_field->core.limit_extra_summon_zone) + zone = pduel->game_field->core.limit_extra_summon_zone; + uint32_t releasable = 0xff00ff; + if(pduel->game_field->core.limit_extra_summon_releasable) + releasable = pduel->game_field->core.limit_extra_summon_releasable; + pduel->game_field->core.release_cards.clear(); + pduel->game_field->core.release_cards_ex.clear(); + pduel->game_field->core.release_cards_ex_oneof.clear(); + pduel->game_field->get_summon_release_list(target, &pduel->game_field->core.release_cards, &pduel->game_field->core.release_cards_ex, &pduel->game_field->core.release_cards_ex_oneof, mg, ex, releasable); + pduel->game_field->select_tribute_cards(0, playerid, 0, min, max, toplayer, zone); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +/** +* \brief Duel.GetTargetCount +* \param filter_func, self, location1, location2, exception card, (extraargs...) +* \return Group +*/ +int32_t scriptlib::duel_get_target_count(lua_State *L) { + check_param_count(L, 5); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) + pexception = *(card**) lua_touserdata(L, 5); + else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 5); + uint32_t extraargs = lua_gettop(L) - 5; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + group* pgroup = pduel->new_group(); + uint32_t count = 0; + pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, pgroup, pexception, pexgroup, extraargs, nullptr, 0, TRUE); + count = (uint32_t)pgroup->container.size(); + lua_pushinteger(L, count); + return 1; +} +/** +* \brief Duel.IsExistingTarget +* \param filter_func, self, location1, location2, count, exception card, (extraargs...) +* \return boolean +*/ +int32_t scriptlib::duel_is_existing_target(lua_State *L) { + check_param_count(L, 6); + if(!lua_isnil(L, 1)) + check_param(L, PARAM_TYPE_FUNCTION, 1); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 6, TRUE)) + pexception = *(card**) lua_touserdata(L, 6); + else if(check_param(L, PARAM_TYPE_GROUP, 6, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 6); + uint32_t extraargs = lua_gettop(L) - 6; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + uint32_t count = (uint32_t)lua_tointeger(L, 5); + lua_pushboolean(L, pduel->game_field->filter_matching_card(L, 1, (uint8_t)self, location1, location2, 0, pexception, pexgroup, extraargs, nullptr, count, TRUE)); + return 1; +} +/** +* \brief Duel.SelectTarget +* \param playerid, filter_func, self, location1, location2, min, max, exception card, (extraargs...) +* \return Group +*/ +int32_t scriptlib::duel_select_target(lua_State *L) { + check_action_permission(L); + check_param_count(L, 8); + if(!lua_isnil(L, 2)) + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pexception = nullptr; + group* pexgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 8, TRUE)) + pexception = *(card**) lua_touserdata(L, 8); + else if(check_param(L, PARAM_TYPE_GROUP, 8, TRUE)) + pexgroup = *(group**) lua_touserdata(L, 8); + uint32_t extraargs = lua_gettop(L) - 8; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + uint32_t self = (uint32_t)lua_tointeger(L, 3); + uint32_t location1 = (uint32_t)lua_tointeger(L, 4); + uint32_t location2 = (uint32_t)lua_tointeger(L, 5); + uint32_t min = (uint32_t)lua_tointeger(L, 6); + uint32_t max = (uint32_t)lua_tointeger(L, 7); + if(pduel->game_field->core.current_chain.size() == 0) + return 0; + group* pgroup = pduel->new_group(); + pduel->game_field->filter_matching_card(L, 2, (uint8_t)self, location1, location2, pgroup, pexception, pexgroup, extraargs, nullptr, 0, TRUE); + pduel->game_field->core.select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (max << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + chain* ch = pduel->game_field->get_chain(0); + if(!ch) + return 0; + if(!ch->target_cards) { + ch->target_cards = pduel->new_group(); + ch->target_cards->is_readonly = GTYPE_READ_ONLY; + } + group* tg = ch->target_cards; + effect* peffect = ch->triggering_effect; + if(peffect->type & EFFECT_TYPE_CONTINUOUS) { + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) + tg->container.insert(pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]); + interpreter::group2value(L, tg); + } else { + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + tg->container.insert(pcard); + pgroup->container.insert(pcard); + pcard->create_relation(*ch); + if(peffect->is_flag(EFFECT_FLAG_CARD_TARGET)) { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(pcard->get_info_location()); + } + } + interpreter::group2value(L, pgroup); + } + return 1; + }); +} +int32_t scriptlib::duel_get_must_material(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t limit = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_must_material_list(playerid, limit, &pgroup->container); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_check_must_material(lua_State *L) { + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* mcard = nullptr; + group* mgroup = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + mcard = *(card**)lua_touserdata(L, 2); + pduel = mcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + mgroup = *(group**)lua_touserdata(L, 2); + pduel = mgroup->pduel; + } else + pduel = interpreter::get_duel_info(L); + if(mgroup) { + pgroup = pduel->new_group(mgroup->container); + pgroup->is_readonly = GTYPE_READ_ONLY; + } else if(mcard) { + pgroup = pduel->new_group(mcard); + pgroup->is_readonly = GTYPE_READ_ONLY; + } else + pgroup = 0; + uint32_t limit = (uint32_t)lua_tointeger(L, 3); + lua_pushboolean(L, pduel->game_field->check_must_material(pgroup, playerid, limit)); + return 1; +} +int32_t scriptlib::duel_select_fusion_material(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + check_param(L, PARAM_TYPE_CARD, 2); + check_param(L, PARAM_TYPE_GROUP, 3); + card* cg = nullptr; + uint32_t chkf = PLAYER_NONE; + uint8_t not_material = FALSE; + if(lua_gettop(L) > 3 && !lua_isnil(L, 4)) { + check_param(L, PARAM_TYPE_CARD, 4); + cg = *(card**) lua_touserdata(L, 4); + } + if(lua_gettop(L) > 4) + chkf = (uint32_t)lua_tointeger(L, 5); + if(lua_gettop(L) > 5) + not_material = lua_toboolean(L, 6); + card* pcard = *(card**) lua_touserdata(L, 2); + group* pgroup = *(group**) lua_touserdata(L, 3); + pcard->fusion_select(playerid, pgroup, cg, chkf, not_material); + duel* pduel = pcard->pduel; + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(pduel->game_field->core.fusion_materials); + if(lua_gettop(L) > 3 && !lua_isnil(L, 4)) { + card* cg = *(card**)lua_touserdata(L, 4); + pgroup->container.insert(cg); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::duel_set_fusion_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + pduel->game_field->core.fusion_materials = pgroup->container; + return 0; +} +int32_t scriptlib::duel_set_synchro_material(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + pduel->game_field->core.synchro_materials = pgroup->container; + return 0; +} +int32_t scriptlib::duel_get_synchro_material(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t facedown = FALSE; + if (lua_gettop(L) >= 2) + facedown = lua_toboolean(L, 2); + duel* pduel = interpreter::get_duel_info(L); + card_set mats; + pduel->game_field->get_synchro_material(playerid, &mats); + group* pgroup = pduel->new_group(); + for (auto cit = mats.begin(); cit != mats.end(); ++cit) { + card* pcard = *cit; + if (pcard->current.location == LOCATION_MZONE && !pcard->is_position(POS_FACEUP) && !facedown) + continue; + pgroup->container.insert(*cit); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_select_synchro_material(lua_State *L) { + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**) lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(!lua_isnil(L, 3)) + check_param(L, PARAM_TYPE_FUNCTION, 3); + if(!lua_isnil(L, 4)) + check_param(L, PARAM_TYPE_FUNCTION, 4); + int32_t min = (int32_t)lua_tointeger(L, 5); + int32_t max = (int32_t)lua_tointeger(L, 6); + card* smat = nullptr; + group* mg = nullptr; + if(lua_gettop(L) >= 7 && !lua_isnil(L, 7)) { + check_param(L, PARAM_TYPE_CARD, 7); + smat = *(card**) lua_touserdata(L, 7); + } + if(lua_gettop(L) >= 8 && !lua_isnil(L, 8)) { + check_param(L, PARAM_TYPE_GROUP, 8); + mg = *(group**) lua_touserdata(L, 8); + } + auto filter1 = interpreter::get_function_handle(L, 3); + auto filter2 = interpreter::get_function_handle(L, 4); + card_set select_cards; + if (mg) { + for (auto& pm : mg->container) { + if (pduel->game_field->check_tuner_material(L, pcard, pm, 3, 4, min, max, nullptr, mg)) + select_cards.insert(pm); + } + pduel->game_field->core.select_cards.assign(select_cards.begin(), select_cards.end()); + pduel->game_field->add_process(PROCESSOR_SELECT_SYNCHRO, 0, nullptr, nullptr, playerid, min + (max << 16), filter1, filter2, pcard, mg); + } + else { + card_set material; + pduel->game_field->get_synchro_material(playerid, &material); + for (auto& tuner : material) { + if (pduel->game_field->check_tuner_material(L, pcard, tuner, 3, 4, min, max, smat, nullptr)) + select_cards.insert(tuner); + } + pduel->game_field->core.select_cards.assign(select_cards.begin(), select_cards.end()); + pduel->game_field->add_process(PROCESSOR_SELECT_SYNCHRO, 0, nullptr, nullptr, playerid + 0x10000, min + (max << 16), filter1, filter2, pcard, smat); + } + return lua_yield(L, 0); +} +int32_t scriptlib::duel_check_synchro_material(lua_State *L) { + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + if(!lua_isnil(L, 2)) + check_param(L, PARAM_TYPE_FUNCTION, 2); + if(!lua_isnil(L, 3)) + check_param(L, PARAM_TYPE_FUNCTION, 3); + int32_t min = (int32_t)lua_tointeger(L, 4); + int32_t max = (int32_t)lua_tointeger(L, 5); + card* smat = nullptr; + group* mg = nullptr; + if(lua_gettop(L) >= 6 && !lua_isnil(L, 6)) { + check_param(L, PARAM_TYPE_CARD, 6); + smat = *(card**) lua_touserdata(L, 6); + } + if(lua_gettop(L) >= 7 && !lua_isnil(L, 7)) { + check_param(L, PARAM_TYPE_GROUP, 7); + mg = *(group**) lua_touserdata(L, 7); + } + lua_pushboolean(L, pduel->game_field->check_synchro_material(L, pcard, 2, 3, min, max, smat, mg)); + return 1; +} +int32_t scriptlib::duel_select_tuner_material(lua_State *L) { + check_param_count(L, 7); + check_param(L, PARAM_TYPE_CARD, 2); + check_param(L, PARAM_TYPE_CARD, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**) lua_touserdata(L, 2); + card* tuner = *(card**) lua_touserdata(L, 3); + duel* pduel = pcard->pduel; + if(!lua_isnil(L, 4)) + check_param(L, PARAM_TYPE_FUNCTION, 4); + if(!lua_isnil(L, 5)) + check_param(L, PARAM_TYPE_FUNCTION, 5); + int32_t min = (int32_t)lua_tointeger(L, 6); + int32_t max = (int32_t)lua_tointeger(L, 7); + group* mg = nullptr; + if(lua_gettop(L) >= 8 && !lua_isnil(L, 8)) { + check_param(L, PARAM_TYPE_GROUP, 8); + mg = *(group**) lua_touserdata(L, 8); + } + if(!pduel->game_field->check_tuner_material(L, pcard, tuner, 4, 5, min, max, nullptr, mg)) + return 0; + pduel->game_field->core.select_cards.clear(); + pduel->game_field->core.select_cards.push_back(tuner); + pduel->game_field->returns.bvalue[1] = 0; + auto filter1 = interpreter::get_function_handle(L, 4); + auto filter2 = interpreter::get_function_handle(L, 5); + pduel->game_field->add_process(PROCESSOR_SELECT_SYNCHRO, 1, nullptr, nullptr, playerid, min + (max << 16), filter1, filter2, pcard, mg); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_check_tuner_material(lua_State *L) { + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* tuner = *(card**) lua_touserdata(L, 2); + duel* pduel = pcard->pduel; + if(!lua_isnil(L, 3)) + check_param(L, PARAM_TYPE_FUNCTION, 3); + if(!lua_isnil(L, 4)) + check_param(L, PARAM_TYPE_FUNCTION, 4); + int32_t min = (int32_t)lua_tointeger(L, 5); + int32_t max = (int32_t)lua_tointeger(L, 6); + group* mg = nullptr; + if(lua_gettop(L) >= 7 && !lua_isnil(L, 7)) { + check_param(L, PARAM_TYPE_GROUP, 7); + mg = *(group**) lua_touserdata(L, 7); + } + lua_pushboolean(L, pduel->game_field->check_tuner_material(L, pcard, tuner, 3, 4, min, max, nullptr, mg)); + return 1; +} +int32_t scriptlib::duel_get_ritual_material(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_ritual_material(playerid, pduel->game_field->core.reason_effect, &pgroup->container); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_get_ritual_material_ex(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_ritual_material(playerid, pduel->game_field->core.reason_effect, &pgroup->container, TRUE); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_release_ritual_material(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + pgroup->pduel->game_field->ritual_release(pgroup->container); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_get_fusion_material(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t location = LOCATION_HAND + LOCATION_MZONE; + if(lua_gettop(L) >= 2) + location = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroupall = pduel->new_group(); + group* pgroupbase = pduel->new_group(); + pduel->game_field->get_fusion_material(playerid, &pgroupall->container, &pgroupbase->container, location); + interpreter::group2value(L, pgroupall); + interpreter::group2value(L, pgroupbase); + return 2; +} +int32_t scriptlib::duel_is_summon_cancelable(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->core.summon_cancelable); + return 1; +} +int32_t scriptlib::duel_set_must_select_cards(lua_State *L) { + check_param_count(L, 1); + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + pduel->game_field->core.must_select_cards.clear(); + pduel->game_field->core.must_select_cards.push_back(pcard); + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + pduel->game_field->core.must_select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + return 0; +} +int32_t scriptlib::duel_grab_must_select_cards(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + if(pduel->game_field->core.must_select_cards.size()) + pgroup->container.insert(pduel->game_field->core.must_select_cards.begin(), pduel->game_field->core.must_select_cards.end()); + pduel->game_field->core.must_select_cards.clear(); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_set_target_card(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + card* pcard = nullptr; + group* pgroup = nullptr; + duel* pduel = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + pcard = *(card**) lua_touserdata(L, 1); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 1); + pduel = pgroup->pduel; + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + chain* ch = pduel->game_field->get_chain(0); + if(!ch) + return 0; + if(!ch->target_cards) { + ch->target_cards = pduel->new_group(); + ch->target_cards->is_readonly = GTYPE_READ_ONLY; + } + group* targets = ch->target_cards; + effect* peffect = ch->triggering_effect; + if(peffect->type & EFFECT_TYPE_CONTINUOUS) { + if(pcard) + targets->container.insert(pcard); + else + targets->container = pgroup->container; + } else { + if(pcard) { + targets->container.insert(pcard); + pcard->create_relation(*ch); + } else { + targets->container.insert(pgroup->container.begin(), pgroup->container.end()); + for(auto& group_card : pgroup->container) + group_card->create_relation(*ch); + } + if(peffect->is_flag(EFFECT_FLAG_CARD_TARGET)) { + if(pcard) { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(pcard->get_info_location()); + } else { + for(auto& group_card : pgroup->container) { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(group_card->get_info_location()); + } + } + } + } + return 0; +} +int32_t scriptlib::duel_clear_target_card(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(0); + if(ch && ch->target_cards) + ch->target_cards->container.clear(); + return 0; +} +int32_t scriptlib::duel_set_target_player(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(0); + if(ch) + ch->target_player = playerid; + return 0; +} +int32_t scriptlib::duel_set_target_param(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + uint32_t param = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(0); + if(ch) + ch->target_param = param; + return 0; +} +/** +* \brief Duel.SetOperationInfo +* \param target_group, target_count, target_player, targ +* \return N/A +*/ +int32_t scriptlib::duel_set_operation_info(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + group* pgroup = nullptr; + card* pcard = nullptr; + group* pg = nullptr; + uint32_t ct = (uint32_t)lua_tointeger(L, 1); + uint32_t cate = (uint32_t)lua_tointeger(L, 2); + uint32_t count = (uint32_t)lua_tointeger(L, 4); + int32_t playerid = (int32_t)lua_tointeger(L, 5); + uint32_t param = (uint32_t)lua_tointeger(L, 6); + duel* pduel; + if(check_param(L, PARAM_TYPE_CARD, 3, TRUE)) { + pcard = *(card**) lua_touserdata(L, 3); + pduel = pcard->pduel; + } else if(check_param(L, PARAM_TYPE_GROUP, 3, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 3); + pduel = pgroup->pduel; + } else + pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(ct); + if(!ch) + return 0; + if(pgroup) { + pg = pduel->new_group(pgroup->container); + pg->is_readonly = GTYPE_READ_ONLY; + } else if(pcard) { + pg = pduel->new_group(pcard); + pg->is_readonly = GTYPE_READ_ONLY; + } else + pg = nullptr; + optarget opt; + opt.op_cards = pg; + opt.op_count = count; + opt.op_player = playerid; + opt.op_param = param; + auto omit = ch->opinfos.find(cate); + if(omit != ch->opinfos.end() && omit->second.op_cards) + pduel->delete_group(omit->second.op_cards); + ch->opinfos[cate] = opt; + return 0; +} +int32_t scriptlib::duel_get_operation_info(lua_State *L) { + check_param_count(L, 2); + uint32_t ct = (uint32_t)lua_tointeger(L, 1); + uint32_t cate = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(ct); + if(!ch) + return 0; + auto oit = ch->opinfos.find(cate); + if(oit != ch->opinfos.end()) { + optarget& opt = oit->second; + lua_pushboolean(L, 1); + if(opt.op_cards) + interpreter::group2value(L, opt.op_cards); + else + lua_pushnil(L); + lua_pushinteger(L, opt.op_count); + lua_pushinteger(L, opt.op_player); + lua_pushinteger(L, opt.op_param); + return 5; + } else { + lua_pushboolean(L, 0); + return 1; + } +} +int32_t scriptlib::duel_get_operation_count(lua_State *L) { + check_param_count(L, 1); + uint32_t ct = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(ct); + if(!ch) + return 0; + lua_pushinteger(L, ch->opinfos.size()); + return 1; +} +int32_t scriptlib::duel_clear_operation_info(lua_State* L) { + check_action_permission(L); + check_param_count(L, 1); + uint32_t ct = (uint32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + chain* ch = pduel->game_field->get_chain(ct); + if(!ch) + return 0; + for(auto& oit : ch->opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + ch->opinfos.clear(); + return 0; +} +int32_t scriptlib::duel_check_xyz_material(lua_State *L) { + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 1); + uint32_t findex = 0; + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + findex = 2; + } + card* scard = *(card**) lua_touserdata(L, 1); + uint32_t lv = (uint32_t)lua_tointeger(L, 3); + uint32_t minc = (uint32_t)lua_tointeger(L, 4); + uint32_t maxc = (uint32_t)lua_tointeger(L, 5); + group* mg = nullptr; + if(!lua_isnil(L, 6)) { + check_param(L, PARAM_TYPE_GROUP, 6); + mg = *(group**) lua_touserdata(L, 6); + } + lua_pushboolean(L, scard->pduel->game_field->check_xyz_material(L, scard, findex, lv, minc, maxc, mg)); + return 1; +} +int32_t scriptlib::duel_select_xyz_material(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + check_param(L, PARAM_TYPE_CARD, 2); + uint32_t findex = 0; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_FUNCTION, 3); + findex = 3; + } + card* scard = *(card**) lua_touserdata(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t lv = (uint32_t)lua_tointeger(L, 4); + uint32_t minc = (uint32_t)lua_tointeger(L, 5); + uint32_t maxc = (uint32_t)lua_tointeger(L, 6); + group* mg = nullptr; + if(lua_gettop(L) >= 7 && !lua_isnil(L, 7)) { + check_param(L, PARAM_TYPE_GROUP, 7); + mg = *(group**) lua_touserdata(L, 7); + } + duel* pduel = scard->pduel; + if(!pduel->game_field->check_xyz_material(L, scard, findex, lv, minc, maxc, mg)) + return 0; + pduel->game_field->get_xyz_material(L, scard, findex, lv, maxc, mg); + scard->pduel->game_field->add_process(PROCESSOR_SELECT_XMATERIAL, 0, 0, (group*)scard, playerid + (lv << 16), minc + (maxc << 16)); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_overlay(lua_State *L) { + check_action_permission(L); + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + card* target = *(card**) lua_touserdata(L, 1); + card* pcard = nullptr; + group* pgroup = nullptr; + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + pcard = *(card**) lua_touserdata(L, 2); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + pgroup = *(group**) lua_touserdata(L, 2); + } else + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + if(pcard) { + card_set cset{ pcard }; + target->xyz_overlay(cset); + } else + target->xyz_overlay(pgroup->container); + uint32_t adjust = TRUE; + if(lua_gettop(L) > 2) { + adjust = lua_toboolean(L, 3); + } + if(adjust) + target->pduel->game_field->adjust_all(); + return lua_yield(L, 0); +} +int32_t scriptlib::duel_get_overlay_group(lua_State *L) { + check_param_count(L, 3); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + pduel->game_field->get_overlay_group(rplayer, s, o, &pgroup->container); + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::duel_get_overlay_count(lua_State *L) { + check_param_count(L, 3); + uint32_t rplayer = (uint32_t)lua_tointeger(L, 1); + if(rplayer != 0 && rplayer != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->get_overlay_count(rplayer, s, o)); + return 1; +} +int32_t scriptlib::duel_check_remove_overlay_card(lua_State *L) { + check_param_count(L, 5); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + int32_t count = (int32_t)lua_tointeger(L, 4); + int32_t reason = (int32_t)lua_tointeger(L, 5); + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_remove_overlay_card(playerid, 0, s, o, count, reason)); + return 1; +} +int32_t scriptlib::duel_remove_overlay_card(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + int32_t min = (int32_t)lua_tointeger(L, 4); + int32_t max = (int32_t)lua_tointeger(L, 5); + int32_t reason = (int32_t)lua_tointeger(L, 6); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->remove_overlay_card(reason, 0, playerid, s, o, min, max); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_hint(lua_State * L) { + check_param_count(L, 3); + int32_t htype = (int32_t)lua_tointeger(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t desc = (int32_t)lua_tointeger(L, 3); + if(htype == HINT_OPSELECTED) + playerid = 1 - playerid; + duel* pduel = interpreter::get_duel_info(L); + if(htype == HINT_SELECTMSG) + pduel->game_field->core.last_select_hint[playerid] = desc; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(htype); + pduel->write_buffer8(playerid); + pduel->write_buffer32(desc); + return 0; +} +int32_t scriptlib::duel_get_last_select_hint(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + uint8_t playerid = pduel->game_field->core.reason_player; + if(lua_gettop(L) >= 1) + playerid = (uint8_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + lua_pushinteger(L, pduel->game_field->core.last_select_hint[playerid]); + return 1; +} +int32_t scriptlib::duel_hint_selection(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + for(auto& pcard : pgroup->container) { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(pcard->get_info_location()); + } + return 0; +} +int32_t scriptlib::duel_select_effect_yesno(lua_State * L) { + check_action_permission(L); + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + card* pcard = *(card**) lua_touserdata(L, 2); + int32_t desc = 95; + if(lua_gettop(L) >= 3) + desc = (int32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)pcard, playerid, desc); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_select_yesno(lua_State * L) { + check_action_permission(L); + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + int32_t desc = (int32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, desc); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushboolean(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_select_option(lua_State * L) { + check_action_permission(L); + check_param_count(L, 1); + uint32_t count = lua_gettop(L) - 1; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.select_options.clear(); + for(uint32_t i = 0; i < count; ++i) + pduel->game_field->core.select_options.push_back((uint32_t)lua_tointeger(L, i + 2)); + pduel->game_field->add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, playerid, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_OPSELECTED); + pduel->write_buffer8(playerid); + pduel->write_buffer32(pduel->game_field->core.select_options[pduel->game_field->returns.ivalue[0]]); + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_select_sequence(lua_State * L) { + check_action_permission(L); + return 0; +} +int32_t scriptlib::duel_select_position(lua_State * L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + uint32_t positions = (uint32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_SELECT_POSITION, 0, 0, 0, playerid + (positions << 16), pcard->data.code); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_get_disable_field(lua_State * L){ + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->player[0].disabled_location + (pduel->game_field->player[1].disabled_location << 16)); + return 1; +} +int32_t scriptlib::duel_select_disable_field(lua_State * L) { + check_action_permission(L); + check_param_count(L, 5); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t count = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + uint32_t filter = (uint32_t)lua_tointeger(L, 5); + duel* pduel = interpreter::get_duel_info(L); + uint32_t ct1 = 0, ct2 = 0, ct3 = 0, ct4 = 0, plist = 0, flag = 0xffffffff; + if(location1 & LOCATION_MZONE) { + ct1 = pduel->game_field->get_useable_count(nullptr, playerid, LOCATION_MZONE, PLAYER_NONE, 0, 0xff, &plist); + flag = (flag & 0xffffff00) | plist; + } + if(location1 & LOCATION_SZONE) { + ct2 = pduel->game_field->get_useable_count(nullptr, playerid, LOCATION_SZONE, PLAYER_NONE, 0, 0xff, &plist); + flag = (flag & 0xffff00ff) | (plist << 8); + } + if(location2 & LOCATION_MZONE) { + ct3 = pduel->game_field->get_useable_count(nullptr, 1 - playerid, LOCATION_MZONE, PLAYER_NONE, 0, 0xff, &plist); + flag = (flag & 0xff00ffff) | (plist << 16); + } + if(location2 & LOCATION_SZONE) { + ct4 = pduel->game_field->get_useable_count(nullptr, 1 - playerid, LOCATION_SZONE, PLAYER_NONE, 0, 0xff, &plist); + flag = (flag & 0xffffff) | (plist << 24); + } + if((location1 & LOCATION_MZONE) && (location2 & LOCATION_MZONE) && pduel->game_field->core.duel_rule >= NEW_MASTER_RULE) { + if(pduel->game_field->is_location_useable(playerid, LOCATION_MZONE, 5)) { + flag &= ~(0x1U << 5); + ct1 += 1; + } + if(pduel->game_field->is_location_useable(playerid, LOCATION_MZONE, 6)) { + flag &= ~(0x1U << 6); + ct1 += 1; + } + } + flag |= filter | 0xe080e080; + if(count > ct1 + ct2 + ct3 + ct4) + count = ct1 + ct2 + ct3 + ct4; + if(count == 0) + return 0; + pduel->game_field->add_process(PROCESSOR_SELECT_DISFIELD, 0, 0, 0, playerid, flag, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 2); + int32_t dfflag = 0; + uint8_t pa = 0; + for(uint32_t i = 0; i < count; ++i) { + uint8_t p = pduel->game_field->returns.bvalue[pa]; + uint8_t l = pduel->game_field->returns.bvalue[pa + 1]; + uint8_t s = pduel->game_field->returns.bvalue[pa + 2]; + dfflag |= 0x1U << (s + (p == playerid ? 0 : 16) + (l == LOCATION_MZONE ? 0 : 8)); + pa += 3; + } + if(dfflag & (0x1U << 5)) + dfflag |= 0x1U << (16 + 6); + if(dfflag & (0x1U << 6)) + dfflag |= 0x1U << (16 + 5); + lua_pushinteger(L, dfflag); + return 1; + }); +} +/* +Select zones from the viewpoint of playerid. +flag selectable zones XX000000 X0000000 for each player, 1 byte for SZONE(6), 1 byte for MZONE(7) +filter excluding zones, 0 for selectable, 1 for unselectable +*/ +int32_t scriptlib::duel_select_field(lua_State* L) { + check_action_permission(L); + check_param_count(L, 5); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t count = (uint32_t)lua_tointeger(L, 2); + uint32_t location1 = (uint32_t)lua_tointeger(L, 3); + uint32_t location2 = (uint32_t)lua_tointeger(L, 4); + uint32_t filter = (uint32_t)lua_tointeger(L, 5); + uint16_t type = PROCESSOR_SELECT_DISFIELD; + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) >= 6) { + type = PROCESSOR_SELECT_PLACE; + uint32_t code = (uint32_t)lua_tointeger(L, 6); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(code); + } + uint32_t flag = 0xffffffff; + if(location1 & LOCATION_MZONE) { + flag &= 0xffffff80; + } + if(location1 & LOCATION_SZONE) { + flag &= (pduel->game_field->core.duel_rule == 3) ? 0xffff00ff : 0xffffc0ff; + } + if(location2 & LOCATION_MZONE) { + flag &= 0xff80ffff; + } + if(location2 & LOCATION_SZONE) { + flag &= (pduel->game_field->core.duel_rule == 3) ? 0x00ffffff : 0xc0ffffff; + } + if((location1 & LOCATION_MZONE) && (location2 & LOCATION_MZONE) && pduel->game_field->core.duel_rule >= NEW_MASTER_RULE) { + flag &= 0xffffff9f; + } + flag |= filter | 0x00800080; + pduel->game_field->add_process(type, 0, 0, 0, playerid, flag, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State* L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + uint32_t count = (uint32_t)lua_tointeger(L, 2); + int32_t dfflag = 0; + uint8_t pa = 0; + for(uint32_t i = 0; i < count; ++i) { + uint8_t p = pduel->game_field->returns.bvalue[pa]; + uint8_t l = pduel->game_field->returns.bvalue[pa + 1]; + uint8_t s = pduel->game_field->returns.bvalue[pa + 2]; + dfflag |= 0x1u << (s + (p == playerid ? 0 : 16) + (l == LOCATION_MZONE ? 0 : 8)); + pa += 3; + } + if(dfflag & (0x1U << 5)) + dfflag |= 0x1U << (16 + 6); + if(dfflag & (0x1U << 6)) + dfflag |= 0x1U << (16 + 5); + lua_pushinteger(L, dfflag); + return 1; + }); +} +int32_t scriptlib::duel_announce_race(lua_State * L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + int32_t available = (int32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_ANNOUNCE_RACE, 0, 0, 0, playerid + (count << 16), available); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_announce_attribute(lua_State * L) { + check_action_permission(L); + check_param_count(L, 3); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + int32_t available = (int32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->add_process(PROCESSOR_ANNOUNCE_ATTRIB, 0, 0, 0, playerid + (count << 16), available); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_announce_level(lua_State * L) { + check_action_permission(L); + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t min = 1; + int32_t max = 12; + if(lua_gettop(L) >= 2 && !lua_isnil(L, 2)) + min = (int32_t)lua_tointeger(L, 2); + if(lua_gettop(L) >= 3 && !lua_isnil(L, 3)) + max = (int32_t)lua_tointeger(L, 3); + if(min > max) { + int32_t aux = max; + max = min; + min = aux; + } + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.select_options.clear(); + int32_t count = 0; + if(lua_gettop(L) > 3) { + for(int32_t i = min; i <= max; ++i) { + int32_t chk = 1; + for(int32_t j = 4; j <= lua_gettop(L); ++j) { + if (!lua_isnil(L, j) && i == lua_tointeger(L, j)) { + chk = 0; + break; + } + } + if(chk) { + count += 1; + pduel->game_field->core.select_options.push_back(i); + } + } + } else { + for(int32_t i = min; i <= max; ++i) { + count += 1; + pduel->game_field->core.select_options.push_back(i); + } + } + if(count == 0) + return 0; + pduel->game_field->add_process(PROCESSOR_ANNOUNCE_NUMBER, 0, 0, 0, playerid + 0x10000, 0xc0001); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->core.select_options[pduel->game_field->returns.ivalue[0]]); + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 2; + }); +} +int32_t scriptlib::duel_announce_card(lua_State * L) { + check_action_permission(L); + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.select_options.clear(); + if(lua_gettop(L) == 1) { + pduel->game_field->core.select_options.push_back(TRUE); + } else if(lua_gettop(L) == 2) { + pduel->game_field->core.select_options.push_back((uint32_t)lua_tointeger(L, 2)); + pduel->game_field->core.select_options.push_back(OPCODE_ISTYPE); + } else { + for(int32_t i = 2; i <= lua_gettop(L); ++i) + pduel->game_field->core.select_options.push_back((uint32_t)lua_tointeger(L, i)); + } + int32_t stack_size = 0; + for(auto& it : pduel->game_field->core.select_options) { + switch(it) { + case OPCODE_ADD: + case OPCODE_SUB: + case OPCODE_MUL: + case OPCODE_DIV: + case OPCODE_AND: + case OPCODE_OR: + stack_size -= 1; + break; + case OPCODE_NEG: + case OPCODE_NOT: + case OPCODE_ISCODE: + case OPCODE_ISSETCARD: + case OPCODE_ISTYPE: + case OPCODE_ISRACE: + case OPCODE_ISATTRIBUTE: + break; + default: + stack_size += 1; + break; + } + if(stack_size <= 0) + break; + } + if(stack_size != 1) + return luaL_error(L, "Parameters are invalid."); + pduel->game_field->add_process(PROCESSOR_ANNOUNCE_CARD, 0, 0, 0, playerid, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_announce_card_silent(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.announce_card_silent = TRUE; + return duel_announce_card(L); +} +int32_t scriptlib::duel_announce_type(lua_State * L) { + check_action_permission(L); + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + pduel->game_field->core.select_options.clear(); + pduel->game_field->core.select_options.push_back(70); + pduel->game_field->core.select_options.push_back(71); + pduel->game_field->core.select_options.push_back(72); + pduel->game_field->add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, playerid, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_OPSELECTED); + pduel->write_buffer8(playerid); + pduel->write_buffer32(pduel->game_field->core.select_options[pduel->game_field->returns.ivalue[0]]); + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_announce_number(lua_State * L) { + check_action_permission(L); + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->core.select_options.clear(); + for(int32_t i = 2; i <= lua_gettop(L); ++i) + pduel->game_field->core.select_options.push_back((uint32_t)lua_tointeger(L, i)); + pduel->game_field->add_process(PROCESSOR_ANNOUNCE_NUMBER, 0, 0, 0, playerid, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->core.select_options[pduel->game_field->returns.ivalue[0]]); + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 2; + }); +} +int32_t scriptlib::duel_announce_coin(lua_State * L) { + check_action_permission(L); + check_param_count(L, 1); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + pduel->game_field->core.select_options.clear(); + pduel->game_field->core.select_options.push_back(60); + pduel->game_field->core.select_options.push_back(61); + pduel->game_field->add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, playerid, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t playerid = (int32_t)lua_tointeger(L, 1); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_OPSELECTED); + pduel->write_buffer8(playerid); + pduel->write_buffer32(pduel->game_field->core.select_options[pduel->game_field->returns.ivalue[0]]); + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_toss_coin(lua_State * L) { + check_action_permission(L); + check_param_count(L, 2); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + if((playerid != 0 && playerid != 1) || count == 0 || count < -1) + return 0; + if(count > MAX_COIN_COUNT) + count = MAX_COIN_COUNT; + pduel->game_field->add_process(PROCESSOR_TOSS_COIN, 0, pduel->game_field->core.reason_effect, 0, (pduel->game_field->core.reason_player << 16) + playerid, count); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t count = pduel->game_field->core.coin_count; + for(int32_t i = 0; i < count; ++i) + lua_pushinteger(L, (pduel->game_field->core.coin_result >> i) & 1u); + return count; + }); +} +int32_t scriptlib::duel_toss_dice(lua_State * L) { + check_action_permission(L); + check_param_count(L, 2); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count1 = (int32_t)lua_tointeger(L, 2); + int32_t count2 = 0; + if(lua_gettop(L) > 2) + count2 = (int32_t)lua_tointeger(L, 3); + if((playerid != 0 && playerid != 1) || count1 <= 0 || count2 < 0) + return 0; + if(count1 > 5) + count1 = 5; + if(count2 > 5 - count1) + count2 = 5 - count1; + pduel->game_field->add_process(PROCESSOR_TOSS_DICE, 0, pduel->game_field->core.reason_effect, 0, (pduel->game_field->core.reason_player << 16) + playerid, count1 + (count2 << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + int32_t count1 = (int32_t)lua_tointeger(L, 2); + int32_t count2 = 0; + if(lua_gettop(L) > 2) + count2 = (int32_t)lua_tointeger(L, 3); + for(int32_t i = 0; i < count1 + count2; ++i) + lua_pushinteger(L, pduel->game_field->core.dice_result[i]); + return count1 + count2; + }); +} +int32_t scriptlib::duel_rock_paper_scissors(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + uint8_t repeat = TRUE; + if (lua_gettop(L) > 0) + repeat = lua_toboolean(L, 1); + pduel->game_field->add_process(PROCESSOR_ROCK_PAPER_SCISSORS, 0, 0, 0, repeat, 0); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + lua_pushinteger(L, pduel->game_field->returns.ivalue[0]); + return 1; + }); +} +int32_t scriptlib::duel_get_coin_result(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + for(int32_t i = 0; i < pduel->game_field->core.coin_count; ++i) + lua_pushinteger(L, (pduel->game_field->core.coin_result >> i) & 1u); + return pduel->game_field->core.coin_count; +} +int32_t scriptlib::duel_get_dice_result(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + for(int32_t i = 0; i < 5; ++i) + lua_pushinteger(L, pduel->game_field->core.dice_result[i]); + return 5; +} +int32_t scriptlib::duel_set_coin_result(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + int32_t res; + auto coin_count = lua_gettop(L); + pduel->game_field->core.coin_result = 0; + if(coin_count > MAX_COIN_COUNT) + coin_count = MAX_COIN_COUNT; + for(int32_t i = 0; i < coin_count; ++i) { + res = (int32_t)lua_tointeger(L, i + 1); + if(res != 0 && res != 1) + res = 0; + if(res) + pduel->game_field->core.coin_result |= (1u << i); + } + return 0; +} +int32_t scriptlib::duel_set_dice_result(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + int32_t res; + for(int32_t i = 0; i < 5; ++i) { + res = (int32_t)lua_tointeger(L, i + 1); + if(res < 1 || res > 255) + res = 1; + pduel->game_field->core.dice_result[i] = res; + } + return 0; +} +int32_t scriptlib::duel_is_player_affected_by_effect(lua_State *L) { + check_param_count(L, 2); + duel* pduel = interpreter::get_duel_info(L); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushnil(L); + return 1; + } + int32_t code = (int32_t)lua_tointeger(L, 2); + effect_set eset; + pduel->game_field->filter_player_effect(playerid, code, &eset); + int32_t size = 0; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->check_count_limit(playerid)) { + interpreter::effect2value(L, eset[i]); + ++size; + } + } + if(!size) { + lua_pushnil(L); + return 1; + } + return size; +} +int32_t scriptlib::duel_is_player_can_draw(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + uint32_t count = 0; + if(lua_gettop(L) > 1) + count = (uint32_t)lua_tointeger(L, 2); + duel* pduel = interpreter::get_duel_info(L); + if(count == 0) + lua_pushboolean(L, pduel->game_field->is_player_can_draw(playerid)); + else + lua_pushboolean(L, (int)(pduel->game_field->is_player_can_draw(playerid) && (pduel->game_field->player[playerid].list_main.size() >= count))); + return 1; +} +int32_t scriptlib::duel_is_player_can_discard_deck(lua_State * L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_discard_deck(playerid, count)); + return 1; +} +int32_t scriptlib::duel_is_player_can_discard_deck_as_cost(lua_State * L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_discard_deck_as_cost(playerid, count)); + return 1; +} +int32_t scriptlib::duel_is_player_can_summon(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_SUMMON)); + else { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 3); + int32_t sumtype = (int32_t)lua_tointeger(L, 2); + card* pcard = *(card**) lua_touserdata(L, 3); + lua_pushboolean(L, pduel->game_field->is_player_can_summon(sumtype, playerid, pcard, playerid)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_mset(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_MSET)); + else { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_CARD, 3); + int32_t sumtype = (int32_t)lua_tointeger(L, 2); + card* pcard = *(card**) lua_touserdata(L, 3); + lua_pushboolean(L, pduel->game_field->is_player_can_mset(sumtype, playerid, pcard, playerid)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_sset(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_SSET)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pduel->game_field->is_player_can_sset(playerid, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_spsummon(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_spsummon(playerid)); + else { + check_param_count(L, 5); + check_param(L, PARAM_TYPE_CARD, 5); + int32_t sumtype = (int32_t)lua_tointeger(L, 2); + int32_t sumpos = (int32_t)lua_tointeger(L, 3); + int32_t toplayer = (int32_t)lua_tointeger(L, 4); + card* pcard = *(card**) lua_touserdata(L, 5); + lua_pushboolean(L, pduel->game_field->is_player_can_spsummon(pduel->game_field->core.reason_effect, sumtype, sumpos, playerid, toplayer, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_flipsummon(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_FLIP_SUMMON)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pduel->game_field->is_player_can_flipsummon(playerid, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_spsummon_monster(lua_State * L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + int32_t code = (int32_t)lua_tointeger(L, 2); + card_data dat; + ::read_card(code, &dat); + dat.code = code; + if(lua_gettop(L) >= 3 && !lua_isnil(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)) + dat.attack = (int32_t)lua_tointeger(L, 5); + if(lua_gettop(L) >= 6 && !lua_isnil(L, 6)) + dat.defense = (int32_t)lua_tointeger(L, 6); + if(lua_gettop(L) >= 7 && !lua_isnil(L, 7)) + dat.level = (uint32_t)lua_tointeger(L, 7); + if(lua_gettop(L) >= 8 && !lua_isnil(L, 8)) + dat.race = (uint32_t)lua_tointeger(L, 8); + if(lua_gettop(L) >= 9 && !lua_isnil(L, 9)) + dat.attribute = (uint32_t)lua_tointeger(L, 9); + int32_t pos = POS_FACEUP; + int32_t toplayer = playerid; + uint32_t sumtype = 0; + if(lua_gettop(L) >= 10) + pos = (int32_t)lua_tointeger(L, 10); + if(lua_gettop(L) >= 11) + toplayer = (int32_t)lua_tointeger(L, 11); + if(lua_gettop(L) >= 12) + sumtype = (uint32_t)lua_tointeger(L, 12); + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_spsummon_monster(playerid, toplayer, pos, sumtype, &dat)); + return 1; +} +int32_t scriptlib::duel_is_player_can_spsummon_count(lua_State * L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + int32_t count = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_player_can_spsummon_count(playerid, count)); + return 1; +} +int32_t scriptlib::duel_is_player_can_release(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_RELEASE)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + uint32_t reason = REASON_COST; + if (lua_gettop(L) >= 3) + reason = (uint32_t)lua_tointeger(L, 3); + lua_pushboolean(L, pduel->game_field->is_player_can_release(playerid, pcard, reason)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_remove(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_REMOVE)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + uint32_t reason = REASON_EFFECT; + if(lua_gettop(L) >= 3) + reason = (uint32_t)lua_tointeger(L, 3); + lua_pushboolean(L, pduel->game_field->is_player_can_remove(playerid, pcard, reason)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_send_to_hand(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_TO_HAND)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pduel->game_field->is_player_can_send_to_hand(playerid, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_send_to_grave(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_TO_GRAVE)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pduel->game_field->is_player_can_send_to_grave(playerid, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_send_to_deck(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(lua_gettop(L) == 1) + lua_pushboolean(L, pduel->game_field->is_player_can_action(playerid, EFFECT_CANNOT_TO_DECK)); + else { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pduel->game_field->is_player_can_send_to_deck(playerid, pcard)); + } + return 1; +} +int32_t scriptlib::duel_is_player_can_additional_summon(lua_State * L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) { + lua_pushboolean(L, 0); + return 1; + } + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->core.extra_summon[playerid] == 0) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_is_chain_solving(lua_State * L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->core.chain_solving); + return 1; +} +int32_t scriptlib::duel_is_chain_negatable(lua_State * L) { + check_param_count(L, 1); + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::duel_is_chain_disablable(lua_State * L) { + check_param_count(L, 1); + int32_t chaincount = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->core.chain_solving) { + lua_pushboolean(L, pduel->game_field->is_chain_disablable(chaincount)); + return 1; + } + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::duel_is_chain_disabled(lua_State* L) { + check_param_count(L, 1); + int32_t chaincount = (int32_t)lua_tointeger(L, 1); + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->core.chain_solving) { + lua_pushboolean(L, pduel->game_field->is_chain_disabled(chaincount)); + return 1; + } + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_check_chain_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 2); + int32_t chaincount = (int32_t)lua_tointeger(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + lua_pushboolean(L, pcard->pduel->game_field->check_chain_target(chaincount, pcard)); + return 1; +} +int32_t scriptlib::duel_check_chain_uniqueness(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + if(pduel->game_field->core.current_chain.size() == 0) { + lua_pushboolean(L, 1); + return 1; + } + std::set er; + for(const auto& ch : pduel->game_field->core.current_chain) + er.insert(ch.triggering_effect->get_handler()->get_code()); + if(er.size() == pduel->game_field->core.current_chain.size()) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::duel_get_activity_count(lua_State *L) { + check_param_count(L, 2); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + int32_t retct = lua_gettop(L) - 1; + for(int32_t i = 0; i < retct; ++i) { + int32_t activity_type = (int32_t)lua_tointeger(L, 2 + i); + switch(activity_type) { + case 1: + lua_pushinteger(L, pduel->game_field->core.summon_state_count[playerid]); + break; + case 2: + lua_pushinteger(L, pduel->game_field->core.normalsummon_state_count[playerid]); + break; + case 3: + lua_pushinteger(L, pduel->game_field->core.spsummon_state_count[playerid]); + break; + case 4: + lua_pushinteger(L, pduel->game_field->core.flipsummon_state_count[playerid]); + break; + case 5: + lua_pushinteger(L, pduel->game_field->core.attack_state_count[playerid]); + break; + case 6: + lua_pushinteger(L, pduel->game_field->core.battle_phase_count[playerid]); + break; + default: + lua_pushinteger(L, 0); + break; + } + } + return retct; +} +int32_t scriptlib::duel_check_phase_activity(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->core.phase_action); + return 1; +} +int32_t scriptlib::duel_add_custom_activity_counter(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_FUNCTION, 3); + int32_t counter_id = (int32_t)lua_tointeger(L, 1); + int32_t activity_type = (int32_t)lua_tointeger(L, 2); + int32_t counter_filter = interpreter::get_function_handle(L, 3); + duel* pduel = interpreter::get_duel_info(L); + switch(activity_type) { + case ACTIVITY_SUMMON: { + pduel->game_field->core.summon_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + case ACTIVITY_NORMALSUMMON: { + pduel->game_field->core.normalsummon_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + case ACTIVITY_SPSUMMON: { + pduel->game_field->core.spsummon_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + case ACTIVITY_FLIPSUMMON: { + pduel->game_field->core.flipsummon_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + case ACTIVITY_ATTACK: { + pduel->game_field->core.attack_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + case ACTIVITY_BATTLE_PHASE: + break; + case ACTIVITY_CHAIN: { + pduel->game_field->core.chain_counter.emplace(counter_id, activity_map::mapped_type(counter_filter, 0)); + break; + } + default: + break; + } + return 0; +} +int32_t scriptlib::duel_get_custom_activity_count(lua_State *L) { + check_param_count(L, 3); + int32_t counter_id = (int32_t)lua_tointeger(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + int32_t activity_type = (int32_t)lua_tointeger(L, 3); + duel* pduel = interpreter::get_duel_info(L); + uint32_t val = 0; + switch(activity_type) { + case ACTIVITY_SUMMON: { + auto iter = pduel->game_field->core.summon_counter.find(counter_id); + if(iter != pduel->game_field->core.summon_counter.end()) + val = iter->second.second; + break; + } + case ACTIVITY_NORMALSUMMON: { + auto iter = pduel->game_field->core.normalsummon_counter.find(counter_id); + if(iter != pduel->game_field->core.normalsummon_counter.end()) + val = iter->second.second; + break; + } + case ACTIVITY_SPSUMMON: { + auto iter = pduel->game_field->core.spsummon_counter.find(counter_id); + if(iter != pduel->game_field->core.spsummon_counter.end()) + val = iter->second.second; + break; + } + case ACTIVITY_FLIPSUMMON: { + auto iter = pduel->game_field->core.flipsummon_counter.find(counter_id); + if(iter != pduel->game_field->core.flipsummon_counter.end()) + val = iter->second.second; + break; + } + case ACTIVITY_ATTACK: { + auto iter = pduel->game_field->core.attack_counter.find(counter_id); + if(iter != pduel->game_field->core.attack_counter.end()) + val = iter->second.second; + break; + } + case ACTIVITY_BATTLE_PHASE: + break; + case ACTIVITY_CHAIN: { + auto iter = pduel->game_field->core.chain_counter.find(counter_id); + if(iter != pduel->game_field->core.chain_counter.end()) + val = iter->second.second; + break; + } + default: + break; + } + if(playerid == 0) + lua_pushinteger(L, val & 0xffff); + else + lua_pushinteger(L, (val >> 16) & 0xffff); + return 1; +} + +int32_t scriptlib::duel_get_battled_count(lua_State *L) { + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + lua_pushinteger(L, pduel->game_field->core.battled_count[playerid]); + return 1; +} +int32_t scriptlib::duel_is_able_to_enter_bp(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + lua_pushboolean(L, pduel->game_field->is_able_to_enter_bp()); + return 1; +} +int32_t scriptlib::duel_swap_deck_and_grave(lua_State *L) { + check_action_permission(L); + check_param_count(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 1); + if(playerid != 0 && playerid != 1) + return 0; + duel* pduel = interpreter::get_duel_info(L); + pduel->game_field->swap_deck_and_grave(playerid); + return 0; +} +int32_t scriptlib::duel_majestic_copy(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_CARD, 1); + check_param(L, PARAM_TYPE_CARD, 2); + card* pcard = *(card**) lua_touserdata(L, 1); + card* copy_target = *(card**) lua_touserdata(L, 2); + for(auto& peffect: copy_target->initial_effect) { + if (!(peffect->type & (EFFECT_TYPES_CHAIN_LINK & ~EFFECT_TYPE_ACTIVATE))) + continue; + if (peffect->type & EFFECT_TYPE_XMATERIAL) + continue; + if (!peffect->is_monster_effect()) + continue; + effect* ceffect = peffect->clone(); + ceffect->owner = pcard; + ceffect->flag[0] &= ~EFFECT_FLAG_INITIAL; + ceffect->effect_owner = PLAYER_NONE; + ceffect->reset_flag = RESET_EVENT | RESETS_STANDARD | RESET_PHASE | PHASE_END | RESET_SELF_TURN | RESET_OPPO_TURN; + ceffect->reset_count = 1; + ceffect->recharge(); + if(ceffect->type & EFFECT_TYPE_TRIGGER_F) { + ceffect->type &= ~EFFECT_TYPE_TRIGGER_F; + ceffect->type |= EFFECT_TYPE_TRIGGER_O; + ceffect->flag[0] |= EFFECT_FLAG_DELAY; + } + if(ceffect->type & EFFECT_TYPE_QUICK_F) { + ceffect->type &= ~EFFECT_TYPE_QUICK_F; + ceffect->type |= EFFECT_TYPE_QUICK_O; + } + pcard->add_effect(ceffect); + } + return 0; +} +int32_t scriptlib::duel_get_meta_value(lua_State *L) { + auto pduel = interpreter::get_duel_info(L); + return pduel->meta.luaop_get(L); +} +int32_t scriptlib::duel_set_meta_value(lua_State *L) { + auto pduel = interpreter::get_duel_info(L); + return pduel->meta.luaop_set(L); +} +int32_t scriptlib::duel_has_meta_value(lua_State *L) { + auto pduel = interpreter::get_duel_info(L); + return pduel->meta.luaop_has(L); +} +int32_t scriptlib::duel_get_meta_keys(lua_State *L) { + auto pduel = interpreter::get_duel_info(L); + return pduel->meta.luaop_keys(L); +} +int32_t scriptlib::duel_clear_meta(lua_State *L) { + auto pduel = interpreter::get_duel_info(L); + return pduel->meta.luaop_clear(); +} +int32_t scriptlib::duel_get_chain_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_INT, 1); + auto pduel = interpreter::get_duel_info(L); + auto c = (uint32_t)lua_tointeger(L, 1); + auto ch = pduel->game_field->get_chain(c); + if(!ch) { + lua_pushnil(L); + return 1; + } + return ch->meta.luaop_get(L, 1); +} +int32_t scriptlib::duel_set_chain_meta_value(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_INT, 1); + auto pduel = interpreter::get_duel_info(L); + auto c = (uint32_t)lua_tointeger(L, 1); + auto ch = pduel->game_field->get_chain(c); + if(!ch) { + lua_pushnil(L); + return 1; + } + return ch->meta.luaop_set(L, 1); +} +int32_t scriptlib::duel_has_chain_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_INT, 1); + auto pduel = interpreter::get_duel_info(L); + auto c = (uint32_t)lua_tointeger(L, 1); + auto ch = pduel->game_field->get_chain(c); + if(!ch) { + lua_pushnil(L); + return 1; + } + return ch->meta.luaop_has(L, 1); +} +int32_t scriptlib::duel_get_chain_meta_keys(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_INT, 1); + auto pduel = interpreter::get_duel_info(L); + auto c = (uint32_t)lua_tointeger(L, 1); + auto ch = pduel->game_field->get_chain(c); + if(!ch) { + lua_pushnil(L); + return 1; + } + return ch->meta.luaop_keys(L); +} +int32_t scriptlib::duel_clear_chain_meta(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_INT, 1); + auto pduel = interpreter::get_duel_info(L); + auto c = (uint32_t)lua_tointeger(L, 1); + auto ch = pduel->game_field->get_chain(c); + if(!ch) { + lua_pushnil(L); + return 1; + } + return ch->meta.luaop_clear(); +} + +static const struct luaL_Reg duellib[] = { + { "GetMasterRule", scriptlib::duel_get_master_rule }, + { "ReadCard", scriptlib::duel_read_card }, + { "Exile", scriptlib::duel_exile }, + { "DisableActionCheck", scriptlib::duel_disable_action_check }, + { "SetMetatable", scriptlib::duel_setmetatable }, + { "MoveTurnCount", scriptlib::duel_move_turn_count }, + { "GetCardsInZone", scriptlib::duel_get_cards_in_zone }, + { "XyzSummonByRose", scriptlib::duel_xyz_summon_by_rose }, + { "LoadScript", scriptlib::duel_load_script }, + { "AnnounceCardFilter", scriptlib::duel_announce_card }, // For compat + { "AnnounceCardSilent", scriptlib::duel_announce_card_silent }, + { "ResetTimeLimit", scriptlib::duel_reset_time_limit }, + { "SetSummonCancelable", scriptlib::duel_set_summon_cancelable }, + { "GetRandomNumber", scriptlib::duel_get_random_number }, + { "GetRegistryValue", scriptlib::duel_get_registry_value }, + { "SetRegistryValue", scriptlib::duel_set_registry_value }, + { "GetRegistryKeys", scriptlib::duel_get_registry_keys }, + { "SetRegistry", scriptlib::duel_set_registry }, + { "GetRegistry", scriptlib::duel_get_registry }, + { "ClearRegistry", scriptlib::duel_clear_registry }, + { "IsGlobalFlag", scriptlib::duel_is_global_flag }, + + { "EnableGlobalFlag", scriptlib::duel_enable_global_flag }, + { "GetLP", scriptlib::duel_get_lp }, + { "SetLP", scriptlib::duel_set_lp }, + { "IsTurnPlayer", scriptlib::duel_is_turn_player }, + { "GetTurnPlayer", scriptlib::duel_get_turn_player }, + { "GetTurnCount", scriptlib::duel_get_turn_count }, + { "GetDrawCount", scriptlib::duel_get_draw_count }, + { "RegisterEffect", scriptlib::duel_register_effect }, + { "RegisterFlagEffect", scriptlib::duel_register_flag_effect }, + { "GetFlagEffect", scriptlib::duel_get_flag_effect }, + { "ResetFlagEffect", scriptlib::duel_reset_flag_effect }, + { "SetFlagEffectLabel", scriptlib::duel_set_flag_effect_label }, + { "GetFlagEffectLabel", scriptlib::duel_get_flag_effect_label }, + { "Destroy", scriptlib::duel_destroy }, + { "Remove", scriptlib::duel_remove }, + { "SendtoGrave", scriptlib::duel_sendto_grave }, + { "SendtoHand", scriptlib::duel_sendto_hand }, + { "SendtoDeck", scriptlib::duel_sendto_deck }, + { "SendtoExtraP", scriptlib::duel_sendto_extra }, + { "GetOperatedGroup", scriptlib::duel_get_operated_group }, + { "Summon", scriptlib::duel_summon }, + { "SpecialSummonRule", scriptlib::duel_special_summon_rule }, + { "SynchroSummon", scriptlib::duel_synchro_summon }, + { "XyzSummon", scriptlib::duel_xyz_summon }, + { "LinkSummon", scriptlib::duel_link_summon }, + { "MSet", scriptlib::duel_setm }, + { "SSet", scriptlib::duel_sets }, + { "CreateToken", scriptlib::duel_create_token }, + { "SpecialSummon", scriptlib::duel_special_summon }, + { "SpecialSummonStep", scriptlib::duel_special_summon_step }, + { "SpecialSummonComplete", scriptlib::duel_special_summon_complete }, + { "IsCanAddCounter", scriptlib::duel_is_can_add_counter }, + { "RemoveCounter", scriptlib::duel_remove_counter }, + { "IsCanRemoveCounter", scriptlib::duel_is_can_remove_counter }, + { "GetCounter", scriptlib::duel_get_counter }, + { "ChangePosition", scriptlib::duel_change_form }, + { "Release", scriptlib::duel_release }, + { "MoveToField", scriptlib::duel_move_to_field }, + { "ReturnToField", scriptlib::duel_return_to_field }, + { "MoveSequence", scriptlib::duel_move_sequence }, + { "SwapSequence", scriptlib::duel_swap_sequence }, + { "Activate", scriptlib::duel_activate_effect }, + { "SetChainLimit", scriptlib::duel_set_chain_limit }, + { "SetChainLimitTillChainEnd", scriptlib::duel_set_chain_limit_p }, + { "GetChainMaterial", scriptlib::duel_get_chain_material }, + { "ConfirmDecktop", scriptlib::duel_confirm_decktop }, + { "ConfirmExtratop", scriptlib::duel_confirm_extratop }, + { "ConfirmCards", scriptlib::duel_confirm_cards }, + { "SortDecktop", scriptlib::duel_sort_decktop }, + { "CheckEvent", scriptlib::duel_check_event }, + { "RaiseEvent", scriptlib::duel_raise_event }, + { "RaiseSingleEvent", scriptlib::duel_raise_single_event }, + { "CheckTiming", scriptlib::duel_check_timing }, + { "IsEnvironment", scriptlib::duel_is_environment }, + { "Win", scriptlib::duel_win }, + { "Draw", scriptlib::duel_draw }, + { "Damage", scriptlib::duel_damage }, + { "Recover", scriptlib::duel_recover }, + { "RDComplete", scriptlib::duel_rd_complete }, + { "Equip", scriptlib::duel_equip }, + { "EquipComplete", scriptlib::duel_equip_complete }, + { "GetControl", scriptlib::duel_get_control }, + { "SwapControl", scriptlib::duel_swap_control }, + { "CheckLPCost", scriptlib::duel_check_lp_cost }, + { "PayLPCost", scriptlib::duel_pay_lp_cost }, + { "DiscardDeck", scriptlib::duel_discard_deck }, + { "DiscardHand", scriptlib::duel_discard_hand }, + { "DisableShuffleCheck", scriptlib::duel_disable_shuffle_check }, + { "DisableSelfDestroyCheck", scriptlib::duel_disable_self_destroy_check }, + { "RevealSelectDeckSequence", scriptlib::duel_reveal_select_deck_sequence }, + { "ShuffleDeck", scriptlib::duel_shuffle_deck }, + { "ShuffleExtra", scriptlib::duel_shuffle_extra }, + { "ShuffleHand", scriptlib::duel_shuffle_hand }, + { "ShuffleSetCard", scriptlib::duel_shuffle_setcard }, + { "ChangeAttacker", scriptlib::duel_change_attacker }, + { "ChangeAttackTarget", scriptlib::duel_change_attack_target }, + { "CalculateDamage", scriptlib::duel_calculate_damage }, + { "GetBattleDamage", scriptlib::duel_get_battle_damage }, + { "ChangeBattleDamage", scriptlib::duel_change_battle_damage }, + { "ChangeTargetCard", scriptlib::duel_change_target }, + { "ChangeTargetPlayer", scriptlib::duel_change_target_player }, + { "ChangeTargetParam", scriptlib::duel_change_target_param }, + { "BreakEffect", scriptlib::duel_break_effect }, + { "ChangeChainOperation", scriptlib::duel_change_effect }, + { "NegateActivation", scriptlib::duel_negate_activate }, + { "NegateEffect", scriptlib::duel_negate_effect }, + { "NegateRelatedChain", scriptlib::duel_negate_related_chain }, + { "NegateSummon", scriptlib::duel_disable_summon }, + { "IncreaseSummonedCount", scriptlib::duel_increase_summon_count }, + { "CheckSummonedCount", scriptlib::duel_check_summon_count }, + { "GetLocationCount", scriptlib::duel_get_location_count }, + { "GetMZoneCount", scriptlib::duel_get_mzone_count }, + { "GetSZoneCount", scriptlib::duel_get_szone_count }, + { "GetLocationCountFromEx", scriptlib::duel_get_location_count_fromex }, + { "GetUsableMZoneCount", scriptlib::duel_get_usable_mzone_count }, + { "GetLinkedGroup", scriptlib::duel_get_linked_group }, + { "GetLinkedGroupCount", scriptlib::duel_get_linked_group_count }, + { "GetLinkedZone", scriptlib::duel_get_linked_zone }, + { "GetFieldCard", scriptlib::duel_get_field_card }, + { "CheckLocation", scriptlib::duel_check_location }, + { "GetCurrentChain", scriptlib::duel_get_current_chain }, + { "GetReadyChain", scriptlib::duel_get_ready_chain }, + { "GetChainInfo", scriptlib::duel_get_chain_info }, + { "GetChainEvent", scriptlib::duel_get_chain_event }, + { "GetFirstTarget", scriptlib::duel_get_first_target }, + { "GetTargetsRelateToChain", scriptlib::duel_get_targets_relate_to_chain }, + { "IsPhase", scriptlib::duel_is_phase }, + { "IsMainPhase", scriptlib::duel_is_main_phase }, + { "IsBattlePhase", scriptlib::duel_is_battle_phase }, + { "GetCurrentPhase", scriptlib::duel_get_current_phase }, + { "SkipPhase", scriptlib::duel_skip_phase }, + { "IsDamageCalculated", scriptlib::duel_is_damage_calculated }, + { "GetAttacker", scriptlib::duel_get_attacker }, + { "GetAttackTarget", scriptlib::duel_get_attack_target }, + { "GetBattleMonster", scriptlib::duel_get_battle_monster }, + { "NegateAttack", scriptlib::duel_disable_attack }, + { "ChainAttack", scriptlib::duel_chain_attack }, + { "Readjust", scriptlib::duel_readjust }, + { "AdjustInstantly", scriptlib::duel_adjust_instantly }, + { "AdjustAll", scriptlib::duel_adjust_all }, + { "GetFieldGroup", scriptlib::duel_get_field_group }, + { "GetFieldGroupCount", scriptlib::duel_get_field_group_count }, + { "GetDecktopGroup", scriptlib::duel_get_decktop_group }, + { "GetExtraTopGroup", scriptlib::duel_get_extratop_group }, + { "GetMatchingGroup", scriptlib::duel_get_matching_group }, + { "GetMatchingGroupCount", scriptlib::duel_get_matching_count }, + { "GetFirstMatchingCard", scriptlib::duel_get_first_matching_card }, + { "IsExistingMatchingCard", scriptlib::duel_is_existing_matching_card }, + { "SelectMatchingCard", scriptlib::duel_select_matching_cards }, + { "GetReleaseGroup", scriptlib::duel_get_release_group }, + { "GetReleaseGroupCount", scriptlib::duel_get_release_group_count }, + { "CheckReleaseGroup", scriptlib::duel_check_release_group }, + { "SelectReleaseGroup", scriptlib::duel_select_release_group }, + { "CheckReleaseGroupEx", scriptlib::duel_check_release_group_ex }, + { "SelectReleaseGroupEx", scriptlib::duel_select_release_group_ex }, + { "GetTributeGroup", scriptlib::duel_get_tribute_group }, + { "GetTributeCount", scriptlib::duel_get_tribute_count }, + { "CheckTribute", scriptlib::duel_check_tribute }, + { "SelectTribute", scriptlib::duel_select_tribute }, + { "GetTargetCount", scriptlib::duel_get_target_count }, + { "IsExistingTarget", scriptlib::duel_is_existing_target }, + { "SelectTarget", scriptlib::duel_select_target }, + { "GetMustMaterial", scriptlib::duel_get_must_material }, + { "CheckMustMaterial", scriptlib::duel_check_must_material }, + { "SelectFusionMaterial", scriptlib::duel_select_fusion_material }, + { "SetFusionMaterial", scriptlib::duel_set_fusion_material }, + { "SetSynchroMaterial", scriptlib::duel_set_synchro_material }, + { "GetSynchroMaterial", scriptlib::duel_get_synchro_material }, + { "SelectSynchroMaterial", scriptlib::duel_select_synchro_material }, + { "CheckSynchroMaterial", scriptlib::duel_check_synchro_material }, + { "SelectTunerMaterial", scriptlib::duel_select_tuner_material }, + { "CheckTunerMaterial", scriptlib::duel_check_tuner_material }, + { "GetRitualMaterial", scriptlib::duel_get_ritual_material }, + { "GetRitualMaterialEx", scriptlib::duel_get_ritual_material_ex }, + { "ReleaseRitualMaterial", scriptlib::duel_release_ritual_material }, + { "GetFusionMaterial", scriptlib::duel_get_fusion_material }, + { "IsSummonCancelable", scriptlib::duel_is_summon_cancelable }, + { "SetSelectedCard", scriptlib::duel_set_must_select_cards }, + { "GrabSelectedCard", scriptlib::duel_grab_must_select_cards }, + { "SetTargetCard", scriptlib::duel_set_target_card }, + { "ClearTargetCard", scriptlib::duel_clear_target_card }, + { "SetTargetPlayer", scriptlib::duel_set_target_player }, + { "SetTargetParam", scriptlib::duel_set_target_param }, + { "SetOperationInfo", scriptlib::duel_set_operation_info }, + { "GetOperationInfo", scriptlib::duel_get_operation_info }, + { "GetOperationCount", scriptlib::duel_get_operation_count }, + { "ClearOperationInfo", scriptlib::duel_clear_operation_info }, + { "CheckXyzMaterial", scriptlib::duel_check_xyz_material }, + { "SelectXyzMaterial", scriptlib::duel_select_xyz_material }, + { "Overlay", scriptlib::duel_overlay }, + { "GetOverlayGroup", scriptlib::duel_get_overlay_group }, + { "GetOverlayCount", scriptlib::duel_get_overlay_count }, + { "CheckRemoveOverlayCard", scriptlib::duel_check_remove_overlay_card }, + { "RemoveOverlayCard", scriptlib::duel_remove_overlay_card }, + { "Hint", scriptlib::duel_hint }, + { "GetLastSelectHint", scriptlib::duel_get_last_select_hint }, + { "HintSelection", scriptlib::duel_hint_selection }, + { "SelectEffectYesNo", scriptlib::duel_select_effect_yesno }, + { "SelectYesNo", scriptlib::duel_select_yesno }, + { "SelectOption", scriptlib::duel_select_option }, + { "SelectSequence", scriptlib::duel_select_sequence }, + { "SelectPosition", scriptlib::duel_select_position }, + { "SelectField", scriptlib::duel_select_field }, + { "SelectDisableField", scriptlib::duel_select_disable_field }, + { "AnnounceRace", scriptlib::duel_announce_race }, + { "AnnounceAttribute", scriptlib::duel_announce_attribute }, + { "AnnounceLevel", scriptlib::duel_announce_level }, + { "AnnounceCard", scriptlib::duel_announce_card }, + { "AnnounceType", scriptlib::duel_announce_type }, + { "AnnounceNumber", scriptlib::duel_announce_number }, + { "AnnounceCoin", scriptlib::duel_announce_coin }, + { "TossCoin", scriptlib::duel_toss_coin }, + { "TossDice", scriptlib::duel_toss_dice }, + { "RockPaperScissors", scriptlib::duel_rock_paper_scissors }, + { "GetCoinResult", scriptlib::duel_get_coin_result }, + { "GetDiceResult", scriptlib::duel_get_dice_result }, + { "SetCoinResult", scriptlib::duel_set_coin_result }, + { "SetDiceResult", scriptlib::duel_set_dice_result }, + { "IsPlayerAffectedByEffect", scriptlib::duel_is_player_affected_by_effect }, + { "IsPlayerCanDraw", scriptlib::duel_is_player_can_draw }, + { "IsPlayerCanDiscardDeck", scriptlib::duel_is_player_can_discard_deck }, + { "IsPlayerCanDiscardDeckAsCost", scriptlib::duel_is_player_can_discard_deck_as_cost }, + { "IsPlayerCanSummon", scriptlib::duel_is_player_can_summon }, + { "IsPlayerCanMSet", scriptlib::duel_is_player_can_mset }, + { "IsPlayerCanSSet", scriptlib::duel_is_player_can_sset }, + { "IsPlayerCanSpecialSummon", scriptlib::duel_is_player_can_spsummon }, + { "IsPlayerCanFlipSummon", scriptlib::duel_is_player_can_flipsummon }, + { "IsPlayerCanSpecialSummonMonster", scriptlib::duel_is_player_can_spsummon_monster }, + { "IsPlayerCanSpecialSummonCount", scriptlib::duel_is_player_can_spsummon_count }, + { "IsPlayerCanRelease", scriptlib::duel_is_player_can_release }, + { "IsPlayerCanRemove", scriptlib::duel_is_player_can_remove }, + { "IsPlayerCanSendtoHand", scriptlib::duel_is_player_can_send_to_hand }, + { "IsPlayerCanSendtoGrave", scriptlib::duel_is_player_can_send_to_grave }, + { "IsPlayerCanSendtoDeck", scriptlib::duel_is_player_can_send_to_deck }, + { "IsPlayerCanAdditionalSummon", scriptlib::duel_is_player_can_additional_summon }, + { "IsChainSolving", scriptlib::duel_is_chain_solving }, + { "IsChainNegatable", scriptlib::duel_is_chain_negatable }, + { "IsChainDisablable", scriptlib::duel_is_chain_disablable }, + { "IsChainDisabled", scriptlib::duel_is_chain_disabled }, + { "CheckChainTarget", scriptlib::duel_check_chain_target }, + { "CheckChainUniqueness", scriptlib::duel_check_chain_uniqueness }, + { "GetActivityCount", scriptlib::duel_get_activity_count }, + { "CheckPhaseActivity", scriptlib::duel_check_phase_activity }, + { "AddCustomActivityCounter", scriptlib::duel_add_custom_activity_counter }, + { "GetCustomActivityCount", scriptlib::duel_get_custom_activity_count }, + { "GetBattledCount", scriptlib::duel_get_battled_count }, + { "IsAbleToEnterBP", scriptlib::duel_is_able_to_enter_bp }, + { "SwapDeckAndGrave", scriptlib::duel_swap_deck_and_grave }, + { "MajesticCopy", scriptlib::duel_majestic_copy }, + { "GetMetaValue", scriptlib::duel_get_meta_value }, + { "SetMetaValue", scriptlib::duel_set_meta_value }, + { "HasMetaValue", scriptlib::duel_has_meta_value }, + { "ClearMeta", scriptlib::duel_clear_meta }, + { "GetMetaKeys", scriptlib::duel_get_meta_keys }, + { "GetChainMetaValue", scriptlib::duel_get_chain_meta_value }, + { "SetChainMetaValue", scriptlib::duel_set_chain_meta_value }, + { "HasChainMetaValue", scriptlib::duel_has_chain_meta_value }, + { "ClearChainMeta", scriptlib::duel_clear_chain_meta }, + { "GetChainMetaKeys", scriptlib::duel_get_chain_meta_keys }, + { nullptr, nullptr } +}; +void scriptlib::open_duellib(lua_State *L) { + luaL_newlib(L, duellib); + lua_setglobal(L, "Duel"); +} diff --git a/core/libeffect.cpp b/core/libeffect.cpp new file mode 100644 index 0000000..bcb1104 --- /dev/null +++ b/core/libeffect.cpp @@ -0,0 +1,732 @@ +/* + * libeffect.cpp + * + * Created on: 2010-7-20 + * Author: Argon + */ + +#include "scriptlib.h" +#include "duel.h" +#include "field.h" +#include "card.h" +#include "effect.h" +#include "group.h" +int32_t scriptlib::effect_set_owner(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + check_param(L, PARAM_TYPE_CARD, 2); + effect* peffect = *(effect**) lua_touserdata(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + peffect->owner = pcard; + return 0; +} +int32_t scriptlib::effect_get_count_limit(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t args = 0; + if (peffect && (peffect->flag[0] & EFFECT_FLAG_COUNT_LIMIT)) { + args = args + 1; + lua_pushinteger(L, peffect->count_limit_max); + if (peffect->count_code) { + args = args + 1; + lua_pushinteger(L, peffect->count_code); + } + } + return args; +} + +int32_t scriptlib::get_effect_property(lua_State* L, effect_member type) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**)lua_touserdata(L, 1); + lua_Integer value{}; + if (peffect) { + switch (type) { + case MEMBER_CATEGORY: + value = (lua_Integer)peffect->category; + break; + case MEMBER_CODE: + value = peffect->code; + break; + case MEMBER_DESCRIPTION: + value = peffect->description; + break; + case MEMBER_ID: + value = peffect->id; + break; + case MEMBER_RANGE: + value = peffect->range; + break; + case MEMBER_TYPE: + value = peffect->type; + break; + } + } + lua_pushinteger(L, value); + return 1; +} +int32_t scriptlib::is_effect_has_property(lua_State* L, effect_member type) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**)lua_touserdata(L, 1); + uint64_t value{}; + if (peffect) { + switch (type) { + case MEMBER_CATEGORY: + value = peffect->category; + break; + case MEMBER_RANGE: + value = peffect->range; + break; + case MEMBER_TYPE: + value = peffect->type; + break; + } + } + uint64_t x = lua_tointeger(L, 2); + if (value & x) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::effect_new(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_CARD, 1); + card* pcard = *(card**) lua_touserdata(L, 1); + duel* pduel = pcard->pduel; + effect* peffect = pduel->new_effect(); + peffect->effect_owner = pduel->game_field->core.reason_player; + peffect->owner = pcard; + interpreter::effect2value(L, peffect); + return 1; +} +int32_t scriptlib::effect_newex(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + effect* peffect = pduel->new_effect(); + peffect->effect_owner = 0; + peffect->owner = pduel->game_field->temp_card; + interpreter::effect2value(L, peffect); + return 1; +} +int32_t scriptlib::effect_clone(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + effect* ceffect = peffect->clone(); + interpreter::effect2value(L, ceffect); + return 1; +} +int32_t scriptlib::effect_reset(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->owner == 0 || peffect->handler == 0) + return 0; + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + peffect->pduel->game_field->remove_effect(peffect); + else + peffect->handler->remove_effect(peffect); + return 0; +} +int32_t scriptlib::effect_get_field_id(lua_State *L) { + return get_effect_property(L, MEMBER_ID); +} +int32_t scriptlib::effect_set_description(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + peffect->description = v; + return 0; +} +int32_t scriptlib::effect_set_code(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + peffect->code = v; + return 0; +} +int32_t scriptlib::effect_set_range(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + peffect->range = v; + return 0; +} +int32_t scriptlib::effect_set_target_range(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t s = (uint32_t)lua_tointeger(L, 2); + uint32_t o = (uint32_t)lua_tointeger(L, 3); + peffect->s_range = s; + peffect->o_range = o; + peffect->flag[0] &= ~EFFECT_FLAG_ABSOLUTE_TARGET; + return 0; +} +int32_t scriptlib::effect_set_absolute_range(lua_State *L) { + check_param_count(L, 4); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + uint32_t s = (uint32_t)lua_tointeger(L, 3); + uint32_t o = (uint32_t)lua_tointeger(L, 4); + if(playerid == 0) { + peffect->s_range = s; + peffect->o_range = o; + } else { + peffect->s_range = o; + peffect->o_range = s; + } + peffect->flag[0] |= EFFECT_FLAG_ABSOLUTE_TARGET; + return 0; +} +int32_t scriptlib::effect_set_count_limit(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + uint32_t code = 0; + if(lua_gettop(L) >= 3) + code = (uint32_t)lua_tointeger(L, 3); + if(v == 0) + v = 1; + if(code == EFFECT_COUNT_CODE_CHAIN) + code = EFFECT_COUNT_CODE_CHAIN | EFFECT_COUNT_CODE_SINGLE; + peffect->flag[0] |= EFFECT_FLAG_COUNT_LIMIT; + peffect->count_limit = v; + peffect->count_limit_max = v; + peffect->count_code = code; + return 0; +} +int32_t scriptlib::effect_set_reset(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + int32_t c = (int32_t)lua_tointeger(L, 3); + if(c == 0) + c = 1; + if(v & (RESET_PHASE) && !(v & (RESET_SELF_TURN | RESET_OPPO_TURN))) + v |= (RESET_SELF_TURN | RESET_OPPO_TURN); + peffect->reset_flag = v; + peffect->reset_count = c; + return 0; +} +int32_t scriptlib::effect_set_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t v = (uint32_t)lua_tointeger(L, 2); + if (v & EFFECT_TYPE_ACTIVATE) { + v = EFFECT_TYPE_FIELD | EFFECT_TYPE_ACTIVATE; + peffect->range = LOCATION_SZONE + LOCATION_FZONE + LOCATION_HAND; + } + else if(v & EFFECT_TYPE_FLIP) { + peffect->code = EVENT_FLIP; + if (v & EFFECT_TYPE_TRIGGER_O) { + v = EFFECT_TYPE_SINGLE | EFFECT_TYPE_FLIP | EFFECT_TYPE_TRIGGER_O; + peffect->flag[0] |= EFFECT_FLAG_DELAY; + } + else { + v = EFFECT_TYPE_SINGLE | EFFECT_TYPE_FLIP | EFFECT_TYPE_TRIGGER_F; + } + } + else if (v & (EFFECT_TYPE_IGNITION | EFFECT_TYPE_QUICK_O | EFFECT_TYPE_QUICK_F)) { + v |= EFFECT_TYPE_FIELD; + } + if (v & (EFFECT_TYPES_CHAIN_LINK | EFFECT_TYPE_CONTINUOUS)) + v |= EFFECT_TYPE_ACTIONS; + else + v &= ~EFFECT_TYPE_ACTIONS; + peffect->type = v; + return 0; +} +int32_t scriptlib::effect_set_property(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint64_t v1 = lua_tointeger(L, 2); + uint64_t v2 = lua_tointeger(L, 3); + peffect->flag[0] = (peffect->flag[0] & INTERNAL_FLAGS) | (v1 & ~INTERNAL_FLAGS); + peffect->flag[1] = v2; + return 0; +} +int32_t scriptlib::effect_set_label(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + peffect->label.clear(); + for(int32_t i = 2; i <= lua_gettop(L); ++i) { + peffect->label.push_back(lua_tointeger(L, i)); + } + return 0; +} +int32_t scriptlib::effect_set_label_object(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(lua_isnil(L, 2)) { + peffect->label_object = 0; + peffect->object_type = PARAM_TYPE_INT; + return 0; + } + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* p = *(card**)lua_touserdata(L, 2); + peffect->label_object = p->ref_handle; + peffect->object_type = PARAM_TYPE_CARD; + } else if(check_param(L, PARAM_TYPE_EFFECT, 2, TRUE)) { + effect* p = *(effect**)lua_touserdata(L, 2); + peffect->label_object = p->ref_handle; + peffect->object_type = PARAM_TYPE_EFFECT; + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + group* p = *(group**)lua_touserdata(L, 2); + peffect->label_object = p->ref_handle; + peffect->object_type = PARAM_TYPE_GROUP; + } else + return luaL_error(L, "Parameter 2 should be \"Card\" or \"Effect\" or \"Group\"."); + return 0; +} +int32_t scriptlib::effect_set_category(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint64_t v = (uint64_t)lua_tointeger(L, 2); + peffect->category = v; + return 0; +} +int32_t scriptlib::effect_set_hint_timing(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t vs = (uint32_t)lua_tointeger(L, 2); + uint32_t vo = vs; + if(lua_gettop(L) >= 3) + vo = (uint32_t)lua_tointeger(L, 3); + peffect->hint_timing[0] = vs; + peffect->hint_timing[1] = vo; + return 0; +} +int32_t scriptlib::effect_set_condition(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->condition) + luaL_unref(L, LUA_REGISTRYINDEX, peffect->condition); + peffect->condition = interpreter::get_function_handle(L, 2); + return 0; +} +int32_t scriptlib::effect_set_target(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->target) + luaL_unref(L, LUA_REGISTRYINDEX, peffect->target); + peffect->target = interpreter::get_function_handle(L, 2); + return 0; +} +int32_t scriptlib::effect_set_cost(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->cost) + luaL_unref(L, LUA_REGISTRYINDEX, peffect->cost); + peffect->cost = interpreter::get_function_handle(L, 2); + return 0; +} +int32_t scriptlib::effect_set_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->value && peffect->is_flag(EFFECT_FLAG_FUNC_VALUE)) + luaL_unref(L, LUA_REGISTRYINDEX, peffect->value); + if (lua_isfunction(L, 2)) { + peffect->value = interpreter::get_function_handle(L, 2); + peffect->flag[0] |= EFFECT_FLAG_FUNC_VALUE; + } else { + peffect->flag[0] &= ~EFFECT_FLAG_FUNC_VALUE; + if(lua_isboolean(L, 2)) + peffect->value = lua_toboolean(L, 2); + else if(lua_isinteger(L, 2)) + peffect->value = (int32_t)lua_tointeger(L, 2); + else + peffect->value = (int32_t)lua_tonumber(L, 2); + } + return 0; +} +int32_t scriptlib::effect_set_operation(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->operation) + luaL_unref(L, LUA_REGISTRYINDEX, peffect->operation); + if(!lua_isnil(L, 2)) { + check_param(L, PARAM_TYPE_FUNCTION, 2); + peffect->operation = interpreter::get_function_handle(L, 2); + } else + peffect->operation = 0; + return 0; +} +int32_t scriptlib::effect_set_owner_player(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + int32_t p = (int32_t)lua_tointeger(L, 2); + if(p != 0 && p != 1) + return 0; + peffect->effect_owner = p; + return 0; +} +int32_t scriptlib::effect_get_description(lua_State *L) { + return get_effect_property(L, MEMBER_DESCRIPTION); +} +int32_t scriptlib::effect_get_code(lua_State *L) { + return get_effect_property(L, MEMBER_CODE); +} +int32_t scriptlib::effect_get_type(lua_State *L) { + return get_effect_property(L, MEMBER_TYPE); +} +int32_t scriptlib::effect_get_property(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if (peffect) { + lua_pushinteger(L, peffect->flag[0]); + lua_pushinteger(L, peffect->flag[1]); + return 2; + } + return 0; +} +int32_t scriptlib::effect_get_label(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if (peffect) { + if(peffect->label.empty()) { + lua_pushinteger(L, 0); + return 1; + } + for(const auto& lab : peffect->label) + lua_pushinteger(L, lab); + return (int32_t)peffect->label.size(); + } + return 0; +} +int32_t scriptlib::effect_get_label_object(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if (!peffect->label_object) { + lua_pushnil(L); + return 1; + } + lua_rawgeti(L, LUA_REGISTRYINDEX, peffect->label_object); + if(lua_isuserdata(L, -1)) + return 1; + else { + lua_pop(L, 1); + lua_pushnil(L); + return 1; + } +} +int32_t scriptlib::effect_get_category(lua_State *L) { + return get_effect_property(L, MEMBER_CATEGORY); +} +int32_t scriptlib::effect_get_range(lua_State* L) { + return get_effect_property(L, MEMBER_RANGE); +} +int32_t scriptlib::effect_get_owner(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::card2value(L, peffect->get_owner()); + return 1; +} +int32_t scriptlib::effect_get_handler(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::card2value(L, peffect->get_handler()); + return 1; +} +int32_t scriptlib::effect_get_owner_player(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushinteger(L, peffect->get_owner_player()); + return 1; +} +int32_t scriptlib::effect_get_handler_player(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushinteger(L, peffect->get_handler_player()); + return 1; +} +int32_t scriptlib::effect_get_condition(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::function2value(L, peffect->condition); + return 1; +} +int32_t scriptlib::effect_get_target(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::function2value(L, peffect->target); + return 1; +} +int32_t scriptlib::effect_get_cost(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::function2value(L, peffect->cost); + return 1; +} +int32_t scriptlib::effect_get_value(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + if(peffect->is_flag(EFFECT_FLAG_FUNC_VALUE)) + interpreter::function2value(L, peffect->value); + else + lua_pushinteger(L, (int32_t)peffect->value); + return 1; +} +int32_t scriptlib::effect_get_operation(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + interpreter::function2value(L, peffect->operation); + return 1; +} +int32_t scriptlib::effect_get_active_type(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushinteger(L, peffect->get_active_type()); + return 1; +} +int32_t scriptlib::effect_is_active_type(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t type = (uint32_t)lua_tointeger(L, 2); + if(peffect->get_active_type() & type) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::effect_is_has_property(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint64_t tflag1 = lua_tointeger(L, 2); + uint64_t tflag2 = lua_tointeger(L, 3); + if (peffect && (!tflag1 || (peffect->flag[0] & tflag1)) && (!tflag2 || (peffect->flag[1] & tflag2))) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::effect_is_has_category(lua_State *L) { + return is_effect_has_property(L, MEMBER_CATEGORY); +} +int32_t scriptlib::effect_is_has_type(lua_State *L) { + return is_effect_has_property(L, MEMBER_TYPE); +} +int32_t scriptlib::effect_is_has_range(lua_State* L) { + return is_effect_has_property(L, MEMBER_RANGE); +} +int32_t scriptlib::effect_is_activatable(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint32_t neglect_loc = 0; + uint32_t neglect_target = 0; + if(lua_gettop(L) > 2) { + neglect_loc = lua_toboolean(L, 3); + if (lua_gettop(L) > 3) + neglect_target = lua_toboolean(L, 4); + } + lua_pushboolean(L, peffect->is_activateable(playerid, peffect->pduel->game_field->nil_event, 0, 0, neglect_target, neglect_loc)); + return 1; +} +int32_t scriptlib::effect_is_activated(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushboolean(L, (peffect->type & EFFECT_TYPES_CHAIN_LINK)); + return 1; +} +int32_t scriptlib::effect_is_cost_checked(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**)lua_touserdata(L, 1); + lua_pushboolean(L, peffect->cost_checked); + return 1; +} +int32_t scriptlib::effect_set_cost_check(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + uint8_t cost_check = lua_toboolean(L, 2); + peffect->cost_checked = cost_check; + return 0; +} +int32_t scriptlib::effect_get_activate_location(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushinteger(L, peffect->active_location); + return 1; +} +int32_t scriptlib::effect_get_activate_sequence(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + lua_pushinteger(L, peffect->active_sequence); + return 1; +} +int32_t scriptlib::effect_check_count_limit(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + int32_t p = (int32_t)lua_tointeger(L, 2); + lua_pushboolean(L, peffect->check_count_limit(p)); + return 1; +} +int32_t scriptlib::effect_use_count_limit(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + int32_t p = (int32_t)lua_tointeger(L, 2); + int32_t count = 1; + int32_t oath_only = 0; + uint32_t code = peffect->count_code; + if(lua_gettop(L) > 2) { + count = (int32_t)lua_tointeger(L, 3); + if (lua_gettop(L) > 3) + oath_only = lua_toboolean(L, 4); + } + if (!oath_only || code & EFFECT_COUNT_CODE_OATH) + while(count) { + peffect->dec_count(p); + --count; + } + return 0; +} +int32_t scriptlib::effect_get_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + return peffect->meta.luaop_get(L, 1); +} +int32_t scriptlib::effect_set_meta_value(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + return peffect->meta.luaop_set(L, 1); +} +int32_t scriptlib::effect_has_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + return peffect->meta.luaop_has(L, 1); +} +int32_t scriptlib::effect_get_meta_keys(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + return peffect->meta.luaop_keys(L); +} +int32_t scriptlib::effect_clear_meta(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_EFFECT, 1); + effect* peffect = *(effect**) lua_touserdata(L, 1); + return peffect->meta.luaop_clear(); +} + +static const struct luaL_Reg effectlib[] = { + { "SetOwner", scriptlib::effect_set_owner }, + { "GetCountLimit", scriptlib::effect_get_count_limit }, + + { "CreateEffect", scriptlib::effect_new }, + { "GlobalEffect", scriptlib::effect_newex }, + { "Clone", scriptlib::effect_clone }, + { "Reset", scriptlib::effect_reset }, + { "GetFieldID", scriptlib::effect_get_field_id }, + { "SetDescription", scriptlib::effect_set_description }, + { "SetCode", scriptlib::effect_set_code }, + { "SetRange", scriptlib::effect_set_range }, + { "SetTargetRange", scriptlib::effect_set_target_range }, + { "SetAbsoluteRange", scriptlib::effect_set_absolute_range }, + { "SetCountLimit", scriptlib::effect_set_count_limit }, + { "SetReset", scriptlib::effect_set_reset }, + { "SetType", scriptlib::effect_set_type }, + { "SetProperty", scriptlib::effect_set_property }, + { "SetLabel", scriptlib::effect_set_label }, + { "SetLabelObject", scriptlib::effect_set_label_object }, + { "SetCategory", scriptlib::effect_set_category }, + { "SetHintTiming", scriptlib::effect_set_hint_timing }, + { "SetCondition", scriptlib::effect_set_condition }, + { "SetTarget", scriptlib::effect_set_target }, + { "SetCost", scriptlib::effect_set_cost }, + { "SetValue", scriptlib::effect_set_value }, + { "SetOperation", scriptlib::effect_set_operation }, + { "SetOwnerPlayer", scriptlib::effect_set_owner_player }, + { "GetDescription", scriptlib::effect_get_description }, + { "GetCode", scriptlib::effect_get_code }, + { "GetType", scriptlib::effect_get_type }, + { "GetProperty", scriptlib::effect_get_property }, + { "GetLabel", scriptlib::effect_get_label }, + { "GetLabelObject", scriptlib::effect_get_label_object }, + { "GetCategory", scriptlib::effect_get_category }, + { "GetRange", scriptlib::effect_get_range }, + { "GetOwner", scriptlib::effect_get_owner }, + { "GetHandler", scriptlib::effect_get_handler }, + { "GetCondition", scriptlib::effect_get_condition }, + { "GetTarget", scriptlib::effect_get_target }, + { "GetCost", scriptlib::effect_get_cost }, + { "GetValue", scriptlib::effect_get_value }, + { "GetOperation", scriptlib::effect_get_operation }, + { "GetActiveType", scriptlib::effect_get_active_type }, + { "IsActiveType", scriptlib::effect_is_active_type }, + { "GetOwnerPlayer", scriptlib::effect_get_owner_player }, + { "GetHandlerPlayer", scriptlib::effect_get_handler_player }, + { "IsHasProperty", scriptlib::effect_is_has_property }, + { "IsHasCategory", scriptlib::effect_is_has_category }, + { "IsHasType", scriptlib::effect_is_has_type }, + { "IsHasRange", scriptlib::effect_is_has_range }, + { "IsActivatable", scriptlib::effect_is_activatable }, + { "IsActivated", scriptlib::effect_is_activated }, + { "IsCostChecked", scriptlib::effect_is_cost_checked }, + { "SetCostCheck", scriptlib::effect_set_cost_check }, + { "GetActivateLocation", scriptlib::effect_get_activate_location }, + { "GetActivateSequence", scriptlib::effect_get_activate_sequence }, + { "CheckCountLimit", scriptlib::effect_check_count_limit }, + { "UseCountLimit", scriptlib::effect_use_count_limit }, + { "GetMetaValue", scriptlib::effect_get_meta_value }, + { "SetMetaValue", scriptlib::effect_set_meta_value }, + { "HasMetaValue", scriptlib::effect_has_meta_value }, + { "ClearMeta", scriptlib::effect_clear_meta }, + { "GetMetaKeys", scriptlib::effect_get_meta_keys }, + { nullptr, nullptr } +}; +void scriptlib::open_effectlib(lua_State *L) { + luaL_newlib(L, effectlib); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + lua_setglobal(L, "Effect"); +} diff --git a/core/libgroup.cpp b/core/libgroup.cpp new file mode 100644 index 0000000..9b5527c --- /dev/null +++ b/core/libgroup.cpp @@ -0,0 +1,999 @@ +/* + * libgroup.cpp + * + * Created on: 2010-5-6 + * Author: Argon + */ + +#include "scriptlib.h" +#include "group.h" +#include "card.h" +#include "effect.h" +#include "duel.h" + +int32_t scriptlib::group_new(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + for(int32_t i = 1; i <= lua_gettop(L); ++i) { + if(!lua_isnil(L, i)) { + if(check_param(L, PARAM_TYPE_CARD, i, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, i); + pgroup->container.insert(pcard); + } else if (check_param(L, PARAM_TYPE_GROUP, i, TRUE)) { + group* mgroup = *(group**) lua_touserdata(L, i); + pgroup->container.insert(mgroup->container.begin(), mgroup->container.end()); + } else { + luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", i); + } + } + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::group_clone(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + group* newgroup = pduel->new_group(pgroup->container); + interpreter::group2value(L, newgroup); + return 1; +} +int32_t scriptlib::group_from_cards(lua_State *L) { + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < lua_gettop(L); ++i) { + if(!lua_isnil(L, i + 1)) { + check_param(L, PARAM_TYPE_CARD, i + 1); + card* pcard = *(card**) lua_touserdata(L, i + 1); + pgroup->container.insert(pcard); + } + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::group_delete(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = *(group**) lua_touserdata(L, 1); + if(pgroup->is_readonly != GTYPE_KEEP_ALIVE) + return 0; + pgroup->is_iterator_dirty = true; + pgroup->is_readonly = GTYPE_DEFAULT; + pduel->sgroups.insert(pgroup); + return 0; +} +int32_t scriptlib::group_keep_alive(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = *(group**) lua_touserdata(L, 1); + if(pgroup->is_readonly == GTYPE_READ_ONLY) + return 0; + pgroup->is_readonly = GTYPE_KEEP_ALIVE; + pduel->sgroups.erase(pgroup); + return 0; +} +int32_t scriptlib::group_clear(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + if (pgroup->is_readonly != GTYPE_READ_ONLY) { + pgroup->is_iterator_dirty = true; + pgroup->container.clear(); + } + return 0; +} +int32_t scriptlib::group_add_card(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + if (pgroup->is_readonly != GTYPE_READ_ONLY) { + pgroup->is_iterator_dirty = true; + for(int32_t i = 2; i <= lua_gettop(L); ++i) { + if(!lua_isnil(L, i)) { + if(check_param(L, PARAM_TYPE_CARD, i, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, i); + pgroup->container.insert(pcard); + } else if (check_param(L, PARAM_TYPE_GROUP, i, TRUE)) { + group* mgroup = *(group**) lua_touserdata(L, i); + pgroup->container.insert(mgroup->container.begin(), mgroup->container.end()); + } else { + luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", i); + } + } + } + } + return 0; +} +int32_t scriptlib::group_remove_card(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + if (pgroup->is_readonly != GTYPE_READ_ONLY) { + pgroup->is_iterator_dirty = true; + for(int32_t i = 2; i <= lua_gettop(L); ++i) { + if(!lua_isnil(L, i)) { + if(check_param(L, PARAM_TYPE_CARD, i, TRUE)) { + card* pcard = *(card**) lua_touserdata(L, i); + pgroup->container.erase(pcard); + } else if (check_param(L, PARAM_TYPE_GROUP, i, TRUE)) { + group* sgroup = *(group**) lua_touserdata(L, i); + for (auto& pcard : sgroup->container) { + pgroup->container.erase(pcard); + } + } else { + luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", i); + } + } + } + } + return 0; +} +int32_t scriptlib::group_get_next(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + if (pgroup->is_iterator_dirty) { + return luaL_error(L, "Called Group.GetNext without calling Group.GetFirst first."); + } + if(pgroup->it == pgroup->container.end()) + lua_pushnil(L); + else { + ++pgroup->it; + if (pgroup->it == pgroup->container.end()) + lua_pushnil(L); + else + interpreter::card2value(L, (*(pgroup->it))); + } + return 1; +} +int32_t scriptlib::group_get_first(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + pgroup->is_iterator_dirty = false; + if (pgroup->container.size()) { + pgroup->it = pgroup->container.begin(); + interpreter::card2value(L, (*(pgroup->it))); + } else + lua_pushnil(L); + return 1; +} +int32_t scriptlib::group_get_count(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + lua_pushinteger(L, pgroup->container.size()); + return 1; +} +int32_t scriptlib::group_filter(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 3, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 3); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 3, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 3); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + group* new_group = pduel->new_group(); + uint32_t extraargs = lua_gettop(L) - 3; + for(auto& pcard : cset) { + if(pduel->lua->check_filter(L, pcard, 2, extraargs)) { + new_group->container.insert(pcard); + } + } + interpreter::group2value(L, new_group); + return 1; +} +int32_t scriptlib::group_filter_count(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 3, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 3); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 3, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 3); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + uint32_t extraargs = lua_gettop(L) - 3; + uint32_t count = 0; + for (auto& pcard : cset) { + if(pduel->lua->check_filter(L, pcard, 2, extraargs)) + ++count; + } + lua_pushinteger(L, count); + return 1; +} +int32_t scriptlib::group_filter_select(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 3); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 6, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 6); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 6, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 6); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t min = (uint32_t)lua_tointeger(L, 4); + uint32_t max = (uint32_t)lua_tointeger(L, 5); + uint32_t extraargs = lua_gettop(L) - 6; + pduel->game_field->core.select_cards.clear(); + for (auto& pcard : cset) { + if(pduel->lua->check_filter(L, pcard, 3, extraargs)) + pduel->game_field->core.select_cards.push_back(pcard); + } + pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (max << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::group_select(lua_State *L) { + check_action_permission(L); + check_param_count(L, 5); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 5); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 5); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + pduel->game_field->core.select_cards.clear(); + for (auto& pcard : cset) { + pduel->game_field->core.select_cards.push_back(pcard); + } + pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (max << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::group_select_unselect(lua_State *L) { + check_action_permission(L); + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + group* select_group = *(group**)lua_touserdata(L, 1); + group* unselect_group = nullptr; + if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) + unselect_group = *(group**)lua_touserdata(L, 2); + duel* pduel = select_group->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 3); + if(playerid != 0 && playerid != 1) + return 0; + if(select_group->container.size() == 0 && (!unselect_group || unselect_group->container.size() == 0)) + return 0; + if(unselect_group) { + for(auto it = unselect_group->container.begin(); it != unselect_group->container.end(); ++it) { + card* pcard = *it; + for(auto it2 = select_group->container.begin(); it2 != select_group->container.end(); ++it2) { + if((*it2) == pcard) { + return 0; + } + } + } + } + uint32_t finishable = FALSE; + if(lua_gettop(L) > 3) { + finishable = lua_toboolean(L, 4); + } + uint32_t cancelable = FALSE; + if(lua_gettop(L) > 4) { + cancelable = lua_toboolean(L, 5); + } + uint32_t min = 1; + if(lua_gettop(L) > 5) { + min = (uint32_t)lua_tointeger(L, 6); + } + uint32_t max = 1; + if(lua_gettop(L) > 6) { + max = (uint32_t)lua_tointeger(L, 7); + } + if(min > max) + min = max; + pduel->game_field->core.select_cards.clear(); + pduel->game_field->core.unselect_cards.clear(); + for(auto it = select_group->container.begin(); it != select_group->container.end(); ++it) { + pduel->game_field->core.select_cards.push_back(*it); + } + if(unselect_group) { + for(auto it = unselect_group->container.begin(); it != unselect_group->container.end(); ++it) { + pduel->game_field->core.unselect_cards.push_back(*it); + } + } + pduel->game_field->add_process(PROCESSOR_SELECT_UNSELECT_CARD, 0, 0, 0, playerid + (cancelable << 16), min + (max << 16), finishable); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + if(pduel->game_field->returns.ivalue[0] == -1) { + lua_pushnil(L); + } else { + card* pcard; + const int32_t index = pduel->game_field->returns.bvalue[1]; + if(index < (int32_t)pduel->game_field->core.select_cards.size()) + pcard = pduel->game_field->core.select_cards[index]; + else + pcard = pduel->game_field->core.unselect_cards[index - pduel->game_field->core.select_cards.size()]; + interpreter::card2value(L, pcard); + } + return 1; + }); +} +int32_t scriptlib::group_random_select(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + int32_t playerid = (int32_t)lua_tointeger(L, 2); + int32_t count = (int32_t)lua_tointeger(L, 3); + int32_t no_hint = lua_toboolean(L, 4); + duel* pduel = pgroup->pduel; + group* newgroup = pduel->new_group(); + if (count > (int32_t)pgroup->container.size()) + count = (int32_t)pgroup->container.size(); + if(count == 0) { + interpreter::group2value(L, newgroup); + return 1; + } + if(count == pgroup->container.size()) + newgroup->container = pgroup->container; + else { + card_vector cv(pgroup->container.begin(), pgroup->container.end()); + int32_t back = (int32_t)cv.size() - 1; + for (int32_t i = 0; i < count; ++i) { + int32_t r = pduel->get_next_integer(i, back); + std::swap(cv[i], cv[r]); + } + newgroup->container.insert(cv.begin(), cv.begin() + count); + } + if(no_hint) { + interpreter::group2value(L, newgroup); + return 1; + } + pduel->write_buffer8(MSG_RANDOM_SELECTED); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + for(auto& pcard : newgroup->container) { + pduel->write_buffer32(pcard->get_info_location()); + } + interpreter::group2value(L, newgroup); + return 1; +} +int32_t scriptlib::group_cancelable_select(lua_State *L) { + check_action_permission(L); + check_param_count(L, 5); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 5, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 5); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 5, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 5); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + uint32_t min = (uint32_t)lua_tointeger(L, 3); + uint32_t max = (uint32_t)lua_tointeger(L, 4); + pduel->game_field->core.select_cards.clear(); + for (auto& pcard : cset) { + pduel->game_field->core.select_cards.push_back(pcard); + } + pduel->game_field->add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + 0x10000, min + (max << 16)); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + if(pduel->game_field->returns.ivalue[0] == -1) { + lua_pushnil(L); + } else { + group* pgroup = pduel->new_group(); + for(int32_t i = 0; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + interpreter::group2value(L, pgroup); + } + return 1; + }); +} +int32_t scriptlib::group_is_exists(lua_State *L) { + check_param_count(L, 4); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + card_set cset(pgroup->container); + if(check_param(L, PARAM_TYPE_CARD, 4, TRUE)) { + card* pexception = *(card**) lua_touserdata(L, 4); + cset.erase(pexception); + } else if(check_param(L, PARAM_TYPE_GROUP, 4, TRUE)) { + group* pexgroup = *(group**) lua_touserdata(L, 4); + for(auto& pcard : pexgroup->container) + cset.erase(pcard); + } + duel* pduel = pgroup->pduel; + uint32_t count = (uint32_t)lua_tointeger(L, 3); + uint32_t extraargs = lua_gettop(L) - 4; + uint32_t fcount = 0; + uint32_t result = FALSE; + if (count > pgroup->container.size()) { + lua_pushboolean(L, 0); + return 1; + } + for (auto& pcard : cset) { + if(pduel->lua->check_filter(L, pcard, 2, extraargs)) { + ++fcount; + if(fcount >= count) { + result = TRUE; + break; + } + } + } + lua_pushboolean(L, result); + return 1; +} +int32_t scriptlib::group_check_with_sum_equal(lua_State *L) { + check_param_count(L, 5); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t acc = (int32_t)lua_tointeger(L, 3); + int32_t min = (int32_t)lua_tointeger(L, 4); + int32_t max = (int32_t)lua_tointeger(L, 5); + if(min < 0) + min = 0; + if(max < min) + max = min; + int32_t extraargs = lua_gettop(L) - 5; + card_vector cv(pduel->game_field->core.must_select_cards); + int32_t mcount = (int32_t)cv.size(); + for(auto& pcard : pgroup->container) { + auto it = std::find(pduel->game_field->core.must_select_cards.begin(), pduel->game_field->core.must_select_cards.end(), pcard); + if(it == pduel->game_field->core.must_select_cards.end()) + cv.push_back(pcard); + } + pduel->game_field->core.must_select_cards.clear(); + for(auto& pcard : cv) + pcard->sum_param = pduel->lua->get_operation_value(pcard, 2, extraargs); + lua_pushboolean(L, field::check_with_sum_limit_m(cv, acc, 0, min, max, 0xffff, mcount)); + return 1; +} +int32_t scriptlib::group_select_with_sum_equal(lua_State *L) { + check_action_permission(L); + check_param_count(L, 6); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 3); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t acc = (int32_t)lua_tointeger(L, 4); + int32_t min = (int32_t)lua_tointeger(L, 5); + int32_t max = (int32_t)lua_tointeger(L, 6); + if(min < 0) + min = 0; + if(max < min) + max = min; + if(max > UINT8_MAX) + return luaL_error(L, "Parameter \"max\" exceeded 255."); + int32_t extraargs = lua_gettop(L) - 6; + pduel->game_field->core.select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + for(auto& pcard : pduel->game_field->core.must_select_cards) { + auto it = std::remove(pduel->game_field->core.select_cards.begin(), pduel->game_field->core.select_cards.end(), pcard); + pduel->game_field->core.select_cards.erase(it, pduel->game_field->core.select_cards.end()); + } + card_vector cv(pduel->game_field->core.must_select_cards); + int32_t mcount = (int32_t)cv.size(); + cv.insert(cv.end(), pduel->game_field->core.select_cards.begin(), pduel->game_field->core.select_cards.end()); + for(auto& pcard : cv) + pcard->sum_param = pduel->lua->get_operation_value(pcard, 3, extraargs); + if(!field::check_with_sum_limit_m(cv, acc, 0, min, max, 0xffff, mcount)) { + pduel->game_field->core.must_select_cards.clear(); + group* empty_group = pduel->new_group(); + interpreter::group2value(L, empty_group); + return 1; + } + pduel->game_field->add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, acc, playerid, min, max); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + int32_t mcount = (int32_t)pduel->game_field->core.must_select_cards.size(); + for(int32_t i = mcount; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + pduel->game_field->core.must_select_cards.clear(); + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::group_check_with_sum_greater(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t acc = (int32_t)lua_tointeger(L, 3); + int32_t extraargs = lua_gettop(L) - 3; + card_vector cv(pduel->game_field->core.must_select_cards); + int32_t mcount = (int32_t)cv.size(); + for(auto& pcard : pgroup->container) { + auto it = std::find(pduel->game_field->core.must_select_cards.begin(), pduel->game_field->core.must_select_cards.end(), pcard); + if(it == pduel->game_field->core.must_select_cards.end()) + cv.push_back(pcard); + } + pduel->game_field->core.must_select_cards.clear(); + for(auto& pcard : cv) + pcard->sum_param = pduel->lua->get_operation_value(pcard, 2, extraargs); + lua_pushboolean(L, field::check_with_sum_greater_limit_m(cv, acc, 0, 0xffff, mcount)); + return 1; +} +int32_t scriptlib::group_select_with_sum_greater(lua_State *L) { + check_action_permission(L); + check_param_count(L, 4); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 3); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t playerid = (int32_t)lua_tointeger(L, 2); + if(playerid != 0 && playerid != 1) + return 0; + int32_t acc = (int32_t)lua_tointeger(L, 4); + int32_t extraargs = lua_gettop(L) - 4; + pduel->game_field->core.select_cards.assign(pgroup->container.begin(), pgroup->container.end()); + for(auto& pcard : pduel->game_field->core.must_select_cards) { + auto it = std::remove(pduel->game_field->core.select_cards.begin(), pduel->game_field->core.select_cards.end(), pcard); + pduel->game_field->core.select_cards.erase(it, pduel->game_field->core.select_cards.end()); + } + card_vector cv(pduel->game_field->core.must_select_cards); + int32_t mcount = (int32_t)cv.size(); + cv.insert(cv.end(), pduel->game_field->core.select_cards.begin(), pduel->game_field->core.select_cards.end()); + for(auto& pcard : cv) + pcard->sum_param = pduel->lua->get_operation_value(pcard, 3, extraargs); + if(!field::check_with_sum_greater_limit_m(cv, acc, 0, 0xffff, mcount)) { + pduel->game_field->core.must_select_cards.clear(); + group* empty_group = pduel->new_group(); + interpreter::group2value(L, empty_group); + return 1; + } + pduel->game_field->add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, acc, playerid); + return lua_yieldk(L, 0, (lua_KContext)pduel, [](lua_State *L, int32_t status, lua_KContext ctx) { + duel* pduel = (duel*)ctx; + group* pgroup = pduel->new_group(); + int32_t mcount = (int32_t)pduel->game_field->core.must_select_cards.size(); + for(int32_t i = mcount; i < pduel->game_field->returns.bvalue[0]; ++i) { + card* pcard = pduel->game_field->core.select_cards[pduel->game_field->returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + pduel->game_field->core.must_select_cards.clear(); + interpreter::group2value(L, pgroup); + return 1; + }); +} +int32_t scriptlib::group_get_min_group(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + if(pgroup->container.size() == 0) + return 0; + group* newgroup = pduel->new_group(); + int32_t min, op; + int32_t extraargs = lua_gettop(L) - 2; + auto cit = pgroup->container.begin(); + min = pduel->lua->get_operation_value(*cit, 2, extraargs); + newgroup->container.insert(*cit); + ++cit; + for(; cit != pgroup->container.end(); ++cit) { + op = pduel->lua->get_operation_value(*cit, 2, extraargs); + if(op == min) + newgroup->container.insert(*cit); + else if(op < min) { + newgroup->container.clear(); + newgroup->container.insert(*cit); + min = op; + } + } + interpreter::group2value(L, newgroup); + lua_pushinteger(L, min); + return 2; +} +int32_t scriptlib::group_get_max_group(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + if(pgroup->container.size() == 0) + return 0; + group* newgroup = pduel->new_group(); + int32_t max, op; + int32_t extraargs = lua_gettop(L) - 2; + auto cit = pgroup->container.begin(); + max = pduel->lua->get_operation_value(*cit, 2, extraargs); + newgroup->container.insert(*cit); + ++cit; + for(; cit != pgroup->container.end(); ++cit) { + op = pduel->lua->get_operation_value(*cit, 2, extraargs); + if(op == max) + newgroup->container.insert(*cit); + else if(op > max) { + newgroup->container.clear(); + newgroup->container.insert(*cit); + max = op; + } + } + interpreter::group2value(L, newgroup); + lua_pushinteger(L, max); + return 2; +} +int32_t scriptlib::group_get_sum(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t extraargs = lua_gettop(L) - 2; + int32_t sum = 0; + for(auto& pcard : pgroup->container) { + sum += pduel->lua->get_operation_value(pcard, 2, extraargs); + } + lua_pushinteger(L, sum); + return 1; +} +int32_t scriptlib::group_get_class_count(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t extraargs = lua_gettop(L) - 2; + std::set er; + for(auto& pcard : pgroup->container) { + er.insert(pduel->lua->get_operation_value(pcard, 2, extraargs)); + } + lua_pushinteger(L, er.size()); + return 1; +} +int32_t scriptlib::group_remove(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + card* pexception = nullptr; + if(!lua_isnil(L, 3)) { + check_param(L, PARAM_TYPE_CARD, 3); + pexception = *(card**) lua_touserdata(L, 3); + } + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + uint32_t extraargs = lua_gettop(L) - 3; + if(pgroup->is_readonly == GTYPE_READ_ONLY) + return 0; + pgroup->is_iterator_dirty = true; + for (auto cit = pgroup->container.begin(); cit != pgroup->container.end();) { + if((*cit) != pexception && pduel->lua->check_filter(L, *cit, 2, extraargs)) { + cit = pgroup->container.erase(cit); + } + else + ++cit; + } + return 0; +} +int32_t scriptlib::group_equal(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_GROUP, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + group* sgroup = *(group**) lua_touserdata(L, 2); + if(pgroup->container.size() != sgroup->container.size()) { + lua_pushboolean(L, 0); + return 1; + } + auto pit = pgroup->container.begin(); + auto sit = sgroup->container.begin(); + for (; pit != pgroup->container.end(); ++pit, ++sit) { + if((*pit) != (*sit)) { + lua_pushboolean(L, 0); + return 1; + } + } + lua_pushboolean(L, 1); + return 1; +} +int32_t scriptlib::group_is_contains(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_CARD, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + card* pcard = *(card**) lua_touserdata(L, 2); + if(pgroup->container.find(pcard) != pgroup->container.end()) + lua_pushboolean(L, 1); + else + lua_pushboolean(L, 0); + return 1; +} +int32_t scriptlib::group_search_card(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + uint32_t extraargs = lua_gettop(L) - 2; + for (auto& pcard : pgroup->container) { + if (pduel->lua->check_filter(L, pcard, 2, extraargs)) { + interpreter::card2value(L, pcard); + return 1; + } + } + return 0; +} +int32_t scriptlib::group_get_bin_class_count(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + check_param(L, PARAM_TYPE_FUNCTION, 2); + group* pgroup = *(group**) lua_touserdata(L, 1); + duel* pduel = pgroup->pduel; + int32_t extraargs = lua_gettop(L) - 2; + int32_t er = 0; + for(auto& pcard : pgroup->container) { + er |= pduel->lua->get_operation_value(pcard, 2, extraargs); + } + int32_t ans = 0; + while(er) { + er &= er - 1; + ++ans; + } + lua_pushinteger(L, ans); + return 1; +} +int32_t scriptlib::group_get_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + return pgroup->meta.luaop_get(L, 1); +} +int32_t scriptlib::group_set_meta_value(lua_State *L) { + check_param_count(L, 3); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + return pgroup->meta.luaop_set(L, 1); +} +int32_t scriptlib::group_has_meta_value(lua_State *L) { + check_param_count(L, 2); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + return pgroup->meta.luaop_has(L, 1); +} +int32_t scriptlib::group_get_meta_keys(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + return pgroup->meta.luaop_keys(L); +} +int32_t scriptlib::group_clear_meta(lua_State *L) { + check_param_count(L, 1); + check_param(L, PARAM_TYPE_GROUP, 1); + group* pgroup = *(group**) lua_touserdata(L, 1); + return pgroup->meta.luaop_clear(); +} +int32_t scriptlib::group_meta_add(lua_State* L) { + check_param_count(L, 2); + if(!check_param(L, PARAM_TYPE_CARD, 1, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + if(!check_param(L, PARAM_TYPE_CARD, 2, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 1); + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 1); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + pgroup->container.insert(*cit); + } + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 2); + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 2); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + pgroup->container.insert(*cit); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::group_meta_sub(lua_State* L) { + check_param_count(L, 2); + if(!check_param(L, PARAM_TYPE_CARD, 1, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + if(!check_param(L, PARAM_TYPE_CARD, 2, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 1); + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 1); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + pgroup->container.insert(*cit); + } + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 2); + pgroup->container.erase(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 2); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + pgroup->container.erase(*cit); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::group_meta_band(lua_State* L) { + check_param_count(L, 2); + if(!check_param(L, PARAM_TYPE_CARD, 1, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + if(!check_param(L, PARAM_TYPE_CARD, 2, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + card_set check_set; + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 1); + check_set.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 1); + check_set = cgroup->container; + } + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 2); + if(check_set.find(ccard) != check_set.end()) + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 2); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + if(check_set.find(*cit) != check_set.end()) + pgroup->container.insert(*cit); + } + interpreter::group2value(L, pgroup); + return 1; +} +int32_t scriptlib::group_meta_bxor(lua_State* L) { + check_param_count(L, 2); + if(!check_param(L, PARAM_TYPE_CARD, 1, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 1); + if(!check_param(L, PARAM_TYPE_CARD, 2, TRUE) && !check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) + return luaL_error(L, "Parameter %d should be \"Card\" or \"Group\".", 2); + duel* pduel = interpreter::get_duel_info(L); + group* pgroup = pduel->new_group(); + if(check_param(L, PARAM_TYPE_CARD, 1, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 1); + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 1, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 1); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) + pgroup->container.insert(*cit); + } + if(check_param(L, PARAM_TYPE_CARD, 2, TRUE)) { + card* ccard = *(card**) lua_touserdata(L, 2); + if(pgroup->container.find(ccard) != pgroup->container.end()) + pgroup->container.erase(ccard); + else + pgroup->container.insert(ccard); + } else if(check_param(L, PARAM_TYPE_GROUP, 2, TRUE)) { + group* cgroup = *(group**) lua_touserdata(L, 2); + for(auto cit = cgroup->container.begin(); cit != cgroup->container.end(); ++cit) { + if(pgroup->container.find(*cit) != pgroup->container.end()) + pgroup->container.erase(*cit); + else + pgroup->container.insert(*cit); + } + } + interpreter::group2value(L, pgroup); + return 1; +} + +static const struct luaL_Reg grouplib[] = { + { "CreateGroup", scriptlib::group_new }, + { "FromCards", scriptlib::group_new }, + { "KeepAlive", scriptlib::group_keep_alive }, + { "DeleteGroup", scriptlib::group_delete }, + { "Clone", scriptlib::group_clone }, + { "Clear", scriptlib::group_clear }, + { "AddCard", scriptlib::group_add_card }, + { "Merge", scriptlib::group_add_card }, + { "RemoveCard", scriptlib::group_remove_card }, + { "Sub", scriptlib::group_remove_card }, + { "GetNext", scriptlib::group_get_next }, + { "GetFirst", scriptlib::group_get_first }, + { "GetCount", scriptlib::group_get_count }, + { "__len", scriptlib::group_get_count }, + { "Filter", scriptlib::group_filter }, + { "FilterCount", scriptlib::group_filter_count }, + { "FilterSelect", scriptlib::group_filter_select }, + { "Select", scriptlib::group_select }, + { "SelectUnselect", scriptlib::group_select_unselect }, + { "RandomSelect", scriptlib::group_random_select }, + { "CancelableSelect", scriptlib::group_cancelable_select }, + { "IsExists", scriptlib::group_is_exists }, + { "CheckWithSumEqual", scriptlib::group_check_with_sum_equal }, + { "SelectWithSumEqual", scriptlib::group_select_with_sum_equal }, + { "CheckWithSumGreater", scriptlib::group_check_with_sum_greater }, + { "SelectWithSumGreater", scriptlib::group_select_with_sum_greater }, + { "GetMinGroup", scriptlib::group_get_min_group }, + { "GetMaxGroup", scriptlib::group_get_max_group }, + { "GetSum", scriptlib::group_get_sum }, + { "GetClassCount", scriptlib::group_get_class_count }, + { "Remove", scriptlib::group_remove }, + { "Equal", scriptlib::group_equal }, + { "IsContains", scriptlib::group_is_contains }, + { "SearchCard", scriptlib::group_search_card }, + { "GetBinClassCount", scriptlib::group_get_bin_class_count }, + { "GetMetaValue", scriptlib::group_get_meta_value }, + { "SetMetaValue", scriptlib::group_set_meta_value }, + { "HasMetaValue", scriptlib::group_has_meta_value }, + { "ClearMeta", scriptlib::group_clear_meta }, + { "GetMetaKeys", scriptlib::group_get_meta_keys }, + { "__add", scriptlib::group_meta_add }, + { "__bor", scriptlib::group_meta_add }, + { "__sub", scriptlib::group_meta_sub }, + { "__band", scriptlib::group_meta_band }, + { "__bxor", scriptlib::group_meta_bxor }, + { nullptr, nullptr } +}; +void scriptlib::open_grouplib(lua_State *L) { + luaL_newlib(L, grouplib); + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + lua_setglobal(L, "Group"); +} diff --git a/core/lua_coverage.cpp b/core/lua_coverage.cpp new file mode 100644 index 0000000..85150be --- /dev/null +++ b/core/lua_coverage.cpp @@ -0,0 +1,155 @@ +#include "lua_coverage.h" + +#include +#include +#include +#include +#include "buffer.h" +#include "duel.h" +#include "interpreter.h" + +namespace { + +bool starts_with(const std::string& text, const char* prefix) { + const size_t len = std::strlen(prefix); + return text.size() >= len && text.compare(0, len, prefix) == 0; +} + +std::vector> sorted_hits(const lua_coverage_file& file) { + std::vector> records; + records.reserve(file.line_hits.size()); + for (const auto& hit : file.line_hits) + records.emplace_back(hit.first, hit.second); + std::sort(records.begin(), records.end(), [](const auto& left, const auto& right) { + return left.first < right.first; + }); + return records; +} + +} // namespace + +std::string lua_coverage_state::normalize_name(const char* source) { + std::string name = source ? source : ""; + if (!name.empty() && name[0] == '@') + name.erase(0, 1); + std::replace(name.begin(), name.end(), '\\', '/'); + if (starts_with(name, "./script/")) + name.erase(0, std::strlen("./script/")); + else if (starts_with(name, "script/")) + name.erase(0, std::strlen("script/")); + else if (starts_with(name, "./")) + name.erase(0, std::strlen("./")); + return name; +} + +void lua_coverage_state::line_hook(lua_State* L, lua_Debug* ar) { + if (ar->event != LUA_HOOKLINE || ar->currentline <= 0) + return; + duel* pd = interpreter::get_duel_info(L); + if (!pd || !pd->lua || !pd->lua->lua_coverage.enabled) + return; + lua_getinfo(L, "S", ar); + pd->lua->lua_coverage.record(ar->source, ar->currentline); +} + +void lua_coverage_state::enable(lua_State* L) { + enabled = true; + install_hook(L); +} + +void lua_coverage_state::install_hook(lua_State* L) const { + if (enabled) + lua_sethook(L, lua_coverage_state::line_hook, LUA_MASKLINE, 0); +} + +void lua_coverage_state::record(const char* source, int32_t line) { + if (!enabled || line <= 0) + return; + auto name = normalize_name(source); + if (name.empty()) + return; + auto& file = files[name]; + if (file.name.empty()) + file.name = name; + auto& count = file.line_hits[line]; + if (count != UINT32_MAX) + ++count; +} + +void lua_coverage_state::clear(const char* name) { + files.erase(normalize_name(name)); +} + +void lua_coverage_state::clear_all() { + files.clear(); +} + +int32_t lua_coverage_state::get_dump_size(const char* name) const { + if (!enabled) + return 0; + const auto it = files.find(normalize_name(name)); + if (it == files.end()) + return 0; + return static_cast(it->second.line_hits.size() * sizeof(uint32_t) * 2); +} + +int32_t lua_coverage_state::dump(const char* name, byte* out_buf, int32_t out_len) const { + const auto required = get_dump_size(name); + if (required <= 0) + return 0; + if (out_len < required) + return required; + const auto it = files.find(normalize_name(name)); + if (it == files.end()) + return 0; + byte* p = out_buf; + for (const auto& hit : sorted_hits(it->second)) { + buffer_write(p, static_cast(hit.first)); + buffer_write(p, hit.second); + } + return required; +} + +int32_t lua_coverage_state::get_all_dump_size() const { + if (!enabled || files.empty()) + return 0; + size_t size = sizeof(uint32_t); + for (const auto& entry : files) { + size += sizeof(uint16_t); + size += entry.first.size(); + size += sizeof(uint32_t); + size += entry.second.line_hits.size() * sizeof(uint32_t) * 2; + } + return static_cast(size); +} + +int32_t lua_coverage_state::dump_all(byte* out_buf, int32_t out_len) const { + const auto required = get_all_dump_size(); + if (required <= 0) + return 0; + if (out_len < required) + return required; + + std::vector ordered; + ordered.reserve(files.size()); + for (const auto& entry : files) + ordered.push_back(&entry.second); + std::sort(ordered.begin(), ordered.end(), [](const auto* left, const auto* right) { + return left->name < right->name; + }); + + byte* p = out_buf; + buffer_write(p, static_cast(ordered.size())); + for (const auto* file : ordered) { + const auto name_length = static_cast(file->name.size()); + buffer_write(p, name_length); + buffer_write_block(p, file->name.data(), name_length); + const auto hits = sorted_hits(*file); + buffer_write(p, static_cast(hits.size())); + for (const auto& hit : hits) { + buffer_write(p, static_cast(hit.first)); + buffer_write(p, hit.second); + } + } + return required; +} diff --git a/core/lua_coverage.h b/core/lua_coverage.h new file mode 100644 index 0000000..35bc3a1 --- /dev/null +++ b/core/lua_coverage.h @@ -0,0 +1,35 @@ +#ifndef LUA_COVERAGE_H_ +#define LUA_COVERAGE_H_ + +#include +#include +#include +#include +#include "common.h" + +struct lua_coverage_file { + std::string name; + std::unordered_map line_hits; +}; + +class lua_coverage_state { +public: + bool enabled{}; + std::unordered_map files; + + static std::string normalize_name(const char* source); + static void line_hook(lua_State* L, lua_Debug* ar); + + void enable(lua_State* L); + void install_hook(lua_State* L) const; + void record(const char* source, int32_t line); + void clear(const char* name); + void clear_all(); + + int32_t get_dump_size(const char* name) const; + int32_t dump(const char* name, byte* out_buf, int32_t out_len) const; + int32_t get_all_dump_size() const; + int32_t dump_all(byte* out_buf, int32_t out_len) const; +}; + +#endif /* LUA_COVERAGE_H_ */ diff --git a/core/metadata.cpp b/core/metadata.cpp new file mode 100644 index 0000000..ac94c95 --- /dev/null +++ b/core/metadata.cpp @@ -0,0 +1,231 @@ +#include "metadata.h" +#include "interpreter.h" +#include "scriptlib.h" +#include + +metadata_entry::metadata_entry() { + type = PARAM_TYPE_INT; + value = 0; +} + +metadata_entry::~metadata_entry() { + reset(); +} + +metadata_entry::metadata_entry(metadata_entry&& other) noexcept { + type = other.type; + value = other.value; + other.type = PARAM_TYPE_INT; + other.value = 0; +} + +metadata_entry& metadata_entry::operator=(metadata_entry&& other) noexcept { + if (this != &other) { + reset(); + type = other.type; + value = other.value; + other.type = PARAM_TYPE_INT; + other.value = 0; + } + return *this; +} + +int32_t metadata_entry::luaop_get(lua_State *L) { + if (type == PARAM_TYPE_INT) { + lua_pushinteger(L, static_cast(value)); + return 1; + } + if (type == PARAM_TYPE_INDEX) { + lua_pushnumber(L, *reinterpret_cast(value)); + return 1; + } + if (type == PARAM_TYPE_STRING) { + lua_pushstring(L, reinterpret_cast(value)); + return 1; + } + if (type == PARAM_TYPE_BOOLEAN) { + lua_pushboolean(L, value != 0); + return 1; + } + if (type == PARAM_TYPE_CARD) { + interpreter::card2value(L, reinterpret_cast(value)); + return 1; + } + if (type == PARAM_TYPE_GROUP) { + interpreter::group2value(L, reinterpret_cast(value)); + return 1; + } + if (type == PARAM_TYPE_EFFECT) { + interpreter::effect2value(L, reinterpret_cast(value)); + return 1; + } + if (type == PARAM_TYPE_FUNCTION) { + interpreter::function2value(L, value); + return 1; + } + lua_pushnil(L); + return 1; +} + +int32_t metadata_entry::luaop_set(lua_State *L, int32_t index) { + reset(); + if (lua_isnil(L, index)) { + // should never reach here because nil would remove the entry + value = 0; + type = PARAM_TYPE_INT; + return 0; + } + if (lua_isboolean(L, index)) { + value = lua_toboolean(L, index); + type = PARAM_TYPE_BOOLEAN; + return 0; + } + if (lua_isinteger(L, index)) { + value = lua_tointeger(L, index); + type = PARAM_TYPE_INT; + return 0; + } + if (lua_isnumber(L, index)) { + auto num = lua_tonumber(L, index); + auto saved_num = new double(num); + value = reinterpret_cast(saved_num); + type = PARAM_TYPE_INDEX; + return 0; + } + if (lua_isstring(L, index)) { + auto str = lua_tostring(L, index); + auto saved_str = new char[strlen(str) + 1]; + strcpy(saved_str, str); + value = reinterpret_cast(saved_str); + type = PARAM_TYPE_STRING; + return 0; + } + if(lua_isfunction(L, index)) { + value = interpreter::get_function_handle(L, index); + type = PARAM_TYPE_FUNCTION; + return 0; + } + if (lua_isuserdata(L, index)) { + void* raw_ud = lua_touserdata(L, index); + void* obj = *(void**)raw_ud; + + LuaParamType type_list[] = { + PARAM_TYPE_CARD, + PARAM_TYPE_GROUP, + PARAM_TYPE_EFFECT + }; + + for (auto t : type_list) { + if (scriptlib::check_param(L, t, index, TRUE)) { + value = reinterpret_cast(obj); + type = t; + return 0; + } + } + return luaL_error(L, "Unsupported userdata for metadata entry."); + } + + return luaL_error(L, "Unsupported type for metadata entry."); +} + +void metadata_entry::copy_from(const metadata_entry& other) { + type = other.type; + switch (type) { + case PARAM_TYPE_STRING: { + const char* str = reinterpret_cast(other.value); + char* dup = new char[strlen(str) + 1]; + strcpy(dup, str); + value = reinterpret_cast(dup); + break; + } + case PARAM_TYPE_INDEX: { + const double* pd = reinterpret_cast(other.value); + value = reinterpret_cast(new double(*pd)); + break; + } + default: + // for int, bool, function handle, and userdata pointer types + value = other.value; + break; + } +} + +metadata_entry::metadata_entry(const metadata_entry& other) { + copy_from(other); +} + +metadata_entry& metadata_entry::operator=(const metadata_entry& other) { + if (this != &other) { + reset(); + copy_from(other); + } + return *this; +} + +void metadata_entry::reset() { + if (type == PARAM_TYPE_STRING) { + delete[] reinterpret_cast(value); + } else if (type == PARAM_TYPE_INDEX) { + delete reinterpret_cast(value); + } + value = 0; + type = PARAM_TYPE_INT; +} + +int32_t metadata::luaop_clear() { + entries.clear(); + return 0; +} + +int32_t metadata::luaop_get(lua_State *L, int32_t offset) { + scriptlib::check_param_count(L, 1 + offset); + scriptlib::check_param(L, PARAM_TYPE_STRING, 1 + offset); + auto key = lua_tostring(L, 1 + offset); + auto it = entries.find(key); + if (it == entries.end()) { + lua_pushnil(L); + return 1; + } + return it->second.luaop_get(L); +} + +int32_t metadata::luaop_set(lua_State *L, int32_t offset) { + scriptlib::check_param_count(L, 2 + offset); + scriptlib::check_param(L, PARAM_TYPE_STRING, 1 + offset); + auto key = lua_tostring(L, 1 + offset); + + if(lua_isnil(L, 2 + offset)) { + entries.erase(key); + return 0; + } + + auto existing = entries.find(key); + if (existing != entries.end()) { + return existing->second.luaop_set(L, 2 + offset); + } + + entries[key] = metadata_entry(); + return entries[key].luaop_set(L, 2 + offset); +} + +int32_t metadata::luaop_has(lua_State *L, int32_t offset) { + scriptlib::check_param_count(L, 1 + offset); + scriptlib::check_param(L, PARAM_TYPE_STRING, 1 + offset); + auto key = lua_tostring(L, 1 + offset); + lua_pushboolean(L, entries.find(key) != entries.end()); + return 1; +} + +int32_t metadata::luaop_keys(lua_State *L) { + if(entries.empty()) { + lua_pushnil(L); + return 1; + } + + int32_t count = 0; + for (const auto& pair : entries) { + lua_pushstring(L, pair.first.c_str()); + ++count; + } + return count; +} diff --git a/core/metadata.h b/core/metadata.h new file mode 100644 index 0000000..8c85adc --- /dev/null +++ b/core/metadata.h @@ -0,0 +1,44 @@ +#ifndef METADATA_H_ +#define METADATA_H_ + +#include "common.h" +#include +#include +#include + +class metadata_entry { + public: + metadata_entry(); + ~metadata_entry(); + + metadata_entry(const metadata_entry& other); + metadata_entry& operator=(const metadata_entry& other); + + metadata_entry(metadata_entry&& other) noexcept; + metadata_entry& operator=(metadata_entry&& other) noexcept; + + int32_t luaop_get(lua_State *L); + int32_t luaop_set(lua_State *L, int32_t index); + + private: + void reset(); + void copy_from(const metadata_entry& other); + LuaParamType type; + intptr_t value; +}; + +using metadata_entry_map = std::unordered_map; + +class metadata { + public: + int32_t luaop_clear(); + int32_t luaop_get(lua_State *L, int32_t offset = 0); + int32_t luaop_set(lua_State *L, int32_t offset = 0); + int32_t luaop_has(lua_State *L, int32_t offset = 0); + int32_t luaop_keys(lua_State *L); + + private: + metadata_entry_map entries; +}; + +#endif /* METADATA_H_ */ diff --git a/core/mtrandom.h b/core/mtrandom.h new file mode 100644 index 0000000..dbde5e4 --- /dev/null +++ b/core/mtrandom.h @@ -0,0 +1,108 @@ +/* + * mtrandom.h + * + * Created on: 2009-10-18 + * Author: Argon.Sun + */ + +#ifndef MTRANDOM_H_ +#define MTRANDOM_H_ + +#include +#include +#include +#include + +class mtrandom { +public: + const unsigned int rand_max{ std::mt19937::max() }; + + mtrandom() = default; + explicit mtrandom(uint_fast32_t value) + : rng(value) {} + explicit mtrandom(std::seed_seq& q) + : rng(q) {} + explicit mtrandom(std::seed_seq&& q) + : rng(q) {} + mtrandom(const uint32_t seq[], size_t len) + : mtrandom(std::seed_seq(seq, seq + len)) {} + + mtrandom(const mtrandom& other) = delete; + void operator=(const mtrandom& other) = delete; + + // mersenne_twister_engine + void seed(const uint32_t seq[], size_t len) { + std::seed_seq q(seq, seq + len); + rng.seed(q); + } + void seed(uint_fast32_t value) { + rng.seed(value); + } + void seed(std::seed_seq& q) { + rng.seed(q); + } + uint_fast32_t rand() { + return rng(); + } + void discard(unsigned long long z) { + rng.discard(z); + } + + // old vesion, discard too many numbers + int get_random_integer_v1(int l, int h) { + uint32_t range = (h - l + 1); + uint32_t secureMax = rand_max - rand_max % range; + uint_fast32_t x; + do { + x = rng(); + } while (x >= secureMax); + return l + (int)(x % range); + } + +#pragma warning(disable:4146) + // N % k = (N - k) % k = (-k) % k + // discard (N % range) numbers from the left end so that it is a multiple of range + int get_random_integer_v2(int l, int h) { + uint32_t range = (h - l + 1); + uint32_t bound = -range % range; + auto x = rng(); + while (x < bound) { + x = rng(); + } + return l + (int)(x % range); + } +#pragma warning(default:4146) + + // Fisher-Yates shuffle [first, last) + template + void shuffle_vector(std::vector& v, int first, int last, int version) { + if ((size_t)last > v.size()) + last = (int)v.size(); + auto shuffle = [&](auto&& distribution) { + for (int i = first; i < last - 1; ++i) { + int r = distribution(i, last - 1); + std::swap(v[i], v[r]); + } + }; + if (version == 1) + shuffle([this](int l, int h) { return get_random_integer_v1(l, h); }); + else + shuffle([this](int l, int h) { return get_random_integer_v2(l, h); }); + } + + template + void shuffle_vector(std::vector& v) { + shuffle_vector(v, 0, (int)v.size(), 2); + } + + template + void shuffle_vector(std::vector& v, int first, int last) { + shuffle_vector(v, first, last, 2); + } + +private: + std::mt19937 rng; +}; + + +#endif /* MTRANDOM_H_ */ diff --git a/core/ocgapi.cpp b/core/ocgapi.cpp new file mode 100644 index 0000000..818a2f8 --- /dev/null +++ b/core/ocgapi.cpp @@ -0,0 +1,830 @@ +/* + * interface.cpp + * + * Created on: 2010-5-2 + * Author: Argon + */ +#include +#include +#include +#include +#include +#include "ocgapi.h" +#include "duel.h" +#include "card.h" +#include "group.h" +#include "effect.h" +#include "field.h" +#include "interpreter.h" +#include "buffer.h" + +static uint32_t default_card_reader(uint32_t code, card_data* data) { + return 0; +} +static uint32_t default_message_handler(intptr_t pduel, uint32_t message_type) { + return 0; +} +static script_reader sreader = default_script_reader; +static card_reader creader = default_card_reader; +static message_handler mhandler = default_message_handler; +static byte buffer[0x100000]; +static std::set duel_set; + +OCGCORE_API void set_script_reader(script_reader f) { + sreader = f; +} +OCGCORE_API void set_card_reader(card_reader f) { + creader = f; +} +OCGCORE_API void set_message_handler(message_handler f) { + mhandler = f; +} +byte* read_script(const char* script_name, int* len) { + return sreader(script_name, len); +} +uint32_t read_card(uint32_t code, card_data* data) { + if (code == TEMP_CARD_ID) { + data->clear(); + return 0; + } + return creader(code, data); +} +uint32_t handle_message(void* pduel, uint32_t message_type) { + return mhandler((intptr_t)pduel, message_type); +} +OCGCORE_API byte* default_script_reader(const char* script_name, int* slen) { + FILE *fp; + fp = std::fopen(script_name, "rb"); + if (!fp) + return nullptr; + size_t len = std::fread(buffer, 1, sizeof buffer, fp); + std::fclose(fp); + if (len >= sizeof buffer) + return nullptr; + *slen = (int)len; + return buffer; +} +OCGCORE_API intptr_t create_duel(uint_fast32_t seed) { + return create_duel_ex(seed, 0); +} +OCGCORE_API intptr_t create_duel_ex(uint_fast32_t seed, uint32_t create_flags) { + duel* pduel = new duel(create_flags); + duel_set.insert(pduel); + pduel->random.seed(seed); + pduel->rng_version = 1; + pduel->lua->preloaded = FALSE; + return (intptr_t)pduel; +} +OCGCORE_API intptr_t create_duel_v2(uint32_t seed_sequence[]) { + return create_duel_v2_ex(seed_sequence, 0); +} +OCGCORE_API intptr_t create_duel_v2_ex(uint32_t seed_sequence[], uint32_t create_flags) { + duel* pduel = new duel(create_flags); + duel_set.insert(pduel); + pduel->random.seed(seed_sequence, SEED_COUNT); + pduel->rng_version = 2; + pduel->lua->preloaded = FALSE; + return (intptr_t)pduel; +} +OCGCORE_API void start_duel(intptr_t pduel, uint32_t options) { + duel* pd = (duel*)pduel; + if(!pd->lua->preloaded) { + pd->lua->preloaded = TRUE; + pd->lua->call_code_function(0, (char*) "PreloadUds", 0, 0); + } + uint16_t duel_rule = options >> 16; + uint16_t duel_options = options & 0xffff; + pd->game_field->core.duel_options |= duel_options; + if (duel_rule >= 1 && duel_rule <= CURRENT_RULE) + pd->game_field->core.duel_rule = duel_rule; + else if(options & DUEL_OBSOLETE_RULING) //provide backward compatibility with replay + pd->game_field->core.duel_rule = 1; + if (pd->game_field->core.duel_rule < 1 || pd->game_field->core.duel_rule > CURRENT_RULE) + pd->game_field->core.duel_rule = CURRENT_RULE; + if (pd->game_field->core.duel_rule == MASTER_RULE3) { + pd->game_field->player[0].szone_size = 8; + pd->game_field->player[1].szone_size = 8; + } + pd->game_field->core.shuffle_hand_check[0] = FALSE; + pd->game_field->core.shuffle_hand_check[1] = FALSE; + pd->game_field->core.shuffle_deck_check[0] = FALSE; + pd->game_field->core.shuffle_deck_check[1] = FALSE; + if(pd->game_field->player[0].start_count > 0) + pd->game_field->draw(0, REASON_RULE, PLAYER_NONE, 0, pd->game_field->player[0].start_count); + if(pd->game_field->player[1].start_count > 0) + pd->game_field->draw(0, REASON_RULE, PLAYER_NONE, 1, pd->game_field->player[1].start_count); + if(options & DUEL_TAG_MODE) { + for(int i = 0; i < pd->game_field->player[0].start_count && pd->game_field->player[0].tag_list_main.size(); ++i) { + card* pcard = pd->game_field->player[0].tag_list_main.back(); + pd->game_field->player[0].tag_list_main.pop_back(); + pd->game_field->player[0].tag_list_hand.push_back(pcard); + pcard->current.controler = 0; + pcard->current.location = LOCATION_HAND; + pcard->current.sequence = (uint8_t)pd->game_field->player[0].tag_list_hand.size() - 1; + pcard->current.position = POS_FACEDOWN; + } + for(int i = 0; i < pd->game_field->player[1].start_count && pd->game_field->player[1].tag_list_main.size(); ++i) { + card* pcard = pd->game_field->player[1].tag_list_main.back(); + pd->game_field->player[1].tag_list_main.pop_back(); + pd->game_field->player[1].tag_list_hand.push_back(pcard); + pcard->current.controler = 1; + pcard->current.location = LOCATION_HAND; + pcard->current.sequence = (uint8_t)pd->game_field->player[1].tag_list_hand.size() - 1; + pcard->current.position = POS_FACEDOWN; + } + } + pd->game_field->add_process(PROCESSOR_TURN, 0, 0, 0, 0, 0); +} +OCGCORE_API void end_duel(intptr_t pduel) { + duel* pd = (duel*)pduel; + if(duel_set.count(pd)) { + duel_set.erase(pd); + delete pd; + } +} +OCGCORE_API void set_player_info(intptr_t pduel, int32_t playerid, int32_t lp, int32_t startcount, int32_t drawcount) { + if (!check_playerid(playerid)) + return; + duel* pd = (duel*)pduel; + if(lp > 0) + pd->game_field->player[playerid].lp = lp; + if(startcount >= 0) + pd->game_field->player[playerid].start_count = startcount; + if(drawcount >= 0) + pd->game_field->player[playerid].draw_count = drawcount; +} +OCGCORE_API void get_log_message(intptr_t pduel, char* buf) { + duel* pd = (duel*)pduel; + std::strncpy(buf, pd->strbuffer, sizeof pd->strbuffer - 1); + buf[sizeof pd->strbuffer - 1] = 0; +} +OCGCORE_API int32_t get_message(intptr_t pduel, byte* buf) { + int32_t len = ((duel*)pduel)->read_buffer(buf); + ((duel*)pduel)->clear_buffer(); + return len; +} +OCGCORE_API uint32_t process(intptr_t pduel) { + duel* pd = (duel*)pduel; + uint32_t result = 0; + do { + result = pd->game_field->process(); + } while ((result & PROCESSOR_BUFFER_LEN) == 0 && (result & PROCESSOR_FLAG) == 0); + return result; +} +OCGCORE_API void new_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position) { + if (!check_playerid(owner) || !check_playerid(playerid)) + return; + duel* ptduel = (duel*)pduel; + if(!ptduel->lua->preloaded) { + ptduel->lua->preloaded = TRUE; + ptduel->lua->call_code_function(0, (char*) "PreloadUds", 0, 0); + } + if(ptduel->game_field->is_location_useable(playerid, location, sequence)) { + card* pcard = ptduel->new_card(code); + pcard->owner = owner; + ptduel->game_field->add_card(playerid, pcard, location, sequence); + pcard->current.position = position; + if(!(location & LOCATION_ONFIELD) || (position & POS_FACEUP)) { + pcard->enable_field_effect(true); + ptduel->game_field->adjust_instant(); + } if(location & LOCATION_ONFIELD) { + if(location == LOCATION_MZONE) + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + } + } +} +OCGCORE_API void new_tag_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t location) { + duel* ptduel = (duel*)pduel; + if(owner > 1 || !(location & (LOCATION_DECK | LOCATION_EXTRA))) + return; + card* pcard = ptduel->new_card(code); + switch(location) { + case LOCATION_DECK: + ptduel->game_field->player[owner].tag_list_main.push_back(pcard); + pcard->owner = owner; + pcard->current.controler = owner; + pcard->current.location = LOCATION_DECK; + pcard->current.sequence = (uint8_t)ptduel->game_field->player[owner].tag_list_main.size() - 1; + pcard->current.position = POS_FACEDOWN_DEFENSE; + break; + case LOCATION_EXTRA: + ptduel->game_field->player[owner].tag_list_extra.push_back(pcard); + pcard->owner = owner; + pcard->current.controler = owner; + pcard->current.location = LOCATION_EXTRA; + pcard->current.sequence = (uint8_t)ptduel->game_field->player[owner].tag_list_extra.size() - 1; + pcard->current.position = POS_FACEDOWN_DEFENSE; + break; + } +} +/** +* @brief Get card information. +* @param buf int32_t array +* @return buffer length in bytes +*/ +OCGCORE_API int32_t query_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint32_t query_flag, byte* buf, int32_t use_cache) { + if (!check_playerid(playerid)) + return LEN_FAIL; + duel* ptduel = (duel*)pduel; + card* pcard = nullptr; + location &= 0x7f; + if (location == LOCATION_MZONE || location == LOCATION_SZONE) + pcard = ptduel->game_field->get_field_card(playerid, location, sequence); + else { + auto ptr = ptduel->game_field->get_field_vector(playerid, location); + if(!ptr) + return LEN_FAIL; + auto& lst = *ptr; + if (sequence >= lst.size()) + return LEN_FAIL; + pcard = lst[sequence]; + } + if (pcard) { + return pcard->get_infos(buf, query_flag, use_cache); + } + else { + buffer_write(buf, LEN_EMPTY); + return LEN_EMPTY; + } +} +OCGCORE_API int32_t query_field_count(intptr_t pduel, uint8_t playerid, uint8_t location) { + duel* ptduel = (duel*)pduel; + if (!check_playerid(playerid)) + return 0; + auto& player = ptduel->game_field->player[playerid]; + if(location == LOCATION_HAND) + return (int32_t)player.list_hand.size(); + if(location == LOCATION_GRAVE) + return (int32_t)player.list_grave.size(); + if(location == LOCATION_REMOVED) + return (int32_t)player.list_remove.size(); + if(location == LOCATION_EXTRA) + return (int32_t)player.list_extra.size(); + if(location == LOCATION_DECK) + return (int32_t)player.list_main.size(); + if(location == LOCATION_MZONE) { + int32_t count = 0; + for(auto& pcard : player.list_mzone) + if(pcard) + ++count; + return count; + } + if(location == LOCATION_SZONE) { + int32_t count = 0; + for(auto& pcard : player.list_szone) + if(pcard) + ++count; + return count; + } + return 0; +} +OCGCORE_API int32_t query_field_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint32_t query_flag, byte* buf, int32_t use_cache) { + if (!check_playerid(playerid)) + return LEN_FAIL; + duel* ptduel = (duel*)pduel; + auto& player = ptduel->game_field->player[playerid]; + byte* p = buf; + if(location == LOCATION_MZONE) { + for(auto& pcard : player.list_mzone) { + if(pcard) { + int32_t clen = pcard->get_infos(p, query_flag, use_cache); + p += clen; + } else { + buffer_write(p, LEN_EMPTY); + } + } + } + else if(location == LOCATION_SZONE) { + for(auto& pcard : player.list_szone) { + if(pcard) { + int32_t clen = pcard->get_infos(p, query_flag, use_cache); + p += clen; + } else { + buffer_write(p, LEN_EMPTY); + } + } + } + else { + card_vector* lst = nullptr; + if(location == LOCATION_HAND) + lst = &player.list_hand; + else if(location == LOCATION_GRAVE) + lst = &player.list_grave; + else if(location == LOCATION_REMOVED) + lst = &player.list_remove; + else if(location == LOCATION_EXTRA) + lst = &player.list_extra; + else if(location == LOCATION_DECK) + lst = &player.list_main; + else + return LEN_FAIL; + for(auto& pcard : *lst) { + int32_t clen = pcard->get_infos(p, query_flag, use_cache); + p += clen; + } + } + return (int32_t)(p - buf); +} +OCGCORE_API int32_t query_field_info(intptr_t pduel, byte* buf) { + duel* ptduel = (duel*)pduel; + byte* p = buf; + *p++ = MSG_RELOAD_FIELD; + *p++ = (uint8_t)ptduel->game_field->core.duel_rule; + for(int playerid = 0; playerid < 2; ++playerid) { + auto& player = ptduel->game_field->player[playerid]; + buffer_write(p, player.lp); + for(auto& pcard : player.list_mzone) { + if(pcard) { + *p++ = 1; + *p++ = pcard->current.position; + *p++ = (uint8_t)pcard->xyz_materials.size(); + } else { + *p++ = 0; + } + } + for(auto& pcard : player.list_szone) { + if(pcard) { + *p++ = 1; + *p++ = pcard->current.position; + } else { + *p++ = 0; + } + } + *p++ = (uint8_t)player.list_main.size(); + *p++ = (uint8_t)player.list_hand.size(); + *p++ = (uint8_t)player.list_grave.size(); + *p++ = (uint8_t)player.list_remove.size(); + *p++ = (uint8_t)player.list_extra.size(); + *p++ = (uint8_t)player.extra_p_count; + } + *p++ = (uint8_t)ptduel->game_field->core.current_chain.size(); + for(const auto& ch : ptduel->game_field->core.current_chain) { + effect* peffect = ch.triggering_effect; + buffer_write(p, peffect->get_handler()->data.code); + buffer_write(p, peffect->get_handler()->get_info_location()); + *p++ = ch.triggering_controler; + *p++ = (uint8_t)ch.triggering_location; + *p++ = ch.triggering_sequence; + buffer_write(p, peffect->description); + } + return (int32_t)(p - buf); +} +OCGCORE_API void set_responsei(intptr_t pduel, int32_t value) { + ((duel*)pduel)->set_responsei(value); +} +OCGCORE_API void set_responseb(intptr_t pduel, byte* buf) { + ((duel*)pduel)->set_responseb(buf); +} +OCGCORE_API int32_t preload_script(intptr_t pduel, const char* script_name) { + return ((duel*)pduel)->lua->load_script(script_name); +} +// [YGOL] patch 0022 (attack preview) -- see ocgapi.h. Looks the two cards up, asks field::get_attack_target +// whether the pair is legal (patch 0024, flag bit 5), answers unknown for a face-down target, else asks +// field::preview_battle (processor.cpp) and writes the 24-byte answer. +OCGCORE_API int32_t ygol_preview_attack(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t target_location, uint8_t target_sequence, uint8_t* out) { + duel* pd = (duel*)pduel; + if(!pd || !out || !duel_set.count(pd) || !pd->game_field) + return 0; + if(!check_playerid(playerid) || location != LOCATION_MZONE) + return 0; + field* f = pd->game_field; + card* attacker = f->get_field_card(playerid, LOCATION_MZONE, sequence); + if(!attacker || attacker->current.controler != playerid || !attacker->is_position(POS_FACEUP)) + return 0; + card* target = nullptr; + if(target_location != 0xFF) { + if(target_location != LOCATION_MZONE) + return 0; + target = f->get_field_card(1 - playerid, LOCATION_MZONE, target_sequence); + if(!target || target->current.controler != 1 - playerid) + return 0; + } + uint8_t flags = 0; + battle_preview preview; + // [YGOL] patch 0024: the legality read of process_battle_command step 4 (field.cpp get_attack_target), + // framed the same way (core.attacker set, attack_player cleared) and put back; v is a local list. + card* saved_attacker = f->core.attacker; + uint8_t saved_attack_player = f->core.attack_player; + uint8_t saved_direct = attacker->direct_attackable; + card_vector legal; + f->core.attacker = attacker; + f->core.attack_player = FALSE; + f->get_attack_target(attacker, &legal, f->core.chain_attack, true); + bool illegal = target ? std::find(legal.begin(), legal.end(), target) == legal.end() + : !attacker->direct_attackable; + attacker->direct_attackable = saved_direct; + f->core.attack_player = saved_attack_player; + f->core.attacker = saved_attacker; + if(illegal) + flags |= 0x20; + else if(target && target->is_position(POS_FACEDOWN)) + flags |= 0x2; + else { + if(!target) + flags |= 0x1; + f->preview_battle(attacker, target, &preview); + if(preview.attacker_destroyed) + flags |= 0x4; + if(preview.target_destroyed) + flags |= 0x8; + if(preview.pierce) + flags |= 0x10; + } + byte* ptr = out; + buffer_write(ptr, 1); + buffer_write(ptr, flags); + buffer_write(ptr, 0); + buffer_write(ptr, preview.attacker_value); + buffer_write(ptr, preview.target_value); + buffer_write(ptr, preview.damage[0]); + buffer_write(ptr, preview.damage[1]); + buffer_write(ptr, 0); + return 1; +} +OCGCORE_API int32_t get_registry_value(intptr_t pduel, const char* key, byte* out_buf) { + if (!pduel || !key || !out_buf) return -1; + + duel* d = (duel*)pduel; + auto it = d->registry.find(key); + if (it == d->registry.end()) + return -1; + + const std::string& val = it->second; + std::memcpy(out_buf, val.c_str(), val.size()); + return static_cast(val.size()); +} +OCGCORE_API void set_registry_value(intptr_t pduel, const char* key, const char* value) { + if (!pduel || !key) return; + duel* d = (duel*)pduel; + if (value) { + d->registry[key] = value; + } else { + d->registry.erase(key); + } +} +OCGCORE_API int32_t get_registry_keys(intptr_t pduel, byte* out_buf) { + if (!pduel || !out_buf) return -1; + + duel* d = (duel*)pduel; + byte* ptr = out_buf; + + for (const auto& pair : d->registry) { + const std::string& key = pair.first; + uint16_t len = static_cast(std::min(key.size(), 0xFFFF)); + + // 写入长度(2字节,小端序) + buffer_write(ptr, len); + + std::memcpy(ptr, key.data(), len); + ptr += len; + } + + return ptr - out_buf; +} +OCGCORE_API void clear_registry(intptr_t pduel) { + if (!pduel) return; + duel* d = (duel*)pduel; + d->registry.clear(); +} + +OCGCORE_API int32_t dump_registry(intptr_t pduel, byte* out_buf) { + if (!pduel || !out_buf) return -1; + + duel* d = (duel*)pduel; + byte* ptr = out_buf; + + for (const auto& pair : d->registry) { + const std::string& key = pair.first; + const std::string& value = pair.second; + + uint16_t key_len = static_cast(std::min(key.size(), 0xFFFF)); + uint16_t val_len = static_cast(std::min(value.size(), 0xFFFF)); + + // 写入 key_len 和 val_len(每次调用后 ptr 自动推进) + buffer_write(ptr, key_len); + buffer_write(ptr, val_len); + + // 写入 key 内容 + std::memcpy(ptr, key.data(), key_len); + ptr += key_len; + + // 写入 value 内容 + std::memcpy(ptr, value.data(), val_len); + ptr += val_len; + } + + return ptr - out_buf; // 返回写入的总字节数 +} +OCGCORE_API void load_registry(intptr_t pduel, const byte* in_buf, int32_t in_len) { + if (!pduel || !in_buf || in_len <= 0) return; + + duel* d = (duel*)pduel; + + byte* ptr = const_cast(in_buf); // buffer_read 要求非 const + byte* end = ptr + in_len; + + while (ptr + 4 <= end) { // 至少要读取 key_len 和 val_len(2 + 2 字节) + uint16_t key_len = buffer_read(ptr); + uint16_t val_len = buffer_read(ptr); + + if (ptr + key_len + val_len > end) { + break; // 数据不完整,退出 + } + + std::string key(reinterpret_cast(ptr), key_len); + ptr += key_len; + + std::string value(reinterpret_cast(ptr), val_len); + ptr += val_len; + + d->registry[std::move(key)] = std::move(value); + } +} +OCGCORE_API int32_t get_lua_coverage_dump_size(intptr_t pduel, const char* name) { + if (!pduel || !name) + return 0; + return ((duel*)pduel)->lua->lua_coverage.get_dump_size(name); +} +OCGCORE_API int32_t dump_lua_coverage(intptr_t pduel, const char* name, byte* out_buf, int32_t out_len) { + if (!pduel || !name || !out_buf || out_len <= 0) + return 0; + return ((duel*)pduel)->lua->lua_coverage.dump(name, out_buf, out_len); +} +OCGCORE_API int32_t get_all_lua_coverages_dump_size(intptr_t pduel) { + if (!pduel) + return 0; + return ((duel*)pduel)->lua->lua_coverage.get_all_dump_size(); +} +OCGCORE_API int32_t dump_all_lua_coverages(intptr_t pduel, byte* out_buf, int32_t out_len) { + if (!pduel || !out_buf || out_len <= 0) + return 0; + return ((duel*)pduel)->lua->lua_coverage.dump_all(out_buf, out_len); +} +OCGCORE_API void clear_lua_coverage(intptr_t pduel, const char* name) { + if (!pduel || !name) + return; + ((duel*)pduel)->lua->lua_coverage.clear(name); +} +OCGCORE_API void clear_all_lua_coverages(intptr_t pduel) { + if (!pduel) + return; + ((duel*)pduel)->lua->lua_coverage.clear_all(); +} + +// [YGOL] patch 0008 (sandbox god mode) -- see ocgapi.h. Helpers first, the six exports after. +static duel* ygol_sb_duel(intptr_t pduel) { + duel* pd = (duel*)pduel; + if(!pd || !duel_set.count(pd) || !pd->game_field) + return nullptr; + return pd; +} +static int32_t ygol_sb_location_ok(uint8_t location) { + switch(location) { + case LOCATION_DECK: + case LOCATION_HAND: + case LOCATION_MZONE: + case LOCATION_SZONE: + case LOCATION_GRAVE: + case LOCATION_REMOVED: + case LOCATION_EXTRA: + return TRUE; + } + return FALSE; +} +static card* ygol_sb_find(field* f, uint8_t playerid, uint8_t location, uint8_t sequence) { + if(!check_playerid(playerid) || !ygol_sb_location_ok(location)) + return nullptr; + return f->get_field_card(playerid, location, sequence); +} +static void ygol_sb_write_move(duel* pd, card* pcard, uint32_t previous, uint32_t current) { + pd->write_buffer8(MSG_MOVE); + pd->write_buffer32(pcard->data.code); + pd->write_buffer32(previous); + pd->write_buffer32(current); + pd->write_buffer32(0); +} +// The card must not be something the running duel still points at: a chain in flight, the +// attacker or its target, a summon in progress, or an overlay unit (address its Xyz instead). +static int32_t ygol_sb_can_touch(field* f, card* pcard) { + auto& core = f->core; + if(!core.current_chain.empty() || core.chain_solving || core.conti_solving) + return FALSE; + if(pcard == core.attacker || pcard == core.attack_target || pcard == core.summoning_card) + return FALSE; + if(pcard->overlay_target) + return FALSE; + return TRUE; +} +static void ygol_sb_erase(card_vector& v, card* pcard) { + v.erase(std::remove(v.begin(), v.end(), pcard), v.end()); +} +// Drop the card from every core.* container that can outlive a prompt. +static void ygol_sb_forget(field* f, card* pcard) { + auto& core = f->core; + card_set* sets[] = { + &core.leave_confirmed, &core.special_summoning, &core.unable_tofield_set, &core.equiping_cards, + &core.control_adjust_set[0], &core.control_adjust_set[1], &core.unique_destroy_set, + &core.self_destroy_set, &core.self_tograve_set, &core.trap_monster_adjust_set[0], + &core.trap_monster_adjust_set[1], &core.release_cards, &core.release_cards_ex, + &core.release_cards_ex_oneof, &core.battle_destroy_rep, &core.fusion_materials, + &core.synchro_materials, &core.operated_set, &core.discarded_set, &core.destroy_canceled, + &core.indestructable_count_set, &core.delayed_enable_set, &core.set_group_pre_set, + &core.set_group_set, + }; + for(auto* s : sets) + s->erase(pcard); + core.readjust_map.erase(pcard); + for(auto it = core.xmaterial_lst.begin(); it != core.xmaterial_lst.end();) { + if(it->second == pcard) + it = core.xmaterial_lst.erase(it); + else + ++it; + } + ygol_sb_erase(core.select_cards, pcard); + ygol_sb_erase(core.unselect_cards, pcard); + ygol_sb_erase(core.must_select_cards, pcard); +} +// Cut every link a card on the field holds: overlays go to their owner's GY (each with its own +// MSG_MOVE), equips both ways are released, targets cleared, field-gained effects reset. +static void ygol_sb_detach(duel* pd, field* f, card* pcard) { + if(pcard->xyz_materials.size()) { + card_vector overlays(pcard->xyz_materials.begin(), pcard->xyz_materials.end()); + for(auto& mat : overlays) { + uint32_t previous = mat->get_info_location(); + pcard->xyz_remove(mat); + mat->sendto_param.position = POS_FACEUP; + f->move_card(mat->owner, mat, LOCATION_GRAVE, 0); + mat->current.position = POS_FACEUP; + mat->reset(RESET_TOGRAVE, RESET_EVENT); + mat->enable_field_effect(true); + mat->refresh_disable_status(); + ygol_sb_write_move(pd, mat, previous, mat->get_info_location()); + } + pcard->xyz_materials_previous_count_onfield = 0; + } + for(auto it = pcard->equiping_cards.begin(); it != pcard->equiping_cards.end();) { + card* equip = *it++; + equip->unequip(); + } + pcard->unequip(); + pcard->clear_card_target(); + if(pcard->current.location & LOCATION_ONFIELD) + pcard->reset(RESET_LEAVE + RESET_MSCHANGE, RESET_EVENT); +} +OCGCORE_API int32_t ygol_sb_add(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position) { + duel* pd = ygol_sb_duel(pduel); + if(!pd || !check_playerid(owner) || !check_playerid(playerid) || !ygol_sb_location_ok(location)) + return FALSE; + field* f = pd->game_field; + if(!f->is_location_useable(playerid, location, sequence)) + return FALSE; + if((location & LOCATION_ONFIELD) && f->get_field_card(playerid, location, sequence)) + return FALSE; + if(!pd->lua->preloaded) { + pd->lua->preloaded = TRUE; + pd->lua->call_code_function(0, (char*) "PreloadUds", 0, 0); + } + card* pcard = pd->new_card(code); + pcard->owner = owner; + f->add_card(playerid, pcard, location, sequence); + if(pcard->current.location == 0) + return FALSE; + pcard->current.position = pcard->current.location == location ? position : pcard->sendto_param.position; + if(!(pcard->current.location & LOCATION_ONFIELD) || (pcard->current.position & POS_FACEUP)) { + pcard->enable_field_effect(true); + f->adjust_instant(); + } + if(pcard->current.location == LOCATION_MZONE) + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + ygol_sb_write_move(pd, pcard, 0, pcard->get_info_location()); + return TRUE; +} +OCGCORE_API int32_t ygol_sb_move(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t dst_playerid, uint8_t dst_location, uint8_t dst_sequence, uint8_t position) { + duel* pd = ygol_sb_duel(pduel); + if(!pd || !check_playerid(dst_playerid) || !ygol_sb_location_ok(dst_location)) + return FALSE; + field* f = pd->game_field; + card* pcard = ygol_sb_find(f, playerid, location, sequence); + if(!pcard || !ygol_sb_can_touch(f, pcard)) + return FALSE; + if(pcard->current.controler == dst_playerid && pcard->current.location == dst_location + && (dst_location & LOCATION_ONFIELD) && pcard->current.sequence == dst_sequence) + return FALSE; + if(!f->is_location_useable(dst_playerid, dst_location, dst_sequence)) + return FALSE; + if((dst_location & LOCATION_ONFIELD) && f->get_field_card(dst_playerid, dst_location, dst_sequence)) + return FALSE; + uint32_t previous = pcard->get_info_location(); + ygol_sb_detach(pd, f, pcard); + ygol_sb_forget(f, pcard); + if(dst_location & LOCATION_ONFIELD) + pcard->sendto_param.position = position ? position : POS_FACEUP_ATTACK; + else if(dst_location == LOCATION_EXTRA) + pcard->sendto_param.position = (position & POS_FACEUP) ? POS_FACEUP_DEFENSE : POS_FACEDOWN_DEFENSE; + else + pcard->sendto_param.position = position ? position : POS_FACEUP; + f->move_card(dst_playerid, pcard, dst_location, dst_sequence); + if(pcard->current.location == 0) + return FALSE; + pcard->current.position = pcard->sendto_param.position; + uint8_t nloc = pcard->current.location; + if(nloc == LOCATION_HAND) + pcard->reset(RESET_TOHAND, RESET_EVENT); + if(nloc == LOCATION_DECK || nloc == LOCATION_EXTRA) + pcard->reset(RESET_TODECK, RESET_EVENT); + if(nloc == LOCATION_GRAVE) + pcard->reset(RESET_TOGRAVE, RESET_EVENT); + if(nloc == LOCATION_REMOVED) + pcard->reset(RESET_REMOVE, RESET_EVENT); + if(!(nloc & LOCATION_ONFIELD) || (pcard->current.position & POS_FACEUP)) + pcard->enable_field_effect(true); + else + pcard->enable_field_effect(false); + pcard->refresh_disable_status(); + if(nloc == LOCATION_MZONE) + pcard->set_status(STATUS_PROC_COMPLETE, TRUE); + ygol_sb_write_move(pd, pcard, previous, pcard->get_info_location()); + f->adjust_instant(); + return TRUE; +} +OCGCORE_API int32_t ygol_sb_remove(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence) { + duel* pd = ygol_sb_duel(pduel); + if(!pd) + return FALSE; + field* f = pd->game_field; + card* pcard = ygol_sb_find(f, playerid, location, sequence); + if(!pcard || !ygol_sb_can_touch(f, pcard)) + return FALSE; + uint32_t previous = pcard->get_info_location(); + ygol_sb_detach(pd, f, pcard); + ygol_sb_forget(f, pcard); + f->remove_card(pcard); + if(pcard->current.location != 0) + return FALSE; + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + ygol_sb_write_move(pd, pcard, previous, 0); + f->adjust_instant(); + return TRUE; +} +OCGCORE_API int32_t ygol_sb_set_position(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position) { + duel* pd = ygol_sb_duel(pduel); + if(!pd || !(location & LOCATION_ONFIELD) || !position || (position & ~0xf)) + return FALSE; + field* f = pd->game_field; + card* pcard = ygol_sb_find(f, playerid, location, sequence); + if(!pcard || !ygol_sb_can_touch(f, pcard)) + return FALSE; + uint8_t opos = pcard->current.position; + if(opos == position) + return FALSE; + pcard->previous.position = opos; + pcard->current.position = position; + if((opos & POS_FACEDOWN) && (position & POS_FACEUP)) { + pcard->fieldid = f->infos.field_id++; + pcard->activate_count_id = pcard->fieldid; + if(f->check_unique_onfield(pcard, pcard->current.controler, pcard->current.location)) + pcard->unique_fieldid = UINT_MAX; + } + pcard->enable_field_effect(!!(position & POS_FACEUP)); + pcard->refresh_disable_status(); + pd->write_buffer8(MSG_POS_CHANGE); + pd->write_buffer32(pcard->data.code); + pd->write_buffer8(pcard->current.controler); + pd->write_buffer8(pcard->current.location); + pd->write_buffer8(pcard->current.sequence); + pd->write_buffer8(pcard->previous.position); + pd->write_buffer8(pcard->current.position); + f->adjust_instant(); + return TRUE; +} +OCGCORE_API int32_t ygol_sb_set_lp(intptr_t pduel, uint8_t playerid, int32_t lp) { + duel* pd = ygol_sb_duel(pduel); + if(!pd || !check_playerid(playerid)) + return FALSE; + if(lp < 0) + lp = 0; + pd->game_field->player[playerid].lp = lp; + pd->write_buffer8(MSG_LPUPDATE); + pd->write_buffer8(playerid); + pd->write_buffer32(lp); + return TRUE; +} +// The prompt only writes the lists its parent command built at step 0, so the parked prompt is +// dropped and the parent rewound: process() rebuilds the lists from the field and re-sends it. +OCGCORE_API int32_t ygol_sb_reask(intptr_t pduel) { + duel* pd = ygol_sb_duel(pduel); + if(!pd) + return FALSE; + auto& core = pd->game_field->core; + if(core.units.size() < 2 || !core.subunits.empty()) + return FALSE; + auto front = core.units.begin(); + auto parent = std::next(front); + bool idle = front->type == PROCESSOR_SELECT_IDLECMD && parent->type == PROCESSOR_IDLE_COMMAND; + bool battle = front->type == PROCESSOR_SELECT_BATTLECMD && parent->type == PROCESSOR_BATTLE_COMMAND; + if(!(idle || battle) || front->step != 1 || parent->step != 1) + return FALSE; + core.units.pop_front(); + parent->step = 0; + return TRUE; +} diff --git a/core/ocgapi.h b/core/ocgapi.h new file mode 100644 index 0000000..aaee489 --- /dev/null +++ b/core/ocgapi.h @@ -0,0 +1,108 @@ +/* + * interface.h + * + * Created on: 2010-4-28 + * Author: Argon + */ + +#ifndef OCGAPI_H_ +#define OCGAPI_H_ + +#include "common.h" + +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C +#endif + +#ifndef OCGCORE_API +#if defined(__EMSCRIPTEN__) +#include +#define OCGCORE_API EXTERN_C EMSCRIPTEN_KEEPALIVE +#elif defined(_WIN32) +#define OCGCORE_API EXTERN_C __declspec(dllexport) +#else +#define OCGCORE_API EXTERN_C __attribute__ ((visibility ("default"))) +#endif +#endif + +#define SEED_COUNT 8 + +#define LEN_FAIL 0 +#define LEN_EMPTY 4 +#define LEN_HEADER 8 +#define TEMP_CARD_ID 0 + +struct card_data; + +typedef byte* (*script_reader)(const char* script_name, int* len); +typedef uint32_t (*card_reader)(uint32_t code, card_data* data); +typedef uint32_t (*message_handler)(intptr_t pduel, uint32_t msg_type); + +OCGCORE_API void set_script_reader(script_reader f); +OCGCORE_API void set_card_reader(card_reader f); +OCGCORE_API void set_message_handler(message_handler f); + +byte* read_script(const char* script_name, int* len); +uint32_t read_card(uint32_t code, card_data* data); +uint32_t handle_message(void* pduel, uint32_t message_type); + +OCGCORE_API intptr_t create_duel(uint_fast32_t seed); +OCGCORE_API intptr_t create_duel_v2(uint32_t seed_sequence[]); +OCGCORE_API intptr_t create_duel_ex(uint_fast32_t seed, uint32_t create_flags); +OCGCORE_API intptr_t create_duel_v2_ex(uint32_t seed_sequence[], uint32_t create_flags); +OCGCORE_API void start_duel(intptr_t pduel, uint32_t options); +OCGCORE_API void end_duel(intptr_t pduel); +OCGCORE_API void set_player_info(intptr_t pduel, int32_t playerid, int32_t lp, int32_t startcount, int32_t drawcount); +OCGCORE_API void get_log_message(intptr_t pduel, char* buf); +OCGCORE_API int32_t get_message(intptr_t pduel, byte* buf); +OCGCORE_API uint32_t process(intptr_t pduel); +OCGCORE_API void new_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position); +OCGCORE_API void new_tag_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t location); +OCGCORE_API int32_t query_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint32_t query_flag, byte* buf, int32_t use_cache); +OCGCORE_API int32_t query_field_count(intptr_t pduel, uint8_t playerid, uint8_t location); +OCGCORE_API int32_t query_field_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint32_t query_flag, byte* buf, int32_t use_cache); +OCGCORE_API int32_t query_field_info(intptr_t pduel, byte* buf); +OCGCORE_API void set_responsei(intptr_t pduel, int32_t value); +OCGCORE_API void set_responseb(intptr_t pduel, byte* buf); +OCGCORE_API int32_t preload_script(intptr_t pduel, const char* script_name); +OCGCORE_API byte* default_script_reader(const char* script_name, int* len); + +// [YGOL] patch 0022 (attack preview): a read-only dry run of the damage step for one attacker and one +// target (target_location 0xFF = direct). Returns 1 answered, 0 refused (bad pointer, no such card, +// attacker not a face-up monster on playerid's field, target not a monster on the other field); a +// refusal writes nothing. out is 24 bytes: version (1), flags (bit 0 direct, bit 1 unknown, bit 2 +// attacker destroyed, bit 3 target destroyed, bit 4 pierce, bit 5 illegal), 2 pad bytes, then five +// int32: attacker value, target value, damage to player 0, damage to player 1, reserved 0. A face-down +// target answers unknown with every number 0. A pair field::get_attack_target would not offer answers +// illegal alone with every number 0 (patch 0024). Call it only while the engine is parked on a prompt; +// the duel is byte-identical afterwards (proposal 43). +OCGCORE_API int32_t ygol_preview_attack(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t target_location, uint8_t target_sequence, uint8_t* out); + +// koishi specific +OCGCORE_API int32_t get_registry_value(intptr_t pduel, const char* key, byte* out_buf); +OCGCORE_API void set_registry_value(intptr_t pduel, const char* key, const char* value); +OCGCORE_API int32_t get_registry_keys(intptr_t pduel, byte* out_buf); +OCGCORE_API void clear_registry(intptr_t pduel); +OCGCORE_API int32_t dump_registry(intptr_t pduel, byte* out_buf); +OCGCORE_API void load_registry(intptr_t pduel, const byte* in_buf, int32_t in_len); +OCGCORE_API int32_t get_lua_coverage_dump_size(intptr_t pduel, const char* name); +OCGCORE_API int32_t dump_lua_coverage(intptr_t pduel, const char* name, byte* out_buf, int32_t out_len); +OCGCORE_API int32_t get_all_lua_coverages_dump_size(intptr_t pduel); +OCGCORE_API int32_t dump_all_lua_coverages(intptr_t pduel, byte* out_buf, int32_t out_len); +OCGCORE_API void clear_lua_coverage(intptr_t pduel, const char* name); +OCGCORE_API void clear_all_lua_coverages(intptr_t pduel); + + +// [YGOL] patch 0008 (sandbox god mode): additive, Sandbox-only exports. Nothing a normal +// duel, replay, BotArena or YGOSharp calls. Each returns 1 when it did the work, 0 when it +// refused and changed nothing. +OCGCORE_API int32_t ygol_sb_add(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position); +OCGCORE_API int32_t ygol_sb_move(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t dst_playerid, uint8_t dst_location, uint8_t dst_sequence, uint8_t position); +OCGCORE_API int32_t ygol_sb_remove(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence); +OCGCORE_API int32_t ygol_sb_set_position(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position); +OCGCORE_API int32_t ygol_sb_set_lp(intptr_t pduel, uint8_t playerid, int32_t lp); +OCGCORE_API int32_t ygol_sb_reask(intptr_t pduel); + +#endif /* OCGAPI_H_ */ diff --git a/core/operations.cpp b/core/operations.cpp new file mode 100644 index 0000000..35a9764 --- /dev/null +++ b/core/operations.cpp @@ -0,0 +1,6574 @@ +/* + * operations.cpp + * + * Created on: 2010-9-18 + * Author: Argon + */ +#include "field.h" +#include "duel.h" +#include "card.h" +#include "effect.h" +#include "group.h" +#include "interpreter.h" +#include + +int32_t field::negate_chain(uint8_t chaincount) { + if(core.current_chain.size() == 0) + return FALSE; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + chain& pchain = core.current_chain[chaincount - 1]; + card* phandler = pchain.triggering_effect->get_handler(); + if(!(pchain.flag & CHAIN_DISABLE_ACTIVATE) && is_chain_negatable(pchain.chain_count) + && (!phandler->is_has_relation(pchain) || phandler->is_affect_by_effect(core.reason_effect))) { + pchain.flag |= CHAIN_DISABLE_ACTIVATE; + pchain.disable_reason = core.reason_effect; + pchain.disable_player = core.reason_player; + if((pchain.triggering_effect->type & EFFECT_TYPE_ACTIVATE) && phandler->is_has_relation(pchain) && (phandler->current.location == LOCATION_SZONE)) { + phandler->set_status(STATUS_LEAVE_CONFIRMED, TRUE); + phandler->set_status(STATUS_ACTIVATE_DISABLED, TRUE); + } + pduel->write_buffer8(MSG_CHAIN_NEGATED); + pduel->write_buffer8(chaincount); + if(pchain.triggering_location == LOCATION_DECK + || core.duel_rule >= MASTER_RULE_2020 && pchain.triggering_location == LOCATION_EXTRA && (pchain.triggering_position & POS_FACEDOWN)) + phandler->release_relation(pchain); + return TRUE; + } + return FALSE; +} +int32_t field::disable_chain(uint8_t chaincount, uint8_t forced) { + if(core.current_chain.size() == 0) + return FALSE; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + chain& pchain = core.current_chain[chaincount - 1]; + card* phandler = pchain.triggering_effect->get_handler(); + if(!(pchain.flag & CHAIN_DISABLE_EFFECT) && is_chain_disablable(pchain.chain_count) + && (!phandler->is_has_relation(pchain) || phandler->is_affect_by_effect(core.reason_effect))) { + core.current_chain[chaincount - 1].flag |= CHAIN_DISABLE_EFFECT; + core.current_chain[chaincount - 1].disable_reason = core.reason_effect; + core.current_chain[chaincount - 1].disable_player = core.reason_player; + pduel->write_buffer8(MSG_CHAIN_DISABLED); + pduel->write_buffer8(chaincount); + if(pchain.triggering_location == LOCATION_DECK + || core.duel_rule >= MASTER_RULE_2020 && pchain.triggering_location == LOCATION_EXTRA && (pchain.triggering_position & POS_FACEDOWN)) + phandler->release_relation(pchain); + return TRUE; + } + return FALSE; +} +void field::change_chain_effect(uint8_t chaincount, int32_t rep_op) { + if(core.current_chain.size() == 0) + return; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + chain& pchain = core.current_chain[chaincount - 1]; + pchain.replace_op = rep_op; + if((pchain.triggering_effect->type & EFFECT_TYPE_ACTIVATE) && (pchain.triggering_effect->handler->current.location == LOCATION_SZONE)) { + pchain.triggering_effect->handler->set_status(STATUS_LEAVE_CONFIRMED, TRUE); + } +} +void field::change_target(uint8_t chaincount, group* targets) { + if(core.current_chain.size() == 0) + return; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + group* ot = core.current_chain[chaincount - 1].target_cards; + if(ot) { + effect* te = core.current_chain[chaincount - 1].triggering_effect; + for(auto& pcard : ot->container) + pcard->release_relation(core.current_chain[chaincount - 1]); + ot->container = targets->container; + for(auto& pcard : ot->container) + pcard->create_relation(core.current_chain[chaincount - 1]); + if(te->is_flag(EFFECT_FLAG_CARD_TARGET)) { + for(auto& pcard : ot->container) { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(pcard->get_info_location()); + } + } + } +} +void field::change_target_player(uint8_t chaincount, uint8_t playerid) { + if(core.current_chain.size() == 0) + return; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + core.current_chain[chaincount - 1].target_player = playerid; +} +void field::change_target_param(uint8_t chaincount, int32_t param) { + if(core.current_chain.size() == 0) + return; + if(chaincount > core.current_chain.size() || chaincount < 1) + chaincount = (uint8_t)core.current_chain.size(); + core.current_chain[chaincount - 1].target_param = param; +} +void field::remove_counter(uint32_t reason, card* pcard, uint32_t rplayer, uint32_t s, uint32_t o, uint32_t countertype, uint32_t count) { + add_process(PROCESSOR_REMOVE_COUNTER, 0, nullptr, (group*)pcard, (rplayer << 16) + (s << 8) + o, countertype, count, reason); +} +void field::remove_overlay_card(uint32_t reason, card* pcard, uint32_t rplayer, uint32_t s, uint32_t o, uint16_t min, uint16_t max) { + add_process(PROCESSOR_REMOVE_OVERLAY, 0, nullptr, (group*)pcard, (rplayer << 16) + (s << 8) + o, (max << 16) + min, reason); +} +void field::get_control(const card_set& targets, effect* reason_effect, uint32_t reason_player, uint32_t playerid, uint32_t reset_phase, uint32_t reset_count, uint32_t zone) { + group* ng = pduel->new_group(targets); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_GET_CONTROL, 0, reason_effect, ng, 0, (reason_player << 28) + (playerid << 24) + (reset_phase << 8) + reset_count, zone); +} +void field::get_control(card* target, effect* reason_effect, uint32_t reason_player, uint32_t playerid, uint32_t reset_phase, uint32_t reset_count, uint32_t zone) { + card_set tset{ target }; + get_control(tset, reason_effect, reason_player, playerid, reset_phase, reset_count, zone); +} +void field::swap_control(effect* reason_effect, uint32_t reason_player, const card_set& targets1, const card_set& targets2, uint32_t reset_phase, uint32_t reset_count) { + group* ng1 = pduel->new_group(targets1); + ng1->is_readonly = GTYPE_READ_ONLY; + group* ng2 = pduel->new_group(targets2); + ng2->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_SWAP_CONTROL, 0, reason_effect, ng1, reason_player, reset_phase, reset_count, 0, ng2); +} +void field::swap_control(effect* reason_effect, uint32_t reason_player, card* pcard1, card* pcard2, uint32_t reset_phase, uint32_t reset_count) { + card_set tset1{ pcard1 }; + card_set tset2{ pcard2 }; + swap_control(reason_effect, reason_player, tset1, tset2, reset_phase, reset_count); +} +void field::equip(uint32_t equip_player, card* equip_card, card* target, uint32_t up, uint32_t is_step) { + add_process(PROCESSOR_EQUIP, 0, nullptr, (group*)target, 0, equip_player + (up << 16) + (is_step << 24), 0, 0, equip_card); +} +void field::draw(effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, int32_t count) { + add_process(PROCESSOR_DRAW, 0, reason_effect, 0, reason, (reason_player << 4) + (playerid), count); +} +void field::damage(effect* reason_effect, uint32_t reason, uint32_t reason_player, card* reason_card, uint32_t playerid, int32_t amount, uint32_t is_step) { + uint32_t arg2 = (is_step << 4) + (reason_player << 2) + (playerid); + if (reason & REASON_BATTLE) + add_process(PROCESSOR_DAMAGE, 0, (effect*)reason_card, 0, reason, arg2, amount); + else + add_process(PROCESSOR_DAMAGE, 0, reason_effect, 0, reason, arg2, amount); +} +void field::recover(effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, int32_t amount, uint32_t is_step) { + add_process(PROCESSOR_RECOVER, 0, reason_effect, 0, reason, (is_step << 4) + (reason_player << 2) + (playerid), amount); +} +void field::summon(uint32_t sumplayer, card* target, effect* proc, uint32_t ignore_count, uint32_t min_tribute, uint32_t zone, uint32_t action_type) { + add_process(PROCESSOR_SUMMON_RULE, 0, proc, (group*)target, sumplayer + (ignore_count << 8) + (min_tribute << 16) + (zone << 24), action_type); +} +void field::mset(uint32_t setplayer, card* target, effect* proc, uint32_t ignore_count, uint32_t min_tribute, uint32_t zone, uint32_t action_type) { + add_process(PROCESSOR_MSET, 0, proc, (group*)target, setplayer + (ignore_count << 8) + (min_tribute << 16) + (zone << 24), action_type); +} +void field::special_summon_rule(uint32_t sumplayer, card* target, uint32_t summon_type, uint32_t action_type) { + add_process(PROCESSOR_SPSUMMON_RULE, 0, 0, (group*)target, sumplayer, summon_type, action_type); +} +void field::special_summon(const card_set& target, uint32_t sumtype, uint32_t sumplayer, uint32_t playerid, uint32_t nocheck, uint32_t nolimit, uint32_t positions, uint32_t zone) { + if((positions & POS_FACEDOWN) && is_player_affected_by_effect(sumplayer, EFFECT_DIVINE_LIGHT)) + positions = (positions & POS_FACEUP) | ((positions & POS_FACEDOWN) >> 1); + for(auto& pcard : target) { + pcard->temp.reason = pcard->current.reason; + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->summon_info = (sumtype & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_SPECIAL | ((uint32_t)pcard->current.location << 16); + pcard->summon_player = sumplayer; + pcard->current.reason = REASON_SPSUMMON; + pcard->current.reason_effect = core.reason_effect; + pcard->current.reason_player = core.reason_player; + pcard->spsummon_param = (playerid << 24) + (nocheck << 16) + (nolimit << 8) + positions; + } + group* pgroup = pduel->new_group(target); + pgroup->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_SPSUMMON, 0, core.reason_effect, pgroup, core.reason_player, zone); +} +void field::special_summon_step(card* target, uint32_t sumtype, uint32_t sumplayer, uint32_t playerid, uint32_t nocheck, uint32_t nolimit, uint32_t positions, uint32_t zone) { + if((positions & POS_FACEDOWN) && is_player_affected_by_effect(sumplayer, EFFECT_DIVINE_LIGHT)) + positions = (positions & POS_FACEUP) | ((positions & POS_FACEDOWN) >> 1); + target->temp.reason = target->current.reason; + target->temp.reason_effect = target->current.reason_effect; + target->temp.reason_player = target->current.reason_player; + target->summon_info = (sumtype & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_SPECIAL | ((uint32_t)target->current.location << 16); + target->summon_player = sumplayer; + target->current.reason = REASON_SPSUMMON; + target->current.reason_effect = core.reason_effect; + target->current.reason_player = core.reason_player; + target->spsummon_param = (playerid << 24) + (nocheck << 16) + (nolimit << 8) + positions; + add_process(PROCESSOR_SPSUMMON_STEP, 0, core.reason_effect, nullptr, zone, 0, 0, 0, target); +} +void field::special_summon_complete(effect* reason_effect, uint8_t reason_player) { + group* ng = pduel->new_group(); + ng->container.swap(core.special_summoning); + ng->is_readonly = GTYPE_READ_ONLY; + core.hint_timing[reason_player] |= TIMING_SPSUMMON; + add_process(PROCESSOR_SPSUMMON, 1, reason_effect, ng, reason_player, 0); +} +void field::destroy(card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence) { + for(auto cit = targets.begin(); cit != targets.end();) { + card* pcard = *cit; + if(pcard->is_status(STATUS_DESTROY_CONFIRMED) && core.destroy_canceled.find(pcard) == core.destroy_canceled.end()) { + cit = targets.erase(cit); + continue; + } + pcard->temp.reason = pcard->current.reason; + pcard->current.reason = reason; + if(reason_player != PLAYER_SELFDES) { + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + if(reason_effect) + pcard->current.reason_effect = reason_effect; + pcard->current.reason_player = reason_player; + } + uint32_t p = playerid; + if(!(destination & (LOCATION_HAND | LOCATION_DECK | LOCATION_REMOVED))) + destination = LOCATION_GRAVE; + if(destination && p == PLAYER_NONE) + p = pcard->owner; + if(destination & (LOCATION_GRAVE | LOCATION_REMOVED)) + p = pcard->owner; + pcard->set_status(STATUS_DESTROY_CONFIRMED, TRUE); + pcard->sendto_param.set(p, POS_FACEUP, destination, sequence); + ++cit; + } + group* ng = pduel->new_group(targets); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_DESTROY, 0, reason_effect, ng, reason, reason_player); +} +void field::destroy(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence) { + card_set tset{ target }; + destroy(tset, reason_effect, reason, reason_player, playerid, destination, sequence); +} +void field::release(const card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player) { + for(auto& pcard : targets) { + pcard->temp.reason = pcard->current.reason; + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->current.reason = reason; + pcard->current.reason_effect = reason_effect; + pcard->current.reason_player = reason_player; + pcard->sendto_param.set(pcard->owner, POS_FACEUP, LOCATION_GRAVE); + } + group* ng = pduel->new_group(targets); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_RELEASE, 0, reason_effect, ng, reason, reason_player); +} +void field::release(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player) { + card_set tset{ target }; + release(tset, reason_effect, reason, reason_player); +} +void field::send_to(const card_set& targets, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence, uint32_t position, uint8_t send_activating) { + if(destination & LOCATION_ONFIELD) + return; + for(auto& pcard : targets) { + pcard->temp.reason = pcard->current.reason; + pcard->current.reason = reason; + if(reason_player != PLAYER_SELFDES) { + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->current.reason_effect = reason_effect; + pcard->current.reason_player = reason_player; + } + uint32_t p = playerid; + // send to hand from deck and playerid not given => send to the hand of controler + if(p == PLAYER_NONE && (destination & LOCATION_HAND) && (pcard->current.location & LOCATION_DECK) && pcard->current.controler == reason_player) + p = reason_player; + if(destination & (LOCATION_GRAVE | LOCATION_REMOVED) || p == PLAYER_NONE) + p = pcard->owner; + if(destination == LOCATION_GRAVE && pcard->current.location == LOCATION_REMOVED) + pcard->current.reason |= REASON_RETURN; + uint32_t pos = position; + if(destination != LOCATION_REMOVED) + pos = POS_FACEUP; + else if(position == 0) + pos = pcard->current.position; + pcard->sendto_param.set(p, pos, destination, sequence); + } + group* ng = pduel->new_group(targets); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_SENDTO, 0, reason_effect, ng, reason, reason_player, send_activating); +} +void field::send_to(card* target, effect* reason_effect, uint32_t reason, uint32_t reason_player, uint32_t playerid, uint32_t destination, uint32_t sequence, uint32_t position, uint8_t send_activating) { + card_set tset{ target }; + send_to(tset, reason_effect, reason, reason_player, playerid, destination, sequence, position, send_activating); +} +void field::move_to_field(card* target, uint32_t move_player, uint32_t playerid, uint32_t destination, uint32_t positions, uint32_t enable, uint32_t ret, uint32_t pzone, uint32_t zone) { + if(!(destination & LOCATION_ONFIELD) || !positions) + return; + if(destination == target->current.location && playerid == target->current.controler && target->current.pzone == !!pzone) + return; + target->to_field_param = (move_player << 24) + (playerid << 16) + (destination << 8) + positions; + add_process(PROCESSOR_MOVETOFIELD, 0, 0, (group*)target, enable, ret + (pzone << 8), zone); +} +void field::change_position(const card_set& targets, effect* reason_effect, uint32_t reason_player, uint32_t au, uint32_t ad, uint32_t du, uint32_t dd, uint32_t flag, uint32_t enable) { + group* ng = pduel->new_group(targets); + ng->is_readonly = GTYPE_READ_ONLY; + for(auto& pcard : targets) { + if(pcard->current.position == POS_FACEUP_ATTACK) + pcard->position_param = au; + else if(pcard->current.position == POS_FACEDOWN_DEFENSE) + pcard->position_param = dd; + else if(pcard->current.position == POS_FACEUP_DEFENSE) + pcard->position_param = du; + else + pcard->position_param = ad; + pcard->position_param |= flag; + } + add_process(PROCESSOR_CHANGEPOS, 0, reason_effect, ng, reason_player, enable); +} +void field::change_position(card* target, effect* reason_effect, uint32_t reason_player, uint32_t npos, uint32_t flag, uint32_t enable) { + group* ng = pduel->new_group(target); + ng->is_readonly = GTYPE_READ_ONLY; + target->position_param = npos; + target->position_param |= flag; + add_process(PROCESSOR_CHANGEPOS, 0, reason_effect, ng, reason_player, enable); +} +void field::operation_replace(int32_t type, int32_t step, group* targets) { + int32_t is_destroy = (type == EFFECT_DESTROY_REPLACE) ? TRUE : FALSE; + auto pr = effects.continuous_effect.equal_range(type); + std::vector opp_effects; + for(auto eit = pr.first; eit != pr.second;) { + effect* reffect = eit->second; + ++eit; + if(reffect->get_handler_player() == infos.turn_player) + add_process(PROCESSOR_OPERATION_REPLACE, step, reffect, targets, is_destroy, 0); + else + opp_effects.push_back(reffect); + } + for(auto& peffect : opp_effects) + add_process(PROCESSOR_OPERATION_REPLACE, step, peffect, targets, is_destroy, 0); +} +void field::select_tribute_cards(card* target, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max, uint8_t toplayer, uint32_t zone) { + add_process(PROCESSOR_SELECT_TRIBUTE, 0, 0, (group*)target, playerid + ((uint32_t)cancelable << 16), min + (max << 16), toplayer, zone); +} +int32_t field::draw(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t playerid, int32_t count) { + switch(step) { + case 0: { + card_vector cv; + uint32_t public_count = 0; + if(!(reason & REASON_RULE) && !is_player_can_draw(playerid)) { + returns.ivalue[0] = 0; + return TRUE; + } + if(count == 0) { + returns.ivalue[0] = 0; + return TRUE; + } + core.overdraw[playerid] = FALSE; + for(int32_t i = 0; i < count; ++i) { + if(player[playerid].list_main.empty()) { + core.overdraw[playerid] = TRUE; + break; + } + card* pcard = player[playerid].list_main.back(); + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + player[playerid].list_main.pop_back(); + pcard->previous.controler = pcard->current.controler; + pcard->previous.location = pcard->current.location; + pcard->previous.sequence = pcard->current.sequence; + pcard->previous.position = pcard->current.position; + pcard->previous.pzone = pcard->current.pzone; + pcard->current.controler = PLAYER_NONE; + pcard->current.reason_effect = reason_effect; + pcard->current.reason_player = reason_player; + pcard->current.reason = reason | REASON_DRAW; + pcard->current.location = 0; + add_card(playerid, pcard, LOCATION_HAND, 0); + pcard->enable_field_effect(true); + effect* pub = pcard->is_affected_by_effect(EFFECT_PUBLIC); + if(pub) + ++public_count; + pcard->current.position = pub ? POS_FACEUP : POS_FACEDOWN; + cv.push_back(pcard); + pcard->reset(RESET_TOHAND, RESET_EVENT); + } + core.hint_timing[playerid] |= TIMING_DRAW + TIMING_TOHAND; + adjust_instant(); + if(core.overdraw[playerid] && (reason & REASON_RULE)) + adjust_all(); + core.units.begin()->arg2 = (core.units.begin()->arg2 & 0xff000000) | (uint32_t)cv.size(); + card_set* drawed_set = new card_set(cv.begin(), cv.end()); + core.units.begin()->ptr1 = drawed_set; + if(cv.size()) { + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + if(player[playerid].list_main.size()) { + card* ptop = player[playerid].list_main.back(); + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)cv.size()); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + pduel->write_buffer8(MSG_DRAW); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)cv.size()); + for (auto& pcard : cv) + pduel->write_buffer32(pcard->data.code | (pcard->is_position(POS_FACEUP) ? 0x80000000 : 0)); + if(core.deck_reversed && (public_count < cv.size())) { + pduel->write_buffer8(MSG_CONFIRM_CARDS); + pduel->write_buffer8(1 - playerid); + pduel->write_buffer8(0); + pduel->write_buffer8((uint8_t)drawed_set->size()); + for(auto& pcard : *drawed_set) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + shuffle(playerid, LOCATION_HAND); + } + for (auto& pcard : *drawed_set) { + if(pcard->owner != pcard->current.controler) { + effect* deffect = pduel->new_effect(); + deffect->owner = pcard; + deffect->code = 0; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 67; + deffect->reset_flag = RESET_EVENT + 0x1fe0000; + pcard->add_effect(deffect); + } + raise_single_event(pcard, 0, EVENT_DRAW, reason_effect, reason, reason_player, playerid, 0); + raise_single_event(pcard, 0, EVENT_TO_HAND, reason_effect, reason, reason_player, playerid, 0); + raise_single_event(pcard, 0, EVENT_MOVE, reason_effect, reason, reason_player, playerid, 0); + } + process_single_event(); + raise_event(*drawed_set, EVENT_DRAW, reason_effect, reason, reason_player, playerid, (uint32_t)cv.size()); + raise_event(*drawed_set, EVENT_TO_HAND, reason_effect, reason, reason_player, playerid, (uint32_t)cv.size()); + raise_event(*drawed_set, EVENT_MOVE, reason_effect, reason, reason_player, playerid, (uint32_t)cv.size()); + process_instant_event(); + } + return FALSE; + } + case 1: { + card_set* drawed_set = (card_set*)core.units.begin()->ptr1; + core.operated_set.swap(*drawed_set); + delete drawed_set; + returns.ivalue[0] = (int32_t)core.operated_set.size(); + return TRUE; + } + } + return TRUE; +} +int32_t field::damage(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, card* reason_card, uint8_t playerid, int32_t amount, uint32_t is_step) { + switch(step) { + case 0: { + effect_set eset; + returns.ivalue[0] = amount; + if(amount <= 0) + return TRUE; + if(!(reason & REASON_RDAMAGE)) { + filter_player_effect(playerid, EFFECT_REVERSE_DAMAGE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(reason_card, PARAM_TYPE_CARD); + if(eset[i]->check_value_condition(4)) { + recover(reason_effect, (reason & REASON_RRECOVER) | REASON_RDAMAGE | REASON_EFFECT, reason_player, playerid, amount, is_step); + core.units.begin()->step = 2; + return FALSE; + } + } + } + eset.clear(); + filter_player_effect(playerid, EFFECT_REFLECT_DAMAGE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(amount, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(reason_card, PARAM_TYPE_CARD); + if (eset[i]->check_value_condition(5)) { + playerid = 1 - playerid; + core.units.begin()->value1 = 1; + break; + } + } + int32_t val = amount; + eset.clear(); + filter_player_effect(playerid, EFFECT_CHANGE_DAMAGE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(val, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(reason_card, PARAM_TYPE_CARD); + val = eset[i]->get_value(5); + returns.ivalue[0] = val; + if(val == 0) + return TRUE; + } + eset.clear(); + filter_player_effect(playerid, EFFECT_REPLACE_DAMAGE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(val, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_player, PARAM_TYPE_INT); + pduel->lua->add_param(reason_card, PARAM_TYPE_CARD); + val = eset[i]->get_value(5); + returns.ivalue[0] = val; + if (val == 0) + return TRUE; + } + core.units.begin()->arg3 = val; + if(is_step) { + core.units.begin()->step = 9; + return TRUE; + } + return FALSE; + } + case 1: { + uint32_t is_reflect = core.units.begin()->value1; + if(is_reflect) + playerid = 1 - playerid; + if(is_reflect || (reason & REASON_RRECOVER)) + core.units.begin()->step = 2; + core.hint_timing[playerid] |= TIMING_DAMAGE; + player[playerid].lp -= amount; + pduel->write_buffer8(MSG_DAMAGE); + pduel->write_buffer8(playerid); + pduel->write_buffer32(amount); + raise_event(reason_card, EVENT_DAMAGE, reason_effect, reason, reason_player, playerid, amount); + if(reason == REASON_BATTLE && reason_card) { + if((player[playerid].lp <= 0) && (core.attack_target == 0) && reason_card->is_affected_by_effect(EFFECT_MATCH_KILL) && !(is_player_affected_by_effect(playerid, EFFECT_CANNOT_LOSE_KOISHI))) { + pduel->write_buffer8(MSG_MATCH_KILL); + pduel->write_buffer32(reason_card->data.code); + } + raise_single_event(reason_card, 0, EVENT_BATTLE_DAMAGE, 0, 0, reason_player, playerid, amount); + raise_event(reason_card, EVENT_BATTLE_DAMAGE, 0, 0, reason_player, playerid, amount); + process_single_event(); + } + process_instant_event(); + return FALSE; + } + case 2: { + returns.ivalue[0] = amount; + return TRUE; + } + case 3: { + returns.ivalue[0] = 0; + return TRUE; + } + case 10: { + //dummy + return TRUE; + } + } + return TRUE; +} +int32_t field::recover(uint16_t step, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t playerid, int32_t amount, uint32_t is_step) { + switch(step) { + case 0: { + effect_set eset; + returns.ivalue[0] = amount; + if(amount <= 0) + return TRUE; + if(!(reason & REASON_RRECOVER)) { + filter_player_effect(playerid, EFFECT_REVERSE_RECOVER, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + pduel->lua->add_param(reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + damage(reason_effect, (reason & REASON_RDAMAGE) | REASON_RRECOVER | REASON_EFFECT, reason_player, 0, playerid, amount, is_step); + core.units.begin()->step = 2; + return FALSE; + } + } + } + if(is_step) { + core.units.begin()->step = 9; + return TRUE; + } + return FALSE; + } + case 1: { + if(reason & REASON_RDAMAGE) + core.units.begin()->step = 2; + core.hint_timing[playerid] |= TIMING_RECOVER; + int32_t limit = 0; + if (player[playerid].lp > 0) { + limit = INT32_MAX - player[playerid].lp; + } + int32_t val = amount; + if (val > limit) { + val = limit; + } + player[playerid].lp += val; + pduel->write_buffer8(MSG_RECOVER); + pduel->write_buffer8(playerid); + pduel->write_buffer32(val); + raise_event(nullptr, EVENT_RECOVER, reason_effect, reason, reason_player, playerid, amount); + process_instant_event(); + return FALSE; + } + case 2: { + returns.ivalue[0] = amount; + return TRUE; + } + case 3: { + returns.ivalue[0] = 0; + return TRUE; + } + case 10: { + //dummy + return TRUE; + } + } + return TRUE; +} +int32_t field::pay_lp_cost(uint32_t step, uint8_t playerid, uint32_t cost, uint32_t must_pay) { + switch(step) { + case 0: { + effect_set eset; + int32_t val = cost; + filter_player_effect(playerid, EFFECT_LPCOST_CHANGE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + pduel->lua->add_param(val, PARAM_TYPE_INT); + val = eset[i]->get_value(3); + } + if(val <= 0) + return TRUE; + core.units.begin()->arg2 = val; + core.select_options.clear(); + core.select_effects.clear(); + if(val <= player[playerid].lp) { + core.select_options.push_back(11); + core.select_effects.push_back(nullptr); + } + if(must_pay) { + if(core.select_options.size() == 0) + return TRUE; + returns.ivalue[0] = 0; + return FALSE; + } + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = val; + e.reason = 0; + e.reason_effect = core.reason_effect; + e.reason_player = playerid; + auto pr = effects.continuous_effect.equal_range(EFFECT_LPCOST_REPLACE); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) { + core.select_options.push_back(peffect->description); + core.select_effects.push_back(peffect); + } + } + if(core.select_options.size() == 0) + return TRUE; + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else if(core.select_effects[0] == 0 && core.select_effects.size() == 2) + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_effects[1]->handler, playerid, 218); + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, playerid, 0); + return FALSE; + } + case 1: { + effect* peffect = core.select_effects[returns.ivalue[0]]; + if(!peffect) { + player[playerid].lp -= cost; + pduel->write_buffer8(MSG_PAY_LPCOST); + pduel->write_buffer8(playerid); + pduel->write_buffer32(cost); + raise_event(nullptr, EVENT_PAY_LPCOST, core.reason_effect, 0, playerid, playerid, cost); + process_instant_event(); + return TRUE; + } + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = cost; + e.reason = 0; + e.reason_effect = core.reason_effect; + e.reason_player = playerid; + solve_continuous(playerid, peffect, e); + return TRUE; + } + } + return TRUE; +} +// rplayer rmoves counter from pcard or the field +// s,o: binary value indicating the available side +// from pcard: Card.RemoveCounter() -> here -> card::remove_counter() -> the script should raise EVENT_REMOVE_COUNTER if necessary +// from the field: Duel.RemoveCounter() -> here -> field::select_counter() -> the system raises EVENT_REMOVE_COUNTER automatically +int32_t field::remove_counter(uint16_t step, uint32_t reason, card* pcard, uint8_t rplayer, uint8_t s, uint8_t o, uint16_t countertype, uint16_t count) { + switch(step) { + case 0: { + core.select_options.clear(); + core.select_effects.clear(); + if((pcard && pcard->get_counter(countertype) >= count) || (!pcard && get_field_counter(rplayer, s, o, countertype))) { + core.select_options.push_back(10); + core.select_effects.push_back(nullptr); + } + auto pr = effects.continuous_effect.equal_range(EFFECT_RCOUNTER_REPLACE + countertype); + tevent e; + e.event_cards = 0; + e.event_player = rplayer; + e.event_value = count; + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = rplayer; + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) { + core.select_options.push_back(peffect->description); + core.select_effects.push_back(peffect); + } + } + returns.ivalue[0] = 0; + if(core.select_options.size() == 0) + return TRUE; + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else if(core.select_effects[0] == nullptr && core.select_effects.size() == 2) + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_effects[1]->handler, rplayer, 220); + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, rplayer, 0); + return FALSE; + } + case 1: { + effect* peffect = core.select_effects[returns.ivalue[0]]; + if(peffect) { + tevent e; + e.event_cards = 0; + e.event_player = rplayer; + e.event_value = count; + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = rplayer; + solve_continuous(rplayer, peffect, e); + core.units.begin()->step = 3; + return FALSE; + } + if(pcard) { + returns.ivalue[0] = pcard->remove_counter(countertype, count); + core.units.begin()->step = 3; + return FALSE; + } + add_process(PROCESSOR_SELECT_COUNTER, 0, nullptr, nullptr, rplayer, countertype, count, (s << 8) + o); + return FALSE; + } + case 2: { + for(uint32_t i = 0; i < core.select_cards.size(); ++i) + if(returns.svalue[i] > 0) + core.select_cards[i]->remove_counter(countertype, returns.svalue[i]); + return FALSE; + } + case 3: { + raise_event(nullptr, EVENT_REMOVE_COUNTER + countertype, core.reason_effect, reason, rplayer, rplayer, count); + process_instant_event(); + return FALSE; + } + case 4: { + returns.ivalue[0] = 1; + return TRUE; + } + } + return TRUE; +} +int32_t field::remove_overlay_card(uint16_t step, uint32_t reason, card* pcard, uint8_t rplayer, uint8_t s, uint8_t o, uint16_t min, uint16_t max) { + switch(step) { + case 0: { + core.select_options.clear(); + core.select_effects.clear(); + int32_t minc = min; + effect_set eset; + filter_player_effect(rplayer, EFFECT_OVERLAY_REMOVE_COST_CHANGE_KOISHI, &eset); + for(int32_t i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(rplayer, PARAM_TYPE_INT); + pduel->lua->add_param(minc, PARAM_TYPE_INT); + pduel->lua->add_param(reason, PARAM_TYPE_INT); + int32_t param_count; + if(pcard) { + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + param_count = 5; + } else { + pduel->lua->add_param(s, PARAM_TYPE_INT); + pduel->lua->add_param(o, PARAM_TYPE_INT); + param_count = 6; + } + minc = eset[i]->get_value(param_count); + } + core.units.begin()->arg2 = (max << 16) + minc; + if((pcard && (int32_t)pcard->xyz_materials.size() >= minc) || (!pcard && get_overlay_count(rplayer, s, o) >= minc)) { + core.select_options.push_back(12); + core.select_effects.push_back(nullptr); + } + auto pr = effects.continuous_effect.equal_range(EFFECT_OVERLAY_REMOVE_REPLACE); + tevent e; + e.event_cards = 0; + e.event_player = rplayer; + e.event_value = minc; + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = rplayer; + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_activateable(peffect->get_handler_player(), e)) { + core.select_options.push_back(peffect->description); + core.select_effects.push_back(peffect); + } + } + returns.ivalue[0] = 0; + if(core.select_options.size() == 0) + return TRUE; + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else if(core.select_effects[0] == 0 && core.select_effects.size() == 2) + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_effects[1]->handler, rplayer, 219); + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, rplayer, 0); + return FALSE; + } + case 1: { + if(effect* peffect = core.select_effects[returns.ivalue[0]]) { + tevent e; + e.event_cards = 0; + e.event_player = rplayer; + e.event_value = min + (max << 16); + e.reason = reason; + e.reason_effect = core.reason_effect; + e.reason_player = rplayer; + solve_continuous(rplayer, peffect, e); + core.units.begin()->peffect = peffect; + } + return FALSE; + } + case 2: { + uint16_t cancelable = FALSE; + if(core.units.begin()->peffect) { + int32_t replace_count = returns.ivalue[0]; + if(replace_count >= max) + return TRUE; + min -= replace_count; + max -= replace_count; + if(min <= 0) { + cancelable = TRUE; + min = 0; + } + core.units.begin()->value1 = replace_count; + } + core.select_cards.clear(); + if(pcard) { + core.select_cards.assign(pcard->xyz_materials.begin(), pcard->xyz_materials.end()); + } else { + card_set cset; + get_overlay_group(rplayer, s, o, &cset); + core.select_cards.assign(cset.begin(), cset.end()); + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(rplayer); + pduel->write_buffer32(519); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, rplayer + (cancelable << 16), min + (max << 16)); + return FALSE; + } + case 3: { + card_set cset; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + cset.insert(core.select_cards[returns.bvalue[i + 1]]); + for(auto cit = cset.begin(); cit != cset.end(); ++cit) { + card* xcard = *cit; + if(xcard->overlay_target) + xcard->overlay_target->removed_overlay_count++; + } + send_to(cset, core.reason_effect, reason, rplayer, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + case 4: { + returns.ivalue[0] += core.units.begin()->value1; + return TRUE; + } + } + return TRUE; +} +int32_t field::get_control(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets, uint8_t playerid, uint16_t reset_phase, uint8_t reset_count, uint32_t zone) { + switch(step) { + case 0: { + card_set* destroy_set = new card_set; + core.units.begin()->ptr1 = destroy_set; + for(auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + pcard->filter_disable_related_cards(); + bool change = true; + if(pcard->overlay_target) + change = false; + else if(pcard->current.controler == playerid) + change = false; + else if(pcard->current.controler == PLAYER_NONE) + change = false; + else if(pcard->current.location != LOCATION_MZONE) + change = false; + else if(!pcard->is_capable_change_control()) + change = false; + else if(!pcard->is_affect_by_effect(reason_effect)) + change = false; + else if(core.duel_rule <= 4 && (pcard->get_type() & TYPE_TRAPMONSTER) + && get_useable_count(pcard, playerid, LOCATION_SZONE, reason_player, LOCATION_REASON_CONTROL) <= 0) + change = false; + if (!change) + cit = targets->container.erase(cit); + else + ++cit; + } + int32_t fcount = get_useable_count(nullptr, playerid, LOCATION_MZONE, reason_player, LOCATION_REASON_CONTROL, zone); + if(fcount <= 0) { + destroy_set->swap(targets->container); + core.units.begin()->step = 5; + return FALSE; + } + if((int32_t)targets->container.size() > fcount) { + core.select_cards.clear(); + for(auto& pcard : targets->container) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(502); + uint32_t ct = (uint32_t)targets->container.size() - fcount; + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, ct + (ct << 16)); + } else + core.units.begin()->step = 1; + return FALSE; + } + case 1: { + card_set* destroy_set = (card_set*)core.units.begin()->ptr1; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + destroy_set->insert(pcard); + targets->container.erase(pcard); + } + return FALSE; + } + case 2: { + for(auto& pcard : targets->container) { + if(pcard->unique_code && (pcard->unique_location & LOCATION_MZONE)) + remove_unique_card(pcard); + } + targets->it = targets->container.begin(); + return FALSE; + } + case 3: { + if(targets->it == targets->container.end()) { + adjust_instant(); + core.units.begin()->step = 4; + return FALSE; + } + card* pcard = *targets->it; + move_to_field(pcard, (reason_player != PLAYER_NONE) ? reason_player : playerid, playerid, LOCATION_MZONE, pcard->current.position, FALSE, 0, FALSE, zone); + return FALSE; + } + case 4: { + if (targets->it != targets->container.end()) { + card* pcard = *targets->it; + pcard->set_status(STATUS_ATTACK_CANCELED, TRUE); + set_control(pcard, playerid, reset_phase, reset_count); + pcard->reset(RESET_CONTROL, RESET_EVENT); + pcard->filter_disable_related_cards(); + ++targets->it; + core.units.begin()->step = 2; + } + return FALSE; + } + case 5: { + for(auto cit = targets->container.begin(); cit != targets->container.end(); ) { + card* pcard = *cit; + if(!(pcard->current.location & LOCATION_ONFIELD)) { + cit = targets->container.erase(cit); + continue; + } + if(pcard->unique_code && (pcard->unique_location & LOCATION_MZONE)) + add_unique_card(pcard); + raise_single_event(pcard, 0, EVENT_CONTROL_CHANGED, reason_effect, REASON_EFFECT, reason_player, playerid, 0); + raise_single_event(pcard, 0, EVENT_MOVE, reason_effect, REASON_EFFECT, reason_player, playerid, 0); + ++cit; + } + if(targets->container.size()) { + raise_event(targets->container, EVENT_CONTROL_CHANGED, reason_effect, REASON_EFFECT, reason_player, playerid, 0); + raise_event(targets->container, EVENT_MOVE, reason_effect, REASON_EFFECT, reason_player, playerid, 0); + } + process_single_event(); + process_instant_event(); + return FALSE; + } + case 6: { + card_set* destroy_set = (card_set*)core.units.begin()->ptr1; + if(destroy_set->size()) + destroy(*destroy_set, 0, REASON_RULE, PLAYER_NONE); + delete destroy_set; + return FALSE; + } + case 7: { + core.operated_set = targets->container; + returns.ivalue[0] = (int32_t)targets->container.size(); + pduel->delete_group(targets); + return TRUE; + } + } + return TRUE; +} +int32_t field::swap_control(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets1, group* targets2, uint16_t reset_phase, uint8_t reset_count) { + switch(step) { + case 0: { + core.units.begin()->step = 9; + if(targets1->container.size() == 0) + return FALSE; + if(targets2->container.size() == 0) + return FALSE; + if(targets1->container.size() != targets2->container.size()) + return FALSE; + for(auto& pcard : targets1->container) + pcard->filter_disable_related_cards(); + for(auto& pcard : targets2->container) + pcard->filter_disable_related_cards(); + auto cit1 = targets1->container.begin(); + auto cit2 = targets2->container.begin(); + uint8_t p1 = (*cit1)->current.controler, p2 = (*cit2)->current.controler; + if(p1 == p2 || p1 == PLAYER_NONE || p2 == PLAYER_NONE) + return FALSE; + for(auto& pcard : targets1->container) { + if(pcard->overlay_target) + return FALSE; + if(pcard->current.controler != p1) + return FALSE; + if(pcard->current.location != LOCATION_MZONE) + return FALSE; + if(!pcard->is_capable_change_control()) + return FALSE; + if(!pcard->is_affect_by_effect(reason_effect)) + return FALSE; + } + for(auto& pcard : targets2->container) { + if(pcard->overlay_target) + return FALSE; + if(pcard->current.controler != p2) + return FALSE; + if(pcard->current.location != LOCATION_MZONE) + return FALSE; + if(!pcard->is_capable_change_control()) + return FALSE; + if(!pcard->is_affect_by_effect(reason_effect)) + return FALSE; + } + int32_t ct = get_useable_count(nullptr, p1, LOCATION_MZONE, reason_player, LOCATION_REASON_CONTROL); + for(auto& pcard : targets1->container) { + if(pcard->current.sequence >= 5) + --ct; + } + if(ct < 0) + return FALSE; + ct = get_useable_count(nullptr, p2, LOCATION_MZONE, reason_player, LOCATION_REASON_CONTROL); + for(auto& pcard : targets2->container) { + if(pcard->current.sequence >= 5) + --ct; + } + if(ct < 0) + return FALSE; + for(auto& pcard : targets1->container) { + if(pcard->unique_code && (pcard->unique_location & LOCATION_MZONE)) + remove_unique_card(pcard); + } + for(auto& pcard : targets2->container) { + if(pcard->unique_code && (pcard->unique_location & LOCATION_MZONE)) + remove_unique_card(pcard); + } + targets1->it = targets1->container.begin(); + targets2->it = targets2->container.begin(); + core.units.begin()->step = 0; + return FALSE; + } + case 1: { + if (targets1->it == targets1->container.end() || targets2->it == targets2->container.end()) { + core.units.begin()->step = 3; + return FALSE; + } + card* pcard1 = *targets1->it; + card* pcard2 = *targets2->it; + uint8_t p1 = pcard1->current.controler; + uint8_t s1 = pcard1->current.sequence; + uint32_t flag; + get_useable_count(nullptr, p1, LOCATION_MZONE, reason_player, LOCATION_REASON_CONTROL, 0xff, &flag); + if(reason_player == p1) + flag = (flag & ~(0x1U << s1) & 0xff) | ~0x1f; + else + flag = ((flag & ~(0x1U << s1)) << 16 & 0xff0000) | ~0x1f0000; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(reason_player); + pduel->write_buffer32(pcard2->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, reason_player, flag, 1); + return FALSE; + } + case 2: { + if (targets1->it == targets1->container.end() || targets2->it == targets2->container.end()) { + core.units.begin()->step = 3; + return FALSE; + } + core.units.begin()->value1 = returns.bvalue[2]; + card* pcard1 = *targets1->it; + card* pcard2 = *targets2->it; + uint8_t p2 = pcard2->current.controler; + uint8_t s2 = pcard2->current.sequence; + uint32_t flag; + get_useable_count(nullptr, p2, LOCATION_MZONE, reason_player, LOCATION_REASON_CONTROL, 0xff, &flag); + if(reason_player == p2) + flag = (flag & ~(0x1U << s2) & 0xff) | ~0x1f; + else + flag = ((flag & ~(0x1U << s2)) << 16 & 0xff0000) | ~0x1f0000; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(reason_player); + pduel->write_buffer32(pcard1->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, reason_player, flag, 1); + return FALSE; + } + case 3: { + if (targets1->it == targets1->container.end() || targets2->it == targets2->container.end()) { + return FALSE; + } + card* pcard1 = *targets1->it; + card* pcard2 = *targets2->it; + uint8_t p1 = pcard1->current.controler, p2 = pcard2->current.controler; + uint8_t new_s1 = (uint8_t)core.units.begin()->value1, new_s2 = returns.bvalue[2]; + swap_card(pcard1, pcard2, new_s1, new_s2); + pcard1->reset(RESET_CONTROL, RESET_EVENT); + pcard2->reset(RESET_CONTROL, RESET_EVENT); + set_control(pcard1, p2, reset_phase, reset_count); + set_control(pcard2, p1, reset_phase, reset_count); + pcard1->set_status(STATUS_ATTACK_CANCELED, TRUE); + pcard2->set_status(STATUS_ATTACK_CANCELED, TRUE); + ++targets1->it; + ++targets2->it; + core.units.begin()->step = 0; + return FALSE; + } + case 4: { + targets1->container.insert(targets2->container.begin(), targets2->container.end()); + for(auto& pcard : targets1->container) { + pcard->filter_disable_related_cards(); + if(pcard->unique_code && (pcard->unique_location & LOCATION_MZONE)) + add_unique_card(pcard); + raise_single_event(pcard, 0, EVENT_CONTROL_CHANGED, reason_effect, REASON_EFFECT, reason_player, pcard->current.controler, 0); + raise_single_event(pcard, 0, EVENT_MOVE, reason_effect, REASON_EFFECT, reason_player, pcard->current.controler, 0); + } + raise_event(targets1->container, EVENT_CONTROL_CHANGED, reason_effect, REASON_EFFECT, reason_player, 0, 0); + raise_event(targets1->container, EVENT_MOVE, reason_effect, REASON_EFFECT, reason_player, 0, 0); + process_single_event(); + process_instant_event(); + return FALSE; + } + case 5: { + core.operated_set = targets1->container; + returns.ivalue[0] = 1; + pduel->delete_group(targets1); + pduel->delete_group(targets2); + return TRUE; + } + case 10: { + core.operated_set.clear(); + returns.ivalue[0] = 0; + pduel->delete_group(targets1); + pduel->delete_group(targets2); + return TRUE; + } + } + return TRUE; +} +int32_t field::self_destroy(uint16_t step, card* ucard, int32_t p) { + switch(step) { + case 0: { + if(core.unique_cards[p].find(ucard) == core.unique_cards[p].end()) { + core.unique_destroy_set.erase(ucard); + return TRUE; + } + card_set cset; + ucard->get_unique_target(&cset, p); + if(cset.size() == 0) + ucard->unique_fieldid = 0; + else if(cset.size() == 1) { + auto cit = cset.begin(); + ucard->unique_fieldid = (*cit)->fieldid; + } else { + core.select_cards.clear(); + uint8_t select_player = p; + for(auto& pcard : cset) { + if(pcard->current.controler == select_player && pcard->unique_fieldid != UINT_MAX) + core.select_cards.push_back(pcard); + } + if(core.select_cards.size() == 0) { + select_player = 1 - p; + for(auto& pcard : cset) { + if(pcard->current.controler == select_player && pcard->unique_fieldid != UINT_MAX) + core.select_cards.push_back(pcard); + } + } + if(core.select_cards.size() == 0) { + select_player = p; + for(auto& pcard : cset) { + if(pcard->current.controler == select_player) + core.select_cards.push_back(pcard); + } + } + if(core.select_cards.size() == 0) { + select_player = 1 - p; + for(auto& pcard : cset) { + if(pcard->current.controler == select_player) + core.select_cards.push_back(pcard); + } + } + if(core.select_cards.size() == 1) { + returns.bvalue[1] = 0; + } + else { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(select_player); + pduel->write_buffer32(534); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, select_player, 0x10001); + } + return FALSE; + } + core.unique_destroy_set.erase(ucard); + return TRUE; + } + case 1: { + card_set cset; + ucard->get_unique_target(&cset, p); + card* mcard = core.select_cards[returns.bvalue[1]]; + ucard->unique_fieldid = mcard->fieldid; + cset.erase(mcard); + for(auto& pcard : cset) { + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->current.reason_effect = ucard->unique_effect; + pcard->current.reason_player = ucard->current.controler; + } + destroy(cset, 0, REASON_RULE, PLAYER_SELFDES); + return FALSE; + } + case 2: { + core.unique_destroy_set.erase(ucard); + return TRUE; + } + case 10: { + if(core.self_destroy_set.empty()) + return FALSE; + auto it = core.self_destroy_set.begin(); + card* pcard = *it; + effect* peffect = pcard->is_affected_by_effect(EFFECT_SELF_DESTROY); + if(peffect) { + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->current.reason_effect = peffect; + pcard->current.reason_player = peffect->get_handler_player(); + destroy(pcard, 0, REASON_EFFECT, PLAYER_SELFDES); + } + core.self_destroy_set.erase(it); + core.units.begin()->step = 9; + return FALSE; + } + case 11: { + returns.ivalue[0] = 0; + core.operated_set.clear(); + return TRUE; + } + case 20: { + if(core.self_tograve_set.empty()) + return FALSE; + auto it = core.self_tograve_set.begin(); + card* pcard = *it; + effect* peffect = pcard->is_affected_by_effect(EFFECT_SELF_TOGRAVE); + if(peffect) { + pcard->temp.reason_effect = pcard->current.reason_effect; + pcard->temp.reason_player = pcard->current.reason_player; + pcard->current.reason_effect = peffect; + pcard->current.reason_player = peffect->get_handler_player(); + send_to(pcard, 0, REASON_EFFECT, PLAYER_SELFDES, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + } + core.self_tograve_set.erase(it); + core.units.begin()->step = 19; + return FALSE; + } + case 21: { + returns.ivalue[0] = 0; + core.operated_set.clear(); + return TRUE; + } + } + return TRUE; +} +int32_t field::trap_monster_adjust(uint16_t step) { + switch(step) { + case 0: { + if(core.duel_rule <= 4) { + core.units.begin()->step = 3; + return FALSE; + } + card_set* to_grave_set = new card_set; + core.units.begin()->ptr1 = to_grave_set; + return FALSE; + } + case 1: { + card_set* to_grave_set = (card_set*)core.units.begin()->ptr1; + uint8_t check_player = infos.turn_player; + if(core.units.begin()->value1) + check_player = 1 - infos.turn_player; + refresh_location_info_instant(); + int32_t fcount = get_useable_count(nullptr, check_player, LOCATION_SZONE, check_player, 0); + if(fcount <= 0) { + for(auto& pcard : core.trap_monster_adjust_set[check_player]) { + to_grave_set->insert(pcard); + } + core.trap_monster_adjust_set[check_player].clear(); + core.units.begin()->step = 2; + } else if((int32_t)core.trap_monster_adjust_set[check_player].size() > fcount) { + uint32_t ct = (uint32_t)core.trap_monster_adjust_set[check_player].size() - fcount; + core.select_cards.clear(); + for(auto& pcard : core.trap_monster_adjust_set[check_player]) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(check_player); + pduel->write_buffer32(502); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, check_player, ct + (ct << 16)); + } else + core.units.begin()->step = 2; + return FALSE; + } + case 2: { + card_set* to_grave_set = (card_set*)core.units.begin()->ptr1; + uint8_t check_player = infos.turn_player; + if(core.units.begin()->value1) + check_player = 1 - infos.turn_player; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + to_grave_set->insert(pcard); + core.trap_monster_adjust_set[check_player].erase(pcard); + } + } + case 3: { + if(!core.units.begin()->value1) { + core.units.begin()->value1 = 1; + core.units.begin()->step = 0; + } + return FALSE; + } + case 4: { + uint8_t tp = infos.turn_player; + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : core.trap_monster_adjust_set[tp]) { + pcard->reset(RESET_TURN_SET, RESET_EVENT); + if(core.duel_rule <= 4) + refresh_location_info_instant(); + move_to_field(pcard, tp, tp, LOCATION_SZONE, pcard->current.position, FALSE, RETURN_TRAP_MONSTER_TO_SZONE); + } + tp = 1 - tp; + } + if(card_set* to_grave_set = (card_set*)core.units.begin()->ptr1) { + if(to_grave_set->size()) + send_to(*to_grave_set, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + delete to_grave_set; + } + return TRUE; + } + } + return TRUE; +} +int32_t field::equip(uint16_t step, uint8_t equip_player, card * equip_card, card * target, uint32_t up, uint32_t is_step) { + switch(step) { + case 0: { + returns.ivalue[0] = FALSE; + if(!equip_card->is_affect_by_effect(core.reason_effect)) + return TRUE; + if(equip_card == target) + return TRUE; + bool to_grave = false; + if(target->current.location != LOCATION_MZONE || (target->current.position & POS_FACEDOWN)) + to_grave = true; + if(equip_card->current.location != LOCATION_SZONE) { + refresh_location_info_instant(); + if(get_useable_count(equip_card, equip_player, LOCATION_SZONE, equip_player, LOCATION_REASON_TOFIELD) <= 0) + to_grave = true; + } + if(to_grave) { + if(equip_card->current.location != LOCATION_GRAVE) + send_to(equip_card, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + core.units.begin()->step = 2; + return FALSE; + } + if (!(equip_card->data.type & TYPE_EQUIP)) + core.units.begin()->value1 = equip_card->get_type(); + if(equip_card->equiping_target) { + equip_card->effect_target_cards.erase(equip_card->equiping_target); + equip_card->equiping_target->effect_target_owner.erase(equip_card); + equip_card->unequip(); + equip_card->enable_field_effect(false); + return FALSE; + } + if(equip_card->current.location == LOCATION_SZONE) { + if(up && equip_card->is_position(POS_FACEDOWN)) + change_position(equip_card, 0, equip_player, POS_FACEUP, 0); + return FALSE; + } + equip_card->enable_field_effect(false); + move_to_field(equip_card, equip_player, equip_player, LOCATION_SZONE, (up || equip_card->is_position(POS_FACEUP)) ? POS_FACEUP : POS_FACEDOWN); + return FALSE; + } + case 1: { + equip_card->equip(target); + if(!(equip_card->data.type & TYPE_EQUIP)) { + uint32_t equip_card_type = core.units.begin()->value1; + effect* peffect = pduel->new_effect(); + peffect->owner = equip_card; + peffect->handler = equip_card; + peffect->type = EFFECT_TYPE_SINGLE; + if (equip_card_type & TYPE_TRAP) { + peffect->code = EFFECT_ADD_TYPE; + peffect->value = TYPE_EQUIP; + } else { + peffect->code = EFFECT_CHANGE_TYPE; + peffect->value = TYPE_EQUIP + TYPE_SPELL; + } + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = RESET_EVENT + 0x17e0000; + equip_card->add_effect(peffect); + } + equip_card->effect_target_cards.insert(target); + target->effect_target_owner.insert(equip_card); + if(!is_step) { + if(equip_card->is_position(POS_FACEUP)) + equip_card->enable_field_effect(true); + adjust_disable_check_list(); + card_set cset; + cset.insert(equip_card); + raise_single_event(target, &cset, EVENT_EQUIP, core.reason_effect, 0, core.reason_player, PLAYER_NONE, 0); + raise_event(cset, EVENT_EQUIP, core.reason_effect, 0, core.reason_player, PLAYER_NONE, 0); + core.hint_timing[target->current.controler] |= TIMING_EQUIP; + process_single_event(); + process_instant_event(); + return FALSE; + } else { + core.equiping_cards.insert(equip_card); + returns.ivalue[0] = TRUE; + return TRUE; + } + } + case 2: { + returns.ivalue[0] = TRUE; + return TRUE; + } + case 3: { + returns.ivalue[0] = FALSE; + return TRUE; + } + } + return TRUE; +} +int32_t field::summon(uint16_t step, uint8_t sumplayer, card* target, effect* proc, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone, uint32_t action_type) { + switch(step) { + case 0: { + if(!target->is_summonable_card()) + return TRUE; + if(check_unique_onfield(target, sumplayer, LOCATION_MZONE)) + return TRUE; + if(target->is_affected_by_effect(EFFECT_CANNOT_SUMMON)) + return TRUE; + if(target->current.location == LOCATION_MZONE) { + if(target->is_position(POS_FACEDOWN)) + return TRUE; + if(!ignore_count && (core.extra_summon[sumplayer] || !target->is_affected_by_effect(EFFECT_EXTRA_SUMMON_COUNT)) + && (core.summon_count[sumplayer] >= get_summon_count_limit(sumplayer))) + return TRUE; + if(!target->is_affected_by_effect(EFFECT_DUAL_SUMMONABLE)) + return TRUE; + if(target->is_affected_by_effect(EFFECT_DUAL_STATUS)) + return TRUE; + if(!is_player_can_summon(SUMMON_TYPE_DUAL, sumplayer, target, sumplayer)) + return TRUE; + } else { + effect_set eset; + int32_t res = target->filter_summon_procedure(sumplayer, &eset, ignore_count, min_tribute, zone); + if(proc) { + if(res < 0 || !target->check_summon_procedure(proc, sumplayer, ignore_count, min_tribute, zone)) + return TRUE; + } else { + if(res == -2) + return TRUE; + core.select_effects.clear(); + core.select_options.clear(); + if(res > 0) { + core.select_effects.push_back(nullptr); + core.select_options.push_back(1); + } + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + core.select_effects.push_back(eset[i]); + core.select_options.push_back(eset[i]->description); + } + if (core.select_options.empty()) + return TRUE; + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, sumplayer, 0); + } + } + if(core.summon_depth) + core.summon_cancelable = FALSE; + ++core.summon_depth; + target->material_cards.clear(); + return FALSE; + } + case 1: { + effect_set eset; + target->filter_effect(EFFECT_EXTRA_SUMMON_COUNT, &eset); + if(target->current.location == LOCATION_MZONE) { + core.units.begin()->step = 3; + if(!ignore_count && !core.extra_summon[sumplayer]) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + core.units.begin()->ptr1 = eset[i]; + return FALSE; + } + } + core.units.begin()->ptr1 = 0; + return FALSE; + } + if(!proc) { + proc = core.select_effects[returns.ivalue[0]]; + core.units.begin()->peffect = proc; + } + core.select_effects.clear(); + core.select_options.clear(); + if(ignore_count || core.summon_count[sumplayer] < get_summon_count_limit(sumplayer)) { + core.select_effects.push_back(nullptr); + core.select_options.push_back(1); + } + if(!ignore_count && !core.extra_summon[sumplayer]) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + new_zone &= zone; + if(proc) { + if(new_min_tribute < min_tribute) + new_min_tribute = min_tribute; + if(!target->is_summonable(proc, new_min_tribute, new_zone, releasable)) + continue; + } else { + int32_t rcount = target->get_summon_tribute_count(); + int32_t min = rcount & 0xffff; + int32_t max = (rcount >> 16) & 0xffff; + if(!is_player_can_summon(SUMMON_TYPE_ADVANCE, sumplayer, target, sumplayer)) + max = 0; + if(min < min_tribute) + min = min_tribute; + if(max < min) + continue; + if(min < new_min_tribute) + min = new_min_tribute; + if(!check_tribute(target, min, max, 0, target->current.controler, new_zone, releasable)) + continue; + } + core.select_effects.push_back(eset[i]); + core.select_options.push_back(eset[i]->description); + } + } + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, sumplayer, 0); + return FALSE; + } + case 2: { + effect* pextra = core.select_effects[returns.ivalue[0]]; + core.units.begin()->ptr1 = pextra; + int32_t releasable = 0xff00ff; + if(pextra) { + std::vector retval; + pextra->get_value(target, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if (min_tribute < new_min_tribute) + min_tribute = static_cast(new_min_tribute); + zone &= new_zone; + core.units.begin()->arg1 = sumplayer + (ignore_count << 8) + (min_tribute << 16) + (zone << 24); + core.units.begin()->value1 = releasable; + } + if(proc) { + core.units.begin()->step = 3; + return FALSE; + } + core.select_cards.clear(); + int32_t required = target->get_summon_tribute_count(); + int32_t min = required & 0xffff; + int32_t max = required >> 16; + if(min < min_tribute) { + min = min_tribute; + } + uint32_t adv = is_player_can_summon(SUMMON_TYPE_ADVANCE, sumplayer, target, sumplayer); + if(max == 0 || !adv) { + returns.bvalue[0] = 0; + core.units.begin()->step = 3; + } else { + core.release_cards.clear(); + core.release_cards_ex.clear(); + core.release_cards_ex_oneof.clear(); + int32_t rcount = get_summon_release_list(target, &core.release_cards, &core.release_cards_ex, &core.release_cards_ex_oneof, nullptr, 0, releasable); + if(rcount == 0) { + returns.bvalue[0] = 0; + core.units.begin()->step = 3; + } else { + int32_t ct = get_tofield_count(target, sumplayer, LOCATION_MZONE, sumplayer, LOCATION_REASON_TOFIELD, zone); + int32_t fcount = get_mzone_limit(sumplayer, sumplayer, LOCATION_REASON_TOFIELD); + if(min == 0 && ct > 0 && fcount > 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, sumplayer, 90); + core.units.begin()->value2 = max; + } else { + if(min < -fcount + 1) { + min = -fcount + 1; + } + select_tribute_cards(target, sumplayer, core.summon_cancelable, min, max, sumplayer, zone); + core.units.begin()->step = 3; + } + } + } + return FALSE; + } + case 3: { + if(returns.ivalue[0]) + returns.bvalue[0] = 0; + else { + int32_t max = core.units.begin()->value2; + select_tribute_cards(target, sumplayer, core.summon_cancelable, 1, max, sumplayer, zone); + } + return FALSE; + } + case 4: { + if(target->current.location == LOCATION_MZONE) + core.units.begin()->step = 8; + else if(proc) + core.units.begin()->step = 5; + else { + if(returns.ivalue[0] == -1) { + --core.summon_depth; + return TRUE; + } + if(returns.bvalue[0]) { + card_set* tributes = new card_set; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + tributes->insert(core.select_cards[returns.bvalue[i + 1]]); + core.units.begin()->peffect = (effect*)tributes; + } + } + effect_set eset; + target->filter_effect(EFFECT_SUMMON_COST, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, sumplayer, 0); + } + } + effect_set* peset = new effect_set; + *peset = std::move(eset); + core.units.begin()->ptr2 = peset; + } + return FALSE; + } + case 5: { + card_set* tributes = (card_set*)proc; + int32_t min = 0; + int32_t level = target->get_level(); + if(level < 5) + min = 0; + else if(level < 7) + min = 1; + else + min = 2; + if(tributes) + min -= (int32_t)tributes->size(); + if(min > 0) { + std::vector duplicate; + effect_set eset; + target->filter_effect(EFFECT_DECREASE_TRIBUTE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + for(effect_set::size_type i = 0; i < eset.size() && min > 0; ++i) { + if(!eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) || eset[i]->count_limit == 0 || !eset[i]->target) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + eset[i]->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + for(effect_set::size_type i = 0; i < eset.size() && min > 0; ++i) { + if(!eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) || eset[i]->count_limit == 0 || eset[i]->target) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + eset[i]->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + } + if(tributes) { + for(auto& pcard : *tributes) + pcard->current.reason_card = target; + target->set_material(tributes); + release(*tributes, 0, REASON_SUMMON | REASON_MATERIAL, sumplayer); + target->summon_info = SUMMON_TYPE_NORMAL | SUMMON_TYPE_ADVANCE | (LOCATION_HAND << 16); + delete tributes; + core.units.begin()->peffect = 0; + adjust_all(); + } else + target->summon_info = SUMMON_TYPE_NORMAL | (LOCATION_HAND << 16); + target->current.reason_effect = 0; + target->current.reason_player = sumplayer; + core.units.begin()->step = 6; + return FALSE; + } + case 6: { + target->summon_info = (proc->get_value(target) & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_NORMAL | (LOCATION_HAND << 16); + target->current.reason_effect = proc; + target->current.reason_player = sumplayer; + int32_t releasable = core.units.begin()->value1; + if(proc->operation) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + pduel->lua->add_param(min_tribute, PARAM_TYPE_INT); + pduel->lua->add_param(zone, PARAM_TYPE_INT); + pduel->lua->add_param(releasable, PARAM_TYPE_INT); + pduel->game_field->core.limit_extra_summon_zone = zone; + pduel->game_field->core.limit_extra_summon_releasable = releasable; + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, proc, 0, sumplayer, 0); + } + proc->dec_count(sumplayer); + return FALSE; + } + case 7: { + pduel->game_field->core.limit_extra_summon_zone = 0; + pduel->game_field->core.limit_extra_summon_releasable = 0; + --core.summon_depth; + if(core.summon_depth) + return TRUE; + break_effect(); + if(ignore_count) + return FALSE; + effect* pextra = (effect*)core.units.begin()->ptr1; + if(!pextra) + ++core.summon_count[sumplayer]; + else { + core.extra_summon[sumplayer] = TRUE; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(pextra->handler->data.code); + if(pextra->operation) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, pextra, 0, sumplayer, 0); + } + } + return FALSE; + } + case 8: { + uint8_t targetplayer = sumplayer; + uint8_t positions = POS_FACEUP_ATTACK; + if(is_player_affected_by_effect(sumplayer, EFFECT_DIVINE_LIGHT)) + positions = POS_FACEUP; + if(proc && proc->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + positions = (uint8_t)proc->s_range & POS_FACEUP; + if(proc->o_range) + targetplayer = 1 - sumplayer; + } + target->enable_field_effect(false); + move_to_field(target, sumplayer, targetplayer, LOCATION_MZONE, positions, FALSE, 0, FALSE, zone); + core.units.begin()->step = 10; + return FALSE; + } + case 9: { + --core.summon_depth; + if(core.summon_depth) + return TRUE; + target->enable_field_effect(false); + target->summon_info |= SUMMON_TYPE_NORMAL; + target->current.reason_effect = 0; + target->current.reason_player = sumplayer; + core.is_gemini_summoning = true; + effect* deffect = pduel->new_effect(); + deffect->owner = target; + deffect->code = EFFECT_DUAL_STATUS; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 64; + deffect->reset_flag = RESET_EVENT + 0x1fe0000; + target->add_effect(deffect); + return FALSE; + } + case 10: { + if(ignore_count) + return FALSE; + effect* pextra = (effect*)core.units.begin()->ptr1; + if(!pextra) + ++core.summon_count[sumplayer]; + else { + core.extra_summon[sumplayer] = TRUE; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(pextra->handler->data.code); + if(pextra->operation) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, pextra, 0, sumplayer, 0); + } + } + return FALSE; + } + case 11: { + set_control(target, target->current.controler, 0, 0); + core.phase_action = TRUE; + target->current.reason = REASON_SUMMON; + target->summon_player = sumplayer; + if (core.current_chain.size() == 0) { + if (core.is_gemini_summoning) { + target->set_status(STATUS_SUMMONING, FALSE); + target->set_status(STATUS_FLIP_SUMMONING, TRUE); + } + else { + target->set_status(STATUS_SUMMONING, TRUE); + target->set_status(STATUS_FLIP_SUMMONING, FALSE); + } + } + pduel->write_buffer8(MSG_SUMMONING); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + if (target->material_cards.size()) { + for (auto& mcard : target->material_cards) + raise_single_event(mcard, 0, EVENT_BE_PRE_MATERIAL, proc, REASON_SUMMON, sumplayer, sumplayer, 0); + raise_event(target->material_cards, EVENT_BE_PRE_MATERIAL, proc, REASON_SUMMON, sumplayer, sumplayer, 0); + } + process_single_event(); + process_instant_event(); + return FALSE; + } + case 12: { + if(core.current_chain.size() == 0) { + if(target->is_affected_by_effect(EFFECT_CANNOT_DISABLE_SUMMON)) + core.units.begin()->step = 14; + return FALSE; + } else { + core.units.begin()->step = 14; + return FALSE; + } + return FALSE; + } + case 13: { + target->set_status(STATUS_SUMMON_DISABLED, FALSE); + target->set_status(STATUS_FLIP_SUMMON_DISABLED, FALSE); + raise_event(target, EVENT_SUMMON, proc, 0, sumplayer, sumplayer, 0); + process_instant_event(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0x101, TRUE); + return FALSE; + } + case 14: { + if (core.is_gemini_summoning && target->is_status(STATUS_FLIP_SUMMONING) || !core.is_gemini_summoning && target->is_status(STATUS_SUMMONING)) { + core.units.begin()->step = 14; + return FALSE; + } + if (core.is_summon_negated) { + if (proc) { + remove_oath_effect(proc); + if (proc->is_flag(EFFECT_FLAG_COUNT_LIMIT) && (proc->count_code & EFFECT_COUNT_CODE_OATH)) { + dec_effect_code(proc->count_code, sumplayer); + } + } + if (effect_set* peset = (effect_set*)core.units.begin()->ptr2) { + for (effect_set::size_type i = 0; i < peset->size(); ++i) + remove_oath_effect(peset->at(i)); + delete peset; + core.units.begin()->ptr2 = 0; + } + if (target->get_status(STATUS_SUMMON_DISABLED | STATUS_FLIP_SUMMON_DISABLED) && target->current.location == LOCATION_MZONE) + send_to(target, 0, REASON_RULE, PLAYER_NONE, sumplayer, LOCATION_GRAVE, 0, 0); + adjust_instant(); + } + core.is_gemini_summoning = false; + core.is_summon_negated = false; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + return TRUE; + } + case 15: { + core.is_gemini_summoning = false; + if(proc) { + release_oath_relation(proc); + } + if(effect_set* peset = (effect_set*)core.units.begin()->ptr2) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + release_oath_relation(peset->at(i)); + delete peset; + core.units.begin()->ptr2 = 0; + } + target->set_status(STATUS_SUMMONING, FALSE); + target->set_status(STATUS_FLIP_SUMMONING, FALSE); + target->set_status(STATUS_SUMMON_TURN, TRUE); + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + target->enable_field_effect(true); + if(target->is_status(STATUS_DISABLED)) + target->reset(RESET_DISABLE, RESET_EVENT); + return FALSE; + } + case 16: { + pduel->write_buffer8(MSG_SUMMONED); + adjust_instant(); + if(target->material_cards.size()) { + for(auto& mcard : target->material_cards) + raise_single_event(mcard, 0, EVENT_BE_MATERIAL, proc, REASON_SUMMON, sumplayer, sumplayer, 0); + raise_event(target->material_cards, EVENT_BE_MATERIAL, proc, REASON_SUMMON, sumplayer, sumplayer, 0); + } + process_single_event(); + process_instant_event(); + return FALSE; + } + case 17: { + ++core.summon_state_count[sumplayer]; + ++core.normalsummon_state_count[sumplayer]; + check_card_counter(target, ACTIVITY_SUMMON, sumplayer); + check_card_counter(target, ACTIVITY_NORMALSUMMON, sumplayer); + raise_single_event(target, 0, EVENT_SUMMON_SUCCESS, proc, 0, sumplayer, sumplayer, 0); + process_single_event(); + raise_event(target, EVENT_SUMMON_SUCCESS, proc, 0, sumplayer, sumplayer, 0); + process_instant_event(); + if (action_type == SUMMON_IN_IDLE) { + adjust_all(); + core.hint_timing[sumplayer] |= TIMING_SUMMON; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } + return TRUE; + } + } + return TRUE; +} +int32_t field::flip_summon(uint16_t step, uint8_t sumplayer, card * target, uint32_t action_type) { + switch(step) { + case 0: { + if(target->current.location != LOCATION_MZONE) + return TRUE; + if(!(target->current.position & POS_FACEDOWN)) + return TRUE; + if(check_unique_onfield(target, sumplayer, LOCATION_MZONE)) + return TRUE; + effect_set eset; + target->filter_effect(EFFECT_FLIPSUMMON_COST, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, sumplayer, 0); + } + } + effect_set* peset = new effect_set; + *peset = std::move(eset); + core.units.begin()->ptr1 = peset; + } + return FALSE; + } + case 1: { + target->previous.position = target->current.position; + target->current.position = POS_FACEUP_ATTACK; + target->summon_player = sumplayer; + target->summon_info |= SUMMON_TYPE_FLIP; + target->fieldid = infos.field_id++; + target->activate_count_id = target->fieldid; + core.phase_action = TRUE; + pduel->write_buffer8(MSG_FLIPSUMMONING); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + if(target->is_affected_by_effect(EFFECT_CANNOT_DISABLE_FLIP_SUMMON)) + core.units.begin()->step = 2; + else { + target->set_status(STATUS_FLIP_SUMMONING, TRUE); + target->set_status(STATUS_FLIP_SUMMON_DISABLED, FALSE); + raise_event(target, EVENT_FLIP_SUMMON, 0, 0, sumplayer, sumplayer, 0); + process_instant_event(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0x101, TRUE); + } + return FALSE; + } + case 2: { + if(target->is_status(STATUS_FLIP_SUMMONING)) + return FALSE; + if (core.is_summon_negated) { + if (effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for (effect_set::size_type i = 0; i < peset->size(); ++i) + remove_oath_effect(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + if (target->is_status(STATUS_FLIP_SUMMON_DISABLED) && target->current.location == LOCATION_MZONE) + send_to(target, 0, REASON_RULE, PLAYER_NONE, sumplayer, LOCATION_GRAVE, 0, 0); + } + core.is_summon_negated = false; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + return TRUE; + } + case 3: { + if(effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + release_oath_relation(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + target->set_status(STATUS_FLIP_SUMMONING, FALSE); + target->enable_field_effect(true); + if(target->is_status(STATUS_DISABLED)) + target->reset(RESET_DISABLE, RESET_EVENT); + target->set_status(STATUS_FLIP_SUMMON_TURN, TRUE); + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + return FALSE; + } + case 4: { + pduel->write_buffer8(MSG_FLIPSUMMONED); + ++core.flipsummon_state_count[sumplayer]; + check_card_counter(target, ACTIVITY_FLIPSUMMON, sumplayer); + adjust_instant(); + raise_single_event(target, 0, EVENT_FLIP, 0, 0, sumplayer, sumplayer, 0); + raise_single_event(target, 0, EVENT_FLIP_SUMMON_SUCCESS, 0, 0, sumplayer, sumplayer, 0); + raise_single_event(target, 0, EVENT_CHANGE_POS, 0, 0, sumplayer, sumplayer, 0); + process_single_event(); + raise_event(target, EVENT_FLIP, 0, 0, sumplayer, sumplayer, 0); + raise_event(target, EVENT_FLIP_SUMMON_SUCCESS, 0, 0, sumplayer, sumplayer, 0); + raise_event(target, EVENT_CHANGE_POS, 0, 0, sumplayer, sumplayer, 0); + process_instant_event(); + adjust_all(); + if (action_type == SUMMON_IN_IDLE) { + core.hint_timing[sumplayer] |= TIMING_FLIPSUMMON; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } + return TRUE; + } + } + return TRUE; +} +int32_t field::mset(uint16_t step, uint8_t setplayer, card* target, effect* proc, uint8_t ignore_count, uint8_t min_tribute, uint32_t zone, uint32_t action_type) { + switch(step) { + case 0: { + if(!target->is_summonable_card()) + return TRUE; + if(target->current.location != LOCATION_HAND) + return TRUE; + if(!(target->data.type & TYPE_MONSTER)) + return TRUE; + if(target->is_affected_by_effect(EFFECT_CANNOT_MSET)) + return TRUE; + effect_set eset; + int32_t res = target->filter_set_procedure(setplayer, &eset, ignore_count, min_tribute, zone); + if(proc) { + if(res < 0 || !target->check_set_procedure(proc, setplayer, ignore_count, min_tribute, zone)) + return TRUE; + } else { + if(res == -2) + return TRUE; + core.select_effects.clear(); + core.select_options.clear(); + if(res > 0) { + core.select_effects.push_back(nullptr); + core.select_options.push_back(1); + } + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + core.select_effects.push_back(eset[i]); + core.select_options.push_back(eset[i]->description); + } + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, setplayer, 0); + } + target->material_cards.clear(); + return FALSE; + } + case 1: { + effect_set eset; + target->filter_effect(EFFECT_EXTRA_SET_COUNT, &eset); + if(!proc) { + proc = core.select_effects[returns.ivalue[0]]; + core.units.begin()->peffect = proc; + } + core.select_effects.clear(); + core.select_options.clear(); + if(ignore_count || core.summon_count[setplayer] < get_summon_count_limit(setplayer)) { + core.select_effects.push_back(nullptr); + core.select_options.push_back(1); + } + if(!ignore_count && !core.extra_summon[setplayer]) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + int32_t releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + new_zone &= zone; + if(proc) { + if(new_min_tribute < (int32_t)min_tribute) + new_min_tribute = min_tribute; + if(!target->is_summonable(proc, new_min_tribute, new_zone, releasable)) + continue; + } else { + int32_t rcount = target->get_set_tribute_count(); + int32_t min = rcount & 0xffff; + int32_t max = (rcount >> 16) & 0xffff; + if(!is_player_can_mset(SUMMON_TYPE_ADVANCE, setplayer, target, setplayer)) + max = 0; + if(min < (int32_t)min_tribute) + min = min_tribute; + if(max < min) + continue; + if(min < new_min_tribute) + min = new_min_tribute; + if(!check_tribute(target, min, max, 0, target->current.controler, new_zone, releasable, POS_FACEDOWN_DEFENSE)) + continue; + } + core.select_effects.push_back(eset[i]); + core.select_options.push_back(eset[i]->description); + } + } + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, setplayer, 0); + return FALSE; + } + case 2: { + effect* pextra = core.select_effects[returns.ivalue[0]]; + core.units.begin()->ptr1 = pextra; + int32_t releasable = 0xff00ff; + if(pextra) { + std::vector retval; + pextra->get_value(target, 0, retval); + int32_t new_min_tribute = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t new_zone = retval.size() > 1 ? static_cast(retval[1]) : 0x1f; + releasable = retval.size() > 2 ? (retval[2] < 0 ? 0xff00ff + static_cast(retval[2]) : static_cast(retval[2])) : 0xff00ff; + if(min_tribute < new_min_tribute) + min_tribute = static_cast(new_min_tribute); + zone &= new_zone; + core.units.begin()->arg1 = setplayer + (ignore_count << 8) + (min_tribute << 16) + (zone << 24); + } + if(proc) { + core.units.begin()->step = 3; + return FALSE; + } + core.select_cards.clear(); + int32_t required = target->get_set_tribute_count(); + int32_t min = required & 0xffff; + int32_t max = required >> 16; + if(min < min_tribute) { + min = min_tribute; + } + uint32_t adv = is_player_can_mset(SUMMON_TYPE_ADVANCE, setplayer, target, setplayer); + if(max == 0 || !adv) { + returns.bvalue[0] = 0; + core.units.begin()->step = 3; + } else { + core.release_cards.clear(); + core.release_cards_ex.clear(); + core.release_cards_ex_oneof.clear(); + int32_t rcount = get_summon_release_list(target, &core.release_cards, &core.release_cards_ex, &core.release_cards_ex_oneof, nullptr, 0, releasable, POS_FACEDOWN_DEFENSE); + if(rcount == 0) { + returns.bvalue[0] = 0; + core.units.begin()->step = 3; + } else { + int32_t ct = get_tofield_count(target, setplayer, LOCATION_MZONE, setplayer, LOCATION_REASON_TOFIELD, zone); + int32_t fcount = get_mzone_limit(setplayer, setplayer, LOCATION_REASON_TOFIELD); + if(min == 0 && ct > 0 && fcount > 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, setplayer, 90); + core.units.begin()->value1 = max; + } else { + if(min < -fcount + 1) { + min = -fcount + 1; + } + select_tribute_cards(target, setplayer, core.summon_cancelable, min, max, setplayer, zone); + core.units.begin()->step = 3; + } + } + } + return FALSE; + } + case 3: { + if(returns.ivalue[0]) + returns.bvalue[0] = 0; + else { + int32_t max = core.units.begin()->value1; + select_tribute_cards(target, setplayer, core.summon_cancelable, 1, max, setplayer, zone); + } + return FALSE; + } + case 4: { + if(proc) + core.units.begin()->step = 5; + else { + if(returns.ivalue[0] == -1) { + return TRUE; + } + if(returns.bvalue[0]) { + card_set* tributes = new card_set; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + tributes->insert(core.select_cards[returns.bvalue[i + 1]]); + core.units.begin()->peffect = (effect*)tributes; + } + } + effect_set eset; + target->filter_effect(EFFECT_MSET_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, setplayer, 0); + } + } + return FALSE; + } + case 5: { + card_set* tributes = (card_set*)proc; + int32_t min = 0; + int32_t level = target->get_level(); + if(level < 5) + min = 0; + else if(level < 7) + min = 1; + else + min = 2; + if(tributes) + min -= (int32_t)tributes->size(); + if(min > 0) { + std::vector duplicate; + effect_set eset; + target->filter_effect(EFFECT_DECREASE_TRIBUTE_SET, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT)) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + for(effect_set::size_type i = 0; i < eset.size() && min > 0; ++i) { + if(!eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) || eset[i]->count_limit == 0 || !eset[i]->target) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + eset[i]->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + for(effect_set::size_type i = 0; i < eset.size() && min > 0; ++i) { + if(!eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT) || eset[i]->count_limit == 0 || eset[i]->target) + continue; + std::vector retval; + eset[i]->get_value(target, 0, retval); + int32_t dec = retval.size() > 0 ? static_cast(retval[0]) : 0; + int32_t effect_code = retval.size() > 1 ? static_cast(retval[1]) : 0; + if(effect_code > 0) { + auto it = std::find(duplicate.begin(), duplicate.end(), effect_code); + if(it == duplicate.end()) + duplicate.push_back(effect_code); + else + continue; + } + min -= dec & 0xffff; + eset[i]->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->handler->data.code); + } + } + if(tributes) { + for(auto& pcard : *tributes) + pcard->current.reason_card = target; + target->set_material(tributes); + release(*tributes, 0, REASON_SUMMON | REASON_MATERIAL, setplayer); + target->summon_info = SUMMON_TYPE_NORMAL | SUMMON_TYPE_ADVANCE | (LOCATION_HAND << 16); + delete tributes; + core.units.begin()->peffect = 0; + adjust_all(); + } else + target->summon_info = SUMMON_TYPE_NORMAL | (LOCATION_HAND << 16); + target->summon_player = setplayer; + target->current.reason_effect = 0; + target->current.reason_player = setplayer; + core.units.begin()->step = 6; + return FALSE; + } + case 6: { + target->summon_info = (proc->get_value(target) & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_NORMAL | (LOCATION_HAND << 16); + target->summon_player = setplayer; + target->current.reason_effect = proc; + target->current.reason_player = setplayer; + pduel->lua->add_param(target, PARAM_TYPE_CARD); + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, proc, 0, setplayer, 0); + proc->dec_count(setplayer); + return FALSE; + } + case 7: { + break_effect(); + if(ignore_count) + return FALSE; + effect* pextra = (effect*)core.units.begin()->ptr1; + if(!pextra) + ++core.summon_count[setplayer]; + else { + core.extra_summon[setplayer] = TRUE; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(pextra->handler->data.code); + if(pextra->operation) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, pextra, 0, setplayer, 0); + } + } + return FALSE; + } + case 8: { + uint8_t targetplayer = setplayer; + uint8_t positions = POS_FACEDOWN_DEFENSE; + if(proc && proc->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + positions = (uint8_t)proc->s_range & POS_FACEDOWN; + if(proc->o_range) + targetplayer = 1 - setplayer; + } + target->enable_field_effect(false); + move_to_field(target, setplayer, targetplayer, LOCATION_MZONE, positions, FALSE, 0, FALSE, zone); + return FALSE; + } + case 9: { + set_control(target, target->current.controler, 0, 0); + core.phase_action = TRUE; + ++core.normalsummon_state_count[setplayer]; + check_card_counter(target, ACTIVITY_NORMALSUMMON, setplayer); + target->set_status(STATUS_SUMMON_TURN, TRUE); + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + pduel->write_buffer8(MSG_SET); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + adjust_instant(); + raise_event(target, EVENT_MSET, proc, 0, setplayer, setplayer, 0); + process_instant_event(); + if(action_type == SUMMON_IN_IDLE) { + adjust_all(); + core.hint_timing[setplayer] |= TIMING_MSET; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, FALSE); + } + return TRUE; + } + } + return TRUE; +} +int32_t field::sset(uint16_t step, uint8_t setplayer, uint8_t toplayer, card * target, effect* reason_effect, uint32_t zone) { + switch(step) { + case 0: { + if(!(target->data.type & TYPE_FIELD) && get_useable_count(target, toplayer, LOCATION_SZONE, setplayer, LOCATION_REASON_TOFIELD, zone ? zone : 0xff) <= 0) + return TRUE; + if(target->data.type & TYPE_MONSTER && !target->is_affected_by_effect(EFFECT_MONSTER_SSET)) + return TRUE; + if(target->current.location == LOCATION_SZONE && target->current.controler == toplayer) + return TRUE; + if(!is_player_can_sset(setplayer, target)) + return TRUE; + if(target->is_affected_by_effect(EFFECT_CANNOT_SSET)) + return TRUE; + uint32_t flag = 0; + if(target->data.type & TYPE_FIELD) { + flag = ~(0x1U << 13); + } else { + get_useable_count(target, setplayer, LOCATION_SZONE, toplayer, LOCATION_REASON_TOFIELD, zone ? zone : 0xff, &flag); + flag = ((flag & 0xff) << 8) | 0xffff00ff; + flag |= 0xe080e080; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(setplayer); + pduel->write_buffer32(target->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, setplayer, flag, 0); + return FALSE; + } + case 1: { + if(returns.bvalue[1] == 0) { + return TRUE; + } + target->to_field_param = returns.bvalue[2]; + effect_set eset; + target->filter_effect(EFFECT_SSET_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, setplayer, 0); + } + } + return FALSE; + } + case 2: { + target->enable_field_effect(false); + move_to_field(target, setplayer, toplayer, LOCATION_SZONE, POS_FACEDOWN, FALSE, 0, FALSE, 0x1U << target->to_field_param); + return FALSE; + } + case 3: { + core.phase_action = TRUE; + target->set_status(STATUS_SET_TURN, TRUE); + if(target->data.type & TYPE_MONSTER) { + effect* peffect = target->is_affected_by_effect(EFFECT_MONSTER_SSET); + int32_t type_val = peffect->get_value(); + peffect = pduel->new_effect(); + peffect->owner = target; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_CHANGE_TYPE; + peffect->reset_flag = RESET_EVENT + 0x1fe0000; + peffect->value = type_val; + target->add_effect(peffect); + } + pduel->write_buffer8(MSG_SET); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + adjust_instant(); + raise_event(target, EVENT_SSET, reason_effect, 0, setplayer, setplayer, 0); + process_instant_event(); + if(core.current_chain.size() == 0) { + adjust_all(); + core.hint_timing[setplayer] |= TIMING_SSET; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, FALSE); + } + } + } + return TRUE; +} +int32_t field::sset_g(uint16_t step, uint8_t setplayer, uint8_t toplayer, group* ptarget, uint8_t confirm, effect* reason_effect, uint32_t zone) { + switch(step) { + case 0: { + card_set* set_cards = new card_set; + core.operated_set.clear(); + core.set_group_set.clear(); + for(auto& target : ptarget->container) { + if((target->data.type & TYPE_MONSTER && !target->is_affected_by_effect(EFFECT_MONSTER_SSET)) + || (target->current.location == LOCATION_SZONE) + || (!is_player_can_sset(setplayer, target)) + || (target->is_affected_by_effect(EFFECT_CANNOT_SSET))) { + continue; + } + if(!(target->data.type & TYPE_FIELD) && get_useable_count(target, toplayer, LOCATION_SZONE, setplayer, LOCATION_REASON_TOFIELD, zone ? zone : 0xff) <= 0) { + if(target->current.location != LOCATION_GRAVE) + core.unable_tofield_set.insert(target); + continue; + } + set_cards->insert(target); + } + if(set_cards->empty()) { + delete set_cards; + core.units.begin()->step = 4; + return FALSE; + } + effect_set eset; + for(auto& pcard : *set_cards) { + eset.clear(); + pcard->filter_effect(EFFECT_SSET_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, setplayer, 0); + } + } + } + core.set_group_pre_set.clear(); + core.set_group_used_zones = 0; + core.phase_action = TRUE; + core.units.begin()->ptarget = (group*)set_cards; + return FALSE; + } + case 1: { + card_set* set_cards = (card_set*)ptarget; + card* target = *set_cards->begin(); + if(target->data.type & TYPE_FIELD) { + returns.bvalue[2] = 5; + return FALSE; + } + uint32_t flag; + get_useable_count(target, toplayer, LOCATION_SZONE, setplayer, LOCATION_REASON_TOFIELD, zone ? zone : 0xff, &flag); + flag |= core.set_group_used_zones; + if(setplayer == toplayer) { + flag = ((flag & 0xff) << 8) | 0xffff00ff; + } else { + flag = ((flag & 0xff) << 24) | 0xffffff; + } + flag |= 0xe080e080; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(setplayer); + pduel->write_buffer32(target->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, setplayer, flag, 1); + return FALSE; + } + case 2: { + card_set* set_cards = (card_set*)ptarget; + card* target = *set_cards->begin(); + uint32_t seq = returns.bvalue[2]; + core.set_group_seq[core.set_group_pre_set.size()] = seq; + core.set_group_pre_set.insert(target); + core.set_group_used_zones |= (0x1U << seq); + set_cards->erase(target); + if(!set_cards->empty()) + core.units.begin()->step = 0; + else + delete set_cards; + return FALSE; + } + case 3: { + card_set* set_cards = &core.set_group_pre_set; + card* target = *set_cards->begin(); + target->enable_field_effect(false); + uint32_t zone; + if(target->data.type & TYPE_FIELD) { + zone = 0x1U << 5; + } else { + for(int32_t i = 0; i < 7; i++) { + zone = 0x1U << i; + if(core.set_group_used_zones & zone) { + core.set_group_used_zones &= ~zone; + break; + } + } + } + move_to_field(target, setplayer, toplayer, LOCATION_SZONE, POS_FACEDOWN, FALSE, 0, FALSE, zone); + return FALSE; + } + case 4: { + card_set* set_cards = &core.set_group_pre_set; + card* target = *set_cards->begin(); + target->set_status(STATUS_SET_TURN, TRUE); + if(target->data.type & TYPE_MONSTER) { + effect* peffect = target->is_affected_by_effect(EFFECT_MONSTER_SSET); + int32_t type_val = peffect->get_value(); + peffect = pduel->new_effect(); + peffect->owner = target; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_CHANGE_TYPE; + peffect->reset_flag = RESET_EVENT + 0x1fe0000; + peffect->value = type_val; + target->add_effect(peffect); + } + pduel->write_buffer8(MSG_SET); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + core.set_group_set.insert(target); + set_cards->erase(target); + if(!set_cards->empty()) + core.units.begin()->step = 2; + return FALSE; + } + case 5: { + if(core.unable_tofield_set.size()) + send_to(core.unable_tofield_set, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + case 6: { + core.unable_tofield_set.clear(); + if(core.set_group_set.size() == 0) { + returns.ivalue[0] = 0; + return TRUE; + } + if(confirm) { + pduel->write_buffer8(MSG_CONFIRM_CARDS); + pduel->write_buffer8(toplayer); + pduel->write_buffer8(0); + pduel->write_buffer8((uint8_t)core.set_group_set.size()); + for(auto& pcard : core.set_group_set) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + } + return FALSE; + } + case 7: { + core.operated_set.clear(); + for(auto& pcard : core.set_group_set) { + core.operated_set.insert(pcard); + } + uint8_t ct = (uint8_t)core.operated_set.size(); + if(core.set_group_used_zones & (0x1U << 5)) + --ct; + if(ct <= 1) + return FALSE; + pduel->write_buffer8(MSG_SHUFFLE_SET_CARD); + pduel->write_buffer8(LOCATION_SZONE); + pduel->write_buffer8(ct); + uint8_t index = 0; + for(auto cit = core.operated_set.begin(); cit != core.operated_set.end(); ++cit) { + card* pcard = *cit; + uint8_t seq = core.set_group_seq[index]; + ++index; + if(pcard->data.type & TYPE_FIELD) + continue; + pduel->write_buffer32(pcard->get_info_location()); + pduel->game_field->player[toplayer].list_szone[seq] = pcard; + pcard->current.sequence = seq; + } + for(uint32_t i = 0; i < ct; ++i) { + pduel->write_buffer32(0); + } + return FALSE; + } + case 8: { + adjust_instant(); + raise_event(core.operated_set, EVENT_SSET, reason_effect, 0, setplayer, setplayer, 0); + process_instant_event(); + if(core.current_chain.size() == 0) { + adjust_all(); + core.hint_timing[setplayer] |= TIMING_SSET; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, FALSE); + } + return FALSE; + } + case 9: { + returns.ivalue[0] = (int32_t)core.operated_set.size(); + return TRUE; + } + } + return TRUE; +} +int32_t field::special_summon_rule(uint16_t step, uint8_t sumplayer, card* target, uint32_t summon_type, uint32_t action_type) { + switch(step) { + case 0: { + effect_set eset; + material_info info; + info.limit_tuner = core.limit_tuner; + info.limit_syn = core.limit_syn; + info.limit_syn_minc = core.limit_syn_minc; + info.limit_syn_maxc = core.limit_syn_maxc; + info.limit_xyz = core.limit_xyz; + info.limit_xyz_minc = core.limit_xyz_minc; + info.limit_xyz_maxc = core.limit_xyz_maxc; + info.limit_link = core.limit_link; + info.limit_link_card = core.limit_link_card; + info.limit_link_minc = core.limit_link_minc; + info.limit_link_maxc = core.limit_link_maxc; + target->filter_spsummon_procedure(sumplayer, &eset, summon_type, info); + target->filter_spsummon_procedure_g(sumplayer, &eset); + if(!eset.size()) { + core.units.begin()->step = 17; + return FALSE; + } + core.select_effects.clear(); + core.select_options.clear(); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + core.select_effects.push_back(eset[i]); + core.select_options.push_back(eset[i]->description); + } + if(core.select_options.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, sumplayer, 0); + return FALSE; + } + case 1: { + effect* peffect = core.select_effects[returns.ivalue[0]]; + core.units.begin()->peffect = peffect; + if(peffect->code == EFFECT_SPSUMMON_PROC_G) { + core.units.begin()->step = 19; + return FALSE; + } + returns.ivalue[0] = TRUE; + if(peffect->target) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + if(core.limit_tuner || core.limit_syn) { + pduel->lua->add_param(core.limit_tuner, PARAM_TYPE_CARD); + pduel->lua->add_param(core.limit_syn, PARAM_TYPE_GROUP); + if(core.limit_syn_minc) { + pduel->lua->add_param(core.limit_syn_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_syn_maxc, PARAM_TYPE_INT); + } + } else if(core.limit_xyz) { + pduel->lua->add_param(core.limit_xyz, PARAM_TYPE_GROUP); + if(core.limit_xyz_minc) { + pduel->lua->add_param(core.limit_xyz_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_xyz_maxc, PARAM_TYPE_INT); + } + } else if(core.limit_link || core.limit_link_card) { + pduel->lua->add_param(core.limit_link, PARAM_TYPE_GROUP); + pduel->lua->add_param(core.limit_link_card, PARAM_TYPE_CARD); + if(core.limit_link_minc) { + pduel->lua->add_param(core.limit_link_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_link_maxc, PARAM_TYPE_INT); + } + } + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_TARGET, 0, peffect, 0, sumplayer, 0); + } + return FALSE; + } + case 2: { + if(!returns.ivalue[0]) { + core.units.begin()->step = 17; + return FALSE; + } + effect_set eset; + target->filter_effect(EFFECT_SPSUMMON_COST, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, sumplayer, 0); + } + } + effect_set* peset = new effect_set; + *peset = std::move(eset); + core.units.begin()->ptr1 = peset; + } + return FALSE; + } + case 3: { + effect* peffect = core.units.begin()->peffect; + target->material_cards.clear(); + if(peffect->operation) { + pduel->lua->add_param(target, PARAM_TYPE_CARD); + if(core.limit_tuner || core.limit_syn) { + pduel->lua->add_param(core.limit_tuner, PARAM_TYPE_CARD); + pduel->lua->add_param(core.limit_syn, PARAM_TYPE_GROUP); + core.limit_tuner = 0; + if(core.limit_syn_minc) { + pduel->lua->add_param(core.limit_syn_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_syn_maxc, PARAM_TYPE_INT); + core.limit_syn_minc = 0; + core.limit_syn_maxc = 0; + } + } + if(core.limit_xyz) { + pduel->lua->add_param(core.limit_xyz, PARAM_TYPE_GROUP); + if(core.limit_xyz_minc) { + pduel->lua->add_param(core.limit_xyz_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_xyz_maxc, PARAM_TYPE_INT); + core.limit_xyz_minc = 0; + core.limit_xyz_maxc = 0; + } + } + if(core.limit_link || core.limit_link_card) { + pduel->lua->add_param(core.limit_link, PARAM_TYPE_GROUP); + pduel->lua->add_param(core.limit_link_card, PARAM_TYPE_CARD); + core.limit_link_card = 0; + if(core.limit_link_minc) { + pduel->lua->add_param(core.limit_link_minc, PARAM_TYPE_INT); + pduel->lua->add_param(core.limit_link_maxc, PARAM_TYPE_INT); + core.limit_link_minc = 0; + core.limit_link_maxc = 0; + } + } + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, peffect, 0, sumplayer, 0); + } + peffect->dec_count(sumplayer); + return FALSE; + } + case 4: { + if(core.limit_syn) { + pduel->delete_group(core.limit_syn); + core.limit_syn = 0; + } + if(core.limit_xyz) { + pduel->delete_group(core.limit_xyz); + core.limit_xyz = 0; + } + if(core.limit_link) { + pduel->delete_group(core.limit_link); + core.limit_link = 0; + } + effect* peffect = core.units.begin()->peffect; + uint8_t targetplayer = sumplayer; + uint8_t positions = POS_FACEUP; + if(peffect->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + positions = (uint8_t)peffect->s_range; + if(peffect->o_range == 0) + targetplayer = sumplayer; + else + targetplayer = 1 - sumplayer; + } + std::vector retval; + peffect->get_value(target, 0, retval); + uint32_t summon_info = retval.size() > 0 ? static_cast(retval[0]) : 0; + positions = target->get_spsummonable_position(peffect, ((summon_info & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_SPECIAL), positions, sumplayer, targetplayer); + if(positions == 0) + positions = POS_FACEUP_ATTACK; + uint32_t zone = retval.size() > 1 ? static_cast(retval[1]) : 0xff; + target->summon_info = (summon_info & (SUMMON_VALUE_SUB_TYPE | SUMMON_VALUE_CUSTOM_TYPE)) | SUMMON_TYPE_SPECIAL | ((uint32_t)target->current.location << 16); + target->enable_field_effect(false); + move_to_field(target, sumplayer, targetplayer, LOCATION_MZONE, positions, FALSE, 0, FALSE, zone); + target->current.reason = REASON_SPSUMMON; + target->current.reason_effect = peffect; + target->current.reason_player = sumplayer; + target->summon_player = sumplayer; + break_effect(); + return FALSE; + } + case 5: { + set_control(target, target->current.controler, 0, 0); + core.phase_action = TRUE; + target->current.reason_effect = core.units.begin()->peffect; + target->set_status(STATUS_SUMMONING, TRUE); + pduel->write_buffer8(MSG_SPSUMMONING); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_public_info_location()); + return FALSE; + } + case 6: { + effect* proc = core.units.begin()->peffect; + int32_t matreason = REASON_SPSUMMON; + if(proc->value == SUMMON_TYPE_SYNCHRO) + matreason = REASON_SYNCHRO; + else if(proc->value == SUMMON_TYPE_XYZ) + { + matreason = REASON_XYZ; + rose_card = 0; + rose_level = 0; + } + else if(proc->value == SUMMON_TYPE_LINK) + matreason = REASON_LINK; + if (target->material_cards.size()) { + for (auto& mcard : target->material_cards) + raise_single_event(mcard, 0, EVENT_BE_PRE_MATERIAL, proc, matreason, sumplayer, sumplayer, 0); + } + raise_event(target->material_cards, EVENT_BE_PRE_MATERIAL, proc, matreason, sumplayer, sumplayer, 0); + process_single_event(); + process_instant_event(); + return FALSE; + } + case 7: { + if (core.current_chain.size() == 0) { + if (target->is_affected_by_effect(EFFECT_CANNOT_DISABLE_SPSUMMON)) + core.units.begin()->step = 14; + else + core.units.begin()->step = 9; + return FALSE; + } else { + core.units.begin()->step = 14; + return FALSE; + } + return FALSE; + } + case 10: { + target->set_status(STATUS_SUMMON_DISABLED, FALSE); + raise_event(target, EVENT_SPSUMMON, core.units.begin()->peffect, 0, sumplayer, sumplayer, 0); + process_instant_event(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0x101, TRUE); + return FALSE; + } + case 11: { + if(target->is_status(STATUS_SUMMONING)) { + core.units.begin()->step = 14; + return FALSE; + } + effect* peffect = core.units.begin()->peffect; + remove_oath_effect(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && (peffect->count_code & EFFECT_COUNT_CODE_OATH)) { + dec_effect_code(peffect->count_code, sumplayer); + } + if(effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + remove_oath_effect(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + if (target->is_status(STATUS_SUMMON_DISABLED) && target->current.location == LOCATION_MZONE) + send_to(target, 0, REASON_RULE, PLAYER_NONE, sumplayer, LOCATION_GRAVE, 0, 0); + adjust_instant(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + return TRUE; + } + case 15: { + release_oath_relation(core.units.begin()->peffect); + if(effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + release_oath_relation(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + target->set_status(STATUS_SUMMONING, FALSE); + target->set_status(STATUS_PROC_COMPLETE | STATUS_SPSUMMON_TURN, TRUE); + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + target->enable_field_effect(true); + if(target->is_status(STATUS_DISABLED)) + target->reset(RESET_DISABLE, RESET_EVENT); + return FALSE; + } + case 16: { + pduel->write_buffer8(MSG_SPSUMMONED); + adjust_instant(); + effect* proc = core.units.begin()->peffect; + int32_t matreason = REASON_SPSUMMON; + if(proc->value == SUMMON_TYPE_SYNCHRO) + matreason = REASON_SYNCHRO; + else if(proc->value == SUMMON_TYPE_XYZ) + matreason = REASON_XYZ; + else if(proc->value == SUMMON_TYPE_LINK) + matreason = REASON_LINK; + if(target->material_cards.size()) { + for(auto& mcard : target->material_cards) + raise_single_event(mcard, 0, EVENT_BE_MATERIAL, proc, matreason, sumplayer, sumplayer, 0); + } + raise_event(target->material_cards, EVENT_BE_MATERIAL, proc, matreason, sumplayer, sumplayer, 0); + process_single_event(); + process_instant_event(); + return FALSE; + } + case 17: { + effect* proc = core.units.begin()->peffect; + set_spsummon_counter(sumplayer); + check_card_counter(target, ACTIVITY_SPSUMMON, sumplayer); + if(target->spsummon_code) + ++core.spsummon_once_map[sumplayer][target->spsummon_code]; + target->set_special_summon_status(proc); + raise_single_event(target, 0, EVENT_SPSUMMON_SUCCESS, proc, 0, sumplayer, sumplayer, 0); + process_single_event(); + raise_event(target, EVENT_SPSUMMON_SUCCESS, proc, 0, sumplayer, sumplayer, 0); + process_instant_event(); + if(action_type == SUMMON_IN_IDLE) { + adjust_all(); + core.hint_timing[sumplayer] |= TIMING_SPSUMMON; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } + return TRUE; + } + case 18: { + if(core.limit_tuner) { + core.limit_tuner = 0; + } + if(core.limit_syn) { + pduel->delete_group(core.limit_syn); + core.limit_syn = 0; + } + if(core.limit_xyz) { + pduel->delete_group(core.limit_xyz); + core.limit_xyz = 0; + } + if(core.limit_link_card) { + core.limit_link_card = 0; + } + if(core.limit_link) { + pduel->delete_group(core.limit_link); + core.limit_link = 0; + } + return TRUE; + } + case 20: { + // EFFECT_SPSUMMON_PROC_G (Pendulum Summon) + effect* peffect = core.units.begin()->peffect; + core.units.begin()->ptarget = pduel->new_group(); + if(peffect->operation) { + core.sub_solving_event.push_back(nil_event); + pduel->lua->add_param(target, PARAM_TYPE_CARD); + pduel->lua->add_param(core.units.begin()->ptarget, PARAM_TYPE_GROUP); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, peffect, 0, sumplayer, 0); + } + peffect->dec_count(sumplayer); + return FALSE; + } + case 21: { + group* pgroup = core.units.begin()->ptarget; + for(auto cit = pgroup->container.begin(); cit != pgroup->container.end(); ) { + card* pcard = *cit; + if(!(pcard->data.type & TYPE_MONSTER) + || (pcard->current.location == LOCATION_MZONE) + || check_unique_onfield(pcard, sumplayer, LOCATION_MZONE) + || pcard->is_affected_by_effect(EFFECT_CANNOT_SPECIAL_SUMMON)) { + cit = pgroup->container.erase(cit); + continue; + } + effect_set eset; + pcard->filter_effect(EFFECT_SPSUMMON_COST, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, sumplayer, 0); + } + } + effect_set* peset = new effect_set; + *peset = std::move(eset); + core.units.begin()->ptr1 = peset; + } + ++cit; + } + return FALSE; + } + case 22: { + group* pgroup = core.units.begin()->ptarget; + if(pgroup->container.size() == 0) + return TRUE; + core.phase_action = TRUE; + pgroup->it = pgroup->container.begin(); + return FALSE; + } + case 23: { + effect* peffect = core.units.begin()->peffect; + group* pgroup = core.units.begin()->ptarget; + if (pgroup->it == pgroup->container.end()) { + core.units.begin()->step = 24; + return FALSE; + } + card* pcard = *pgroup->it; + pcard->enable_field_effect(false); + pcard->current.reason = REASON_SPSUMMON; + pcard->current.reason_effect = peffect; + pcard->current.reason_player = sumplayer; + pcard->summon_player = sumplayer; + std::vector retval; + peffect->get_value(target, 0, retval); + // pcard->summon_info = retval.size() > 0 ? static_cast(retval[0]) : 0; + uint32_t zone = retval.size() > 1 ? static_cast(retval[1]) : 0xff; + pcard->summon_info = (peffect->get_value(pcard) & DEFAULT_SUMMON_TYPE) | SUMMON_TYPE_SPECIAL | ((uint32_t)pcard->current.location << 16); + uint32_t flag1, flag2; + int32_t ct1 = get_tofield_count(pcard, sumplayer, LOCATION_MZONE, sumplayer, LOCATION_REASON_TOFIELD, zone, &flag1); + int32_t ct2 = get_spsummonable_count_fromex(pcard, sumplayer, sumplayer, zone, &flag2); + for(auto it = pgroup->it; it != pgroup->container.end(); ++it) { + if((*it)->current.location != LOCATION_EXTRA) + --ct1; + else + --ct2; + } + if(pcard->current.location != LOCATION_EXTRA) { + if(ct2 == 0) + zone = flag2; + } else { + if(ct1 == 0) + zone = flag1; + } + uint8_t positions = pcard->get_spsummonable_position(peffect, ((peffect->get_value(pcard) & 0xff00ffff) | SUMMON_TYPE_SPECIAL), POS_FACEUP, sumplayer, sumplayer); + move_to_field(pcard, sumplayer, sumplayer, LOCATION_MZONE, positions, FALSE, 0, FALSE, zone); + return FALSE; + } + case 24: { + group* pgroup = core.units.begin()->ptarget; + if (pgroup->it == pgroup->container.end()) { + return FALSE; + } + card* pcard = *pgroup->it; + ++pgroup->it; + set_control(pcard, pcard->current.controler, 0, 0); + pcard->set_status(STATUS_SUMMONING, TRUE); + pduel->write_buffer8(MSG_SPSUMMONING); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_public_info_location()); + if(pgroup->it != pgroup->container.end()) + core.units.begin()->step = 22; + return FALSE; + } + case 25: { + group* pgroup = core.units.begin()->ptarget; + card_set cset; + for(auto& pcard : pgroup->container) { + if(!pcard->is_affected_by_effect(EFFECT_CANNOT_DISABLE_SPSUMMON)) { + cset.insert(pcard); + } + } + if(cset.size()) { + raise_event(cset, EVENT_SPSUMMON, core.units.begin()->peffect, 0, sumplayer, sumplayer, 0); + process_instant_event(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0x101, TRUE); + } + return FALSE; + } + case 26: { + group* pgroup = core.units.begin()->ptarget; + card_set cset; + for(auto cit = pgroup->container.begin(); cit != pgroup->container.end(); ) { + card* pcard = *cit; + if (!pcard->is_status(STATUS_SUMMONING)) { + cit = pgroup->container.erase(cit); + if (pcard->is_status(STATUS_SUMMON_DISABLED) && pcard->current.location == LOCATION_MZONE) + cset.insert(pcard); + } + else + ++cit; + } + if(cset.size()) { + send_to(cset, 0, REASON_RULE, PLAYER_NONE, sumplayer, LOCATION_GRAVE, 0, 0); + adjust_instant(); + } + if(pgroup->container.size() == 0) { + effect* peffect = core.units.begin()->peffect; + remove_oath_effect(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && (peffect->count_code & EFFECT_COUNT_CODE_OATH)) { + dec_effect_code(peffect->count_code, sumplayer); + } + if(effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + remove_oath_effect(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + return TRUE; + } + return FALSE; + } + case 27: { + group* pgroup = core.units.begin()->ptarget; + release_oath_relation(core.units.begin()->peffect); + if(effect_set* peset = (effect_set*)core.units.begin()->ptr1) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + release_oath_relation(peset->at(i)); + delete peset; + core.units.begin()->ptr1 = 0; + } + for(auto& pcard : pgroup->container) { + pcard->set_status(STATUS_SUMMONING, FALSE); + pcard->set_status(STATUS_SPSUMMON_TURN, TRUE); + pcard->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + pcard->enable_field_effect(true); + if(pcard->is_status(STATUS_DISABLED)) + pcard->reset(RESET_DISABLE, RESET_EVENT); + } + return FALSE; + } + case 28: { + group * pgroup = core.units.begin()->ptarget; + raise_event(pgroup->container, EVENT_SPSUMMON_SUCCESS_G_P, core.units.begin()->peffect, 0, sumplayer, sumplayer, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 29: { + effect* peffect = core.units.begin()->peffect; + group* pgroup = core.units.begin()->ptarget; + pduel->write_buffer8(MSG_SPSUMMONED); + set_spsummon_counter(sumplayer); + check_card_counter(pgroup, ACTIVITY_SPSUMMON, sumplayer); + std::set spsummon_once_set; + for(auto& pcard : pgroup->container) { + if(pcard->spsummon_code) + spsummon_once_set.insert(pcard->spsummon_code); + pcard->set_special_summon_status(peffect); + } + for(auto& cit : spsummon_once_set) + ++core.spsummon_once_map[sumplayer][cit]; + for(auto& pcard : pgroup->container) + raise_single_event(pcard, 0, EVENT_SPSUMMON_SUCCESS, peffect, 0, sumplayer, pcard->summon_player, 0); + process_single_event(); + raise_event(pgroup->container, EVENT_SPSUMMON_SUCCESS, peffect, 0, sumplayer, sumplayer, 0); + process_instant_event(); + if(action_type == SUMMON_IN_IDLE) { + adjust_all(); + core.hint_timing[sumplayer] |= TIMING_SPSUMMON; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } + return TRUE; + } + } + return TRUE; +} +int32_t field::special_summon_step(uint16_t step, group* targets, card* target, uint32_t zone) { + uint8_t playerid = (target->spsummon_param >> 24) & 0xf; + uint8_t nocheck = (target->spsummon_param >> 16) & 0xff; + uint8_t nolimit = (target->spsummon_param >> 8) & 0xff; + uint8_t positions = target->spsummon_param & 0xff; + switch(step) { + case 0: { + effect_set eset; + if(target->is_affected_by_effect(EFFECT_REVIVE_LIMIT) && !target->is_status(STATUS_PROC_COMPLETE)) { + if((!nolimit && (target->current.location & 0x38)) || (!nocheck && !nolimit && (target->current.location & 0x3))) { + core.units.begin()->step = 4; + return FALSE; + } + } + if((target->current.location == LOCATION_MZONE) + || !(positions & POS_FACEDOWN) && check_unique_onfield(target, playerid, LOCATION_MZONE) + || !is_player_can_spsummon(core.reason_effect, target->summon_info & DEFAULT_SUMMON_TYPE, positions, target->summon_player, playerid, target) + || (!nocheck && !(target->data.type & TYPE_MONSTER))) { + core.units.begin()->step = 4; + return FALSE; + } + if(get_useable_count(target, playerid, LOCATION_MZONE, target->summon_player, LOCATION_REASON_TOFIELD, zone) <= 0) { + if(target->current.location != LOCATION_GRAVE) + core.unable_tofield_set.insert(target); + core.units.begin()->step = 4; + return FALSE; + } + if(!nocheck) { + target->filter_effect(EFFECT_SPSUMMON_CONDITION, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(core.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(target->summon_player, PARAM_TYPE_INT); + pduel->lua->add_param(target->summon_info & DEFAULT_SUMMON_TYPE, PARAM_TYPE_INT); + pduel->lua->add_param(positions, PARAM_TYPE_INT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + if(!eset[i]->check_value_condition(5)) { + core.units.begin()->step = 4; + return FALSE; + } + } + } + eset.clear(); + target->filter_effect(EFFECT_SPSUMMON_COST, &eset); + if(eset.size()) { + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, target->summon_player, 0); + } + } + effect_set* peset = new effect_set; + *peset = std::move(eset); + core.units.begin()->ptr2 = peset; + } + return FALSE; + } + case 1: { + if(effect_set* peset = (effect_set*)core.units.begin()->ptr2) { + for(effect_set::size_type i = 0; i < peset->size(); ++i) + release_oath_relation(peset->at(i)); + delete peset; + core.units.begin()->ptr2 = 0; + } + if(!targets) + core.special_summoning.insert(target); + target->enable_field_effect(false); + if(targets && core.duel_rule >= NEW_MASTER_RULE) { + uint32_t flag1, flag2; + int32_t ct1 = get_tofield_count(target, playerid, LOCATION_MZONE, target->summon_player, LOCATION_REASON_TOFIELD, zone, &flag1); + int32_t ct2 = get_spsummonable_count_fromex(target, playerid, target->summon_player, zone, &flag2); + for(auto& pcard : targets->container) { + if(pcard->current.location == LOCATION_MZONE) + continue; + if(pcard->current.location != LOCATION_EXTRA) + --ct1; + else + --ct2; + } + if(target->current.location != LOCATION_EXTRA) { + if(ct2 == 0) + zone &= flag2; + } else { + if(ct1 == 0) + zone &= flag1; + } + } + uint8_t sumpositions = target->get_spsummonable_position(core.reason_effect, target->summon_info & DEFAULT_SUMMON_TYPE, positions, target->summon_player, playerid); + move_to_field(target, target->summon_player, playerid, LOCATION_MZONE, sumpositions, FALSE, 0, FALSE, zone); + return FALSE; + } + case 2: { + pduel->write_buffer8(MSG_SPSUMMONING); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_public_info_location()); + return FALSE; + } + case 3: { + returns.ivalue[0] = TRUE; + set_control(target, target->current.controler, 0, 0); + target->set_status(STATUS_SPSUMMON_STEP, TRUE); + return TRUE; + } + case 5: { + returns.ivalue[0] = FALSE; + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + if(targets) + targets->container.erase(target); + return TRUE; + } + } + return TRUE; +} +int32_t field::special_summon(uint16_t step, effect* reason_effect, uint8_t reason_player, group* targets, uint32_t zone) { + switch(step) { + case 0: { + card_vector cvs, cvo; + for(auto& pcard : targets->container) { + if(pcard->summon_player == infos.turn_player) + cvs.push_back(pcard); + else + cvo.push_back(pcard); + } + if(!cvs.empty()) { + if(cvs.size() > 1) + std::sort(cvs.begin(), cvs.end(), card::card_operation_sort); + core.hint_timing[infos.turn_player] |= TIMING_SPSUMMON; + for(auto& pcard : cvs) + add_process(PROCESSOR_SPSUMMON_STEP, 0, nullptr, targets, zone, 0, 0, 0, pcard); + } + if(!cvo.empty()) { + if(cvo.size() > 1) + std::sort(cvo.begin(), cvo.end(), card::card_operation_sort); + core.hint_timing[1 - infos.turn_player] |= TIMING_SPSUMMON; + for(auto& pcard : cvo) + add_process(PROCESSOR_SPSUMMON_STEP, 0, nullptr, targets, zone, 0, 0, 0, pcard); + } + return FALSE; + } + case 1: { + if(core.unable_tofield_set.size()) + send_to(core.unable_tofield_set, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + case 2: { + core.unable_tofield_set.clear(); + if(targets->container.size() == 0) { + returns.ivalue[0] = 0; + core.operated_set.clear(); + pduel->delete_group(targets); + return TRUE; + } + bool tp = false, ntp = false; + std::set spsummon_once_set[2]; + for(auto& pcard : targets->container) { + if(pcard->summon_player == infos.turn_player) + tp = true; + else + ntp = true; + if(pcard->spsummon_code) + spsummon_once_set[pcard->summon_player].insert(pcard->spsummon_code); + } + if(tp) + set_spsummon_counter(infos.turn_player); + if(ntp) + set_spsummon_counter(1 - infos.turn_player); + for(auto& cit : spsummon_once_set[0]) + ++core.spsummon_once_map[0][cit]; + for(auto& cit : spsummon_once_set[1]) + ++core.spsummon_once_map[1][cit]; + for(auto& pcard : targets->container) { + pcard->set_status(STATUS_SPSUMMON_STEP, FALSE); + pcard->set_status(STATUS_SPSUMMON_TURN, TRUE); + pcard->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + if(pcard->is_position(POS_FACEUP)) + pcard->enable_field_effect(true); + } + adjust_instant(); + return FALSE; + } + case 3: { + pduel->write_buffer8(MSG_SPSUMMONED); + for(auto& pcard : targets->container) { + check_card_counter(pcard, ACTIVITY_SPSUMMON, pcard->summon_player); + pcard->set_special_summon_status(reason_effect); + if(!(pcard->current.position & POS_FACEDOWN)) + raise_single_event(pcard, 0, EVENT_SPSUMMON_SUCCESS, reason_effect, 0, reason_player, pcard->summon_player, 0); + int32_t summontype = pcard->summon_info & (SUMMON_VALUE_MAIN_TYPE | SUMMON_VALUE_SUB_TYPE); + int32_t custom_type = pcard->summon_info & SUMMON_VALUE_CUSTOM_TYPE; + if(summontype && pcard->material_cards.size() && custom_type != SUMMON_VALUE_FUTURE_FUSION) { + int32_t matreason = 0; + if(summontype == SUMMON_TYPE_FUSION) + matreason = REASON_FUSION; + else if(summontype == SUMMON_TYPE_RITUAL) + matreason = REASON_RITUAL; + else if(summontype == SUMMON_TYPE_XYZ) + matreason = REASON_XYZ; + else if(summontype == SUMMON_TYPE_LINK) + matreason = REASON_LINK; + for(auto& mcard : pcard->material_cards) + raise_single_event(mcard, &targets->container, EVENT_BE_MATERIAL, reason_effect, matreason, reason_player, pcard->summon_player, 0); + raise_event(pcard->material_cards, EVENT_BE_MATERIAL, reason_effect, matreason, reason_player, pcard->summon_player, 0); + } + } + process_single_event(); + process_instant_event(); + return FALSE; + } + case 4: { + raise_event(targets->container, EVENT_SPSUMMON_SUCCESS, reason_effect, 0, reason_player, PLAYER_NONE, 0); + process_instant_event(); + return FALSE; + } + case 5: { + core.operated_set.clear(); + core.operated_set = targets->container; + returns.ivalue[0] = (int32_t)targets->container.size(); + pduel->delete_group(targets); + return TRUE; + } + } + return TRUE; +} +int32_t field::destroy_replace(uint16_t step, group* targets, card* target, uint8_t battle) { + if(target->current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) { + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + target->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + targets->container.erase(target); + return TRUE; + } + if(targets->container.find(target) == targets->container.end()) + return TRUE; + returns.ivalue[0] = FALSE; + effect_set eset; + target->filter_single_continuous_effect(EFFECT_DESTROY_REPLACE, &eset); + if(!battle) { + for (effect_set::size_type i = 0; i < eset.size(); ++i) + add_process(PROCESSOR_OPERATION_REPLACE, 0, eset[i], targets, 1, 0, 0, 0, target); + } else { + for (effect_set::size_type i = 0; i < eset.size(); ++i) + add_process(PROCESSOR_OPERATION_REPLACE, 10, eset[i], targets, 1, 0, 0, 0, target); + } + return TRUE; +} +int32_t field::destroy(uint16_t step, group * targets, effect * reason_effect, uint32_t reason, uint8_t reason_player) { + switch (step) { + case 0: { + card_set extra; + effect_set eset; + card_set indestructable_set; + std::set indestructable_effect_set; + for (auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + if(!pcard->is_destructable()) { + indestructable_set.insert(pcard); + ++cit; + continue; + } + if (!(pcard->current.reason & (REASON_RULE | REASON_COST))) { + bool is_destructable = true; + if (pcard->is_affect_by_effect(pcard->current.reason_effect)) { + effect* indestructable_effect = pcard->check_indestructable_by_effect(pcard->current.reason_effect, pcard->current.reason_player); + if (indestructable_effect) { + if(reason_player != PLAYER_SELFDES) + indestructable_effect_set.insert(indestructable_effect); + is_destructable = false; + } + } else + is_destructable = false; + if (!is_destructable) { + indestructable_set.insert(pcard); + ++cit; + continue; + } + } + eset.clear(); + pcard->filter_effect(EFFECT_INDESTRUCTABLE, &eset); + if(eset.size()) { + bool is_destructable = true; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + if(reason_player != PLAYER_SELFDES) + indestructable_effect_set.insert(eset[i]); + is_destructable = false; + break; + } + } + if(!is_destructable) { + indestructable_set.insert(pcard); + ++cit; + continue; + } + } + // monsters with EFFECT_INDESTRUCTABLE_COUNT cannot apply EFFECT_DESTROY_REPLACE + eset.clear(); + pcard->filter_effect(EFFECT_INDESTRUCTABLE_COUNT, &eset); + if (eset.size()) { + bool is_destructable = true; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + if(eset[i]->count_limit == 0) + continue; + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + indestructable_effect_set.insert(eset[i]); + is_destructable = false; + } + } else { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + int32_t ct = eset[i]->get_value(3); + if(ct) { + auto ret = pcard->indestructable_effects.emplace(eset[i]->id, 0); + if(++ret.first->second <= ct) { + indestructable_effect_set.insert(eset[i]); + is_destructable = false; + } + } + } + } + if(!is_destructable) { + core.indestructable_count_set.insert(pcard); + ++cit; + continue; + } + } + eset.clear(); + pcard->filter_effect(EFFECT_DESTROY_SUBSTITUTE, &eset); + if (eset.size()) { + bool sub = false; + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + extra.insert(eset[i]->handler); + sub = true; + } + } + if(sub) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + core.destroy_canceled.insert(pcard); + cit = targets->container.erase(cit); + continue; + } + } + ++cit; + } + for (auto& pcard : indestructable_set) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + targets->container.erase(pcard); + } + for (auto& pcard : core.indestructable_count_set) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + targets->container.erase(pcard); + } + for (auto& rep : extra) { + if(targets->container.count(rep) == 0) { + rep->temp.reason = rep->current.reason; + rep->temp.reason_effect = rep->current.reason_effect; + rep->temp.reason_player = rep->current.reason_player; + rep->current.reason = REASON_EFFECT | REASON_DESTROY | REASON_REPLACE; + rep->current.reason_effect = 0; + rep->current.reason_player = rep->current.controler; + rep->sendto_param.set(rep->owner, POS_FACEUP, LOCATION_GRAVE); + targets->container.insert(rep); + } + } + for (auto& peffect : indestructable_effect_set) { + peffect->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(peffect->owner->data.code); + if(peffect->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(peffect->description); + } + } + operation_replace(EFFECT_DESTROY_REPLACE, 5, targets); + return FALSE; + } + case 1: { + for (auto& pcard : targets->container) { + add_process(PROCESSOR_DESTROY_REPLACE, 0, nullptr, targets, 0, 0, 0, 0, pcard); + } + return FALSE; + } + case 2: { + for (auto& pcard : core.destroy_canceled) + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + core.destroy_canceled.clear(); + for (auto& pcard : core.indestructable_count_set) + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + core.indestructable_count_set.clear(); + return FALSE; + } + case 3: { + if(!targets->container.size()) { + returns.ivalue[0] = 0; + core.operated_set.clear(); + pduel->delete_group(targets); + return TRUE; + } + card_vector cv(targets->container.begin(), targets->container.end()); + if(cv.size() > 1) + std::sort(cv.begin(), cv.end(), card::card_operation_sort); + for (auto& pcard : cv) { + if(pcard->current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + targets->container.erase(pcard); + continue; + } + pcard->current.reason |= REASON_DESTROY; + core.hint_timing[pcard->current.controler] |= TIMING_DESTROY; + raise_single_event(pcard, 0, EVENT_DESTROY, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + adjust_disable_check_list(); + process_single_event(); + raise_event(targets->container, EVENT_DESTROY, reason_effect, reason, reason_player, 0, 0); + process_instant_event(); + return FALSE; + } + case 4: { + group* sendtargets = pduel->new_group(targets->container); + sendtargets->is_readonly = GTYPE_READ_ONLY; + for(auto& pcard : sendtargets->container) { + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + uint32_t dest = pcard->sendto_param.location; + if(!dest) + dest = LOCATION_GRAVE; + if((dest == LOCATION_HAND && !pcard->is_capable_send_to_hand(reason_player)) + || (dest == LOCATION_DECK && !pcard->is_capable_send_to_deck(reason_player)) + || (dest == LOCATION_REMOVED && !pcard->is_removeable(reason_player, pcard->sendto_param.position, reason))) + dest = LOCATION_GRAVE; + pcard->sendto_param.location = dest; + } + operation_replace(EFFECT_SEND_REPLACE, 5, sendtargets); + add_process(PROCESSOR_SENDTO, 1, reason_effect, sendtargets, reason | REASON_DESTROY, reason_player); + return FALSE; + } + case 5: { + core.operated_set.clear(); + core.operated_set = targets->container; + for(auto cit = core.operated_set.begin(); cit != core.operated_set.end();) { + if((*cit)->current.reason & REASON_REPLACE) + cit = core.operated_set.erase(cit); + else + ++cit; + } + returns.ivalue[0] = (int32_t)core.operated_set.size(); + pduel->delete_group(targets); + adjust_self_destroy_set(); + return TRUE; + } + case 10: { + effect_set eset; + for (auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + if (!pcard->is_destructable()) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + cit = targets->container.erase(cit); + continue; + } + eset.clear(); + pcard->filter_effect(EFFECT_INDESTRUCTABLE, &eset); + if(eset.size()) { + bool indes = false; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->owner->data.code); + if(eset[i]->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->description); + } + indes = true; + break; + } + } + if(indes) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + cit = targets->container.erase(cit); + continue; + } + } + eset.clear(); + pcard->filter_effect(EFFECT_INDESTRUCTABLE_COUNT, &eset); + if (eset.size()) { + bool indes = false; + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->is_flag(EFFECT_FLAG_COUNT_LIMIT)) { + if(eset[i]->count_limit == 0) + continue; + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + eset[i]->dec_count(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->owner->data.code); + if(eset[i]->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->description); + } + indes = true; + } + } else { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + int32_t ct = eset[i]->get_value(3); + if(ct) { + auto it = pcard->indestructable_effects.emplace(eset[i]->id, 0); + if(++it.first->second <= ct) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->owner->data.code); + if(eset[i]->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(eset[i]->description); + } + indes = true; + } + } + } + } + if(indes) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + cit = targets->container.erase(cit); + continue; + } + } + eset.clear(); + pcard->filter_effect(EFFECT_DESTROY_SUBSTITUTE, &eset); + if (eset.size()) { + bool sub = false; + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(pcard->current.reason_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard->current.reason, PARAM_TYPE_INT); + pduel->lua->add_param(pcard->current.reason_player, PARAM_TYPE_INT); + if(eset[i]->check_value_condition(3)) { + core.battle_destroy_rep.insert(eset[i]->handler); + sub = true; + } + } + if(sub) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + core.destroy_canceled.insert(pcard); + cit = targets->container.erase(cit); + continue; + } + } + ++cit; + } + if(targets->container.size()) { + operation_replace(EFFECT_DESTROY_REPLACE, 12, targets); + } + return FALSE; + } + case 11: { + for (auto& pcard : targets->container) { + add_process(PROCESSOR_DESTROY_REPLACE, 0, nullptr, targets, 0, TRUE, 0, 0, pcard); + } + return FALSE; + } + case 12: { + for (auto& pcard : core.destroy_canceled) + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + core.destroy_canceled.clear(); + return TRUE; + } + } + return TRUE; +} +int32_t field::release_replace(uint16_t step, group* targets, card* target) { + if(target->current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) { + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + targets->container.erase(target); + return TRUE; + } + if(targets->container.find(target) == targets->container.end()) + return TRUE; + if(!(target->current.reason & REASON_RULE)) { + returns.ivalue[0] = FALSE; + effect_set eset; + target->filter_single_continuous_effect(EFFECT_RELEASE_REPLACE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) + add_process(PROCESSOR_OPERATION_REPLACE, 0, eset[i], targets, 0, 0, 0, 0, target); + } + return TRUE; +} +int32_t field::release(uint16_t step, group * targets, effect * reason_effect, uint32_t reason, uint8_t reason_player) { + switch (step) { + case 0: { + for (auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + if (pcard->get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP) + || ((reason & REASON_SUMMON) && !pcard->is_releasable_by_summon(reason_player, pcard->current.reason_card)) + || ((reason & REASON_EFFECT) + && (!pcard->is_affect_by_effect(pcard->current.reason_effect) || !pcard->is_releasable_by_nonsummon(reason_player, reason)))) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + cit = targets->container.erase(cit); + } + else + ++cit; + } + if(reason & REASON_RULE) + return FALSE; + operation_replace(EFFECT_RELEASE_REPLACE, 5, targets); + return FALSE; + } + case 1: { + for (auto& pcard : targets->container) { + add_process(PROCESSOR_RELEASE_REPLACE, 0, nullptr, targets, 0, 0, 0, 0, pcard); + } + return FALSE; + } + case 2: { + if(!targets->container.size()) { + returns.ivalue[0] = 0; + core.operated_set.clear(); + pduel->delete_group(targets); + return TRUE; + } + card_vector cv(targets->container.begin(), targets->container.end()); + if(cv.size() > 1) + std::sort(cv.begin(), cv.end(), card::card_operation_sort); + for (auto& pcard : cv) { + if(pcard->current.location & (LOCATION_GRAVE | LOCATION_REMOVED)) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_effect = pcard->temp.reason_effect; + pcard->current.reason_player = pcard->temp.reason_player; + targets->container.erase(pcard); + continue; + } + pcard->current.reason |= REASON_RELEASE; + } + adjust_instant(); + return FALSE; + } + case 3: { + group* sendtargets = pduel->new_group(targets->container); + sendtargets->is_readonly = GTYPE_READ_ONLY; + operation_replace(EFFECT_SEND_REPLACE, 5, sendtargets); + add_process(PROCESSOR_SENDTO, 1, reason_effect, sendtargets, reason | REASON_RELEASE, reason_player); + return FALSE; + } + case 4: { + for(auto& peffect : core.dec_count_reserve) + peffect->dec_count(); + core.dec_count_reserve.clear(); + core.operated_set.clear(); + core.operated_set = targets->container; + returns.ivalue[0] = (int32_t)targets->container.size(); + pduel->delete_group(targets); + return TRUE; + } + } + return TRUE; +} +int32_t field::send_replace(uint16_t step, group * targets, card * target) { + uint8_t playerid = target->sendto_param.playerid; + uint8_t dest = target->sendto_param.location; + if(targets->container.find(target) == targets->container.end()) + return TRUE; + if(target->current.location == dest && target->current.controler == playerid) { + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + targets->container.erase(target); + return TRUE; + } + if(!(target->current.reason & REASON_RULE)) { + returns.ivalue[0] = FALSE; + effect_set eset; + target->filter_single_continuous_effect(EFFECT_SEND_REPLACE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) + add_process(PROCESSOR_OPERATION_REPLACE, 0, eset[i], targets, 0, 0, 0, 0, target); + } + return TRUE; +} +int32_t field::send_to(uint16_t step, group * targets, effect * reason_effect, uint32_t reason, uint8_t reason_player, uint8_t send_activating) { + struct exargs { + group* targets{ nullptr }; + card_set leave_field, leave_grave, leave_deck, detach; + bool show_decktop[2]{}; + card_vector cv; + card_vector::iterator cvit; + effect* predirect{ nullptr }; + } ; + switch(step) { + case 0: { + for(auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + uint8_t dest = pcard->sendto_param.location; + if(!(reason & REASON_RULE) && + (pcard->get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP) + || ((pcard->current.reason & REASON_EFFECT) && !pcard->is_affect_by_effect(pcard->current.reason_effect)) + || (dest == LOCATION_HAND && !pcard->is_capable_send_to_hand(core.reason_player)) + || (dest == LOCATION_DECK && !pcard->is_capable_send_to_deck(core.reason_player, send_activating)) + || (dest == LOCATION_REMOVED && !pcard->is_removeable(core.reason_player, pcard->sendto_param.position, reason)) + || (dest == LOCATION_GRAVE && !pcard->is_capable_send_to_grave(core.reason_player)) + || (dest == LOCATION_EXTRA && !pcard->is_capable_send_to_extra(core.reason_player)))) { + pcard->current.reason = pcard->temp.reason; + pcard->current.reason_player = pcard->temp.reason_player; + pcard->current.reason_effect = pcard->temp.reason_effect; + cit = targets->container.erase(cit); + } + else + ++cit; + } + if(reason & REASON_RULE) + return FALSE; + operation_replace(EFFECT_SEND_REPLACE, 5, targets); + return FALSE; + } + case 1: { + // the entrance of release, destroy + for(auto& pcard : targets->container) { + add_process(PROCESSOR_SENDTO_REPLACE, 0, nullptr, targets, 0, 0, 0, 0, pcard); + } + return FALSE; + } + case 2: { + if(!targets->container.size()) { + returns.ivalue[0] = 0; + core.operated_set.clear(); + pduel->delete_group(targets); + return TRUE; + } + card_set leave_p; + for(auto& pcard : targets->container) { + if((pcard->current.location & LOCATION_ONFIELD) && !pcard->is_status(STATUS_SUMMON_DISABLED) && !pcard->is_status(STATUS_ACTIVATE_DISABLED)) { + raise_single_event(pcard, 0, EVENT_LEAVE_FIELD_P, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + leave_p.insert(pcard); + } + if((pcard->current.location & LOCATION_ONFIELD)) { + if(pcard->current.position & POS_FACEUP) { + pcard->previous.code = pcard->get_code(); + pcard->previous.code2 = pcard->get_another_code(); + pcard->previous.type = pcard->get_type(); + if(pcard->current.location & LOCATION_MZONE) { + pcard->previous.level = pcard->get_level(); + pcard->previous.rank = pcard->get_rank(); + pcard->previous.attribute = pcard->get_attribute(); + pcard->previous.race = pcard->get_race(); + std::pair atk_def = pcard->get_atk_def(); + pcard->previous.attack = atk_def.first; + pcard->previous.defense = atk_def.second; + } + } else { + auto codes = pcard->get_original_code_rule(); + pcard->previous.code = std::get<0>(codes); + pcard->previous.code2 = std::get<1>(codes); + pcard->previous.type = pcard->data.type; + pcard->previous.level = pcard->data.level; + pcard->previous.rank = pcard->data.level; + pcard->previous.attribute = pcard->data.attribute; + pcard->previous.race = pcard->data.race; + pcard->previous.attack = pcard->data.attack; + pcard->previous.defense = pcard->data.defense; + } + pcard->previous.setcode.clear(); + effect_set eset; + pcard->filter_effect(EFFECT_ADD_SETCODE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pcard->previous.setcode.push_back((uint32_t)eset[i]->get_value(pcard) & 0xffff); + } + } + } + if(leave_p.size()) + raise_event(leave_p, EVENT_LEAVE_FIELD_P, reason_effect, reason, reason_player, 0, 0); + process_single_event(); + process_instant_event(); + return FALSE; + } + case 3: { + uint32_t dest, redirect, redirect_seq, check_cb; + for(auto& pcard : targets->container) + pcard->enable_field_effect(false); + adjust_disable_check_list(); + for(auto& pcard : targets->container) { + dest = pcard->sendto_param.location; + redirect = 0; + redirect_seq = 0; + check_cb = 0; + if((dest & LOCATION_GRAVE) && pcard->is_affected_by_effect(EFFECT_TO_GRAVE_REDIRECT_CB)) + check_cb = 1; + if((pcard->current.location & LOCATION_ONFIELD) && !pcard->is_status(STATUS_SUMMON_DISABLED) && !pcard->is_status(STATUS_ACTIVATE_DISABLED)) { + redirect = pcard->leave_field_redirect(pcard->current.reason); + redirect_seq = redirect >> 16; + redirect &= 0xffff; + } + if(redirect) { + pcard->current.reason &= ~REASON_TEMPORARY; + pcard->current.reason |= REASON_REDIRECT; + pcard->sendto_param.location = redirect; + pcard->sendto_param.sequence = redirect_seq; + dest = redirect; + if(dest == LOCATION_REMOVED) { + if(pcard->sendto_param.position & POS_FACEDOWN_ATTACK) + pcard->sendto_param.position = (pcard->sendto_param.position & ~POS_FACEDOWN_ATTACK) | POS_FACEUP_ATTACK; + if(pcard->sendto_param.position & POS_FACEDOWN_DEFENSE) + pcard->sendto_param.position = (pcard->sendto_param.position & ~POS_FACEDOWN_DEFENSE) | POS_FACEUP_DEFENSE; + } + } + redirect = pcard->destination_redirect(dest, pcard->current.reason); + if(redirect) { + redirect_seq = redirect >> 16; + redirect &= 0xffff; + } + if(redirect && (pcard->current.location != redirect)) { + pcard->current.reason |= REASON_REDIRECT; + pcard->sendto_param.location = redirect; + pcard->sendto_param.sequence = redirect_seq; + } + if(check_cb) + pcard->sendto_param.playerid |= 0x1u << 4; + } + return FALSE; + } + case 4: { + exargs* param = new exargs; + core.units.begin()->ptarget = (group*)param; + param->targets = targets; + param->show_decktop[0] = false; + param->show_decktop[1] = false; + param->cv.assign(targets->container.begin(), targets->container.end()); + if(param->cv.size() > 1) + std::sort(param->cv.begin(), param->cv.end(), card::card_operation_sort); + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + for(auto& pcard : param->cv) { + if(pcard->current.location == LOCATION_DECK) + param->show_decktop[pcard->current.controler] = true; + } + } + param->cvit = param->cv.begin(); + return FALSE; + } + case 5: { + exargs* param = (exargs*)targets; + if(param->cvit == param->cv.end()) { + core.units.begin()->step = 8; + return FALSE; + } + card* pcard = *param->cvit; + param->predirect = 0; + uint32_t check_cb = pcard->sendto_param.playerid >> 4; + if(check_cb) + param->predirect = pcard->is_affected_by_effect(EFFECT_TO_GRAVE_REDIRECT_CB); + pcard->enable_field_effect(false); + if(pcard->data.type & TYPE_TOKEN) { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer8(pcard->current.position); + pduel->write_buffer8(0); + pduel->write_buffer8(0); + pduel->write_buffer8(0); + pduel->write_buffer8(0); + pduel->write_buffer32(pcard->current.reason); + pcard->previous.controler = pcard->current.controler; + pcard->previous.location = pcard->current.location; + pcard->previous.sequence = pcard->current.sequence; + pcard->previous.position = pcard->current.position; + pcard->previous.pzone = pcard->current.pzone; + pcard->current.reason &= ~REASON_TEMPORARY; + pcard->fieldid = infos.field_id++; + pcard->fieldid_r = pcard->fieldid; + pcard->reset(RESET_LEAVE, RESET_EVENT); + pcard->clear_relate_effect(); + remove_card(pcard); + param->leave_field.insert(pcard); + ++param->cvit; + core.units.begin()->step = 4; + return FALSE; + } + if(param->predirect && get_useable_count(pcard, pcard->current.controler, LOCATION_SZONE, pcard->current.controler, LOCATION_REASON_TOFIELD) > 0) + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)pcard, pcard->current.controler, 97); + else + returns.ivalue[0] = 0; + return FALSE; + } + case 6: { + if(returns.ivalue[0]) + return FALSE; + exargs* param = (exargs*)targets; + card* pcard = *param->cvit; + uint8_t oloc = pcard->current.location; + uint8_t playerid = pcard->sendto_param.playerid & 0x7; + uint8_t dest = pcard->sendto_param.location; + uint8_t seq = pcard->sendto_param.sequence; + uint8_t control_player = pcard->overlay_target ? pcard->overlay_target->current.controler : pcard->current.controler; + if(dest == LOCATION_GRAVE) { + core.hint_timing[control_player] |= TIMING_TOGRAVE; + } else if(dest == LOCATION_HAND) { + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + core.hint_timing[control_player] |= TIMING_TOHAND; + } else if(dest == LOCATION_DECK) { + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + core.hint_timing[control_player] |= TIMING_TODECK; + } else if(dest == LOCATION_REMOVED) { + core.hint_timing[control_player] |= TIMING_REMOVE; + } + if(pcard->current.controler != playerid || pcard->current.location != dest) { + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + if(pcard->overlay_target) { + param->detach.insert(pcard->overlay_target); + pcard->overlay_target->xyz_remove(pcard); + } + move_card(playerid, pcard, dest, seq); + pcard->current.position = pcard->sendto_param.position; + pduel->write_buffer32(pcard->get_public_info_location()); + pduel->write_buffer32(pcard->current.reason); + } + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + if(pcard->current.location == LOCATION_DECK) + param->show_decktop[pcard->current.controler] = true; + } + pcard->set_status(STATUS_LEAVE_CONFIRMED, FALSE); + pcard->set_status(STATUS_FLIP_SUMMONING, FALSE); + pcard->set_status(STATUS_FLIP_SUMMON_DISABLED, FALSE); + if(pcard->status & (STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED)) { + pcard->set_status(STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED, FALSE); + pcard->previous.location = 0; + } else if(oloc & LOCATION_ONFIELD) { + pcard->reset(RESET_LEAVE, RESET_EVENT); + param->leave_field.insert(pcard); + } else if(oloc == LOCATION_GRAVE) { + param->leave_grave.insert(pcard); + } else if(oloc == LOCATION_DECK || oloc == LOCATION_EXTRA) { + param->leave_deck.insert(pcard); + } + if(pcard->previous.location == LOCATION_OVERLAY) + pcard->previous.controler = control_player; + ++param->cvit; + core.units.begin()->step = 4; + return FALSE; + } + case 7: { + // crystal beast redirection + exargs* param = (exargs*)targets; + card* pcard = *param->cvit; + uint32_t flag; + get_useable_count(pcard, pcard->current.controler, LOCATION_SZONE, pcard->current.controler, LOCATION_REASON_TOFIELD, 0xff, &flag); + flag = ((flag << 8) & 0xff00) | 0xffffe0ff; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer32(pcard->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, pcard->current.controler, flag, 1); + return FALSE; + } + case 8: { + exargs* param = (exargs*)targets; + card* pcard = *param->cvit; + uint8_t oloc = pcard->current.location; + uint8_t seq = returns.bvalue[2]; + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + if(pcard->overlay_target) { + param->detach.insert(pcard->overlay_target); + pcard->overlay_target->xyz_remove(pcard); + } + move_card(pcard->current.controler, pcard, LOCATION_SZONE, seq); + pcard->current.position = POS_FACEUP; + pduel->write_buffer32(pcard->get_public_info_location()); + pduel->write_buffer32(pcard->current.reason); + pcard->set_status(STATUS_LEAVE_CONFIRMED, FALSE); + pcard->set_status(STATUS_FLIP_SUMMONING, FALSE); + pcard->set_status(STATUS_FLIP_SUMMON_DISABLED, FALSE); + if(pcard->status & (STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED)) { + pcard->set_status(STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED, FALSE); + pcard->previous.location = 0; + } else if(oloc & LOCATION_ONFIELD) { + pcard->reset(RESET_LEAVE + RESET_MSCHANGE, RESET_EVENT); + pcard->clear_card_target(); + param->leave_field.insert(pcard); + } + if(param->predirect->operation) { + tevent e; + e.event_cards = targets; + e.event_player = pcard->current.controler; + e.event_value = 0; + e.reason = pcard->current.reason; + e.reason_effect = reason_effect; + e.reason_player = pcard->current.controler; + core.sub_solving_event.push_back(e); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, param->predirect, 0, pcard->current.controler, 0); + } + pcard->sendto_param.playerid |= 0x1u << 5; + ++param->cvit; + core.units.begin()->step = 4; + return FALSE; + } + case 9: { + exargs* param = (exargs*)targets; + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + for(int32_t p = 0; p < 2; ++p) { + if(param->show_decktop[p] && player[p].list_main.size()) { + card* ptop = *player[p].list_main.rbegin(); + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(p); + pduel->write_buffer8(0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + } + for(auto& pcard : param->targets->container) { + if(!(pcard->data.type & TYPE_TOKEN)) + pcard->enable_field_effect(true); + uint8_t nloc = pcard->current.location; + if(nloc == LOCATION_HAND) + pcard->reset(RESET_TOHAND, RESET_EVENT); + if(nloc == LOCATION_DECK || nloc == LOCATION_EXTRA) + pcard->reset(RESET_TODECK, RESET_EVENT); + if(nloc == LOCATION_GRAVE) + pcard->reset(RESET_TOGRAVE, RESET_EVENT); + if(nloc == LOCATION_REMOVED || ((pcard->data.type & TYPE_TOKEN) && pcard->sendto_param.location == LOCATION_REMOVED)) { + if(pcard->current.reason & REASON_TEMPORARY) + pcard->reset(RESET_TEMP_REMOVE, RESET_EVENT); + else + pcard->reset(RESET_REMOVE, RESET_EVENT); + } + pcard->refresh_disable_status(); + } + for(auto& pcard : param->leave_field) + raise_single_event(pcard, 0, EVENT_LEAVE_FIELD, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + for(auto& pcard : param->leave_grave) + raise_single_event(pcard, 0, EVENT_LEAVE_GRAVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + for(auto& pcard : param->leave_deck) + raise_single_event(pcard, 0, EVENT_LEAVE_DECK, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + if((core.global_flag & GLOBALFLAG_DETACH_EVENT) && param->detach.size()) { + for(auto& pcard : param->detach) { + if(pcard->current.location & LOCATION_MZONE) + raise_single_event(pcard, 0, EVENT_DETACH_MATERIAL, reason_effect, reason, reason_player, 0, 0); + } + } + process_single_event(); + if(param->leave_field.size()) + raise_event(param->leave_field, EVENT_LEAVE_FIELD, reason_effect, reason, reason_player, 0, 0); + if(param->leave_grave.size()) + raise_event(param->leave_grave, EVENT_LEAVE_GRAVE, reason_effect, reason, reason_player, 0, 0); + if(param->leave_deck.size()) + raise_event(param->leave_deck, EVENT_LEAVE_DECK, reason_effect, reason, reason_player, 0, 0); + if((core.global_flag & GLOBALFLAG_DETACH_EVENT) && param->detach.size()) + raise_event(param->detach, EVENT_DETACH_MATERIAL, reason_effect, reason, reason_player, 0, 0); + process_instant_event(); + adjust_instant(); + return FALSE; + } + case 10: { + exargs* param = (exargs*)targets; + core.units.begin()->ptarget = param->targets; + targets = param->targets; + delete param; + card_set tohand, todeck, tograve, remove, discard, released, destroyed; + card_set equipings, overlays; + for(auto& pcard : targets->container) { + uint8_t nloc = pcard->current.location; + uint8_t cb_redirected = pcard->sendto_param.playerid >> 5; + if(pcard->equiping_target && !cb_redirected) + pcard->unequip(); + if(pcard->equiping_cards.size()) { + for(auto csit = pcard->equiping_cards.begin(); csit != pcard->equiping_cards.end();) { + card* equipc = *(csit++); + equipc->unequip(); + if(equipc->current.location == LOCATION_SZONE) + equipings.insert(equipc); + } + } + pcard->clear_card_target(); + if(nloc == LOCATION_HAND) { + if(pcard->owner != pcard->current.controler) { + effect* deffect = pduel->new_effect(); + deffect->owner = pcard; + deffect->code = 0; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 67; + deffect->reset_flag = RESET_EVENT + 0x1fe0000; + pcard->add_effect(deffect); + } + tohand.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_HAND, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(nloc == LOCATION_DECK || nloc == LOCATION_EXTRA) { + todeck.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_DECK, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(nloc == LOCATION_GRAVE && !(pcard->current.reason & REASON_RETURN)) { + tograve.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_GRAVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(nloc == LOCATION_REMOVED) { + remove.insert(pcard); + raise_single_event(pcard, 0, EVENT_REMOVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(pcard->current.reason & REASON_DISCARD) { + discard.insert(pcard); + raise_single_event(pcard, 0, EVENT_DISCARD, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(pcard->current.reason & REASON_RELEASE) { + released.insert(pcard); + raise_single_event(pcard, 0, EVENT_RELEASE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + // non-battle destroy + if(pcard->current.reason & REASON_DESTROY && !(pcard->current.reason & REASON_BATTLE)) { + destroyed.insert(pcard); + raise_single_event(pcard, 0, EVENT_DESTROYED, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(pcard->xyz_materials.size()) { + pcard->xyz_materials_previous_count_onfield = (int32_t)pcard->xyz_materials.size(); + for(auto& mcard : pcard->xyz_materials) + overlays.insert(mcard); + } else { + pcard->xyz_materials_previous_count_onfield = 0; + } + raise_single_event(pcard, 0, EVENT_MOVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + if(tohand.size()) + raise_event(tohand, EVENT_TO_HAND, reason_effect, reason, reason_player, 0, 0); + if(todeck.size()) + raise_event(todeck, EVENT_TO_DECK, reason_effect, reason, reason_player, 0, 0); + if(tograve.size()) + raise_event(tograve, EVENT_TO_GRAVE, reason_effect, reason, reason_player, 0, 0); + if(remove.size()) + raise_event(remove, EVENT_REMOVE, reason_effect, reason, reason_player, 0, 0); + if(discard.size()) + raise_event(discard, EVENT_DISCARD, reason_effect, reason, reason_player, 0, 0); + if(released.size()) + raise_event(released, EVENT_RELEASE, reason_effect, reason, reason_player, 0, 0); + if(destroyed.size()) + raise_event(destroyed, EVENT_DESTROYED, reason_effect, reason, reason_player, 0, 0); + raise_event(targets->container, EVENT_MOVE, reason_effect, reason, reason_player, 0, 0); + process_single_event(); + process_instant_event(); + if(equipings.size()) + destroy(equipings, 0, REASON_RULE + REASON_LOST_TARGET, PLAYER_NONE); + if(overlays.size()) + send_to(overlays, 0, REASON_RULE + REASON_LOST_OVERLAY, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + adjust_instant(); + return FALSE; + } + case 11: { + core.operated_set.clear(); + core.operated_set = targets->container; + returns.ivalue[0] = (int32_t)targets->container.size(); + pduel->delete_group(targets); + return TRUE; + } + } + return TRUE; +} +int32_t field::discard_deck(uint16_t step, uint8_t playerid, uint8_t count, uint32_t reason) { + switch(step) { + case 0: { + if(is_player_affected_by_effect(playerid, EFFECT_CANNOT_DISCARD_DECK)) { + core.operated_set.clear(); + returns.ivalue[0] = 0; + return TRUE; + } + int32_t i = 0; + for(auto cit = player[playerid].list_main.rbegin(); i < count && cit != player[playerid].list_main.rend(); ++cit, ++i) { + uint32_t dest = LOCATION_GRAVE; + (*cit)->sendto_param.location = LOCATION_GRAVE; + (*cit)->current.reason_effect = core.reason_effect; + (*cit)->current.reason_player = core.reason_player; + (*cit)->current.reason = reason; + uint32_t redirect = (*cit)->destination_redirect(dest, reason) & 0xffff; + if(redirect) { + (*cit)->sendto_param.location = redirect; + } + } + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + if(player[playerid].list_main.size() > count) { + card* ptop = *(player[playerid].list_main.rbegin() + count); + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + return FALSE; + } + case 1: { + card_set tohand, todeck, tograve, remove; + core.discarded_set.clear(); + for (int32_t i = 0; i < count; ++i) { + if(player[playerid].list_main.size() == 0) + break; + card* pcard = player[playerid].list_main.back(); + uint8_t dest = pcard->sendto_param.location; + if(dest == LOCATION_GRAVE) + pcard->reset(RESET_TOGRAVE, RESET_EVENT); + else if(dest == LOCATION_HAND) { + pcard->reset(RESET_TOHAND, RESET_EVENT); + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + } else if(dest == LOCATION_DECK) { + pcard->reset(RESET_TODECK, RESET_EVENT); + pcard->set_status(STATUS_PROC_COMPLETE, FALSE); + } else if(dest == LOCATION_REMOVED) { + if(pcard->current.reason & REASON_TEMPORARY) + pcard->reset(RESET_TEMP_REMOVE, RESET_EVENT); + else + pcard->reset(RESET_REMOVE, RESET_EVENT); + } + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer8(pcard->current.position); + pcard->enable_field_effect(false); + pcard->cancel_field_effect(); + player[playerid].list_main.pop_back(); + pcard->previous.controler = pcard->current.controler; + pcard->previous.location = pcard->current.location; + pcard->previous.sequence = pcard->current.sequence; + pcard->previous.position = pcard->current.position; + pcard->previous.pzone = pcard->current.pzone; + pcard->current.controler = PLAYER_NONE; + pcard->current.location = 0; + add_card(pcard->owner, pcard, dest, 0); + pcard->enable_field_effect(true); + pcard->current.position = POS_FACEUP; + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer8(pcard->current.position); + pduel->write_buffer32(pcard->current.reason); + if(dest == LOCATION_HAND) { + if(pcard->owner != pcard->current.controler) { + effect* deffect = pduel->new_effect(); + deffect->owner = pcard; + deffect->code = 0; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 67; + deffect->reset_flag = RESET_EVENT + 0x1fe0000; + pcard->add_effect(deffect); + } + tohand.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_HAND, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } else if(dest == LOCATION_DECK || dest == LOCATION_EXTRA) { + todeck.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_DECK, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } else if(dest == LOCATION_GRAVE) { + tograve.insert(pcard); + raise_single_event(pcard, 0, EVENT_TO_GRAVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } else if(dest == LOCATION_REMOVED) { + remove.insert(pcard); + raise_single_event(pcard, 0, EVENT_REMOVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + } + raise_single_event(pcard, 0, EVENT_MOVE, pcard->current.reason_effect, pcard->current.reason, pcard->current.reason_player, 0, 0); + core.discarded_set.insert(pcard); + } + if(tohand.size()) + raise_event(tohand, EVENT_TO_HAND, core.reason_effect, reason, core.reason_player, 0, 0); + if(todeck.size()) + raise_event(todeck, EVENT_TO_DECK, core.reason_effect, reason, core.reason_player, 0, 0); + if(tograve.size()) + raise_event(tograve, EVENT_TO_GRAVE, core.reason_effect, reason, core.reason_player, 0, 0); + if(remove.size()) + raise_event(remove, EVENT_REMOVE, core.reason_effect, reason, core.reason_player, 0, 0); + raise_event(core.discarded_set, EVENT_MOVE, core.reason_effect, reason, core.reason_player, 0, 0); + process_single_event(); + process_instant_event(); + adjust_instant(); + return FALSE; + } + case 2: { + core.operated_set.clear(); + core.operated_set = core.discarded_set; + returns.ivalue[0] = (int32_t)core.discarded_set.size(); + core.discarded_set.clear(); + return TRUE; + } + } + return TRUE; +} +// move a card from anywhere to field, including sp_summon, Duel.MoveToField(), Duel.ReturnToField() +// ret: 0 = default, 1 = return after temporarily banished, 2 = trap_monster return to LOCATION_SZONE +// call move_card() in step 2 +int32_t field::move_to_field(uint16_t step, card* target, uint32_t enable, uint32_t ret, uint32_t pzone, uint32_t zone) { + uint32_t move_player = (target->to_field_param >> 24) & 0xff; + uint32_t playerid = (target->to_field_param >> 16) & 0xff; + uint32_t location = (target->to_field_param >> 8) & 0xff; + uint32_t positions = (target->to_field_param) & 0xff; + switch(step) { + case 0: { + returns.ivalue[0] = FALSE; + if((ret == RETURN_TEMP_REMOVE_TO_FIELD) && (!(target->current.reason & REASON_TEMPORARY) || (target->current.reason_effect->owner != core.reason_effect->owner))) + return TRUE; + if(location == LOCATION_SZONE && zone == 0x1U << 5 && (target->data.type & TYPE_FIELD) && (target->data.type & TYPE_SPELL)) { + card* pcard = get_field_card(playerid, LOCATION_SZONE, 5); + if(pcard) { + if(core.duel_rule >= 3) + send_to(pcard, 0, REASON_RULE, pcard->current.controler, PLAYER_NONE, LOCATION_GRAVE, 0, 0); + else + destroy(pcard, 0, REASON_RULE, pcard->current.controler); + adjust_all(); + } + } else if(pzone && location == LOCATION_SZONE && (target->data.type & TYPE_PENDULUM)) { + uint32_t flag = 0; + zone &= 0x3; + if (!zone) + zone = 0x3; + if (is_location_useable(playerid, LOCATION_PZONE, 0) && (zone & 0x1)) + flag |= 0x100U << get_pzone_sequence(0); + if (is_location_useable(playerid, LOCATION_PZONE, 1) && (zone & 0x2)) + flag |= 0x100U << get_pzone_sequence(1); + if(!flag) { + core.units.begin()->step = 3; + if(target->current.location != LOCATION_GRAVE) + send_to(target, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + if(move_player != playerid) + flag = flag << 16; + flag = ~flag; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(move_player); + pduel->write_buffer32(target->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, move_player, flag, 1); + } else { + uint32_t flag; + uint32_t lreason = (target->current.location == LOCATION_MZONE) ? LOCATION_REASON_CONTROL : LOCATION_REASON_TOFIELD; + int32_t ct = get_useable_count(target, playerid, location, move_player, lreason, zone, &flag); + if((ret == RETURN_TEMP_REMOVE_TO_FIELD) && (ct <= 0 || target->is_status(STATUS_FORBIDDEN) || !(positions & POS_FACEDOWN) && check_unique_onfield(target, playerid, location))) { + core.units.begin()->step = 3; + send_to(target, core.reason_effect, REASON_RULE, core.reason_player, PLAYER_NONE, LOCATION_GRAVE, 0, 0); + return FALSE; + } + if(ct <= 0) { + core.units.begin()->step = 3; + if(target->current.location != LOCATION_GRAVE) + send_to(target, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + if((zone & zone - 1) == 0) { + for(uint8_t seq = 0; seq < 8; ++seq) { + if((0x1U << seq) & zone) { + returns.bvalue[2] = seq; + return FALSE; + } + } + } + if(ret == RETURN_TRAP_MONSTER_TO_SZONE && core.duel_rule <= 4) { + returns.bvalue[2] = target->previous.sequence; + return FALSE; + } + if(move_player == playerid) { + if(location == LOCATION_SZONE) + flag = ((flag & 0xff) << 8) | 0xffff00ff; + else + flag = (flag & 0xff) | 0xffffff00; + } else { + if(location == LOCATION_SZONE) + flag = ((flag & 0xff) << 24) | 0xffffff; + else + flag = ((flag & 0xff) << 16) | 0xff00ffff; + } + flag |= 0xe080e080; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(move_player); + pduel->write_buffer32(target->data.code); + add_process(PROCESSOR_SELECT_PLACE, 0, 0, 0, move_player, flag, 1); + } + return FALSE; + } + case 1: { + uint32_t seq = returns.bvalue[2]; + if(location == LOCATION_SZONE && zone == 0x1U << 5 && (target->data.type & TYPE_FIELD) && (target->data.type & TYPE_SPELL)) + seq = 5; + if(ret != RETURN_TEMP_REMOVE_TO_FIELD) { + if(location != target->current.location) { + uint32_t resetflag = 0; + if(location & LOCATION_ONFIELD) + resetflag |= RESET_TOFIELD; + if(target->current.location & LOCATION_ONFIELD) + resetflag |= RESET_LEAVE; + effect* peffect = target->is_affected_by_effect(EFFECT_PRE_MONSTER); + if((location & LOCATION_ONFIELD) && (target->current.location & LOCATION_ONFIELD) + && !(peffect && (peffect->value & TYPE_TRAP)) && ret != 2) + resetflag |= RESET_MSCHANGE; + target->reset(resetflag, RESET_EVENT); + target->clear_card_target(); + } + } + if(!(target->current.location & LOCATION_ONFIELD)) + target->clear_relate_effect(); + if(ret == RETURN_TEMP_REMOVE_TO_FIELD) { + target->current.reason &= ~REASON_TEMPORARY; + } + if(ret == 0 && location != target->current.location || ret == RETURN_TEMP_REMOVE_TO_FIELD) { + target->set_status(STATUS_SUMMON_TURN, FALSE); + target->set_status(STATUS_FLIP_SUMMON_TURN, FALSE); + target->set_status(STATUS_SPSUMMON_TURN, FALSE); + target->set_status(STATUS_SET_TURN, FALSE); + target->set_status(STATUS_CANNOT_CHANGE_FORM, FALSE); + } + target->temp.sequence = seq; + if(location != LOCATION_MZONE) { + returns.ivalue[0] = positions; + return FALSE; + } + if(target->data.type & TYPE_LINK) { + returns.ivalue[0] = POS_FACEUP_ATTACK; + return FALSE; + } + add_process(PROCESSOR_SELECT_POSITION, 0, 0, 0, (positions << 16) + move_player, target->data.code); + return FALSE; + } + case 2: { + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + if(target->current.location == LOCATION_DECK) { + uint32_t curp = target->current.controler; + uint32_t curs = target->current.sequence; + if(curs > 0 && (curs == player[curp].list_main.size() - 1)) { + card* ptop = player[curp].list_main[curs - 1]; + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(curp); + pduel->write_buffer8(1); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + } + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(target->data.code); + pduel->write_buffer32(target->get_info_location()); + if(target->overlay_target) + target->overlay_target->xyz_remove(target); + move_card(playerid, target, location, target->temp.sequence, pzone); + target->current.position = returns.ivalue[0]; + target->set_status(STATUS_LEAVE_CONFIRMED, FALSE); + pduel->write_buffer32(target->get_public_info_location()); + pduel->write_buffer32(target->current.reason); + if(target->current.location != LOCATION_MZONE) { + if(target->equiping_cards.size()) { + destroy(target->equiping_cards, 0, REASON_LOST_TARGET + REASON_RULE, PLAYER_NONE); + for(auto csit = target->equiping_cards.begin(); csit != target->equiping_cards.end();) { + auto rm = csit++; + (*rm)->unequip(); + } + } + if(target->xyz_materials.size()) { + target->xyz_materials_previous_count_onfield = (int32_t)target->xyz_materials.size(); + card_set overlays(target->xyz_materials.begin(), target->xyz_materials.end()); + send_to(overlays, 0, REASON_LOST_OVERLAY + REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + } else { + target->xyz_materials_previous_count_onfield = 0; + } + } + if((target->previous.location == LOCATION_SZONE) && target->equiping_target) + target->unequip(); + if(target->current.location == LOCATION_MZONE) { + effect_set eset; + filter_player_effect(0, EFFECT_MUST_USE_MZONE, &eset, FALSE); + filter_player_effect(1, EFFECT_MUST_USE_MZONE, &eset, FALSE); + target->filter_effect(EFFECT_MUST_USE_MZONE, &eset); + for(effect_set::size_type i = 0; i < eset.size(); i++) { + effect* peffect = eset[i]; + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && peffect->count_limit == 0) + continue; + uint32_t value = 0x1f; + if(peffect->is_flag(EFFECT_FLAG_PLAYER_TARGET)) + value = peffect->get_value(); + else { + uint32_t lreason = (target->current.location == LOCATION_MZONE) ? LOCATION_REASON_CONTROL : LOCATION_REASON_TOFIELD; + pduel->lua->add_param(target->current.controler, PARAM_TYPE_INT); + pduel->lua->add_param(move_player, PARAM_TYPE_INT); + pduel->lua->add_param(lreason, PARAM_TYPE_INT); + value = peffect->get_value(target, 3); + } + if(peffect->get_handler_player() != target->current.controler) + value = value >> 16; + if(value & (0x1U << target->current.sequence)) { + peffect->dec_count(); + } + } + if(effect* teffect = target->is_affected_by_effect(EFFECT_PRE_MONSTER)) { + uint32_t type = teffect->value; + if(type & TYPE_TRAP) + type |= TYPE_TRAPMONSTER | target->data.type; + target->reset(EFFECT_PRE_MONSTER, RESET_CODE); + effect* peffect = pduel->new_effect(); + peffect->owner = target; + peffect->type = EFFECT_TYPE_SINGLE; + peffect->code = EFFECT_CHANGE_TYPE; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = RESET_EVENT + 0x1fc0000; + peffect->value = TYPE_MONSTER | type; + target->add_effect(peffect); + if(core.duel_rule <= NEW_MASTER_RULE && (type & TYPE_TRAPMONSTER)) { + peffect = pduel->new_effect(); + peffect->owner = target; + peffect->type = EFFECT_TYPE_FIELD; + peffect->range = LOCATION_MZONE; + peffect->code = EFFECT_USE_EXTRA_SZONE; + peffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE; + peffect->reset_flag = RESET_EVENT + 0x1fe0000; + peffect->value = 1 + (0x10000 << target->previous.sequence); + target->add_effect(peffect); + } + } + } + if(enable || ((ret == RETURN_TEMP_REMOVE_TO_FIELD) && target->is_position(POS_FACEUP))) + target->enable_field_effect(true); + if(ret == RETURN_TEMP_REMOVE_TO_FIELD && target->current.location == LOCATION_MZONE && !(target->data.type & TYPE_MONSTER)) + send_to(target, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, 0); + else { + if(target->previous.location == LOCATION_GRAVE) { + raise_single_event(target, 0, EVENT_LEAVE_GRAVE, target->current.reason_effect, target->current.reason, move_player, 0, 0); + raise_event(target, EVENT_LEAVE_GRAVE, target->current.reason_effect, target->current.reason, move_player, 0, 0); + } else if(target->previous.location == LOCATION_DECK || target->previous.location == LOCATION_EXTRA) { + raise_single_event(target, 0, EVENT_LEAVE_DECK, target->current.reason_effect, target->current.reason, move_player, 0, 0); + raise_event(target, EVENT_LEAVE_DECK, target->current.reason_effect, target->current.reason, move_player, 0, 0); + } + raise_single_event(target, 0, EVENT_MOVE, target->current.reason_effect, target->current.reason, target->current.reason_player, 0, 0); + raise_event(target, EVENT_MOVE, target->current.reason_effect, target->current.reason, target->current.reason_player, 0, 0); + process_single_event(); + process_instant_event(); + } + adjust_disable_check_list(); + return FALSE; + } + case 3: { + returns.ivalue[0] = TRUE; + return TRUE; + } + case 4: { + returns.ivalue[0] = FALSE; + return TRUE; + } + } + return TRUE; +} +int32_t field::change_position(uint16_t step, group * targets, effect * reason_effect, uint8_t reason_player, uint32_t enable) { + switch(step) { + case 0: { + for(auto cit = targets->container.begin(); cit != targets->container.end();) { + card* pcard = *cit; + uint8_t npos = pcard->position_param & 0xff; + uint8_t opos = pcard->current.position; + if ((pcard->current.location != LOCATION_MZONE && pcard->current.location != LOCATION_SZONE) + || (pcard->data.type & TYPE_LINK) + || pcard->get_status(STATUS_SUMMONING | STATUS_SPSUMMON_STEP) + || !pcard->is_affect_by_effect(reason_effect) || npos == opos + || (!(pcard->data.type & TYPE_TOKEN) && (opos & POS_FACEUP) && (npos & POS_FACEDOWN) && !pcard->is_capable_turn_set(reason_player)) + || (reason_effect && pcard->is_affected_by_effect(EFFECT_CANNOT_CHANGE_POS_E))) { + cit = targets->container.erase(cit); + } + else + ++cit; + } + card_set* to_grave_set = new card_set; + core.units.begin()->ptr1 = to_grave_set; + return FALSE; + } + case 1: { + card_set* to_grave_set = (card_set*)core.units.begin()->ptr1; + uint8_t playerid = reason_player; + if(core.units.begin()->value1) + playerid = 1 - reason_player; + card_set ssets; + for(auto& pcard : targets->container) { + uint8_t npos = pcard->position_param & 0xff; + uint8_t opos = pcard->current.position; + if((opos & POS_FACEUP) && (npos & POS_FACEDOWN)) { + if(pcard->get_type() & TYPE_TRAPMONSTER) { + if(pcard->current.controler == playerid) + ssets.insert(pcard); + } + } + } + if(ssets.size()) { + refresh_location_info_instant(); + int32_t fcount = get_useable_count(nullptr, playerid, LOCATION_SZONE, playerid, 0); + if(fcount <= 0) { + for(auto& pcard : ssets) { + to_grave_set->insert(pcard); + targets->container.erase(pcard); + } + core.units.begin()->step = 2; + } else if((int32_t)ssets.size() > fcount) { + core.select_cards.clear(); + for(auto& pcard : ssets) + core.select_cards.push_back(pcard); + uint32_t ct = (uint32_t)ssets.size() - fcount; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(reason_player); + pduel->write_buffer32(502); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, reason_player, ct + (ct << 16)); + } + } else + core.units.begin()->step = 2; + return FALSE; + } + case 2: { + card_set* to_grave_set = (card_set*)core.units.begin()->ptr1; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + to_grave_set->insert(pcard); + targets->container.erase(pcard); + } + return FALSE; + } + case 3: { + if(!core.units.begin()->value1) { + core.units.begin()->value1 = 1; + core.units.begin()->step = 0; + } + return FALSE; + } + case 4: { + card_set equipings; + card_set flips; + card_set ssets; + card_set pos_changed; + card_vector cv(targets->container.begin(), targets->container.end()); + if(cv.size() > 1) + std::sort(cv.begin(), cv.end(), card::card_operation_sort); + for(auto& pcard : cv) { + uint8_t npos = pcard->position_param & 0xff; + uint8_t opos = pcard->current.position; + uint8_t flag = pcard->position_param >> 16; + if((pcard->data.type & TYPE_TOKEN) && (npos & POS_FACEDOWN)) + npos = POS_FACEUP_DEFENSE; + pcard->previous.position = opos; + pcard->current.position = npos; + if((npos & POS_DEFENSE) && !pcard->is_affected_by_effect(EFFECT_DEFENSE_ATTACK)) + pcard->set_status(STATUS_ATTACK_CANCELED, TRUE); + pcard->set_status(STATUS_JUST_POS, TRUE); + pduel->write_buffer8(MSG_POS_CHANGE); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer8(pcard->previous.position); + pduel->write_buffer8(pcard->current.position); + core.hint_timing[pcard->current.controler] |= TIMING_POS_CHANGE; + if((opos & POS_FACEDOWN) && (npos & POS_FACEUP)) { + pcard->fieldid = infos.field_id++; + pcard->activate_count_id = pcard->fieldid; + if(check_unique_onfield(pcard, pcard->current.controler, pcard->current.location)) + pcard->unique_fieldid = UINT_MAX; + if(pcard->current.location == LOCATION_MZONE) { + raise_single_event(pcard, 0, EVENT_FLIP, reason_effect, 0, reason_player, 0, flag); + flips.insert(pcard); + } + if(enable) { + if(!reason_effect || !(reason_effect->type & EFFECT_TYPES_CHAIN_LINK) || pcard->current.location != LOCATION_MZONE) + pcard->enable_field_effect(true); + else + core.delayed_enable_set.insert(pcard); + } else + pcard->refresh_disable_status(); + } + if(pcard->current.location == LOCATION_MZONE) { + raise_single_event(pcard, 0, EVENT_CHANGE_POS, reason_effect, 0, reason_player, 0, 0); + pos_changed.insert(pcard); + } + bool trapmonster = false; + if((opos & POS_FACEUP) && (npos & POS_FACEDOWN)) { + if(pcard->get_type() & TYPE_TRAPMONSTER) + trapmonster = true; + if(pcard->status & (STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED)) + pcard->set_status(STATUS_SUMMON_DISABLED | STATUS_ACTIVATE_DISABLED, FALSE); + pcard->set_status(STATUS_FLIP_SUMMONING, FALSE); + pcard->set_status(STATUS_FLIP_SUMMON_DISABLED, FALSE); + pcard->reset(RESET_TURN_SET, RESET_EVENT); + pcard->clear_card_target(); + pcard->set_status(STATUS_SET_TURN, TRUE); + pcard->enable_field_effect(false); + pcard->previous.location = 0; + pcard->summon_info &= (DEFAULT_SUMMON_TYPE & ~SUMMON_TYPE_FLIP); + if ((pcard->summon_info & SUMMON_TYPE_PENDULUM) == SUMMON_TYPE_PENDULUM) + pcard->summon_info &= (SUMMON_VALUE_MAIN_TYPE | SUMMON_VALUE_CUSTOM_TYPE); + pcard->spsummon_counter[0] = pcard->spsummon_counter[1] = 0; + } + if((npos & POS_FACEDOWN) && pcard->equiping_cards.size()) { + for(auto csit = pcard->equiping_cards.begin(); csit != pcard->equiping_cards.end();) { + auto erm = csit++; + equipings.insert(*erm); + (*erm)->unequip(); + } + } + if((npos & POS_FACEDOWN) && pcard->equiping_target) + pcard->unequip(); + if(trapmonster) { + refresh_location_info_instant(); + move_to_field(pcard, pcard->current.controler, pcard->current.controler, LOCATION_SZONE, POS_FACEDOWN, FALSE, RETURN_TRAP_MONSTER_TO_SZONE); + raise_single_event(pcard, 0, EVENT_SSET, reason_effect, 0, reason_player, 0, 0); + ssets.insert(pcard); + } + } + adjust_instant(); + process_single_event(); + if(flips.size()) + raise_event(flips, EVENT_FLIP, reason_effect, 0, reason_player, 0, 0); + if(ssets.size()) { + for(auto& pcard : ssets) { + targets->container.erase(pcard); + } + raise_event(ssets, EVENT_SSET, reason_effect, 0, reason_player, 0, 0); + } + if(pos_changed.size()) + raise_event(pos_changed, EVENT_CHANGE_POS, reason_effect, 0, reason_player, 0, 0); + process_instant_event(); + if(equipings.size()) + destroy(equipings, 0, REASON_LOST_TARGET + REASON_RULE, PLAYER_NONE); + card_set* to_grave_set = (card_set*)core.units.begin()->ptr1; + if(to_grave_set->size()) { + send_to(*to_grave_set, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + } + delete to_grave_set; + return FALSE; + } + case 5: { + core.operated_set.clear(); + core.operated_set = targets->container; + returns.ivalue[0] = (int32_t)targets->container.size(); + pduel->delete_group(targets); + return TRUE; + } + } + return TRUE; +} +int32_t field::operation_replace(uint16_t step, effect* replace_effect, group* targets, card* target, int32_t is_destroy) { + switch (step) { + case 0: { + if(returns.ivalue[0]) + return TRUE; + if(!replace_effect->target) + return TRUE; + tevent e; + e.event_cards = targets; + e.event_player = replace_effect->get_handler_player(); + e.event_value = 0; + e.reason = target->current.reason; + e.reason_effect = target->current.reason_effect; + e.reason_player = target->current.reason_player; + if(!replace_effect->is_activateable(replace_effect->get_handler_player(), e)) + return TRUE; + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = replace_effect; + newchain.triggering_player = e.event_player; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + core.solving_event.push_front(e); + core.sub_solving_event.push_back(e); + core.continuous_chain.push_back(newchain); + add_process(PROCESSOR_EXECUTE_TARGET, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 1: { + if (returns.ivalue[0]) { + if(!(target->current.reason_effect && target->current.reason_effect->is_self_destroy_related())) { + targets->container.erase(target); + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + if(is_destroy) + core.destroy_canceled.insert(target); + } + replace_effect->dec_count(replace_effect->get_handler_player()); + } else + core.units.begin()->step = 2; + return FALSE; + } + case 2: { + if(!replace_effect->operation) + return FALSE; + core.sub_solving_event.push_back(*core.solving_event.begin()); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 3: { + if(core.continuous_chain.rbegin()->target_cards) + pduel->delete_group(core.continuous_chain.rbegin()->target_cards); + for(auto& oit : core.continuous_chain.rbegin()->opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + core.continuous_chain.pop_back(); + core.solving_event.pop_front(); + return TRUE; + } + case 5: { + tevent e; + e.event_cards = targets; + e.event_player = replace_effect->get_handler_player(); + e.event_value = 0; + if(targets->container.size() == 0) + return TRUE; + card* pc = *targets->container.begin(); + e.reason = pc->current.reason; + e.reason_effect = pc->current.reason_effect; + e.reason_player = pc->current.reason_player; + if(!replace_effect->is_activateable(replace_effect->get_handler_player(), e) || !replace_effect->value) + return TRUE; + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = replace_effect; + newchain.triggering_player = e.event_player; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + core.solving_event.push_front(e); + core.sub_solving_event.push_back(e); + core.continuous_chain.push_back(newchain); + add_process(PROCESSOR_EXECUTE_TARGET, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 6: { + if (returns.ivalue[0]) { + for (auto cit = targets->container.begin(); cit != targets->container.end();) { + if(replace_effect->get_value(*cit) && !((*cit)->current.reason_effect && (*cit)->current.reason_effect->is_self_destroy_related())) { + (*cit)->current.reason = (*cit)->temp.reason; + (*cit)->current.reason_effect = (*cit)->temp.reason_effect; + (*cit)->current.reason_player = (*cit)->temp.reason_player; + if(is_destroy) + core.destroy_canceled.insert(*cit); + cit = targets->container.erase(cit); + } + else + ++cit; + } + replace_effect->dec_count(replace_effect->get_handler_player()); + } else + core.units.begin()->step = 7; + return FALSE; + } + case 7: { + if(!replace_effect->operation) + return FALSE; + core.sub_solving_event.push_back(*core.solving_event.begin()); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 8: { + if(core.continuous_chain.rbegin()->target_cards) + pduel->delete_group(core.continuous_chain.rbegin()->target_cards); + for(auto& oit : core.continuous_chain.rbegin()->opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + core.continuous_chain.pop_back(); + core.solving_event.pop_front(); + return TRUE; + } + case 10: { + if(returns.ivalue[0]) + return TRUE; + if(!replace_effect->target) + return TRUE; + tevent e; + e.event_cards = targets; + e.event_player = replace_effect->get_handler_player(); + e.event_value = 0; + e.reason = target->current.reason; + e.reason_effect = target->current.reason_effect; + e.reason_player = target->current.reason_player; + if(!replace_effect->is_activateable(replace_effect->get_handler_player(), e)) + return TRUE; + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = replace_effect; + newchain.triggering_player = e.event_player; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + core.sub_solving_event.push_back(e); + core.continuous_chain.push_back(newchain); + add_process(PROCESSOR_EXECUTE_TARGET, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 11: { + if (returns.ivalue[0]) { + targets->container.erase(target); + target->current.reason = target->temp.reason; + target->current.reason_effect = target->temp.reason_effect; + target->current.reason_player = target->temp.reason_player; + if(is_destroy) + core.destroy_canceled.insert(target); + replace_effect->dec_count(replace_effect->get_handler_player()); + core.desrep_chain.push_back(core.continuous_chain.front()); + } + core.continuous_chain.pop_front(); + return TRUE; + } + case 12: { + tevent e; + e.event_cards = targets; + e.event_player = replace_effect->get_handler_player(); + e.event_value = 0; + if(targets->container.size() == 0) + return TRUE; + card* pc = *targets->container.begin(); + e.reason = pc->current.reason; + e.reason_effect = pc->current.reason_effect; + e.reason_player = pc->current.reason_player; + if(!replace_effect->is_activateable(replace_effect->get_handler_player(), e) || !replace_effect->value) + return TRUE; + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = replace_effect; + newchain.triggering_player = e.event_player; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + core.continuous_chain.push_back(newchain); + core.sub_solving_event.push_back(e); + add_process(PROCESSOR_EXECUTE_TARGET, 0, replace_effect, 0, replace_effect->get_handler_player(), 0); + return FALSE; + } + case 13: { + if (returns.ivalue[0]) { + for (auto cit = targets->container.begin(); cit != targets->container.end();) { + if (replace_effect->get_value(*cit)) { + (*cit)->current.reason = (*cit)->temp.reason; + (*cit)->current.reason_effect = (*cit)->temp.reason_effect; + (*cit)->current.reason_player = (*cit)->temp.reason_player; + if(is_destroy) + core.destroy_canceled.insert(*cit); + cit = targets->container.erase(cit); + } + else + ++cit; + } + replace_effect->dec_count(replace_effect->get_handler_player()); + core.desrep_chain.push_back(core.continuous_chain.front()); + } + core.continuous_chain.pop_front(); + return TRUE; + } + case 15: { + if(core.desrep_chain.size() == 0) + return TRUE; + core.continuous_chain.push_back(core.desrep_chain.front()); + core.desrep_chain.pop_front(); + effect* reffect = core.continuous_chain.back().triggering_effect; + if(!reffect->operation) + return FALSE; + core.sub_solving_event.push_back(core.continuous_chain.back().evt); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, reffect, 0, reffect->get_handler_player(), 0); + return FALSE; + } + case 16: { + if(core.continuous_chain.rbegin()->target_cards) + pduel->delete_group(core.continuous_chain.rbegin()->target_cards); + for(auto& oit : core.continuous_chain.rbegin()->opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + core.continuous_chain.pop_back(); + core.units.begin()->step = 14; + return FALSE; + } + } + return TRUE; +} +int32_t field::activate_effect(uint16_t step, effect* peffect) { + switch(step) { + case 0: { + card* phandler = peffect->get_handler(); + int32_t playerid = phandler->current.controler; + tevent test_event; + test_event.event_code = EVENT_FREE_CHAIN; + if(!peffect->is_activateable(playerid, test_event)) + return TRUE; + chain newchain; + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt.event_code = peffect->code; + newchain.evt.event_player = PLAYER_NONE; + newchain.evt.event_value = 0; + newchain.evt.event_cards = 0; + newchain.evt.reason = 0; + newchain.evt.reason_effect = 0; + newchain.evt.reason_player = PLAYER_NONE; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = playerid; + core.new_chains.push_back(newchain); + phandler->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(playerid); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - playerid); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + return FALSE; + } + case 1: { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + return TRUE; + } + } + return TRUE; +} +int32_t field::select_synchro_material(int16_t step, uint8_t playerid, card* pcard, int32_t min, int32_t max, card* smat, group* mg, int32_t filter1, int32_t filter2) { + switch(step) { + case 0: { + if(core.select_cards.size() == 0) + return TRUE; + if(core.summon_cancelable == FALSE && mg && min == max && mg->container.size() == min + 1) { + group* pgroup = pduel->new_group(); + pgroup->container.insert(mg->container.begin(), mg->container.end()); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + pduel->restore_assumes(); + core.limit_tuner = 0; + return TRUE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(512); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), 0x10001); + return FALSE; + } + case 1: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + core.limit_tuner = 0; + return TRUE; + } + card* tuner = core.select_cards[returns.bvalue[1]]; + effect* pcheck = tuner->is_affected_by_effect(EFFECT_SYNCHRO_CHECK); + if(pcheck) + pcheck->get_value(tuner); + core.limit_tuner = tuner; + effect* peffect; + if((peffect = tuner->is_affected_by_effect(EFFECT_SYNCHRO_MATERIAL_CUSTOM, pcard))) { + if(!peffect->operation) + return FALSE; + core.synchro_materials.clear(); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + pduel->lua->add_param(filter2, PARAM_TYPE_FUNCTION); + pduel->lua->add_param(min, PARAM_TYPE_INT); + pduel->lua->add_param(max, PARAM_TYPE_INT); + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, peffect, 0, playerid, 0); + } else { + core.units.begin()->step = 2; + } + return FALSE; + } + case 2: { + group* pgroup = pduel->new_group(core.synchro_materials); + pgroup->container.insert(core.limit_tuner); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + pduel->restore_assumes(); + core.limit_tuner = 0; + return TRUE; + } + case 3: { + card* tuner = core.limit_tuner; + effect* tuner_limit = tuner->is_affected_by_effect(EFFECT_TUNER_MATERIAL_LIMIT); + if(tuner_limit && tuner_limit->is_flag(EFFECT_FLAG_SPSUM_PARAM)) { + if(tuner_limit->s_range && tuner_limit->s_range > min) + min = tuner_limit->s_range; + if(tuner_limit->o_range && tuner_limit->o_range < max) + max = tuner_limit->o_range; + core.units.begin()->arg2 = min + (max << 16); + } + int32_t mzone_limit = get_mzone_limit(playerid, playerid, LOCATION_REASON_TOFIELD); + if(mzone_limit < 0) { + int32_t ft = -mzone_limit; + if(ft > min) + min = ft; + core.units.begin()->arg2 = min + (max << 16); + } + if(!smat) + return FALSE; + effect* pcheck = tuner->is_affected_by_effect(EFFECT_SYNCHRO_CHECK); + if(pcheck) + pcheck->get_value(smat); + if(min == 1 && max == 1) { + group* pgroup = pduel->new_group(); + pgroup->container.insert(tuner); + pgroup->container.insert(smat); + if(!check_must_material(pgroup, playerid, EFFECT_MUST_BE_SMATERIAL)) { + pduel->restore_assumes(); + core.limit_tuner = 0; + return TRUE; + } + int32_t lv = pcard->get_level(); + card_vector nsyn; + nsyn.push_back(tuner); + nsyn.push_back(smat); + tuner->sum_param = tuner->get_synchro_level(pcard); + smat->sum_param = smat->get_synchro_level(pcard); + if(check_with_sum_limit_m(nsyn, lv, 0, 0, 0, 0xffff, 2)) + core.units.begin()->step = 8; + } + return FALSE; + } + case 4: { + card* tuner = core.limit_tuner; + int32_t location = LOCATION_MZONE; + effect* tuner_limit = tuner->is_affected_by_effect(EFFECT_TUNER_MATERIAL_LIMIT); + if(tuner_limit) { + if(tuner_limit->value) + location = tuner_limit->value; + } + effect* pcheck = tuner->is_affected_by_effect(EFFECT_SYNCHRO_CHECK); + core.must_select_cards.clear(); + core.must_select_cards.push_back(tuner); + tuner->sum_param = tuner->get_synchro_level(pcard); + if(smat) { + core.must_select_cards.push_back(smat); + smat->sum_param = smat->get_synchro_level(pcard); + } + card_set must_list; + int32_t mct = get_must_material_list(playerid, EFFECT_MUST_BE_SMATERIAL, &must_list); + if(mct > 0) { + for(auto& mcard : must_list) { + if(mcard == tuner || mcard == smat) + continue; + core.must_select_cards.push_back(mcard); + mcard->sum_param = mcard->get_synchro_level(pcard); + } + } + card_vector nsyn(core.must_select_cards); + int32_t mcount = (int32_t)nsyn.size(); + if(mg) { + for(auto& pm : mg->container) { + if(pm == tuner || pm == smat || must_list.find(pm) != must_list.end() || !pm->is_can_be_synchro_material(pcard, tuner)) + continue; + if(tuner_limit) { + if(tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) + continue; + } + if(tuner_limit->value && !(pm->current.location & location)) + continue; + } + if(pcheck) + pcheck->get_value(pm); + if(pm->current.location == LOCATION_MZONE && !pm->is_position(POS_FACEUP)) + continue; + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if (!pduel->lua->check_condition(filter2, 2)) + continue; + nsyn.push_back(pm); + pm->sum_param = pm->get_synchro_level(pcard); + } + } else { + card_set cv; + get_synchro_material(playerid, &cv, tuner_limit); + for(auto& pm : cv) { + if(!pm || pm == tuner || pm == smat || must_list.find(pm) != must_list.end() || !pm->is_can_be_synchro_material(pcard, tuner)) + continue; + if(tuner_limit && tuner_limit->target) { + pduel->lua->add_param(tuner_limit, PARAM_TYPE_EFFECT); + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + if(!pduel->lua->get_function_value(tuner_limit->target, 2)) + continue; + } + if(pcheck) + pcheck->get_value(pm); + if(pm->current.location == LOCATION_MZONE && !pm->is_position(POS_FACEUP)) + continue; + pduel->lua->add_param(pm, PARAM_TYPE_CARD); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if (!pduel->lua->check_condition(filter2, 2)) + continue; + nsyn.push_back(pm); + pm->sum_param = pm->get_synchro_level(pcard); + } + } + int32_t lv = pcard->get_level(); + core.select_cards.clear(); + auto start = nsyn.begin() + mcount; + for(auto cit = start; cit != nsyn.end(); ++cit) { + card* pm = *cit; + if(start != cit) + std::iter_swap(start, cit); + if(check_other_synchro_material(nsyn, lv, min - mcount, max - mcount, mcount + 1)) + core.select_cards.push_back(pm); + if(start != cit) + std::iter_swap(start, cit); + } + return FALSE; + } + case 5: { + card* tuner = core.limit_tuner; + int32_t playerid = pcard->current.controler; + int32_t ct = get_spsummonable_count(pcard, playerid); + if(ct > 0) { + core.units.begin()->step = 6; + return FALSE; + } + card_set handover_zone_cards; + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, pcard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, pcard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + if(handover_zone_cards.find(tuner) != handover_zone_cards.end()) + ++ct; + if(smat && handover_zone_cards.find(smat) != handover_zone_cards.end()) + ++ct; + if(ct > 0) { + core.units.begin()->step = 6; + return FALSE; + } + card_vector* select_cards = new card_vector; + for(auto& pm : core.select_cards) { + if(handover_zone_cards.find(pm) != handover_zone_cards.end()) + select_cards->push_back(pm); + } + if(select_cards->size() == core.select_cards.size()) { + delete select_cards; + core.units.begin()->step = 6; + return FALSE; + } + select_cards->swap(core.select_cards); + card_vector* must_select_cards = new card_vector; + must_select_cards->swap(core.must_select_cards); + core.units.begin()->ptr3 = select_cards; + core.units.begin()->ptr4 = must_select_cards; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(512); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, 0x10001); + return FALSE; + } + case 6: { + card* pcard = core.select_cards[returns.bvalue[1]]; + card_vector* select_cards = (card_vector*)core.units.begin()->ptr3; + auto it = std::find(select_cards->begin(), select_cards->end(), pcard); + select_cards->erase(it); + card_vector* must_select_cards = (card_vector*)core.units.begin()->ptr4; + must_select_cards->push_back(pcard); + select_cards->swap(core.select_cards); + must_select_cards->swap(core.must_select_cards); + delete select_cards; + delete must_select_cards; + --min; + --max; + core.units.begin()->arg2 = min + (max << 16); + return FALSE; + } + case 7: { + int32_t lv = pcard->get_level(); + int32_t mcount = (int32_t)core.must_select_cards.size(); + if(core.global_flag & GLOBALFLAG_SCRAP_CHIMERA) { + effect* peffect = nullptr; + for(auto& pm : core.select_cards) { + peffect = pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA); + if(peffect) + break; + } + if(peffect) { + card_vector nsyn(core.must_select_cards); + nsyn.insert(nsyn.end(), core.select_cards.begin(), core.select_cards.end()); + card_vector nsyn_filtered; + for(auto& pm : nsyn) { + if(!peffect->get_value(pm)) + nsyn_filtered.push_back(pm); + } + if(nsyn_filtered.size() < nsyn.size()) { + card_vector nsyn_removed; + for(auto& pm : nsyn) { + if(!pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA)) + nsyn_removed.push_back(pm); + } + bool mfiltered = true; + bool mremoved = true; + int32_t mcount = (int32_t)core.must_select_cards.size(); + for(int32_t i = 0; i < mcount; ++i) { + if(peffect->get_value(nsyn[i])) + mfiltered = false; + if(nsyn[i]->is_affected_by_effect(EFFECT_SCRAP_CHIMERA)) + mremoved = false; + } + if(mfiltered && check_with_sum_limit_m(nsyn_filtered, lv, 0, min, max, 0xffff, mcount)) { + if(mremoved && check_with_sum_limit_m(nsyn_removed, lv, 0, min, max, 0xffff, mcount)) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, peffect->description); + core.units.begin()->step = 9; + return FALSE; + } else + core.select_cards.assign(nsyn_filtered.begin() + mcount, nsyn_filtered.end()); + } else + core.select_cards.assign(nsyn_removed.begin() + mcount, nsyn_removed.end()); + } + } + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(512); + add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, lv, playerid, min - (mcount - 1), max - (mcount - 1)); + return FALSE; + } + case 8: { + group* pgroup = pduel->new_group(); + int32_t mcount = (int32_t)core.must_select_cards.size(); + for(int32_t i = mcount; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + pgroup->container.insert(core.must_select_cards.begin(), core.must_select_cards.end()); + core.must_select_cards.clear(); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + pduel->restore_assumes(); + core.limit_tuner = 0; + return TRUE; + } + case 9: { + group* pgroup = pduel->new_group(); + pgroup->container.insert(core.limit_tuner); + pgroup->container.insert(smat); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + pduel->restore_assumes(); + core.limit_tuner = 0; + return TRUE; + } + case 10: { + int32_t lv = pcard->get_level(); + int32_t mcount = (int32_t)core.must_select_cards.size(); + if(returns.ivalue[0]) { + effect* peffect = nullptr; + for(auto& pm : core.select_cards) { + peffect = pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA); + if(peffect) + break; + } + card_vector nsyn_filtered; + for(auto& pm : core.select_cards) { + if(!peffect->get_value(pm)) + nsyn_filtered.push_back(pm); + } + core.select_cards.swap(nsyn_filtered); + } else { + card_vector nsyn_removed; + for(auto& pm : core.select_cards) { + if(!pm->is_affected_by_effect(EFFECT_SCRAP_CHIMERA)) + nsyn_removed.push_back(pm); + } + core.select_cards.swap(nsyn_removed); + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(512); + add_process(PROCESSOR_SELECT_SUM, 0, 0, 0, lv, playerid, min - (mcount - 1), max - (mcount - 1)); + core.units.begin()->step = 7; + return FALSE; + } + } + return TRUE; +} +int32_t field::select_xyz_material(int16_t step, uint8_t playerid, uint32_t lv, card* scard, int32_t min, int32_t max) { + switch(step) { + case 0: { + core.operated_set.clear(); + int32_t mzone_limit = get_mzone_limit(playerid, playerid, LOCATION_REASON_TOFIELD); + if(mzone_limit <= 0) { + int32_t ft = -mzone_limit + 1; + if(ft > min) { + min = ft; + core.units.begin()->arg2 = min + (max << 16); + } + } + card_set mcset; + int32_t mct = get_must_material_list(playerid, EFFECT_MUST_BE_XMATERIAL, &mcset); + core.select_cards.clear(); + for(auto& mcard : mcset) { + core.select_cards.push_back(mcard); + } + if(mct == 0) { + returns.ivalue[0] = 1; + core.units.begin()->step = 1; + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), mct + (mct << 16)); + return FALSE; + } + case 1: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + return TRUE; + } + int32_t pv = 0; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + core.operated_set.insert(pcard); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) { + if(iter->second == pcard) { + if(pv < iter->first) + pv = iter->first; + core.xmaterial_lst.erase(iter); + break; + } + } + } + max -= returns.bvalue[0]; + if(max == 0 || core.xmaterial_lst.size() == 0) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + min -= returns.bvalue[0]; + if(min < 0) + min = 0; + if(pv - (int32_t)core.operated_set.size() > min) + min = pv - (int32_t)core.operated_set.size(); + core.units.begin()->arg2 = min + (max << 16); + if(min == 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 93); + return FALSE; + } + returns.ivalue[0] = 1; + return FALSE; + } + case 2: { + if(!returns.ivalue[0]) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + if(!(core.global_flag & GLOBALFLAG_TUNE_MAGICIAN)) + return FALSE; + int32_t ct = get_spsummonable_count(scard, playerid); + card_set handover_zone_cards; + if(ct <= 0) { + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, scard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, scard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + } + for(auto& pcard : core.operated_set) { + effect* peffect = pcard->is_affected_by_effect(EFFECT_TUNE_MAGICIAN_X); + if(peffect) { + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end();) { + if(peffect->get_value(cit->second)) + cit = core.xmaterial_lst.erase(cit); + else + ++cit; + } + } + } + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end(); ++cit) + cit->second->sum_param = 0; + int32_t digit = 1; + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end();) { + card* pcard = cit->second; + effect* peffect = pcard->is_affected_by_effect(EFFECT_TUNE_MAGICIAN_X); + if(peffect) { + if(std::any_of(core.operated_set.begin(), core.operated_set.end(), + [=](card* pcard) { return !!peffect->get_value(pcard); })) { + cit = core.xmaterial_lst.erase(cit); + continue; + } + digit <<= 1; + for(auto mit = core.xmaterial_lst.begin(); mit != core.xmaterial_lst.end(); ++mit) { + if(!peffect->get_value(mit->second)) + mit->second->sum_param |= digit; + } + pcard->sum_param |= digit; + } else + pcard->sum_param |= 1; + ++cit; + } + int32_t selectable = 0; + std::multimap> mat; + for(int32_t icheck = 1; icheck <= digit; icheck <<= 1) { + mat.clear(); + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end(); ++cit) { + if(cit->second->sum_param & icheck) + mat.insert(*cit); + } + if(core.global_flag & GLOBALFLAG_XMAT_COUNT_LIMIT) { + int32_t maxc = std::min(max, (int32_t)mat.size()) + (int32_t)core.operated_set.size(); + auto iter = mat.lower_bound(maxc); + mat.erase(mat.begin(), iter); + } + if(ct <= 0) { + int32_t ft = ct; + for(auto cit = mat.begin(); cit != mat.end(); ++cit) { + card* pcard = cit->second; + if(handover_zone_cards.find(pcard) != handover_zone_cards.end()) + ++ft; + } + if(ft <= 0) + continue; + } + if((int32_t)mat.size() >= min) + selectable |= icheck; + } + int32_t acc = selectable; + for(auto cit = core.xmaterial_lst.begin(); cit != core.xmaterial_lst.end();) { + if(!(cit->second->sum_param & selectable)) + cit = core.xmaterial_lst.erase(cit); + else { + acc &= cit->second->sum_param; + ++cit; + } + } + if(acc) + return FALSE; + core.units.begin()->value1 = selectable; + core.units.begin()->step = 19; + return FALSE; + } + case 3: { + int32_t ct = get_spsummonable_count(scard, playerid); + if(ct > 0) { + returns.ivalue[0] = 1; + core.units.begin()->step = 4; + return FALSE; + } + card_set handover_zone_cards; + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, scard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, scard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + int32_t ft = ct + (int32_t)std::count_if(core.operated_set.begin(), core.operated_set.end(), + [=](card* pcard) { return handover_zone_cards.find(pcard) != handover_zone_cards.end(); }); + if(ft > 0) { + returns.ivalue[0] = 1; + core.units.begin()->step = 4; + return FALSE; + } + int32_t mmax = 0; + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) { + card* pcard = iter->second; + if(handover_zone_cards.find(pcard) != handover_zone_cards.end()) + core.select_cards.push_back(pcard); + else + ++mmax; + } + if(min > mmax) { + returns.ivalue[0] = 1; + core.units.begin()->step = 4; + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), 0x10001); + return FALSE; + } + case 4: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + return TRUE; + } + card* pcard = core.select_cards[returns.bvalue[1]]; + core.operated_set.insert(pcard); + int32_t pv = 0; + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) { + if(iter->second == pcard) { + if(pv < iter->first) + pv = iter->first; + core.xmaterial_lst.erase(iter); + break; + } + } + --max; + if(max == 0 || core.xmaterial_lst.size() == 0) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + if(min > 0) + --min; + if(pv - (int32_t)core.operated_set.size() > min) + min = pv - (int32_t)core.operated_set.size(); + core.units.begin()->arg2 = min + (max << 16); + if(min == 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 93); + return FALSE; + } + returns.ivalue[0] = 1; + return FALSE; + } + case 5: { + if(!returns.ivalue[0]) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + int32_t maxv = 0; + if(core.xmaterial_lst.size()) + maxv = core.xmaterial_lst.begin()->first; + if(min >= maxv) { + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) + core.select_cards.push_back(iter->second); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), min + (max << 16)); + } else + core.units.begin()->step = 6; + return FALSE; + } + case 6: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + return TRUE; + } + group* pgroup = pduel->new_group(core.operated_set); + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + case 7: { + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) + core.select_cards.push_back(iter->second); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), min + (min << 16)); + return FALSE; + } + case 8: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + return TRUE; + } + int32_t pv = 0; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + core.operated_set.insert(pcard); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) { + if(iter->second == pcard) { + if(pv < iter->first) + pv = iter->first; + core.xmaterial_lst.erase(iter); + break; + } + } + } + max -= returns.bvalue[0]; + if(max == 0 || core.xmaterial_lst.size() == 0) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + min = 0; + if(pv - (int32_t)core.operated_set.size() > min) + min = pv - (int32_t)core.operated_set.size(); + core.units.begin()->arg2 = min + (max << 16); + if(min == 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 93); + return FALSE; + } + returns.ivalue[0] = 1; + return FALSE; + } + case 9: { + if(!returns.ivalue[0]) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) + core.select_cards.push_back(iter->second); + int32_t maxv = core.xmaterial_lst.begin()->first; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + if(min == 0) + min = 1; + if(min + (int32_t)core.operated_set.size() >= maxv) + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (max << 16)); + else { + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid, min + (min << 16)); + core.units.begin()->step = 7; + } + return FALSE; + } + case 10: { + group* pgroup = pduel->new_group(core.operated_set); + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + pgroup->container.insert(pcard); + } + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + case 20: { + int32_t ct = get_spsummonable_count(scard, playerid); + if(ct > 0) + return FALSE; + card_set handover_zone_cards; + uint32_t must_use_zone_flag = 0; + filter_must_use_mzone(playerid, playerid, LOCATION_REASON_TOFIELD, scard, &must_use_zone_flag); + uint32_t handover_zone = get_rule_zone_fromex(playerid, scard) & ~must_use_zone_flag; + get_cards_in_zone(&handover_zone_cards, handover_zone, playerid, LOCATION_MZONE); + int32_t ft = ct + (int32_t)std::count_if(core.operated_set.begin(), core.operated_set.end(), + [=](card* pcard) { return handover_zone_cards.find(pcard) != handover_zone_cards.end(); }); + if(ft > 0) + return FALSE; + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) { + card* pcard = iter->second; + if(handover_zone_cards.find(pcard) != handover_zone_cards.end()) + core.select_cards.push_back(pcard); + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), 0x10001); + core.units.begin()->step = 21; + return FALSE; + } + case 21: { + core.select_cards.clear(); + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end(); ++iter) + core.select_cards.push_back(iter->second); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(513); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, playerid + ((uint32_t)core.summon_cancelable << 16), 0x10001); + return FALSE; + } + case 22: { + if(returns.ivalue[0] == -1) { + pduel->lua->add_param(nullptr, PARAM_TYPE_GROUP); + return TRUE; + } + int32_t pv = 0; + card* pcard = core.select_cards[returns.bvalue[1]]; + core.operated_set.insert(pcard); + int32_t selectable = core.units.begin()->value1 & pcard->sum_param; + for(auto iter = core.xmaterial_lst.begin(); iter != core.xmaterial_lst.end();) { + if(iter->second == pcard) { + pv = iter->first; + iter = core.xmaterial_lst.erase(iter); + } else if(!(iter->second->sum_param & selectable)) + iter = core.xmaterial_lst.erase(iter); + else + ++iter; + } + --max; + if(max == 0 || core.xmaterial_lst.size() == 0) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + if(min > 0) + --min; + if(pv - (int32_t)core.operated_set.size() > min) + min = pv - (int32_t)core.operated_set.size(); + core.units.begin()->arg2 = min + (max << 16); + core.units.begin()->value1 = selectable; + if(min == 0) { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 93); + return FALSE; + } + core.units.begin()->step = 20; + return FALSE; + } + case 23: { + if(!returns.ivalue[0]) { + group* pgroup = pduel->new_group(core.operated_set); + pduel->lua->add_param(pgroup, PARAM_TYPE_GROUP); + return TRUE; + } + core.units.begin()->step = 20; + return FALSE; + } + } + return TRUE; +} +int32_t field::select_release_cards(int16_t step, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max) { + switch(step) { + case 0: { + core.operated_set.clear(); + if(core.release_cards_ex.empty()) { + core.units.begin()->step = 1; + return FALSE; + } + core.select_cards.clear(); + for(auto& pcard : core.release_cards_ex) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + return FALSE; + } + case 1: { + int32_t count = returns.bvalue[0]; + max -= count; + if(max == 0 || core.release_cards.size() + core.release_cards_ex_oneof.size() == 0) + return TRUE; + for(int32_t i = 0; i < count; ++i) + core.operated_set.insert(core.select_cards[returns.bvalue[i + 1]]); + min -= count; + if(min < 0) + min = 0; + core.units.begin()->arg2 = (max << 16) + min; + return FALSE; + } + case 2: { + if(!core.release_cards_ex_oneof.empty()) { + if((int32_t)core.release_cards.size() < min) + returns.ivalue[0] = TRUE; + else + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 98); + } else + returns.ivalue[0] = FALSE; + return FALSE; + } + case 3: { + if(!returns.ivalue[0]) { + core.units.begin()->step = 4; + return FALSE; + } + core.select_cards.clear(); + for(auto& pcard : core.release_cards_ex_oneof) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, 0x10001); + return FALSE; + } + case 4: { + card* pcard = core.select_cards[returns.bvalue[1]]; + core.operated_set.insert(pcard); + effect* peffect = pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE_NONSUM); + core.dec_count_reserve.push_back(peffect); + --max; + if(max == 0 || core.release_cards.empty()) { + core.units.begin()->step = 6; + return FALSE; + } + --min; + if(min < 0) + min = 0; + core.units.begin()->arg2 = (max << 16) + min; + return FALSE; + } + case 5: { + core.select_cards.clear(); + for(auto& pcard : core.release_cards) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + return FALSE; + } + case 6: { + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + core.operated_set.insert(core.select_cards[returns.bvalue[i + 1]]); + return FALSE; + } + case 7: { + core.select_cards.clear(); + returns.bvalue[0] = (uint8_t)core.operated_set.size(); + int32_t i = 0; + for(auto cit = core.operated_set.begin(); cit != core.operated_set.end(); ++cit, ++i) { + core.select_cards.push_back(*cit); + returns.bvalue[i + 1] = i; + } + return TRUE; + } + } + return TRUE; +} +int32_t field::select_tribute_cards(int16_t step, card* target, uint8_t playerid, uint8_t cancelable, int32_t min, int32_t max, uint8_t toplayer, uint32_t zone) { + switch(step) { + case 0: { + core.operated_set.clear(); + zone &= 0x1f; + int32_t ct = get_tofield_count(target, toplayer, LOCATION_MZONE, playerid, LOCATION_REASON_TOFIELD, zone); + if(ct > 0) { + returns.ivalue[0] = TRUE; + core.units.begin()->step = 1; + return FALSE; + } + int32_t rmax = 0; + core.select_cards.clear(); + for(auto& pcard : core.release_cards) { + if(pcard->current.location == LOCATION_MZONE && pcard->current.controler == toplayer && ((zone >> pcard->current.sequence) & 1)) + core.select_cards.push_back(pcard); + else + rmax += pcard->release_param; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + if(core.release_cards_ex.empty() && core.release_cards_ex_oneof.empty() && min > rmax) { + if(rmax > 0) { + core.select_cards.clear(); + for(auto& pcard : core.release_cards) + core.select_cards.push_back(pcard); + } + add_process(PROCESSOR_SELECT_TRIBUTE_P, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + return TRUE; + } + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, 0x10001); + return FALSE; + } + case 1: { + if(returns.ivalue[0] == -1) + return TRUE; + card* pcard = core.select_cards[returns.bvalue[1]]; + core.operated_set.insert(pcard); + core.release_cards.erase(pcard); + if(min <= (int32_t)pcard->release_param) { + if(max > 1 && core.release_cards_ex.empty() && (!core.release_cards.empty() || !core.release_cards_ex_oneof.empty())) + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 210); + else if(max > 1 && !core.release_cards_ex.empty()) + returns.ivalue[0] = TRUE; + else + core.units.begin()->step = 8; + } else + returns.ivalue[0] = TRUE; + return FALSE; + } + case 2: { + if(!returns.ivalue[0]) { + core.units.begin()->step = 8; + return FALSE; + } + int32_t fcount = get_mzone_limit(toplayer, playerid, LOCATION_REASON_TOFIELD); + if(!core.operated_set.empty()) { + min -= (*core.operated_set.begin())->release_param; + --max; + ++fcount; + } + if(core.release_cards_ex.size() + core.release_cards_ex_oneof.size() == 0 + || (fcount <= 0 && min < 2)) { + core.select_cards.clear(); + for(auto& pcard : core.release_cards) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_TRIBUTE_P, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + core.units.begin()->step = 7; + return FALSE; + } + if(core.release_cards_ex.size() >= (uint32_t)max) { + core.select_cards.clear(); + for(auto& pcard : core.release_cards_ex) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_TRIBUTE_P, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + core.units.begin()->step = 7; + return FALSE; + } + int32_t rmax = 0; + for(auto& pcard : core.release_cards) + rmax += pcard->release_param; + for(auto& pcard : core.release_cards_ex) + rmax += pcard->release_param; + core.temp_var[0] = 0; + if(rmax < min) { + returns.ivalue[0] = TRUE; + if(rmax == 0 && min == 2) + core.temp_var[0] = 1; + } else if(!core.release_cards_ex_oneof.empty()) + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 92); + else + core.units.begin()->step = 4; + return FALSE; + } + case 3: { + if(!returns.ivalue[0]) { + core.units.begin()->step = 4; + return FALSE; + } + core.select_cards.clear(); + if(core.temp_var[0] == 0) + for(auto& pcard : core.release_cards_ex_oneof) + core.select_cards.push_back(pcard); + else + for(auto& pcard : core.release_cards_ex_oneof) + if(pcard->release_param == 2) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, 0x10001); + return FALSE; + } + case 4: { + if(returns.ivalue[0] == -1) + return TRUE; + card* pcard = core.select_cards[returns.bvalue[1]]; + core.operated_set.insert(pcard); + core.units.begin()->peffect = pcard->is_affected_by_effect(EFFECT_EXTRA_RELEASE_SUM); + return FALSE; + } + case 5: { + core.select_cards.clear(); + for(auto& pcard : core.release_cards_ex) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, ((uint32_t)core.release_cards_ex.size() << 16) | (uint32_t)core.release_cards_ex.size()); + return FALSE; + } + case 6: { + if(returns.ivalue[0] == -1) + return TRUE; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + core.operated_set.insert(core.select_cards[returns.bvalue[i + 1]]); + uint32_t rmin = (uint32_t)core.operated_set.size(); + uint32_t rmax = 0; + for(auto& pcard : core.operated_set) + rmax += pcard->release_param; + min -= rmax; + max -= rmin; + min = min > 0 ? min : 0; + max = max > 0 ? max : 0; + core.units.begin()->arg2 = (max << 16) + min; + if(min <= 0) { + if(max > 0 && !core.release_cards.empty()) + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, playerid, 210); + else + core.units.begin()->step = 8; + } else + returns.ivalue[0] = TRUE; + return FALSE; + } + case 7: { + if(!returns.ivalue[0]) { + core.units.begin()->step = 8; + return FALSE; + } + core.select_cards.clear(); + for(auto& pcard : core.release_cards) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(playerid); + pduel->write_buffer32(500); + add_process(PROCESSOR_SELECT_TRIBUTE_P, 0, 0, 0, ((uint32_t)cancelable << 16) + playerid, (max << 16) + min); + return FALSE; + } + case 8: { + if(returns.ivalue[0] == -1) + return TRUE; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + core.operated_set.insert(core.select_cards[returns.bvalue[i + 1]]); + return FALSE; + } + case 9: { + core.select_cards.clear(); + returns.bvalue[0] = (uint8_t)core.operated_set.size(); + int32_t i = 0; + for(auto cit = core.operated_set.begin(); cit != core.operated_set.end(); ++cit, ++i) { + core.select_cards.push_back(*cit); + returns.bvalue[i + 1] = i; + } + effect* peffect = core.units.begin()->peffect; + if(peffect) + peffect->dec_count(); + return TRUE; + } + } + return TRUE; +} +int32_t field::toss_coin(uint16_t step, effect * reason_effect, uint8_t reason_player, uint8_t playerid, int32_t count) { + if (count == 0 || count < -1) + return TRUE; + if (count > MAX_COIN_COUNT) + count = MAX_COIN_COUNT; + switch(step) { + case 0: { + effect_set eset; + effect* peffect = nullptr; + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = count; + e.reason = 0; + e.reason_effect = reason_effect; + e.reason_player = reason_player; + core.coin_result = 0; + auto pr = effects.continuous_effect.equal_range(EFFECT_TOSS_COIN_REPLACE); + for(auto eit = pr.first; eit != pr.second;) { + effect* pe = eit->second; + ++eit; + if(pe->is_activateable(pe->get_handler_player(), e)) { + peffect = pe; + break; + } + } + if(!peffect) { + auto gen_bits = [&](int32_t n) -> uint32_t { + uint32_t mask = (1u << n) - 1u; + return (uint32_t)pduel->get_next_integer(0, (int32_t)mask); + }; + + if (count > 0) { + pduel->write_buffer8(MSG_TOSS_COIN); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)count); + core.coin_count = count; + core.coin_result = gen_bits(count); + for (int32_t i = 0; i < count; ++i) { + pduel->write_buffer8((uint8_t)((core.coin_result >> i) & 1u)); + } + } + else if (count == -1) { + core.coin_count = 0; + uint32_t bits = gen_bits(MAX_COIN_COUNT); + int32_t first_zero = -1; + for (int32_t i = 0; i < MAX_COIN_COUNT; ++i) { + if (((bits >> i) & 1u) == 0u) { + first_zero = i; + break; + } + } + core.coin_count = (first_zero == -1) ? MAX_COIN_COUNT : (first_zero + 1); + core.coin_result = bits; + pduel->write_buffer8(MSG_TOSS_COIN); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.coin_count); + for (int32_t i = 0; i < core.coin_count; ++i) { + pduel->write_buffer8((uint8_t)((core.coin_result >> i) & 1u)); + } + } + raise_event(nullptr, EVENT_TOSS_COIN_NEGATE, reason_effect, 0, reason_player, playerid, count); + process_instant_event(); + } else { + solve_continuous(peffect->get_handler_player(), peffect, e); + //call Duel.SetCoinResult in operation if necessary + return TRUE; + } + return FALSE; + } + case 1: { + raise_event(nullptr, EVENT_TOSS_COIN, reason_effect, 0, reason_player, playerid, core.coin_count); + process_instant_event(); + return TRUE; + } + } + return TRUE; +} +int32_t field::toss_dice(uint16_t step, effect * reason_effect, uint8_t reason_player, uint8_t playerid, uint8_t count1, uint8_t count2) { + switch(step) { + case 0: { + effect_set eset; + effect* peffect = nullptr; + tevent e; + e.event_cards = 0; + e.event_player = playerid; + e.event_value = count1 + (count2 << 16); + e.reason = 0; + e.reason_effect = reason_effect; + e.reason_player = reason_player; + memset(core.dice_result, 0, sizeof(core.dice_result)); + auto pr = effects.continuous_effect.equal_range(EFFECT_TOSS_DICE_REPLACE); + for(auto eit = pr.first; eit != pr.second;) { + effect* pe = eit->second; + ++eit; + if(pe->is_activateable(pe->get_handler_player(), e)) { + peffect = pe; + break; + } + } + if(!peffect) { + pduel->write_buffer8(MSG_TOSS_DICE); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count1); + for(int32_t i = 0; i < count1; ++i) { + core.dice_result[i] = pduel->get_next_integer(1, 6); + pduel->write_buffer8(core.dice_result[i]); + } + if(count2 > 0) { + pduel->write_buffer8(MSG_TOSS_DICE); + pduel->write_buffer8(1 - playerid); + pduel->write_buffer8(count2); + for(int32_t i = 0; i < count2; ++i) { + core.dice_result[count1 + i] = pduel->get_next_integer(1, 6); + pduel->write_buffer8(core.dice_result[count1 + i]); + } + } + raise_event(nullptr, EVENT_TOSS_DICE_NEGATE, reason_effect, 0, reason_player, playerid, count1 + (count2 << 16)); + process_instant_event(); + } else { + solve_continuous(peffect->get_handler_player(), peffect, e); + //call Duel.SetDiceResult in operation if necessary + return TRUE; + } + return FALSE; + } + case 1: { + raise_event(nullptr, EVENT_TOSS_DICE, reason_effect, 0, reason_player, playerid, count1 + (count2 << 16)); + process_instant_event(); + return TRUE; + } + } + return TRUE; +} diff --git a/core/playerop.cpp b/core/playerop.cpp new file mode 100644 index 0000000..11ff71f --- /dev/null +++ b/core/playerop.cpp @@ -0,0 +1,1109 @@ +/* + * playerop.cpp + * + * Created on: 2010-12-22 + * Author: Argon + */ + +#include "field.h" +#include "duel.h" +#include "effect.h" +#include "card.h" +#include "ocgapi.h" + +#include +#include + +bool field::check_response(size_t vector_size, int32_t min_len, int32_t max_len) const { + const int32_t len = returns.bvalue[0]; + if (len < min_len || len > max_len) + return false; + std::set index_set; + for (int32_t i = 0; i < len; ++i) { + uint8_t index = returns.bvalue[1 + i]; + if (index >= (int32_t)vector_size || index_set.count(index)) { + return false; + } + index_set.insert(index); + } + return true; +} +int32_t field::select_battle_command(uint16_t step, uint8_t playerid) { + if(step == 0) { + pduel->write_buffer8(MSG_SELECT_BATTLECMD); + pduel->write_buffer8(playerid); + //Activatable + pduel->write_buffer8((uint8_t)core.select_chains.size()); + std::sort(core.select_chains.begin(), core.select_chains.end(), chain::chain_operation_sort); + for(const auto& ch : core.select_chains) { + effect* peffect = ch.triggering_effect; + card* pcard = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + pduel->write_buffer32(pcard->data.code); + else + pduel->write_buffer32(pcard->data.code | 0x80000000); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer32(peffect->description); + } + //Attackable + pduel->write_buffer8((uint8_t)core.attackable_cards.size()); + for(auto& pcard : core.attackable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer8(pcard->direct_attackable); + } + //M2, EP + if(core.to_m2) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + if(core.to_ep) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + return FALSE; + } else { + uint32_t t = (uint32_t)returns.ivalue[0] & 0xffff; + uint32_t s = (uint32_t)returns.ivalue[0] >> 16; + if(t > 3 + || (t == 0 && s >= (uint32_t)core.select_chains.size()) + || (t == 1 && s >= (uint32_t)core.attackable_cards.size()) + || (t == 2 && !core.to_m2) + || (t == 3 && !core.to_ep)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_idle_command(uint16_t step, uint8_t playerid) { + if(step == 0) { + pduel->write_buffer8(MSG_SELECT_IDLECMD); + pduel->write_buffer8(playerid); + //idle summon + pduel->write_buffer8((uint8_t)core.summonable_cards.size()); + for(auto& pcard : core.summonable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + //idle spsummon + pduel->write_buffer8((uint8_t)core.spsummonable_cards.size()); + for(auto& pcard : core.spsummonable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + //idle pos change + pduel->write_buffer8((uint8_t)core.repositionable_cards.size()); + for(auto& pcard : core.repositionable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + //idle mset + pduel->write_buffer8((uint8_t)core.msetable_cards.size()); + for(auto& pcard : core.msetable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + //idle sset + pduel->write_buffer8((uint8_t)core.ssetable_cards.size()); + for(auto& pcard : core.ssetable_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + //idle activate + pduel->write_buffer8((uint8_t)core.select_chains.size()); + std::sort(core.select_chains.begin(), core.select_chains.end(), chain::chain_operation_sort); + for(const auto& ch : core.select_chains) { + effect* peffect = ch.triggering_effect; + card* pcard = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + pduel->write_buffer32(pcard->data.code); + else + pduel->write_buffer32(pcard->data.code | 0x80000000); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + pduel->write_buffer32(peffect->description); + } + //To BP + if(infos.phase == PHASE_MAIN1 && core.to_bp) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + if(core.to_ep) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + if(infos.can_shuffle && player[playerid].list_hand.size() > 1) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + return FALSE; + } else { + int32_t t = (uint32_t)returns.ivalue[0] & 0xffff; + int32_t s = (uint32_t)returns.ivalue[0] >> 16; + if(t < 0 || t > 8 || s < 0 + || (t == 0 && s >= (int32_t)core.summonable_cards.size()) + || (t == 1 && s >= (int32_t)core.spsummonable_cards.size()) + || (t == 2 && s >= (int32_t)core.repositionable_cards.size()) + || (t == 3 && s >= (int32_t)core.msetable_cards.size()) + || (t == 4 && s >= (int32_t)core.ssetable_cards.size()) + || (t == 5 && s >= (int32_t)core.select_chains.size()) + || (t == 6 && (infos.phase != PHASE_MAIN1 || !core.to_bp)) + || (t == 7 && !core.to_ep) + || (t == 8 && !(infos.can_shuffle && (int32_t)player[playerid].list_hand.size() > 1))) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_effect_yes_no(uint16_t step, uint8_t playerid, uint32_t description, card* pcard) { + if(step == 0) { + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.ivalue[0] = 1; + return TRUE; + } + pduel->write_buffer8(MSG_SELECT_EFFECTYN); + pduel->write_buffer8(playerid); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(description); + returns.ivalue[0] = -1; + return FALSE; + } else { + if(returns.ivalue[0] != 0 && returns.ivalue[0] != 1) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_yes_no(uint16_t step, uint8_t playerid, uint32_t description) { + if(step == 0) { + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.ivalue[0] = 1; + return TRUE; + } + pduel->write_buffer8(MSG_SELECT_YESNO); + pduel->write_buffer8(playerid); + pduel->write_buffer32(description); + returns.ivalue[0] = -1; + return FALSE; + } else { + if(returns.ivalue[0] != 0 && returns.ivalue[0] != 1) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_option(uint16_t step, uint8_t playerid) { + if(step == 0) { + returns.ivalue[0] = -1; + if(core.select_options.size() == 0) + return TRUE; + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.ivalue[0] = 0; + return TRUE; + } + pduel->write_buffer8(MSG_SELECT_OPTION); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.select_options.size()); + for(auto& option : core.select_options) + pduel->write_buffer32(option); + return FALSE; + } else { + if(returns.ivalue[0] < 0 || returns.ivalue[0] >= (int32_t)core.select_options.size()) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_card(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max) { + if(step == 0) { + returns.bvalue[0] = 0; + if(max == 0 || core.select_cards.empty()) + return TRUE; + core.selecting_player = playerid; + std::sort(core.select_cards.begin(), core.select_cards.end(), card::card_operation_sort); + if (core.select_cards.size() > UINT8_MAX) + core.select_cards.resize(UINT8_MAX); + if(max > core.select_cards.size()) + max = (uint8_t)core.select_cards.size(); + if(min > max) + min = max; + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.bvalue[0] = min; + for(uint8_t i = 0; i < min; ++i) + returns.bvalue[i + 1] = i; + return TRUE; + } + core.units.begin()->arg2 = ((uint32_t)min) + (((uint32_t)max) << 16); + pduel->write_buffer8(MSG_SELECT_CARD); + pduel->write_buffer8(playerid); + pduel->write_buffer8(cancelable); + pduel->write_buffer8(min); + pduel->write_buffer8(max); + pduel->write_buffer8((uint8_t)core.select_cards.size()); + uint8_t deck_seq = 0; + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_select_info_location(&deck_seq)); + } + return FALSE; + } else { + if (returns.ivalue[0] == -1) { + if (cancelable) + return TRUE; + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + if (!check_response(core.select_cards.size(), min, max)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_unselect_card(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max, uint8_t finishable) { + if(step == 0) { + returns.bvalue[0] = 0; + if(core.select_cards.empty() && core.unselect_cards.empty()) + return TRUE; + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.bvalue[0] = 1; + for(uint8_t i = 0; i < 1; ++i) + returns.bvalue[i + 1] = i; + return TRUE; + } + core.selecting_player = playerid; + std::sort(core.select_cards.begin(), core.select_cards.end(), card::card_operation_sort); + if (core.select_cards.size() > UINT8_MAX) + core.select_cards.resize(UINT8_MAX); + if (core.unselect_cards.size() > UINT8_MAX) + core.unselect_cards.resize(UINT8_MAX); + pduel->write_buffer8(MSG_SELECT_UNSELECT_CARD); + pduel->write_buffer8(playerid); + pduel->write_buffer8(finishable); + pduel->write_buffer8(cancelable); + pduel->write_buffer8(min); + pduel->write_buffer8(max); + pduel->write_buffer8((uint8_t)core.select_cards.size()); + uint8_t deck_seq = 0; + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_select_info_location(&deck_seq)); + } + pduel->write_buffer8((uint8_t)core.unselect_cards.size()); + for(auto& pcard : core.unselect_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_select_info_location(&deck_seq)); + } + return FALSE; + } else { + if(returns.ivalue[0] == -1) { + if(cancelable || finishable) + return TRUE; + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + if (!check_response(core.select_cards.size() + core.unselect_cards.size(), 1, 1)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_chain(uint16_t step, uint8_t playerid, uint8_t spe_count) { + if(step == 0) { + returns.ivalue[0] = -1; + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + if(core.select_chains.size() == 0) + returns.ivalue[0] = -1; + else { + bool act = true; + for(const auto& ch : core.current_chain) + if(ch.triggering_player == 1) + act = false; + for(const auto& ch : core.select_chains) + if(ch.flag & CHAIN_FORCED) + act = true; + if(act) + returns.ivalue[0] = 0; + else + returns.ivalue[0] = -1; + } + return TRUE; + } + pduel->write_buffer8(MSG_SELECT_CHAIN); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.select_chains.size()); + pduel->write_buffer8(spe_count); + pduel->write_buffer32(pduel->game_field->core.hint_timing[playerid]); + pduel->write_buffer32(pduel->game_field->core.hint_timing[1 - playerid]); + std::sort(core.select_chains.begin(), core.select_chains.end(), chain::chain_operation_sort); + for(const auto& ch : core.select_chains) { + effect* peffect = ch.triggering_effect; + card* pcard = peffect->get_handler(); + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + pduel->write_buffer8(EDESC_OPERATION); + else if(!(peffect->type & EFFECT_TYPE_ACTIONS)) + pduel->write_buffer8(EDESC_RESET); + else + pduel->write_buffer8(0); + if(ch.flag & CHAIN_FORCED) + pduel->write_buffer8(1); + else + pduel->write_buffer8(0); + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(peffect->description); + } + return FALSE; + } else { + if(returns.ivalue[0] == -1) { + for(const auto& ch : core.select_chains) { + if(ch.flag & CHAIN_FORCED) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + } + return TRUE; + } + if(returns.ivalue[0] < 0 || returns.ivalue[0] >= (int32_t)core.select_chains.size()) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_place(uint16_t step, uint8_t playerid, uint32_t flag, uint8_t count) { + if(step == 0) { + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + flag = ~flag; + uint32_t filter; + int32_t pzone = 0; + if(flag & 0x7f) { + returns.bvalue[0] = 1; + returns.bvalue[1] = LOCATION_MZONE; + filter = flag & 0x7f; + } else if(flag & 0x1f00) { + returns.bvalue[0] = 1; + returns.bvalue[1] = LOCATION_SZONE; + filter = (flag >> 8) & 0x1f; + } else if(flag & 0xc000) { + returns.bvalue[0] = 1; + returns.bvalue[1] = LOCATION_SZONE; + filter = (flag >> 14) & 0x3; + pzone = 1; + } else if(flag & 0x7f0000) { + returns.bvalue[0] = 0; + returns.bvalue[1] = LOCATION_MZONE; + filter = (flag >> 16) & 0x7f; + } else if(flag & 0x1f000000) { + returns.bvalue[0] = 0; + returns.bvalue[1] = LOCATION_SZONE; + filter = (flag >> 24) & 0x1f; + } else { + returns.bvalue[0] = 0; + returns.bvalue[1] = LOCATION_SZONE; + filter = (flag >> 30) & 0x3; + pzone = 1; + } + if(!pzone) { + if(filter & 0x40) returns.bvalue[2] = 6; + else if(filter & 0x20) returns.bvalue[2] = 5; + else if(filter & 0x4) returns.bvalue[2] = 2; + else if(filter & 0x2) returns.bvalue[2] = 1; + else if(filter & 0x8) returns.bvalue[2] = 3; + else if(filter & 0x1) returns.bvalue[2] = 0; + else if(filter & 0x10) returns.bvalue[2] = 4; + } else { + if(filter & 0x1) returns.bvalue[2] = 6; + else if(filter & 0x2) returns.bvalue[2] = 7; + } + return TRUE; + } + if(core.units.begin()->type == PROCESSOR_SELECT_PLACE) + pduel->write_buffer8(MSG_SELECT_PLACE); + else + pduel->write_buffer8(MSG_SELECT_DISFIELD); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + pduel->write_buffer32(flag); + returns.bvalue[0] = 0; + return FALSE; + } else { + uint8_t pt = 0; + uint32_t selected = 0; + int32_t len = std::max(1, (int32_t)count); + for (int32_t i = 0; i < len; ++i) { + uint8_t p = returns.bvalue[pt]; + uint8_t l = returns.bvalue[pt + 1]; + uint8_t s = returns.bvalue[pt + 2]; + uint32_t sel = 0x1u << (s + (p == playerid ? 0 : 16) + (l == LOCATION_MZONE ? 0 : 8)); + if(!(count == 0 && i == 0 && l == 0) + && ((p != 0 && p != 1) + || ((l != LOCATION_MZONE) && (l != LOCATION_SZONE)) + || (sel & flag) || (sel & selected))) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + if(sel & (0x1u << 5)) + sel |= 0x1u << (16 + 6); + if(sel & (0x1u << 6)) + sel |= 0x1u << (16 + 5); + selected |= sel; + pt += 3; + } + return TRUE; + } +} +int32_t field::select_position(uint16_t step, uint8_t playerid, uint32_t code, uint8_t positions) { + if(step == 0) { + if(positions == 0) { + returns.ivalue[0] = POS_FACEUP_ATTACK; + return TRUE; + } + positions &= 0xf; + if(positions == 0x1 || positions == 0x2 || positions == 0x4 || positions == 0x8) { + returns.ivalue[0] = positions; + return TRUE; + } + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + if((uint32_t)positions & 0x4) + returns.ivalue[0] = 0x4; + else if((uint32_t)positions & 0x1) + returns.ivalue[0] = 0x1; + else if((uint32_t)positions & 0x8) + returns.ivalue[0] = 0x8; + else + returns.ivalue[0] = 0x2; + return TRUE; + } + pduel->write_buffer8(MSG_SELECT_POSITION); + pduel->write_buffer8(playerid); + pduel->write_buffer32(code); + pduel->write_buffer8(positions); + returns.ivalue[0] = 0; + return FALSE; + } else { + uint32_t pos = returns.ivalue[0]; + if(pos != 0x1 && pos != 0x2 && pos != 0x4 && pos != 0x8 || !(pos & positions)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_tribute(uint16_t step, uint8_t playerid, uint8_t cancelable, uint8_t min, uint8_t max) { + if(step == 0) { + returns.bvalue[0] = 0; + if(max == 0 || core.select_cards.empty()) + return TRUE; + core.selecting_player = playerid; + std::sort(core.select_cards.begin(), core.select_cards.end(), card::card_operation_sort); + if (core.select_cards.size() > UINT8_MAX) + core.select_cards.resize(UINT8_MAX); + uint8_t tm = 0; + for(auto& pcard : core.select_cards) + tm += pcard->release_param; + if(max > 5) + max = 5; + if(max > tm) + max = tm; + if(min > max) + min = max; + core.units.begin()->arg2 = ((uint32_t)min) + (((uint32_t)max) << 16); + pduel->write_buffer8(MSG_SELECT_TRIBUTE); + pduel->write_buffer8(playerid); + pduel->write_buffer8(cancelable); + pduel->write_buffer8(min); + pduel->write_buffer8(max); + pduel->write_buffer8((uint8_t)core.select_cards.size()); + uint8_t deck_seq = 0; + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->get_select_sequence(&deck_seq)); + pduel->write_buffer8(pcard->release_param); + } + return FALSE; + } else { + if (returns.ivalue[0] == -1) { + if (cancelable) + return TRUE; + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + if(returns.bvalue[0] > max) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + std::set c; + int32_t m = (int32_t)core.select_cards.size(), tt = 0; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + uint8_t v = returns.bvalue[i + 1]; + if(v >= m || c.count(v)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + c.insert(v); + tt += core.select_cards[v]->release_param; + } + if(tt < min) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } +} +int32_t field::select_counter(uint16_t step, uint8_t playerid, uint16_t countertype, uint16_t count, uint8_t s, uint8_t o) { + if(step == 0) { + if(count == 0) + return TRUE; + uint8_t avail = s; + uint8_t fp = playerid; + uint32_t total = 0; + core.select_cards.clear(); + for(int p = 0; p < 2; ++p) { + if(avail) { + for(auto& pcard : player[fp].list_mzone) { + if(pcard && pcard->get_counter(countertype)) { + core.select_cards.push_back(pcard); + total += pcard->get_counter(countertype); + } + } + for(auto& pcard : player[fp].list_szone) { + if(pcard && pcard->get_counter(countertype)) { + core.select_cards.push_back(pcard); + total += pcard->get_counter(countertype); + } + } + } + fp = 1 - fp; + avail = o; + } + if(count > total) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + pduel->write_buffer8(MSG_SELECT_COUNTER); + pduel->write_buffer8(playerid); + pduel->write_buffer16(countertype); + pduel->write_buffer16(count); + pduel->write_buffer8((uint8_t)core.select_cards.size()); + core.selecting_player = playerid; + std::sort(core.select_cards.begin(), core.select_cards.end(), card::card_operation_sort); + uint8_t deck_seq = 0; + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->get_select_sequence(&deck_seq)); + pduel->write_buffer16(pcard->get_counter(countertype)); + } + return FALSE; + } else { + int32_t ct = 0; + for(int32_t i = 0; i < (int32_t)core.select_cards.size(); ++i) { + if(core.select_cards[i]->get_counter(countertype) < returns.svalue[i]) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + ct += returns.svalue[i]; + } + if(ct != count) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + } + return TRUE; +} +static int32_t select_sum_check1(const uint32_t* oparam, int32_t size, int32_t index, int32_t acc, int32_t opmin) { + if(acc == 0 || index == size) + return FALSE; + int32_t o1, o2; + field::get_sum_params(oparam[index], o1, o2); + if(index == size - 1) + return (acc == o1 && acc + opmin > o1) || (o2 && acc == o2 && acc + opmin > o2); + return (acc > o1 && select_sum_check1(oparam, size, index + 1, acc - o1, std::min(o1, opmin))) + || (o2 > 0 && acc > o2 && select_sum_check1(oparam, size, index + 1, acc - o2, std::min(o2, opmin))); +} +int32_t field::select_with_sum_limit(int16_t step, uint8_t playerid, int32_t acc, int32_t min, int32_t max) { + if(step == 0) { + returns.bvalue[0] = 0; + if(core.select_cards.empty()) + return TRUE; + core.selecting_player = playerid; + std::sort(core.select_cards.begin(), core.select_cards.end(), card::card_operation_sort); + if (core.select_cards.size() > UINT8_MAX) + core.select_cards.resize(UINT8_MAX); + if (core.must_select_cards.size() > UINT8_MAX) + core.must_select_cards.resize(UINT8_MAX); + pduel->write_buffer8(MSG_SELECT_SUM); + if(max) + pduel->write_buffer8(0); + else + pduel->write_buffer8(1); + if(max < min) + max = min; + pduel->write_buffer8(playerid); + pduel->write_buffer32(acc); + pduel->write_buffer8(min); + pduel->write_buffer8(max); + pduel->write_buffer8((uint8_t)core.must_select_cards.size()); + uint8_t deck_seq = 0; + for(auto& pcard : core.must_select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->get_select_sequence(&deck_seq)); + pduel->write_buffer32(pcard->sum_param); + } + pduel->write_buffer8((uint8_t)core.select_cards.size()); + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->get_select_sequence(&deck_seq)); + pduel->write_buffer32(pcard->sum_param); + } + return FALSE; + } else { + std::set c; + int32_t mcount = (int32_t)core.must_select_cards.size(); + if (mcount > UINT8_MAX) + mcount = UINT8_MAX; + if(max) { + uint32_t oparam[512]{}; + if(returns.bvalue[0] < min + mcount || returns.bvalue[0] > max + mcount) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + for(int32_t i = 0; i < mcount; ++i) + oparam[i] = core.must_select_cards[i]->sum_param; + int32_t m = (int32_t)core.select_cards.size(); + for(int32_t i = mcount; i < returns.bvalue[0]; ++i) { + int32_t v = returns.bvalue[i + 1]; + if(v < 0 || v >= m || c.count(v)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + c.insert(v); + oparam[i] = core.select_cards[v]->sum_param; + } + if(!select_sum_check1(oparam, returns.bvalue[0], 0, acc, 0xffff)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } else { + int32_t sum = 0, mx = 0, mn = 0x7fffffff; + for(int32_t i = 0; i < mcount; ++i) { + int32_t o1, o2; + field::get_sum_params(core.must_select_cards[i]->sum_param, o1, o2); + int32_t ms = (o2 && o2 < o1) ? o2 : o1; + sum += ms; + mx += std::max(o1, o2); + if(ms < mn) + mn = ms; + } + int32_t m = (int32_t)core.select_cards.size(); + for(int32_t i = mcount; i < returns.bvalue[0]; ++i) { + int32_t v = returns.bvalue[i + 1]; + if(v < 0 || v >= m || c.count(v)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + c.insert(v); + int32_t o1, o2; + field::get_sum_params(core.select_cards[v]->sum_param, o1, o2); + int32_t ms = (o2 && o2 < o1) ? o2 : o1; + sum += ms; + mx += std::max(o1, o2); + if(ms < mn) + mn = ms; + } + if(mx < acc || sum - mn >= acc) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + return TRUE; + } + } + return TRUE; +} +int32_t field::sort_card(int16_t step, uint8_t playerid) { + if(step == 0) { + returns.bvalue[0] = 0; + if((playerid == 1) && (core.duel_options & DUEL_SIMPLE_AI)) { + returns.bvalue[0] = 0xff; + return TRUE; + } + if(core.select_cards.empty()) + return TRUE; + if (core.select_cards.size() > UINT8_MAX) + core.select_cards.resize(UINT8_MAX); + pduel->write_buffer8(MSG_SORT_CARD); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.select_cards.size()); + for(auto& pcard : core.select_cards) { + pduel->write_buffer32(pcard->data.code); + pduel->write_buffer8(pcard->current.controler); + pduel->write_buffer8(pcard->current.location); + pduel->write_buffer8(pcard->current.sequence); + } + return FALSE; + } else { + if(returns.bvalue[0] == 0xff) + return TRUE; + std::set c; + int32_t m = (int32_t)core.select_cards.size(); + for(int32_t i = 0; i < m; ++i) { + uint8_t v = returns.bvalue[i]; + if(v >= m || c.count(v)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + c.insert(v); + } + return TRUE; + } + return TRUE; +} +int32_t field::announce_race(int16_t step, uint8_t playerid, int32_t count, int32_t available) { + if(step == 0) { + int32_t scount = 0; + for(uint32_t ft = 0x1; ft < (0x1U << RACES_COUNT); ft <<= 1) { + if(ft & available) + ++scount; + } + if(scount <= count) { + count = scount; + core.units.begin()->arg1 = (count << 16) + playerid; + } + pduel->write_buffer8(MSG_ANNOUNCE_RACE); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + pduel->write_buffer32(available); + return FALSE; + } else { + int32_t rc = returns.ivalue[0]; + int32_t sel = 0; + for(uint32_t ft = 0x1; ft < (0x1U << RACES_COUNT); ft <<= 1) { + if(!(ft & rc)) + continue; + if(!(ft & available)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + ++sel; + } + if(sel != count) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_RACE); + pduel->write_buffer8(playerid); + pduel->write_buffer32(returns.ivalue[0]); + return TRUE; + } + return TRUE; +} +int32_t field::announce_attribute(int16_t step, uint8_t playerid, int32_t count, int32_t available) { + if(step == 0) { + int32_t scount = 0; + for(int32_t ft = 0x1; ft != 0x80; ft <<= 1) { + if(ft & available) + ++scount; + } + if(scount <= count) { + count = scount; + core.units.begin()->arg1 = (count << 16) + playerid; + } + pduel->write_buffer8(MSG_ANNOUNCE_ATTRIB); + pduel->write_buffer8(playerid); + pduel->write_buffer8(count); + pduel->write_buffer32(available); + return FALSE; + } else { + int32_t rc = returns.ivalue[0]; + int32_t sel = 0; + for(int32_t ft = 0x1; ft != 0x80; ft <<= 1) { + if(!(ft & rc)) continue; + if(!(ft & available)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + ++sel; + } + if(sel != count) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_ATTRIB); + pduel->write_buffer8(playerid); + pduel->write_buffer32(returns.ivalue[0]); + return TRUE; + } + return TRUE; +} +static int32_t is_declarable(const card_data& cd, const std::vector& opcode) { + if (cd.alias) + return FALSE; + std::stack stack; + for(auto& it : opcode) { + switch(it) { + case OPCODE_ADD: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(lhs + rhs); + } + break; + } + case OPCODE_SUB: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(lhs - rhs); + } + break; + } + case OPCODE_MUL: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(lhs * rhs); + } + break; + } + case OPCODE_DIV: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(rhs != 0 ? lhs / rhs : 0); + } + break; + } + case OPCODE_AND: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(static_cast(lhs && rhs)); + } + break; + } + case OPCODE_OR: { + if(stack.size() >= 2) { + int32_t rhs = stack.top(); + stack.pop(); + int32_t lhs = stack.top(); + stack.pop(); + stack.push(static_cast(lhs || rhs)); + } + break; + } + case OPCODE_NEG: { + if(stack.size() >= 1) { + int32_t val = stack.top(); + stack.pop(); + stack.push(-val); + } + break; + } + case OPCODE_NOT: { + if(stack.size() >= 1) { + int32_t val = stack.top(); + stack.pop(); + stack.push(static_cast(!val)); + } + break; + } + case OPCODE_ISCODE: { + if(stack.size() >= 1) { + int32_t code = stack.top(); + stack.pop(); + stack.push(cd.code == code); + } + break; + } + case OPCODE_ISSETCARD: { + if(stack.size() >= 1) { + int32_t set_code = stack.top(); + stack.pop(); + bool res = cd.is_setcode(set_code); + stack.push(res); + } + break; + } + case OPCODE_ISTYPE: { + if(stack.size() >= 1) { + int32_t val = stack.top(); + stack.pop(); + stack.push(cd.type & val); + } + break; + } + case OPCODE_ISRACE: { + if(stack.size() >= 1) { + int32_t race = stack.top(); + stack.pop(); + stack.push(cd.race & race); + } + break; + } + case OPCODE_ISATTRIBUTE: { + if(stack.size() >= 1) { + int32_t attribute = stack.top(); + stack.pop(); + stack.push(cd.attribute & attribute); + } + break; + } + default: { + stack.push(it); + break; + } + } + } + if(stack.size() != 1 || stack.top() == 0) + return FALSE; + if (!second_code.count(cd.code) && (cd.rule_code || (cd.type & TYPE_TOKEN))) + return FALSE; + return TRUE; +} +int32_t field::announce_card(int16_t step, uint8_t playerid) { + if(step == 0) { + pduel->write_buffer8(MSG_ANNOUNCE_CARD); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.select_options.size()); + for(auto& option : core.select_options) + pduel->write_buffer32(option); + return FALSE; + } else { + int32_t code = returns.ivalue[0]; + card_data data; + ::read_card(code, &data); + if(!data.code) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } else { + if(!is_declarable(data, core.select_options)) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + } + if(!core.announce_card_silent) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CODE); + pduel->write_buffer8(playerid); + pduel->write_buffer32(code); + } else { + core.announce_card_silent = FALSE; + } + return TRUE; + } + return TRUE; +} +int32_t field::announce_number(int16_t step, uint8_t playerid) { + if(step == 0) { + if (core.select_options.size() > UINT8_MAX) + core.select_options.resize(UINT8_MAX); + pduel->write_buffer8(MSG_ANNOUNCE_NUMBER); + pduel->write_buffer8(playerid); + pduel->write_buffer8((uint8_t)core.select_options.size()); + for(auto& option : core.select_options) + pduel->write_buffer32(option); + return FALSE; + } else { + int32_t ret = returns.ivalue[0]; + if(ret < 0 || ret >= (int32_t)core.select_options.size()) { + pduel->write_buffer8(MSG_RETRY); + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_NUMBER); + pduel->write_buffer8(playerid); + pduel->write_buffer32(core.select_options[returns.ivalue[0]]); + return TRUE; + } +} +int32_t field::rock_paper_scissors(uint16_t step, uint8_t repeat) { + auto check_valid = [&]() { + if(returns.ivalue[0] < 1 || returns.ivalue[0] > 3) { + pduel->write_buffer8(MSG_RETRY); + --core.units.begin()->step; + return false; + } + return true; + }; + switch (step) { + case 0: { + pduel->write_buffer8(MSG_ROCK_PAPER_SCISSORS); + pduel->write_buffer8(0); + return FALSE; + } + case 1: { + if(!check_valid()) + return FALSE; + core.units.begin()->value1 = returns.ivalue[0]; + pduel->write_buffer8(MSG_ROCK_PAPER_SCISSORS); + pduel->write_buffer8(1); + return FALSE; + } + case 2: { + if(!check_valid()) + return FALSE; + uint8_t hand0 = static_cast(core.units.begin()->value1); + uint8_t hand1 = static_cast(returns.ivalue[0]); + pduel->write_buffer8(MSG_HAND_RES); + pduel->write_buffer8(hand0 + (hand1 << 2)); + if(hand0 == hand1) { + if(repeat) { + pduel->write_buffer8(MSG_ROCK_PAPER_SCISSORS); + pduel->write_buffer8(0); + core.units.begin()->step = 0; + return FALSE; + } else + returns.ivalue[0] = PLAYER_NONE; + } else if((hand0 == 1 && hand1 == 2) || (hand0 == 2 && hand1 == 3) || (hand0 == 3 && hand1 == 1)) { + returns.ivalue[0] = 1; + } else { + returns.ivalue[0] = 0; + } + return TRUE; + } + } + return TRUE; +} diff --git a/core/premake/dll.lua b/core/premake/dll.lua new file mode 100644 index 0000000..8308819 --- /dev/null +++ b/core/premake/dll.lua @@ -0,0 +1,232 @@ +LUA_DIR = "./lua" + +newoption { trigger = "lua-dir", description = "", value = "PATH" } +newoption { trigger = "sqlite3-dir", description = "", value = "PATH" } +newoption { trigger = "ndk-dir", category = "YGOPro - android", description = "", value = "PATH" } +newoption { trigger = "android-api-level", category = "YGOPro - android", description = "", value = "LEVEL" } +newoption { trigger = "no-longjmp", description = "Disable use of longjmp for error handling in Lua" } + +boolOptions = { + "no-lua-safe", +} + +for _, boolOption in ipairs(boolOptions) do + newoption { trigger = boolOption, category = "YGOPro - options", description = "" } +end + +function GetParam(param) + return _OPTIONS[param] or os.getenv(string.upper(string.gsub(param,"-","_"))) +end + +LUA_DIR = GetParam("lua-dir") or LUA_DIR +if not os.isdir(LUA_DIR) then + LUA_DIR = "../lua" +end + +SQLITE3_DIR=GetParam("sqlite3-dir") +USE_LONGJMP=not GetParam("no-longjmp") +ANDROID_NDK_DIR=GetParam("ndk-dir") + +function QuoteIfNeeded(value) + if string.find(value, " ", 1, true) then + return "\"" .. value .. "\"" + end + return value +end + +function FindAndroidToolchainBin(ndkDir) + local prebuiltDir = path.join(ndkDir, "toolchains/llvm/prebuilt") + local prebuilts = os.matchdirs(path.join(prebuiltDir, "*")) + table.sort(prebuilts) + if #prebuilts == 0 then + error("Android NDK toolchain not found under " .. prebuiltDir) + end + return path.join(prebuilts[1], "bin") +end + +ANDROID_ENABLED=false +ANDROID_API_LEVEL_TEXT=GetParam("android-api-level") or "26" +ANDROID_API_LEVEL=tonumber(ANDROID_API_LEVEL_TEXT) +if not ANDROID_API_LEVEL then + error("Invalid android api level: " .. ANDROID_API_LEVEL_TEXT) +end +if ANDROID_NDK_DIR then + ANDROID_NDK_DIR=path.getabsolute(ANDROID_NDK_DIR) + if not os.isdir(ANDROID_NDK_DIR) then + error("Android NDK directory not found: " .. ANDROID_NDK_DIR) + end + ANDROID_ENABLED=true + ANDROID_TOOLCHAIN_BIN=FindAndroidToolchainBin(ANDROID_NDK_DIR) + ANDROID_TARGET="aarch64-linux-android" .. ANDROID_API_LEVEL + premake.override(premake.tools.clang, "gettoolname", function(base, cfg, tool) + if cfg.system == premake.ANDROID then + if tool == "cc" then + return QuoteIfNeeded(path.join(ANDROID_TOOLCHAIN_BIN, "clang")) .. " --target=" .. ANDROID_TARGET + elseif tool == "cxx" then + return QuoteIfNeeded(path.join(ANDROID_TOOLCHAIN_BIN, "clang++")) .. " --target=" .. ANDROID_TARGET + elseif tool == "ar" then + return QuoteIfNeeded(path.join(ANDROID_TOOLCHAIN_BIN, "llvm-ar")) + end + end + return base(cfg, tool) + end) +end + +function ApplyBoolean(param) + if GetParam(param) then + defines { "YGOPRO_" .. string.upper(string.gsub(param,"-","_")) } + end +end + +local workspacePlatforms = { "x64", "x32", "arm64", "wasm_cjs", "wasm_esm" } +if ANDROID_ENABLED then + table.insert(workspacePlatforms, "android_arm64") +end + +workspace "ocgcoredll" + location "build" + language "C++" + cppdialect "C++14" + configurations { "Release", "Debug" } + platforms(workspacePlatforms) + + if USE_LONGJMP then + defines { "LUA_USE_LONGJMP" } + end + + for _, boolOption in ipairs(boolOptions) do + ApplyBoolean(boolOption) + end + + filter "platforms:x32" + architecture "x32" + + filter "platforms:x64" + architecture "x64" + + filter "platforms:arm64" + architecture "ARM64" + + filter "platforms:android_arm64" + architecture "ARM64" + system "android" + toolset "clang" + pic "On" + + filter "configurations:Release" + optimize "Speed" + + filter "configurations:Debug" + symbols "On" + defines "_DEBUG" + + filter "system:windows" + systemversion "latest" + startproject "ocgcore" + + filter { "system:windows", "action:vs2026" } + toolset "v143" + + filter { "configurations:Release", "action:vs*" } + linktimeoptimization "On" + staticruntime "On" + disablewarnings { "4334" } + + filter "action:vs*" + cdialect "C11" + conformancemode "On" + buildoptions { "/utf-8" } + defines { "_CRT_SECURE_NO_WARNINGS" } + + filter "system:bsd" + defines { "LUA_USE_POSIX" } + + filter "system:macosx" + defines { "LUA_USE_MACOSX" } + + filter "system:linux" + defines { "LUA_USE_LINUX" } + pic "On" + if USE_LONGJMP then + linkoptions { "-static-libstdc++", "-static-libgcc" } + end + + filter { "system:android", "language:C++" } + linkoptions { "-static-libstdc++" } + + filter "platforms:wasm_cjs or platforms:wasm_esm" + toolset "emcc" + -- defines { "LUA_USE_C89" } + pic "On" + +filter {} + +include(LUA_DIR) + +project "ocgcore" + + kind "SharedLib" + + files { "*.cpp", "*.h" } + links { "lua" } + + includedirs { LUA_DIR .. "/src" } + + filter "platforms:wasm_cjs or platforms:wasm_esm" + -- Avoid -shared so emcc emits JS glue + .wasm. + kind "ConsoleApp" + targetprefix "lib" + local wasmLinkOptions = { + "-s MODULARIZE=1", + "-s EXPORT_NAME=\"createOcgcore\"", + "--no-entry", + "-s EXIT_RUNTIME=1", + "-s ENVIRONMENT=web,worker,node", + "-s EXPORTED_RUNTIME_METHODS=[\"ccall\",\"cwrap\",\"addFunction\",\"removeFunction\"]", + "-s EXPORTED_FUNCTIONS=[\"_malloc\",\"_free\"]", + "-s ALLOW_TABLE_GROWTH=1", + "-s ALLOW_MEMORY_GROWTH=1", + "-s ASSERTIONS=0", + "-s SAFE_HEAP=0", + "-s DEMANGLE_SUPPORT=0", + } + linkoptions(wasmLinkOptions) + + filter "platforms:wasm_esm" + -- Build as ES module + targetextension ".mjs" + linkoptions { "-s EXPORT_ES6=1" } + + filter "platforms:wasm_cjs" + targetextension ".cjs" + + filter {} + +if not WASM and SQLITE3_DIR and os.isdir(SQLITE3_DIR) then +project "sqlite3" + kind "SharedLib" + language "C" + + files { + SQLITE3_DIR .. "/sqlite3.c", + SQLITE3_DIR .. "/sqlite3.h" + } + + filter "system:windows" + systemversion "latest" + defines { "SQLITE_API=__declspec(dllexport)" } + + filter "system:linux or system:macosx" + pic "On" + + filter "system:linux" + linkoptions { "-static-libstdc++", "-static-libgcc" } + + filter "configurations:Debug" + symbols "On" + defines { "DEBUG" } + + filter "configurations:Release" + optimize "On" + defines { "NDEBUG" } +end diff --git a/core/premake/lua.lua b/core/premake/lua.lua new file mode 100644 index 0000000..b1d990e --- /dev/null +++ b/core/premake/lua.lua @@ -0,0 +1,25 @@ +project "lua" + kind "StaticLib" + compileas "C++" + + files { "src/*.c", "src/*.h" } + removefiles { "src/lua.c", "src/luac.c", "src/onelua.c" } + + if not GetParam("no-lua-safe") then + removefiles { "src/linit.c" } + end + + filter "configurations:Debug" + defines { "LUA_USE_APICHECK" } + + filter "system:bsd" + defines { "LUA_USE_POSIX" } + + filter "system:macosx" + defines { "LUA_USE_MACOSX" } + + filter "system:android" + defines { "LUA_USE_POSIX" } + + filter "system:linux" + defines { "LUA_USE_LINUX" } diff --git a/core/premake/migrate.sql b/core/premake/migrate.sql new file mode 100644 index 0000000..c76cce4 --- /dev/null +++ b/core/premake/migrate.sql @@ -0,0 +1,20 @@ +--step 1: add new column +ALTER TABLE datas ADD COLUMN rule_code INTEGER DEFAULT 0; + +--step 2: update "treated as X" cards +UPDATE datas +SET rule_code = alias, alias = 0 +WHERE id IN ( + SELECT id + FROM datas + WHERE NOT (type & 0x4000) AND alias != 0 AND abs(id - alias) >= 20 +); + +--step 3: update special cards +UPDATE datas +SET rule_code = alias, alias = 0 +WHERE id = 5405695; + +UPDATE datas +SET rule_code = 13331639 +WHERE alias = 6218704; diff --git a/core/premake5.lua b/core/premake5.lua new file mode 100644 index 0000000..a3f5e0e --- /dev/null +++ b/core/premake5.lua @@ -0,0 +1,38 @@ +project "ocgcore" + if OCGCORE_DYNAMIC then + kind "SharedLib" + links { LUA_LIB_NAME } + libdirs { LUA_LIB_DIR } + else + kind "StaticLib" + end + + files { "*.cpp", "*.h" } + + if BUILD_LUA then + includedirs { "../lua/src" } + else + includedirs { LUA_INCLUDE_DIR } + end + + filter "not action:vs*" + cppdialect "C++14" + + filter "system:bsd" + defines { "LUA_USE_POSIX" } + + filter "system:macosx" + defines { "LUA_USE_MACOSX" } + + filter "system:android" + defines { "LUA_USE_POSIX" } + if OCGCORE_DYNAMIC then + pic "On" + end + + filter "system:linux" + defines { "LUA_USE_LINUX" } + if OCGCORE_DYNAMIC then + pic "On" + -- linkoptions { "-static-libstdc++", "-static-libgcc" } + end diff --git a/core/processor.cpp b/core/processor.cpp new file mode 100644 index 0000000..267c0f6 --- /dev/null +++ b/core/processor.cpp @@ -0,0 +1,5272 @@ +/* + * processor.cpp + * + * Created on: 2010-8-3 + * Author: Argon + */ + +#include "field.h" +#include "duel.h" +#include "card.h" +#include "group.h" +#include "effect.h" +#include "interpreter.h" +#include "ocgapi.h" +#include + +void field::add_process(uint16_t type, uint16_t step, effect* peffect, group* target, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4, void* ptr1, void* ptr2) { + processor_unit new_unit; + new_unit.type = type; + new_unit.step = step; + new_unit.peffect = peffect; + new_unit.ptarget = target; + new_unit.arg1 = arg1; + new_unit.arg2 = arg2; + new_unit.arg3 = arg3; + new_unit.arg4 = arg4; + new_unit.ptr1 = ptr1; + new_unit.ptr2 = ptr2; + core.subunits.push_back(new_unit); +} +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable: 4244) +#endif +uint32_t field::process() { + if (core.subunits.size()) + core.units.splice(core.units.begin(), core.subunits); + if (core.units.size() == 0) + return PROCESSOR_END | pduel->buffer_size(); + auto it = core.units.begin(); + switch (it->type) { + case PROCESSOR_ADJUST: { + if (adjust_step(it->step)) + core.units.pop_front(); + else { + ++it->step; + } + return pduel->buffer_size(); + } + case PROCESSOR_TURN: { + if (process_turn(it->step, it->arg1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_WAIT: { + core.units.pop_front(); + return PROCESSOR_WAITING | pduel->buffer_size(); + } + case PROCESSOR_REFRESH_LOC: { + if (refresh_location_info(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SELECT_BATTLECMD: { + if (select_battle_command(it->step, it->arg1)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_IDLECMD: { + if (select_idle_command(it->step, it->arg1)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_EFFECTYN: { + if (select_effect_yes_no(it->step, it->arg1, it->arg2, (card*)it->ptarget)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_YESNO: { + if (select_yes_no(it->step, it->arg1, it->arg2)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_OPTION: { + if (select_option(it->step, it->arg1)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_CARD: { + if (select_card(it->step, it->arg1 & 0xff, (it->arg1 >> 16) & 0xff, (it->arg2) & 0xff, (it->arg2 >> 16) & 0xff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_UNSELECT_CARD: { + if (select_unselect_card(it->step, it->arg1 & 0xff, (it->arg1 >> 16) & 0xff, (it->arg2) & 0xff, (it->arg2 >> 16) & 0xff, (it->arg3) & 0xff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_CHAIN: { + if (select_chain(it->step, it->arg1, it->arg2 & 0xffff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_DISFIELD: + case PROCESSOR_SELECT_PLACE: { + if (select_place(it->step, it->arg1, it->arg2, it->arg3)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_POSITION: { + if (select_position(it->step, it->arg1 & 0xffff, it->arg2, (it->arg1 >> 16) & 0xffff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_TRIBUTE_P: { + if (select_tribute(it->step, it->arg1 & 0xff, (it->arg1 >> 16) & 0xff, (it->arg2) & 0xff, (it->arg2 >> 16) & 0xff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_COUNTER: { + if (select_counter(it->step, it->arg1, it->arg2, it->arg3, it->arg4 >> 8, it->arg4 & 0xff)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_SUM: { + if (select_with_sum_limit(it->step, it->arg2, it->arg1, it->arg3, it->arg4)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SORT_CARD: { + if (sort_card(it->step, it->arg1)) { + core.units.pop_front(); + return pduel->buffer_size(); + } else { + it->step = 1; + return PROCESSOR_WAITING | pduel->buffer_size(); + } + } + case PROCESSOR_SELECT_RELEASE: { + if (select_release_cards(it->step, it->arg1 & 0xff, (it->arg1 >> 16) & 0xff, (it->arg2) & 0xff, (it->arg2 >> 16) & 0xff)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SELECT_TRIBUTE: { + if (select_tribute_cards(it->step, (card*)it->ptarget, it->arg1 & 0xff, (it->arg1 >> 16) & 0xff, (it->arg2) & 0xff, (it->arg2 >> 16) & 0xff, it->arg3, it->arg4)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_POINT_EVENT: { + if(process_point_event(it->step, it->arg1 & 0xff, (it->arg1 >> 8) & 0xff, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_QUICK_EFFECT: { + if(process_quick_effect(it->step, it->arg1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_IDLE_COMMAND: { + if(process_idle_command(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_PHASE_EVENT: { + if(process_phase_event(it->step, it->arg1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_BATTLE_COMMAND: { + if(process_battle_command(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DAMAGE_STEP: { + if(process_damage_step(it->step, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_ADD_CHAIN: { + if (add_chain(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SOLVE_CHAIN: { + if (solve_chain(it->step, it->arg1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SOLVE_CONTINUOUS: { + if (solve_continuous(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_EXECUTE_COST: { + if (execute_cost(it->step, it->peffect, it->arg1)) { + core.units.pop_front(); + core.solving_event.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_EXECUTE_OPERATION: { + if (execute_operation(it->step, it->peffect, it->arg1)) { + core.units.pop_front(); + core.solving_event.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_EXECUTE_TARGET: { + if (execute_target(it->step, it->peffect, it->arg1)) { + core.units.pop_front(); + core.solving_event.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DESTROY: { + if (destroy(it->step, it->ptarget, it->peffect, it->arg1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_RELEASE: { + if (release(it->step, it->ptarget, it->peffect, it->arg1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SENDTO: { + if (send_to(it->step, it->ptarget, it->peffect, it->arg1, it->arg2, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DESTROY_REPLACE: { + if(destroy_replace(it->step, it->ptarget, (card*)it->ptr1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_RELEASE_REPLACE: { + if (release_replace(it->step, it->ptarget, (card*)it->ptr1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SENDTO_REPLACE: { + if (send_replace(it->step, it->ptarget, (card*)it->ptr1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_MOVETOFIELD: { + if (move_to_field(it->step, (card*)it->ptarget, it->arg1, it->arg2 & 0xff, (it->arg2 >> 8) & 0xff, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_CHANGEPOS: { + if (change_position(it->step, it->ptarget, it->peffect, it->arg1, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_OPERATION_REPLACE: { + if (operation_replace(it->step, it->peffect, it->ptarget, (card*)it->ptr1, it->arg1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_ACTIVATE_EFFECT: { + if (activate_effect(it->step, it->peffect)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SUMMON_RULE: { + if (summon(it->step, it->arg1 & 0xff, (card*)it->ptarget, it->peffect, (it->arg1 >> 8) & 0xff, (it->arg1 >> 16) & 0xff, (it->arg1 >> 24) & 0xff, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SPSUMMON_RULE: { + if (special_summon_rule(it->step, it->arg1, (card*)it->ptarget, it->arg2, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SPSUMMON: { + if (special_summon(it->step, it->peffect, it->arg1, it->ptarget, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_FLIP_SUMMON: { + if (flip_summon(it->step, it->arg1, (card*)(it->ptarget), it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_MSET: { + if (mset(it->step, it->arg1 & 0xff, (card*)it->ptarget, it->peffect, (it->arg1 >> 8) & 0xff, (it->arg1 >> 16) & 0xff, (it->arg1 >> 24) & 0xff, it->arg2)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SSET: { + if (sset(it->step, it->arg1, it->arg2, (card*)(it->ptarget), it->peffect, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SPSUMMON_STEP: { + if (special_summon_step(it->step, it->ptarget, (card*)(it->ptr1), it->arg1)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SSET_G: { + if (sset_g(it->step, it->arg1, it->arg2, it->ptarget, it->arg3, it->peffect, it->arg4)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DRAW : { + if (draw(it->step, it->peffect, it->arg1, (it->arg2 >> 4) & 0xf, (it->arg2) & 0xf, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DAMAGE: { + int32_t reason = it->arg1; + effect* reason_effect = nullptr; + card* reason_card = nullptr; + if(reason & REASON_BATTLE) + reason_card = (card*)it->peffect; + else + reason_effect = it->peffect; + if (damage(it->step, reason_effect, reason, (it->arg2 >> 2) & 0x3, reason_card, (it->arg2) & 0x3, it->arg3, (it->arg2 >> 4) & 0x1)) { + if(it->step == 9) { + it->step = 1; + core.recover_damage_reserve.splice(core.recover_damage_reserve.end(), core.units, it); + } else + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_RECOVER: { + if (recover(it->step, it->peffect, it->arg1, (it->arg2 >> 2) & 0x3, (it->arg2) & 0x3, it->arg3, (it->arg2 >> 4) & 0x1)) { + if(it->step == 9) { + it->step = 1; + core.recover_damage_reserve.splice(core.recover_damage_reserve.end(), core.units, it); + } else + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_EQUIP: { + if (equip(it->step, it->arg2 & 0xffff, (card*)it->ptr1, (card*)it->ptarget, (it->arg2 >> 16) & 0xff, (it->arg2 >> 24) & 0xff)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_GET_CONTROL: { + if (get_control(it->step, it->peffect, (it->arg2 >> 28) & 0xf, it->ptarget, (it->arg2 >> 24) & 0xf, (it->arg2 >> 8) & 0x3ff, it->arg2 & 0xff, it->arg3)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SWAP_CONTROL: { + if (swap_control(it->step, it->peffect, it->arg1, it->ptarget, (group*)it->ptr1, it->arg2, it->arg3)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SELF_DESTROY: { + if (self_destroy(it->step, (card*)it->ptr1, it->arg1)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_TRAP_MONSTER_ADJUST: { + if (trap_monster_adjust(it->step)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_PAY_LPCOST: { + if (pay_lp_cost(it->step, it->arg1, it->arg2, it->arg3)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_REMOVE_COUNTER: { + if (remove_counter(it->step, it->arg4, (card*)it->ptarget, (it->arg1 >> 16) & 0xff, (it->arg1 >> 8) & 0xff, it->arg1 & 0xff, it->arg2, it->arg3)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_ATTACK_DISABLE: { + if(it->step == 0) { + card* attacker = core.attacker; + if(!attacker + || core.effect_damage_step != 0 + || (attacker->fieldid_r != core.pre_field[0]) + || (attacker->current.location != LOCATION_MZONE) + || !attacker->is_capable_attack() + || !attacker->is_affect_by_effect(core.reason_effect)) { + returns.ivalue[0] = 0; + core.units.pop_front(); + } else { + effect* peffect = pduel->new_effect(); + peffect->code = EFFECT_ATTACK_DISABLED; + peffect->type = EFFECT_TYPE_SINGLE; + attacker->add_effect(peffect); + attacker->set_status(STATUS_ATTACK_CANCELED, TRUE); + raise_event(attacker, EVENT_ATTACK_DISABLED, core.reason_effect, 0, core.reason_player, PLAYER_NONE, 0); + process_instant_event(); + ++it->step; + } + } else { + returns.ivalue[0] = 1; + core.units.pop_front(); + } + return pduel->buffer_size(); + } + case PROCESSOR_ANNOUNCE_RACE: { + if(announce_race(it->step, it->arg1 & 0xffff, it->arg1 >> 16, it->arg2)) { + core.units.pop_front(); + } else { + ++it->step; + } + return PROCESSOR_WAITING | pduel->buffer_size(); + } + case PROCESSOR_ANNOUNCE_ATTRIB: { + if(announce_attribute(it->step, it->arg1 & 0xffff, it->arg1 >> 16, it->arg2)) { + core.units.pop_front(); + } else { + ++it->step; + } + return PROCESSOR_WAITING | pduel->buffer_size(); + } + case PROCESSOR_ANNOUNCE_CARD: { + if(announce_card(it->step, it->arg1)) { + core.units.pop_front(); + } else { + ++it->step; + } + return PROCESSOR_WAITING | pduel->buffer_size(); + } + case PROCESSOR_ANNOUNCE_NUMBER: { + if(announce_number(it->step, it->arg1)) { + core.units.pop_front(); + } else { + ++it->step; + } + return PROCESSOR_WAITING | pduel->buffer_size(); + } + case PROCESSOR_TOSS_DICE: { + if(toss_dice(it->step, it->peffect, it->arg1 >> 16, it->arg1 & 0xff, it->arg2 & 0xff, it->arg2 >> 16)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_TOSS_COIN: { + if (toss_coin(it->step, it->peffect, (it->arg1 >> 16), it->arg1 & 0xff, it->arg2)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_ROCK_PAPER_SCISSORS: { + if (rock_paper_scissors(it->step, it->arg1)) { + core.units.pop_front(); + } else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SELECT_FUSION: { + if(it->step == 0) { + tevent e; + e.event_cards = it->ptarget; + e.reason_effect = it->peffect; + e.reason_player = it->arg1; + core.fusion_materials.clear(); + if(!it->peffect) { + core.units.pop_front(); + return pduel->buffer_size(); + } + core.not_material = it->arg2; + core.sub_solving_event.push_back(e); + pduel->lua->add_param(it->ptr1, PARAM_TYPE_CARD); + pduel->lua->add_param(it->arg1 >> 16, PARAM_TYPE_INT); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, it->peffect, 0, it->arg1 & 0xffff, 0); + ++it->step; + } else { + core.not_material = 0; + core.units.pop_front(); + } + return pduel->buffer_size(); + } + case PROCESSOR_SELECT_SYNCHRO: { + int32_t ret = TRUE; + if (!(it->arg1 >> 16)) + ret = select_synchro_material(it->step, it->arg1 & 0xffff, (card*)it->ptr1, it->arg2 & 0xffff, it->arg2 >> 16, nullptr, (group*)it->ptr2, it->arg3, it->arg4); + else + ret = select_synchro_material(it->step, it->arg1 & 0xffff, (card*)it->ptr1, it->arg2 & 0xffff, it->arg2 >> 16, (card*)it->ptr2, nullptr, it->arg3, it->arg4); + if(ret) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_SELECT_XMATERIAL: { + if (select_xyz_material(it->step, it->arg1 & 0xffff, it->arg1 >> 16, (card*)it->ptarget, it->arg2 & 0xffff, it->arg2 >> 16)) + core.units.pop_front(); + else + ++it->step; + return pduel->buffer_size(); + } + case PROCESSOR_DISCARD_HAND: { + if(it->step == 0) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(it->arg1); + if(it->arg3 & REASON_DISCARD) + pduel->write_buffer32(501); + else + pduel->write_buffer32(504); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, it->arg1, it->arg2); + ++it->step; + } else if(it->step == 1) { + card_set cset; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) { + card* pcard = core.select_cards[returns.bvalue[i + 1]]; + cset.insert(pcard); + } + if(cset.size()) + send_to(cset, core.reason_effect, it->arg3, core.reason_player, it->arg1, LOCATION_GRAVE, 0, POS_FACEUP); + else + returns.ivalue[0] = 0; + ++it->step; + } else { + core.units.pop_front(); + } + return pduel->buffer_size(); + } + case PROCESSOR_DISCARD_DECK: { + if(discard_deck(it->step, it->arg1 & 0xff, it->arg1 >> 16, it->arg2)) { + core.units.pop_front(); + } else { + ++it->step; + } + return pduel->buffer_size(); + } + case PROCESSOR_SORT_DECK: { + uint8_t sort_player = it->arg1 & 0xffff; + uint8_t target_player = it->arg1 >> 16; + uint8_t count = it->arg2, i = 0; + if(count > player[target_player].list_main.size()) + count = (uint8_t)player[target_player].list_main.size(); + if(it->step == 0) { + core.select_cards.clear(); + for(auto clit = player[target_player].list_main.rbegin(); i < count; ++i, ++clit) + core.select_cards.push_back(*clit); + add_process(PROCESSOR_SORT_CARD, 0, 0, 0, sort_player, 0); + ++it->step; + } else { + if(returns.bvalue[0] != 0xff) { + card* tc[256]; + for(i = 0; i < count; ++i) + player[target_player].list_main.pop_back(); + for(i = 0; i < count; ++i) + tc[returns.bvalue[i]] = core.select_cards[i]; + for(i = 0; i < count; ++i) { + player[target_player].list_main.push_back(tc[count - 1 - i]); + } + reset_sequence(target_player, LOCATION_DECK); + auto clit = player[target_player].list_main.rbegin(); + for(i = 0; i < count; ++i, ++clit) { + card* pcard = *clit; + pduel->write_buffer8(MSG_MOVE); + pduel->write_buffer32(0); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(pcard->get_info_location()); + pduel->write_buffer32(REASON_EFFECT); + } + } + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + if(count > 0) { + card* ptop = player[target_player].list_main.back(); + if(core.deck_reversed || (ptop->current.position == POS_FACEUP_DEFENSE)) { + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(target_player); + pduel->write_buffer8(0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + core.units.pop_front(); + } + return pduel->buffer_size(); + } + case PROCESSOR_REMOVE_OVERLAY: { + if(remove_overlay_card(it->step, it->arg3, (card*)(it->ptarget), it->arg1 >> 16, + (it->arg1 >> 8) & 0xff, it->arg1 & 0xff, it->arg2 & 0xffff, it->arg2 >> 16)) { + core.units.pop_front(); + } else { + ++it->step; + } + return pduel->buffer_size(); + } + } + return pduel->buffer_size(); +} +#ifdef _MSC_VER +#pragma warning(pop) +#endif +int32_t field::execute_cost(uint16_t step, effect * triggering_effect, uint8_t triggering_player) { + if(!triggering_effect->cost) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + pduel->lua->params.clear(); + return TRUE; + } + if (step == 0) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + const tevent& e = core.solving_event.front(); + pduel->lua->add_param(1, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT, true); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP, true); + pduel->lua->add_param(triggering_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(triggering_effect, PARAM_TYPE_EFFECT, true); + if(core.check_level == 0) { + core.shuffle_deck_check[0] = FALSE; + core.shuffle_deck_check[1] = FALSE; + core.shuffle_hand_check[0] = FALSE; + core.shuffle_hand_check[1] = FALSE; + } + core.units.begin()->arg2 = core.shuffle_check_disabled; + core.shuffle_check_disabled = FALSE; + ++core.check_level; + } + core.reason_effect = triggering_effect; + core.reason_player = triggering_player; + uint32_t count = (uint32_t)pduel->lua->params.size(); + int32_t yield_value = 0; + int32_t result = pduel->lua->call_coroutine(triggering_effect->cost, count, &yield_value, step); + returns.ivalue[0] = yield_value; + if (result == COROUTINE_FINISH || result == COROUTINE_ERROR || result == OPERATION_FAIL) { + core.reason_effect = 0; + core.reason_player = PLAYER_NONE; + --core.check_level; + if(core.check_level == 0) { + if(core.shuffle_hand_check[0]) + shuffle(0, LOCATION_HAND); + if(core.shuffle_hand_check[1]) + shuffle(1, LOCATION_HAND); + if(core.shuffle_deck_check[0]) + shuffle(0, LOCATION_DECK); + if(core.shuffle_deck_check[1]) + shuffle(1, LOCATION_DECK); + } + core.shuffle_check_disabled = (uint8_t)core.units.begin()->arg2; + return TRUE; + } + return FALSE; +} +int32_t field::execute_operation(uint16_t step, effect * triggering_effect, uint8_t triggering_player) { + if(!triggering_effect->operation) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + pduel->lua->params.clear(); + return TRUE; + } + if (step == 0) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + const tevent& e = core.solving_event.front(); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT, true); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP, true); + pduel->lua->add_param(triggering_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(triggering_effect, PARAM_TYPE_EFFECT, true); + if(core.check_level == 0) { + core.shuffle_deck_check[0] = FALSE; + core.shuffle_deck_check[1] = FALSE; + core.shuffle_hand_check[0] = FALSE; + core.shuffle_hand_check[1] = FALSE; + } + core.units.begin()->arg2 = core.shuffle_check_disabled; + core.shuffle_check_disabled = FALSE; + ++core.check_level; + } + core.reason_effect = triggering_effect; + core.reason_player = triggering_player; + uint32_t count = (uint32_t)pduel->lua->params.size(); + int32_t yield_value = 0; + int32_t result = pduel->lua->call_coroutine(triggering_effect->operation, count, &yield_value, step); + returns.ivalue[0] = yield_value; + if (result == COROUTINE_FINISH || result == COROUTINE_ERROR || result == OPERATION_FAIL) { + core.reason_effect = 0; + core.reason_player = PLAYER_NONE; + --core.check_level; + if(core.check_level == 0) { + if(core.shuffle_hand_check[0]) + shuffle(0, LOCATION_HAND); + if(core.shuffle_hand_check[1]) + shuffle(1, LOCATION_HAND); + if(core.shuffle_deck_check[0]) + shuffle(0, LOCATION_DECK); + if(core.shuffle_deck_check[1]) + shuffle(1, LOCATION_DECK); + //cost[0].count = 0; + //cost[1].count = 0; + //cost[0].amount = 0; + //cost[1].amount = 0; + } + core.shuffle_check_disabled = (uint8_t)core.units.begin()->arg2; + return TRUE; + } + return FALSE; +} +int32_t field::execute_target(uint16_t step, effect * triggering_effect, uint8_t triggering_player) { + if(!triggering_effect->target) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + pduel->lua->params.clear(); + return TRUE; + } + if (step == 0) { + core.solving_event.splice(core.solving_event.begin(), core.sub_solving_event); + const tevent& e = core.solving_event.front(); + pduel->lua->add_param(1, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.reason_effect , PARAM_TYPE_EFFECT, true); + pduel->lua->add_param(e.event_value, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(e.event_cards , PARAM_TYPE_GROUP, true); + pduel->lua->add_param(triggering_player, PARAM_TYPE_INT, true); + pduel->lua->add_param(triggering_effect, PARAM_TYPE_EFFECT, true); + if(core.check_level == 0) { + core.shuffle_deck_check[0] = FALSE; + core.shuffle_deck_check[1] = FALSE; + core.shuffle_hand_check[0] = FALSE; + core.shuffle_hand_check[1] = FALSE; + } + core.units.begin()->arg2 = core.shuffle_check_disabled; + core.shuffle_check_disabled = FALSE; + ++core.check_level; + } + core.reason_effect = triggering_effect; + core.reason_player = triggering_player; + uint32_t count = (uint32_t)pduel->lua->params.size(); + int32_t yield_value = 0; + int32_t result = pduel->lua->call_coroutine(triggering_effect->target, count, &yield_value, step); + returns.ivalue[0] = yield_value; + if (result == COROUTINE_FINISH || result == COROUTINE_ERROR || result == OPERATION_FAIL) { + core.reason_effect = 0; + core.reason_player = PLAYER_NONE; + --core.check_level; + if(core.check_level == 0) { + if(core.shuffle_hand_check[0]) + shuffle(0, LOCATION_HAND); + if(core.shuffle_hand_check[1]) + shuffle(1, LOCATION_HAND); + if(core.shuffle_deck_check[0]) + shuffle(0, LOCATION_DECK); + if(core.shuffle_deck_check[1]) + shuffle(1, LOCATION_DECK); + } + core.shuffle_check_disabled = (uint8_t)core.units.begin()->arg2; + return TRUE; + } + return FALSE; +} +void field::raise_event(card* event_card, uint32_t event_code, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value) { + tevent new_event; + new_event.trigger_card = 0; + if (event_card) { + group* pgroup = pduel->new_group(event_card); + pgroup->is_readonly = GTYPE_READ_ONLY; + new_event.event_cards = pgroup; + } else + new_event.event_cards = nullptr; + new_event.event_code = event_code; + new_event.reason_effect = reason_effect; + new_event.reason = reason; + new_event.reason_player = reason_player; + new_event.event_player = event_player; + new_event.event_value = event_value; + core.queue_event.push_back(new_event); +} +void field::raise_event(const card_set& event_cards, uint32_t event_code, effect* reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value) { + tevent new_event; + new_event.trigger_card = 0; + group* pgroup = pduel->new_group(event_cards); + pgroup->is_readonly = GTYPE_READ_ONLY; + new_event.event_cards = pgroup; + new_event.event_code = event_code; + new_event.reason_effect = reason_effect; + new_event.reason = reason; + new_event.reason_player = reason_player; + new_event.event_player = event_player; + new_event.event_value = event_value; + core.queue_event.push_back(new_event); +} +void field::raise_single_event(card* trigger_card, card_set* event_cards, uint32_t event_code, effect * reason_effect, uint32_t reason, uint8_t reason_player, uint8_t event_player, uint32_t event_value) { + tevent new_event; + new_event.trigger_card = trigger_card; + if (event_cards) { + group* pgroup = pduel->new_group(*event_cards); + pgroup->is_readonly = GTYPE_READ_ONLY; + new_event.event_cards = pgroup; + } else + new_event.event_cards = nullptr; + new_event.event_code = event_code; + new_event.reason_effect = reason_effect; + new_event.reason = reason; + new_event.reason_player = reason_player; + new_event.event_player = event_player; + new_event.event_value = event_value; + core.single_event.push_back(new_event); +} +int32_t field::check_event(uint32_t code, tevent * pe) { + for(const auto& ev : core.point_event) { + if(ev.event_code == code) { + if(pe) + *pe = ev; + return TRUE; + } + } + for(const auto& ev : core.instant_event) { + if(ev.event_code == code) { + if(pe) + *pe = ev; + return TRUE; + } + } + return FALSE; +} +int32_t field::check_event_c(effect* peffect, uint8_t playerid, int32_t neglect_con, int32_t neglect_cost, int32_t copy_info, tevent* pe) { + if(peffect->code == EVENT_FREE_CHAIN) { + return peffect->is_activate_ready(core.reason_effect, playerid, nil_event, neglect_con, neglect_cost, FALSE); + } + for(const auto& ev : core.point_event) { + if(ev.event_code == peffect->code && + peffect->is_activate_ready(core.reason_effect, playerid, ev, neglect_con, neglect_cost, FALSE)) { + if(pe) + *pe = ev; + if(copy_info && !pduel->lua->no_action && core.current_chain.size()) { + core.current_chain.back().evt = ev; + } + return TRUE; + } + } + for(const auto& ev : core.instant_event) { + if(ev.event_code == peffect->code && + peffect->is_activate_ready(core.reason_effect, playerid, ev, neglect_con, neglect_cost, FALSE)) { + if(pe) + *pe = ev; + if(copy_info && !pduel->lua->no_action && core.current_chain.size()) { + core.current_chain.back().evt = ev; + } + return TRUE; + } + } + return FALSE; +} +int32_t field::check_hint_timing(effect* peffect) { + int32_t p = peffect->get_handler_player(); + if(p == 0) + return (peffect->hint_timing[0] & core.hint_timing[0]) || (peffect->hint_timing[1] & core.hint_timing[1]); + else + return (peffect->hint_timing[0] & core.hint_timing[1]) || (peffect->hint_timing[1] & core.hint_timing[0]); +} +// core.spe_effect[p]: # of optional trigger effects, activate/quick effect with hints +int32_t field::process_phase_event(int16_t step, int32_t phase) { + tevent test_event; + test_event.event_code = EVENT_PHASE + phase; + test_event.event_player = infos.turn_player; + switch(step) { + case 0: { + if((phase == PHASE_DRAW && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_DP)) + || (phase == PHASE_STANDBY && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_SP)) + || (phase == PHASE_BATTLE_START && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP)) + || (phase == PHASE_BATTLE && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP)) + || (phase == PHASE_END && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_EP))) { + core.units.begin()->step = 24; + return FALSE; + } + int32_t phase_event = EVENT_PHASE + phase; + int32_t check_player = infos.turn_player; + if(core.units.begin()->arg2 & 0x2) + check_player = 1 - infos.turn_player; + core.select_chains.clear(); + int32_t tf_count = 0, to_count = 0, fc_count = 0, cn_count = 0; + auto pr = effects.trigger_f_effect.equal_range(phase_event); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + newchain.flag |= CHAIN_FORCED; + core.select_chains.push_back(newchain); + ++tf_count; + } + pr = effects.continuous_effect.equal_range(phase_event); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + //effects.continuous_effect may be changed in is_activateable (e.g. Rescue Cat) + if(peffect->get_handler_player() != check_player || !peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + newchain.flag |= CHAIN_FORCED; + core.select_chains.push_back(newchain); + ++cn_count; + } + //all effects taking control non-permanently are only until End Phase, not until Turn end + for(auto& peffect : effects.pheff) { + if(peffect->code != EFFECT_SET_CONTROL) + continue; + if(!(peffect->reset_flag & phase)) + continue; + uint8_t pid = peffect->get_handler_player(); + if(pid != check_player) + continue; + uint8_t tp = infos.turn_player; + if(!(((peffect->reset_flag & RESET_SELF_TURN) && pid == tp) || ((peffect->reset_flag & RESET_OPPO_TURN) && pid != tp))) + continue; + if(peffect->reset_count != 1) + continue; + card* phandler = peffect->get_handler(); + if(peffect->get_value(phandler) != phandler->current.controler) + continue; + chain newchain; + newchain.triggering_effect = peffect; + newchain.flag |= CHAIN_FORCED; + core.select_chains.push_back(newchain); + ++cn_count; + } + core.spe_effect[check_player] = 0; + if(!core.hand_adjusted) { + pr = effects.trigger_o_effect.equal_range(phase_event); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + core.select_chains.push_back(newchain); + ++to_count; + ++core.spe_effect[check_player]; + } + if(phase == PHASE_DRAW) + core.hint_timing[infos.turn_player] = TIMING_DRAW_PHASE; + else if(phase == PHASE_STANDBY) + core.hint_timing[infos.turn_player] = TIMING_STANDBY_PHASE; + else if(phase == PHASE_BATTLE_START) + core.hint_timing[infos.turn_player] = TIMING_BATTLE_START; + else if(phase == PHASE_BATTLE) + core.hint_timing[infos.turn_player] = TIMING_BATTLE_END; + else + core.hint_timing[infos.turn_player] = TIMING_END_PHASE; + pr = effects.activate_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_chainable(check_player) || !peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + if(check_hint_timing(peffect) || check_cteffect_hint(peffect, check_player)) + ++core.spe_effect[check_player]; + core.select_chains.push_back(newchain); + ++fc_count; + } + pr = effects.quick_o_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_chainable(check_player) || !peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + if(check_hint_timing(peffect)) + ++core.spe_effect[check_player]; + core.select_chains.push_back(newchain); + ++fc_count; + } + pr = effects.continuous_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->get_handler_player() != check_player || !peffect->is_activateable(check_player, test_event)) + continue; + peffect->id = infos.field_id++; + chain newchain; + newchain.triggering_effect = peffect; + core.select_chains.push_back(newchain); + ++fc_count; + } + } + if(core.select_chains.size() == 0) { + returns.ivalue[0] = -1; + core.units.begin()->step = 1; + return FALSE; + } else if(tf_count == 0 && cn_count == 1 && to_count == 0 && fc_count == 0) { + returns.ivalue[0] = 0; + core.units.begin()->step = 1; + return FALSE; + } else { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(check_player); + if(infos.phase == PHASE_DRAW) + pduel->write_buffer32(20); + else if(infos.phase == PHASE_STANDBY) + pduel->write_buffer32(21); + else if(infos.phase == PHASE_BATTLE_START) + pduel->write_buffer32(28); + else if(infos.phase == PHASE_BATTLE) + pduel->write_buffer32(25); + else + pduel->write_buffer32(26); + if(tf_count == 0 && to_count == 1 && fc_count == 0 && cn_count == 0) { + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_chains[0].triggering_effect->get_handler(), check_player, 0); + return FALSE; + } else { + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, check_player, core.spe_effect[check_player]); + core.units.begin()->step = 1; + return FALSE; + } + } + return FALSE; + } + case 1: { + --returns.ivalue[0]; + return FALSE; + } + case 2: { + int32_t priority_passed = core.units.begin()->arg2 & 0x1; + int32_t is_opponent = core.units.begin()->arg2 & 0x2; + if(returns.ivalue[0] == -1) { + if(priority_passed) + core.units.begin()->step = 19; + else { + priority_passed = 1; + is_opponent ^= 0x2; + core.units.begin()->arg2 = is_opponent | priority_passed; + core.units.begin()->step = -1; + } + return FALSE; + } + priority_passed = 0; + core.units.begin()->arg2 = is_opponent | priority_passed; + chain newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + card* phandler = peffect->get_handler(); + if(!(peffect->type & EFFECT_TYPE_ACTIONS)) { + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + remove_effect(peffect); + else + peffect->handler->remove_effect(peffect); + adjust_all(); + core.units.begin()->step = 3; + } else if(!(peffect->type & EFFECT_TYPE_CONTINUOUS)) { + int32_t check_player = infos.turn_player; + if(is_opponent) + check_player = 1 - infos.turn_player; + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = test_event; + newchain.set_triggering_state(phandler); + newchain.triggering_player = check_player; + core.new_chains.push_back(newchain); + phandler->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(check_player); + core.select_chains.clear(); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - check_player); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + } else { + core.select_chains.clear(); + solve_continuous(peffect->get_handler_player(), peffect, test_event); + core.units.begin()->step = 3; + } + return FALSE; + } + case 3: { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 4: { + adjust_instant(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 20: { + if(phase != PHASE_END) { + core.units.begin()->step = 24; + return FALSE; + } + int32_t limit = 6; + effect_set eset; + filter_player_effect(infos.turn_player, EFFECT_HAND_LIMIT, &eset); + if(eset.size()) + limit = eset.back()->get_value(); + int32_t hd = (int32_t)player[infos.turn_player].list_hand.size(); + if(hd <= limit) { + core.units.begin()->step = 24; + return FALSE; + } + core.select_cards.clear(); + for(auto& pcard : player[infos.turn_player].list_hand) + core.select_cards.push_back(pcard); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(infos.turn_player); + pduel->write_buffer32(501); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, infos.turn_player, hd - limit + ((hd - limit) << 16)); + return FALSE; + } + case 21: { + card_set cset; + for(int32_t i = 0; i < returns.bvalue[0]; ++i) + cset.insert(core.select_cards[returns.bvalue[i + 1]]); + send_to(cset, 0, REASON_RULE + REASON_DISCARD + REASON_ADJUST, infos.turn_player, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + case 22: { + core.hand_adjusted = TRUE; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + core.units.begin()->step = -1; + core.units.begin()->arg2 = 0; + return FALSE; + } + case 25: { + core.hint_timing[infos.turn_player] = 0; + reset_phase(phase); + adjust_all(); + return FALSE; + } + case 26: { + core.quick_f_chain.clear(); + core.instant_event.clear(); + core.point_event.clear(); + core.delayed_activate_event.clear(); + core.full_event.clear(); + return TRUE; + } + } + return TRUE; +} +int32_t field::process_point_event(int16_t step, int32_t skip_trigger, int32_t skip_freechain, int32_t skip_new) { + switch(step) { + case 0: { + core.select_chains.clear(); + core.point_event.splice(core.point_event.end(), core.instant_event); + if(skip_trigger) { + core.units.begin()->step = 7; + return FALSE; + } + core.new_fchain_s.splice(core.new_fchain_s.begin(), core.new_fchain); + core.new_ochain_s.splice(core.new_ochain_s.begin(), core.new_ochain); + core.full_event.splice(core.full_event.end(), core.delayed_activate_event); + core.delayed_quick.clear(); + core.delayed_quick_tmp.swap(core.delayed_quick); + core.current_player = infos.turn_player; + core.units.begin()->step = 1; + return FALSE; + } + case 1: { + return FALSE; + } + case 2: { + //forced trigger + core.select_chains.clear(); + for (auto clit = core.new_fchain_s.begin(); clit != core.new_fchain_s.end(); ) { + effect* peffect = clit->triggering_effect; + card* phandler = peffect->get_handler(); + if(phandler->is_has_relation(*clit)) //work around: position and control should be refreshed before raising event + clit->set_triggering_state(phandler); + if(clit->triggering_player != phandler->current.controler && !peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER)) { + clit->triggering_player = phandler->current.controler; + clit->set_triggering_state(phandler); + } + uint8_t tp = clit->triggering_player; + if(check_trigger_effect(*clit) && peffect->is_chainable(tp) + && peffect->is_activateable(tp, clit->evt, !peffect->is_flag(EFFECT_FLAG_ACTIVATE_CONDITION))) { + if(tp == core.current_player) { + clit->flag |= CHAIN_FORCED; + core.select_chains.push_back(*clit); + } + } else { + peffect->active_type = 0; + clit = core.new_fchain_s.erase(clit); + continue; + } + ++clit; + } + if(core.select_chains.size() == 0) { + returns.ivalue[0] = -1; + } else if(core.select_chains.size() == 1) { + returns.ivalue[0] = 0; + } else { + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, core.current_player, 0x7f); + } + return FALSE; + } + case 3: { + if(returns.ivalue[0] == -1) { + if(core.new_fchain_s.size()) { + core.current_player = 1 - infos.turn_player; + core.units.begin()->step = 1; + } else { + core.current_player = infos.turn_player; + } + return FALSE; + } + chain newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + uint8_t tp = newchain.triggering_player; + peffect->get_handler()->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(tp); + core.new_chains.push_back(newchain); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + core.new_fchain_s.remove_if([chain_id = newchain.chain_id](chain ch) { return ch.chain_id == chain_id; }); + core.units.begin()->step = 1; + return FALSE; + } + case 4: { + //optional trigger + core.select_chains.clear(); + for (auto clit = core.new_ochain_s.begin(); clit != core.new_ochain_s.end(); ) { + effect* peffect = clit->triggering_effect; + card* phandler = peffect->get_handler(); + if(phandler->is_has_relation(*clit)) //work around: position and control should be refreshed before raising event + clit->set_triggering_state(phandler); + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) && (peffect->type & EFFECT_TYPE_FIELD) + && (peffect->range & LOCATION_HAND) && phandler->current.location == LOCATION_HAND) { + if(!phandler->is_has_relation(*clit) && peffect->is_condition_check(phandler->current.controler, clit->evt)) + phandler->create_relation(*clit); + peffect->set_activate_location(); + clit->triggering_player = phandler->current.controler; + clit->set_triggering_state(phandler); + } + if(clit->triggering_player != phandler->current.controler && !peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER)) { + clit->triggering_player = phandler->current.controler; + clit->set_triggering_state(phandler); + } + uint8_t tp = clit->triggering_player; + if(check_nonpublic_trigger(*clit) && check_trigger_effect(*clit) && peffect->is_chainable(tp) + && peffect->is_activateable(tp, clit->evt, !peffect->is_flag(EFFECT_FLAG_ACTIVATE_CONDITION)) + && check_spself_from_hand_trigger(*clit)) { + if(tp == core.current_player) + core.select_chains.push_back(*clit); + } else { + peffect->active_type = 0; + clit = core.new_ochain_s.erase(clit); + continue; + } + ++clit; + } + if(core.select_chains.size() == 0) { + returns.ivalue[0] = -1; + core.units.begin()->step = 5; + return FALSE; + } else if(core.select_chains.size() == 1 && !core.current_chain.size()) { + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.select_chains[0].triggering_effect->get_handler(), core.current_player, 221); + return FALSE; + } else { + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, core.current_player, 0x7f); + core.units.begin()->step = 5; + return FALSE; + } + return FALSE; + } + case 5: { + --returns.ivalue[0]; + return FALSE; + } + case 6: { + if(returns.ivalue[0] == -1) { + for(const auto& ch : core.select_chains) { + ch.triggering_effect->active_type = 0; + core.new_ochain_s.remove_if([chain_id = ch.chain_id](chain ch) { return ch.chain_id == chain_id; }); + } + if(core.new_ochain_s.size()) { + core.current_player = 1 - infos.turn_player; + core.units.begin()->step = 3; + } else { + core.current_player = infos.turn_player; + core.units.begin()->step = 6; + } + return FALSE; + } + chain newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + uint8_t tp = newchain.triggering_player; + peffect->get_handler()->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(tp); + core.new_chains.push_back(newchain); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + core.new_ochain_s.remove_if([chain_id = newchain.chain_id](chain ch) { return ch.chain_id == chain_id; }); + core.new_ochain_h.remove_if([chain_id = newchain.chain_id](chain ch) { return ch.chain_id == chain_id; }); + core.units.begin()->step = 3; + return FALSE; + } + case 7: { + core.select_chains.clear(); + return FALSE; + } + case 8: { + if((core.duel_rule >= 2) || (infos.phase != PHASE_MAIN1 && infos.phase != PHASE_MAIN2)) + return FALSE; + // Obsolete ignition effect ruling + tevent evt; + if(core.current_chain.size() == 0 && + (check_event(EVENT_SUMMON_SUCCESS, &evt) || check_event(EVENT_SPSUMMON_SUCCESS, &evt) || check_event(EVENT_FLIP_SUMMON_SUCCESS, &evt)) + && evt.reason_player == infos.turn_player) { + chain newchain; + tevent e; + e.event_cards = 0; + e.event_value = 0; + e.event_player = PLAYER_NONE; + e.reason_effect = 0; + e.reason = 0; + e.reason_player = PLAYER_NONE; + for(auto eit = effects.ignition_effect.begin(); eit != effects.ignition_effect.end();) { + effect* peffect = eit->second; + ++eit; + card* phandler = peffect->get_handler(); + e.event_code = peffect->code; + if(phandler->current.location == LOCATION_MZONE && peffect->is_chainable(infos.turn_player) + && peffect->is_activateable(infos.turn_player, e)) { + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = e; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = infos.turn_player; + core.ignition_priority_chains.push_back(newchain); + } + } + } + return FALSE; + } + case 9: { + infos.priorities[0] = 0; + infos.priorities[1] = 0; + if(core.current_chain.size() == 0) { + if(!core.hand_adjusted) + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, skip_freechain, infos.turn_player); + } else + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - core.current_chain.back().triggering_player); + return FALSE; + } + case 10: { + core.new_ochain_h.clear(); + core.full_event.clear(); + core.delayed_quick.clear(); + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + if(core.current_chain.size()) { + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, skip_trigger | ((skip_freechain | skip_new) << 8), skip_new); + } else { + core.used_event.splice(core.used_event.end(), core.point_event); + for(auto& ch_lim_p : core.chain_limit_p) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim_p.function); + core.chain_limit_p.clear(); + core.effect_count_code_chain[0].clear(); + core.effect_count_code_chain[1].clear(); + core.effect_count_code_chain[PLAYER_NONE].clear(); + reset_chain(); + returns.ivalue[0] = FALSE; + } + return TRUE; + } + case 30: { + // Battle Step, EFFECT_TYPE_CONTINUOUS with EVENT_FREE_CHAIN + int32_t check_player = infos.turn_player; + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + chain newchain; + core.select_chains.clear(); + core.spe_effect[check_player] = 0; + auto pr = effects.continuous_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + newchain.triggering_effect = peffect; + if(peffect->get_handler_player() == check_player && peffect->is_activateable(check_player, free_event)) { + core.select_chains.push_back(newchain); + ++core.spe_effect[check_player]; + } + } + if(!core.select_chains.empty()) + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, check_player, core.spe_effect[check_player]); + else + core.units.begin()->step = 31; + return FALSE; + } + case 31: { + if(returns.ivalue[0] == -1) + return FALSE; + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + const chain& newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + core.select_chains.clear(); + solve_continuous(peffect->get_handler_player(), peffect, free_event); + core.units.begin()->step = 29; + return FALSE; + } + case 32: { + int32_t check_player = 1 - infos.turn_player; + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + chain newchain; + core.select_chains.clear(); + core.spe_effect[check_player] = 0; + auto pr = effects.continuous_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + newchain.triggering_effect = peffect; + if(peffect->get_handler_player() == check_player && peffect->is_activateable(check_player, free_event)) { + core.select_chains.push_back(newchain); + ++core.spe_effect[check_player]; + } + } + if(!core.select_chains.empty()) + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, check_player, core.spe_effect[check_player]); + else + core.units.begin()->step = -1; + return FALSE; + } + case 33: { + if(returns.ivalue[0] == -1) { + core.units.begin()->step = -1; + return FALSE; + } + const chain& newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + core.select_chains.clear(); + solve_continuous(peffect->get_handler_player(), peffect, free_event); + core.units.begin()->step = 31; + return FALSE; + } + } + return TRUE; +} +int32_t field::process_quick_effect(int16_t step, int32_t skip_freechain, uint8_t priority) { + switch(step) { + case 0: { + uint8_t check_player = infos.turn_player; + if(core.units.begin()->arg3) + check_player = 1 - infos.turn_player; + core.select_chains.clear(); + for(auto ifit = core.quick_f_chain.begin(); ifit != core.quick_f_chain.end(); ) { + effect* peffect = ifit->first; + card* phandler = peffect->get_handler(); + if(peffect->is_chainable(ifit->second.triggering_player) && peffect->check_count_limit(ifit->second.triggering_player) + && phandler->is_has_relation(ifit->second)) { + if(ifit->second.triggering_player == check_player) { + ifit->second.flag |= CHAIN_FORCED; + core.select_chains.push_back(ifit->second); + } + } else { + ifit = core.quick_f_chain.erase(ifit); + continue; + } + ++ifit; + } + if(core.select_chains.size() == 0) + returns.ivalue[0] = -1; + else if(core.select_chains.size() == 1) + returns.ivalue[0] = 0; + else + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, check_player, 0); + return FALSE; + } + case 1: { + if(returns.ivalue[0] == -1) { + if(core.quick_f_chain.size()) { + core.units.begin()->arg3 = TRUE; + core.units.begin()->step = -1; + } else if(core.new_chains.size()) { + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - core.new_chains.back().triggering_player); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + return TRUE; + } + return FALSE; + } + chain newchain = core.select_chains[returns.ivalue[0]]; + effect* peffect = newchain.triggering_effect; + uint8_t tp = newchain.triggering_player; + peffect->get_handler()->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(tp); + core.new_chains.push_back(newchain); + core.quick_f_chain.erase(peffect); + core.units.begin()->step = -1; + return FALSE; + } + case 2: { + chain newchain; + if(core.ignition_priority_chains.size()) + core.select_chains.swap(core.ignition_priority_chains); + for(const auto& ev_list : { core.point_event, core.instant_event }) { + for(const auto& ev : ev_list) { + auto pr = effects.activate_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_flag(EFFECT_FLAG_DELAY) && peffect->is_chainable(priority) && peffect->is_activateable(priority, ev)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + } + } + pr = effects.quick_o_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(!peffect->is_flag(EFFECT_FLAG_DELAY) && peffect->is_chainable(priority) && peffect->is_activateable(priority, ev)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + } + } + } + } + for(auto& ch : core.new_ochain_h) { + effect* peffect = ch.triggering_effect; + card* phandler = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) && (peffect->type & EFFECT_TYPE_FIELD) + && (peffect->range & LOCATION_HAND) && phandler->current.location == LOCATION_HAND) { + if(!phandler->is_has_relation(ch) && peffect->is_condition_check(phandler->current.controler, ch.evt)) + phandler->create_relation(ch); + peffect->set_activate_location(); + ch.triggering_player = phandler->current.controler; + ch.set_triggering_state(phandler); + } + if(ch.triggering_player == priority && !phandler->is_status(STATUS_CHAINING) + && (ch.triggering_location == LOCATION_HAND && phandler->is_position(POS_FACEDOWN) || ch.triggering_location == LOCATION_DECK) + && phandler->is_has_relation(ch) && peffect->is_chainable(priority) && peffect->is_activateable(priority, ch.evt, TRUE) + && check_spself_from_hand_trigger(ch)) + core.select_chains.push_back(ch); + } + //delayed activate + for(const auto& ev : core.full_event) { + auto pr = effects.activate_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(peffect->is_flag(EFFECT_FLAG_DELAY) && peffect->is_chainable(priority) && peffect->is_activateable(priority, ev)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + } + } + pr = effects.quick_o_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_flag(EFFECT_FLAG_DELAY) && peffect->is_condition_check(peffect->get_handler()->current.controler, ev)) + core.delayed_quick.emplace(peffect, ev); + } + } + // delayed quick + for(auto eit = core.delayed_quick.begin(); eit != core.delayed_quick.end();) { + effect* peffect = eit->first; + const tevent& evt = eit->second; + ++eit; + peffect->set_activate_location(); + if(peffect->is_chainable(priority) && peffect->is_activateable(priority, evt, TRUE)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = evt; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + } + } + core.spe_effect[priority] = (int32_t)core.select_chains.size(); + if(!skip_freechain) { + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + auto pr = effects.activate_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(peffect->is_chainable(priority) && peffect->is_activateable(priority, free_event)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = free_event; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + if(check_hint_timing(peffect) || check_cteffect_hint(peffect, priority)) + ++core.spe_effect[priority]; + } + } + pr = effects.quick_o_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + if(peffect->is_chainable(priority) && peffect->is_activateable(priority, free_event)) { + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = free_event; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + newchain.triggering_player = priority; + core.select_chains.push_back(newchain); + if(check_hint_timing(peffect)) + ++core.spe_effect[priority]; + } + } + } + if(core.current_chain.size() || (core.hint_timing[0] & TIMING_ATTACK) || (core.hint_timing[1] & TIMING_ATTACK)) + core.spe_effect[priority] = (int32_t)core.select_chains.size(); + add_process(PROCESSOR_SELECT_CHAIN, 0, 0, 0, priority, core.spe_effect[priority]); + return FALSE; + } + case 3: { + if(core.select_chains.size() && returns.ivalue[0] != -1) { + chain newchain = core.select_chains[returns.ivalue[0]]; + core.new_chains.push_back(newchain); + effect* peffect = newchain.triggering_effect; + core.delayed_quick.erase(delayed_effect_collection::value_type(peffect, newchain.evt)); + peffect->get_handler()->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(priority); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - priority); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + } else { + infos.priorities[priority] = 1; + if(!infos.priorities[0] || !infos.priorities[1]) + add_process(PROCESSOR_QUICK_EFFECT, 1, 0, 0, skip_freechain, 1 - priority); + else { + core.hint_timing[0] &= TIMING_DAMAGE_STEP | TIMING_DAMAGE_CAL; + core.hint_timing[1] &= TIMING_DAMAGE_STEP | TIMING_DAMAGE_CAL; + } + } + core.select_chains.clear(); + return TRUE; + } + } + return TRUE; +} +int32_t field::process_instant_event() { + if (core.queue_event.size() == 0) + return TRUE; + chain newchain; + chain_list tp; + chain_list ntp; + for(const auto& ev : core.queue_event) { + //continuous events + auto pr = effects.continuous_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + uint8_t owner_player = peffect->get_handler_player(); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (ev.event_player == 0 || ev.event_player == 1)) + owner_player = ev.event_player; + if(peffect->is_activateable(owner_player, ev)) { + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = peffect; + newchain.triggering_player = owner_player; + newchain.evt = ev; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + if(peffect->is_flag(EFFECT_FLAG_DELAY) && (core.chain_solving || core.conti_solving)) { + if(owner_player == infos.turn_player) + core.delayed_continuous_tp.push_back(newchain); + else + core.delayed_continuous_ntp.push_back(newchain); + } else { + if(owner_player == infos.turn_player) + tp.push_back(newchain); + else + ntp.push_back(newchain); + } + } + } + if (is_continuous_event(ev.event_code)) + continue; + //triggers + pr = effects.trigger_f_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + card* phandler = peffect->get_handler(); + if(!phandler->is_status(STATUS_EFFECT_ENABLED)) + continue; + if(!peffect->is_flag(EFFECT_FLAG_ACTIVATE_CONDITION) && !peffect->is_condition_check(phandler->current.controler, ev)) + continue; + peffect->set_activate_location(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (ev.event_player == 0 || ev.event_player == 1)) + newchain.triggering_player = ev.event_player; + else + newchain.triggering_player = phandler->current.controler; + core.new_fchain.push_back(newchain); + phandler->create_relation(newchain); + } + pr = effects.trigger_o_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + card* phandler = peffect->get_handler(); + bool act = phandler->is_status(STATUS_EFFECT_ENABLED) + && (peffect->is_flag(EFFECT_FLAG_ACTIVATE_CONDITION) || peffect->is_condition_check(phandler->current.controler, ev)); + if(!act && !(peffect->range & LOCATION_HAND)) + continue; + peffect->set_activate_location(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (ev.event_player == 0 || ev.event_player == 1)) + newchain.triggering_player = ev.event_player; + else + newchain.triggering_player = phandler->current.controler; + core.new_ochain.push_back(newchain); + if(peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) + || !(peffect->range & LOCATION_HAND) + || (peffect->range & phandler->current.location) && act) + phandler->create_relation(newchain); + } + //instant_f + pr = effects.quick_f_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + card* phandler = peffect->get_handler(); + peffect->set_activate_location(); + if(peffect->is_activateable(phandler->current.controler, ev)) { + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = ev; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (ev.event_player == 0 || ev.event_player == 1)) + newchain.triggering_player = ev.event_player; + else + newchain.triggering_player = phandler->current.controler; + core.quick_f_chain[peffect] = newchain; + phandler->create_relation(newchain); + } + } + // delayed activate effect + core.delayed_activate_event.push_back(ev); + // delayed quick effect + pr = effects.quick_o_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->is_flag(EFFECT_FLAG_DELAY) && peffect->is_condition_check(peffect->get_handler()->current.controler, ev)) + core.delayed_quick_tmp.emplace(peffect, ev); + } + } + while(tp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), tp, tp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } + while(ntp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), ntp, ntp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } + core.instant_event.splice(core.instant_event.end(), core.queue_event); + return TRUE; +} +int32_t field::process_single_event() { + if(core.single_event.size() == 0) + return TRUE; + chain_list tp; + chain_list ntp; + for(const auto& ev : core.single_event) { + card* starget = ev.trigger_card; + auto pr = starget->single_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + process_single_event(peffect, ev, tp, ntp); + } + for(auto& pcard : starget->xyz_materials) { + pr = pcard->xmaterial_effect.equal_range(ev.event_code); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(peffect->type & EFFECT_TYPE_FIELD) + continue; + process_single_event(peffect, ev, tp, ntp); + } + } + } + while(tp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), tp, tp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } + while(ntp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), ntp, ntp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } + core.single_event.clear(); + return TRUE; +} +int32_t field::process_single_event(effect* peffect, const tevent& e, chain_list& tp, chain_list& ntp) { + if(!(peffect->type & EFFECT_TYPE_ACTIONS)) + return FALSE; + if((peffect->type & EFFECT_TYPE_FLIP) && (e.event_value & (NO_FLIP_EFFECT >> 16))) + return FALSE; + //continuous & trigger (single) + if(peffect->type & EFFECT_TYPE_CONTINUOUS) { + uint8_t owner_player = peffect->get_handler_player(); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (e.event_player == 0 || e.event_player == 1)) + owner_player = e.event_player; + if(peffect->is_activateable(owner_player, e)) { + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = peffect; + newchain.triggering_player = owner_player; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + if(peffect->is_flag(EFFECT_FLAG_DELAY) && (core.chain_solving || core.conti_solving)) { + if(owner_player == infos.turn_player) + core.delayed_continuous_tp.push_back(newchain); + else + core.delayed_continuous_ntp.push_back(newchain); + } else { + if(owner_player == infos.turn_player) + tp.push_back(newchain); + else + ntp.push_back(newchain); + } + } + } else { + card* phandler = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_ACTIVATE_CONDITION) && !peffect->is_condition_check(phandler->current.controler, e)) + return FALSE; + peffect->set_activate_location(); + chain newchain; + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt = e; + newchain.triggering_effect = peffect; + newchain.set_triggering_state(phandler); + if(peffect->is_flag(EFFECT_FLAG_EVENT_PLAYER) && (e.event_player == 0 || e.event_player == 1)) + newchain.triggering_player = e.event_player; + else { + if(phandler->current.reason & REASON_TEMPORARY) + newchain.triggering_player = phandler->previous.controler; + else + newchain.triggering_player = newchain.triggering_controler; + } + if(core.flip_delayed && e.event_code == EVENT_FLIP) { + if (peffect->type & EFFECT_TYPE_TRIGGER_O) + core.new_ochain_b.push_back(newchain); + else + core.new_fchain_b.push_back(newchain); + } else { + if (peffect->type & EFFECT_TYPE_TRIGGER_O) + core.new_ochain.push_back(newchain); + else + core.new_fchain.push_back(newchain); + } + peffect->set_active_type(); + phandler->create_relation(newchain); + effect* deffect = phandler->is_affected_by_effect(EFFECT_DISABLE_EFFECT); + if(deffect) { + effect* negeff = pduel->new_effect(); + negeff->owner = deffect->owner; + negeff->type = EFFECT_TYPE_SINGLE; + negeff->code = EFFECT_DISABLE_CHAIN; + negeff->value = newchain.chain_id; + negeff->reset_flag = RESET_EVENT | deffect->get_value(); + phandler->add_effect(negeff); + } + } + return TRUE; +} +int32_t field::process_idle_command(uint16_t step) { + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + switch(step) { + case 0: { + bool must_attack = false; + core.select_chains.clear(); + chain newchain; + core.to_bp = TRUE; + core.to_ep = TRUE; + if((!(core.duel_options & DUEL_ATTACK_FIRST_TURN) && infos.turn_id == 1) || infos.phase == PHASE_MAIN2 || is_player_affected_by_effect(infos.turn_player, EFFECT_CANNOT_BP)) + core.to_bp = FALSE; + if(infos.phase == PHASE_MAIN1) { + for(auto& pcard : player[infos.turn_player].list_mzone) { + if(pcard && pcard->is_capable_attack() && pcard->is_affected_by_effect(EFFECT_MUST_ATTACK)) { + must_attack = true; + break; + } + } + if(core.to_bp && (must_attack || is_player_affected_by_effect(infos.turn_player, EFFECT_CANNOT_EP))) + core.to_ep = FALSE; + } + if((infos.phase == PHASE_MAIN1 && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_M1)) + || (infos.phase == PHASE_MAIN2 && is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_M2))) { + if(core.to_bp && core.to_ep) { + core.select_options.clear(); + core.select_options.push_back(80); + core.select_options.push_back(81); + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, infos.turn_player, 0); + core.units.begin()->step = 11; + } else if(core.to_bp) { + core.units.begin()->arg1 = 6; + core.units.begin()->step = 10; + reset_phase(infos.phase); + adjust_all(); + } else { + core.units.begin()->arg1 = 7; + core.units.begin()->step = 10; + reset_phase(infos.phase); + adjust_all(); + } + return FALSE; + } + if((infos.phase == PHASE_MAIN2) && core.skip_m2) { + core.skip_m2 = FALSE; + returns.ivalue[0] = 7; + return FALSE; + } + auto pr = effects.activate_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + newchain.triggering_effect = peffect; + if(peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + pr = effects.quick_o_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + newchain.triggering_effect = peffect; + if(peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + pr = effects.continuous_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + newchain.triggering_effect = peffect; + if(peffect->get_handler_player() == infos.turn_player && peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + for(auto eit = effects.ignition_effect.begin(); eit != effects.ignition_effect.end();) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + newchain.triggering_effect = peffect; + if(peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + core.summonable_cards.clear(); + for(auto& pcard : player[infos.turn_player].list_hand) + if(pcard->is_can_be_summoned(infos.turn_player, FALSE, 0, 0)) + core.summonable_cards.push_back(pcard); + for(auto& pcard : player[infos.turn_player].list_mzone) { + if(pcard && pcard->is_can_be_summoned(infos.turn_player, FALSE, 0, 0)) + core.summonable_cards.push_back(pcard); + } + core.spsummonable_cards.clear(); + effect_set eset; + filter_field_effect(EFFECT_SPSUMMON_PROC, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + card* pcard = eset[i]->get_handler(); + if(!eset[i]->check_count_limit(pcard->current.controler)) + continue; + if(pcard->current.controler == infos.turn_player && pcard->is_special_summonable(infos.turn_player, 0)) + core.spsummonable_cards.push_back(pcard); + } + eset.clear(); + filter_field_effect(EFFECT_SPSUMMON_PROC_G, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + card* pcard = eset[i]->get_handler(); + if(!eset[i]->check_count_limit(infos.turn_player)) + continue; + if(pcard->current.controler != infos.turn_player && !eset[i]->is_flag(EFFECT_FLAG_BOTH_SIDE)) + continue; + effect* oreason = core.reason_effect; + uint8_t op = core.reason_player; + core.reason_effect = eset[i]; + core.reason_player = pcard->current.controler; + save_lp_cost(); + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(pcard, PARAM_TYPE_CARD); + if(pduel->lua->check_condition(eset[i]->condition, 2)) + core.spsummonable_cards.push_back(pcard); + restore_lp_cost(); + core.reason_effect = oreason; + core.reason_player = op; + } + core.repositionable_cards.clear(); + for(auto& pcard : player[infos.turn_player].list_mzone) { + if(pcard && ((pcard->is_position(POS_FACEUP | POS_FACEDOWN_ATTACK) && pcard->is_capable_change_position(infos.turn_player)) + || (pcard->is_position(POS_FACEDOWN) && pcard->is_can_be_flip_summoned(infos.turn_player)))) + core.repositionable_cards.push_back(pcard); + } + core.msetable_cards.clear(); + core.ssetable_cards.clear(); + for(auto& pcard : player[infos.turn_player].list_hand) { + if(pcard->is_setable_mzone(infos.turn_player, FALSE, 0, 0)) + core.msetable_cards.push_back(pcard); + if(pcard->is_setable_szone(infos.turn_player)) + core.ssetable_cards.push_back(pcard); + } + add_process(PROCESSOR_SELECT_IDLECMD, 0, 0, 0, infos.turn_player, 0); + return FALSE; + } + case 1: { + uint32_t ctype = returns.ivalue[0] & 0xffff; + uint32_t sel = returns.ivalue[0] >> 16; + if(ctype == 5) { + chain newchain = core.select_chains[sel]; + effect* peffect = newchain.triggering_effect; + if(peffect->type & EFFECT_TYPE_CONTINUOUS) { + core.select_chains.clear(); + solve_continuous(peffect->get_handler_player(), peffect, free_event); + core.units.begin()->step = 2; + return FALSE; + } + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt.event_code = peffect->code; + newchain.evt.event_player = PLAYER_NONE; + newchain.evt.event_value = 0; + newchain.evt.event_cards = 0; + newchain.evt.reason = 0; + newchain.evt.reason_effect = 0; + newchain.evt.reason_player = PLAYER_NONE; + newchain.set_triggering_state(phandler); + newchain.triggering_player = infos.turn_player; + core.new_chains.push_back(newchain); + phandler->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(infos.turn_player); + core.select_chains.clear(); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - infos.turn_player); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + core.select_chains.clear(); + return FALSE; + } else if(ctype == 0) { + core.units.begin()->step = 4; + return FALSE; + } else if(ctype == 1) { + core.units.begin()->step = 5; + return FALSE; + } else if(ctype == 2) { + core.units.begin()->step = 6; + return FALSE; + } else if(ctype == 3) { + core.units.begin()->step = 7; + return FALSE; + } else if(ctype == 4) { + core.units.begin()->step = 8; + return FALSE; + } else if (ctype == 8) { + core.units.begin()->step = -1; + shuffle(infos.turn_player, LOCATION_HAND); + infos.can_shuffle = FALSE; + return FALSE; + } else { + core.units.begin()->step = 9; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1 - infos.turn_player); + pduel->write_buffer32(23); + core.select_chains.clear(); + core.hint_timing[infos.turn_player] = TIMING_MAIN_END; + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - infos.turn_player); + infos.priorities[infos.turn_player] = 1; + infos.priorities[1 - infos.turn_player] = 0; + core.units.begin()->arg1 = ctype; + return FALSE; + } + return TRUE; + } + case 2: { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 3: { + adjust_instant(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 5: { + card* target = core.summonable_cards[returns.ivalue[0] >> 16]; + core.summon_cancelable = TRUE; + summon(infos.turn_player, target, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 6: { + card* target = core.spsummonable_cards[returns.ivalue[0] >> 16]; + core.summon_cancelable = TRUE; + special_summon_rule(infos.turn_player, target, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 7: { + card* target = core.repositionable_cards[returns.ivalue[0] >> 16]; + if(target->is_position(POS_FACEUP_ATTACK)) { + core.phase_action = TRUE; + change_position(target, 0, infos.turn_player, POS_FACEUP_DEFENSE, FALSE); + adjust_all(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } else if(target->is_position(POS_FACEUP_DEFENSE)) { + core.phase_action = TRUE; + change_position(target, 0, infos.turn_player, POS_FACEUP_ATTACK, FALSE); + adjust_all(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } else if(target->is_position(POS_FACEDOWN_ATTACK)) { + //deprecated rule (POS_FACEDOWN_ATTACK) + core.units.begin()->ptarget = (group*)target; + int32_t positions = 0; + if(target->is_capable_change_position(infos.turn_player)) + positions |= POS_FACEDOWN_DEFENSE; + if(target->is_can_be_flip_summoned(infos.turn_player)) + positions |= POS_FACEUP_ATTACK; + add_process(PROCESSOR_SELECT_POSITION, 0, 0, 0, infos.turn_player + (positions << 16), target->data.code); + core.units.begin()->step = 12; + return FALSE; + } else + add_process(PROCESSOR_FLIP_SUMMON, 0, 0, (group*)target, target->current.controler, 0); + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.units.begin()->step = -1; + return FALSE; + } + case 8: { + card* target = core.msetable_cards[returns.ivalue[0] >> 16]; + core.summon_cancelable = TRUE; + mset(target->current.controler, target, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 9: { + card* target = core.ssetable_cards[returns.ivalue[0] >> 16]; + add_process(PROCESSOR_SSET, 0, 0, (group*)target, target->current.controler, target->current.controler); + core.units.begin()->step = -1; + return FALSE; + } + case 10: { + //end announce + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + if(core.current_chain.size()) { + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + reset_phase(infos.phase); + adjust_all(); + return FALSE; + } + case 11: { + returns.ivalue[0] = (int32_t)core.units.begin()->arg1; + infos.can_shuffle = TRUE; + return TRUE; + } + case 12: { + if(returns.ivalue[0] == 0) + core.units.begin()->arg1 = 6; + else + core.units.begin()->arg1 = 7; + reset_phase(infos.phase); + adjust_all(); + core.units.begin()->step = 10; + return FALSE; + } + case 13: { + //deprecated rule (POS_FACEDOWN_ATTACK) + card* target = (card*)core.units.begin()->ptarget; + if(returns.ivalue[0] == POS_FACEUP_ATTACK) + add_process(PROCESSOR_FLIP_SUMMON, 0, 0, (group*)target, target->current.controler, 0); + else { + core.phase_action = TRUE; + change_position(target, 0, infos.turn_player, POS_FACEDOWN_DEFENSE, FALSE); + adjust_all(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, FALSE, 0); + } + target->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.units.begin()->step = -1; + return FALSE; + } + } + return TRUE; +} +int32_t field::process_battle_command(uint16_t step) { + tevent free_event; + free_event.event_code = EVENT_FREE_CHAIN; + switch(step) { + case 0: { + core.select_chains.clear(); + chain newchain; + if(!core.chain_attack) { + core.chain_attacker_id = 0; + core.chain_attack_target = 0; + } + core.attack_player = FALSE; + core.attacker = 0; + core.attack_target = 0; + effect* skip_effect = is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP); + if(skip_effect) { + core.units.begin()->step = 41; + core.units.begin()->arg1 = 2; + if(is_player_affected_by_effect(infos.turn_player, EFFECT_BP_TWICE)) + core.units.begin()->arg2 = 1; + else core.units.begin()->arg2 = 0; + if(!skip_effect->value) { + reset_phase(PHASE_BATTLE_STEP); + adjust_all(); + infos.phase = PHASE_BATTLE; + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_BATTLE, 0); + } else { + core.hint_timing[infos.turn_player] = 0; + reset_phase(PHASE_BATTLE); + adjust_all(); + } + return FALSE; + } + auto pr = effects.activate_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + newchain.triggering_effect = peffect; + if(peffect->is_activateable(infos.turn_player, free_event) && peffect->get_speed() > 1) + core.select_chains.push_back(newchain); + } + pr = effects.quick_o_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + peffect->set_activate_location(); + newchain.triggering_effect = peffect; + if(peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + pr = effects.continuous_effect.equal_range(EVENT_FREE_CHAIN); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + newchain.triggering_effect = peffect; + if(peffect->get_handler_player() == infos.turn_player && peffect->is_activateable(infos.turn_player, free_event)) + core.select_chains.push_back(newchain); + } + core.attackable_cards.clear(); + card_vector first_attack; + card_vector must_attack; + if(!is_player_affected_by_effect(infos.turn_player, EFFECT_CANNOT_ATTACK_ANNOUNCE)) { + for(auto& pcard : player[infos.turn_player].list_mzone) { + if(!pcard) + continue; + if(!pcard->is_capable_attack_announce(infos.turn_player)) + continue; + uint8_t chain_attack = FALSE; + if(core.chain_attack && core.chain_attacker_id == pcard->fieldid) + chain_attack = TRUE; + card_vector cv; + get_attack_target(pcard, &cv, chain_attack); + if(cv.size() == 0 && pcard->direct_attackable == 0) + continue; + core.attackable_cards.push_back(pcard); + if(pcard->is_affected_by_effect(EFFECT_FIRST_ATTACK)) + first_attack.push_back(pcard); + if(pcard->is_affected_by_effect(EFFECT_MUST_ATTACK)) + must_attack.push_back(pcard); + } + if(first_attack.size()) + core.attackable_cards = first_attack; + } + core.to_m2 = TRUE; + core.to_ep = TRUE; + if(must_attack.size() || is_player_affected_by_effect(infos.turn_player, EFFECT_CANNOT_M2)) + core.to_m2 = FALSE; + if(must_attack.size()) + core.to_ep = FALSE; + core.attack_cancelable = TRUE; + add_process(PROCESSOR_SELECT_BATTLECMD, 0, 0, 0, infos.turn_player, 0); + return FALSE; + } + case 1: { + int32_t ctype = returns.ivalue[0] & 0xffff; + int32_t sel = returns.ivalue[0] >> 16; + if(ctype == 0) { + chain newchain = core.select_chains[sel]; + effect* peffect = newchain.triggering_effect; + if(peffect->type & EFFECT_TYPE_CONTINUOUS) { + core.select_chains.clear(); + solve_continuous(peffect->get_handler_player(), peffect, free_event); + core.units.begin()->step = 13; + return FALSE; + } + card* phandler = peffect->get_handler(); + newchain.flag = 0; + newchain.chain_id = infos.field_id++; + newchain.evt.event_code = peffect->code; + newchain.evt.event_player = PLAYER_NONE; + newchain.evt.event_value = 0; + newchain.evt.event_cards = 0; + newchain.evt.reason = 0; + newchain.evt.reason_effect = 0; + newchain.evt.reason_player = PLAYER_NONE; + newchain.set_triggering_state(phandler); + newchain.triggering_player = infos.turn_player; + core.new_chains.push_back(newchain); + phandler->set_status(STATUS_CHAINING, TRUE); + peffect->dec_count(infos.turn_player); + core.select_chains.clear(); + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - infos.turn_player); + infos.priorities[0] = 0; + infos.priorities[1] = 0; + core.select_chains.clear(); + return FALSE; + } else if(ctype == 1) { + core.units.begin()->step = 2; + core.units.begin()->arg3 = FALSE; + core.attacker = core.attackable_cards[sel]; + core.attacker->set_status(STATUS_ATTACK_CANCELED, FALSE); + core.attacker->attack_controler = core.attacker->current.controler; + core.pre_field[0] = core.attacker->fieldid_r; + if(core.chain_attack && core.chain_attacker_id != core.attacker->fieldid) { + core.chain_attack = FALSE; + core.chain_attacker_id = 0; + } + effect_set eset; + filter_player_effect(infos.turn_player, EFFECT_ATTACK_COST, &eset, FALSE); + core.attacker->filter_effect(EFFECT_ATTACK_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->operation) { + core.attack_cancelable = FALSE; + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, infos.turn_player, 0); + adjust_all(); + } + } + return FALSE; + } else { + core.units.begin()->step = 39; + core.units.begin()->arg1 = ctype; + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1 - infos.turn_player); + pduel->write_buffer32(29); + core.select_chains.clear(); + core.hint_timing[infos.turn_player] = TIMING_BATTLE_STEP_END; + add_process(PROCESSOR_QUICK_EFFECT, 0, 0, 0, FALSE, 1 - infos.turn_player); + infos.priorities[infos.turn_player] = 1; + infos.priorities[1 - infos.turn_player] = 0; + return FALSE; + } + return TRUE; + } + case 2: { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 3: { + core.units.begin()->arg1 = FALSE; + if(core.attacker->is_status(STATUS_ATTACK_CANCELED)) { + core.units.begin()->arg3 = TRUE; + core.units.begin()->step = 6; + return FALSE; + } + return FALSE; + } + case 4: { + // select attack target(replay start point) + core.attack_player = FALSE; + core.select_cards.clear(); + auto atype = get_attack_target(core.attacker, &core.select_cards, core.chain_attack); + // direct attack + if(core.attacker->direct_attackable) { + if(core.select_cards.size() == 0) { + returns.ivalue[0] = -2; + core.units.begin()->step = 5; + return FALSE; + } + if(is_player_affected_by_effect(infos.turn_player, EFFECT_PATRICIAN_OF_DARKNESS)) { + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)core.attacker, 1 - infos.turn_player, 31); + } + else { + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, infos.turn_player, 31); + } + return FALSE; + } + // no target and not direct attackable + if(core.select_cards.size() == 0) { + core.units.begin()->arg3 = TRUE; + core.units.begin()->step = 6; + return FALSE; + } + bool must_attack_monster = atype == 3; + bool oppo_select = is_player_affected_by_effect(infos.turn_player, EFFECT_PATRICIAN_OF_DARKNESS); + if(!oppo_select && must_attack_monster && core.select_cards.size() > 1) { + effect_set eset; + core.attacker->filter_effect(EFFECT_MUST_ATTACK_MONSTER, &eset); + oppo_select = eset.size() > 1; + } + if(oppo_select) { + if(core.select_cards.size() == 1) + returns.bvalue[1] = 0; + else { + pduel->write_buffer8(MSG_BECOME_TARGET); + pduel->write_buffer8(1); + pduel->write_buffer32(core.attacker->get_info_location()); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(1 - infos.turn_player); + pduel->write_buffer32(549); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, 1 - infos.turn_player, 0x10001); + } + } else { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(infos.turn_player); + pduel->write_buffer32(549); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, infos.turn_player + (core.attack_cancelable && !must_attack_monster ? 0x20000 : 0), 0x10001); + } + core.units.begin()->step = 5; + return FALSE; + } + case 5: { + // the answer of "direct attack or not" + if(returns.ivalue[0]) { + returns.ivalue[0] = -2; + } else { + if(core.select_cards.size()) { + auto opposel = is_player_affected_by_effect(infos.turn_player, EFFECT_PATRICIAN_OF_DARKNESS); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SELECTMSG); + pduel->write_buffer8(opposel ? 1 - infos.turn_player : infos.turn_player); + pduel->write_buffer32(549); + add_process(PROCESSOR_SELECT_CARD, 0, 0, 0, opposel ? 1 - infos.turn_player : infos.turn_player + (core.attack_cancelable ? 0x20000 : 0), 0x10001); + } else { + core.units.begin()->arg3 = TRUE; + core.units.begin()->step = 6; + } + } + return FALSE; + } + case 6: { + if(returns.ivalue[0] == -1) {//cancel attack manually + if(core.units.begin()->arg1) { + core.units.begin()->step = 12; + return FALSE; + } + core.units.begin()->step = -1; + return FALSE; + } + if(returns.ivalue[0] == -2) + core.attack_target = 0; + else + core.attack_target = core.select_cards[returns.bvalue[1]]; + if(core.attack_target) + core.pre_field[1] = core.attack_target->fieldid_r; + else + core.pre_field[1] = 0; + return FALSE; + } + case 7: { + if(!core.units.begin()->arg1) { + core.phase_action = TRUE; + ++core.attack_state_count[infos.turn_player]; + check_card_counter(core.attacker, ACTIVITY_ATTACK, infos.turn_player); + ++core.attacker->attack_announce_count; + } + if(core.units.begin()->arg3) {//attack announce failed + ++core.attacker->announce_count; + core.attacker->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.chain_attack = FALSE; + core.units.begin()->step = -1; + } + return FALSE; + } + case 8: { + core.attack_cancelable = TRUE; + pduel->write_buffer8(MSG_ATTACK); + pduel->write_buffer32(core.attacker->get_info_location()); + if(core.attack_target) { + raise_single_event(core.attack_target, 0, EVENT_BE_BATTLE_TARGET, 0, 0, 0, 1 - infos.turn_player, 0); + raise_event(core.attack_target, EVENT_BE_BATTLE_TARGET, 0, 0, 0, 1 - infos.turn_player, 0); + pduel->write_buffer32(core.attack_target->get_info_location()); + } else + pduel->write_buffer32(0); + core.attack_rollback = FALSE; + core.opp_mzone.clear(); + for(auto& pcard : player[1 - infos.turn_player].list_mzone) { + if(pcard) + core.opp_mzone.insert(pcard->fieldid_r); + } + //core.units.begin()->arg1 ---> is rollbacked + if(!core.units.begin()->arg1) { + raise_single_event(core.attacker, 0, EVENT_ATTACK_ANNOUNCE, 0, 0, 0, infos.turn_player, 0); + raise_event(core.attacker, EVENT_ATTACK_ANNOUNCE, 0, 0, 0, infos.turn_player, 0); + } + core.attacker->attack_controler = core.attacker->current.controler; + core.pre_field[0] = core.attacker->fieldid_r; + process_single_event(); + process_instant_event(); + core.hint_timing[infos.turn_player] = TIMING_ATTACK; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + return FALSE; + } + case 9: { + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP) + || core.attacker->is_status(STATUS_ATTACK_CANCELED) || core.attack_rollback) { + core.units.begin()->step = 10; + return FALSE; + } + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(24); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(24); + core.hint_timing[0] = TIMING_BATTLE_PHASE; + core.hint_timing[1] = TIMING_BATTLE_PHASE; + add_process(PROCESSOR_POINT_EVENT, 30, 0, 0, 0, 0); + return FALSE; + } + case 10: { + if(returns.ivalue[0]) + core.units.begin()->step = 8; + else + adjust_all(); + return FALSE; + } + case 11: { + if(core.attacker->is_affected_by_effect(EFFECT_ATTACK_DISABLED)) { + core.attacker->reset(EFFECT_ATTACK_DISABLED, RESET_CODE); + pduel->write_buffer8(MSG_ATTACK_DISABLED); + core.attacker->set_status(STATUS_ATTACK_CANCELED, TRUE); + } + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP) + || core.attacker->is_status(STATUS_ATTACK_CANCELED)) { + core.units.begin()->step = 12; + return FALSE; + } + // go to damage step + if(!core.attack_rollback) { + ++core.attacker->announce_count; + core.attacker->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.attacker->announced_cards.addcard(core.attack_target); + attack_all_target_check(); + core.units.begin()->step = 18; + return FALSE; + } + card_vector cv; + get_attack_target(core.attacker, &cv, core.chain_attack); + if(!cv.size() && !core.attacker->direct_attackable) { + core.units.begin()->step = 12; + return FALSE; + } + // replay + if(!core.attacker->is_affected_by_effect(EFFECT_MUST_ATTACK)) + add_process(PROCESSOR_SELECT_YESNO, 0, 0, 0, infos.turn_player, 30); + else { + returns.ivalue[0] = TRUE; + core.attack_cancelable = FALSE; + } + return FALSE; + } + case 12: { + // answer of "replay or not" + if(returns.ivalue[0]) { + core.units.begin()->arg1 = TRUE; + core.units.begin()->arg3 = FALSE; + core.units.begin()->step = 3; + } + return FALSE; + } + case 13: { + if(core.attacker->fieldid_r == core.pre_field[0]) { + ++core.attacker->announce_count; + core.attacker->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.attacker->announced_cards.addcard(core.attack_target); + attack_all_target_check(); + } + core.chain_attack = FALSE; + core.units.begin()->step = -1; + reset_phase(PHASE_DAMAGE); + adjust_all(); + return FALSE; + } + case 14: { + adjust_instant(); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + core.units.begin()->step = -1; + return FALSE; + } + case 19: { + infos.phase = PHASE_DAMAGE; + core.chain_attack = FALSE; + core.units.begin()->arg1 = FALSE; + core.damage_calculated = FALSE; + core.selfdes_disabled = TRUE; + core.flip_delayed = TRUE; + core.attacker->attack_controler = core.attacker->current.controler; + core.pre_field[0] = core.attacker->fieldid_r; + if(core.attack_target) { + core.attack_target->attack_controler = core.attack_target->current.controler; + core.pre_field[1] = core.attack_target->fieldid_r; + } else + core.pre_field[1] = 0; + ++core.attacker->attacked_count; + core.attacker->attacked_cards.addcard(core.attack_target); + ++core.battled_count[infos.turn_player]; + adjust_all(); + return FALSE; + } + case 20: { + // start of PHASE_DAMAGE; + pduel->write_buffer8(MSG_DAMAGE_STEP_START); + raise_single_event(core.attacker, 0, EVENT_BATTLE_START, 0, 0, 0, 0, 0); + if(core.attack_target) { + raise_single_event(core.attack_target, 0, EVENT_BATTLE_START, 0, 0, 0, 0, 1); + } + raise_event(nullptr, EVENT_BATTLE_START, 0, 0, 0, 0, 0); + process_single_event(); + process_instant_event(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(40); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(40); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, TRUE); + return FALSE; + } + case 21: { + if(core.attacker->is_status(STATUS_ATTACK_CANCELED)) { + core.units.begin()->step = 33; + return FALSE; + } + if(!core.attack_target) { + return FALSE; + } + core.attacker->temp.position = core.attacker->current.position; + core.attack_target->temp.position = core.attack_target->current.position; + if(core.attack_target->is_position(POS_FACEDOWN)) { + change_position(core.attack_target, 0, PLAYER_NONE, core.attack_target->current.position >> 1, 0, TRUE); + adjust_all(); + } + return FALSE; + } + case 22: { + raise_single_event(core.attacker, 0, EVENT_BATTLE_CONFIRM, 0, 0, 0, 0, 0); + if(core.attack_target) { + if(core.attack_target->temp.position & POS_FACEDOWN) + core.pre_field[1] = core.attack_target->fieldid_r; + raise_single_event(core.attack_target, 0, EVENT_BATTLE_CONFIRM, 0, 0, 0, 0, 1); + } + raise_event(nullptr, EVENT_BATTLE_CONFIRM, 0, 0, 0, 0, 0); + process_single_event(); + process_instant_event(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(41); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(41); + core.hint_timing[infos.turn_player] = TIMING_DAMAGE_STEP; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + return FALSE; + } + case 23: { + if(core.attacker->is_status(STATUS_ATTACK_CANCELED)) { + core.units.begin()->step = 33; + return FALSE; + } + infos.phase = PHASE_DAMAGE_CAL; + adjust_all(); + return FALSE; + } + case 24: { + // PHASE_DAMAGE_CAL; + calculate_battle_damage(nullptr, nullptr, nullptr); + raise_single_event(core.attacker, 0, EVENT_PRE_DAMAGE_CALCULATE, 0, 0, 0, 0, 0); + if(core.attack_target) + raise_single_event(core.attack_target, 0, EVENT_PRE_DAMAGE_CALCULATE, 0, 0, 0, 0, 1); + raise_event(nullptr, EVENT_PRE_DAMAGE_CALCULATE, 0, 0, 0, 0, 0); + process_single_event(); + process_instant_event(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(42); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(42); + core.hint_timing[infos.turn_player] = TIMING_DAMAGE_CAL; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, TRUE); + return FALSE; + } + case 25: { + if(core.attacker->is_status(STATUS_ATTACK_CANCELED)) { + reset_phase(PHASE_DAMAGE_CAL); + adjust_all(); + infos.phase = PHASE_DAMAGE; + core.units.begin()->step = 33; + return FALSE; + } + return FALSE; + } + case 26: { + // Duel.CalculateDamage() goes here + std::pair atk_def = core.attacker->get_atk_def(); + uint32_t aa = atk_def.first, ad = atk_def.second; + uint32_t da = 0, dd = 0; + uint8_t pa = core.attacker->current.controler, pd; + core.attacker->q_cache.attack = aa; + core.attacker->q_cache.defense = ad; + core.attacker->set_status(STATUS_BATTLE_RESULT, FALSE); + core.attacker->set_status(STATUS_BATTLE_DESTROYED, FALSE); + if(core.attack_target) { + atk_def = core.attack_target->get_atk_def(); + da = atk_def.first; + dd = atk_def.second; + core.attack_target->q_cache.attack = da; + core.attack_target->q_cache.defense = dd; + core.attack_target->set_status(STATUS_BATTLE_RESULT, FALSE); + core.attack_target->set_status(STATUS_BATTLE_DESTROYED, FALSE); + pd = core.attack_target->current.controler; + if(pa != pd) { + core.attacker->set_status(STATUS_OPPO_BATTLE, TRUE); + core.attack_target->set_status(STATUS_OPPO_BATTLE, TRUE); + } + core.attacker_player = pa; + core.attack_target_player = pd; + } + effect* damchange = nullptr; + card* reason_card = nullptr; + uint8_t bd[2]; + calculate_battle_damage(&damchange, &reason_card, bd); + if(bd[0]) { + effect* indestructable_effect = core.attacker->is_affected_by_effect(EFFECT_INDESTRUCTABLE_BATTLE, core.attack_target); + if(indestructable_effect) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(indestructable_effect->owner->data.code); + if(indestructable_effect->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(indestructable_effect->description); + } + bd[0] = FALSE; + } else + core.attacker->set_status(STATUS_BATTLE_RESULT, TRUE); + } + if(bd[1]) { + effect* indestructable_effect = core.attack_target->is_affected_by_effect(EFFECT_INDESTRUCTABLE_BATTLE, core.attacker); + if(indestructable_effect) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_CARD); + pduel->write_buffer8(0); + pduel->write_buffer32(indestructable_effect->owner->data.code); + if(indestructable_effect->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_SOUND); + pduel->write_buffer8(0); + pduel->write_buffer32(indestructable_effect->description); + } + bd[1] = FALSE; + } else + core.attack_target->set_status(STATUS_BATTLE_RESULT, TRUE); + } + pduel->write_buffer8(MSG_BATTLE); + pduel->write_buffer32(core.attacker->get_info_location()); + pduel->write_buffer32(aa); + pduel->write_buffer32(ad); + pduel->write_buffer8(bd[0]); + if(core.attack_target) { + pduel->write_buffer32(core.attack_target->get_info_location()); + pduel->write_buffer32(da); + pduel->write_buffer32(dd); + pduel->write_buffer8(bd[1]); + } else { + pduel->write_buffer32(0); + pduel->write_buffer32(0); + pduel->write_buffer32(0); + pduel->write_buffer8(0); + } + core.units.begin()->peffect = damchange; + if(reason_card) + core.temp_var[0] = reason_card->current.controler; + if(!reason_card) + core.temp_var[1] = 0; + else if(reason_card == core.attacker) + core.temp_var[1] = 1; + else core.temp_var[1] = 2; + if(!damchange) { + if(core.battle_damage[infos.turn_player]) { + raise_single_event(core.attacker, 0, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, infos.turn_player, core.battle_damage[infos.turn_player]); + if(core.attack_target) + raise_single_event(core.attack_target, 0, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, infos.turn_player, core.battle_damage[infos.turn_player]); + raise_event(reason_card, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, infos.turn_player, core.battle_damage[infos.turn_player]); + } + if(core.battle_damage[1 - infos.turn_player]) { + raise_single_event(core.attacker, 0, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, 1 - infos.turn_player, core.battle_damage[1 - infos.turn_player]); + if(core.attack_target) + raise_single_event(core.attack_target, 0, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, 1 - infos.turn_player, core.battle_damage[1 - infos.turn_player]); + raise_event(reason_card, EVENT_PRE_BATTLE_DAMAGE, 0, 0, reason_card->current.controler, 1 - infos.turn_player, core.battle_damage[1 - infos.turn_player]); + } + } + process_single_event(); + process_instant_event(); + //this timing does not exist in Master Rule 3 + core.damage_calculated = TRUE; + return FALSE; + } + case 27: { + infos.phase = PHASE_DAMAGE; + core.hint_timing[infos.turn_player] = 0; + core.chain_attack = FALSE; + core.attacker->battled_cards.addcard(core.attack_target); + if(core.attack_target) + core.attack_target->battled_cards.addcard(core.attacker); + uint8_t reason_player = (uint8_t)core.temp_var[0]; + card* reason_card = nullptr; + if(core.temp_var[1] == 1) + reason_card = core.attacker; + else if(core.temp_var[1] == 2) + reason_card = core.attack_target; + effect* damchange = core.units.begin()->peffect; + if(!damchange) { + if(core.battle_damage[0]) + damage(0, REASON_BATTLE, reason_player, reason_card, 0, core.battle_damage[0]); + if(core.battle_damage[1]) + damage(0, REASON_BATTLE, reason_player, reason_card, 1, core.battle_damage[1]); + } else { + if(core.battle_damage[0]) + damage(damchange, REASON_EFFECT, reason_player, reason_card, 0, core.battle_damage[0]); + if(core.battle_damage[1]) + damage(damchange, REASON_EFFECT, reason_player, reason_card, 1, core.battle_damage[1]); + } + reset_phase(PHASE_DAMAGE_CAL); + adjust_all(); + return FALSE; + } + case 28: { + card_set des; + effect* peffect; + if(core.attacker->is_status(STATUS_BATTLE_RESULT) + && core.attacker->current.location == LOCATION_MZONE && core.attacker->fieldid_r == core.pre_field[0]) { + des.insert(core.attacker); + core.attacker->temp.reason = core.attacker->current.reason; + core.attacker->temp.reason_card = core.attacker->current.reason_card; + core.attacker->temp.reason_effect = core.attacker->current.reason_effect; + core.attacker->temp.reason_player = core.attacker->current.reason_player; + core.attacker->current.reason_effect = 0; + core.attacker->current.reason = REASON_BATTLE; + core.attacker->current.reason_card = core.attack_target; + core.attacker->current.reason_player = core.attack_target->current.controler; + uint32_t dest = LOCATION_GRAVE; + uint32_t seq = 0; + if((peffect = core.attack_target->is_affected_by_effect(EFFECT_BATTLE_DESTROY_REDIRECT)) && (core.attacker->data.type & TYPE_MONSTER)) { + dest = peffect->get_value(core.attacker); + seq = dest >> 16; + dest &= 0xffff; + } + core.attacker->sendto_param.set(core.attacker->owner, POS_FACEUP, dest, seq); + core.attacker->set_status(STATUS_DESTROY_CONFIRMED, TRUE); + } + if(core.attack_target && core.attack_target->is_status(STATUS_BATTLE_RESULT) + && core.attack_target->current.location == LOCATION_MZONE && core.attack_target->fieldid_r == core.pre_field[1]) { + des.insert(core.attack_target); + core.attack_target->temp.reason = core.attack_target->current.reason; + core.attack_target->temp.reason_card = core.attack_target->current.reason_card; + core.attack_target->temp.reason_effect = core.attack_target->current.reason_effect; + core.attack_target->temp.reason_player = core.attack_target->current.reason_player; + core.attack_target->current.reason_effect = 0; + core.attack_target->current.reason = REASON_BATTLE; + core.attack_target->current.reason_card = core.attacker; + core.attack_target->current.reason_player = core.attacker->current.controler; + uint32_t dest = LOCATION_GRAVE; + uint32_t seq = 0; + if((peffect = core.attacker->is_affected_by_effect(EFFECT_BATTLE_DESTROY_REDIRECT)) && (core.attack_target->data.type & TYPE_MONSTER)) { + dest = peffect->get_value(core.attack_target); + seq = dest >> 16; + dest &= 0xffff; + } + core.attack_target->sendto_param.set(core.attack_target->owner, POS_FACEUP, dest, seq); + core.attack_target->set_status(STATUS_DESTROY_CONFIRMED, TRUE); + } + core.attacker->set_status(STATUS_BATTLE_RESULT, FALSE); + if(core.attack_target) + core.attack_target->set_status(STATUS_BATTLE_RESULT, FALSE); + core.battle_destroy_rep.clear(); + core.desrep_chain.clear(); + if(des.size()) { + group* ng = pduel->new_group(); + ng->container.swap(des); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_DESTROY, 10, 0, ng, REASON_BATTLE, PLAYER_NONE); + core.units.begin()->ptarget = ng; + } + return FALSE; + } + case 29: { + if(core.battle_destroy_rep.size()) + destroy(core.battle_destroy_rep, 0, REASON_EFFECT | REASON_REPLACE, PLAYER_NONE); + if(core.desrep_chain.size()) + add_process(PROCESSOR_OPERATION_REPLACE, 15, nullptr, nullptr, 0, 0); + adjust_all(); + return FALSE; + } + case 30: { + group* des = core.units.begin()->ptarget; + if(des && des->container.size()) { + for(auto& pcard : des->container) { + pcard->set_status(STATUS_BATTLE_DESTROYED, TRUE); + pcard->set_status(STATUS_DESTROY_CONFIRMED, FALSE); + pcard->filter_disable_related_cards(); + } + } + core.selfdes_disabled = FALSE; + adjust_all(); + //EVENT_BATTLE_END was here, but this timing does not exist in Master Rule 3 + return FALSE; + } + case 31: { + core.flip_delayed = FALSE; + core.new_fchain.splice(core.new_fchain.begin(), core.new_fchain_b); + core.new_ochain.splice(core.new_ochain.begin(), core.new_ochain_b); + raise_single_event(core.attacker, 0, EVENT_BATTLED, 0, 0, PLAYER_NONE, 0, 0); + if(core.attack_target) + raise_single_event(core.attack_target, 0, EVENT_BATTLED, 0, 0, PLAYER_NONE, 0, 1); + raise_event(nullptr, EVENT_BATTLED, 0, 0, PLAYER_NONE, 0, 0); + process_single_event(); + process_instant_event(); + if(core.effect_damage_step) { + core.damage_step_reserved.ptr1 = core.units.begin()->ptarget; + return TRUE; + } + core.units.begin()->step = 32; + } + // fall through + case 32: { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(43); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(43); + core.hint_timing[0] |= TIMING_BATTLED; + core.hint_timing[1] |= TIMING_BATTLED; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, TRUE); + return FALSE; + } + case 33: { + group* des = core.units.begin()->ptarget; + if(des) { + for(auto cit = des->container.begin(); cit != des->container.end();) { + if ((*cit)->current.location != LOCATION_MZONE || ((*cit)->fieldid_r != core.pre_field[0] && (*cit)->fieldid_r != core.pre_field[1])) + cit = des->container.erase(cit); + else + ++cit; + } + add_process(PROCESSOR_DESTROY, 3, 0, des, REASON_BATTLE, PLAYER_NONE); + } + adjust_all(); + return FALSE; + } + case 34: { + core.units.begin()->ptarget = 0; + core.damage_calculated = TRUE; + core.selfdes_disabled = FALSE; + core.flip_delayed = FALSE; + core.new_fchain.splice(core.new_fchain.begin(), core.new_fchain_b); + core.new_ochain.splice(core.new_ochain.begin(), core.new_ochain_b); + card_set ing; + card_set ed; + // [YGOL] core.attacker null guards: a damage-step unit resumed after the + // battle it belonged to has ended must not crash (see process_damage_step step 1) + if(core.attacker && core.attacker->is_status(STATUS_BATTLE_DESTROYED) && (core.attacker->current.reason & REASON_BATTLE)) { + raise_single_event(core.attack_target, 0, EVENT_BATTLE_DESTROYING, 0, core.attacker->current.reason, core.attack_target_player, 0, 1); + raise_single_event(core.attacker, 0, EVENT_BATTLE_DESTROYED, 0, core.attacker->current.reason, core.attack_target_player, 0, 0); + raise_single_event(core.attacker, 0, EVENT_DESTROYED, 0, core.attacker->current.reason, core.attack_target_player, 0, 0); + ing.insert(core.attack_target); + ed.insert(core.attacker); + } + if(core.attacker && core.attack_target && core.attack_target->is_status(STATUS_BATTLE_DESTROYED) && (core.attack_target->current.reason & REASON_BATTLE)) { + raise_single_event(core.attacker, 0, EVENT_BATTLE_DESTROYING, 0, core.attack_target->current.reason, core.attacker_player, 0, 0); + raise_single_event(core.attack_target, 0, EVENT_BATTLE_DESTROYED, 0, core.attack_target->current.reason, core.attacker_player, 0, 1); + raise_single_event(core.attack_target, 0, EVENT_DESTROYED, 0, core.attack_target->current.reason, core.attacker_player, 0, 1); + ing.insert(core.attacker); + ed.insert(core.attack_target); + } + if(ing.size()) + raise_event(ing, EVENT_BATTLE_DESTROYING, 0, 0, 0, 0, 0); + if(ed.size()) { + raise_event(ed, EVENT_BATTLE_DESTROYED, 0, 0, 0, 0, 0); + raise_event(ed, EVENT_DESTROYED, 0, 0, 0, 0, 0); + } + if(core.attacker) + raise_single_event(core.attacker, 0, EVENT_DAMAGE_STEP_END, 0, 0, 0, 0, 0); + if(core.attack_target) + raise_single_event(core.attack_target, 0, EVENT_DAMAGE_STEP_END, 0, 0, 0, 0, 1); + raise_event(nullptr, EVENT_DAMAGE_STEP_END, 0, 0, 0, 0, 0); + process_single_event(); + process_instant_event(); + if(core.attacker) + core.attacker->set_status(STATUS_BATTLE_DESTROYED, FALSE); + if(core.attack_target) + core.attack_target->set_status(STATUS_BATTLE_DESTROYED, FALSE); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(0); + pduel->write_buffer32(44); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(1); + pduel->write_buffer32(44); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, TRUE); + core.units.begin()->step = 38; + return FALSE; + } + case 38: { + return FALSE; + } + case 39: { + //end of damage step + if(core.attacker) { // [YGOL] null guard, see step 34 + if(core.attacker->is_status(STATUS_ATTACK_CANCELED)) + core.attacker->reset(EFFECT_ATTACK_DISABLED, RESET_CODE); + core.attacker->set_status(STATUS_OPPO_BATTLE, FALSE); + } + if(core.attack_target) + core.attack_target->set_status(STATUS_OPPO_BATTLE, FALSE); + core.attacker_player = PLAYER_NONE; + core.attack_target_player = PLAYER_NONE; + core.units.begin()->step = -1; + infos.phase = PHASE_BATTLE_STEP; + pduel->write_buffer8(MSG_DAMAGE_STEP_END); + reset_phase(PHASE_DAMAGE); + adjust_all(); + if(core.effect_damage_step) + return TRUE; + return FALSE; + } + case 40: { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + if(core.current_chain.size()) { + for(auto& ch : core.current_chain) + ch.triggering_effect->get_handler()->set_status(STATUS_CHAINING, FALSE); + add_process(PROCESSOR_SOLVE_CHAIN, 0, 0, 0, FALSE, 0); + core.units.begin()->step = -1; + return FALSE; + } + reset_phase(PHASE_BATTLE_STEP); + adjust_all(); + return FALSE; + } + case 41: { + // normal end of battle step + if(is_player_affected_by_effect(infos.turn_player, EFFECT_BP_TWICE)) + core.units.begin()->arg2 = 1; + else + core.units.begin()->arg2 = 0; + infos.phase = PHASE_BATTLE; + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_BATTLE, 0); + adjust_all(); + return FALSE; + } + case 42: { + core.attacker = 0; + core.attack_target = 0; + returns.ivalue[0] = (int32_t)core.units.begin()->arg1; + returns.ivalue[1] = (int32_t)core.units.begin()->arg2; + return TRUE; + } + } + return TRUE; +} +int32_t field::process_damage_step(uint16_t step, uint32_t new_attack) { + switch(step) { + case 0: { + if(core.effect_damage_step && !new_attack) + return TRUE; + core.effect_damage_step = 1; + card* tmp = core.attacker; + core.attacker = (card*)core.units.begin()->peffect; + core.units.begin()->peffect = (effect*)tmp; + tmp = core.attack_target; + core.attack_target = (card*)core.units.begin()->ptarget; + core.units.begin()->ptarget = (group*)tmp; + core.units.begin()->arg1 = infos.phase; + if(core.attacker->current.location != LOCATION_MZONE || core.attack_target && core.attack_target->current.location != LOCATION_MZONE) { + core.units.begin()->step = 2; + return FALSE; + } + if(new_attack) { + ++core.attack_state_count[infos.turn_player]; + ++core.battled_count[infos.turn_player]; + check_card_counter(core.attacker, ACTIVITY_ATTACK, infos.turn_player); + } + core.attacker->announced_cards.addcard(core.attack_target); + attack_all_target_check(); + pduel->write_buffer8(MSG_ATTACK); + pduel->write_buffer32(core.attacker->get_info_location()); + if(core.attack_target) + pduel->write_buffer32(core.attack_target->get_info_location()); + else + pduel->write_buffer32(0); + infos.phase = PHASE_DAMAGE; + pduel->write_buffer8(MSG_DAMAGE_STEP_START); + core.pre_field[0] = core.attacker->fieldid_r; + ++core.attacker->attacked_count; + if(core.attack_target) { + core.pre_field[1] = core.attack_target->fieldid_r; + if(core.attack_target->is_position(POS_FACEDOWN)) { + change_position(core.attack_target, 0, PLAYER_NONE, core.attack_target->current.position >> 1, 0, TRUE); + adjust_all(); + } + } else + core.pre_field[1] = 0; + return FALSE; + } + case 1: { + infos.phase = PHASE_DAMAGE_CAL; + add_process(PROCESSOR_BATTLE_COMMAND, 26, 0, 0, 0, 0); + core.units.begin()->step = 2; + // [YGOL] Duel.CalculateDamage outside a chain (e.g. a CONTINUOUS effect at + // EVENT_ATTACK_ANNOUNCE, anime "Training Field"): nothing will ever re-queue + // damage_step_reserved, because that only happens at chain end (solve_chain + // step 12). The parked unit then woke up turns later with core.attacker == 0 + // and crashed in process_battle_command step 34. Stay on the unit list and + // continue at step 2 right after the damage calculation instead. + if(core.current_chain.empty()) + return FALSE; + core.damage_step_reserved = core.units.front(); + return TRUE; + } + case 2: { + core.effect_damage_step = 2; + add_process(PROCESSOR_BATTLE_COMMAND, 32, 0, (group*)core.units.begin()->ptr1, 0, 0); + return FALSE; + } + case 3: { + core.attacker = (card*)core.units.begin()->peffect; + core.attack_target = (card*)core.units.begin()->ptarget; + if(core.attacker) { + core.attacker->set_status(STATUS_ATTACK_CANCELED, TRUE); + ++core.attacker->announce_count; + core.attacker->set_status(STATUS_CANNOT_CHANGE_FORM, TRUE); + core.attacker->announced_cards.addcard(core.attack_target); + attack_all_target_check(); + } + if(core.attack_target) + core.attack_target->set_status(STATUS_ATTACK_CANCELED, TRUE); + core.effect_damage_step = 0; + infos.phase = (uint16_t)core.units.begin()->arg1; + return TRUE; + } + } + return TRUE; +} +void field::calculate_battle_damage(effect** pdamchange, card** preason_card, uint8_t* battle_destroyed) { + int32_t aa = core.attacker->get_battle_attack(), ad = core.attacker->get_battle_defense(); + int32_t da = 0, dd = 0, attacker_value = aa, defender_value = 0; + uint8_t pa = core.attacker->current.controler, pd = PLAYER_NONE; + uint8_t damaged_player = 0; + effect* damchange = nullptr; + card* reason_card = nullptr; + uint8_t bd[2] = {FALSE, FALSE}; + bool pierce = false; + core.battle_damage[0] = core.battle_damage[1] = 0; + if(core.attacker->is_position(POS_FACEUP_DEFENSE)) { + effect* defattack = core.attacker->is_affected_by_effect(EFFECT_DEFENSE_ATTACK); + if(defattack && defattack->get_value(core.attacker)) + attacker_value = ad; + } + if(core.attack_target) { + da = core.attack_target->get_battle_attack(); + dd = core.attack_target->get_battle_defense(); + pd = core.attack_target->current.controler; + if(core.attack_target->is_position(POS_ATTACK)) { + defender_value = da; + if(attacker_value > defender_value) { + damaged_player = pd; + core.battle_damage[damaged_player] = attacker_value - defender_value; + reason_card = core.attacker; + bd[1] = TRUE; + } else if(attacker_value < defender_value) { + damaged_player = pa; + core.battle_damage[damaged_player] = defender_value - attacker_value; + reason_card = core.attack_target; + bd[0] = TRUE; + } else { + if(attacker_value != 0) { + bd[0] = TRUE; + bd[1] = TRUE; + } + } + } else { + defender_value = dd; + if(attacker_value > defender_value) { + effect_set eset; + core.attacker->filter_effect(EFFECT_PIERCE, &eset); + if(eset.size()) { + pierce = true; + uint8_t dp[2] = {}; + for(effect_set::size_type i = 0; i < eset.size(); ++i) + dp[1 - eset[i]->get_handler_player()] = 1; + if(dp[0]) + core.battle_damage[0] = attacker_value - defender_value; + if(dp[1]) + core.battle_damage[1] = attacker_value - defender_value; + bool double_damage = false; + //bool half_damage = false; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + if(eset[i]->get_value() == DOUBLE_DAMAGE) + double_damage = true; + //if(eset[i]->get_value() == HALF_DAMAGE) + // half_damage = true; + } + //if(double_damage && half_damage) { + // double_damage = false; + // half_damage = false; + //} + if(double_damage) { + if(dp[0]) + core.battle_damage[0] *= 2; + if(dp[1]) + core.battle_damage[1] *= 2; + } + //if(half_damage) { + // if(dp[0]) + // core.battle_damage[0] /= 2; + // if(dp[1]) + // core.battle_damage[1] /= 2; + //} + bool both = dp[0] && dp[1]; + if(!both) { + damaged_player = dp[0] ? 0 : 1; + if(core.attacker->is_affected_by_effect(EFFECT_BOTH_BATTLE_DAMAGE) + || core.attack_target->is_affected_by_effect(EFFECT_BOTH_BATTLE_DAMAGE)) { + core.battle_damage[1 - damaged_player] = core.battle_damage[damaged_player]; + both = true; + } + } + effect* reflect[2] = {}; + if(!(reflect[pd] = core.attack_target->is_affected_by_effect(EFFECT_REFLECT_BATTLE_DAMAGE, core.attacker))) + reflect[pd] = is_player_affected_by_effect(pd, EFFECT_REFLECT_BATTLE_DAMAGE); + if(!(reflect[1 - pd] = core.attacker->is_affected_by_effect(EFFECT_REFLECT_BATTLE_DAMAGE, core.attack_target))) + reflect[1 - pd] = is_player_affected_by_effect(1 - pd, EFFECT_REFLECT_BATTLE_DAMAGE); + bool also[2] = { false, false }; + if(!both + && (core.attack_target->is_affected_by_effect(EFFECT_ALSO_BATTLE_DAMAGE) + || is_player_affected_by_effect(pd, EFFECT_ALSO_BATTLE_DAMAGE))) + also[pd] = true; + if(!both + && (core.attacker->is_affected_by_effect(EFFECT_ALSO_BATTLE_DAMAGE) + || is_player_affected_by_effect(1 - pd, EFFECT_ALSO_BATTLE_DAMAGE))) + also[1 - pd] = true; + if(both) { + //turn player's effect applies first + if(reflect[pa] && reflect[pa]->get_handler_player() == pa) { + core.battle_damage[1 - pa] += core.battle_damage[pa]; + core.battle_damage[pa] = 0; + } else if(reflect[1 - pa] && reflect[1 - pa]->get_handler_player() == pa) { + core.battle_damage[pa] += core.battle_damage[1 - pa]; + core.battle_damage[1 - pa] = 0; + } else if(reflect[pa] && reflect[pa]->get_handler_player() == 1 - pa) { + core.battle_damage[1 - pa] += core.battle_damage[pa]; + core.battle_damage[pa] = 0; + } else if(reflect[1 - pa] && reflect[1 - pa]->get_handler_player() == 1 - pa) { + core.battle_damage[pa] += core.battle_damage[1 - pa]; + core.battle_damage[1 - pa] = 0; + } + } else { + if(reflect[damaged_player]) { + if(!also[1 - damaged_player]) { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] = 0; + } else { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] = core.battle_damage[1 - damaged_player]; + } + } else if(also[damaged_player]) { + if(!reflect[1 - damaged_player]) { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + } else { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] += core.battle_damage[1 - damaged_player]; + core.battle_damage[1 - damaged_player] = 0; + } + } + } + effect_set change_effects; + core.attacker->filter_effect(EFFECT_CHANGE_BATTLE_DAMAGE, &change_effects, FALSE); + core.attacker->filter_effect(EFFECT_CHANGE_INVOLVING_BATTLE_DAMAGE, &change_effects, FALSE); + core.attack_target->filter_effect(EFFECT_CHANGE_INVOLVING_BATTLE_DAMAGE, &change_effects, FALSE); + filter_player_effect(pa, EFFECT_CHANGE_BATTLE_DAMAGE, &change_effects, FALSE); + filter_player_effect(1 - pa, EFFECT_CHANGE_BATTLE_DAMAGE, &change_effects, FALSE); + std::sort(change_effects.begin(), change_effects.end(), effect_sort_id); + for(uint8_t p = 0; p < 2; ++p) { + bool double_dam = false; + bool half_dam = false; + int32_t dam_value = -1; + for(effect_set::size_type i = 0; i < change_effects.size(); ++i) { + int32_t val = -1; + if(!change_effects[i]->is_flag(EFFECT_FLAG_PLAYER_TARGET)) { + pduel->lua->add_param(p, PARAM_TYPE_INT); + val = change_effects[i]->get_value(1); + } else if(change_effects[i]->is_target_player(p)) + val = change_effects[i]->get_value(); + if(val == 0) { + dam_value = 0; + break; + } else if(val > 0) + dam_value = val; + else if(val == DOUBLE_DAMAGE) + double_dam = true; + else if(val == HALF_DAMAGE) + half_dam = true; + } + if(double_dam && half_dam) { + double_dam = false; + half_dam = false; + } + if(double_dam) + core.battle_damage[p] *= 2; + if(half_dam) + core.battle_damage[p] /= 2; + if(dam_value >= 0 && core.battle_damage[p] > 0) + core.battle_damage[p] = dam_value; + } + if(core.attacker->is_affected_by_effect(EFFECT_NO_BATTLE_DAMAGE) + || core.attack_target->is_affected_by_effect(EFFECT_AVOID_BATTLE_DAMAGE, core.attacker) + || is_player_affected_by_effect(pd, EFFECT_AVOID_BATTLE_DAMAGE)) + core.battle_damage[pd] = 0; + if(core.attack_target->is_affected_by_effect(EFFECT_NO_BATTLE_DAMAGE) + || core.attacker->is_affected_by_effect(EFFECT_AVOID_BATTLE_DAMAGE, core.attack_target) + || is_player_affected_by_effect(1 - pd, EFFECT_AVOID_BATTLE_DAMAGE)) + core.battle_damage[1 - pd] = 0; + reason_card = core.attacker; + } + bd[1] = TRUE; + } else if(attacker_value < defender_value) { + damaged_player = pa; + core.battle_damage[damaged_player] = defender_value - attacker_value; + reason_card = core.attack_target; + } + } + } else { + if(attacker_value != 0) { + damaged_player = 1 - pa; + core.battle_damage[damaged_player] = attacker_value; + reason_card = core.attacker; + } + } + if(reason_card && !pierce + && !(damchange = reason_card->is_affected_by_effect(EFFECT_BATTLE_DAMAGE_TO_EFFECT))) { + card* dam_card = (reason_card == core.attacker) ? core.attack_target : core.attacker; + bool both = false; + if(reason_card->is_affected_by_effect(EFFECT_BOTH_BATTLE_DAMAGE) + || dam_card && dam_card->is_affected_by_effect(EFFECT_BOTH_BATTLE_DAMAGE)) { + core.battle_damage[1 - damaged_player] = core.battle_damage[damaged_player]; + both = true; + } + effect* reflect[2] = {}; + if(!dam_card || !(reflect[damaged_player] = dam_card->is_affected_by_effect(EFFECT_REFLECT_BATTLE_DAMAGE, reason_card))) + reflect[damaged_player] = is_player_affected_by_effect(damaged_player, EFFECT_REFLECT_BATTLE_DAMAGE); + if(!(reflect[1 - damaged_player] = reason_card->is_affected_by_effect(EFFECT_REFLECT_BATTLE_DAMAGE, dam_card))) + reflect[1 - damaged_player] = is_player_affected_by_effect(1 - damaged_player, EFFECT_REFLECT_BATTLE_DAMAGE); + bool also[2] = { false, false }; + if(!both + && (dam_card && dam_card->is_affected_by_effect(EFFECT_ALSO_BATTLE_DAMAGE) + || is_player_affected_by_effect(damaged_player, EFFECT_ALSO_BATTLE_DAMAGE))) + also[damaged_player] = true; + if(!both + && (reason_card->is_affected_by_effect(EFFECT_ALSO_BATTLE_DAMAGE) + || is_player_affected_by_effect(1 - damaged_player, EFFECT_ALSO_BATTLE_DAMAGE))) + also[1 - damaged_player] = true; + if(both) { + //turn player's effect applies first + if(reflect[pa] && reflect[pa]->get_handler_player() == pa) { + core.battle_damage[1 - pa] += core.battle_damage[pa]; + core.battle_damage[pa] = 0; + } else if(reflect[1 - pa] && reflect[1 - pa]->get_handler_player() == pa) { + core.battle_damage[pa] += core.battle_damage[1 - pa]; + core.battle_damage[1 - pa] = 0; + } else if(reflect[pa] && reflect[pa]->get_handler_player() == 1 - pa) { + core.battle_damage[1 - pa] += core.battle_damage[pa]; + core.battle_damage[pa] = 0; + } else if(reflect[1 - pa] && reflect[1 - pa]->get_handler_player() == 1 - pa) { + core.battle_damage[pa] += core.battle_damage[1 - pa]; + core.battle_damage[1 - pa] = 0; + } + } else { + if(reflect[damaged_player]) { + if(!also[1 - damaged_player]) { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] = 0; + } else { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] = core.battle_damage[1 - damaged_player]; + } + } else if(also[damaged_player]) { + if(!reflect[1 - damaged_player]) { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + } else { + core.battle_damage[1 - damaged_player] += core.battle_damage[damaged_player]; + core.battle_damage[damaged_player] += core.battle_damage[1 - damaged_player]; + core.battle_damage[1 - damaged_player] = 0; + } + } + } + effect_set eset; + reason_card->filter_effect(EFFECT_CHANGE_BATTLE_DAMAGE, &eset, FALSE); + reason_card->filter_effect(EFFECT_CHANGE_INVOLVING_BATTLE_DAMAGE, &eset, FALSE); + if(dam_card) + dam_card->filter_effect(EFFECT_CHANGE_INVOLVING_BATTLE_DAMAGE, &eset, FALSE); + filter_player_effect(damaged_player, EFFECT_CHANGE_BATTLE_DAMAGE, &eset, FALSE); + filter_player_effect(1 - damaged_player, EFFECT_CHANGE_BATTLE_DAMAGE, &eset, FALSE); + std::sort(eset.begin(), eset.end(), effect_sort_id); + for(uint8_t p = 0; p < 2; ++p) { + bool double_dam = false; + bool half_dam = false; + int32_t dam_value = -1; + int32_t current_min = INT32_MAX; + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + int32_t val = -1; + if(!eset[i]->is_flag(EFFECT_FLAG_PLAYER_TARGET)) { + pduel->lua->add_param(p, PARAM_TYPE_INT); + val = eset[i]->get_value(1); + } + else if (eset[i]->is_target_player(p)) + val = eset[i]->get_value(); + if (val == 0) { + dam_value = 0; + break; + } + else if (val > 0 && val < current_min) { + current_min = val; + dam_value = val; + } + else if (val == DOUBLE_DAMAGE) + double_dam = true; + else if (val == HALF_DAMAGE) + half_dam = true; + } + if(double_dam && half_dam) { + double_dam = false; + half_dam = false; + } + if(double_dam) + core.battle_damage[p] *= 2; + if(half_dam) + core.battle_damage[p] /= 2; + if(dam_value >= 0 && core.battle_damage[p] > 0) + core.battle_damage[p] = dam_value; + } + if(reason_card->is_affected_by_effect(EFFECT_NO_BATTLE_DAMAGE) + || dam_card && dam_card->is_affected_by_effect(EFFECT_AVOID_BATTLE_DAMAGE, reason_card) + || is_player_affected_by_effect(damaged_player, EFFECT_AVOID_BATTLE_DAMAGE)) + core.battle_damage[damaged_player] = 0; + if(dam_card && dam_card->is_affected_by_effect(EFFECT_NO_BATTLE_DAMAGE) + || reason_card->is_affected_by_effect(EFFECT_AVOID_BATTLE_DAMAGE, dam_card) + || is_player_affected_by_effect(1 - damaged_player, EFFECT_AVOID_BATTLE_DAMAGE)) + core.battle_damage[1 - damaged_player] = 0; + } + if(!core.battle_damage[damaged_player] && !core.battle_damage[1 - damaged_player]) + reason_card = nullptr; + if(pdamchange) + *pdamchange = damchange; + if(preason_card) + *preason_card = reason_card; + if(battle_destroyed) { + battle_destroyed[0] = bd[0]; + battle_destroyed[1] = bd[1]; + } +} +// [YGOL] patch 0022 (attack preview): a read-only dry run of the damage step, asked by the client while +// it drags an attacker onto a target. The frame is what steps 19-26 of process_battle_command set before +// they read the battle (PHASE_DAMAGE_CAL, core.attacker, core.attack_target, the two battle players, +// damage_calculated FALSE), so a conditional effect such as Skyscraper reads the same answer here as in +// the real step. The battle values, the damage and the destroy rules are read through the same code the +// real step runs (calculate_battle_damage, EFFECT_INDESTRUCTABLE_BATTLE both ways); the values +// calculate_battle_damage keeps in locals are read the same way it reads them. Every field touched is +// put back; nothing is destroyed, damaged, raised or written to the message buffer. Only for a face-up +// attacker and a face-up (or no) target: the caller answers unknown for a face-down target. +void field::preview_battle(card* attacker, card* target, battle_preview* out) { + card* saved_attacker = core.attacker; + card* saved_target = core.attack_target; + uint8_t saved_attacker_player = core.attacker_player; + uint8_t saved_target_player = core.attack_target_player; + uint8_t saved_calculated = core.damage_calculated; + int32_t saved_damage[2] = { core.battle_damage[0], core.battle_damage[1] }; + uint16_t saved_phase = infos.phase; + core.attacker = attacker; + core.attack_target = target; + if(target) { + core.attacker_player = attacker->current.controler; + core.attack_target_player = target->current.controler; + } + core.damage_calculated = FALSE; + infos.phase = PHASE_DAMAGE_CAL; + int32_t attacker_value = attacker->get_battle_attack(); + if(attacker->is_position(POS_FACEUP_DEFENSE)) { + effect* defattack = attacker->is_affected_by_effect(EFFECT_DEFENSE_ATTACK); + if(defattack && defattack->get_value(attacker)) + attacker_value = attacker->get_battle_defense(); + } + int32_t target_value = 0; + uint8_t pierce = FALSE; + if(target) { + if(target->is_position(POS_ATTACK)) + target_value = target->get_battle_attack(); + else { + target_value = target->get_battle_defense(); + if(attacker_value > target_value) { + effect_set eset; + attacker->filter_effect(EFFECT_PIERCE, &eset); + if(eset.size()) + pierce = TRUE; + } + } + } + uint8_t bd[2] = { FALSE, FALSE }; + calculate_battle_damage(nullptr, nullptr, bd); + if(bd[0] && attacker->is_affected_by_effect(EFFECT_INDESTRUCTABLE_BATTLE, target)) + bd[0] = FALSE; + if(bd[1] && target && target->is_affected_by_effect(EFFECT_INDESTRUCTABLE_BATTLE, attacker)) + bd[1] = FALSE; + out->attacker_destroyed = bd[0]; + out->target_destroyed = bd[1]; + out->pierce = pierce; + out->attacker_value = attacker_value; + out->target_value = target_value; + out->damage[0] = core.battle_damage[0]; + out->damage[1] = core.battle_damage[1]; + core.battle_damage[0] = saved_damage[0]; + core.battle_damage[1] = saved_damage[1]; + infos.phase = saved_phase; + core.damage_calculated = saved_calculated; + core.attack_target_player = saved_target_player; + core.attacker_player = saved_attacker_player; + core.attack_target = saved_target; + core.attacker = saved_attacker; +} +int32_t field::process_turn(uint16_t step, uint8_t turn_player) { + switch(step) { + case 0: { + //Pre Draw + for(const auto& ev : core.used_event) { + if(ev.event_cards) + pduel->delete_group(ev.event_cards); + } + core.used_event.clear(); + for(auto& peffect : core.reseted_effects) { + pduel->delete_effect(peffect); + } + core.reseted_effects.clear(); + core.effect_count_code[0].clear(); + core.effect_count_code[1].clear(); + core.effect_count_code[PLAYER_NONE].clear(); + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(!pcard) + continue; + pcard->set_status(STATUS_SUMMON_TURN, FALSE); + pcard->set_status(STATUS_FLIP_SUMMON_TURN, FALSE); + pcard->set_status(STATUS_SPSUMMON_TURN, FALSE); + pcard->set_status(STATUS_SET_TURN, FALSE); + pcard->set_status(STATUS_CANNOT_CHANGE_FORM, FALSE); + pcard->indestructable_effects.clear(); + pcard->attack_announce_count = 0; + pcard->announce_count = 0; + pcard->attacked_count = 0; + pcard->removed_overlay_count = 0; + pcard->announced_cards.clear(); + pcard->attacked_cards.clear(); + pcard->battled_cards.clear(); + pcard->attack_all_target = TRUE; + } + for(auto& pcard : player[p].list_szone) { + if(!pcard) + continue; + pcard->set_status(STATUS_SET_TURN, FALSE); + pcard->indestructable_effects.clear(); + } + core.summon_state_count[p] = 0; + core.normalsummon_state_count[p] = 0; + core.flipsummon_state_count[p] = 0; + core.spsummon_state_count[p] = 0; + core.attack_state_count[p] = 0; + core.battle_phase_count[p] = 0; + core.battled_count[p] = 0; + core.summon_count[p] = 0; + core.extra_summon[p] = 0; + core.spsummon_once_map[p].clear(); + } + for(auto& peffect : effects.rechargeable) + if(!peffect->is_flag(EFFECT_FLAG_NO_TURN_RESET)) + peffect->recharge(); + for(auto& iter : core.summon_counter) + iter.second.second = 0; + for(auto& iter : core.normalsummon_counter) + iter.second.second = 0; + for(auto& iter : core.spsummon_counter) + iter.second.second = 0; + for(auto& iter : core.flipsummon_counter) + iter.second.second = 0; + for(auto& iter : core.attack_counter) + iter.second.second = 0; + for(auto& iter : core.chain_counter) + iter.second.second = 0; + if(core.global_flag & GLOBALFLAG_SPSUMMON_COUNT) { + for(auto& peffect : effects.spsummon_count_eff) { + card* pcard = peffect->get_handler(); + if(!peffect->is_flag(EFFECT_FLAG_NO_TURN_RESET)) { + pcard->spsummon_counter[0] = pcard->spsummon_counter[1] = 0; + } + } + } + ++infos.turn_id; + ++infos.turn_id_by_player[turn_player]; + infos.turn_player = turn_player; + pduel->write_buffer8(MSG_NEW_TURN); + pduel->write_buffer8(turn_player); + // [YGOL] first seat holds the hand: no swap on a team's first turn, so the duelist loaded as the + // main deck plays first and the partner swaps in from each team's second turn on. + if((core.duel_options & DUEL_TAG_MODE) && infos.turn_id > 2) + tag_swap(turn_player); + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_TURN)) { + core.units.begin()->step = 17; + reset_phase(PHASE_DRAW); + reset_phase(PHASE_STANDBY); + reset_phase(PHASE_END); + adjust_all(); + return FALSE; + } + infos.phase = PHASE_DRAW; + core.phase_action = FALSE; + core.hand_adjusted = FALSE; + raise_event(nullptr, EVENT_PHASE_START + PHASE_DRAW, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 1: { + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_DP)) { + core.units.begin()->step = 2; + reset_phase(PHASE_DRAW); + adjust_all(); + return FALSE; + } + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + raise_event(nullptr, EVENT_PREDRAW, 0, 0, 0, turn_player, 0); + process_instant_event(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_EVENT); + pduel->write_buffer8(turn_player); + pduel->write_buffer32(27); + if(core.new_fchain.size() || core.new_ochain.size()) + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0x100, TRUE); + return FALSE; + } + case 2: { + // Draw, new ruling + if((core.duel_rule <= 2) || (infos.turn_id > 1)) { + int32_t count = get_draw_count(infos.turn_player); + if(count > 0) { + draw(nullptr, REASON_RULE, turn_player, turn_player, count); + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + } + } + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_DRAW, 0); + return FALSE; + } + case 3: { + // EVENT_PHASE_PRESTART is removed + return FALSE; + } + case 4: { + //Standby Phase + infos.phase = PHASE_STANDBY; + core.phase_action = FALSE; + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_SP)) { + core.units.begin()->step = 5; + reset_phase(PHASE_STANDBY); + adjust_all(); + return FALSE; + } + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + raise_event(nullptr, EVENT_PHASE_START + PHASE_STANDBY, 0, 0, 0, turn_player, 0); + process_instant_event(); + return FALSE; + } + case 5: { + // EVENT_PHASE_START + PHASE_STANDBY is a special case(c89642993) + if(core.new_fchain.size() || core.new_ochain.size() || core.instant_event.back().event_code != EVENT_PHASE_START + PHASE_STANDBY) + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_STANDBY, 0); + return FALSE; + } + case 6: { + //Main1 + infos.phase = PHASE_MAIN1; + core.phase_action = FALSE; + raise_event(nullptr, EVENT_PHASE_START + PHASE_MAIN1, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 7: { + return FALSE; + } + case 8: { + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + add_process(PROCESSOR_IDLE_COMMAND, 0, 0, 0, 0, 0); + return FALSE; + } + case 9: { + if(returns.ivalue[0] == 7) { // End Phase + core.units.begin()->step = 14; + return FALSE; + } + infos.phase = PHASE_BATTLE_START; + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + core.phase_action = FALSE; + ++core.battle_phase_count[infos.turn_player]; + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + // Show the texts to indicate that BP is entered and skipped + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_BP)) { + core.units.begin()->step = 14; + reset_phase(PHASE_BATTLE_START); + reset_phase(PHASE_BATTLE_STEP); + reset_phase(PHASE_BATTLE); + adjust_all(); + return FALSE; + } + raise_event(nullptr, EVENT_PHASE_START + PHASE_BATTLE_START, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 10: { + if(core.new_fchain.size() || core.new_ochain.size()) + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_BATTLE_START, 0); + return FALSE; + } + case 11: { + infos.phase = PHASE_BATTLE_STEP; + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + core.phase_action = FALSE; + core.chain_attack = FALSE; + add_process(PROCESSOR_BATTLE_COMMAND, 0, 0, 0, 0, 0); + return FALSE; + } + case 12: { + if(core.units.begin()->arg2 == 0 && returns.ivalue[1]) { // 2nd Battle Phase + core.units.begin()->arg2 = 1; + core.units.begin()->step = 8; + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(!pcard) + continue; + pcard->attack_announce_count = 0; + pcard->announce_count = 0; + pcard->attacked_count = 0; + pcard->removed_overlay_count = 0; + pcard->announced_cards.clear(); + pcard->attacked_cards.clear(); + pcard->battled_cards.clear(); + } + } + return FALSE; + } + // ensure "entered 2nd Battle Phase" marker stored in `arg2` do not carry over into Main Phase 2. + core.units.begin()->arg2 = 0; + + core.skip_m2 = FALSE; + if(returns.ivalue[0] == 3) { // End Phase + core.skip_m2 = TRUE; + } + //Main2 + infos.phase = PHASE_MAIN2; + core.phase_action = FALSE; + raise_event(nullptr, EVENT_PHASE_START + PHASE_MAIN2, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 13: { + if(core.new_fchain.size() || core.new_ochain.size()) + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + return FALSE; + } + case 14: { + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + infos.can_shuffle = TRUE; + add_process(PROCESSOR_IDLE_COMMAND, 0, 0, 0, 0, 0); + return FALSE; + } + case 15: { + //End Phase + infos.phase = PHASE_END; + core.phase_action = FALSE; + if(is_player_affected_by_effect(infos.turn_player, EFFECT_SKIP_EP)) { + core.units.begin()->step = 17; + reset_phase(PHASE_END); + adjust_all(); + return FALSE; + } + pduel->write_buffer8(MSG_NEW_PHASE); + pduel->write_buffer16(infos.phase); + raise_event(nullptr, EVENT_PHASE_START + PHASE_END, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 16: { + if(core.new_fchain.size() || core.new_ochain.size()) + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, 0, 0); + return FALSE; + } + case 17: { + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + add_process(PROCESSOR_PHASE_EVENT, 0, 0, 0, PHASE_END, 0); + return FALSE; + } + case 18: { + raise_event(nullptr, EVENT_TURN_END, 0, 0, 0, turn_player, 0); + process_instant_event(); + adjust_all(); + return FALSE; + } + case 19: { + core.new_fchain.clear(); + core.new_ochain.clear(); + core.quick_f_chain.clear(); + core.delayed_quick_tmp.clear(); + core.units.begin()->step = -1; + core.units.begin()->arg1 = 1 - core.units.begin()->arg1; + return FALSE; + } + } + return TRUE; +} +int32_t field::add_chain(uint16_t step) { + switch (step) { + case 0: { + if (!core.new_chains.size()) + return TRUE; + auto& clit = core.new_chains.front(); + effect* peffect = clit.triggering_effect; + card* phandler = peffect->get_handler(); + effect_set eset; + filter_player_effect(clit.triggering_player, EFFECT_ACTIVATE_COST, &eset); + for(effect_set::size_type i = 0; i < eset.size(); ++i) { + pduel->lua->add_param(eset[i], PARAM_TYPE_EFFECT); + pduel->lua->add_param(clit.triggering_effect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(clit.triggering_player, PARAM_TYPE_INT); + if(!pduel->lua->check_condition(eset[i]->target, 3)) + continue; + if(eset[i]->operation) { + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, eset[i], 0, clit.triggering_player, 0); + } + } + if(peffect->type & EFFECT_TYPE_ACTIVATE) { + if(peffect->get_required_handorset_effects(&clit.required_handorset_effects, clit.triggering_player, clit.evt) != 2) { + clit.required_handorset_effects.clear(); + } + if(phandler->current.location == LOCATION_HAND) { + uint32_t zone = 0xff; + if(!(phandler->data.type & (TYPE_FIELD | TYPE_PENDULUM)) && peffect->is_flag(EFFECT_FLAG_LIMIT_ZONE)) { + pduel->lua->add_param(clit.triggering_player, PARAM_TYPE_INT); + pduel->lua->add_param(clit.evt.event_cards , PARAM_TYPE_GROUP); + pduel->lua->add_param(clit.evt.event_player, PARAM_TYPE_INT); + pduel->lua->add_param(clit.evt.event_value, PARAM_TYPE_INT); + pduel->lua->add_param(clit.evt.reason_effect , PARAM_TYPE_EFFECT); + pduel->lua->add_param(clit.evt.reason, PARAM_TYPE_INT); + pduel->lua->add_param(clit.evt.reason_player, PARAM_TYPE_INT); + zone = peffect->get_value(7); + if(!zone) + zone = 0xff; + } + phandler->enable_field_effect(false); + phandler->set_status(STATUS_ACT_FROM_HAND, TRUE); + if(phandler->data.type & TYPE_FIELD) + zone = 0x1U << 5; + move_to_field(phandler, phandler->current.controler, phandler->current.controler, LOCATION_SZONE, POS_FACEUP, FALSE, 0, (phandler->data.type & TYPE_PENDULUM) ? TRUE : FALSE, zone); + } else { + phandler->set_status(STATUS_ACT_FROM_HAND, FALSE); + change_position(phandler, 0, phandler->current.controler, POS_FACEUP, 0); + } + } + return FALSE; + } + case 1: { + auto& clit = core.new_chains.front(); + effect* peffect = clit.triggering_effect; + card* phandler = peffect->get_handler(); + phandler->refresh_disable_status(); + if(peffect->type & EFFECT_TYPE_ACTIVATE) { + clit.set_triggering_state(phandler); + } + pduel->write_buffer8(MSG_CHAINING); + pduel->write_buffer32(phandler->data.code); + pduel->write_buffer32(phandler->get_info_location()); + pduel->write_buffer8(clit.triggering_controler); + pduel->write_buffer8((uint8_t)clit.triggering_location); + pduel->write_buffer8(clit.triggering_sequence); + pduel->write_buffer32(peffect->description); + pduel->write_buffer8((uint8_t)core.current_chain.size() + 1); + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + peffect->card_type = phandler->get_type(); + if((peffect->card_type & (TYPE_TRAP | TYPE_MONSTER)) == (TYPE_TRAP | TYPE_MONSTER)) + peffect->card_type -= TYPE_TRAP; + peffect->set_active_type(); + if (peffect->type & EFFECT_TYPE_XMATERIAL) { + peffect->active_handler = peffect->handler->overlay_target; + peffect->last_handler = peffect->handler->overlay_target; + } + clit.chain_count = (uint8_t)core.current_chain.size() + 1; + clit.target_cards = 0; + clit.target_player = PLAYER_NONE; + clit.target_param = 0; + clit.disable_reason = 0; + clit.disable_player = PLAYER_NONE; + clit.replace_op = 0; + if(phandler->current.location == LOCATION_HAND) + clit.flag |= CHAIN_HAND_EFFECT; + core.current_chain.push_back(clit); + core.is_target_ready = false; + check_chain_counter(peffect, clit.triggering_player, clit.chain_count); + // triggered events which are not caused by event create relation with the handler + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) + && (!(peffect->type & (EFFECT_TYPE_TRIGGER_F | EFFECT_TYPE_TRIGGER_O)) || peffect->get_code_type() == CODE_PHASE)) { + phandler->create_relation(clit); + } + peffect->effect_owner = clit.triggering_player; + // DISABLE_CHAIN should be check before cost + effect* deffect; + if(!peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) && phandler->is_has_relation(clit) && (deffect = phandler->is_affected_by_effect(EFFECT_DISABLE_EFFECT))) { + effect* negeff = pduel->new_effect(); + negeff->owner = deffect->owner; + negeff->type = EFFECT_TYPE_SINGLE; + negeff->code = EFFECT_DISABLE_CHAIN; + negeff->value = clit.chain_id; + negeff->reset_flag = RESET_CHAIN | RESET_EVENT | deffect->get_value(); + phandler->add_effect(negeff); + } + core.new_chains.pop_front(); + return FALSE; + } + case 2: { + auto& clit = core.current_chain.back(); + int32_t playerid = clit.triggering_player; + effect* peffect = clit.triggering_effect; + if(get_cteffect(peffect, playerid, TRUE)) { + const bool damage_step = infos.phase == PHASE_DAMAGE && !peffect->is_flag(EFFECT_FLAG_DAMAGE_STEP); + const bool damage_cal = infos.phase == PHASE_DAMAGE_CAL && !peffect->is_flag(EFFECT_FLAG_DAMAGE_CAL); + if(damage_step || damage_cal) { + returns.ivalue[0] = TRUE; + return FALSE; + } + add_process(PROCESSOR_SELECT_EFFECTYN, 0, 0, (group*)peffect->get_handler(), playerid, 94); + } else + returns.ivalue[0] = FALSE; + return FALSE; + } + case 3: { + if(!returns.ivalue[0]) { + core.select_chains.clear(); + core.select_options.clear(); + core.units.begin()->step = 4; + return FALSE; + } + if(core.select_chains.size() > 1) { + auto& clit = core.current_chain.back(); + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, clit.triggering_player, 0); + } else + returns.ivalue[0] = 0; + return FALSE; + } + case 4: { + auto& clit = core.current_chain.back(); + chain& ch = core.select_chains[returns.ivalue[0]]; + int32_t playerid = clit.triggering_player; + effect* peffect = ch.triggering_effect; + card* phandler = peffect->get_handler(); + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_OPSELECTED); + pduel->write_buffer8(playerid); + pduel->write_buffer32(core.select_options.size() > 1 ? core.select_options[returns.ivalue[0]] : 65); + clit.triggering_effect = peffect; + clit.evt = ch.evt; + phandler->create_relation(clit); + peffect->set_active_type(); + peffect->dec_count(playerid); + if(!(peffect->type & EFFECT_TYPE_ACTIVATE)) { + peffect->type |= EFFECT_TYPE_ACTIVATE; + clit.flag |= CHAIN_ACTIVATING; + } + core.select_chains.clear(); + core.select_options.clear(); + effect* deffect = pduel->new_effect(); + deffect->owner = phandler; + deffect->code = 0; + deffect->type = EFFECT_TYPE_SINGLE; + deffect->flag[0] = EFFECT_FLAG_CANNOT_DISABLE | EFFECT_FLAG_CLIENT_HINT; + deffect->description = 65; + deffect->reset_flag = RESET_CHAIN; + phandler->add_effect(deffect); + return FALSE; + } + case 5: { + auto& clit = core.current_chain.back(); + if (!clit.required_handorset_effects.size()) { + core.units.begin()->step = 6; + return FALSE; + } + if(clit.required_handorset_effects.size() == 1) { + returns.ivalue[0] = 0; + return FALSE; + } else { + // check if there's only one type of ceffects + auto peffect = clit.triggering_effect; + auto playerid = clit.triggering_player; + int32_t ceffect_unique_id = 0; + for(effect_set::size_type i = 0; i < clit.required_handorset_effects.size(); ++i) { + pduel->lua->add_param(peffect, PARAM_TYPE_EFFECT); + pduel->lua->add_param(playerid, PARAM_TYPE_INT); + auto id = clit.required_handorset_effects[i]->get_value(2); + if (!id || ceffect_unique_id != id) { + ceffect_unique_id = 0; + break; + } + if (!ceffect_unique_id) { + ceffect_unique_id = id; + } + } + if (ceffect_unique_id) { + // all ceffects are the same type, so skip asking + returns.ivalue[0] = 0; + return FALSE; + } + } + core.select_options.clear(); + for(effect_set::size_type i = 0; i < clit.required_handorset_effects.size(); ++i) { + core.select_options.push_back(clit.required_handorset_effects[i]->description); + } + add_process(PROCESSOR_SELECT_OPTION, 0, 0, 0, clit.triggering_player, 0); + return FALSE; + } + case 6: { + auto& clit = core.current_chain.back(); + auto ceffect = clit.required_handorset_effects[returns.ivalue[0]]; + ceffect->dec_count(clit.triggering_player); + if(ceffect->description) { + pduel->write_buffer8(MSG_HINT); + pduel->write_buffer8(HINT_OPSELECTED); + pduel->write_buffer8(clit.triggering_player); + pduel->write_buffer32(ceffect->description); + } + if(ceffect->cost) { + pduel->lua->add_param(clit.triggering_effect, PARAM_TYPE_EFFECT); + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_COST, 0, ceffect, 0, clit.triggering_player, 0); + } + return FALSE; + } + case 7: { + auto& clit = core.current_chain.back(); + effect* peffect = clit.triggering_effect; + peffect->cost_checked = TRUE; + if(peffect->cost) { + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_COST, 0, peffect, 0, clit.triggering_player, 0); + } + return FALSE; + } + case 8: { + auto& clit = core.current_chain.back(); + effect* peffect = clit.triggering_effect; + if(peffect->target) { + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_TARGET, 0, peffect, 0, clit.triggering_player, 0); + } + return FALSE; + } + case 9: { + break_effect(); + core.is_target_ready = true; + auto& clit = core.current_chain.back(); + effect* peffect = clit.triggering_effect; + peffect->cost_checked = FALSE; + card* phandler = peffect->get_handler(); + if(clit.target_cards && clit.target_cards->container.size()) { + if(peffect->is_flag(EFFECT_FLAG_CARD_TARGET)) { + for(auto& pcard : clit.target_cards->container) + raise_single_event(pcard, 0, EVENT_BECOME_TARGET, peffect, 0, clit.triggering_player, 0, clit.chain_count); + process_single_event(); + if(clit.target_cards->container.size()) + raise_event(clit.target_cards->container, EVENT_BECOME_TARGET, peffect, 0, clit.triggering_player, clit.triggering_player, clit.chain_count); + } + } + if(peffect->type & EFFECT_TYPE_ACTIVATE) { + core.leave_confirmed.insert(phandler); + if(!(phandler->data.type & (TYPE_CONTINUOUS | TYPE_FIELD | TYPE_EQUIP | TYPE_PENDULUM)) + && !phandler->is_affected_by_effect(EFFECT_REMAIN_FIELD)) + phandler->set_status(STATUS_LEAVE_CONFIRMED, TRUE); + } + if((phandler->get_type() & (TYPE_SPELL | TYPE_TRAP)) + && (phandler->get_type() & (TYPE_CONTINUOUS | TYPE_FIELD | TYPE_EQUIP | TYPE_PENDULUM)) + && phandler->is_has_relation(clit) && phandler->current.location == LOCATION_SZONE + && !peffect->is_flag(EFFECT_FLAG_FIELD_ONLY)) + clit.flag |= CHAIN_CONTINUOUS_CARD; + pduel->write_buffer8(MSG_CHAINED); + pduel->write_buffer8(clit.chain_count); + raise_event(phandler, EVENT_CHAINING, peffect, 0, clit.triggering_player, clit.triggering_player, clit.chain_count); + process_instant_event(); + if(core.new_chains.size()) + add_process(PROCESSOR_ADD_CHAIN, 0, 0, 0, 0, 0); + adjust_all(); + return TRUE; + } + } + return TRUE; +} +void field::solve_continuous(uint8_t playerid, effect* peffect, const tevent& e) { + chain newchain; + newchain.chain_id = 0; + newchain.chain_count = 0; + newchain.triggering_effect = peffect; + newchain.triggering_player = playerid; + newchain.evt = e; + newchain.target_cards = 0; + newchain.target_player = PLAYER_NONE; + newchain.target_param = 0; + newchain.disable_player = PLAYER_NONE; + newchain.disable_reason = 0; + newchain.flag = 0; + core.sub_solving_continuous.push_back(newchain); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); +} +int32_t field::solve_continuous(uint16_t step) { + switch(step) { + case 0: { + core.solving_continuous.splice(core.solving_continuous.begin(), core.sub_solving_continuous); + auto& clit = core.solving_continuous.front(); + effect* peffect = clit.triggering_effect; + uint8_t triggering_player = clit.triggering_player; + if(!peffect->check_count_limit(triggering_player)) { + core.solving_continuous.pop_front(); + return TRUE; + } + core.continuous_chain.push_back(clit); + if(peffect->is_flag(EFFECT_FLAG_DELAY) || peffect->get_code_type() == CODE_PHASE) + core.conti_solving = TRUE; + core.units.begin()->ptarget = (group*)core.reason_effect; + core.units.begin()->arg2 = core.reason_player; + if(!peffect->target) + return FALSE; + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_TARGET, 0, peffect, 0, triggering_player, 0); + return FALSE; + } + case 1: { + return FALSE; + } + case 2: { + auto& clit = core.solving_continuous.front(); + effect* peffect = clit.triggering_effect; + uint8_t triggering_player = clit.triggering_player; + if(!peffect->operation) + return FALSE; + peffect->dec_count(triggering_player); + core.sub_solving_event.push_back(clit.evt); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, peffect, 0, triggering_player, 0); + return FALSE; + } + case 3: { + auto& clit = core.solving_continuous.front(); + effect* peffect = clit.triggering_effect; + core.reason_effect = (effect*)core.units.begin()->ptarget; + core.reason_player = (uint8_t)core.units.begin()->arg2; + if(core.continuous_chain.back().target_cards) + pduel->delete_group(core.continuous_chain.back().target_cards); + for(auto& oit : core.continuous_chain.back().opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + core.continuous_chain.pop_back(); + core.solving_continuous.pop_front(); + if(peffect->is_flag(EFFECT_FLAG_DELAY) || peffect->get_code_type() == CODE_PHASE) { + core.conti_solving = FALSE; + adjust_all(); + return FALSE; + } + return TRUE; + } + case 4: { + if(core.conti_player == PLAYER_NONE) + core.conti_player = infos.turn_player; + if(core.conti_player == infos.turn_player) { + if(core.delayed_continuous_tp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), core.delayed_continuous_tp, core.delayed_continuous_tp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } else + core.conti_player = 1 - infos.turn_player; + } + if(core.conti_player == 1 - infos.turn_player) { + if(core.delayed_continuous_ntp.size()) { + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), core.delayed_continuous_ntp, core.delayed_continuous_ntp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } else if(core.delayed_continuous_tp.size()) { + core.conti_player = infos.turn_player; + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), core.delayed_continuous_tp, core.delayed_continuous_tp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } else + core.conti_player = PLAYER_NONE; + } + return TRUE; + } + } + return TRUE; +} +int32_t field::solve_chain(uint16_t step, uint32_t chainend_arg1, uint32_t chainend_arg2) { + if(core.current_chain.size() == 0 && step == 0) + return TRUE; + auto cait = core.current_chain.rbegin(); + switch(step) { + case 0: { + core.phase_action = TRUE; + pduel->write_buffer8(MSG_CHAIN_SOLVING); + pduel->write_buffer8(cait->chain_count); + add_to_disable_check_list(cait->triggering_effect->get_handler()); + adjust_instant(); + raise_event(nullptr, EVENT_CHAIN_ACTIVATING, cait->triggering_effect, 0, cait->triggering_player, cait->triggering_player, cait->chain_count); + process_instant_event(); + return FALSE; + } + case 1: { + effect* peffect = cait->triggering_effect; + if(cait->flag & CHAIN_DISABLE_ACTIVATE && is_chain_negatable(cait->chain_count)) { + remove_oath_effect(peffect); + if(peffect->is_flag(EFFECT_FLAG_COUNT_LIMIT) && (peffect->count_code & EFFECT_COUNT_CODE_OATH)) { + dec_effect_code(peffect->count_code, cait->triggering_player); + } + check_chain_counter(peffect, cait->triggering_player, cait->chain_count, true); + core.new_fchain.remove_if([chaincount = cait->chain_count](chain ch) { return ch.evt.event_code == EVENT_CHAINING && ch.evt.event_value == chaincount; }); + core.new_ochain.remove_if([chaincount = cait->chain_count](chain ch) { return ch.evt.event_code == EVENT_CHAINING && ch.evt.event_value == chaincount; }); + raise_event(nullptr, EVENT_CHAIN_NEGATED, peffect, 0, cait->triggering_player, cait->triggering_player, cait->chain_count); + process_instant_event(); + core.units.begin()->step = 9; + return FALSE; + } + release_oath_relation(peffect); + break_effect(); + core.chain_solving = TRUE; + raise_event(nullptr, EVENT_CHAIN_SOLVING, peffect, 0, cait->triggering_player, cait->triggering_player, cait->chain_count); + process_instant_event(); + return FALSE; + } + case 2: { + effect* peffect = cait->triggering_effect; + card* pcard = peffect->get_handler(); + if((cait->flag & CHAIN_CONTINUOUS_CARD) && !pcard->is_has_relation(*cait)){ + core.units.begin()->step = 3; + return FALSE; + } + if((peffect->type & EFFECT_TYPE_ACTIVATE) && pcard->is_has_relation(*cait) && !cait->replace_op) { + pcard->enable_field_effect(true); + if(core.duel_rule <= 2) { + if(pcard->data.type & TYPE_FIELD) { + card* fscard = player[1 - pcard->current.controler].list_szone[5]; + if(fscard && fscard->is_position(POS_FACEUP)) + fscard->enable_field_effect(false); + } + } + adjust_instant(); + } + // creating continuous target: peffect->is_flag(EFFECT_FLAG_CONTINUOUS_TARGET) && !cait->replace_op + // operation function creating continuous target should be executed even when disabled + if(is_chain_disablable(cait->chain_count) && (!peffect->is_flag(EFFECT_FLAG_CONTINUOUS_TARGET) || cait->replace_op)) { + if(is_chain_disabled(cait->chain_count) || (pcard->get_status(STATUS_DISABLED | STATUS_FORBIDDEN) && pcard->is_has_relation(*cait))) { + if(!(cait->flag & CHAIN_DISABLE_EFFECT)) { + pduel->write_buffer8(MSG_CHAIN_DISABLED); + pduel->write_buffer8(cait->chain_count); + } + raise_event(nullptr, EVENT_CHAIN_DISABLED, peffect, 0, cait->triggering_player, cait->triggering_player, cait->chain_count); + process_instant_event(); + core.units.begin()->step = 3; + return FALSE; + } + } + return FALSE; + } + case 3 : { + effect* peffect = cait->triggering_effect; + card* pcard = peffect->get_handler(); + if((cait->flag & CHAIN_CONTINUOUS_CARD) && !pcard->is_has_relation(*cait)){ + return FALSE; + } + if(cait->replace_op) { + core.units.begin()->arg4 = cait->triggering_effect->operation; + cait->triggering_effect->operation = cait->replace_op; + } else + core.units.begin()->arg4 = 0; + if(cait->triggering_effect->operation) { + core.sub_solving_event.push_back(cait->evt); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, cait->triggering_effect, 0, cait->triggering_player, 0); + } + return FALSE; + } + case 4: { + // clean up when the resolution ends + effect* peffect = cait->triggering_effect; + if(core.units.begin()->arg4) { + peffect->operation = (int32_t)core.units.begin()->arg4; + core.units.begin()->arg4 = 0; + } + core.special_summoning.clear(); + core.equiping_cards.clear(); + return FALSE; + } + case 5: { + core.chain_solving = FALSE; + if(core.delayed_continuous_tp.size()) { + core.conti_player = infos.turn_player; + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), core.delayed_continuous_tp, core.delayed_continuous_tp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } else if(core.delayed_continuous_ntp.size()) { + core.conti_player = 1 - infos.turn_player; + core.sub_solving_continuous.splice(core.sub_solving_continuous.end(), core.delayed_continuous_ntp, core.delayed_continuous_ntp.begin()); + add_process(PROCESSOR_SOLVE_CONTINUOUS, 0, 0, 0, 0, 0); + } else + core.conti_player = PLAYER_NONE; + pduel->write_buffer8(MSG_CHAIN_SOLVED); + pduel->write_buffer8(cait->chain_count); + raise_event(nullptr, EVENT_CHAIN_SOLVED, cait->triggering_effect, 0, cait->triggering_player, cait->triggering_player, cait->chain_count); + adjust_disable_check_list(); + process_instant_event(); + core.units.begin()->step = 9; + return FALSE; + } + case 10: { + effect* peffect = cait->triggering_effect; + card* pcard = peffect->get_handler(); + if((peffect->type & EFFECT_TYPE_ACTIVATE) && (cait->flag & CHAIN_ACTIVATING)) + peffect->type &= ~EFFECT_TYPE_ACTIVATE; + if((cait->flag & CHAIN_HAND_EFFECT) && !pcard->is_position(POS_FACEUP) && (pcard->current.location == LOCATION_HAND)) + shuffle(pcard->current.controler, LOCATION_HAND); + if(cait->target_cards && cait->target_cards->container.size()) { + for(auto& ptarget : cait->target_cards->container) + ptarget->release_relation(*cait); + } + if((pcard->data.type & TYPE_EQUIP) && (peffect->type & EFFECT_TYPE_ACTIVATE) + && !pcard->equiping_target && pcard->is_has_relation(*cait)) + pcard->set_status(STATUS_LEAVE_CONFIRMED, TRUE); + if(core.duel_rule <= 2) { + if((pcard->data.type & TYPE_FIELD) && (peffect->type & EFFECT_TYPE_ACTIVATE) + && !pcard->is_status(STATUS_LEAVE_CONFIRMED) && pcard->is_has_relation(*cait)) { + card* fscard = player[1 - pcard->current.controler].list_szone[5]; + if(fscard && fscard->is_position(POS_FACEUP)) + destroy(fscard, 0, REASON_RULE, 1 - pcard->current.controler); + } + } + // keep last active_type until the next activate, for the using in script + // peffect->active_type = 0; + peffect->active_handler = 0; + pcard->release_relation(*cait); + if(cait->target_cards) + pduel->delete_group(cait->target_cards); + for(auto& oit : cait->opinfos) { + if(oit.second.op_cards) + pduel->delete_group(oit.second.op_cards); + } + for(auto& cit : core.delayed_enable_set) { + if(cit->current.location == LOCATION_MZONE) + cit->enable_field_effect(true); + } + core.delayed_enable_set.clear(); + adjust_all(); + core.current_chain.pop_back(); + if(!core.current_chain.size()) { + for(auto& ch_lim : core.chain_limit) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim.function); + core.chain_limit.clear(); + return FALSE; + } + if(core.summoning_card) + core.subunits.push_back(core.summon_reserved); + core.summoning_card = 0; + core.units.begin()->step = -1; + return FALSE; + } + case 11: { + for(auto cit = core.leave_confirmed.begin(); cit != core.leave_confirmed.end();) { + if(!(*cit)->is_status(STATUS_LEAVE_CONFIRMED)) + cit = core.leave_confirmed.erase(cit); + else + ++cit; + } + if(core.leave_confirmed.size()) + send_to(core.leave_confirmed, 0, REASON_RULE, PLAYER_NONE, PLAYER_NONE, LOCATION_GRAVE, 0, POS_FACEUP); + return FALSE; + } + case 12: { + core.used_event.splice(core.used_event.end(), core.point_event); + pduel->write_buffer8(MSG_CHAIN_END); + for(auto& ch_lim_p : core.chain_limit_p) + luaL_unref(pduel->lua->lua_state, LUA_REGISTRYINDEX, ch_lim_p.function); + core.chain_limit_p.clear(); + core.effect_count_code_chain[0].clear(); + core.effect_count_code_chain[1].clear(); + core.effect_count_code_chain[PLAYER_NONE].clear(); + reset_chain(); + if (core.summoning_card) + core.subunits.push_back(core.summon_reserved); + // [YGOL] never re-queue a unit that is not a parked damage step + if (core.effect_damage_step == 1 && core.damage_step_reserved.type == PROCESSOR_DAMAGE_STEP) + core.subunits.push_back(core.damage_step_reserved); + core.summoning_card = 0; + return FALSE; + } + case 13: { + raise_event(nullptr, EVENT_CHAIN_END, 0, 0, 0, 0, 0); + process_instant_event(); + adjust_all(); + if(chainend_arg1 != 0x101 || chainend_arg2 != TRUE) { + core.hint_timing[0] |= TIMING_CHAIN_END; + core.hint_timing[1] |= TIMING_CHAIN_END; + add_process(PROCESSOR_POINT_EVENT, 0, 0, 0, chainend_arg1, chainend_arg2); + } + returns.ivalue[0] = TRUE; + return TRUE; + } + } + return TRUE; +} +int32_t field::break_effect() { + core.hint_timing[0] &= TIMING_DAMAGE_STEP | TIMING_DAMAGE_CAL; + core.hint_timing[1] &= TIMING_DAMAGE_STEP | TIMING_DAMAGE_CAL; + for (auto chit = core.new_ochain.begin(); chit != core.new_ochain.end();) { + effect* peffect = chit->triggering_effect; + if (!peffect->is_flag(EFFECT_FLAG_DELAY)) { + if (peffect->is_flag(EFFECT_FLAG_FIELD_ONLY) + || !(peffect->type & EFFECT_TYPE_FIELD) || peffect->in_range(*chit)) { + pduel->write_buffer8(MSG_MISSED_EFFECT); + pduel->write_buffer32(peffect->get_handler()->get_info_location()); + pduel->write_buffer32(peffect->get_handler()->data.code); + } + chit = core.new_ochain.erase(chit); + } + else + ++chit; + } + core.used_event.splice(core.used_event.end(), core.instant_event); + adjust_instant(); + return 0; +} +void field::adjust_instant() { + adjust_disable_check_list(); + adjust_self_destroy_set(); +} +void field::adjust_all() { + core.readjust_map.clear(); + add_process(PROCESSOR_ADJUST, 0, 0, 0, 0, 0); +} +void field::refresh_location_info_instant() { + effect_set eset; + uint32_t dis1 = player[0].disabled_location | (player[1].disabled_location << 16); + player[0].disabled_location = 0; + player[1].disabled_location = 0; + filter_field_effect(EFFECT_DISABLE_FIELD, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t value = eset[i]->get_value(); + player[0].disabled_location |= value & 0x1f7f; + player[1].disabled_location |= (value >> 16) & 0x1f7f; + } + eset.clear(); + filter_field_effect(EFFECT_USE_EXTRA_MZONE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint8_t p = eset[i]->get_handler_player(); + uint32_t value = eset[i]->get_value(); + player[p].disabled_location |= (value >> 16) & 0x1f; + } + eset.clear(); + filter_field_effect(EFFECT_USE_EXTRA_SZONE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint8_t p = eset[i]->get_handler_player(); + uint32_t value = eset[i]->get_value(); + player[p].disabled_location |= (value >> 8) & 0x1f00; + } + player[0].disabled_location |= (((player[1].disabled_location >> 5) & 1) << 6) | (((player[1].disabled_location >> 6) & 1) << 5); + player[1].disabled_location |= (((player[0].disabled_location >> 5) & 1) << 6) | (((player[0].disabled_location >> 6) & 1) << 5); + uint32_t dis2 = player[0].disabled_location | (player[1].disabled_location << 16); + if(dis1 != dis2) { + pduel->write_buffer8(MSG_FIELD_DISABLED); + pduel->write_buffer32(dis2); + } +} +int32_t field::refresh_location_info(uint16_t step) { + switch(step) { + case 0: { + effect_set eset; + core.units.begin()->arg2 = player[0].disabled_location | (player[1].disabled_location << 16); + player[0].disabled_location = 0; + player[1].disabled_location = 0; + core.disfield_effects.clear(); + core.extra_mzone_effects.clear(); + core.extra_szone_effects.clear(); + filter_field_effect(EFFECT_DISABLE_FIELD, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint32_t value = eset[i]->get_value(); + if(value) { + player[0].disabled_location |= value & 0x1f7f; + player[1].disabled_location |= (value >> 16) & 0x1f7f; + } else + core.disfield_effects.push_back(eset[i]); + } + eset.clear(); + filter_field_effect(EFFECT_USE_EXTRA_MZONE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint8_t p = eset[i]->get_handler_player(); + uint32_t value = eset[i]->get_value(); + player[p].disabled_location |= (value >> 16) & 0x1f; + if((uint32_t)field_used_count[(value >> 16) & 0x1f] < (value & 0xffff)) + core.extra_mzone_effects.push_back(eset[i]); + } + eset.clear(); + filter_field_effect(EFFECT_USE_EXTRA_SZONE, &eset); + for (effect_set::size_type i = 0; i < eset.size(); ++i) { + uint8_t p = eset[i]->get_handler_player(); + uint32_t value = eset[i]->get_value(); + player[p].disabled_location |= (value >> 8) & 0x1f00; + if((uint32_t)field_used_count[(value >> 16) & 0x1f] < (value & 0xffff)) + core.extra_szone_effects.push_back(eset[i]); + } + return FALSE; + } + case 1: { + if(core.disfield_effects.size() == 0) { + core.units.begin()->step = 2; + return FALSE; + } + effect* peffect = core.disfield_effects[0]; + core.units.begin()->peffect = peffect; + core.disfield_effects.erase(core.disfield_effects.begin()); + if(!peffect->operation) { + peffect->value = 0x80; + core.units.begin()->step = 0; + return FALSE; + } + core.sub_solving_event.push_back(nil_event); + add_process(PROCESSOR_EXECUTE_OPERATION, 0, peffect, 0, peffect->get_handler_player(), 0); + return FALSE; + } + case 2: { + returns.ivalue[0] &= 0x1f7f1f7f; + if(returns.ivalue[0] == 0) + returns.ivalue[0] = 0x80; + if(core.units.begin()->peffect->get_handler_player() == 0) { + core.units.begin()->peffect->value = returns.ivalue[0]; + player[0].disabled_location |= returns.ivalue[0] & 0x1f7f; + player[1].disabled_location |= (returns.ivalue[0] >> 16) & 0x1f7f; + } else { + core.units.begin()->peffect->value = ((returns.ivalue[0] << 16) | (returns.ivalue[0] >> 16)); + player[1].disabled_location |= returns.ivalue[0] & 0x1f7f; + player[0].disabled_location |= (returns.ivalue[0] >> 16) & 0x1f7f; + } + core.units.begin()->step = 0; + return FALSE; + } + case 3: { + if(core.extra_mzone_effects.size() == 0) { + core.units.begin()->step = 4; + return FALSE; + } + effect* peffect = core.extra_mzone_effects[0]; + core.units.begin()->peffect = peffect; + core.extra_mzone_effects.erase(core.extra_mzone_effects.begin()); + uint32_t p = peffect->get_handler_player(); + uint32_t mzone_flag = (player[p].disabled_location | player[p].used_location) & 0x1f; + if(mzone_flag == 0x1f) { + core.units.begin()->step = 4; + return FALSE; + } + int32_t val = peffect->get_value(); + int32_t dis_count = (val & 0xffff) - field_used_count[(val >> 16) & 0x1f]; + int32_t empty_count = 5 - field_used_count[mzone_flag]; + uint32_t flag = mzone_flag | 0xffffffe0; + if(dis_count > empty_count) + dis_count = empty_count; + core.units.begin()->arg1 = dis_count; + add_process(PROCESSOR_SELECT_DISFIELD, 0, 0, 0, p, flag, dis_count); + return FALSE; + } + case 4: { + uint32_t dis_count = (uint32_t)core.units.begin()->arg1; + uint32_t mzone_flag = 0; + uint8_t pt = 0; + for(uint32_t i = 0; i < dis_count; ++i) { + uint8_t s = returns.bvalue[pt + 2]; + mzone_flag |= 0x1u << s; + pt += 3; + } + effect* peffect = core.units.begin()->peffect; + player[peffect->get_handler_player()].disabled_location |= mzone_flag; + peffect->value = (int32_t)(peffect->value | (mzone_flag << 16)); + core.units.begin()->step = 2; + return FALSE; + } + case 5: { + if(core.extra_szone_effects.size() == 0) { + core.units.begin()->step = 6; + return FALSE; + } + effect* peffect = core.extra_szone_effects[0]; + core.units.begin()->peffect = peffect; + core.extra_szone_effects.erase(core.extra_szone_effects.begin()); + uint32_t p = peffect->get_handler_player(); + uint32_t szone_flag = ((player[p].disabled_location | player[p].used_location) >> 8) & 0x1f; + if(szone_flag == 0x1f) { + core.units.begin()->step = 6; + return FALSE; + } + int32_t val = peffect->get_value(); + uint32_t dis_count = (val & 0xffff) - field_used_count[(val >> 16) & 0x1f]; + uint32_t empty_count = 5 - field_used_count[szone_flag]; + uint32_t flag = (szone_flag << 8) | 0xffffe0ff; + if(dis_count > empty_count) + dis_count = empty_count; + core.units.begin()->arg1 = dis_count; + add_process(PROCESSOR_SELECT_DISFIELD, 0, 0, 0, p, flag, dis_count); + return FALSE; + } + case 6: { + uint32_t dis_count = (uint32_t)core.units.begin()->arg1; + uint32_t szone_flag = 0; + uint8_t pt = 0; + for(uint32_t i = 0; i < dis_count; ++i) { + uint8_t s = returns.bvalue[pt + 2]; + szone_flag |= 0x1u << s; + pt += 3; + } + effect* peffect = core.units.begin()->peffect; + player[peffect->get_handler_player()].disabled_location |= szone_flag << 8; + peffect->value = (int32_t)(peffect->value | (szone_flag << 16)); + core.units.begin()->step = 4; + return FALSE; + } + case 7: { + player[0].disabled_location |= (((player[1].disabled_location >> 5) & 1) << 6) | (((player[1].disabled_location >> 6) & 1) << 5); + player[1].disabled_location |= (((player[0].disabled_location >> 5) & 1) << 6) | (((player[0].disabled_location >> 6) & 1) << 5); + uint32_t dis = player[0].disabled_location | (player[1].disabled_location << 16); + if(dis != (uint32_t)core.units.begin()->arg2) { + pduel->write_buffer8(MSG_FIELD_DISABLED); + pduel->write_buffer32(dis); + } + return TRUE; + } + } + return TRUE; +} +int32_t field::adjust_step(uint16_t step) { + switch(step) { + case 0: { + core.re_adjust = FALSE; + return FALSE; + } + case 1: { + //win check + uint32_t winp = 5, rea = 1; + bool lp_zero_0 = (player[0].lp <= 0 && !is_player_affected_by_effect(0, EFFECT_CANNOT_LOSE_KOISHI)); + bool lp_zero_1 = (player[1].lp <= 0 && !is_player_affected_by_effect(1, EFFECT_CANNOT_LOSE_KOISHI)); + if(lp_zero_0 && !lp_zero_1) { + winp = 1; + rea = 1; + } + if(core.overdraw[0] && !core.overdraw[1]) { + winp = 1; + rea = 2; + } + if(lp_zero_1 && !lp_zero_0) { + winp = 0; + rea = 1; + } + if(core.overdraw[1] && !core.overdraw[0]) { + winp = 0; + rea = 2; + } + if(lp_zero_0 && lp_zero_1) { + winp = PLAYER_NONE; + rea = 1; + } + if(core.overdraw[1] && core.overdraw[0]) { + winp = PLAYER_NONE; + rea = 2; + } + if(winp != 5) { + pduel->write_buffer8(MSG_WIN); + pduel->write_buffer8(winp); + pduel->write_buffer8(rea); + core.overdraw[0] = core.overdraw[1] = FALSE; + core.win_player = 5; + core.win_reason = 0; + } else if(core.win_player != 5) { + pduel->write_buffer8(MSG_WIN); + pduel->write_buffer8(core.win_player); + pduel->write_buffer8(core.win_reason); + core.win_player = 5; + core.win_reason = 0; + core.overdraw[0] = core.overdraw[1] = FALSE; + } + return FALSE; + } + case 2: { + //disable check + uint8_t tp = infos.turn_player; + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[tp].list_mzone) { + if(pcard) + add_to_disable_check_list(pcard); + } + for(auto& pcard : player[tp].list_szone) { + if(pcard) + add_to_disable_check_list(pcard); + } + tp = 1 - tp; + } + adjust_disable_check_list(); + add_process(PROCESSOR_REFRESH_LOC, 0, 0, 0, 0, 0); + return FALSE; + } + case 3: { + //trap monster + core.trap_monster_adjust_set[0].clear(); + core.trap_monster_adjust_set[1].clear(); + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(!pcard) continue; + if((pcard->get_type() & TYPE_TRAPMONSTER) && pcard->is_affected_by_effect(EFFECT_DISABLE_TRAPMONSTER)) { + core.trap_monster_adjust_set[p].insert(pcard); + } + } + } + if(core.trap_monster_adjust_set[0].size() || core.trap_monster_adjust_set[1].size()) { + core.re_adjust = TRUE; + add_process(PROCESSOR_TRAP_MONSTER_ADJUST, 0, 0, 0, 0, 0); + } + return FALSE; + } + case 4: { + //control + core.control_adjust_set[0].clear(); + core.control_adjust_set[1].clear(); + card_set reason_cards; + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(!pcard) continue; + uint8_t cur = pcard->current.controler; + auto res = pcard->refresh_control_status(); +#ifdef _IRR_ANDROID_PLATFORM_ + uint8_t ref = std::get<0>(res); + effect* peffect = std::get<1>(res); +#else + uint8_t ref = std::get(res); + effect* peffect = std::get(res); +#endif + if(cur != ref && pcard->is_capable_change_control()) { + core.control_adjust_set[p].insert(pcard); + if(peffect && (!(peffect->type & EFFECT_TYPE_SINGLE) || peffect->condition)) + reason_cards.insert(peffect->get_handler()); + } + } + } + if(core.control_adjust_set[0].size() || core.control_adjust_set[1].size()) { + core.re_adjust = TRUE; + get_control(core.control_adjust_set[1 - infos.turn_player], 0, PLAYER_NONE, infos.turn_player, 0, 0, 0xff); + get_control(core.control_adjust_set[infos.turn_player], 0, PLAYER_NONE, 1 - infos.turn_player, 0, 0, 0xff); + for(auto& rcard : reason_cards) { + ++core.readjust_map[rcard]; + if(core.readjust_map[rcard] > 3) + destroy(rcard, 0, REASON_RULE, PLAYER_NONE); + } + } + core.last_control_changed_id = infos.field_id; + return FALSE; + } + case 5: { + //remove brainwashing + if(core.global_flag & GLOBALFLAG_BRAINWASHING_CHECK) { + core.control_adjust_set[0].clear(); + core.control_adjust_set[1].clear(); + effect_set eset; + filter_field_effect(EFFECT_REMOVE_BRAINWASHING, &eset, FALSE); + uint32_t res = eset.size() ? TRUE : FALSE; + if(res) { + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[p].list_mzone) { + if(pcard && pcard->is_affected_by_effect(EFFECT_REMOVE_BRAINWASHING)) { + //the opposite of pcard->check_control_effect() + auto pr = pcard->single_effect.equal_range(EFFECT_SET_CONTROL); + for(auto eit = pr.first; eit != pr.second;) { + effect* peffect = eit->second; + ++eit; + if(!peffect->condition) + peffect->handler->remove_effect(peffect); + } + if(p != pcard->owner && pcard->is_capable_change_control()) + core.control_adjust_set[p].insert(pcard); + } + } + } + } + core.remove_brainwashing = res; + if(core.control_adjust_set[0].size() || core.control_adjust_set[1].size()) { + core.re_adjust = TRUE; + get_control(core.control_adjust_set[1 - infos.turn_player], 0, PLAYER_NONE, infos.turn_player, 0, 0, 0xff); + get_control(core.control_adjust_set[infos.turn_player], 0, PLAYER_NONE, 1 - infos.turn_player, 0, 0, 0xff); + } + } + core.units.begin()->step = 7; + return FALSE; + } + case 8: { + if(adjust_grant_effect()) + core.re_adjust = TRUE; + return FALSE; + } + case 9: { + if(core.selfdes_disabled) { + core.units.begin()->step = 10; + return FALSE; + } + //self destroy + adjust_self_destroy_set(); + return FALSE; + } + case 10: { + //equip check + uint8_t tp = infos.turn_player; + card_set destroy_set; + for(uint8_t p = 0; p < 2; ++p) { + for(uint8_t i = 0; i < 5; ++i) { + card* pcard = player[tp].list_szone[i]; + if(pcard && pcard->equiping_target && !pcard->is_affected_by_effect(EFFECT_EQUIP_LIMIT, pcard->equiping_target)) + destroy_set.insert(pcard); + } + tp = 1 - tp; + } + if(destroy_set.size()) { + core.re_adjust = TRUE; + destroy(destroy_set, 0, REASON_RULE, PLAYER_NONE); + } + return FALSE; + } + case 11: { + //position + uint32_t tp = infos.turn_player, pos; + card_set pos_adjust; + effect_set eset; + for(uint8_t p = 0; p < 2; ++p) { + for(auto& pcard : player[tp].list_mzone) { + if(!pcard || (pcard->data.type & TYPE_LINK) || pcard->is_affected_by_effect(EFFECT_CANNOT_CHANGE_POS_E)) + continue; + eset.clear(); + pcard->filter_effect(EFFECT_SET_POSITION, &eset); + if(eset.size()) { + pos = eset.back()->get_value(); + if((pos & 0xff) != pcard->current.position) { + pos_adjust.insert(pcard); + pcard->position_param = pos; + if(pcard->is_status(STATUS_JUST_POS)) + pcard->set_status(STATUS_CONTINUOUS_POS, TRUE); + else + pcard->set_status(STATUS_CONTINUOUS_POS, FALSE); + } else + pcard->set_status(STATUS_CONTINUOUS_POS, FALSE); + pcard->set_status(STATUS_JUST_POS, FALSE); + } + } + tp = 1 - tp; + } + if(pos_adjust.size()) { + core.re_adjust = TRUE; + group* ng = pduel->new_group(); + ng->container.swap(pos_adjust); + ng->is_readonly = GTYPE_READ_ONLY; + add_process(PROCESSOR_CHANGEPOS, 0, 0, ng, PLAYER_NONE, TRUE); + } + return FALSE; + } + case 12: { + //shuffle check + for(auto& pcard : player[0].list_hand) { + effect* pub = pcard->is_affected_by_effect(EFFECT_PUBLIC); + if(!pub && pcard->is_position(POS_FACEUP)) + core.shuffle_hand_check[0] = TRUE; + pcard->current.position = pub ? POS_FACEUP : POS_FACEDOWN; + } + for(auto& pcard : player[1].list_hand) { + effect* pub = pcard->is_affected_by_effect(EFFECT_PUBLIC); + if(!pub && pcard->is_position(POS_FACEUP)) + core.shuffle_hand_check[1] = TRUE; + pcard->current.position = pub ? POS_FACEUP : POS_FACEDOWN; + } + if(core.shuffle_hand_check[infos.turn_player]) + shuffle(infos.turn_player, LOCATION_HAND); + if(core.shuffle_hand_check[1 - infos.turn_player]) + shuffle(1 - infos.turn_player, LOCATION_HAND); + return FALSE; + } + case 13: { + //reverse_deck + effect_set eset; + uint32_t res = 0; + if(core.global_flag & GLOBALFLAG_DECK_REVERSE_CHECK) { + filter_field_effect(EFFECT_REVERSE_DECK, &eset, FALSE); + res = eset.size() ? TRUE : FALSE; + if(core.deck_reversed ^ res) { + reverse_deck(0); + reverse_deck(1); + pduel->write_buffer8(MSG_REVERSE_DECK); + if(res) { + if(player[0].list_main.size()) { + card* ptop = player[0].list_main.back(); + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(0); + pduel->write_buffer8(0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + if(player[1].list_main.size()) { + card* ptop = player[1].list_main.back(); + pduel->write_buffer8(MSG_DECK_TOP); + pduel->write_buffer8(1); + pduel->write_buffer8(0); + if(ptop->current.position != POS_FACEUP_DEFENSE) + pduel->write_buffer32(ptop->data.code); + else + pduel->write_buffer32(ptop->data.code | 0x80000000); + } + } + } + core.deck_reversed = res; + } + return FALSE; + } + case 14: { + //attack cancel + card* attacker = core.attacker; + if(!attacker) + return FALSE; + if(attacker->is_status(STATUS_ATTACK_CANCELED)) + return FALSE; + if(infos.phase != PHASE_DAMAGE && infos.phase != PHASE_DAMAGE_CAL) { + if(!core.attacker->is_capable_attack() + || core.attacker->current.controler != core.attacker->attack_controler + || core.attacker->fieldid_r != core.pre_field[0]) { + attacker->set_status(STATUS_ATTACK_CANCELED, TRUE); + return FALSE; + } + if(core.attack_rollback) + return FALSE; + std::set fidset; + for(auto& pcard : player[1 - infos.turn_player].list_mzone) { + if(pcard) + fidset.insert(pcard->fieldid_r); + } + if(fidset != core.opp_mzone || !confirm_attack_target()) + core.attack_rollback = TRUE; + } else { + if(core.attacker->current.location != LOCATION_MZONE || core.attacker->fieldid_r != core.pre_field[0] + || ((core.attacker->current.position & POS_DEFENSE) && !(core.attacker->is_affected_by_effect(EFFECT_DEFENSE_ATTACK))) + || core.attacker->current.controler != core.attacker->attack_controler + || (core.attack_target && (core.attack_target->current.location != LOCATION_MZONE + || core.attack_target->current.controler != core.attack_target->attack_controler + || core.attack_target->fieldid_r != core.pre_field[1]))) + core.attacker->set_status(STATUS_ATTACK_CANCELED, TRUE); + } + return FALSE; + } + case 15: { + raise_event(nullptr, EVENT_ADJUST, 0, 0, PLAYER_NONE, PLAYER_NONE, 0); + process_instant_event(); + return FALSE; + } + case 16: { + if(core.re_adjust) { + core.units.begin()->step = -1; + return FALSE; + } + if(core.shuffle_hand_check[0]) + shuffle(0, LOCATION_HAND); + if(core.shuffle_hand_check[1]) + shuffle(1, LOCATION_HAND); + if(core.shuffle_deck_check[0]) + shuffle(0, LOCATION_DECK); + if(core.shuffle_deck_check[1]) + shuffle(1, LOCATION_DECK); + return TRUE; + } + } + return TRUE; +} diff --git a/core/scriptlib.cpp b/core/scriptlib.cpp new file mode 100644 index 0000000..1b2ea72 --- /dev/null +++ b/core/scriptlib.cpp @@ -0,0 +1,83 @@ +/* + * scriptlib.cpp + * + * Created on: 2010-7-29 + * Author: Argon + */ +#include "scriptlib.h" +#include "duel.h" + +static int32_t check_data_type(lua_State* L, int32_t index, const char* tname) { + luaL_checkstack(L, 2, nullptr); + int32_t 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; +} +int32_t scriptlib::check_param(lua_State* L, int32_t param_type, int32_t index, int32_t retfalse) { + switch (param_type) { + case PARAM_TYPE_CARD: { + luaL_checkstack(L, 1, nullptr); + int32_t result = FALSE; + if(lua_isuserdata(L, index) && lua_getmetatable(L, index)) { + result = check_data_type(L, -1, "Card"); + lua_pop(L, 1); + } + if(result) + return TRUE; + if(retfalse) + return FALSE; + return luaL_error(L, "Parameter %d should be \"Card\".", index); + break; + } + case PARAM_TYPE_GROUP: { + if(lua_isuserdata(L, index) && check_data_type(L, index, "Group")) + return TRUE; + if(retfalse) + return FALSE; + return luaL_error(L, "Parameter %d should be \"Group\".", index); + break; + } + case PARAM_TYPE_EFFECT: { + if(lua_isuserdata(L, index) && check_data_type(L, index, "Effect")) + return TRUE; + if(retfalse) + return FALSE; + return luaL_error(L, "Parameter %d should be \"Effect\".", index); + break; + } + case PARAM_TYPE_FUNCTION: { + if(lua_isfunction(L, index)) + return TRUE; + if(retfalse) + return FALSE; + return luaL_error(L, "Parameter %d should be \"Function\".", index); + break; + } + case PARAM_TYPE_STRING: { + if(lua_isstring(L, index)) + return TRUE; + if(retfalse) + return FALSE; + return luaL_error(L, "Parameter %d should be \"String\".", index); + break; + } + } + return FALSE; +} + +int32_t scriptlib::check_param_count(lua_State* L, int32_t count) { + if (lua_gettop(L) < count) + return luaL_error(L, "%d Parameters are needed.", count); + return TRUE; +} +int32_t scriptlib::check_action_permission(lua_State* L) { + duel* pduel = interpreter::get_duel_info(L); + if(pduel->lua->no_action && !pduel->lua->disable_action_check) + return luaL_error(L, "Action is not allowed here."); + return TRUE; +} diff --git a/core/scriptlib.h b/core/scriptlib.h new file mode 100644 index 0000000..40f6540 --- /dev/null +++ b/core/scriptlib.h @@ -0,0 +1,728 @@ +/* + * scriptlib.h + * + * Created on: 2009-1-20 + * Author: Argon.Sun + */ + +#ifndef SCRIPTLIB_H_ +#define SCRIPTLIB_H_ + +#include "common.h" +#include "interpreter.h" + +constexpr bool match_all(uint32_t x, uint32_t y) { + return (x & y) == y; +} + +class scriptlib { +public: + enum effect_member : int32_t { + MEMBER_CATEGORY, + MEMBER_CODE, + MEMBER_DESCRIPTION, + MEMBER_ID, + MEMBER_RANGE, + MEMBER_TYPE, + }; + static int32_t check_param(lua_State* L, int32_t param_type, int32_t index, int32_t retfalse = FALSE); + static int32_t check_param_count(lua_State* L, int32_t count); + static int32_t check_action_permission(lua_State* L); + //millux + static int32_t card_get_card_registered(lua_State *L); + static int32_t card_is_ritual_type(lua_State *L); + static int32_t card_set_entity_code(lua_State *L); + static int32_t card_set_card_data(lua_State *L); + static int32_t card_get_link_marker(lua_State *L); + static int32_t card_get_origin_link_marker(lua_State *L); + static int32_t card_is_xyz_summonable_by_rose(lua_State *L); + static int32_t card_get_removed_overlay_count(lua_State *L); + static int32_t card_check_spsummon_once(lua_State *L); + static int32_t effect_set_owner(lua_State *L); + static int32_t effect_get_count_limit(lua_State *L); + static int32_t duel_get_master_rule(lua_State *L); + static int32_t duel_read_card(lua_State *L); + static int32_t duel_exile(lua_State *L); + static int32_t duel_disable_action_check(lua_State *L); + static int32_t duel_setmetatable(lua_State *L); + static int32_t duel_move_turn_count(lua_State *L); + static int32_t duel_get_cards_in_zone(lua_State *L); + static int32_t duel_xyz_summon_by_rose(lua_State *L); + static int32_t duel_load_script(lua_State *L); + static int32_t duel_reset_time_limit(lua_State *L); + static int32_t duel_set_summon_cancelable(lua_State *L); + static int32_t duel_get_random_number(lua_State *L); + static int32_t duel_get_registry_value(lua_State *L); + static int32_t duel_set_registry_value(lua_State *L); + static int32_t duel_get_registry_keys(lua_State *L); + static int32_t duel_set_registry(lua_State *L); + static int32_t duel_get_registry(lua_State *L); + static int32_t duel_clear_registry(lua_State *L); + static int32_t duel_is_global_flag(lua_State *L); + //card lib + static int32_t card_get_code(lua_State *L); + static int32_t card_get_origin_code(lua_State *L); + static int32_t card_get_origin_code_rule(lua_State *L); + static int32_t card_get_fusion_code(lua_State *L); + static int32_t card_get_link_code(lua_State *L); + static int32_t card_is_fusion_code(lua_State *L); + static int32_t card_is_link_code(lua_State *L); + static int32_t card_is_set_card(lua_State *L); + static int32_t card_is_origin_set_card(lua_State *L); + static int32_t card_is_pre_set_card(lua_State *L); + static int32_t card_is_fusion_set_card(lua_State *L); + static int32_t card_is_link_set_card(lua_State *L); + static int32_t card_is_special_summon_set_card(lua_State *L); + static int32_t card_get_type(lua_State *L); + static int32_t card_get_origin_type(lua_State *L); + static int32_t card_get_fusion_type(lua_State *L); + static int32_t card_get_synchro_type(lua_State *L); + static int32_t card_get_xyz_type(lua_State *L); + static int32_t card_get_link_type(lua_State *L); + static int32_t card_get_level(lua_State *L); + static int32_t card_get_rank(lua_State *L); + static int32_t card_get_link(lua_State *L); + static int32_t card_get_synchro_level(lua_State *L); + static int32_t card_get_ritual_level(lua_State *L); + static int32_t card_get_origin_level(lua_State *L); + static int32_t card_get_origin_rank(lua_State *L); + static int32_t card_is_xyz_level(lua_State *L); + static int32_t card_get_lscale(lua_State *L); + static int32_t card_get_origin_lscale(lua_State *L); + static int32_t card_get_rscale(lua_State *L); + static int32_t card_get_origin_rscale(lua_State *L); + static int32_t card_get_current_scale(lua_State *L); + static int32_t card_is_link_marker(lua_State *L); + static int32_t card_get_linked_group(lua_State *L); + static int32_t card_get_linked_group_count(lua_State *L); + static int32_t card_get_linked_zone(lua_State *L); + static int32_t card_get_mutual_linked_group(lua_State *L); + static int32_t card_get_mutual_linked_group_count(lua_State *L); + static int32_t card_get_mutual_linked_zone(lua_State *L); + static int32_t card_is_link_state(lua_State *L); + static int32_t card_is_extra_link_state(lua_State *L); + static int32_t card_get_column_group(lua_State *L); + static int32_t card_get_column_group_count(lua_State *L); + static int32_t card_get_column_zone(lua_State *L); + static int32_t card_is_all_column(lua_State *L); + static int32_t card_get_attribute(lua_State *L); + static int32_t card_get_origin_attribute(lua_State *L); + static int32_t card_get_fusion_attribute(lua_State *L); + static int32_t card_get_link_attribute(lua_State *L); + static int32_t card_get_attribute_in_grave(lua_State *L); + static int32_t card_get_race(lua_State *L); + static int32_t card_get_origin_race(lua_State *L); + static int32_t card_get_link_race(lua_State *L); + static int32_t card_get_race_in_grave(lua_State *L); + static int32_t card_get_attack(lua_State *L); + static int32_t card_get_origin_attack(lua_State *L); + static int32_t card_get_text_attack(lua_State *L); + static int32_t card_get_defense(lua_State *L); + static int32_t card_get_origin_defense(lua_State *L); + static int32_t card_get_text_defense(lua_State *L); + static int32_t card_get_previous_code_onfield(lua_State *L); + static int32_t card_get_previous_type_onfield(lua_State *L); + static int32_t card_get_previous_level_onfield(lua_State *L); + static int32_t card_get_previous_rank_onfield(lua_State *L); + static int32_t card_get_previous_attribute_onfield(lua_State *L); + static int32_t card_get_previous_race_onfield(lua_State *L); + static int32_t card_get_previous_attack_onfield(lua_State *L); + static int32_t card_get_previous_defense_onfield(lua_State *L); + static int32_t card_get_previous_overlay_count_onfield(lua_State *L); + static int32_t card_get_owner(lua_State *L); + static int32_t card_get_controler(lua_State *L); + static int32_t card_get_previous_controler(lua_State *L); + static int32_t card_set_reason(lua_State *L); + static int32_t card_get_reason(lua_State *L); + static int32_t card_get_reason_card(lua_State *L); + static int32_t card_get_reason_player(lua_State *L); + static int32_t card_get_reason_effect(lua_State *L); + static int32_t card_get_position(lua_State *L); + static int32_t card_get_previous_position(lua_State *L); + static int32_t card_get_battle_position(lua_State *L); + static int32_t card_get_location(lua_State *L); + static int32_t card_get_previous_location(lua_State *L); + static int32_t card_get_sequence(lua_State *L); + static int32_t card_get_previous_sequence(lua_State *L); + static int32_t card_get_summon_type(lua_State *L); + static int32_t card_get_summon_location(lua_State *L); + static int32_t card_get_summon_player(lua_State *L); + static int32_t card_get_destination(lua_State *L); + static int32_t card_get_leave_field_dest(lua_State *L); + static int32_t card_get_turnid(lua_State *L); + static int32_t card_get_fieldid(lua_State *L); + static int32_t card_get_fieldidr(lua_State *L); + static int32_t card_is_origin_code_rule(lua_State *L); + static int32_t card_is_code(lua_State *L); + static int32_t card_is_type(lua_State *L); + static int32_t card_is_all_types(lua_State *L); + static int32_t card_is_fusion_type(lua_State *L); + static int32_t card_is_synchro_type(lua_State *L); + static int32_t card_is_xyz_type(lua_State *L); + static int32_t card_is_link_type(lua_State *L); + static int32_t card_is_level(lua_State *L); + static int32_t card_is_rank(lua_State *L); + static int32_t card_is_link(lua_State *L); + static int32_t card_is_attack(lua_State *L); + static int32_t card_is_defense(lua_State *L); + static int32_t card_is_race(lua_State *L); + static int32_t card_is_link_race(lua_State *L); + static int32_t card_is_attribute(lua_State *L); + static int32_t card_is_fusion_attribute(lua_State *L); + static int32_t card_is_link_attribute(lua_State *L); + static int32_t card_is_non_attribute(lua_State *L); + static int32_t card_is_extra_deck_monster(lua_State *L); + static int32_t card_is_reason(lua_State *L); + static int32_t card_is_all_reasons(lua_State *L); + static int32_t card_is_summon_type(lua_State *L); + static int32_t card_is_summon_location(lua_State *L); + static int32_t card_is_summon_player(lua_State *L); + static int32_t card_get_special_summon_info(lua_State *L); + static int32_t card_is_status(lua_State *L); + static int32_t card_is_not_tuner(lua_State *L); + static int32_t card_is_tuner(lua_State* L); + static int32_t card_is_original_effect_property(lua_State* L); + static int32_t card_is_effect_property(lua_State* L); + static int32_t card_set_status(lua_State *L); + static int32_t card_is_dual_state(lua_State *L); + static int32_t card_enable_dual_state(lua_State *L); + static int32_t card_set_turn_counter(lua_State *L); + static int32_t card_get_turn_counter(lua_State *L); + static int32_t card_set_material(lua_State *L); + static int32_t card_get_material(lua_State *L); + static int32_t card_get_material_count(lua_State *L); + static int32_t card_get_equip_group(lua_State *L); + static int32_t card_get_equip_count(lua_State *L); + static int32_t card_get_equip_target(lua_State *L); + static int32_t card_get_pre_equip_target(lua_State *L); + static int32_t card_check_equip_target(lua_State *L); + static int32_t card_check_union_target(lua_State *L); + static int32_t card_get_union_count(lua_State *L); + static int32_t card_get_overlay_group(lua_State *L); + static int32_t card_get_overlay_count(lua_State *L); + static int32_t card_get_overlay_target(lua_State *L); + static int32_t card_check_remove_overlay_card(lua_State *L); + static int32_t card_remove_overlay_card(lua_State *L); + static int32_t card_get_attacked_group(lua_State *L); + static int32_t card_get_attacked_group_count(lua_State *L); + static int32_t card_get_attacked_count(lua_State *L); + static int32_t card_get_battled_group(lua_State *L); + static int32_t card_get_battled_group_count(lua_State *L); + static int32_t card_get_attack_announced_count(lua_State *L); + static int32_t card_is_direct_attacked(lua_State *L); + static int32_t card_set_card_target(lua_State *L); + static int32_t card_get_card_target(lua_State *L); + static int32_t card_get_first_card_target(lua_State *L); + static int32_t card_get_card_target_count(lua_State *L); + static int32_t card_is_has_card_target(lua_State *L); + static int32_t card_cancel_card_target(lua_State *L); + static int32_t card_get_owner_target(lua_State *L); + static int32_t card_get_owner_target_count(lua_State *L); + static int32_t card_get_activate_effect(lua_State *L); + static int32_t card_check_activate_effect(lua_State *L); + static int32_t card_get_tuner_limit(lua_State * L); + static int32_t card_get_hand_synchro(lua_State * L); + static int32_t card_register_effect(lua_State *L); + static int32_t card_is_has_effect(lua_State *L); + static int32_t card_reset_effect(lua_State *L); + static int32_t card_get_effect_count(lua_State *L); + static int32_t card_register_flag_effect(lua_State *L); + static int32_t card_get_flag_effect(lua_State *L); + static int32_t card_reset_flag_effect(lua_State *L); + static int32_t card_set_flag_effect_label(lua_State *L); + static int32_t card_get_flag_effect_label(lua_State *L); + static int32_t card_create_relation(lua_State *L); + static int32_t card_release_relation(lua_State *L); + static int32_t card_create_effect_relation(lua_State *L); + static int32_t card_release_effect_relation(lua_State *L); + static int32_t card_clear_effect_relation(lua_State *L); + static int32_t card_is_relate_to_effect(lua_State *L); + static int32_t card_is_relate_to_chain(lua_State *L); + static int32_t card_is_relate_to_card(lua_State *L); + static int32_t card_is_relate_to_battle(lua_State *L); + static int32_t card_copy_effect(lua_State *L); + static int32_t card_replace_effect(lua_State *L); + static int32_t card_enable_revive_limit(lua_State *L); + static int32_t card_complete_procedure(lua_State *L); + static int32_t card_is_disabled(lua_State *L); + static int32_t card_is_destructable(lua_State *L); + static int32_t card_is_summonable(lua_State *L); + static int32_t card_is_special_summonable_card(lua_State *L); + static int32_t card_is_fusion_summonable_card(lua_State *L); + static int32_t card_is_msetable(lua_State *L); + static int32_t card_is_ssetable(lua_State *L); + static int32_t card_is_special_summonable(lua_State *L); + static int32_t card_is_synchro_summonable(lua_State *L); + static int32_t card_is_xyz_summonable(lua_State *L); + static int32_t card_is_link_summonable(lua_State *L); + static int32_t card_is_can_be_summoned(lua_State *L); + static int32_t card_is_can_be_special_summoned(lua_State *L); + static int32_t card_is_can_be_placed_on_field(lua_State *L); + static int32_t card_is_able_to_hand(lua_State *L); + static int32_t card_is_able_to_grave(lua_State *L); + static int32_t card_is_able_to_deck(lua_State *L); + static int32_t card_is_able_to_extra(lua_State *L); + static int32_t card_is_able_to_remove(lua_State *L); + static int32_t card_is_able_to_hand_as_cost(lua_State *L); + static int32_t card_is_able_to_grave_as_cost(lua_State *L); + static int32_t card_is_able_to_deck_as_cost(lua_State *L); + static int32_t card_is_able_to_extra_as_cost(lua_State *L); + static int32_t card_is_able_to_deck_or_extra_as_cost(lua_State *L); + static int32_t card_is_able_to_remove_as_cost(lua_State *L); + static int32_t card_is_able_to_decrease_attack_as_cost(lua_State *L); + static int32_t card_is_able_to_decrease_defense_as_cost(lua_State *L); + static int32_t card_is_releasable(lua_State *L); + static int32_t card_is_releasable_by_effect(lua_State *L); + static int32_t card_is_discardable(lua_State *L); + static int32_t card_is_attackable(lua_State *L); + static int32_t card_is_chain_attackable(lua_State *L); + static int32_t card_is_faceup(lua_State *L); + static int32_t card_is_faceup_ex(lua_State *L); + static int32_t card_is_attack_pos(lua_State *L); + static int32_t card_is_facedown(lua_State *L); + static int32_t card_is_defense_pos(lua_State *L); + static int32_t card_is_position(lua_State *L); + static int32_t card_is_pre_position(lua_State *L); + static int32_t card_is_controler(lua_State *L); + static int32_t card_is_pre_controler(lua_State *L); + static int32_t card_is_onfield(lua_State *L); + static int32_t card_is_location(lua_State *L); + static int32_t card_is_pre_location(lua_State *L); + static int32_t card_is_level_below(lua_State *L); + static int32_t card_is_level_above(lua_State *L); + static int32_t card_is_rank_below(lua_State *L); + static int32_t card_is_rank_above(lua_State *L); + static int32_t card_is_link_below(lua_State *L); + static int32_t card_is_link_above(lua_State *L); + static int32_t card_is_attack_below(lua_State *L); + static int32_t card_is_attack_above(lua_State *L); + static int32_t card_is_defense_below(lua_State *L); + static int32_t card_is_defense_above(lua_State *L); + static int32_t card_is_has_level(lua_State *L); + static int32_t card_is_has_defense(lua_State *L); + static int32_t card_is_public(lua_State *L); + static int32_t card_is_forbidden(lua_State *L); + static int32_t card_is_able_to_change_controler(lua_State *L); + static int32_t card_is_controler_can_be_changed(lua_State *L); + static int32_t card_add_counter(lua_State *L); + static int32_t card_remove_counter(lua_State *L); + static int32_t card_get_counter(lua_State *L); + static int32_t card_enable_counter_permit(lua_State *L); + static int32_t card_set_counter_limit(lua_State *L); + static int32_t card_is_can_change_position(lua_State *L); + static int32_t card_is_can_turn_set(lua_State *L); + static int32_t card_is_can_add_counter(lua_State *L); + static int32_t card_is_can_remove_counter(lua_State *L); + static int32_t card_is_can_have_counter(lua_State* L); + static int32_t card_is_can_overlay(lua_State *L); + static int32_t card_is_can_be_fusion_material(lua_State *L); + static int32_t card_is_can_be_synchro_material(lua_State *L); + static int32_t card_is_can_be_ritual_material(lua_State *L); + static int32_t card_is_can_be_xyz_material(lua_State *L); + static int32_t card_is_can_be_link_material(lua_State *L); + static int32_t card_check_fusion_material(lua_State *L); + static int32_t card_check_fusion_substitute(lua_State *L); + static int32_t card_is_immune_to_effect(lua_State *L); + static int32_t card_is_can_be_disabled_by_effect(lua_State* L); + static int32_t card_is_can_be_effect_target(lua_State *L); + static int32_t card_is_can_be_battle_target(lua_State *L); + static int32_t card_add_monster_attribute(lua_State *L); + static int32_t card_cancel_to_grave(lua_State *L); + static int32_t card_get_tribute_requirement(lua_State *L); + static int32_t card_get_battle_target(lua_State *L); + static int32_t card_get_attackable_target(lua_State *L); + static int32_t card_set_hint(lua_State *L); + static int32_t card_reverse_in_deck(lua_State *L); + static int32_t card_set_unique_onfield(lua_State *L); + static int32_t card_check_unique_onfield(lua_State *L); + static int32_t card_reset_negate_effect(lua_State *L); + static int32_t card_assume_prop(lua_State *L); + static int32_t card_set_spsummon_once(lua_State *L); + static void open_cardlib(lua_State *L); + + //Effect functions + static int32_t get_effect_property(lua_State* L, effect_member type); + static int32_t is_effect_has_property(lua_State* L, effect_member type); + static int32_t effect_new(lua_State *L); + static int32_t effect_newex(lua_State *L); + static int32_t effect_clone(lua_State *L); + static int32_t effect_reset(lua_State *L); + static int32_t effect_get_field_id(lua_State *L); + static int32_t effect_set_description(lua_State *L); + static int32_t effect_set_code(lua_State *L); + static int32_t effect_set_range(lua_State *L); + static int32_t effect_set_target_range(lua_State *L); + static int32_t effect_set_absolute_range(lua_State *L); + static int32_t effect_set_count_limit(lua_State *L); + static int32_t effect_set_reset(lua_State *L); + static int32_t effect_set_type(lua_State *L); + static int32_t effect_set_property(lua_State *L); + static int32_t effect_set_label(lua_State *L); + static int32_t effect_set_label_object(lua_State *L); + static int32_t effect_set_category(lua_State *L); + static int32_t effect_set_hint_timing(lua_State *L); + static int32_t effect_set_condition(lua_State *L); + static int32_t effect_set_target(lua_State *L); + static int32_t effect_set_cost(lua_State *L); + static int32_t effect_set_value(lua_State *L); + static int32_t effect_set_operation(lua_State *L); + static int32_t effect_set_owner_player(lua_State *L); + static int32_t effect_get_description(lua_State *L); + static int32_t effect_get_code(lua_State *L); + static int32_t effect_get_type(lua_State *L); + static int32_t effect_get_property(lua_State *L); + static int32_t effect_get_label(lua_State *L); + static int32_t effect_get_label_object(lua_State *L); + static int32_t effect_get_category(lua_State *L); + static int32_t effect_get_range(lua_State* L); + static int32_t effect_get_owner(lua_State *L); + static int32_t effect_get_handler(lua_State *L); + static int32_t effect_get_owner_player(lua_State *L); + static int32_t effect_get_handler_player(lua_State *L); + static int32_t effect_get_condition(lua_State *L); + static int32_t effect_get_target(lua_State *L); + static int32_t effect_get_cost(lua_State *L); + static int32_t effect_get_value(lua_State *L); + static int32_t effect_get_operation(lua_State *L); + static int32_t effect_get_active_type(lua_State *L); + static int32_t effect_is_active_type(lua_State *L); + static int32_t effect_is_has_property(lua_State *L); + static int32_t effect_is_has_category(lua_State *L); + static int32_t effect_is_has_range(lua_State* L); + static int32_t effect_is_has_type(lua_State *L); + static int32_t effect_is_activatable(lua_State *L); + static int32_t effect_is_activated(lua_State *L); + static int32_t effect_is_cost_checked(lua_State *L); + static int32_t effect_set_cost_check(lua_State *L); + static int32_t effect_get_activate_location(lua_State *L); + static int32_t effect_get_activate_sequence(lua_State *L); + static int32_t effect_check_count_limit(lua_State *L); + static int32_t effect_use_count_limit(lua_State *L); + static void open_effectlib(lua_State *L); + + //Group functions + static int32_t group_new(lua_State *L); + static int32_t group_clone(lua_State *L); + static int32_t group_from_cards(lua_State *L); + static int32_t group_delete(lua_State *L); + static int32_t group_keep_alive(lua_State *L); + static int32_t group_clear(lua_State *L); + static int32_t group_add_card(lua_State *L); + static int32_t group_remove_card(lua_State *L); + static int32_t group_get_next(lua_State *L); + static int32_t group_get_first(lua_State *L); + static int32_t group_get_count(lua_State *L); + static int32_t group_filter(lua_State *L); + static int32_t group_filter_count(lua_State *L); + static int32_t group_filter_select(lua_State *L); + static int32_t group_select(lua_State *L); + static int32_t group_select_unselect(lua_State *L); + static int32_t group_random_select(lua_State *L); + static int32_t group_cancelable_select(lua_State *L); + static int32_t group_is_exists(lua_State *L); + static int32_t group_check_with_sum_equal(lua_State *L); + static int32_t group_select_with_sum_equal(lua_State *L); + static int32_t group_check_with_sum_greater(lua_State *L); + static int32_t group_select_with_sum_greater(lua_State *L); + static int32_t group_get_min_group(lua_State *L); + static int32_t group_get_max_group(lua_State *L); + static int32_t group_get_sum(lua_State *L); + static int32_t group_get_class_count(lua_State *L); + static int32_t group_remove(lua_State *L); + static int32_t group_equal(lua_State *L); + static int32_t group_is_contains(lua_State *L); + static int32_t group_search_card(lua_State *L); + static int32_t group_get_bin_class_count(lua_State *L); + static void open_grouplib(lua_State *L); + + //Duel functions + static int32_t duel_enable_global_flag(lua_State *L); + static int32_t duel_get_lp(lua_State *L); + static int32_t duel_set_lp(lua_State *L); + static int32_t duel_is_turn_player(lua_State *L); + static int32_t duel_get_turn_player(lua_State *L); + static int32_t duel_get_turn_count(lua_State *L); + static int32_t duel_get_draw_count(lua_State *L); + static int32_t duel_register_effect(lua_State *L); + static int32_t duel_register_flag_effect(lua_State *L); + static int32_t duel_get_flag_effect(lua_State *L); + static int32_t duel_reset_flag_effect(lua_State *L); + static int32_t duel_set_flag_effect_label(lua_State *L); + static int32_t duel_get_flag_effect_label(lua_State *L); + static int32_t duel_destroy(lua_State *L); + static int32_t duel_remove(lua_State *L); + static int32_t duel_sendto_grave(lua_State *L); + static int32_t duel_summon(lua_State *L); + static int32_t duel_special_summon_rule(lua_State *L); + static int32_t duel_synchro_summon(lua_State *L); + static int32_t duel_xyz_summon(lua_State *L); + static int32_t duel_link_summon(lua_State *L); + static int32_t duel_setm(lua_State *L); + static int32_t duel_sets(lua_State *L); + static int32_t duel_create_token(lua_State *L); + static int32_t duel_special_summon(lua_State *L); + static int32_t duel_special_summon_step(lua_State *L); + static int32_t duel_special_summon_complete(lua_State *L); + static int32_t duel_sendto_hand(lua_State *L); + static int32_t duel_sendto_deck(lua_State *L); + static int32_t duel_sendto_extra(lua_State *L); + static int32_t duel_get_operated_group(lua_State *L); + static int32_t duel_is_can_add_counter(lua_State *L); + static int32_t duel_remove_counter(lua_State *L); + static int32_t duel_is_can_remove_counter(lua_State *L); + static int32_t duel_get_counter(lua_State *L); + static int32_t duel_change_form(lua_State *L); + static int32_t duel_release(lua_State *L); + static int32_t duel_move_to_field(lua_State *L); + static int32_t duel_return_to_field(lua_State *L); + static int32_t duel_move_sequence(lua_State *L); + static int32_t duel_swap_sequence(lua_State *L); + static int32_t duel_activate_effect(lua_State *L); + static int32_t duel_set_chain_limit(lua_State *L); + static int32_t duel_set_chain_limit_p(lua_State *L); + static int32_t duel_get_chain_material(lua_State *L); + static int32_t duel_confirm_decktop(lua_State *L); + static int32_t duel_confirm_extratop(lua_State *L); + static int32_t duel_confirm_cards(lua_State *L); + static int32_t duel_sort_decktop(lua_State *L); + static int32_t duel_check_event(lua_State *L); + static int32_t duel_raise_event(lua_State *L); + static int32_t duel_raise_single_event(lua_State *L); + static int32_t duel_check_timing(lua_State *L); + static int32_t duel_is_environment(lua_State *L); + + static int32_t duel_win(lua_State *L); + static int32_t duel_draw(lua_State *L); + static int32_t duel_damage(lua_State *L); + static int32_t duel_recover(lua_State *L); + static int32_t duel_rd_complete(lua_State *L); + static int32_t duel_equip(lua_State *L); + static int32_t duel_equip_complete(lua_State *L); + static int32_t duel_get_control(lua_State *L); + static int32_t duel_swap_control(lua_State *L); + static int32_t duel_check_lp_cost(lua_State *L); + static int32_t duel_pay_lp_cost(lua_State *L); + static int32_t duel_discard_deck(lua_State *L); + static int32_t duel_discard_hand(lua_State *L); + static int32_t duel_disable_shuffle_check(lua_State *L); + static int32_t duel_disable_self_destroy_check(lua_State *L); + static int32_t duel_reveal_select_deck_sequence(lua_State *L); + static int32_t duel_shuffle_deck(lua_State *L); + static int32_t duel_shuffle_extra(lua_State *L); + static int32_t duel_shuffle_hand(lua_State *L); + static int32_t duel_shuffle_setcard(lua_State *L); + static int32_t duel_change_attacker(lua_State *L); + static int32_t duel_change_attack_target(lua_State *L); + static int32_t duel_calculate_damage(lua_State *L); + static int32_t duel_get_battle_damage(lua_State *L); + static int32_t duel_change_battle_damage(lua_State *L); + static int32_t duel_change_target(lua_State *L); + static int32_t duel_change_target_player(lua_State *L); + static int32_t duel_change_target_param(lua_State *L); + static int32_t duel_break_effect(lua_State *L); + static int32_t duel_change_effect(lua_State *L); + static int32_t duel_negate_activate(lua_State *L); + static int32_t duel_negate_effect(lua_State *L); + static int32_t duel_negate_related_chain(lua_State *L); + static int32_t duel_disable_summon(lua_State *L); + static int32_t duel_increase_summon_count(lua_State *L); + static int32_t duel_check_summon_count(lua_State *L); + static int32_t duel_get_location_count(lua_State *L); + static int32_t duel_get_mzone_count(lua_State *L); + static int32_t duel_get_szone_count(lua_State *L); + static int32_t duel_get_location_count_fromex(lua_State *L); + static int32_t duel_get_usable_mzone_count(lua_State *L); + static int32_t duel_get_linked_group(lua_State *L); + static int32_t duel_get_linked_group_count(lua_State *L); + static int32_t duel_get_linked_zone(lua_State *L); + static int32_t duel_get_field_card(lua_State *L); + static int32_t duel_check_location(lua_State *L); + static int32_t duel_get_current_chain(lua_State *L); + static int32_t duel_get_ready_chain(lua_State* L); + static int32_t duel_get_chain_info(lua_State *L); + static int32_t duel_get_chain_event(lua_State *L); + static int32_t duel_get_first_target(lua_State *L); + static int32_t duel_get_targets_relate_to_chain(lua_State *L); + static int32_t duel_is_phase(lua_State *L); + static int32_t duel_is_main_phase(lua_State *L); + static int32_t duel_is_battle_phase(lua_State *L); + static int32_t duel_get_current_phase(lua_State *L); + static int32_t duel_skip_phase(lua_State *L); + static int32_t duel_is_damage_calculated(lua_State *L); + static int32_t duel_get_attacker(lua_State *L); + static int32_t duel_get_attack_target(lua_State* L); + static int32_t duel_get_battle_monster(lua_State* L); + static int32_t duel_disable_attack(lua_State *L); + static int32_t duel_chain_attack(lua_State *L); + static int32_t duel_readjust(lua_State *L); + static int32_t duel_adjust_instantly(lua_State *L); + static int32_t duel_adjust_all(lua_State *L); + + static int32_t duel_get_field_group(lua_State *L); + static int32_t duel_get_field_group_count(lua_State *L); + static int32_t duel_get_decktop_group(lua_State *L); + static int32_t duel_get_extratop_group(lua_State *L); + static int32_t duel_get_matching_group(lua_State *L); + static int32_t duel_get_matching_count(lua_State *L); + static int32_t duel_get_first_matching_card(lua_State *L); + static int32_t duel_is_existing_matching_card(lua_State *L); + static int32_t duel_select_matching_cards(lua_State *L); + static int32_t duel_get_release_group(lua_State *L); + static int32_t duel_get_release_group_count(lua_State *L); + static int32_t duel_check_release_group(lua_State *L); + static int32_t duel_select_release_group(lua_State *L); + static int32_t duel_check_release_group_ex(lua_State *L); + static int32_t duel_select_release_group_ex(lua_State *L); + static int32_t duel_get_tribute_group(lua_State *L); + static int32_t duel_get_tribute_count(lua_State *L); + static int32_t duel_check_tribute(lua_State *L); + static int32_t duel_select_tribute(lua_State *L); + static int32_t duel_get_target_count(lua_State *L); + static int32_t duel_is_existing_target(lua_State *L); + static int32_t duel_select_target(lua_State *L); + static int32_t duel_get_must_material(lua_State *L); + static int32_t duel_check_must_material(lua_State *L); + static int32_t duel_select_fusion_material(lua_State *L); + static int32_t duel_set_fusion_material(lua_State *L); + static int32_t duel_set_synchro_material(lua_State *L); + static int32_t duel_get_synchro_material(lua_State *L); + static int32_t duel_select_synchro_material(lua_State *L); + static int32_t duel_check_synchro_material(lua_State *L); + static int32_t duel_select_tuner_material(lua_State *L); + static int32_t duel_check_tuner_material(lua_State *L); + static int32_t duel_get_ritual_material(lua_State *L); + static int32_t duel_get_ritual_material_ex(lua_State *L); + static int32_t duel_release_ritual_material(lua_State *L); + static int32_t duel_get_fusion_material(lua_State *L); + static int32_t duel_is_summon_cancelable(lua_State *L); + static int32_t duel_set_must_select_cards(lua_State *L); + static int32_t duel_grab_must_select_cards(lua_State *L); + static int32_t duel_set_target_card(lua_State *L); + static int32_t duel_clear_target_card(lua_State *L); + static int32_t duel_set_target_player(lua_State *L); + static int32_t duel_set_target_param(lua_State *L); + static int32_t duel_set_operation_info(lua_State *L); + static int32_t duel_get_operation_info(lua_State *L); + static int32_t duel_get_operation_count(lua_State *L); + static int32_t duel_clear_operation_info(lua_State *L); + static int32_t duel_check_xyz_material(lua_State *L); + static int32_t duel_select_xyz_material(lua_State *L); + static int32_t duel_overlay(lua_State *L); + static int32_t duel_get_overlay_group(lua_State *L); + static int32_t duel_get_overlay_count(lua_State *L); + static int32_t duel_check_remove_overlay_card(lua_State *L); + static int32_t duel_remove_overlay_card(lua_State *L); + static int32_t duel_get_disable_field(lua_State *L); + + static int32_t duel_hint(lua_State *L); + static int32_t duel_get_last_select_hint(lua_State *L); + static int32_t duel_hint_selection(lua_State *L); + static int32_t duel_select_effect_yesno(lua_State *L); + static int32_t duel_select_yesno(lua_State *L); + static int32_t duel_select_option(lua_State *L); + static int32_t duel_select_sequence(lua_State *L); + static int32_t duel_select_position(lua_State *L); + static int32_t duel_select_disable_field(lua_State *L); + static int32_t duel_select_field(lua_State *L); + static int32_t duel_announce_race(lua_State *L); + static int32_t duel_announce_attribute(lua_State *L); + static int32_t duel_announce_level(lua_State *L); + static int32_t duel_announce_card(lua_State *L); + static int32_t duel_announce_card_silent(lua_State *L); + static int32_t duel_announce_type(lua_State *L); + static int32_t duel_announce_number(lua_State *L); + static int32_t duel_announce_coin(lua_State *L); + static int32_t duel_toss_coin(lua_State *L); + static int32_t duel_toss_dice(lua_State *L); + static int32_t duel_rock_paper_scissors(lua_State *L); + static int32_t duel_get_coin_result(lua_State *L); + static int32_t duel_get_dice_result(lua_State *L); + static int32_t duel_set_coin_result(lua_State *L); + static int32_t duel_set_dice_result(lua_State *L); + + static int32_t duel_is_player_affected_by_effect(lua_State *L); + static int32_t duel_is_player_can_draw(lua_State *L); + static int32_t duel_is_player_can_discard_deck(lua_State *L); + static int32_t duel_is_player_can_discard_deck_as_cost(lua_State *L); + static int32_t duel_is_player_can_summon(lua_State *L); + static int32_t duel_is_player_can_mset(lua_State *L); + static int32_t duel_is_player_can_sset(lua_State *L); + static int32_t duel_is_player_can_spsummon(lua_State *L); + static int32_t duel_is_player_can_flipsummon(lua_State *L); + static int32_t duel_is_player_can_spsummon_monster(lua_State *L); + static int32_t duel_is_player_can_spsummon_count(lua_State *L); + static int32_t duel_is_player_can_release(lua_State *L); + static int32_t duel_is_player_can_remove(lua_State *L); + static int32_t duel_is_player_can_send_to_hand(lua_State *L); + static int32_t duel_is_player_can_send_to_grave(lua_State *L); + static int32_t duel_is_player_can_send_to_deck(lua_State *L); + static int32_t duel_is_player_can_additional_summon(lua_State *L); + static int32_t duel_is_chain_solving(lua_State *L); + static int32_t duel_is_chain_negatable(lua_State *L); + static int32_t duel_is_chain_disablable(lua_State *L); + static int32_t duel_is_chain_disabled(lua_State *L); + static int32_t duel_check_chain_target(lua_State *L); + static int32_t duel_check_chain_uniqueness(lua_State *L); + static int32_t duel_get_activity_count(lua_State *L); + static int32_t duel_check_phase_activity(lua_State *L); + static int32_t duel_add_custom_activity_counter(lua_State *L); + static int32_t duel_get_custom_activity_count(lua_State *L); + static int32_t duel_is_able_to_enter_bp(lua_State *L); + static int32_t duel_get_battled_count(lua_State *L); + + //specific card functions + static int32_t duel_swap_deck_and_grave(lua_State *L); + static int32_t duel_majestic_copy(lua_State *L); + + static void open_duellib(lua_State *L); + + //group metamethods + //__len is in the group lib, which is same as group_get_count + static int32_t group_meta_add(lua_State *L); + static int32_t group_meta_sub(lua_State *L); + static int32_t group_meta_band(lua_State *L); + static int32_t group_meta_bxor(lua_State *L); + + //meta-related methods + static int32_t duel_get_meta_value(lua_State *L); + static int32_t duel_set_meta_value(lua_State *L); + static int32_t duel_has_meta_value(lua_State *L); + static int32_t duel_get_meta_keys(lua_State *L); + static int32_t duel_clear_meta(lua_State *L); + static int32_t duel_get_chain_meta_value(lua_State *L); + static int32_t duel_set_chain_meta_value(lua_State *L); + static int32_t duel_has_chain_meta_value(lua_State *L); + static int32_t duel_get_chain_meta_keys(lua_State *L); + static int32_t duel_clear_chain_meta(lua_State *L); + static int32_t card_get_meta_value(lua_State *L); + static int32_t card_set_meta_value(lua_State *L); + static int32_t card_has_meta_value(lua_State *L); + static int32_t card_get_meta_keys(lua_State *L); + static int32_t card_clear_meta(lua_State *L); + static int32_t group_get_meta_value(lua_State *L); + static int32_t group_set_meta_value(lua_State *L); + static int32_t group_has_meta_value(lua_State *L); + static int32_t group_get_meta_keys(lua_State *L); + static int32_t group_clear_meta(lua_State *L); + static int32_t effect_get_meta_value(lua_State *L); + static int32_t effect_set_meta_value(lua_State *L); + static int32_t effect_has_meta_value(lua_State *L); + static int32_t effect_get_meta_keys(lua_State *L); + static int32_t effect_clear_meta(lua_State *L); + + //preload + static int32_t debug_message(lua_State *L); + static int32_t debug_add_card(lua_State *L); + static int32_t debug_set_player_info(lua_State *L); + static int32_t debug_pre_summon(lua_State *L); + static int32_t debug_pre_equip(lua_State *L); + static int32_t debug_pre_set_target(lua_State *L); + static int32_t debug_pre_add_counter(lua_State *L); + static int32_t debug_reload_field_begin(lua_State *L); + static int32_t debug_reload_field_end(lua_State *L); + static int32_t debug_set_ai_name(lua_State *L); + static int32_t debug_show_hint(lua_State *L); + static void open_debuglib(lua_State *L); +}; + +#endif /* SCRIPTLIB_H_ */ diff --git a/core/sort.h b/core/sort.h new file mode 100644 index 0000000..48ca01d --- /dev/null +++ b/core/sort.h @@ -0,0 +1,10 @@ +#ifndef SORT_H_ +#define SORT_H_ + +class card; + +struct card_sort { + bool operator()(card* const& c1, card* const& c2) const; +}; + +#endif /* SORT_H_ */