Mattsuharu1991

Card Game Script

Nov 29th, 2025 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 261.76 KB | Source Code | 0 0
  1. ##=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. #                           CARDGAME SKILL/BATTLE SYSTEM
  3. #                               Author: pencilcase27
  4. #                               Version:       v1.09
  5. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  6. # v1.00 - Script released
  7. # v1.01 - Added stack, skills now can't revive themself.
  8. #       - Added CardHelp_Window, normal help window is no longer used to draw cards
  9. #       - Item Window is back to normal
  10. # v1.02 - reworked card effects
  11. #       - added deck count and toggle actor to deck build scene
  12. #       - simplified scriptcalls to add and remove cards from collection
  13. #       - Added max_card limit exception
  14. # v1.03 - reworked card effects (again)
  15. #       - added card cost effects
  16. #       - added custom images
  17. # v1.04 - added more script calls
  18. #       - changed card costs slightly
  19. #       - bugfixes
  20. # v1.05 - bugfixes
  21. # v1.06 - added initial decks for actors
  22. # v1.07 - fixed a bug with item canceling
  23. #       - fixed a bug with the draw card effect
  24. # v1.08 - added Attack and Guard command
  25. #       - added two notetags for equip
  26. # v1.09 - fixed an issue with attack and guard
  27. #       - added limitations on how many cards can be played each turn
  28. #       - added a feature to instantly empty your deck
  29. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  30. #
  31. #                                  Description
  32. #
  33. # This script changes the way actors aquire and use skills. Instead of each
  34. # actor having an own set of skills, the party now has a card collection of
  35. # different cards, each card representing a skill. Out of this collection, each
  36. # actor can now create a custom deck.
  37. # In battle, actors will act in turns. They draw cards and, while an actor is
  38. # active, he can play as many cards and items as he wants, as long as he has
  39. # the recources to do so. Once he is done, he can end his turn and the next
  40. # actor becomes active. When every actor finished his or her turn, the enemys
  41. # use their skills normal.
  42. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  43. #
  44. #                                  Script Calls
  45. #
  46. # add_card_to_coll(id1, id2, id3, ...)
  47. # Adds cards of the given skill ids to your collection
  48. #
  49. # remove_card?(id)
  50. # Tries to remove a single card of the given skill id from your collection.
  51. # Returns true if successfull and false otherwise.
  52. #
  53. # remove_all_cards(id1, id2, ...)
  54. # Removes all cards of the given skill ids from both the players decks and
  55. # from the collection
  56. #
  57. # has_card?(id)
  58. # Checks if the player has a specific card in his collection or in his deck
  59. #
  60. # has_card_in_deck?(id)
  61. # Checks if the player has a specific card in his deck
  62. #
  63. # has_card_in_grave?(id)
  64. # Checks if the player has a specific card in his graveyard
  65. #
  66. # has_x_cards_in_grave?(id, count)
  67. # Checks if the player has a specific cards in his graveyard
  68. #
  69. # has_card_in_banish?(id)
  70. # Checks if the player has a specific card in his banish
  71. #
  72. # has_x_cards_in_banish?(id, count)
  73. # Checks if the player has a specific cards in his banish
  74. #
  75. # has_card_in_hand?(id)
  76. # Checks if the player has a specific card in his hand
  77. #
  78. # add_card_deck(id1, id2, id3, ...)
  79. # Adds cards of the given skill ids to the designed actor deck
  80. #
  81. # remove_card_deck(id1, id2, id3, ...)
  82. # Remove cards of the given skill ids to the designed actor deck
  83. #
  84. # add_card_hand(id1, id2, id3, ...)
  85. # Adds cards of the given skill ids to the designed actor hand
  86. #
  87. # remove_card_hand(id1, id2, id3, ...)
  88. # Remove cards of the given skill ids to the designed actor hand
  89. #
  90. # count_actor_deck(id)
  91. # Returns the amount of cards in this actors deck
  92. #
  93. # count_all_decks
  94. # Returns the combined deck size of all actors
  95. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  96. #
  97. #                                Skill Notetags
  98. #
  99. #           <card id: x>
  100. # The card id of the skill. Each card must have a unique id.
  101. # Important: Every time this script uses the word id, it refers to the skills
  102. # id, not the card id! The card id is only used to sort cards.
  103. #
  104. #           <card type: x>
  105. # The type of the card. Different types can be defined below.
  106. #
  107. #           <card rarity: x>
  108. # The rarity of the card. Different raritys can be defined below.
  109. #
  110. #           <card cost: effect_name>
  111. # Adds the given cost effect to the card. Please read about card effects
  112. # down below.
  113. #
  114. #           <card impact: effect_name>
  115. # Adds the given impact effect to the card. Please read about card effects
  116. # down below.
  117. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  118. #
  119. #                             Armor/Weapon Notetags
  120. #
  121. #           <extra start cards: x>
  122. # Draws x extra cards at the begin of each battle
  123. #
  124. #           <extra turn cards: x>
  125. # Draws x extra cards at the begin of each turn
  126. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  127. #
  128. #                                Damage Formula
  129. #
  130. # Those commands can be used for a skills damage formula. Make sure they
  131. # always target an actor, since they won't have any effect on enemys. Minor
  132. # scripting knowlegde is required to make the most out of those methods.
  133. #
  134. # a/b.revive_grave(true/false)  
  135. # Shuffles your grave in your deck without penalty. If the argumet is true,
  136. # a message will be shown.
  137. #
  138. # a/b.revive_penalty
  139. # Causes the revive penalty to happen.
  140. #
  141. # a/b.shuffle_deck(true/false)
  142. # Shuffles the actors deck. If the argument is true, a sound will be played.
  143. #
  144. # a/b.card_to_grave?(skill_id)
  145. # Tries to throw the skill with the given ID to the grave. Returns true, if it's
  146. # successfull and false otherwise.
  147. #
  148. # a/b.throw_cards(n)
  149. # Throws n random cards to the grave. If n = 0, the actor will loose all cards.
  150. # Returns how many cards were thrown away.
  151. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  152. #
  153. #                                Card Effects
  154. #
  155. # Card effects are special, card related effects like "draw x cards" or
  156. # "discard a fire spell to play this card" or "look at the top 5 cards of your
  157. # deck and put them back in any order". Every card effect can be played both as
  158. # a card cost or as a card impact. Costs are handled before the player plays the
  159. # cards, impacts after.
  160. # To set up a card effect, scroll to the end of Options where you will find an
  161. # option called CARD_EFFECT. There you can add a effect name and the effects
  162. # arguments. Then add the effect either as cost or as impact via note tag to
  163. # a skill.
  164. # Each skill can have one cost effect and one impact effect.
  165. # Each item can have one impact effect.
  166. # WARNING: This is not trivial. Read carefully!
  167. #
  168. # Type: Draw
  169. # Simply draws cards
  170. # "effect_name" => [:draw, a, b, c]
  171. # a -> how many cards do you want to draw?
  172. # b -> chance the effect is successfull (between 0 and 100)
  173. # c -> :self to make only the user draw a card, :target to apply the effect to
  174. #      each target.
  175. # Example:
  176. # "draw2" => [:draw, 2, 100, :self]
  177. # -> The user of this skill will draw 2 cards.
  178. #
  179. # Type: View (impact only)
  180. # Looks at a certain number of cards taken from location a, and then puts them
  181. # back to location b. Locations can be :deck, :hand and :grave.
  182. # "effect_name" => [ :view, a, b, c]
  183. # a -> how many cards do you want to view? 0 for the entire location.
  184. # b -> from where do you want to take those cards?
  185. # c -> where do you want to put them when you're done?
  186. # Example:
  187. # "view_effect" => [:view, 10, :deck, :deck]
  188. # -> look at the top 10 cards of your deck.
  189. #
  190. # Type: Sort
  191. # Same as view but allows to sort the cards.
  192. # Example:
  193. # "stupid_effect" => [:sort, 0, :hand, :deck]
  194. # -> Put your entire hand ontop of your deck in any order.
  195. #
  196. # Type: Take
  197. # This one is a little bit harder. It allows to take some of the viewed cards
  198. # from one location to another.
  199. # "effect_name" => [:take, a, b, c, d, e, f]
  200. # a, b, c -> same as above
  201. # d -> filter that determines which cards can be choosen,
  202. #      [element_id, type_id, rarity_id], 0 for all id's allowed.
  203. # e -> the location to put the choosen cards.
  204. # f -> how many cards can be choosen.
  205. # Example:
  206. # "search_for_element2" => [:take, 0, :deck, :deck, [2,0,0], :hand, 1]
  207. # -> Search your deck for one card of element 2 and take it to your hand.
  208. # Another Example:
  209. # "discard_cost" => [:take, 0, :hand, :hand, [0,0,0], :grave, 2]
  210. # -> If this is added as a cost effect, the player has to discard two cards
  211. #    from his or her hand to play this card.
  212. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  213. module PC27
  214.   module CG
  215. #-------------------------------------------------------------------------------
  216. #
  217. #                                   Options
  218. #
  219. #-------------------------------------------------------------------------------
  220.  
  221.  
  222.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  223.  #                                   Vocab
  224.  #------------------------------------------------------------------------------
  225.  # DECK             - Name of the actors Deck
  226.  # GRAVE            - Name of the actors Grave
  227.  # COLLECTION       - Name of the Card Collection
  228.  # END_TURN         - Name of the command that will end a players turn
  229.  # DRAW_MESSAGE     - Text that appears when an actor draws a card. Displayed
  230.  #                    as "Actor Name" + DRAW_MESSAGE + "Skill Name"
  231.  # REVIVE_MSG       - Text that appears when you revive your grave
  232.  # CARD_HELP_WINDOW - Help text shown in the deck scene (Use \n to break line)
  233.  # FILTER_TEXT      - First part of the text inside of the filter window
  234.  # F_...            - Second part of the text inside of the filter window
  235.  # EMPTY_DECK       - Text in filter window that allows to empty the entire deck
  236.  # CONFIRM          - Confirmation for EMPTY_DECK
  237.  # CARD_EFF_TEXT    - Text displayed when a card effect is evaluated.
  238.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  239.   DECK              = "Deck"
  240.   GRAVE             = "Grave"
  241.   BANISH            = "Banish"
  242.   EXTRA             = "Extra"
  243.   FIELD             = "Field"
  244.   COLLECTION        = "Collection"
  245.   END_TURN          = "End"
  246.   DRAW_MESSAGE      = "draws:"
  247.   REVIVE_MSG        = "'s deck was restored!"
  248.   CARD_HELP_WINDOW  = "(Z): Move card\n(A): Open filter\n(Shift): Change between\nMain/Extra Deck"
  249.   FILTER_TEXT       = "Filter by "
  250.   F_ELEMENT         = "Attribute: "
  251.   F_TYPE            = "Type:    "
  252.   F_RARITY          = "Rarity:  "
  253.   F_SUBTYPE         = "Sub Type:  "
  254.   F_LEVEL           = "Level:  "
  255.   EMPTY_DECK        = "Clean Deck"
  256.   CONFIRM           = "Are you sure?"
  257.   CARD_EFF_TEXT     = "Choose the cards:"
  258.  
  259.  
  260.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  261.  #                                  Gameplay
  262.  #------------------------------------------------------------------------------
  263.  # USE_ATTACK       - Enables the regular attack command in battle.
  264.  # USE_GUARD        - Enables the regular guard command in battle.
  265.  # USE_ITEMS        - Are items in battle enabeld?
  266.  # START_CARDS      - Cards drawn in Turn 1
  267.  # MAX_DECK_SIZE    - Maximum size of a players deck
  268.  # MAX_SAME_CARDS   - Maximum amount of same cards in each deck
  269.  # MAX_ACTIONS      - Maximum amount of cards an actor can use each turn
  270.  # DECK_EMPTY       - What happens if an actor tries to draw a card but has an
  271.  #                    empty deck?
  272.  #                    1 -> The actor dies
  273.  #                    2 -> The actor dies if his grave is empty, too. If it
  274.  #                         isn't, he revives his grave, meaning he shuffles his
  275.  #                         grave back into his deck. Revive penalty is applied
  276.  #                    3 -> Like 2, but the actor won't die if the grave is empty
  277.  # REVIVE_PENALTY   - The revive penalty. Types are :hp, :mhp, :mp and :mmp.
  278.  #                    The actor will lose either hp or mp, based on its current
  279.  #                    or maximum value
  280.  # MAX_SAME_EXCEP   - Exceptions to MAX_SAME_CARDS
  281.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  282.   USE_ATTACK        = true
  283.   USE_GUARD         = true
  284.   USE_ITEMS         = true
  285.   START_CARDS       = 5
  286.   MONSTER_ZONE_COUNT = 3
  287.   SPELL_TRAP_ZONE_COUNT = 3
  288.   MAX_DECK_SIZE     = 60
  289.   MIN_DECK_SIZE     = 39
  290.   MAX_EXTRA_SIZE    = 15
  291.   MAX_SAME_CARDS    = 3
  292.   MAX_ACTIONS       = 0  # if this is 0, an actor can play infinite cards
  293.                          # (until he's out of mana or cards)
  294.   DECK_EMPTY        = 1
  295.  
  296.  #                    [percent, type]
  297.   REVIVE_PENALTY    = [     50,  :hp]
  298.  
  299.   MAX_SAME_EXCEP    = {
  300.  #                    skill id => max. amount,
  301.                             67 => 1,
  302.                             45 => 1,
  303.                             #7 => 1,
  304.   } # do not remove this
  305.  
  306.  
  307.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  308.  #                              Icons and Sound
  309.  #------------------------------------------------------------------------------
  310.  # DRAW_SOUND       - Sound that will play when you draw a card
  311.  # PENALTY_SOUND    - Sound that will play when the revive penalty is applied
  312.  # DECK_ICON        - Icon that displays an actors deck size in battle
  313.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  314.  #                    ["filename", volume, pitch]  -> ["", 0, 0] means no sound
  315.   DRAW_SOUND        = [   "draw",    100,   150]
  316.   PENALTY_SOUND     = [ "Attack1",    100,    50]
  317.   DECK_ICON         = 804
  318.  
  319.  
  320.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  321.  #                                   Cards
  322.  #------------------------------------------------------------------------------
  323.  # To add custom card images, add an image with the name Card + skill id to your
  324.  # graphics folder (example: Card2). The script will use every picture it can
  325.  # find. Card0 is used when no card is selected. If you choose to do this, most
  326.  # of those options are irrelevant.
  327.  #
  328.  # CUSTOM_CARD_BG   - Name of the windowskin used for cards (in Graphics/System)
  329.  # CARD_BACK_OP     - Background opacity of cards (0-255)
  330.  # CARD_NAME_UP     - If true, card names will be written in capital letters
  331.  # MP_COLOR         - Textcolor the mana costs are drawn in
  332.  # TP_COLOR         - Textcolor the tp costs are drawn in
  333.  # RARITY           - Name and icon index of the raritys your cards have
  334.  # TYPES            - Different types your cards can have
  335.  # ELEMENT_COLOR    - Background color of your cards based on their element ID
  336.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  337.   CUSTOM_CARD_BG    = "Window"   # RPG Maker default is "Window"
  338.   CARD_BACK_OP      = 255
  339.   CARD_NAME_UP      = true
  340.   MP_COLOR          = 4
  341.   TP_COLOR          = 3
  342.  
  343.   RARITY = {
  344.  #      ID => [ icon,     "name"]
  345.          1 => [  798,   "Normal"],
  346.          2 => [  797,   "Rare"],
  347.          3 => [  796,   "Super Rare"],
  348.          4 => [  795,   "Ultra Rare"],
  349.   } # do not remove this
  350.  
  351.   SUBTYPE = {
  352.  #      ID => [ icon,     "name"]
  353.          1 =>  [  896,  "Dragon"],
  354.          2 =>  [  897,  "Zombie"],
  355.          3 =>  [  898,  "Fiend"],
  356.          4 =>  [  899,  "Pyro"],
  357.          5 =>  [  900,  "Sea-Serpent"],
  358.          6 =>  [  901,  "Rock"],
  359.          7 =>  [  902,  "Machine"],
  360.          8 =>  [  903,  "Fish"],
  361.          9 =>  [  904,  "Dinosaur"],
  362.          10 => [  905,  "Insect"],
  363.          11 => [  906,  "Beast"],
  364.          12 => [  907,  "Beast-Warrior"],
  365.          13 => [  908,  "Plant"],
  366.          14 => [  909,  "Aqua"],
  367.          15 => [  910,  "Warrior"],
  368.          16 => [  911,  "Winged-Beast"],
  369.          17 => [  912,  "Fairy"],
  370.          18 => [  913,  "Spellcaster"],
  371.          19 => [  914,  "Thunder"],
  372.          20 => [  915,  "Reptile"],
  373.          21 => [  916,  "Psychic"],
  374.          22 => [  917,  "Wyrm"],
  375.          23 => [  918,  "Divine-Beast"],
  376.          24 => [  913,  "Normal Spell"],
  377.          25 => [  914,  "Quick-Play Spell"],
  378.          26 => [  915,  "Continuous Spell"],
  379.          27 => [  916,  "Field Spell"],
  380.          28 => [  917,  "Equip Spell"],
  381.          29 => [  918,  "Normal Trap"],
  382.          30 => [  917,  "Continuous Trap"],
  383.          31 => [  918,  "Counter Trap"],
  384.   } # do not remove this
  385.    
  386.   LEVEL = {
  387.  #      ID => [ icon,     "name"]
  388.          1  => [ 803,   "Level 1"],
  389.          2  => [ 803,   "Level 2"],
  390.          3  => [ 803,   "Level 3"],
  391.          4  => [ 803,   "Level 4"],
  392.          5  => [ 803,   "Level 5"],
  393.          6  => [ 803,   "Level 6"],
  394.          7  => [ 803,   "Level 7"],
  395.          8  => [ 803,   "Level 8"],
  396.          9  => [ 803,   "Level 9"],
  397.          10 => [ 803,   "Level 10"],
  398.          11 => [ 803,   "Level 11"],
  399.          12 => [ 803,   "Level 12"],
  400.  
  401.   } # do not remove this
  402.      
  403.   NORMEFF = {
  404.  #      ID => [ icon,     "name"]
  405.          1  => [ 803,   "None"],
  406.          2  => [ 803,   "Effect"],
  407.          3  => [ 803,   "Normal"],
  408.          
  409.   } # do not remove this
  410.  
  411.   FRAME = {
  412.  #      ID => [ icon,     "name"]
  413.          1  => [ 803,   "Fusion"],
  414.          2  => [ 803,   "Synchro"],
  415.          3  => [ 803,   "Ritual"],
  416.          4  => [ 803,   "Tuner"],
  417.          5  => [ 803,   "Token"],
  418.          6  => [ 803,   "Normal"],
  419.          7  => [ 803,   "Effect"],
  420.          
  421.   } # do not remove this
  422.  
  423.   ARCHETYPE = {
  424.  #      ID => [ icon,     "name"]
  425.          1  => [ 803,   "X-Saber"],
  426.          2  => [ 803,   "Ice Barrier"],
  427.          3  => [ 803,   "Mist Valley"],
  428.          4  => [ 803,   "Flamvell"],
  429.          5  => [ 803,   "Dragunity"],
  430.          6  => [ 803,   "Gishki"],
  431.          7  => [ 803,   "Dragunity"],
  432.          8  => [ 803,   "Jurrac"],
  433.          9  => [ 803,   "Worm"],
  434.          10 => [ 803,   "Fabled"],
  435.          11 => [ 803,   "Polymerization"],
  436.          12 => [ 803,   "Kuriboh"],
  437.          13 => [ 803,   "Dark World"],
  438.          14 => [ 803,   "Jurassic World"],
  439.          15 => [ 803,   "Evolution Pill"],
  440.          
  441.   } # do not remove this
  442.  
  443.   SPECIAL = {
  444.  #      ID => [ icon,     "name"]
  445.          1 => [  782,   "Exodia"],
  446.          2 => [  782,   "Thunder Dragon"],
  447.          3 => [  782,   "Consonance"],
  448.          4 => [  782,   "exceptjarofavarice"],
  449.          5 => [  782,   "non-Effect"],
  450.          6 => [  782,   "Normal"],
  451.          7 => [  782,   "knowledge"],
  452.   } # do not remove this
  453.  
  454.   TYPE = {
  455.  #      ID => "name"
  456.          1 => "Spell",
  457.          2 => "Trap",
  458.          3 => "Monster",
  459.          4 => "Extra",
  460.   } # do not remove this
  461.  
  462.   ELEMENT_COLOR = {
  463.  #      ID => [red, grn, blu]
  464.          0 => [  0,   0,   0],   # 0 is used for skills without an element
  465.          1 => [200,   0, 100],
  466.          2 => [200,   0,   0],
  467.          3 => [200, 200,   0],
  468.          4 => [  0,   0, 200],
  469.          5 => [  0,   0, 200],
  470.          6 => [  0,   0, 200],
  471.          7 => [  0,   0, 200],
  472.          8 => [  0,   0, 200],
  473.          9 => [  0,   0, 200],
  474.   } # do not remove this
  475.  
  476.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  477.  #                              Start Decks
  478.  #------------------------------------------------------------------------------
  479.  # Those are the initial decks for each actor. If an actors id is not listed
  480.  # below, his initial deck will be empty.
  481.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  482.   START_DECKS = {
  483.           1 =>  [],
  484.          22 =>  [72,72,72,68,68,68,56,56,56,38,38,38,51,51,51,28,28,28,30,30,30,36,36,36,114,114,114,254,254,254,256,256,256,255,255,255,257,257,257,258,258,258,259,259,259,260,260,260,262,262,262,263,263,264,264,264],
  485.          12 =>  [29,29,29,26,26,26,27,27,114,114,114,254,254,254,256,256,256,255,255,255,257,257,257,258,258,258,259,259,259,260,260,260,262,262,262,263,263,264,264,264],
  486.          14 =>  [29,29,29,26,26,26,27,27,114,114,114,254,254,254,256,256,256,255,255,255,257,257,257,258,258,258,259,259,259,260,260,260,262,262,262,263,263,264,264,264],
  487.          6  =>  [29,29,29,26,26,26,27,27,114,114,114,254,254,254,256,256,256,255,255,255,257,257,257,258,258,258,259,259,259,260,260,260,262,262,262,263,263,264,264,264],
  488.          15 =>  [21,21,21,205,205,205,195,195,195,193,193,193,188,189,190,191,192],
  489.          16 =>  [186,186,186,186,186,186,186,186,186,186,186,101,101,101,112,189,190,191,192,74,74,74,193,193,193],
  490.          17 =>  [186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,188,189,190,191,192,74,74,74,193,193,193],
  491.          18 =>  [186,186,186,186,186,186,186,186,186,186,186,186,186,186,193,193,193,74,74,74],
  492.          19 =>  [21,21,21,205,205,205,195,195,195,193,193,193,188,189,190,191,192],
  493.          20 =>  [186,186,186,186,186,186,186,186,186,186,186,101,101,101,112,189,190,191,192,74,74,74,193,193,193],
  494.          21 =>  [186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,186,188,189,190,191,192,74,74,74,193,193,193],
  495.          23 =>  [186,186,186,186,186,186,186,186,186,186,186,186,186,186,193,193,193,74,74,74],
  496.          24 =>  [21,21,21,205,205,205,195,195,195,193,193,193,188,189,190,191,192],
  497.          25 =>  [186,186,186,186,186,186,186,186,186,186,186,101,101,101,112,189,190,191,192,74,74,74,193,193,193],
  498.          } # do not remove this
  499.  
  500.  #------------------------------------------------------------------------------
  501.  # The first effect called "grave" is available as a command in battle and
  502.  # usually used to view your grave. You can change this effect if you wish, but
  503.  # keep in mind that the player can use this effect as many times as he wants.
  504.  # Please add all other effects below. Do not forget that every effect needs its
  505.  # own name. You can name them however you want, as long as you dont use the
  506.  # same name twice.
  507.  #[Attritube, Type, Rarity, Subtype, Level, Normal/Effect, Frame, Archetype, ATK, DEF, Special]
  508.  #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  509.   CARD_EFFECT = {
  510.       "grave"       => [:view, 0, :grave, :grave],    # Grave command effect
  511.       "banish"      => [:view, 0, :banish, :banish],  # Banish command effect
  512.       "extra"       => [:view, 0, :extra, :extra],    # Extra command effect
  513.       "field"       => [:view, 0, :field, :field],    # Extra command effect
  514.       "senditself"  => [:cost, 1, :none, :none, nil, :grave],
  515.       "none"        => [:none],
  516.       "draw1"       => [:draw, 1, 100, :self],
  517.       "draw2"       => [:draw, 2, 100, :self],
  518.       "draw3"       => [:draw, 3, 100, :self],
  519.       "draw4"       => [:draw, 4, 100, :self],
  520.       "draw5"       => [:draw, 5, 100, :self],
  521.       "draw6"       => [:draw, 6, 100, :self],
  522.       "destroyitself" => [:destroy_active_card],
  523.       "exodia"      => [:take, 0, :hand, :hand,     [0,0,0,0,0,0,0,0,0,0,1], :grave, 4],
  524.       "discard1"    => [:takem, 0, :hand, :hand,    [0,0,0,0,0,0,0,0,0,0,0], :grave, 1],
  525.       "discard2"    => [:takem, 0, :hand, :hand,    [0,0,0,0,0,0,0,0,0,0,0], :grave, 2],
  526.       "discard5"    => [:take, 0, :hand, :hand,     [0,0,0,0,0,0,0,0,0,0,0], :grave, 5],
  527.       "kujikuri"    => [:take, 0, :hand_or_field, :hand_or_field,     [0,0,0,0,9,0,0,0,0,0,0], :grave, 1],
  528.       "discardhand" => [:takem, 0, :hand, :hand,    [0,0,0,0,0,0,0,0,0,0,0], :grave, 0],
  529.       "disc1monst"  => [:take, 0, :hand, :hand,     [0,3,0,0,0,0,0,0,0,0,0], :grave, 1],
  530.       "disc3monst"  => [:take, 0, :hand, :hand,     [0,3,0,0,0,0,0,0,0,0,0], :grave, 3],
  531.       "dealings"    => [:draw, 1, 100, :self, "discard1"],
  532.       "beginning"   => [:takem, 0, :grave,:grave,   [5,0,0,0,0,0,0,0,0,0,0], :banish, 5],
  533.       "allure"      => [:takem, 0, :hand, :hand,    [5,3,0,0,0,0,0,0,0,0,0], :banish, 1],
  534.       "avarice"     => [:takem, 0, :grave, :grave,   [0,3,0,0,0,0,0,0,0,0,0], :deck, 5],
  535.       "shuffle1"    => [:take, 0, :hand, :hand,     [0,0,0,0,0,0,0,0,0,0,0], :deck, 1],
  536.       "jaravarice"  => [:take, 0, :grave, :grave,   [0,0,0,0,0,0,0,0,0,0,0, :exclude_active], :deck, 5],
  537.       "moray"       => [:take, 0, :hand, :hand,     [4,3,0,0,0,0,0,0,0,0,0], :deck, 2],
  538.       "duality"     => [:takem, 3, :deck, :deck,    [0,0,0,0,0,0,0,0,0,0,0], :hand, 1],
  539.       "prosperity"  => [:takem, 6, :deck, :deck,    [0,0,0,0,0,0,0,0,0,0,0], :hand, 1],
  540.       "discardlv8"  => [:take, 0, :hand, :hand,     [0,0,0,0,8,0,0,0,0,0,0], :grave, 1],
  541.       "consonance"  => [:take, 0, :hand, :hand,     [0,0,0,1,0,0,4,0,0..1000,0,0], :grave, 1],
  542.       "cardsky"     => [:takem, 0, :hand, :hand,    [6,3,0,17,0,0,0,0,0,0,0], :banish, 1],
  543.       "addlv1"      => [:takem, 0, :deck, :deck,    [0,3,0,0,1,0,0,0,0,0,0], :hand, 1],
  544.       "teleport"    => [:take, 0, :deck, :deck,     [0,3,0,21,1..3,0,0,0,0,0,0], :field, 1],
  545.       "desires"     => [:auto, 10, :deck, :deck,    [0,0,0,0,0,0,0,0,0,0,0], :banish, :face_down, 10],
  546.       "acqui"       => [:take, 0, :banish, :banish, [0,3,0,0,0,0,0,0,0,0,0], :deck, 3],
  547.       "7sword"      => [:take, 0, :hand_or_field, :hand_or_field,     [0,3,0,0,7,0,0,0,0,0,0], :banish, 1],
  548.       "elephant"    => [:take, 0, :hand, :hand,     [0,3,0,0,0,3,0,0,0,0,0], :grave, 1],
  549.   "banish1normal"   => [:take, 0, :hand, :hand,     [0,3,0,0,0,3,0,0,0,0,0], :banish, 1],
  550.       "knowledge"   => [:take, 0, :hand, :hand,     [0,3,0,18,0,0,0,0,0,0,0], :grave, 1],
  551.       "brightf"     => [:take, 0, :banish, :banish, [0,0,3,21,0,0,0,0,0,0,0], :grave, 2],
  552.       "overload"    => [:take, 0, :grave, :grave,   [0,0,0,21,0,0,0,0,0,0,0], :deck, 3],
  553.       "backup"      => [:take, 0, :grave, :grave,   [0,0,0,0,0,3,0,0,0,0,0], :hand, 3],
  554.       "addpoly"     => [:take, 0, :deck, :deck,     [0,0,0,0,0,0,0,11,0,0,0], :hand, 1],
  555.       "miracledig"  => [:take, 0, :banish, :banish, [0,3,0,0,0,0,0,0,0,0,0], :grave, 3],
  556.       "kuriboh"     => [:take, 0, :hand, :hand,     [0,3,0,0,0,0,0,12,0,0,0], :grave, 1],
  557.       "release"     => [:takem, 0, :grave, :grave,  [0,0,0,0,0,0,0,0,0,0,0], :banish, 5],
  558.       "fossildig"   => [:takem, 0, :deck, :deck,    [0,0,0,9,1..6,0,0,0,0,0,0], :hand, 1],
  559.       "lightload"   => [:takem, 0, :hand, :hand,    [0,3,0,0,7..12,0,0,0,0,0,0], :deck, 1],
  560.     "foolishburial" => [:take, 0, :deck, :deck,     [0,3,0,0,0,0,0,0,0,0,0], :grave, 1],
  561.       "banish1wb"   => [:take, 0, :deck, :deck,     [0,3,0,11,0,0,0,0,0,0,0], :banish, 1],
  562.       "banish1b"    => [:take, 0, :deck, :deck,     [0,3,0,16,0,0,0,0,0,0,0], :banish, 1],
  563.       "rota"        => [:take, 0, :deck, :deck,     [0,3,0,15,1..4,0,0,0,0,0,0], :hand, 1],
  564.     "reincarnation" => [:take, 0, :grave, :grave,   [0,3,0,0,0,0,0,0,0,0,0], :hand, 1],
  565.     "throwextra"    => [:take, 0, :extra, :extra,   [0,0,0,0,0,0,0,0,0,0,0], :grave, 1],
  566.     "prosp3"        => [:take, 0, :extra, :extra,   [0,0,0,0,0,0,0,0,0,0,0], :banish, 3],
  567.     "prosp6"        => [:take, 0, :extra, :extra,   [0,0,0,0,0,0,0,0,0,0,0], :banish, 6],
  568.     "bombardment"   => [:take, 0, :deck, :deck,     [0,3,0,6,0,0,0,0,0,0,0], :grave, 1],
  569.     "genta"         => [:take, 0, :deck, :deck,     [0,0,0,27,0,0,0,13,0,0,0], :hand, 1],
  570.     "costgatesdw"   => [:take, 0, :grave, :grave,   [0,0,0,3,0,0,0,0,0,0,0], :banish, 1],
  571.     "effgatesdw"    => [:takem, 0, :hand, :hand,    [0,0,0,3,0,0,0,0,0,0,0], :grave, 1],
  572.     "jurassicworld" => [:take, 0, :deck, :deck,     [0,0,0,27,0,0,0,14,0,0,0], :hand, 1],
  573.     "ovisummon"     => [:takem, 0, :deck, :deck,    [0,0,0,9,0,0,0,0,0,0,0], :choice_hand_grave, 1],
  574.     "ovi_destroy"   => [:take, 0, :field, :field,   [0,3,0,9,1..4,0,0,0,0,0,0, :exclude_itself, :face_up], :destroy_and_chain, "ovi_revive"],
  575.     "ovi_revive"    => [:takem, 0, :grave, :grave,  [0,3,0,9,0,0,0,0,0,0,0], :special_summon_def, 1],
  576.     "babycera"      => [:takem, 0, :deck, :deck,    [0,3,0,9,1..4,0,0,0,0,0,0], :special_summon, 1],
  577.     "UCT"           => [:take, 0, :grave, :grave,   [0,3,0,9,0,0,0,0,0,0,0], :inherent_landing, 2],
  578.     "endofline"     => [:none],
  579.     "miscellahand"  => [:none],
  580.     "miscellagrave" => [:take, 0, :grave, :grave,   [0,3,0,9,0,0,0,0,0,0,0], :inherent_landing, 2],
  581.     "archo_destroy" => [:takem, 0, :hand_or_field, :hand_or_field, [0,0,0,9,0,0,0,0,0,0,0, :exclude_active, :face_up], :destroy_and_chain, 1, "archo_add"],
  582.     "archo_add" => [:takem, 0, :deck, :deck,
  583.     [
  584.       [0,0,0,12, :destroyed_level,0,0,0,0,0,0],
  585.       [0,0,0,5, :destroyed_level,0,0,0,0,0,0],
  586.       [0,0,0,20, :destroyed_level,0,0,0,0,0,0],
  587.       [0,0,0,0,0,0,0,15,0,0,0]
  588.     ], :hand, 1],
  589.     "double_pill_cost1" => [:takem, 0, :hand_or_grave, :hand_or_grave, [0,3,0,9,0,0,0,0,0,0,0], :banish_and_chain, 1, "double_pill_cost2"],
  590.     "double_pill_cost2" => [:takem, 0, :hand_or_grave, :hand_or_grave, [0,3,0,"!9",0,0,0,0,0,0,0], :banish, 1, ],
  591.     "double_pill_resolve" => [:takem, 0, :hand_or_deck, :hand_or_deck, [0,3,0,9,7..12,0,0,0,0,0,0], :special_summon_ignore, 1],
  592.     "giantrex" => [:impact, 1, :self_banished, :none, nil, :special_summon_self],
  593.     "draining_shield" => [:quick_reactive, :negate_attack, :heal_atk],
  594.     "enchantedjavelin" => [:quick_reactive, :heal_atk],
  595.     "numinous_healer" => [:quick_reactive, :heal_1000, :heal_500_x_29_grave],
  596.     "rainbowlife" => [:add_state, 0, :user, 66],
  597.     "heal700" =>  [:quick_reactive, :heal_700],
  598.     "heal2000" => [:quick_reactive, :heal_2000],
  599.     "heal3000" => [:quick_reactive, :heal_3000],
  600.     "heal4000" => [:quick_reactive, :heal_4000],
  601.  
  602.  
  603. } # do not remove this
  604.  
  605. #-------------------------------------------------------------------------------
  606. #
  607. #                                 End Options
  608. #
  609. #-------------------------------------------------------------------------------
  610.  
  611.   end #CG
  612. end #PC27
  613.  
  614. ################################################################################
  615. #                          Start of script                                     #
  616. ################################################################################
  617.  
  618. $imported = {} if $imported.nil?
  619. $imported["PC27: CardGame System"] = true
  620.  
  621. #===============================================================================
  622. # Flow trace helper (DEBUG)
  623. #===============================================================================
  624. module FlowTrace
  625.   def trace_call(name)
  626.     puts "[FLOW] #{name}"
  627.   end
  628. end
  629.  
  630. #===============================================================================
  631. # Load notetags
  632. #===============================================================================
  633.  
  634. module DataManager
  635.  
  636.   class <<self; alias load_database_pc27_cardgame load_database; end
  637.    
  638.   def self.load_database
  639.     load_database_pc27_cardgame
  640.     load_notetags_pc27_cardgame
  641.   end
  642.  
  643.   def self.load_notetags_effect
  644.     for skill in $data_skills
  645.       next if skill.nil?
  646.       skill.load_notetags_effect
  647.     end
  648.   end
  649.  
  650.   def self.load_notetags_pc27_cardgame
  651.     for skill in $data_skills
  652.       next if skill.nil?
  653.       skill.load_notetags_pc27_cardgame_skill
  654.     end
  655.     for item in $data_items
  656.       next if item.nil?
  657.       item.load_notetags_pc27_cardgame_item
  658.     end
  659.     for state in $data_states
  660.       next if state.nil?
  661.       state.load_notetags_pc27_cardgame_states
  662.     end
  663.     for equip_type in [$data_weapons, $data_armors]
  664.       for obj in equip_type
  665.         next if obj.nil?
  666.         obj.load_notetags_pc27_cardgame_equip
  667.       end
  668.     end
  669.   end
  670. end
  671.  
  672. #===============================================================================
  673. # Usable Item
  674. #===============================================================================
  675.  
  676. class RPG::UsableItem < RPG::BaseItem
  677.   attr_accessor :card_id
  678.   attr_accessor :attribute
  679.   attr_accessor :type
  680.   attr_accessor :rarity
  681.   attr_accessor :subtype
  682.   attr_accessor :level
  683.   attr_accessor :normeff
  684.   attr_accessor :frame
  685.   attr_accessor :archetype
  686.   attr_accessor :atk
  687.   attr_accessor :def
  688.   attr_accessor :special
  689.   attr_accessor :cost
  690.   attr_accessor :impact
  691.   attr_accessor :impact2
  692.   attr_accessor :fieldeff
  693.   attr_accessor :graveeff
  694.   attr_accessor :handeff
  695.   attr_accessor :hand_impact
  696.   attr_accessor :grave_impact
  697.   attr_accessor :field_impact
  698.   attr_accessor :summon_impact
  699.   attr_accessor :properly_summoned
  700.  
  701.   #-----------------------------------------------------------------------------
  702.   # Add card attributes
  703.   #-----------------------------------------------------------------------------
  704.   def load_notetags_pc27_cardgame_skill
  705.     @description = wordwrap(@description)
  706.     @card_id = 0
  707.     @attribute = 0
  708.     @type    = 1
  709.     @rarity  = 1
  710.     @subtype = 1
  711.     @level   = 1
  712.     @normeff = 1
  713.     @frame   = 1
  714.     @archetype = 1
  715.     @atk     = 1
  716.     @def     = 1
  717.     @special = 1
  718.     @cost    = false
  719.     @impact  = false
  720.     @fieldeff = 0
  721.     @graveeff = 0
  722.     @hand_impact  = false
  723.     @grave_impact = false
  724.     @field_impact = false
  725.     @summon_impact = false
  726.     self.note.split(/[\r\n]+/).each { |line|
  727.       if /<card[ -_]id: +(?<inr>\d+)>/i =~ line
  728.         @card_id = inr.to_i
  729.       elsif /<card[ -_]attribute: +(?<attrnr>\d+)>/i =~ line
  730.         @attribute = attrnr.to_i
  731.       elsif /<card[ -_]type: +(?<tnr>\d+)>/i =~ line
  732.         @type = tnr.to_i
  733.       elsif /<card[ -_]rarity: +(?<rnr>\d+)>/i =~ line
  734.         @rarity = rnr.to_i
  735.       elsif /<card[ -_]subtype: +(?<stnr>\d+)>/i =~ line
  736.         @subtype = stnr.to_i
  737.       elsif /<card[ -_]level: +(?<lvnr>\d+)>/i =~ line
  738.         @level = lvnr.to_i
  739.       elsif /<card[ -_]normeff: +(?<nenr>\d+)>/i =~ line
  740.         @normeff = nenr.to_i
  741.       elsif /<card[ -_]frame: +(?<frnr>\d+)>/i =~ line
  742.         @frame = frnr.to_i
  743.       elsif /<card[ -_]archetype: +(?<arnr>\d+)>/i =~ line
  744.         @archetype = arnr.to_i
  745.       elsif /<card[ -_]atk: +(?<atnr>\d+)>/i =~ line
  746.         @atk = atnr.to_i
  747.       elsif /<card[ -_]def: +(?<dfnr>\d+)>/i =~ line
  748.         @def = dfnr.to_i
  749.       elsif /<card[ -_]special: +(?<spnr>\d+)>/i =~ line
  750.         @special = spnr.to_i
  751.       elsif /<card[ -_]cost: +(?<cst>.+)>/i =~ line
  752.         @cost = cst
  753.       elsif /<card[ -_]impact: +(?<imp>.+)>/i =~ line
  754.         @impact = imp
  755.       elsif /<card[ -_]impact2: +(?<imp2>.+)>/i =~ line
  756.         @impact2 = imp2
  757.       elsif /<(?:field)[ ](\d+)>/i =~ line
  758.         @fieldeff = $1.to_i
  759.       elsif /<(?:grave)[ ](\d+)>/i =~ line
  760.         @graveeff = $1.to_i
  761.       elsif /<(?:hand)[ ](\d+)>/i =~ line
  762.         @handeff = $1.to_i
  763.       elsif /<hand[-_ ]impact:\s*(.+?)>/i =~ line
  764.         @hand_impact = $1.to_s.strip
  765.       elsif /<grave[-_ ]impact:\s*(.+?)>/i =~ line
  766.         @grave_impact = $1.to_s.strip
  767.       elsif /<field[-_ ]impact:\s*(.+?)>/i =~ line
  768.         @field_impact = $1.to_s.strip
  769.       elsif /<summon[-_ ]impact:\s*(.+?)>/i =~ line
  770.         @summon_impact = $1.to_s.strip
  771.       end
  772.     }
  773.   end
  774.  
  775.   def load_notetags_pc27_cardgame_item
  776.     @impact = false
  777.     @impact2 = false
  778.     @fieldeff = 0
  779.     @graveeff = 0
  780.     @handeff = 0
  781.     self.note.split(/[\r\n]+/).each { |line|
  782.       if /<card[ -_]impact: +(?<imp>.+)>/i =~ line
  783.         @impact = imp
  784.       elsif /<card[ -_]impact2: +(?<imp2>.+)>/i =~ line
  785.         @impact2 = imp2
  786.       elsif /<(?:field)[ ](\d+)>/i =~ line
  787.         @fieldeff = $1.to_i
  788.       elsif /<(?:grave)[ ](\d+)>/i =~ line
  789.         @graveeff = $1.to_i
  790.       elsif /<(?:hand)[ ](\d+)>/i =~ line
  791.         @handeff = $1.to_i
  792.       end
  793.       }
  794.   end
  795.    
  796.   #-----------------------------------------------------------------------------
  797.   # Method to rearange text so it fits inside a window
  798.   #-----------------------------------------------------------------------------
  799.   def wordwrap(desc_text, window = nil, default_max_chars = 16)
  800.     max_chars = (window ? (window.width - 2 * window.padding) / 10 : default_max_chars)
  801.     text, line= "", ""
  802.     desc_text.split(/[\r\n ]+/).each { |word|
  803.     if word.size + line.size > max_chars
  804.       text += line+"\n"
  805.       line = word
  806.     else
  807.       line += word
  808.     end
  809.     line += " "
  810.     }
  811.     text += line
  812.     return text
  813.   end
  814.  
  815.   def match_filter?(filter)
  816.     return true if filter.nil?
  817.     return false if filter[0] != 0 && self.type != filter[0]
  818.     return false if filter[1] != 0 && self.subtype != filter[1]
  819.     return false if filter[2] != 0 && self.rarity != filter[2]
  820.     return false if filter[3] != 0 && self.archetype != filter[3]
  821.     if filter[4].is_a?(Range)
  822.       return false unless filter[4].include?(self.level)
  823.     elsif filter[4] != 0
  824.       return false if self.level != filter[4]
  825.     end
  826.     return true
  827.   end
  828.  
  829. end
  830.  
  831. #===============================================================================
  832. # Equip Item
  833. #===============================================================================
  834.  
  835. class RPG::EquipItem < RPG::BaseItem
  836.   attr_accessor :extra_turn_cards
  837.   attr_accessor :extra_start_cards
  838.  
  839.   def load_notetags_pc27_cardgame_equip
  840.     @extra_turn_cards = 0
  841.     @extra_start_cards = 0
  842.     self.note.split(/[\r\n]+/).each { |line|
  843.       if /<extra[ -_]turn[ -_]cards: +(?<tnr>\d+)>/i =~ line
  844.         @extra_turn_cards = tnr.to_i
  845.       elsif /<extra[ -_]start[ -_]cards: +(?<snr>\d+)>/i =~ line
  846.         @extra_start_cards = snr.to_i
  847.       end
  848.     }
  849.   end
  850. end
  851.  
  852. #===============================================================================
  853. # State
  854. #===============================================================================
  855.  
  856. class RPG::State < RPG::BaseItem
  857.   attr_reader :limited_actions
  858.  
  859.   def load_notetags_pc27_cardgame_states
  860.     @limited_actions = 0
  861.     self.note.split(/[\r\n]+/).each { |line|
  862.       if /<limited[ -_]actions: +(?<lnr>\d+)>/i =~ line
  863.         @limited_actions = lnr.to_i
  864.       end
  865.     }
  866.   end
  867. end
  868.  
  869. #===============================================================================
  870. # Game_Interpreter
  871. #===============================================================================
  872. class Game_Interpreter
  873.  
  874.   def add_card_to_coll(*args)
  875.     for id in args
  876.       $game_party.coll.push($data_skills[id])
  877.     end
  878.   end
  879.  
  880.   def remove_card?(id)
  881.     for i in (0...$game_party.coll.length)
  882.       if $game_party.coll[i].id == id
  883.         $game_party.coll.delete_at(i)
  884.         return true
  885.       end
  886.     end
  887.     return false
  888.   end
  889.  
  890.   def remove_all_cards(*args)
  891.     for id in args
  892.       $game_party.coll -= [$data_skills[id]]
  893.       for actor in $game_party.members
  894.         actor.deck -= [$data_skills[id]]
  895.       end
  896.     end
  897.   end
  898.  
  899.   def has_card?(id)
  900.     for card in $game_party.coll
  901.       return true if card.id == id
  902.     end
  903.     for actor in $game_party.members
  904.       for card in actor.deck
  905.         return true if card.id == id
  906.       end
  907.     end
  908.     return false
  909.   end
  910.  
  911.   def atributes_in_grave(id, count)
  912.     if self.grave.count { |card| skill.attribute == id} <= count
  913.       return true
  914.     else
  915.       return false
  916.     end
  917.   end
  918.  
  919.   def has_card_in_deck?(id)
  920.     for card in self.deck
  921.       return true if card.id == id
  922.     end
  923.     return false
  924.   end
  925.    
  926.   def has_card_in_field?(id)
  927.     for card in self.field
  928.       return true if card.id == id
  929.     end
  930.     return false
  931.   end
  932.      
  933.   def has_many_card_in_deck?(count)
  934.     return self.deck.count { |card| } >= count
  935.   end
  936.  
  937.   def has_x_cards?(id, count)
  938.     return self.deck.count { |card| card.id == id } >= count
  939.   end
  940.  
  941.   def has_card_in_hand?(id)
  942.     for card in self.hand
  943.       return true if card.id == id
  944.     end
  945.     return false
  946.   end
  947.  
  948.   def has_card_in_grave?(id)
  949.     actor = $game_actors[$game_variables[27]]
  950.     for card in actor.grave
  951.       return true if card.id == id
  952.     end
  953.     return false
  954.   end
  955.    
  956.   def has_x_cards_in_grave?(id, count)
  957.     actor = $game_actors[$game_variables[27]]
  958.     return actor.grave.count { |card| card.id == id } >= count
  959.   end
  960.  
  961.   def numinous_in_grave
  962.     nr = 0
  963.     for actor in $game_party.members
  964.       nr += actor.grave.count { |card| card.id == 29 }
  965.     end
  966.     return nr
  967.   end
  968.  
  969.   def has_card_in_banish?(id)
  970.     actor = $game_actors[$game_variables[27]]
  971.     for card in actor.banish
  972.       return true if card.id == id
  973.     end
  974.     return false
  975.   end
  976.    
  977.   def has_x_cards_in_banish?(id, count)
  978.     actor = $game_actors[$game_variables[27]]
  979.     return actor.banish.count { |card| card.id == id } >= count
  980.   end
  981.  
  982.   def add_card_deck(*args)
  983.     for id in args
  984.       actor = $game_actors[$game_variables[27]]
  985.       actor.deck += [$data_skills[id]]
  986.     end
  987.   end
  988.    
  989.   def remove_card_deck(*args)
  990.     for id in args
  991.       actor = $game_actors[$game_variables[27]]
  992.       actor.deck -= [$data_skills[id]]
  993.     end
  994.   end
  995.  
  996.   def add_card_hand(*args)
  997.     for id in args
  998.       self.hand += [$data_skills[id]]
  999.     end
  1000.   end
  1001.  
  1002.   def summon_token(*args)
  1003.     for id in args
  1004.       self.field += [$data_skills[id]]
  1005.     end
  1006.   end
  1007.    
  1008.   def add_card_hand_2(*args)
  1009.     for id in args
  1010.       actor = $game_actors[$game_variables[27]]
  1011.       actor.hand += [$data_skills[id]]
  1012.     end
  1013.   end
  1014.  
  1015.   def remove_card_hand(*args)
  1016.     for id in args
  1017.       self.hand -= [$data_skills[id]]
  1018.     end
  1019.   end
  1020.  
  1021.   def send_card_from_hand_to_grave(*args)
  1022.     for id in args
  1023.       actor = $game_actors[$game_variables[42]]
  1024.       actor.hand -= [$data_skills[id]]
  1025.       actor.grave += [$data_skills[id]]
  1026.     end
  1027.   end
  1028.  
  1029.   def send_card_from_hand_to_grave_2(id)
  1030.     actor = $game_actors[$game_variables[42]]
  1031.     for i in (0...actor.hand.length)
  1032.       if actor.hand[i].id == id
  1033.         actor.grave.push(@hand[i])
  1034.         actor.hand.delete_at(i)
  1035.         return true
  1036.       end
  1037.     end
  1038.     return false
  1039.   end
  1040.  
  1041.   def count_actor_deck(id)
  1042.     return $game_actors[id].deck.size
  1043.   end
  1044.  
  1045.   def count_all_decks
  1046.     nr = 0
  1047.     for actor in $game_party.members
  1048.       nr += actor.deck.size
  1049.     end
  1050.     return nr
  1051.   end
  1052. end
  1053.  
  1054. #===============================================================================
  1055. # Game_Battler
  1056. #===============================================================================
  1057.  
  1058. class Game_Battler < Game_BattlerBase
  1059.  
  1060.   #-----------------------------------------------------------------------------
  1061.   # defines card actions for non actors, in case the user messes up
  1062.   #-----------------------------------------------------------------------------
  1063.   def revive_grave(show = true) ;end
  1064.   def revive_penalty            ;end
  1065.   def shuffle_deck(sound = true);end
  1066.   def card_to_grave?(id)        ;end
  1067.   def throw_cards(n)            ;end
  1068.   def draw_cards(n)             ;end
  1069.  
  1070. end
  1071.  
  1072. #===============================================================================
  1073. # Game_Actor
  1074. #===============================================================================
  1075.  
  1076. class Game_Actor < Game_Battler
  1077.  
  1078.   #-----------------------------------------------------------------------------
  1079.   # Attributes
  1080.   #-----------------------------------------------------------------------------
  1081.   attr_accessor :deck
  1082.   attr_accessor :grave
  1083.   attr_accessor :banish
  1084.   attr_accessor :extra
  1085.   attr_accessor :hand
  1086.   attr_accessor :field
  1087.   attr_accessor :monster_zones
  1088.   attr_accessor :st_zones
  1089.   attr_accessor :field_zone
  1090.   attr_accessor :stack
  1091.   attr_accessor :turn_actions
  1092.   attr_accessor :current_zone
  1093.   attr_accessor :used_turn_effects
  1094.   attr_accessor :is_official_shuffle
  1095.   attr_accessor :monster_zones_state
  1096.   attr_accessor :st_zones_state
  1097.   attr_accessor :field_zone_state
  1098.  
  1099.   def init_card_zones
  1100.     @monster_zones ||= Array.new(5)
  1101.     @monster_zones_state ||= Array.new(5) { { position: :attack, face_down: false } }
  1102.     @spell_zones   ||= Array.new(5)
  1103.     @field_zone    ||= nil
  1104.   end
  1105.  
  1106.   def card_in_zone(zone, index)
  1107.     case zone
  1108.     when :monster
  1109.       @monster_zones[index]
  1110.     when :spell
  1111.       @st_zones[index]
  1112.     when :fieldspell
  1113.       @field_zone
  1114.     else
  1115.       nil
  1116.     end
  1117.   end
  1118.  
  1119.   def free_monster_zone_available?
  1120.     init_card_zones
  1121.     @monster_zones.any? { |z| z.nil? }
  1122.   end
  1123.  
  1124.   def st_zone_available?
  1125.     init_card_zones
  1126.     @st_zones.include?(nil)
  1127.   end
  1128.  
  1129.   def st_zones_state
  1130.     @st_zones_state ||= Array.new(5) { { face_down: false, set_this_turn: false } }
  1131.   end
  1132.  
  1133.   def field_zone_state
  1134.     @field_zone_state ||= { face_down: false, set_this_turn: false }
  1135.   end
  1136.  
  1137.    def public_banish
  1138.     self.banish.reject { |c| c.instance_variable_get(:@banished_face_down) }
  1139.   end
  1140.  
  1141.   def private_banish
  1142.     self.banish.select { |c| c.instance_variable_get(:@banished_face_down) }
  1143.   end
  1144.  
  1145.   def card_counters
  1146.     @card_counters ||= {}
  1147.   end
  1148.  
  1149.   def add_card_counter(card_obj_id, type, amount = 1)
  1150.     card_counters[card_obj_id] ||= {}
  1151.     card_counters[card_obj_id][type] ||= 0
  1152.     card_counters[card_obj_id][type] += amount
  1153.   end
  1154.  
  1155.   def get_card_counters(card_obj_id, type)
  1156.     return 0 unless card_counters[card_obj_id]
  1157.     card_counters[card_obj_id][type] || 0
  1158.   end
  1159.  
  1160.   def clear_card_counters(card_obj_id)
  1161.     card_counters.delete(card_obj_id) if card_counters.key?(card_obj_id)
  1162.   end
  1163.  
  1164.   def leads_to_special_summon?(effect_name, history = [])
  1165.     return false if effect_name.nil? || history.include?(effect_name)
  1166.     history << effect_name
  1167.     effect_data = PC27::CG::CARD_EFFECT[effect_name]
  1168.     return false unless effect_data
  1169.     destination = effect_data[5]
  1170.     sp_summons = [:special_summon, :special_summon_def, :special_summon_atk,
  1171.                   :special_summon_ignore, :special_summon_self, :inherent_landing]
  1172.     return true if sp_summons.include?(destination)
  1173.     next_effect = effect_data[7] || effect_data[6]
  1174.     if next_effect.is_a?(String)
  1175.       if [:destroy_and_chain, :banish_and_chain].include?(destination) || effect_data[0] == :cost
  1176.         return leads_to_special_summon?(next_effect, history)
  1177.       end
  1178.     end
  1179.     return false
  1180.   end
  1181.  
  1182.   def can_activate_impact?(impact_name)
  1183.     return false unless impact_name
  1184.     if self.state?(18) && leads_to_special_summon?(impact_name)
  1185.       return false
  1186.     end
  1187.     card = nil
  1188.     if SceneManager.scene_is?(Scene_Battle)
  1189.       card = SceneManager.scene.instance_variable_get(:@selected_card) ||
  1190.              SceneManager.scene.instance_variable_get(:@active_effect_card) ||
  1191.              $temp_active_card
  1192.     end
  1193.     if card
  1194.       card.note.split(/[\r\n]+/).each do |line|
  1195.         if line =~ /<block_if_state:\s*(.+?)\s*,\s*(\d+)>/i
  1196.           effect_to_block = $1.to_s.strip.downcase
  1197.           state_id = $2.to_i
  1198.           if effect_to_block == impact_name.to_s.downcase && self.state?(state_id)
  1199.             return false
  1200.           end
  1201.         end
  1202.       end
  1203.     end
  1204.     effect_data = PC27::CG::CARD_EFFECT[impact_name]
  1205.     return true unless effect_data
  1206.     target_zone = effect_data[2]
  1207.     filter = effect_data[4]
  1208.     cards_to_check = case target_zone
  1209.                      when :deck then self.deck
  1210.                      when :grave then self.grave
  1211.                      when :hand then self.hand
  1212.                      when :banish then self.banish
  1213.                      when :extra then self.extra
  1214.                      when :field then self.field
  1215.                      else nil
  1216.                      end
  1217.     if cards_to_check && filter && filter.is_a?(Array)
  1218.       valid_targets = cards_to_check.count do |card_target|
  1219.         ok = true
  1220.         ok = false if filter[0] != 0 && card_target.attribute != filter[0]
  1221.         ok = false if filter[1] != 0 && card_target.type      != filter[1]
  1222.         ok = false if filter[2] != 0 && card_target.rarity    != filter[2]
  1223.         ok = false if filter[3] != 0 && card_target.subtype   != filter[3]
  1224.         if filter[4].is_a?(Range)
  1225.           ok = false unless filter[4].include?(card_target.level)
  1226.         elsif filter[4] != 0
  1227.           ok = false if card_target.level != filter[4]
  1228.         end
  1229.         ok = false if filter[5] != 0 && card_target.normeff   != filter[5]
  1230.         ok = false if filter[6] != 0 && card_target.frame     != filter[6]
  1231.         ok = false if filter[7] != 0 && card_target.archetype != filter[7]
  1232.         ok = false if filter[8] != 0 && card_target.atk       != filter[8]
  1233.         ok = false if filter[9] != 0 && card_target.def       != filter[9]
  1234.         ok = false if filter[10] && filter[10] != 0 && card_target.special != filter[10]
  1235.         ok
  1236.       end
  1237.       req_amount = effect_data[1].to_i > 0 ? effect_data[1].to_i : 1
  1238.       return valid_targets >= req_amount
  1239.     elsif cards_to_check
  1240.       return cards_to_check.size > 0
  1241.     end
  1242.     return true
  1243.   end
  1244.    
  1245.   def is_face_up?(card)
  1246.     if self.respond_to?(:monster_zones) && self.monster_zones.include?(card)
  1247.       idx = self.monster_zones.index(card)
  1248.       state = self.monster_zones_state[idx]
  1249.       return state ? !state[:face_down] : true
  1250.     end
  1251.     if self.respond_to?(:st_zones) && self.st_zones.include?(card)
  1252.       idx = self.st_zones.index(card)
  1253.       state = self.st_zones_state[idx] rescue nil
  1254.       return state ? !state[:face_down] : true
  1255.     end
  1256.     if self.respond_to?(:field_zone) && self.field_zone == card
  1257.       state = self.field_zone_state rescue nil
  1258.       return state ? !state[:face_down] : true
  1259.     end
  1260.     return true
  1261.   end
  1262.  
  1263.   def active_continuous_effects
  1264.     effects = []
  1265.     return effects unless self.respond_to?(:field)
  1266.     self.field.each do |card|
  1267.       next if card.nil?
  1268.       next unless is_face_up?(card)
  1269.       regex = /<(on_[a-z_]+)_(at_[a-z_]+):\s*([a-z_]+)\.(\d+)\s*(=[+-])\s*([a-z_]+)\.(\d+)>/i
  1270.       card.note.scan(regex) do |trigger, target_loc, f_type, f_val, op, stat, stat_val|
  1271.         effects << {
  1272.           source_card: card,
  1273.           trigger: trigger.downcase,       # Ej: "on_field"
  1274.           target_loc: target_loc.downcase,   # Ej: "at_field" o "at_hand"
  1275.           filter_type: f_type.downcase,    # Ej: "subtype" o "type"
  1276.           filter_val: f_val.to_i,          # Ej: 9 o 3
  1277.           operation: op,                   # Ej: "=+" o "=-"
  1278.           stat: stat.downcase,             # Ej: "atk", "def" o "level"
  1279.           amount: stat_val.to_i            # Ej: 300 o 1
  1280.         }
  1281.       end
  1282.     end
  1283.     effects
  1284.   end
  1285.  
  1286.   def calculate_continuous_stat(card, base_stat, stat_name, current_location)
  1287.     return base_stat if card.nil?
  1288.     total_stat = base_stat
  1289.     active_continuous_effects.each do |eff|
  1290.       next unless eff[:target_loc] == current_location
  1291.       match = false
  1292.       case eff[:filter_type]
  1293.       when "subtype"
  1294.         sub = card.respond_to?(:subtype) ? card.subtype : nil
  1295.         if sub.is_a?(Array)
  1296.           match = sub.map(&:to_i).include?(eff[:filter_val])
  1297.         else
  1298.           match = (sub.to_i == eff[:filter_val])
  1299.         end
  1300.       when "type"
  1301.         match = (card.respond_to?(:type) && card.type == eff[:filter_val])
  1302.       when "element", "attribute"
  1303.         match = (card.respond_to?(:damage) && card.attribute == eff[:filter_val])
  1304.       end
  1305.       next unless match
  1306.       next unless eff[:stat] == stat_name
  1307.       if eff[:operation] == "=+"
  1308.         total_stat += eff[:amount]
  1309.       elsif eff[:operation] == "=-"
  1310.         total_stat -= eff[:amount]
  1311.         total_stat = 0 if total_stat < 0
  1312.       end
  1313.     end
  1314.    
  1315.     return total_stat
  1316.   end
  1317.  
  1318.   def current_monster_atk(card, location = "at_field")
  1319.     base = card.atk rescue 0
  1320.     personal_buff = 0
  1321.     if location == "at_field" && self.respond_to?(:monster_zones)
  1322.       idx = self.monster_zones.index { |c| c && c.equal?(card) }
  1323.       if idx
  1324.         state = self.monster_zones_state[idx]
  1325.         personal_buff = state[:personal_atk_buff].to_i if state && !state[:face_down]
  1326.       end
  1327.     end
  1328.     calculate_continuous_stat(card, base, "atk", location) + personal_buff
  1329.   end
  1330.  
  1331.   def current_monster_def(card, location = "at_field")
  1332.     base = card.def rescue 0
  1333.     personal_buff = 0
  1334.     if location == "at_field" && self.respond_to?(:monster_zones)
  1335.       idx = self.monster_zones.index { |c| c && c.equal?(card) }
  1336.       if idx
  1337.         state = self.monster_zones_state[idx]
  1338.         personal_buff = state[:personal_def_buff].to_i if state && !state[:face_down]
  1339.       end
  1340.     end
  1341.     calculate_continuous_stat(card, base, "def", location) + personal_buff
  1342.   end
  1343.  
  1344.   def current_monster_level(card, location = "at_field")
  1345.     base = card.level rescue 0
  1346.     personal_buff = 0
  1347.     if location == "at_field" && self.respond_to?(:monster_zones)
  1348.       idx = self.monster_zones.index { |c| c && c.equal?(card) }
  1349.       if idx
  1350.         state = self.monster_zones_state[idx]
  1351.         personal_buff = state[:personal_level_buff].to_i if state && !state[:face_down]
  1352.       end
  1353.     end
  1354.     calculate_continuous_stat(card, base, "level", location) + personal_buff
  1355.   end
  1356.  
  1357.   alias pc27_param_plus_ygo param_plus unless method_defined?(:pc27_param_plus_ygo)
  1358.   def param_plus(param_id)
  1359.     total = pc27_param_plus_ygo(param_id)
  1360.     if param_id == 2 || param_id == 3
  1361.       if self.respond_to?(:monster_zones) && self.monster_zones
  1362.         self.monster_zones.each_with_index do |card, idx|
  1363.           next if card.nil?
  1364.           state = self.monster_zones_state[idx]
  1365.           next if state.nil? || state[:face_down]
  1366.           if param_id == 2 && state[:position] == :attack
  1367.             total += current_monster_atk(card, "at_field")
  1368.           elsif param_id == 3 && state[:position] == :defense
  1369.             total += current_monster_def(card, "at_field")
  1370.           end
  1371.         end
  1372.       end
  1373.     end
  1374.     return total
  1375.   end
  1376.  
  1377.   def hopt_blocked?(card, effect_name)
  1378.     return false unless card && card.note
  1379.     return false if effect_name.nil? || effect_name == ""
  1380.     self.used_turn_effects ||= []
  1381.     if card.note =~ /<hopt:\s*(.+?)>/i
  1382.       hopt_groups = $1.to_s.split(",").map { |s| s.strip }
  1383.       hopt_groups.each do |group|
  1384.         sub_effs = group.split(/\s*\|\|\s*|\s+or\s+/i).map { |s| s.strip }
  1385.         if sub_effs.include?(effect_name.to_s)
  1386.           return true if sub_effs.any? { |id| self.used_turn_effects.include?(id) }
  1387.         end
  1388.       end
  1389.     end
  1390.     return false
  1391.   end
  1392.  
  1393.   def can_activate_summon_effect?(card)
  1394.     return false unless card
  1395.     return false if card.summon_impact.nil? || card.summon_impact == false || card.summon_impact == ""
  1396.     self.used_turn_effects ||= []
  1397.     return false if hopt_blocked?(card, card.summon_impact)
  1398.     if card.note =~ /<shared_opt:\s*(.+?)>/i
  1399.       shared_name = $1.to_s.strip
  1400.       return false if self.used_turn_effects.include?(shared_name)
  1401.     end
  1402.     return meets_activation_requirements?(card, card.summon_impact)
  1403.   end
  1404.  
  1405.   def can_activate_field_effect?(card)
  1406.     return false unless card
  1407.     return false if card.field_impact && self.state?(18) && leads_to_special_summon?(card.field_impact)
  1408.     return false if card.field_impact.nil? || card.field_impact == false || card.field_impact == ""
  1409.     self.used_turn_effects ||= []
  1410.     eff_str = card.field_impact.to_s
  1411.     return false if hopt_blocked?(card, eff_str)
  1412.     if card.note =~ /<shared_opt:\s*(.+?)>/i
  1413.       shared_name = $1.to_s.strip
  1414.       return false if self.used_turn_effects.include?(shared_name)
  1415.     end
  1416.     return meets_activation_requirements?(card, eff_str)
  1417.   end
  1418.    
  1419.   def used_turn_effects
  1420.     @used_turn_effects ||= []
  1421.   end
  1422.  
  1423.   def custom_req_met?(card)
  1424.     return true unless card
  1425.     if card.note =~ /<custom_req>\s*(.*?)\s*<\/custom_req>/im
  1426.       begin
  1427.         return !!eval($1.to_s)
  1428.       rescue => e
  1429.         p "[ERROR CUSTOM REQ] Carta: #{card.name} | Fallo: #{e.message}"
  1430.         return false
  1431.       end
  1432.     end
  1433.     return true
  1434.   end
  1435.  
  1436.   def meets_activation_requirements?(card, effect_name)
  1437.     return false unless card && effect_name
  1438.     eff_str = effect_name.to_s
  1439.     if card.note =~ /<req_field_counters:\s*#{eff_str}\s*,\s*([^,>]+)\s*,\s*(\d+)>/i
  1440.       c_type = $1.to_s.strip.downcase.to_sym
  1441.       c_amount = $2.to_i
  1442.       return false unless self.get_card_counters(card.object_id, c_type) >= c_amount
  1443.     end
  1444.     if card.note =~ /<req_ss_grave:\s*#{eff_str}\s*,\s*(\d+)(?:\s*,\s*(\d+))?>/i
  1445.       return false unless self.count_legal_ss_targets($1.to_i) >= ($2 ? $2.to_i : 1)
  1446.     end
  1447.     if card.note =~ /<req_deck_subtype:\s*#{eff_str}\s*,\s*(\d+)(?:\s*,\s*(\d+))?>/i
  1448.       return false unless self.deck.count { |c| c && c.subtype == $1.to_i } >= ($2 ? $2.to_i : 1)
  1449.     end
  1450.     if card.note =~ /<req_grave_subtype:\s*#{eff_str}\s*,\s*(\d+)(?:\s*,\s*(\d+))?>/i
  1451.       return false unless self.count_legal_ss_targets($1.to_i) >= ($2 ? $2.to_i : 1)
  1452.     end
  1453.     if card.note =~ /<req_field_other_subtype:\s*#{eff_str}\s*,\s*(\d+)(?:\s*,\s*(\d+))?>/i
  1454.       valid_count = self.monster_zones.compact.count { |c| c.subtype == $1.to_i }
  1455.       req_total = (card.subtype == $1.to_i) ? ($2.to_i + 1) : $2.to_i
  1456.       return false unless valid_count >= req_total
  1457.     end
  1458.     if card.note =~ /<req_dry_run:\s*([^,>]+)\s*,\s*([^,>]+)(?:\s*,\s*([^>]+))?>/i
  1459.       e1, e2, e3 = $1.strip, $2.strip, ($3 ? $3.strip : nil)
  1460.       d1, d2 = PC27::CG::CARD_EFFECT[e1], PC27::CG::CARD_EFFECT[e2]
  1461.       return false unless d1 && d2
  1462.       get_clean_pool = lambda do |zone_sym|
  1463.         pool = []
  1464.         pool += self.hand if [:hand, :hand_or_field, :hand_or_grave, :hand_or_deck].include?(zone_sym)
  1465.         pool += self.grave if [:grave, :hand_or_grave].include?(zone_sym)
  1466.         pool += self.deck if [:deck, :hand_or_deck].include?(zone_sym)
  1467.         pool += self.monster_zones.compact if [:field, :hand_or_field].include?(zone_sym)
  1468.         pool.delete_if { |c| c.id == card.id }
  1469.         pool
  1470.       end
  1471.       p1, p2 = get_clean_pool.call(d1[2]), get_clean_pool.call(d2[2])
  1472.       p1.each do |t1|
  1473.         if match_complex_filter?(t1, d1[4], card, nil)
  1474.           lvl = t1.respond_to?(:level) ? t1.level : nil
  1475.           p2.each do |t2|
  1476.             next if t1 == t2
  1477.             if match_complex_filter?(t2, d2[4], card, lvl)
  1478.               if e3
  1479.                 d3 = PC27::CG::CARD_EFFECT[e3]
  1480.                 return true if get_clean_pool.call(d3[2]).any? { |t3|
  1481.                   t3 != t1 && t3 != t2 && match_complex_filter?(t3, d3[4], card, lvl)
  1482.                 }
  1483.               else
  1484.                 return true
  1485.               end
  1486.             end
  1487.           end
  1488.         end
  1489.       end
  1490.       return false
  1491.     end
  1492.     return true
  1493.   end
  1494.  
  1495.   def match_complex_filter?(item, filter, active_card = nil, simulated_level = nil, zone_index = nil, source_zone = nil)
  1496.     return true if filter.nil? || filter.empty?
  1497.     if filter[0].is_a?(Array)
  1498.       return filter.any? { |f| match_single_complex_filter?(item, f, active_card, simulated_level, zone_index, source_zone) }
  1499.     else
  1500.       return match_single_complex_filter?(item, filter, active_card, simulated_level, zone_index, source_zone)
  1501.     end
  1502.   end
  1503.  
  1504.   def match_single_complex_filter?(item, f, active_card = nil, simulated_level = nil, zone_index = nil, source_zone = nil)
  1505.     begin
  1506.       f = [] if f.nil?
  1507.       f_str = f.map(&:to_s)
  1508.       if f_str.include?("face_up") || f_str.include?("face_down")
  1509.         is_face_up = true
  1510.         has_position = false
  1511.         if source_zone == :hand || source_zone == :deck || source_zone == :grave || source_zone == :banish || source_zone == :extra
  1512.         else
  1513.           if zone_index != nil
  1514.             state = self.monster_zones_state[zone_index]
  1515.             is_face_up = state ? !state[:face_down] : true
  1516.             has_position = true
  1517.           else
  1518.             ($game_party.battle_members + $game_troop.members).each do |battler|
  1519.               next unless battler.respond_to?(:monster_zones)
  1520.               if battler.monster_zones.include?(item)
  1521.                 idx = battler.monster_zones.index(item)
  1522.                 state = battler.monster_zones_state[idx]
  1523.                 is_face_up = state ? !state[:face_down] : true
  1524.                 has_position = true
  1525.                 break
  1526.               end
  1527.             end
  1528.           end
  1529.         end
  1530.         if has_position
  1531.           return false if f_str.include?("face_up") && !is_face_up
  1532.           return false if f_str.include?("face_down") && is_face_up
  1533.         end
  1534.       end
  1535.       return false unless (f[0] || 0).to_s == "0" || (item.attribute rescue 0).to_s == f[0].to_s
  1536.       return false unless (f[1] || 0).to_s == "0" || (item.type rescue 0).to_s == f[1].to_s
  1537.       return false unless (f[2] || 0).to_s == "0" || (item.rarity rescue 0).to_s == f[2].to_s
  1538.       if (f[3] || 0).to_s != "0" || f[3].is_a?(String)
  1539.         item_sub = item.respond_to?(:subtype) ? item.subtype : nil
  1540.         item_sub = 0 if item_sub.nil?
  1541.         f3_str = f[3].to_s
  1542.         is_not = f3_str.start_with?("!")
  1543.         target_val = is_not ? f3_str[1..-1] : f3_str
  1544.         if item_sub.is_a?(Array)
  1545.           res = item_sub.map(&:to_s).include?(target_val)
  1546.         else
  1547.           res = item_sub.to_s == target_val
  1548.         end
  1549.         return false if is_not && res
  1550.         return false if !is_not && !res
  1551.       end
  1552.       f4 = f[4] || 0
  1553.       if f4.to_s != "0"
  1554.         target_lvl = simulated_level || ($chain_memory_card ? $chain_memory_card.level : 0)
  1555.         check_val = (f4 == :destroyed_level) ? target_lvl : f4
  1556.         if check_val.is_a?(Range)
  1557.           return false unless item.respond_to?(:level) && check_val.include?(item.level.to_i)
  1558.         elsif check_val.to_s != "0"
  1559.           return false unless item.respond_to?(:level) && item.level.to_s == check_val.to_s
  1560.         end
  1561.       end
  1562.       return false unless (f[5] || 0).to_s == "0" || (item.normeff rescue 0).to_s == f[5].to_s
  1563.       return false unless (f[6] || 0).to_s == "0" || (item.frame rescue 0).to_s == f[6].to_s
  1564.       if (f[7] || 0).to_s != "0"
  1565.         item_arch = item.respond_to?(:archetype) ? item.archetype : nil
  1566.         return false if item_arch.nil?
  1567.         if item_arch.is_a?(Array)
  1568.           return false unless item_arch.map(&:to_s).include?(f[7].to_s)
  1569.         else
  1570.           return false unless item_arch.to_s == f[7].to_s
  1571.         end
  1572.       end
  1573.       return false unless (f[8] || 0).to_s == "0" || (item.atk rescue 0).to_s == f[8].to_s
  1574.       return false unless (f[9] || 0).to_s == "0" || (item.def rescue 0).to_s == f[9].to_s
  1575.       return false unless (f[10]|| 0).to_s == "0" || (item.special rescue 0).to_s == f[10].to_s
  1576.       if f[11] == :exclude_active
  1577.         ref = active_card || $temp_active_card
  1578.         return false if ref && item.id == ref.id
  1579.       end
  1580.       return true
  1581.     rescue => e
  1582.       p "[ERROR EN FILTRO] Carta: #{item.name} | Fallo: #{e.message}"
  1583.       return false
  1584.     end
  1585.   end
  1586.  
  1587.   #--------------------------------------------------------------------------
  1588.   # Verifica si hay blancos legales para Special Summon en una zona
  1589.   #--------------------------------------------------------------------------
  1590.   def has_legal_spec_summon_target?(zone_type, subtype)
  1591.     cards_to_check = case zone_type
  1592.                       when :grave then self.grave
  1593.                       when :hand  then self.hand
  1594.                       when :banish  then self.banish
  1595.                       when :deck  then self.deck
  1596.                       when :extra  then self.extra
  1597.                      else []
  1598.                      end
  1599.     cards_to_check.any? do |card|
  1600.       next false unless card && card.subtype == subtype
  1601.       is_nomi = (card.note =~ /<special_only:/i)
  1602.       if is_nomi
  1603.         card.properly_summoned == true
  1604.       else
  1605.         true
  1606.       end
  1607.     end
  1608.   end
  1609.  
  1610.   def count_legal_ss_targets(subtype)
  1611.     targets = self.grave.select do |card|
  1612.       next false unless card && card.subtype == subtype
  1613.       if card.note =~ /<special_only:/i
  1614.         card.properly_summoned == true
  1615.       else
  1616.         true
  1617.       end
  1618.     end
  1619.     return targets.size
  1620.   end  
  1621.   #-----------------------------------------------------------------------------
  1622.   # Adds Hand, Deck, Grave, Banish, Extra, Field and Stack to an Actor (alias)
  1623.   #-----------------------------------------------------------------------------
  1624.   alias pc27_cardgame_gameactor_setup                                      setup
  1625.   def setup(actor_id)
  1626.     pc27_cardgame_gameactor_setup(actor_id)
  1627.     @current_zone = :hand
  1628.     setup_start_deck(actor_id)
  1629.     @hand   = []
  1630.     @grave  = []
  1631.     @banish = []
  1632.     @extra  = []
  1633.     @field  = []
  1634.     @stack  = []
  1635.     @monster_zones = Array.new(5, nil)
  1636.     @monster_zones_state = Array.new(5) { { position: :attack, face_down: false } }
  1637.     @st_zones      = Array.new(5, nil)
  1638.     @field_zone    = nil
  1639.     @turn_actions = { :can_attack => true,
  1640.                       :can_guard  => true,
  1641.                       :actions    =>    0, }
  1642.   end
  1643.                      
  1644.   #-----------------------------------------------------------------------------
  1645.   # Setup for the initial deck (new)
  1646.   #-----------------------------------------------------------------------------
  1647.   def setup_start_deck(actor_id)
  1648.     @deck  = []
  1649.     if PC27::CG::START_DECKS.has_key?(actor_id)
  1650.       PC27::CG::START_DECKS[actor_id].each { |card| @deck.push($data_skills[card]) }
  1651.     end
  1652.   end
  1653.  
  1654.   #-----------------------------------------------------------------------------
  1655.   # Setup for the initial extra (new)
  1656.   #-----------------------------------------------------------------------------
  1657.   def setup_start_extra(actor_id)
  1658.     @extra  = []
  1659.     if PC27::CG::START_EXTRA.has_key?(actor_id)
  1660.       PC27::CG::START_EXTRA[actor_id].each { |card| @extra.push($data_skills[card]) }
  1661.     end
  1662.   end
  1663.  
  1664.   #-----------------------------------------------------------------------------
  1665.   # Draws n cards from the deck (new)
  1666.   #-----------------------------------------------------------------------------
  1667.   def draw_cards(n, show = true, sound = true)
  1668.     is_interrupt = false
  1669.     if $game_party && $game_party.in_battle && SceneManager.scene_is?(Scene_Battle)
  1670.       if SceneManager.scene.instance_variable_get(:@is_interrupt_effect)
  1671.         show = false
  1672.         is_interrupt = true
  1673.       end
  1674.     end
  1675.     drawn_cards = []
  1676.     begin
  1677.       n.times do
  1678.         if @deck.length == 0
  1679.           add_state(1) if PC27::CG::DECK_EMPTY == 1
  1680.           return if PC27::CG::DECK_EMPTY == 1
  1681.           if @grave.length == 0
  1682.             add_state(1) if PC27::CG::DECK_EMPTY == 2
  1683.             return
  1684.           else
  1685.             revive_grave
  1686.             revive_penalty if PC27::CG::REVIVE_PENALTY[0] > 0
  1687.           end
  1688.         end
  1689.         skill = @deck.shift
  1690.         @hand.push(skill)
  1691.         drawn_cards << skill
  1692.       end
  1693.       if $game_party.in_battle && SceneManager.scene_is?(Scene_Battle)
  1694.         scene = SceneManager.scene
  1695.         scene.instance_variable_get(:@status_window).refresh rescue nil
  1696.       end
  1697.       if $game_party && $game_party.in_battle && SceneManager.scene_is?(Scene_Battle) && drawn_cards.size > 0
  1698.         scene = SceneManager.scene
  1699.         sprites = []
  1700.         center_x = Graphics.width / 2
  1701.         center_y = Graphics.height / 2
  1702.         total_cards = drawn_cards.size
  1703.         spacing = [80, 400 / total_cards].min
  1704.         start_x = center_x - ((total_cards - 1) * spacing) / 2
  1705.         drawn_cards.each_with_index do |skill, i|
  1706.           sprite = Sprite.new
  1707.           begin
  1708.             sprite.bitmap = Cache.picture("Card#{skill.id}")
  1709.           rescue
  1710.             sprite.bitmap = Bitmap.new(100, 140)
  1711.             sprite.bitmap.fill_rect(0, 0, 100, 140, Color.new(255, 255, 255))
  1712.           end
  1713.           sprite.ox = sprite.bitmap.width / 2 rescue 0
  1714.           sprite.oy = sprite.bitmap.height / 2 rescue 0
  1715.           sprite.z = 10050 + i
  1716.           dest_x = start_x + (i * spacing)
  1717.           dest_y = center_y
  1718.           sprite.x = dest_x
  1719.           sprite.y = -150 - (i * 150)
  1720.           sprites << { sprite: sprite, dest_x: dest_x, dest_y: dest_y, skill: skill }
  1721.         end
  1722.         falling = true
  1723.         while falling
  1724.           falling = false
  1725.           sprites.each do |data|
  1726.             s = data[:sprite]
  1727.             if s.y < data[:dest_y]
  1728.               s.y += [(data[:dest_y] - s.y) * 0.25, 15].max
  1729.               s.y = data[:dest_y] if s.y > data[:dest_y]
  1730.               falling = true if s.y < data[:dest_y]
  1731.             end
  1732.           end
  1733.           scene.update_basic if falling
  1734.         end
  1735.         45.times { scene.update_basic }
  1736.         if self.actor?
  1737.           target_x = self.screen_x rescue center_x
  1738.           target_y = self.screen_y rescue Graphics.height
  1739.         else
  1740.           target_x = self.screen_x rescue center_x
  1741.           target_y = (self.screen_y - 50) rescue -50
  1742.         end
  1743.         sprites.reverse.each do |data|
  1744.           s = data[:sprite]
  1745.           RPG::SE.new(*PC27::CG::DRAW_SOUND).play if sound
  1746.           15.times do |frame|
  1747.             progress = (frame + 1) / 15.0
  1748.             s.x = data[:dest_x] + (target_x - data[:dest_x]) * progress
  1749.             s.y = data[:dest_y] + (target_y - data[:dest_y]) * progress
  1750.             s.zoom_x = 1.0 - progress
  1751.             s.zoom_y = 1.0 - progress
  1752.             s.opacity = 255 * (1.0 - progress)
  1753.             scene.update_basic
  1754.           end
  1755.         end
  1756.         sprites.each do |data|
  1757.           data[:sprite].bitmap.dispose if data[:sprite].bitmap
  1758.           data[:sprite].dispose
  1759.         end
  1760.       end
  1761.       if show
  1762.         drawn_cards.each do |skill|
  1763.           text = @name + " " + PC27::CG::DRAW_MESSAGE + " " + skill.name
  1764.           if $game_party && $game_party.in_battle && SceneManager.scene_is?(Scene_Battle)
  1765.             begin
  1766.               SceneManager.scene.log_window.add_text(text)
  1767.             rescue
  1768.             end
  1769.           else
  1770.             $game_message.background = 1
  1771.             $game_message.position   = 1
  1772.             $game_message.add(text)
  1773.           end
  1774.         end
  1775.       end
  1776.     ensure
  1777.       if is_interrupt && SceneManager.scene_is?(Scene_Battle)
  1778.         SceneManager.scene.instance_variable_set(:@is_interrupt_effect, false)
  1779.       end
  1780.     end
  1781.   end
  1782.  
  1783.   #-----------------------------------------------------------------------------
  1784.   # Puts all cards from your grave back into your deck (new)
  1785.   #-----------------------------------------------------------------------------
  1786.   def revive_grave(show = true)
  1787.     @deck += @grave
  1788.     @grave = []
  1789.     shuffle_deck(false)
  1790.     if show
  1791.       $game_message.background = 1
  1792.       $game_message.position = 1
  1793.       text = @name + PC27::CG::REVIVE_MSG
  1794.       $game_message.add(text)
  1795.     end
  1796.   end
  1797.  
  1798.   #-----------------------------------------------------------------------------
  1799.   # Revive penalty (new)
  1800.   #-----------------------------------------------------------------------------
  1801.   def revive_penalty
  1802.     percent = PC27::CG::REVIVE_PENALTY[0] * 0.01
  1803.     case PC27::CG::REVIVE_PENALTY[1]
  1804.     when :hp
  1805.       @hp  -= (@hp  * percent).round
  1806.     when :mhp
  1807.       @hp  -= (@mhp * percent).round
  1808.     when :mp
  1809.       @mp  -= (@mp  * percent).round
  1810.     when :mmp
  1811.       @mp  -= (@mmp * percent).round
  1812.     end
  1813.     RPG::SE.new(*PC27::CG::PENALTY_SOUND).play
  1814.   end
  1815.  
  1816.   #-----------------------------------------------------------------------------
  1817.   # Shuffles the deck (Ahora con soporte Visual)
  1818.   #-----------------------------------------------------------------------------
  1819.   def shuffle_deck(sound = true)
  1820.     if sound
  1821.       if SceneManager.scene_is?(Scene_Battle)
  1822.         if SceneManager.scene.instance_variable_get(:@deck_was_searched) && !@is_official_shuffle
  1823.           @deck = @deck.shuffle
  1824.           return
  1825.         end
  1826.         SceneManager.scene.show_deck_shuffle_visuals
  1827.       else
  1828.         5.times do
  1829.           RPG::SE.new(PC27::CG::DRAW_SOUND[0], 100, 150).play rescue nil
  1830.           20.times { Graphics.update; Input.update }
  1831.           RPG::SE.new(PC27::CG::DRAW_SOUND[0], 100, 100).play rescue nil
  1832.           10.times { Graphics.update; Input.update }
  1833.         end
  1834.       end
  1835.     end
  1836.     @deck = @deck.shuffle
  1837.   end
  1838.  
  1839.   #-----------------------------------------------------------------------------
  1840.   # Sorts Hand, Deck or Grave (new)
  1841.   #-----------------------------------------------------------------------------
  1842.   def sort_cards(location)
  1843.     case location
  1844.     when :deck
  1845.       @deck.sort!{|a,b| a.card_id <=> b.card_id}
  1846.     when :hand
  1847.       @hand.sort!{|a,b| a.card_id <=> b.card_id}
  1848.     when :grave
  1849.       @grave.sort!{|a,b| a.card_id <=> b.card_id}
  1850.     when :banish
  1851.       @banish.sort!{|a,b| a.card_id <=> b.card_id}
  1852.     when :extra
  1853.       @extra.sort!{|a,b| a.card_id <=> b.card_id}
  1854.     when :field
  1855.       @field.sort!{|a,b| a.card_id <=> b.card_id}
  1856.     end
  1857.   end
  1858.  
  1859.   #-----------------------------------------------------------------------------
  1860.   # Tries to put a specific card from your hand to your grave (new)
  1861.   #-----------------------------------------------------------------------------
  1862.   def card_to_grave?(id)
  1863.     for i in (0...@hand.length)
  1864.       if @hand[i].id == id
  1865.         @grave.push(@hand[i])
  1866.         @hand.delete_at(i)
  1867.         return true
  1868.       end
  1869.     end
  1870.     return false
  1871.   end
  1872.  
  1873.   #-----------------------------------------------------------------------------
  1874.   # Tries to put a specific card from your deck to your grave (new)
  1875.   #-----------------------------------------------------------------------------
  1876.   def card_from_deck_to_grave?(id)
  1877.     for i in (0...@deck.length)
  1878.       if @deck[i].id == id
  1879.         @grave.push(@deck[i])
  1880.         @deck.delete_at(i)
  1881.         return true
  1882.       end
  1883.     end
  1884.     return false
  1885.   end
  1886.  
  1887.   #-----------------------------------------------------------------------------
  1888.   # Tries to put a specific card from your hand to your banish (new)
  1889.   #-----------------------------------------------------------------------------
  1890.   def card_to_banish?(id)
  1891.     for i in (0...@hand.length)
  1892.       if @hand[i].id == id
  1893.         @banish.push(@hand[i])
  1894.         @hand.delete_at(i)
  1895.         return true
  1896.       end
  1897.     end
  1898.     return false
  1899.   end
  1900.  
  1901.   #-----------------------------------------------------------------------------
  1902.   # Tries to put a specific card from your hand to your stack (new)
  1903.   #-----------------------------------------------------------------------------
  1904.   def card_to_stack?(id)
  1905.     for i in (0...@hand.length)
  1906.       if @hand[i].id == id
  1907.         @stack.push(@hand[i])
  1908.         @hand.delete_at(i)
  1909.         return true
  1910.       end
  1911.     end
  1912.     return false
  1913.   end
  1914.  
  1915.   #-----------------------------------------------------------------------------
  1916.   # Tries to put a specific card from your field to your stack (new)
  1917.   #-----------------------------------------------------------------------------
  1918.   def card_to_field?(id)
  1919.     init_card_zones
  1920.     index_hand = @hand.index { |c| c.id == id }
  1921.     return false unless index_hand
  1922.     card = @hand[index_hand]
  1923.     index_field = @monster_zones.index(nil)
  1924.     return false unless index_field
  1925.     @monster_zones[index_field] = card
  1926.     @hand.delete_at(index_hand)
  1927.     return index_field
  1928.   end
  1929.  
  1930.   def card_face_down?(card)
  1931.     index = @monster_zones.index(card)
  1932.     return false unless index
  1933.     state = @monster_zones_state[index]
  1934.     return state ? state[:face_down] : false
  1935.   end
  1936.  
  1937.   def card_face_up?(card)
  1938.     index = @monster_zones.index(card)
  1939.     return false unless index
  1940.     state = @monster_zones_state[index]
  1941.     return state ? !state[:face_down] : true
  1942.   end
  1943.  
  1944.   #-----------------------------------------------------------------------------
  1945.   # Tries to put a specific spell/trap from hand to field (new)
  1946.   #-----------------------------------------------------------------------------
  1947.   def PRUEBAspell_to_field?(card)
  1948.     init_card_zones
  1949.     index_hand = @hand.index { |c| c.equal?(card) }
  1950.     return false unless index_hand
  1951.     if card.subtype == 27
  1952.       if @field_zone != nil
  1953.         @grave.push(@field_zone)
  1954.         @field.delete(@field_zone)
  1955.       end
  1956.       @field_zone = card
  1957.       @field.push(card)
  1958.       @hand.delete_at(index_hand)
  1959.       @field_zone_state ||= {}
  1960.       @field_zone_state[:face_down] = false
  1961.       @field_zone_state[:set_this_turn] = false
  1962.       return :field
  1963.     else
  1964.       index_field = @st_zones.index(nil)
  1965.       return false unless index_field
  1966.       @st_zones[index_field] = card
  1967.       @field.push(card)
  1968.       @hand.delete_at(index_hand)
  1969.       @st_zones_state ||= []
  1970.       @st_zones_state[index_field] ||= {}
  1971.       @st_zones_state[index_field][:face_down] = false
  1972.       @st_zones_state[index_field][:set_this_turn] = false
  1973.       return index_field
  1974.     end
  1975.   end
  1976.  
  1977.   def spell_to_field?(card)
  1978.     if card.subtype == 27
  1979.       idx_hand = self.hand.index { |c| c && c.equal?(card) }
  1980.       if self.field_zone != nil
  1981.         self.grave.push(self.field_zone)
  1982.         self.field.delete(self.field_zone)
  1983.       end
  1984.       self.hand.delete_at(idx_hand) if idx_hand
  1985.       self.field_zone = card
  1986.       self.field.push(card)
  1987.       self.field_zone_state = { face_down: false, set_this_turn: false }
  1988.       return :field
  1989.     else
  1990.       idx = self.st_zones.index(nil)
  1991.       idx_hand = self.hand.index { |c| c && c.equal?(card) }
  1992.       if idx && idx_hand
  1993.         self.hand.delete_at(idx_hand)
  1994.         self.st_zones[idx] = card
  1995.         self.field.push(card)
  1996.         self.st_zones_state ||= []
  1997.         self.st_zones_state[idx] = { face_down: false, set_this_turn: false }
  1998.         return idx
  1999.       end
  2000.     end
  2001.     return nil
  2002.   end
  2003.  
  2004.   #-----------------------------------------------------------------------------
  2005.   # Tries to put a specific card from your field to your graveyard (new)
  2006.   #-----------------------------------------------------------------------------
  2007.   def field_to_grave?(id)
  2008.     for i in (0...@field.length)
  2009.       if @field[i].id == id
  2010.         @grave.push(@field[i])
  2011.         @field.delete_at(i)
  2012.         return true
  2013.       end
  2014.     end
  2015.     return false
  2016.   end
  2017.  
  2018.   #-----------------------------------------------------------------------------
  2019.   # Puts n random cards from your field to your grave (new)
  2020.   #-----------------------------------------------------------------------------
  2021.   def send_field(n)
  2022.     amount_send_field = 0
  2023.     if n == 0
  2024.       amount_send_field += @field.size
  2025.       @grave += @field
  2026.       @field = []
  2027.     else
  2028.       n.times do
  2029.         break if @field.size == 0
  2030.         @grave.push(@field.slice!(rand(@field.size-1)))
  2031.         amount_send_field += 1
  2032.       end
  2033.     end
  2034.   end
  2035.  
  2036.   #-----------------------------------------------------------------------------
  2037.   # Puts n random card(s) TYPE from your field to your grave (new)
  2038.   #-----------------------------------------------------------------------------
  2039.   def send_card_from_field(n)
  2040.     for i in (0...@field.length)
  2041.       if @field[i].type == n
  2042.         @grave.push(@field[i])
  2043.         @field.delete_at(i)
  2044.         return true
  2045.       end
  2046.     end
  2047.     return false
  2048.   end
  2049.  
  2050.   #-----------------------------------------------------------------------------
  2051.   # Puts n random cards from your hand to your grave (new)
  2052.   #-----------------------------------------------------------------------------
  2053.   def throw_cards(n)
  2054.     amount_thrown_cards = 0
  2055.     if n == 0
  2056.       amount_thrown_cards += @hand.size
  2057.       @grave += @hand
  2058.       @hand = []
  2059.     else
  2060.       n.times do
  2061.         break if @hand.size == 0
  2062.         @grave.push(@hand.slice!(rand(@hand.size-1)))
  2063.         amount_thrown_cards += 1
  2064.       end
  2065.     end
  2066.     $game_variables[46] = amount_thrown_cards
  2067.     return amount_thrown_cards
  2068.   end
  2069.  
  2070.   #-----------------------------------------------------------------------------
  2071.   # Mill n random cards from your deck to your grave (new)
  2072.   #-----------------------------------------------------------------------------
  2073.   def mill_cards(n)
  2074.     amount_mill_cards = 0
  2075.     if n == 0
  2076.       amount_mill_cards += @deck.size
  2077.       @grave += @deck
  2078.       @deck = []
  2079.     else
  2080.       n.times do
  2081.         break if @deck.size == 0
  2082.         @grave.push(@deck.slice!(rand(@deck.size-1)))
  2083.         amount_mill_cards += 1
  2084.       end
  2085.     end
  2086.   end
  2087.  
  2088.   #-----------------------------------------------------------------------------
  2089.   # Puts n random cards from your hand to your banish (new)
  2090.   #-----------------------------------------------------------------------------
  2091.   def banish_cards(n)
  2092.     amount_banish_cards = 0
  2093.     if n == 0
  2094.       amount_banish_cards += @hand.size
  2095.       @banish += @hand
  2096.       @hand = []
  2097.     else
  2098.       n.times do
  2099.         break if @hand.size == 0
  2100.         @banish.push(@hand.slice!(rand(@hand.size-1)))
  2101.         amount_banish_cards += 1
  2102.       end
  2103.     end
  2104.     $game_variables[51] = amount_banish_cards
  2105.     return amount_banish_cards
  2106.   end
  2107.  
  2108.   #-----------------------------------------------------------------------------
  2109.   # Mill n random cards from your deck to your banish (new)
  2110.   #-----------------------------------------------------------------------------
  2111.   def mill_banish_cards(n)
  2112.     amount_mill_banish_cards = 0
  2113.     if n == 0
  2114.       amount_mill_banish_cards += @deck.size
  2115.       @banish += @deck
  2116.       @deck = []
  2117.     else
  2118.       n.times do
  2119.         break if @deck.size == 0
  2120.         @banish.push(@deck.slice!(rand(@deck.size-1)))
  2121.         amount_mill_banish_cards += 1
  2122.       end
  2123.     end
  2124.   end
  2125.  
  2126.   #-----------------------------------------------------------------------------
  2127.   # Banish n random cards from your extra (new)
  2128.   #-----------------------------------------------------------------------------
  2129.   def banish_extra_cards(n)
  2130.     amount_banish_extra_cards = 0
  2131.     if n == 0
  2132.       amount_banish_extra_cards += @extra.size
  2133.       @banish += @extra
  2134.       @extra = []
  2135.     else
  2136.       n.times do
  2137.         break if @extra.size == 0
  2138.         @banish.push(@extra.slice!(rand(@extra.size-1)))
  2139.         amount_banish_extra_cards += 1
  2140.       end
  2141.     end
  2142.   end
  2143.  
  2144.   #-----------------------------------------------------------------------------
  2145.   # Replace Field Spell
  2146.   #-----------------------------------------------------------------------------
  2147.   def replace_field_spell(id)
  2148.     for i in (0...@field.length)
  2149.       if @field[i].subtype == id
  2150.         @grave.push(@field[i])
  2151.         @field.delete_at(i)
  2152.         return true
  2153.       end
  2154.     end
  2155.     return false
  2156.   end
  2157.  
  2158.   #-----------------------------------------------------------------------------
  2159.   # Shuffles Deck and draws cards at the begin of each battle (new)
  2160.   #-----------------------------------------------------------------------------
  2161.   def on_battle_start
  2162.     super()
  2163.     shuffle_deck(false)
  2164.     draw_cards(starting_cards, false, false)
  2165.     @turn_actions[:can_attack] = true
  2166.     @turn_actions[:can_guard]  = true
  2167.     @turn_actions[:actions]    = available_actions
  2168.   end
  2169.  
  2170.   #-----------------------------------------------------------------------------
  2171.   # Starting Cards (new)
  2172.   #-----------------------------------------------------------------------------
  2173.   def starting_cards
  2174.     x = PC27::CG::START_CARDS
  2175.     @equips.each { |equip|
  2176.       next unless equip
  2177.       next unless equip.object
  2178.       x += equip.object.extra_start_cards
  2179.     }
  2180.     return x
  2181.   end
  2182.  
  2183.   #-----------------------------------------------------------------------------
  2184.   # Draws a card at the end of each turn (new)
  2185.   #-----------------------------------------------------------------------------
  2186.   def on_turn_end
  2187.     super()
  2188.     if self.respond_to?(:used_turn_effects) && self.used_turn_effects
  2189.       self.used_turn_effects.clear
  2190.       puts "[SISTEMA] Memoria de turnos limpiada para #{self.name} (on_turn_end)"
  2191.     end
  2192.     if self.monster_zones_state
  2193.       self.monster_zones_state.each do |state|
  2194.         state[:changed_this_turn] = false if state
  2195.       end
  2196.     end
  2197.     if self.respond_to?(:st_zones_state) && self.st_zones_state
  2198.       self.st_zones_state.each do |state|
  2199.         state[:set_this_turn] = false if state
  2200.       end
  2201.     end
  2202.     if $game_party.in_battle && !death_state?
  2203.       cannot_draw_state = self.states.any? { |state| state.note =~ /<cannot_draw>/i }
  2204.       unless cannot_draw_state
  2205.         draw_cards(turn_cards, true, false)
  2206.         valid_field_cards = []
  2207.         if self.respond_to?(:st_zones) && self.st_zones
  2208.           self.st_zones.each_with_index do |card, i|
  2209.             if card
  2210.               st_state = self.st_zones_state[i] rescue nil
  2211.               is_fd = st_state ? st_state[:face_down] : false
  2212.               valid_field_cards << card unless is_fd
  2213.             end
  2214.           end
  2215.         end
  2216.         if self.respond_to?(:field_zone) && self.field_zone
  2217.           f_state = self.field_zone_state rescue nil
  2218.           f_is_fd = f_state ? f_state[:face_down] : false
  2219.           valid_field_cards << self.field_zone unless f_is_fd
  2220.         end
  2221.         if valid_field_cards.any?
  2222.           PC27::LunaticCards.run_effects(valid_field_cards, self, :field, :ondrawphase, true)
  2223.         end
  2224.       end
  2225.       @turn_actions[:can_attack] = true
  2226.       @turn_actions[:can_guard]  = true
  2227.       @turn_actions[:actions]    = available_actions
  2228.     end
  2229.   end
  2230.  
  2231.   #-----------------------------------------------------------------------------
  2232.   # Determines the amount of actions an actor can make (new)
  2233.   #-----------------------------------------------------------------------------
  2234.   def available_actions
  2235.     for id in @states
  2236.       state = $data_states[id]
  2237.       if state.limited_actions != 0
  2238.         x = state.limited_actions if x.nil? || x > state.limited_actions
  2239.       end
  2240.     end
  2241.     if PC27::CG::MAX_ACTIONS <= 0
  2242.       return (x ? x : -1)
  2243.     else
  2244.       return (x ? [x, PC27::CG::MAX_ACTIONS].min : PC27::CG::MAX_ACTIONS)
  2245.     end
  2246.   end
  2247.  
  2248.   #-----------------------------------------------------------------------------
  2249.   # Turn Cards(new)
  2250.   #-----------------------------------------------------------------------------
  2251.   def turn_cards
  2252.     x = 1
  2253.     @equips.each { |equip|
  2254.       next unless equip
  2255.       next unless equip.object
  2256.       x += equip.object.extra_turn_cards
  2257.       if state?(82)
  2258.         x = 0
  2259.       end
  2260.     }
  2261.     return x
  2262.   end
  2263.  
  2264.   #-----------------------------------------------------------------------------
  2265.   # Restore deck at the end of battle (new)
  2266.   #-----------------------------------------------------------------------------
  2267.   def on_battle_end
  2268.     actual_field_cards = []
  2269.     actual_field_cards += @monster_zones.compact if @monster_zones
  2270.     actual_field_cards += @st_zones.compact if @st_zones
  2271.     if self.respond_to?(:card_in_zone)
  2272.       f_card = self.card_in_zone(:fieldspell, 0) rescue nil
  2273.       actual_field_cards << f_card if f_card
  2274.     end
  2275.     all_field_cards = (actual_field_cards + (@field || [])).uniq
  2276.     @deck += @hand + @grave + @banish + all_field_cards
  2277.     @deck.select! { |card| card.frame != 5 }
  2278.     @deck.select! { |card| card.type != 4 }
  2279.     @extra += @hand + @grave + @banish + all_field_cards
  2280.     @extra.select! { |card| card.type == 4 }
  2281.     @hand, @grave, @banish, @field = [], [], [], []
  2282.     @monster_zones = Array.new(5, nil) if @monster_zones
  2283.     @st_zones = Array.new(5, nil) if @st_zones
  2284.     if @monster_zones_state
  2285.       @monster_zones_state = Array.new(5) { { face_down: false, position: :attack, changed_this_turn: false } }
  2286.     end
  2287.     if @st_zones_state
  2288.       @st_zones_state = Array.new(5) { { face_down: false, set_this_turn: false } }
  2289.     end
  2290.     if @field_zone_state
  2291.       @field_zone_state = { face_down: false, set_this_turn: false }
  2292.     end
  2293.     @field_zone = nil if defined?(@field_zone)
  2294.     @field_spell = nil if defined?(@field_spell)
  2295.     super()
  2296.   end
  2297.  
  2298.   #--------------------------------------------------------------------------
  2299.   # * Determine Usability of Attack
  2300.   #--------------------------------------------------------------------------
  2301.   def attack_usable?
  2302.     super && @turn_actions[:can_attack]
  2303.   end
  2304.  
  2305.   #--------------------------------------------------------------------------
  2306.   # * Determine Usability of Guard
  2307.   #--------------------------------------------------------------------------
  2308.   def guard_usable?
  2309.     super && @turn_actions[:can_guard]
  2310.   end
  2311.  
  2312. end
  2313.  
  2314. #===============================================================================
  2315. # Game_Party
  2316. #===============================================================================
  2317.  
  2318. class Game_Party < Game_Unit
  2319.  
  2320.   #-----------------------------------------------------------------------------
  2321.   # Attributes
  2322.   #-----------------------------------------------------------------------------
  2323.   attr_accessor :coll
  2324.    
  2325.   #-----------------------------------------------------------------------------
  2326.   # Adds a card collection to the party (alias)
  2327.   #-----------------------------------------------------------------------------
  2328.   alias pc27_gameparty_initialize                                     initialize
  2329.   def initialize
  2330.     pc27_gameparty_initialize
  2331.     @coll = []
  2332.   end
  2333.  
  2334. end
  2335.  
  2336. #==============================================================================
  2337. # Window_CardZoneLabel
  2338. #==============================================================================
  2339.  
  2340. class Window_CardZoneLabel < Window_Base
  2341.   def initialize(x, y)
  2342.     super(x, y, window_width, window_height)
  2343.     @text = ""
  2344.     refresh
  2345.     @icon_index = nil
  2346.   end
  2347.  
  2348.   def window_width
  2349.     120
  2350.   end
  2351.  
  2352.   def window_height
  2353.     fitting_height(1)
  2354.   end
  2355.  
  2356.   def set_text(text, icon_index = nil)
  2357.     return if @text == text && @icon_index == icon_index
  2358.     @text = text
  2359.     @icon_index = icon_index
  2360.     refresh
  2361.   end
  2362.  
  2363.   def standard_padding
  2364.     6
  2365.   end
  2366.  
  2367.   def refresh
  2368.     contents.clear
  2369.     contents.font.size = 22
  2370.     contents.font.bold = false
  2371.     icon_w = @icon_index ? 24 : 0
  2372.     text_w = text_size(@text).width
  2373.     w = icon_w + text_w + standard_padding * 2 + 8
  2374.     h = line_height + standard_padding * 2
  2375.     self.width  = w
  2376.     self.height = h
  2377.     create_contents
  2378.     x = 0
  2379.     if @icon_index
  2380.       draw_icon(@icon_index, x, 0)
  2381.       x += 24 + 4
  2382.     end
  2383.     contents.font.italic = false
  2384.     contents.font.color = text_color(0)
  2385.     draw_text(x, 0, contents.width - x, line_height, @text)
  2386.   end
  2387.  
  2388. end
  2389.  
  2390. #===============================================================================
  2391. # Window_SkillList
  2392. #===============================================================================
  2393. class Window_SkillList < Window_Selectable
  2394.   attr_accessor :cursor_opacity
  2395.  
  2396.   def initialize(x, y, width, height)
  2397.     super(x, y, Graphics.width, 300)
  2398.     @data = []
  2399.     @last_selected_index = nil
  2400.     @highlight_counter = 0
  2401.     make_item_list
  2402.     refresh
  2403.   end
  2404.  
  2405.   def create_contents
  2406.     self.contents.dispose if self.contents
  2407.     self.contents = Bitmap.new(
  2408.       width - padding * 2,
  2409.       height - padding * 2
  2410.     )
  2411.   end
  2412.  
  2413.   def make_item_list
  2414.     @actor ||= $game_party.battle_members[0]
  2415.     return @data = [] if @actor.nil?
  2416.     case @actor.current_zone
  2417.     when :hand
  2418.       @actor.sort_cards(:hand)
  2419.       @data = @actor.hand || []
  2420.     when :field
  2421.       @data = @actor.field || []
  2422.     when :graveyard
  2423.       @data = @actor.grave || []
  2424.     when :banish
  2425.       @data = @actor.banish || []
  2426.     when :extra
  2427.       @data = @actor.extra || []
  2428.     when :deck
  2429.       @data = @actor.deck || []
  2430.     else
  2431.       @data = @actor.hand || []
  2432.     end
  2433.     if @data.empty?
  2434.       self.index = -1 if respond_to?(:index=)
  2435.     else
  2436.       self.index = 0 if respond_to?(:index=) && (self.index.nil? || self.index < 0 || self.index >= @data.size)
  2437.     end
  2438.   end
  2439.  
  2440.   def refresh
  2441.     contents.clear if contents
  2442.     return if @data.nil? || @data.empty?
  2443.     selected_index = self.index
  2444.     selected_index = nil if selected_index.nil? || selected_index < 0 || selected_index >= @data.size
  2445.     unless selected_index
  2446.       (0...@data.size).to_a.reverse.each do |index|
  2447.         draw_item(index)
  2448.       end
  2449.       return
  2450.     end
  2451.     left_count = selected_index
  2452.     right_count = @data.size - selected_index - 1
  2453.     max_steps = [left_count, right_count].max
  2454.     (0..max_steps).each do |step|
  2455.       left_index = step
  2456.       draw_item(left_index) if left_index < selected_index
  2457.       right_index = @data.size - step - 1
  2458.       draw_item(right_index) if right_index > selected_index
  2459.     end
  2460.     draw_item(selected_index)
  2461.     draw_highlight(selected_index) if selected_index
  2462.     @last_selected_index = selected_index
  2463.   end
  2464.  
  2465.   def draw_item(index)
  2466.     card = @data[index]
  2467.     return unless card
  2468.     rect = item_rect(index)
  2469.     draw_card_illustration(card, rect.x, rect.y)
  2470.     is_grave_or_banish = false
  2471.     if self.is_a?(Window_CardSelect) && @zone_tags && @zone_tags[index]
  2472.       src = @zone_tags[index]
  2473.       is_grave_or_banish = true if src == :grave || src == :banish
  2474.     elsif @actor && [:graveyard, :grave, :banish].include?(@actor.current_zone)
  2475.       is_grave_or_banish = true
  2476.     end
  2477.     if is_grave_or_banish
  2478.       is_nomi = (card.note =~ /<special_only:/i)
  2479.       if is_nomi && !card.properly_summoned
  2480.         draw_icon(809, rect.x + 8, rect.y + 8)
  2481.       end
  2482.     end
  2483.   end
  2484.  
  2485.   def draw_card_illustration(card, x, y)
  2486.     bitmap = Cache.picture("Card" + card.id.to_s)
  2487.     return unless bitmap
  2488.     rect = Rect.new(0, 0, 190, 264)
  2489.     contents.blt(x, y, bitmap, rect)
  2490.     if card.instance_variable_get(:@banished_face_down)
  2491.       back_bitmap = Cache.picture("BackCard")
  2492.       contents.blt(x, y, back_bitmap, rect, 120) if back_bitmap
  2493.     end
  2494.   end
  2495.  
  2496.   def draw_highlight(index)
  2497.     return if index < 0 || index >= @data.size
  2498.     rect = item_rect(index)
  2499.     overlay = Cache.picture("blank_rectangle")
  2500.     return unless overlay
  2501.     opacity = 30 + (Math.sin(@highlight_counter / 20.0) * 64).abs
  2502.     overlay_opacity = opacity
  2503.     contents.blt(rect.x, rect.y, overlay, Rect.new(0, 0, overlay.width, overlay.height), overlay_opacity)
  2504.   end
  2505.  
  2506.   def item_rect(index)
  2507.     return Rect.new(0, 0, 0, 0) unless @data && !@data.empty?
  2508.     total_cards = @data.size
  2509.     card_width = 190
  2510.     card_height = 264
  2511.     usable_width = width - standard_padding * 2
  2512.     if total_cards > 1
  2513.       overlap = [(card_width * total_cards - usable_width) / (total_cards - 1), card_width].min
  2514.       overlap = [overlap, 0].max
  2515.     else
  2516.       overlap = 0
  2517.     end
  2518.     x = index * (card_width - overlap)
  2519.     y = 12
  2520.     Rect.new(x, y, card_width, card_height)
  2521.   end
  2522.  
  2523.   def item_height
  2524.     276
  2525.   end
  2526.  
  2527.   def row_max
  2528.     1
  2529.   end
  2530.  
  2531.   def contents_height
  2532.     height - standard_padding * 2
  2533.   end
  2534.  
  2535.   def ensure_cursor_visible
  2536.   end
  2537.  
  2538.   def cursor_visible?
  2539.     active && open? && !@cursor_all && @index >= 0
  2540.   end
  2541.  
  2542.   def update_cursor
  2543.     if @cursor_all
  2544.       cursor_rect.set(rect.x, rect.y, contents.width, contents.height)
  2545.       self.cursor_opacity = 255
  2546.     elsif @index < 0
  2547.       cursor_rect.empty
  2548.       self.cursor_opacity = 0
  2549.     else
  2550.       rect = item_rect(@index)
  2551.       cursor_rect.set(rect.x, rect.y, rect.width, rect.height)
  2552.       self.cursor_opacity = cursor_visible? ? 255 : 0
  2553.     end
  2554.   end
  2555.  
  2556.   def update
  2557.     super
  2558.     if @last_index != self.index
  2559.       @last_index = self.index
  2560.       refresh
  2561.     end
  2562.     @highlight_counter += 1
  2563.     refresh if @highlight_counter % 1 == 0
  2564.   end
  2565.  
  2566.   def cursor_down(wrap = false); end
  2567.   def cursor_up(wrap = false); end
  2568.   def cursor_right(wrap = false)
  2569.     return if item_max <= 0
  2570.     self.index = (self.index + 1) % item_max
  2571.   end
  2572.  
  2573.   def cursor_left(wrap = false)
  2574.     return if item_max <= 0
  2575.     self.index = (self.index - 1) % item_max
  2576.   end
  2577.  
  2578.   alias pc27_process_ok_graveblock process_ok
  2579.   def process_ok
  2580.     actor = BattleManager.actor
  2581.     return pc27_process_ok_graveblock unless actor
  2582.     if actor.current_zone == :graveyard
  2583.       skill = item
  2584.       if skill.nil?
  2585.         Sound.play_buzzer
  2586.         return
  2587.       end
  2588.       unless card_has_grave_effect?(skill)
  2589.         Sound.play_buzzer
  2590.         return
  2591.       end
  2592.     end
  2593.     pc27_process_ok_graveblock
  2594.   end
  2595.  
  2596.   def card_has_grave_effect?(skill)
  2597.     skill.note =~ /<grave[-_ ]?effect/i || skill.note =~ /<grave/i
  2598.   end
  2599.  
  2600.   def monster_has_hand_effect?(skill)
  2601.     skill.note =~ /<hand[-_ ]?effect>/i || skill.note =~ /<hand/i
  2602.   end
  2603.  
  2604. end
  2605. #===============================================================================
  2606. # Window_ActorCommand
  2607. #===============================================================================
  2608.  
  2609. class Window_ActorCommand < Window_Command
  2610.    
  2611.   #--------------------------------------------------------------------------
  2612.   # Add "End Turn" command (new)
  2613.   #--------------------------------------------------------------------------
  2614.   def add_turn_end_command
  2615.     add_command(PC27::CG::END_TURN, :end_turn)
  2616.   end
  2617.  
  2618.   #--------------------------------------------------------------------------
  2619.   # Add "Grave" command (new)
  2620.   #--------------------------------------------------------------------------
  2621.   def add_look_at_grave_command
  2622.     add_command(PC27::CG::GRAVE, :look_grave)
  2623.   end
  2624.  
  2625.   #--------------------------------------------------------------------------
  2626.   # Add "Banish" command (new)
  2627.   #--------------------------------------------------------------------------
  2628.   def add_look_at_banish_command
  2629.     add_command(PC27::CG::BANISH, :look_banish)
  2630.   end
  2631.  
  2632.   #--------------------------------------------------------------------------
  2633.   # Add "Extra" command (new)
  2634.   #--------------------------------------------------------------------------
  2635.   def add_look_at_extra_command
  2636.     add_command(PC27::CG::EXTRA, :look_extra)
  2637.   end
  2638.  
  2639.   #--------------------------------------------------------------------------
  2640.   # Add "Field" command (new)
  2641.   #--------------------------------------------------------------------------
  2642.   def add_look_at_field_command
  2643.     add_command(PC27::CG::FIELD, :look_field)
  2644.   end
  2645.  
  2646.   #--------------------------------------------------------------------------
  2647.   # Make new command List (overwrite)
  2648.   #--------------------------------------------------------------------------
  2649.   def make_command_list
  2650.     return unless @actor
  2651.     add_attack_command if PC27::CG::USE_ATTACK
  2652.     add_guard_command if PC27::CG::USE_GUARD
  2653.     add_skill_commands
  2654.     add_item_command if PC27::CG::USE_ITEMS
  2655.     add_look_at_grave_command
  2656.     add_look_at_banish_command
  2657.     add_look_at_extra_command
  2658.     add_look_at_field_command
  2659.     add_turn_end_command
  2660.   end
  2661.  
  2662. end
  2663.  
  2664. #===============================================================================
  2665. # Window_BattleStatus
  2666. #===============================================================================
  2667.  
  2668. class Window_BattleStatus < Window_Selectable
  2669.  
  2670.   #--------------------------------------------------------------------------
  2671.   # Add deck to Draw Item (alias)
  2672.   #--------------------------------------------------------------------------
  2673.   alias pc27_cardgame_wndbttlstat_draw_item                            draw_item
  2674.   def draw_item(index)
  2675.     pc27_cardgame_wndbttlstat_draw_item(index)
  2676.     actor = $game_party.battle_members[index]
  2677.     draw_deck_area(deck_area_rect(index), actor)
  2678.   end
  2679.  
  2680.   #--------------------------------------------------------------------------
  2681.   # Shrink basic rect (overwrite)
  2682.   #--------------------------------------------------------------------------
  2683.   def basic_area_rect(index)
  2684.     rect = item_rect_for_text(index)
  2685.     rect.width -= gauge_area_width + deck_area_width + 10
  2686.     rect
  2687.   end
  2688.  
  2689.   #--------------------------------------------------------------------------
  2690.   # Get Deck rect (new)
  2691.   #--------------------------------------------------------------------------
  2692.   def deck_area_rect(index)
  2693.     rect = item_rect_for_text(index)
  2694.     rect.x += rect.width - gauge_area_width - deck_area_width
  2695.     rect.width = deck_area_width
  2696.     rect
  2697.   end
  2698.  
  2699.   #--------------------------------------------------------------------------
  2700.   # Get Deck Area Width (new)
  2701.   #--------------------------------------------------------------------------
  2702.   def deck_area_width
  2703.     return 48
  2704.   end
  2705.  
  2706.   #--------------------------------------------------------------------------
  2707.   # Draw Deck Area (new)
  2708.   #--------------------------------------------------------------------------
  2709.   def draw_deck_area(rect, actor)
  2710.     draw_icon(PC27::CG::DECK_ICON, rect.x + 246, rect.y + 43)
  2711.     draw_text(rect.x + 248, rect.y + 12, 80, rect.height, actor.deck.size.to_s)
  2712.   end
  2713. end
  2714.  
  2715. #===============================================================================
  2716. # Window_SkillList
  2717. #===============================================================================
  2718. class Window_BattleSkill < Window_SkillList
  2719.  
  2720. #-----------------------------------------------------------------------------
  2721. # changes the BattleSkillList Window layout (overwrite)
  2722. #-----------------------------------------------------------------------------
  2723.   def initialize(help_window, info_viewport)
  2724.     y = 0
  2725.     super(0, 0, (Graphics.width - 210) * 0.7, info_viewport.rect.y)
  2726.     self.visible = false
  2727.     @help_window = help_window
  2728.     @info_viewport = info_viewport
  2729.   end
  2730.  
  2731. #--------------------------------------------------------------------------
  2732. # * Display Skill in Active State?
  2733. #--------------------------------------------------------------------------
  2734.   def enable?(item)
  2735.     return false unless item
  2736.     return true if @actor && @actor.current_zone == :hand
  2737.     return false unless @actor.skill_cost_payable?(item)
  2738.     return false unless @actor.custom_req_met?(item)
  2739.     if item.note =~ /<card_id:\s*(\d+)>/i
  2740.       impact_name = PC27::CG::SKILL_IMPACT[item.id]
  2741.       if impact_name
  2742.         return false unless @actor.can_activate_impact?(impact_name)
  2743.       end
  2744.     end
  2745.     return true
  2746.   end
  2747.    
  2748. end
  2749.  
  2750. #===============================================================================
  2751. # Window CardHelp (new)
  2752. #===============================================================================
  2753.  
  2754. class Window_CardHelp < Window_Base
  2755.   include PC27::CG
  2756.  
  2757.   def initialize()
  2758.     super(Graphics.width - 200, 10, 190, 264)
  2759.     self.windowskin = Cache.system(PC27::CG::CUSTOM_CARD_BG) if PC27::CG::CUSTOM_CARD_BG
  2760.     @card = nil
  2761.     refresh
  2762.   end
  2763.  
  2764.   def set_text(card)
  2765.     if card != @card
  2766.       @card = card
  2767.       refresh
  2768.     end
  2769.   end
  2770.  
  2771.   def set_item(item)
  2772.     set_text(item)
  2773.   end
  2774.  
  2775.   def refresh
  2776.     contents.clear
  2777.     draw_card_pic(@card)
  2778.   end
  2779.  
  2780.   def draw_card_pic(item)
  2781.     if item
  2782.       draw_card_illustration(item) rescue draw_card_text(item)
  2783.     else
  2784.       draw_card_illustration_no_card rescue nil
  2785.     end
  2786.   end
  2787.  
  2788.   def draw_card_illustration_no_card
  2789.     contents.dispose
  2790.     self.contents = Bitmap.new(190, 264)
  2791.     bitmap = Cache.picture("BackCard")
  2792.     self.padding = 0
  2793.     rect = Rect.new(0, 0, 190, 264)
  2794.     contents.blt(0, 0, bitmap, rect, 255)
  2795.     bitmap.dispose
  2796.   end
  2797.  
  2798.   def draw_card_illustration(item)
  2799.     contents.dispose
  2800.     self.contents = Bitmap.new(190, 264)
  2801.     bitmap = Cache.picture("Card" + item.id.to_s)
  2802.     self.padding = 0
  2803.     rect = Rect.new(0, 0, 190, 264)
  2804.     contents.blt(0, 0, bitmap, rect, 255)
  2805.     bitmap.dispose
  2806.   end
  2807.  
  2808.   def draw_card_text(item)
  2809.     create_contents
  2810.     self.padding = standard_padding
  2811.     draw_text_ex(0, 0, item.name)
  2812.   end
  2813.  
  2814.   def update_tone;    end
  2815.   def update_padding; end
  2816.    
  2817. end
  2818. #===============================================================================
  2819. # Window_CardSelect (new)
  2820. #===============================================================================
  2821.  
  2822. class Window_CardSelect < Window_SkillList
  2823.   attr_accessor :actor
  2824.   attr_accessor :cards
  2825.   attr_accessor :sort_index
  2826.   attr_accessor :filter
  2827.   attr_accessor :dry_run_eff2
  2828.   attr_accessor :zone_tags
  2829.  
  2830.   def initialize(help_window, info_viewport)
  2831.     super(0, 0, (Graphics.width - 210) * 0.7, info_viewport.rect.y)
  2832.     self.visible = false
  2833.     @help_window = help_window
  2834.     @info_viewport = info_viewport
  2835.     @filter = [0,0,0,0,0,0,0,0,0,0]
  2836.     @cards  = []
  2837.     @zone_tags = []
  2838.     @sort_index = -1
  2839.   end
  2840.  
  2841.   def make_item_list
  2842.     @data = @cards ? @cards.dup : []
  2843.     if SceneManager.scene.is_a?(Scene_Battle)
  2844.       scene = SceneManager.scene
  2845.       effect_data = scene.instance_variable_get(:@temp_effect_data)
  2846.       if effect_data && effect_data[1] && PC27::CG::CARD_EFFECT[effect_data[1]]
  2847.         destination = PC27::CG::CARD_EFFECT[effect_data[1]][5]
  2848.         if [:special_summon, :special_summon_def, :special_summon_atk].include?(destination)
  2849.           @data.reject! do |card|
  2850.             is_nomi = (card.note =~ /<special_only:/i)
  2851.             is_nomi && !card.properly_summoned
  2852.           end
  2853.         end
  2854.       end
  2855.     end
  2856.   end
  2857.  
  2858.   def enable?(item)
  2859.     return false unless item
  2860.     return false if @actor.nil?
  2861.     active_c = nil
  2862.     scene = SceneManager.scene
  2863.     if scene.is_a?(Scene_Battle)
  2864.       active_c = scene.instance_variable_get(:@active_effect_card)
  2865.     end
  2866.     sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  2867.     return false unless @actor.match_complex_filter?(item, @filter, active_c, sim_lvl)
  2868.     if @dry_run_eff2
  2869.       eff2_data = PC27::CG::CARD_EFFECT[@dry_run_eff2]
  2870.       if eff2_data
  2871.         eff2_filter = eff2_data[4]
  2872.         eff2_zone   = eff2_data[2]
  2873.         sim_lvl2 = item.level
  2874.         has_target = false
  2875.         if [:deck, :hand_or_deck].include?(eff2_zone)
  2876.           has_target ||= @actor.deck.any? { |c| @actor.match_complex_filter?(c, eff2_filter, active_c, sim_lvl2) }
  2877.         end
  2878.         if [:hand, :hand_or_deck, :hand_or_grave, :hand_or_field].include?(eff2_zone)
  2879.           has_target ||= @actor.hand.any? { |c| c != item && @actor.match_complex_filter?(c, eff2_filter, active_c, sim_lvl2) }
  2880.         end
  2881.         if [:grave, :hand_or_grave].include?(eff2_zone)
  2882.           has_target ||= @actor.grave.any? { |c| c != item && @actor.match_complex_filter?(c, eff2_filter, active_c, sim_lvl2) }
  2883.         end
  2884.         return false unless has_target
  2885.       end
  2886.     end
  2887.     return true
  2888.   end
  2889.  
  2890.   def match_single_filter?(item, f)
  2891.     return false unless item.attribute == f[0] || f[0] == 0
  2892.     return false unless item.type              == f[1] || f[1] == 0
  2893.     return false unless item.rarity            == f[2] || f[2] == 0
  2894.     return false unless item.subtype           == f[3] || f[3] == 0
  2895.     lvl_check = f[4]
  2896.     if lvl_check == :destroyed_level
  2897.       scene = SceneManager.scene
  2898.       if scene.is_a?(Scene_Battle)
  2899.         temp_data = scene.instance_variable_get(:@temp_effect_data)
  2900.         lvl_check = temp_data[4].level if temp_data && temp_data[4]
  2901.       end
  2902.       lvl_check = 0 unless lvl_check.is_a?(Numeric)
  2903.     end
  2904.     if f[4].is_a?(Range)
  2905.       return false unless f[4].include?(item.level)
  2906.     elsif lvl_check != 0
  2907.       return false unless item.level == lvl_check
  2908.     end
  2909.     return false unless item.normeff           == f[5] || f[5] == 0
  2910.     return false unless item.frame             == f[6] || f[6] == 0
  2911.     return false unless item.archetype         == f[7] || f[7] == 0
  2912.     return false unless item.atk               == f[8] || f[8] == 0
  2913.     return false unless item.def               == f[9] || f[9] == 0
  2914.     return false unless item.special           == f[10]|| f[10] == 0
  2915.     if f[11] == :exclude_active
  2916.       scene = SceneManager.scene
  2917.       if scene.is_a?(Scene_Battle)
  2918.         active_card = scene.instance_variable_get(:@active_effect_card)
  2919.         return false if active_card && active_card.id == item.id
  2920.       end
  2921.     end
  2922.     return true
  2923.   end
  2924.  
  2925.   def show
  2926.     super
  2927.     activate
  2928.     select(0) if @data && !@data.empty?
  2929.   end
  2930.  
  2931.   def activate
  2932.     self.active = true
  2933.   end
  2934.  
  2935.   def hide
  2936.     @help_window.hide
  2937.     super
  2938.   end
  2939.  
  2940.   alias archo_draw_item draw_item
  2941.   def draw_item(index)
  2942.     archo_draw_item(index)
  2943.     source = @zone_tags[index] if @zone_tags
  2944.     if source
  2945.       rect = item_rect(index)
  2946.       icon_id = nil
  2947.       icon_id = 1015 if source == :field
  2948.       icon_id = 800  if source == :hand
  2949.       icon_id = 801  if source == :grave # Icono Cementerio
  2950.       icon_id = 783  if source == :deck  # Icono Mazo
  2951.       draw_icon(icon_id, rect.x + 190 - 28, rect.y + 4) if icon_id
  2952.     end
  2953.   end
  2954.  
  2955. end
  2956.  
  2957. #===============================================================================
  2958. # Field Map
  2959. #===============================================================================
  2960.  
  2961. class Window_FieldMap < Window_Selectable
  2962.   attr_accessor :actor
  2963.   attr_accessor :preview_window
  2964.  
  2965.   def initialize(x, y, actor)
  2966.     @actor = actor
  2967.     super(0, 0, Graphics.width, 300)
  2968.     @zones = []
  2969.     make_zone_list
  2970.     select(0)
  2971.     activate
  2972.     refresh
  2973.   end
  2974.  
  2975.   alias fieldmap_select select
  2976.   def select(index)
  2977.     fieldmap_select(index)
  2978.     update_preview
  2979.   end
  2980.    
  2981.   def refresh
  2982.     contents.clear
  2983.     draw_all_items
  2984.   end
  2985.  
  2986.   def update
  2987.     super
  2988.   end
  2989.  
  2990.   def update_preview
  2991.     return unless @preview_window
  2992.     return unless @actor
  2993.     zone, i = current_zone
  2994.     card = @actor.card_in_zone(zone, i)
  2995.     @preview_window.set_card(card)
  2996.   end
  2997.  
  2998.   def make_zone_list
  2999.     @zones = []
  3000.     5.times { |i| @zones << [:monster, i] }
  3001.     5.times { |i| @zones << [:spell, i] }
  3002.     @zones << [:fieldspell, 0]
  3003.   end
  3004.  
  3005.   def item_max
  3006.     @zones ? @zones.size : 0
  3007.   end
  3008.  
  3009.   def item_rect(index)
  3010.     rect = Rect.new
  3011.     zone, i = @zones[index]
  3012.     base_x = 40
  3013.     case zone
  3014.     when :monster
  3015.       rect.x = base_x + i * 48
  3016.       rect.y = 120
  3017.     when :spell
  3018.       rect.x = base_x + i * 48
  3019.       rect.y = 180
  3020.     when :fieldspell
  3021.       rect.x = base_x + 2 * 48
  3022.       rect.y = 60
  3023.     end
  3024.     rect.width = 24; rect.height = 24
  3025.     rect
  3026.   end
  3027.  
  3028.   def draw_item(index)
  3029.     zone, i = @zones[index]
  3030.     rect = item_rect(index)
  3031.     actor = @actor || BattleManager.actor
  3032.     card = actor.card_in_zone(zone, i) rescue nil
  3033.     if card
  3034.       is_fd = false
  3035.       if zone == :monster
  3036.         states = actor.monster_zones_state rescue nil
  3037.         state = states ? states[i] : { face_down: false, position: :attack }
  3038.         is_fd = state[:face_down]
  3039.         if is_fd
  3040.           draw_icon(840, rect.x, rect.y)
  3041.         else
  3042.           draw_icon(card.icon_index, rect.x, rect.y)
  3043.         end
  3044.         pos_icon = (state[:position] == :defense) ? 526 : 525
  3045.         draw_icon(pos_icon, rect.x + 4, rect.y + 4)
  3046.       elsif zone == :spell
  3047.         states = actor.st_zones_state rescue nil
  3048.         state = states ? states[i] : nil
  3049.         is_fd = state && state[:face_down]
  3050.         if is_fd
  3051.           draw_icon(840, rect.x, rect.y)
  3052.         else
  3053.           draw_icon(card.icon_index, rect.x, rect.y)
  3054.         end
  3055.       elsif zone == :fieldspell
  3056.         f_state = actor.field_zone_state rescue nil
  3057.         is_fd = f_state && f_state[:face_down]
  3058.         if is_fd
  3059.           draw_icon(840, rect.x, rect.y)
  3060.         else
  3061.           draw_icon(card.icon_index, rect.x, rect.y)
  3062.         end
  3063.       else
  3064.         draw_icon(card.icon_index, rect.x, rect.y)
  3065.       end
  3066.     draw_card_counters(actor, card, rect.x, rect.y, false) unless is_fd
  3067.     else
  3068.       case zone
  3069.       when :monster     then draw_icon(1019, rect.x, rect.y)
  3070.       when :spell       then draw_icon(1020, rect.x, rect.y)
  3071.       when :fieldspell  then draw_icon(1021, rect.x, rect.y)
  3072.       end
  3073.     end
  3074.   end
  3075.  
  3076.   def cursor_up(wrap = false)
  3077.     zone, i = @zones[index]
  3078.     case zone
  3079.     when :monster
  3080.       select(10)
  3081.     when :spell
  3082.       select(i)
  3083.     end
  3084.   end
  3085.  
  3086.   def cursor_down(wrap = false)
  3087.     zone, i = @zones[index]
  3088.     case zone
  3089.     when :monster
  3090.       select(5 + i)
  3091.     when :fieldspell
  3092.       select(0)
  3093.     end
  3094.   end
  3095.  
  3096.   def cursor_left(wrap = false)
  3097.     return if index == 10
  3098.     select([index - 1, row_start(index)].max)
  3099.   end
  3100.  
  3101.   def cursor_right(wrap = false)
  3102.     return if index == 10
  3103.     select([index + 1, row_end(index)].min)
  3104.   end
  3105.  
  3106.   def row_start(index)
  3107.     return 10 if index == 10
  3108.     index < 5 ? 0 : 5
  3109.   end
  3110.  
  3111.   def row_end(index)
  3112.     return 10 if index == 10
  3113.     index < 5 ? 4 : 9
  3114.   end
  3115.  
  3116.   def current_zone
  3117.     @zones[index]
  3118.   end
  3119.  
  3120. end
  3121.  
  3122. #===============================================================================
  3123. # Field Preview
  3124. #===============================================================================
  3125.  
  3126. class Window_FieldPreview < Window_Base
  3127.  
  3128.   def standard_padding
  3129.     return 0
  3130.   end
  3131.  
  3132.   def initialize(x, y)
  3133.     super(Graphics.width - 200, 10, 190, 264)
  3134.     self.z = 150
  3135.     self.opacity = 0      
  3136.     self.back_opacity = 0
  3137.     @current_card = nil
  3138.     refresh
  3139.   end
  3140.  
  3141.   def set_card(card)
  3142.     return if @current_card == card
  3143.     @current_card = card
  3144.     refresh
  3145.   end
  3146.  
  3147.   def refresh
  3148.     contents.clear
  3149.     picture_name =
  3150.       if @current_card
  3151.         "Card#{@current_card.id}"
  3152.       else
  3153.         "BackCard"
  3154.       end
  3155.     bitmap = Cache.picture(picture_name)
  3156.     x = (contents.width  - bitmap.width)  / 2
  3157.     y = (contents.height - bitmap.height) / 2
  3158.     contents.blt(x, y, bitmap, bitmap.rect)
  3159.     draw_dynamic_stats(@current_card, x, y)
  3160.   end
  3161.  
  3162.   def draw_dynamic_stats(card, base_x, base_y)
  3163.     return unless card
  3164.     return unless card.type == 3
  3165.     base_atk = card.atk rescue 0
  3166.     base_def = card.def rescue 0
  3167.     real_atk = BattleManager.actor.current_monster_atk(card, "at_field")
  3168.     real_def = BattleManager.actor.current_monster_def(card, "at_field")
  3169.     if real_atk != base_atk || real_def != base_def
  3170.       ajuste_x_atk = 40
  3171.       ajuste_x_def = 100
  3172.       ajuste_y = 150
  3173.       rect_w = 140
  3174.       rect_h = 20
  3175.       cover_color = Color.new(0, 0, 0, 220)
  3176.       contents.fill_rect(base_x + ajuste_x_atk, base_y + ajuste_y, rect_w, rect_h, cover_color)
  3177.       old_size = contents.font.size
  3178.       contents.font.bold = true
  3179.       atk_text = "ATK:#{real_atk}"
  3180.       if atk_text.length >= 12
  3181.         contents.font.size = 18
  3182.       elsif atk_text.length >= 10
  3183.         contents.font.size = 19
  3184.       else
  3185.         contents.font.size = 20
  3186.       end
  3187.       if real_atk > base_atk
  3188.         change_color(text_color(3))
  3189.       elsif real_atk < base_atk
  3190.         change_color(text_color(2))
  3191.       else
  3192.         change_color(normal_color)
  3193.       end
  3194.       draw_text(base_x + ajuste_x_atk, base_y + ajuste_y, 60, rect_h, atk_text, 0)
  3195.       def_text = "DEF:#{real_def}"
  3196.       if def_text.length >= 12
  3197.         contents.font.size = 18
  3198.       elsif def_text.length >= 10
  3199.         contents.font.size = 19
  3200.       else
  3201.         contents.font.size = 20
  3202.       end
  3203.       if real_def > base_def
  3204.         change_color(text_color(3))
  3205.       elsif real_def < base_def
  3206.         change_color(text_color(2))
  3207.       else
  3208.         change_color(normal_color)
  3209.       end
  3210.       draw_text(base_x + ajuste_x_def, base_y + ajuste_y, 60, rect_h, def_text, 0)
  3211.       contents.font.size = old_size
  3212.       contents.font.bold = false
  3213.       change_color(normal_color)
  3214.     end
  3215.   end
  3216.  
  3217. end
  3218.  
  3219. #===============================================================================
  3220. # Scene_Battle
  3221. #===============================================================================
  3222. class Scene_Battle < Scene_Base
  3223.   include FlowTrace
  3224.  
  3225.   ZONE_LABELS = {
  3226.     hand:       ["Hand", 800],
  3227.     field:      ["Field", 1015],
  3228.     grave:      ["Graveyard", 801],
  3229.     graveyard:  ["Graveyard", 801],
  3230.     banish:     ["Banish", 806],
  3231.     extra:      ["Extra", 815],
  3232.     deck:       ["Deck", 783],
  3233.     hand_or_field: ["Hand & Field", 800],
  3234.     hand_or_grave: ["Hand & Graveyard", 800],
  3235.     hand_or_deck: ["Hand & Deck", 800]
  3236.   }
  3237.  
  3238.   alias pc27_turn_start_fix turn_start unless method_defined?(:pc27_turn_start_fix)
  3239.   def turn_start
  3240.     pc27_turn_start_fix
  3241.     if BattleManager.actor
  3242.       BattleManager.actor.used_turn_effects.clear
  3243.       BattleManager.actor.monster_zones_state.each do |state|
  3244.         state[:changed_this_turn] = false if state
  3245.       end
  3246.     end
  3247.     @pending_triggers ||= []
  3248.   end
  3249.  
  3250.   def prior_command
  3251.     start_actor_command_selection
  3252.   end
  3253.  
  3254.   alias rescate_seguro_on_skill_cancel on_skill_cancel
  3255.   def on_skill_cancel
  3256.     rescate_seguro_on_skill_cancel
  3257.     if @actor_command_window
  3258.       @actor_command_window.show
  3259.       @actor_command_window.activate
  3260.     end
  3261.   end
  3262.  
  3263.   def update_field_preview
  3264.     return unless @field_map_window && @field_map_window.active
  3265.     zone, index = @field_map_window.current_zone
  3266.     card = BattleManager.actor.card_in_zone(zone, index)
  3267.     @field_preview_window.set_card(card)
  3268.   end
  3269.  
  3270.   def command_skill
  3271.     @skill_window.make_item_list
  3272.     @skill_window.refresh
  3273.     @skill_window.show
  3274.     @skill_window.activate
  3275.   end
  3276.  
  3277.   def on_field_map_ok
  3278.     zone, index = @field_map_window.current_zone
  3279.     actor = BattleManager.actor
  3280.     card  = @pending_card
  3281.     if actor.card_to_field?(card.id, zone, index)
  3282.       Sound.play_ok
  3283.       @field_map_window.refresh
  3284.       @field_preview_window.set_card(nil)
  3285.       @pending_card = nil
  3286.       skill_select(1)
  3287.     else
  3288.       Sound.play_buzzer
  3289.     end
  3290.   end
  3291.  
  3292.   def on_field_ok
  3293.     zone_type, zone_index = @field_map_window.current_zone
  3294.     card = BattleManager.actor.card_in_zone(zone_type, zone_index)
  3295.     @selected_card = card
  3296.     if @targeting_mode
  3297.       if card
  3298.         filter = @targeting_filter
  3299.         sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  3300.         ok = BattleManager.actor.match_complex_filter?(card, filter, @active_effect_card, sim_lvl, zone_index)
  3301.         if ok && filter.is_a?(Array)
  3302.           f_str = filter.flatten.map(&:to_s)
  3303.           if f_str.include?("exclude_itself")
  3304.             if @active_effect_card && @active_effect_zone_index
  3305.               if zone_type == @active_effect_zone_type && zone_index == @active_effect_zone_index
  3306.                 ok = false
  3307.               end
  3308.             end
  3309.           end
  3310.         end
  3311.         if ok
  3312.           @targeting_mode = false
  3313.           @temp_effect_data[3] = [card]
  3314.           BattleManager.actor.field.delete(card)
  3315.           if zone_type == :monster
  3316.             BattleManager.actor.monster_zones[zone_index] = nil
  3317.           end
  3318.           @field_map_window.hide
  3319.           @field_map_window.deactivate
  3320.           @field_preview_window.hide if @field_preview_window
  3321.           $game_message.clear
  3322.           restore_taken_cards(:proceed)
  3323.         else
  3324.           Sound.play_buzzer
  3325.           @field_map_window.activate
  3326.         end
  3327.       else
  3328.         Sound.play_buzzer
  3329.         @field_map_window.activate
  3330.       end
  3331.       return
  3332.     end
  3333.     if card
  3334.       options = []
  3335.       disabled_indices = []
  3336.       can_eff = BattleManager.actor.can_activate_field_effect?(card)
  3337.       options << "Activate Effect" if can_eff
  3338.       if zone_type == :monster
  3339.         state = BattleManager.actor.monster_zones_state[zone_index]
  3340.         can_change = !state[:changed_this_turn]
  3341.         if can_change
  3342.           if state[:face_down]
  3343.             options << "Flip Summon"
  3344.           else
  3345.             options << (state[:position] == :attack ? "Change to Defense" : "Change to Attack")
  3346.           end
  3347.         end
  3348.       end
  3349.       if zone_type == :spell
  3350.         st_states = BattleManager.actor.st_zones_state rescue nil
  3351.         st_state = st_states ? st_states[zone_index] : nil
  3352.         if st_state && st_state[:face_down]
  3353.           options << "Activate"
  3354.           can_act = true
  3355.           impact_name = nil
  3356.           if card.note =~ /<card impact:\s*(.+?)>/i || card.note =~ /<spell_impact:\s*(.+?)>/i || card.note =~ /<hand_impact:\s*(.+?)>/i
  3357.             impact_name = $1.to_s.strip
  3358.           end
  3359.           can_act = false if impact_name && !BattleManager.actor.can_activate_impact?(impact_name)
  3360.           can_act = false unless BattleManager.actor.custom_req_met?(card)
  3361.           can_act = false unless BattleManager.actor.can_pay_card_cost?(card)
  3362.           can_act = false if card.note =~ /<reactive_only>/i
  3363.           can_act = false if card.respond_to?(:mp_cost) && BattleManager.actor.mp < card.mp_cost
  3364.           is_trap = (card.type == 2)
  3365.           is_quick_play = (card.subtype == 25)
  3366.           if st_state[:set_this_turn] && (is_trap || is_quick_play)
  3367.             can_act = false
  3368.           end
  3369.           can_act = false if BattleManager.actor.hopt_blocked?(card, impact_name)
  3370.           disabled_indices << (options.size - 1) unless can_act
  3371.         end
  3372.       elsif zone_type == :fieldspell
  3373.         f_state = BattleManager.actor.field_zone_state rescue nil
  3374.         if f_state && f_state[:face_down]
  3375.           options << "Activate"
  3376.           can_act = true
  3377.           impact_name = nil
  3378.           if card.note =~ /<card impact:\s*(.+?)>/i || card.note =~ /<spell_impact:\s*(.+?)>/i || card.note =~ /<hand_impact:\s*(.+?)>/i
  3379.             impact_name = $1.to_s.strip
  3380.           end
  3381.           can_act = false if impact_name && !BattleManager.actor.can_activate_impact?(impact_name)
  3382.           can_act = false unless BattleManager.actor.custom_req_met?(card)
  3383.           can_act = false if card.respond_to?(:mp_cost) && BattleManager.actor.mp < card.mp_cost
  3384.           can_act = false if BattleManager.actor.hopt_blocked?(card, impact_name)
  3385.           disabled_indices << (options.size - 1) unless can_act
  3386.         end
  3387.       end
  3388.       options << "Cancel"
  3389.       if options.size > 1
  3390.         show_advanced_options(options, disabled_indices) do |choice|
  3391.           selected = options[choice]
  3392.           $game_message.choice_proc = nil
  3393.           $game_message.clear
  3394.           if @message_window
  3395.             choice_window = @message_window.instance_variable_get(:@choice_window)
  3396.             choice_window.openness = 0 if choice_window
  3397.             @message_window.openness = 0
  3398.             @message_window.visible = false
  3399.           end
  3400.           case selected
  3401.           when "Activate Effect"
  3402.             @field_map_window.hide
  3403.             @field_map_window.deactivate
  3404.             @field_preview_window.hide if @field_preview_window
  3405.             @current_card_action = :field_effect
  3406.             @active_effect_card = card
  3407.             @active_effect_zone_type = zone_type
  3408.             @active_effect_zone_index = zone_index
  3409.             eff_name = nil
  3410.             if card.note =~ /<field_impact:\s*(.+?)>/i || card.note =~ /<field impact:\s*(.+?)>/i
  3411.               eff_name = $1.to_s.strip
  3412.             end
  3413.             BattleManager.actor.used_turn_effects << eff_name if eff_name
  3414.             if card.note =~ /<shared_opt:\s*(.+?)>/i
  3415.               BattleManager.actor.used_turn_effects << $1.to_s.strip
  3416.             end
  3417.             resolve_card_effect(card)
  3418.           when "Activate"
  3419.             if zone_type == :fieldspell
  3420.               active_state = BattleManager.actor.field_zone_state
  3421.             else
  3422.               active_state = BattleManager.actor.st_zones_state[zone_index]
  3423.             end
  3424.             active_state[:face_down] = false
  3425.             if @field_map_window
  3426.               @field_map_window.dispose
  3427.               @field_map_window = nil
  3428.             end
  3429.             if @field_preview_window
  3430.               @field_preview_window.dispose
  3431.               @field_preview_window = nil
  3432.             end
  3433.             @field_ui_created = false
  3434.             @field_zone_label.hide if @field_zone_label
  3435.             @skill = card
  3436.             @selected_card = card
  3437.             @current_card_action = :field_effect
  3438.             @active_effect_card = card
  3439.             @active_effect_zone_type = zone_type
  3440.             @active_effect_zone_index = zone_index
  3441.             impact_name = nil
  3442.             if card.note =~ /<card impact:\s*(.+?)>/i || card.note =~ /<spell_impact:\s*(.+?)>/i || card.note =~ /<hand_impact:\s*(.+?)>/i
  3443.               impact_name = $1.to_s.strip
  3444.             end
  3445.             BattleManager.actor.used_turn_effects << impact_name if impact_name
  3446.             if card.note =~ /<shared_opt:\s*(.+?)>/i
  3447.               BattleManager.actor.used_turn_effects << $1.to_s.strip
  3448.             end
  3449.             restore_skill_window_layout
  3450.             refresh_card_zone_label
  3451.             @card_zone_label.show if @card_zone_label
  3452.             if impact_name
  3453.               resolve_card_effect(card)
  3454.             else
  3455.               @active_effect_card = nil
  3456.               @current_processing_card = nil
  3457.               $temp_active_card = nil
  3458.               $chain_memory_card = nil
  3459.               create_field_ui unless @field_ui_created rescue nil
  3460.             end
  3461.           when "Change to Defense"
  3462.             state[:position] = :defense
  3463.             state[:changed_this_turn] = true
  3464.             @field_map_window.refresh
  3465.             @field_map_window.activate
  3466.           when "Change to Attack"
  3467.             state[:position] = :attack
  3468.             state[:changed_this_turn] = true
  3469.             @field_map_window.refresh
  3470.             @field_map_window.activate
  3471.           when "Flip Summon"
  3472.             state[:position] = :attack
  3473.             state[:face_down] = false
  3474.             state[:changed_this_turn] = true
  3475.             @field_map_window.refresh
  3476.             @field_map_window.activate
  3477.           when "Cancel"
  3478.             @field_map_window.activate
  3479.           end
  3480.         end
  3481.       else
  3482.         Sound.play_buzzer
  3483.         @field_map_window.activate
  3484.       end
  3485.     else
  3486.       Sound.play_buzzer
  3487.       @field_map_window.activate
  3488.     end
  3489.   end
  3490.  
  3491.   alias pc27_cg_process_action_end process_action_end
  3492.   def process_action_end
  3493.     pc27_cg_process_action_end
  3494.     if @pending_triggers && @pending_triggers.size > 0
  3495.       check_pending_triggers
  3496.     end
  3497.   end
  3498.  
  3499.   #--------------------------------------------------------------------------
  3500.   # Efecto con monedas!
  3501.   #--------------------------------------------------------------------------
  3502.  
  3503.   def process_coin_toss(num_coins, conditions_text)
  3504.     @is_interrupt_effect = true # Clavamos los frenos de la batalla
  3505.    
  3506.     results = []
  3507.     sprites = []
  3508.    
  3509.     offsets = case num_coins
  3510.               when 1 then [0]
  3511.               when 2 then [-50, 50]
  3512.               when 3 then [-70, 0, 70]
  3513.               else Array.new(num_coins, 0)
  3514.               end
  3515.              
  3516.     num_coins.times do |i|
  3517.       res = (rand(2) == 0 ? :heads : :tails)
  3518.       results << res
  3519.       anim_id = (res == :heads ? 136 : 137)
  3520.      
  3521.       dummy = Sprite_Base.new(nil)
  3522.       dummy.bitmap = Bitmap.new(1, 1)
  3523.       dummy.x = (Graphics.width / 2) + offsets[i]
  3524.       dummy.y = (Graphics.height / 2) - 50
  3525.      
  3526.       anim = $data_animations[anim_id]
  3527.       dummy.start_animation(anim, false) if anim
  3528.       sprites << dummy
  3529.      
  3530.       if i < num_coins - 1
  3531.         20.times do
  3532.           sprites.each { |s| s.update }
  3533.           update_basic
  3534.         end
  3535.       end
  3536.     end
  3537.    
  3538.     while sprites.any? { |s| s.animation? }
  3539.       sprites.each { |s| s.update }
  3540.       update_basic
  3541.     end
  3542.    
  3543.     sprites.each { |s| s.dispose }
  3544.    
  3545.     txt_res = results.map { |r| r == :heads ? "Heads" : "Tails" }.join(", ")
  3546.     $game_message.clear
  3547.     if @message_window
  3548.       @message_window.openness = 255
  3549.       @message_window.visible = true
  3550.     end
  3551.     $game_message.background = 1
  3552.     $game_message.position = 1
  3553.     $game_message.add("Coin Toss Result:")
  3554.     $game_message.add(txt_res + "!")
  3555.     wait_for_message
  3556.    
  3557.     # ===================================================================
  3558.     # SISTEMA: SECOND COIN TOSS
  3559.     # ===================================================================
  3560.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  3561.     available_repeaters = []
  3562.    
  3563.     if player.respond_to?(:st_zones)
  3564.       player.st_zones.each_with_index do |c, i|
  3565.         next unless c && c.note =~ /<repeat coin>/i
  3566.         state = player.st_zones_state[i] rescue nil
  3567.         next if state && state[:face_down] # Tiene que estar boca arriba
  3568.        
  3569.         # Soft Once Per Turn: Usamos el ID interno del objeto en el motor
  3570.         player.used_turn_effects ||= []
  3571.         next if player.used_turn_effects.include?("repeat_coin_#{c.object_id}")
  3572.        
  3573.         available_repeaters << c
  3574.       end
  3575.     end
  3576.    
  3577.     # Si encontramos al menos una copia válida y es el jugador (no la IA)
  3578.     if available_repeaters.size > 0 && player.actor?
  3579.       $game_message.clear
  3580.       if @message_window
  3581.         @message_window.openness = 0
  3582.         @message_window.visible = true
  3583.       end
  3584.       $game_message.background = 1
  3585.       $game_message.position = 1
  3586.       $game_message.add("Activate Second Coin Toss to retry?")
  3587.      
  3588.       @status_window.hide if respond_to?(:@status_window) && @status_window
  3589.       @status_back_window.hide if respond_to?(:@status_back_window) && @status_back_window
  3590.      
  3591.       show_options(["Yes", "No"], 2) do |choice|
  3592.         @status_window.show if respond_to?(:@status_window) && @status_window
  3593.         @status_back_window.show if respond_to?(:@status_back_window) && @status_back_window
  3594.         $game_message.choice_proc = nil
  3595.         $game_message.clear
  3596.         if @message_window
  3597.           choice_window = @message_window.instance_variable_get(:@choice_window)
  3598.           choice_window.openness = 0 if choice_window
  3599.         end
  3600.        
  3601.         if choice == 0
  3602.           # ¡ELIGIO REPETIR!
  3603.           repeater = available_repeaters[0]
  3604.           # Marcamos esta copia física específica como usada
  3605.           player.used_turn_effects << "repeat_coin_#{repeater.object_id}"
  3606.           # Llamada recursiva: Borrón y cuenta nueva
  3607.           process_coin_toss(num_coins, conditions_text)
  3608.         else
  3609.           # ¡ELIGIO NO REPETIR! Continuamos a la Parte 2.
  3610.           finalize_coin_toss(results, num_coins, conditions_text)
  3611.         end
  3612.       end
  3613.       wait_for_message
  3614.       return # Cortamos acá porque el futuro se decide adentro del bloque de arriba
  3615.     end
  3616.    
  3617.     # Si no tenías Second Coin Toss, pasamos directo a la Parte 2
  3618.     finalize_coin_toss(results, num_coins, conditions_text)
  3619.   end
  3620.  
  3621.   #==============================================================================
  3622.   # MOTOR DE MONEDAS: PARTE 2 (Traducción y Resolución)
  3623.   #==============================================================================
  3624.   def finalize_coin_toss(results, num_coins, conditions_text)
  3625.     heads_count = results.count(:heads)
  3626.     tails_count = results.count(:tails)
  3627.     final_impact = nil
  3628.    
  3629.     conditions_text.split("\n").each do |line|
  3630.       next if line.strip.empty?
  3631.      
  3632.       if line =~ /(heads|tails)(?:,\s*(\d+)(?:\.\.(\d+))?)?\s*:\s*(.+)/i
  3633.         face = $1.downcase.to_sym
  3634.         min = $2 ? $2.to_i : 1
  3635.         max = $3 ? $3.to_i : ($2 ? $2.to_i : num_coins)
  3636.         effect = $4.strip
  3637.        
  3638.         count_to_check = face == :heads ? heads_count : tails_count
  3639.        
  3640.         if count_to_check >= min && count_to_check <= max
  3641.           final_impact = effect
  3642.           break
  3643.         end
  3644.       end
  3645.     end
  3646.    
  3647.     @is_interrupt_effect = false # Soltamos los frenos
  3648.    
  3649.     if final_impact
  3650.       invoke_card_impact(final_impact)
  3651.     else
  3652.       finish_proceed_selection
  3653.     end
  3654.   end
  3655.  
  3656.   #--------------------------------------------------------------------------
  3657.   # Animación Visual de Mezclar el Mazo (Scene_Battle) - VERSIÓN NATIVA
  3658.   #--------------------------------------------------------------------------
  3659.   def show_deck_shuffle_visuals
  3660.     if @card_zone_label
  3661.       @card_zone_label.hide
  3662.       @card_zone_label.visible = false
  3663.     end
  3664.     if @field_zone_label
  3665.       @field_zone_label.hide
  3666.       @field_zone_label.visible = false
  3667.     end
  3668.     if @log_window && @shuffle_message
  3669.       @log_window.clear
  3670.       @log_window.add_text(@shuffle_message)
  3671.       @shuffle_message = nil
  3672.       @added_card_name = nil
  3673.     end
  3674.     cx = Graphics.width / 2
  3675.     cy = (Graphics.height / 2) - 40
  3676.     sprites = []
  3677.     3.times do |i|
  3678.       s = Sprite.new
  3679.       begin
  3680.         s.bitmap = Cache.picture("BackCard")
  3681.       rescue
  3682.         s.bitmap = Bitmap.new(190, 264)
  3683.       end
  3684.       s.ox = s.bitmap.width / 2
  3685.       s.oy = s.bitmap.height / 2
  3686.       s.x = cx
  3687.       s.y = cy
  3688.       s.z = 400 + i
  3689.       s.zoom_x = 0.80
  3690.       s.zoom_y = 0.80
  3691.       sprites << s
  3692.     end
  3693.     4.times do
  3694.       RPG::SE.new(PC27::CG::DRAW_SOUND[0], 100, 150).play rescue nil
  3695.       8.times do
  3696.         sprites[0].x -= 10
  3697.         sprites[2].x += 10
  3698.         sprites.each(&:update)
  3699.         @card_zone_label.visible = false if @card_zone_label
  3700.         update_basic
  3701.       end
  3702.       RPG::SE.new(PC27::CG::DRAW_SOUND[0], 100, 100).play rescue nil
  3703.       sprites[0].z, sprites[2].z = sprites[2].z, sprites[0].z
  3704.       8.times do
  3705.         sprites[0].x += 10
  3706.         sprites[2].x -= 10
  3707.         sprites.each(&:update)
  3708.         @card_zone_label.visible = false if @card_zone_label
  3709.         update_basic
  3710.       end
  3711.     end
  3712.     sprites.each do |s|
  3713.       s.bitmap.dispose if s.bitmap
  3714.       s.dispose
  3715.     end
  3716.     @log_window.clear if @log_window
  3717.   end
  3718.  
  3719.   #--------------------------------------------------------------------------
  3720.   # Muestra los efectos visuales de Invocación (Escalado 80% y HUD intacto)
  3721.   #--------------------------------------------------------------------------
  3722.   def show_special_summon_visuals(card, is_special = false)
  3723.     BattleManager.actor.used_turn_effects ||= []
  3724.     BattleManager.actor.used_turn_effects << "special_summon_performed"
  3725.     return unless card
  3726.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  3727.    
  3728.     if @card_zone_label
  3729.       @card_zone_label.hide
  3730.       @card_zone_label.visible = false
  3731.     end
  3732.     if @field_zone_label
  3733.       @field_zone_label.hide
  3734.       @field_zone_label.visible = false
  3735.     end
  3736.     @skill_window.hide if @skill_window
  3737.    
  3738.     if @log_window
  3739.       @log_window.clear
  3740.       @log_window.add_text(player.name + " summons " + card.name + "!")
  3741.     end
  3742.    
  3743.     # 1. Obtenemos el índice del actor
  3744.     actor_index = $game_party.battle_members.index(player) || 0
  3745.     begin
  3746.       ring_x = MOG_BATTLE_COMMAND_EX::FIXED_COMMAND_POSITION[actor_index][0]
  3747.     rescue
  3748.       ring_x = 65 + (actor_index * 100)
  3749.     end
  3750.     offset_x = ring_x - 60
  3751.    
  3752.     # 2. BÚSQUEDA SEGURA
  3753.     zone_index = player.monster_zones.rindex { |c| c && c.id == card.id } || 2
  3754.    
  3755.     # 3. Calculamos la posición de aterrizaje
  3756.     final_x = offset_x + (zone_index * 24) + 12
  3757.     final_y = (Graphics.height - 120 - 76) + 24 + 12
  3758.    
  3759.     # === DEBUG SPECIAL SUMMON ===
  3760.     p "--- DEBUG SPECIAL SUMMON ---"
  3761.     p "Carta: #{card.name} (ID: #{card.id})"
  3762.     p "Offset X del jugador: #{offset_x}"
  3763.     p "Casillero encontrado (zone_index): #{zone_index}"
  3764.     p "Destino Final -> X: #{final_x} | Y: #{final_y}"
  3765.     p "----------------------------"
  3766.    
  3767.     # 4. Posición inicial
  3768.     start_x = player.screen_x
  3769.     start_y = player.screen_y - 150
  3770.     start_scale = 0.50
  3771.     final_scale = 0.10
  3772.    
  3773.     if card.note =~ /<summon_anim:\s*(\d+)>/i
  3774.       anim_id = $1.to_i
  3775.     else
  3776.       anim_id = 43
  3777.     end
  3778.    
  3779.     card_sprite = Sprite.new
  3780.     begin
  3781.       card_sprite.bitmap = Cache.picture("Card" + card.id.to_s)
  3782.     rescue
  3783.       card_sprite.bitmap = Bitmap.new(190, 264)
  3784.     end
  3785.     card_sprite.ox = card_sprite.bitmap.width / 2
  3786.     card_sprite.oy = card_sprite.bitmap.height / 2
  3787.     card_sprite.zoom_x = start_scale
  3788.     card_sprite.zoom_y = start_scale
  3789.     card_sprite.x = start_x
  3790.     card_sprite.y = start_y
  3791.     card_sprite.z = 300
  3792.     card_sprite.opacity = 0
  3793.    
  3794.     # FASE 1
  3795.     15.times do
  3796.       card_sprite.opacity += 17
  3797.       @card_zone_label.visible = false if @card_zone_label
  3798.       update_basic
  3799.     end
  3800.    
  3801.     # FASE 2
  3802.     frames_viaje = 20
  3803.     frames_viaje.times do |i|
  3804.       progress = (i + 1).to_f / frames_viaje
  3805.       card_sprite.x = start_x + (final_x - start_x) * progress
  3806.       card_sprite.y = start_y + (final_y - start_y) * progress
  3807.      
  3808.       current_scale = start_scale - ((start_scale - final_scale) * progress)
  3809.       card_sprite.zoom_x = current_scale
  3810.       card_sprite.zoom_y = current_scale
  3811.      
  3812.       @card_zone_label.visible = false if @card_zone_label
  3813.       update_basic
  3814.     end
  3815.    
  3816.     # FASE 3
  3817.     player.animation_id = anim_id
  3818.     card_sprite.flash(Color.new(255, 255, 255, 255), 30)
  3819.    
  3820.     30.times do
  3821.       card_sprite.update
  3822.       @card_zone_label.visible = false if @card_zone_label
  3823.       update_basic
  3824.     end
  3825.    
  3826.     # FASE 4
  3827.     15.times do
  3828.       card_sprite.opacity -= 17
  3829.       card_sprite.update
  3830.       @card_zone_label.visible = false if @card_zone_label
  3831.       update_basic
  3832.     end
  3833.    
  3834.     card_sprite.bitmap.dispose if card_sprite.bitmap
  3835.     card_sprite.dispose
  3836.     @log_window.clear if @log_window
  3837.   end
  3838.  
  3839.   def show_set_card_visuals
  3840.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  3841.    
  3842.     if @card_zone_label
  3843.       @card_zone_label.hide
  3844.       @card_zone_label.visible = false
  3845.     end
  3846.     if @field_zone_label
  3847.       @field_zone_label.hide
  3848.       @field_zone_label.visible = false
  3849.     end
  3850.     @skill_window.hide if @skill_window
  3851.    
  3852.     if @log_window
  3853.       @log_window.clear
  3854.       @log_window.add_text(player.name + " sets a card!")
  3855.     end
  3856.    
  3857.     # 1. Matemática de Posición
  3858.     actor_index = $game_party.battle_members.index(player) || 0
  3859.     begin
  3860.       ring_x = MOG_BATTLE_COMMAND_EX::FIXED_COMMAND_POSITION[actor_index][0]
  3861.     rescue
  3862.       ring_x = 65 + (actor_index * 100)
  3863.     end
  3864.     offset_x = ring_x - 60
  3865.    
  3866.     # 2. FILTRO
  3867.     card = @selected_card
  3868.     if card.nil? || (card.type.nil? rescue true)
  3869.       card = @skill if @skill && (@skill.type != nil rescue false)
  3870.     end
  3871.     card = player.field.last if card.nil? || (card.type.nil? rescue true)
  3872.    
  3873.     # Corrección: Asignamos el Type 3 para Monstruos (Ajustar si tus monstruos usan otro Type)
  3874.     is_monster = card.type == 3 rescue false
  3875.     is_field_spell = card.subtype == 27 rescue false
  3876.    
  3877.     p "--- DEBUG SET CARD ---"
  3878.     p "Carta leída: #{card ? card.name : 'NINGUNA'} (ID: #{card ? card.id : 'N/A'})"
  3879.     p "Type interno: #{card ? card.type : 'N/A'} | Subtype: #{card ? card.subtype : 'N/A'}"
  3880.     p "Es monstruo?: #{is_monster} | Es Field Spell?: #{is_field_spell}"
  3881.    
  3882.     if is_field_spell
  3883.       final_x = offset_x + 48 + 12
  3884.       final_y = (Graphics.height - 120 - 76) + 12
  3885.       p "Calculando para Zona: Field Spell"
  3886.     elsif is_monster
  3887.       zone_index = player.monster_zones.rindex { |c| c && c.id == card.id } || 2
  3888.       final_x = offset_x + (zone_index * 24) + 12
  3889.       final_y = (Graphics.height - 120 - 76) + 24 + 12
  3890.       p "Calculando para Zona: Monstruo | Índice (zone_index): #{zone_index}"
  3891.     else
  3892.       zone_index = player.st_zones.rindex { |c| c && c.id == card.id } || 2
  3893.       final_x = offset_x + (zone_index * 24) + 12
  3894.       final_y = (Graphics.height - 120 - 76) + 48 + 12
  3895.       p "Calculando para Zona: Magia/Trampa | Índice (zone_index): #{zone_index}"
  3896.     end
  3897.    
  3898.     p "Destino Final -> X: #{final_x} | Y: #{final_y}"
  3899.     p "----------------------"
  3900.    
  3901.     start_x = player.screen_x
  3902.     start_y = player.screen_y - 150
  3903.     start_scale = 0.50
  3904.     final_scale = 0.10
  3905.    
  3906.     card_sprite = Sprite.new
  3907.     begin
  3908.       card_sprite.bitmap = Cache.picture("BackCard")
  3909.     rescue
  3910.       card_sprite.bitmap = Bitmap.new(190, 264)
  3911.     end
  3912.     card_sprite.ox = card_sprite.bitmap.width / 2
  3913.     card_sprite.oy = card_sprite.bitmap.height / 2
  3914.     card_sprite.zoom_x = start_scale
  3915.     card_sprite.zoom_y = start_scale
  3916.     card_sprite.x = start_x
  3917.     card_sprite.y = start_y
  3918.     card_sprite.z = 300
  3919.     card_sprite.opacity = 0
  3920.    
  3921.     15.times do
  3922.       card_sprite.opacity += 17
  3923.       @card_zone_label.visible = false if @card_zone_label
  3924.       update_basic
  3925.     end
  3926.    
  3927.     RPG::SE.new("Equip1", 80, 100).play
  3928.    
  3929.     frames_viaje = 20
  3930.     frames_viaje.times do |i|
  3931.       progress = (i + 1).to_f / frames_viaje
  3932.       card_sprite.x = start_x + (final_x - start_x) * progress
  3933.       card_sprite.y = start_y + (final_y - start_y) * progress
  3934.      
  3935.       current_scale = start_scale - ((start_scale - final_scale) * progress)
  3936.       card_sprite.zoom_x = current_scale
  3937.       card_sprite.zoom_y = current_scale
  3938.      
  3939.       @card_zone_label.visible = false if @card_zone_label
  3940.       update_basic
  3941.     end
  3942.    
  3943.     # Animación de Set
  3944.     player.animation_id = 138
  3945.     card_sprite.flash(Color.new(255, 255, 255, 255), 30)
  3946.    
  3947.     30.times { card_sprite.update; update_basic; @card_zone_label.visible = false if @card_zone_label }
  3948.     15.times do
  3949.       card_sprite.opacity -= 17
  3950.       card_sprite.update
  3951.       update_basic
  3952.       @card_zone_label.visible = false if @card_zone_label
  3953.     end
  3954.    
  3955.     card_sprite.bitmap.dispose if card_sprite.bitmap
  3956.     card_sprite.dispose
  3957.     @log_window.clear if @log_window
  3958.   end
  3959.  
  3960.   def restore_hud_visibility
  3961.     @status_window.show if @status_window
  3962.     @status_back_window.show if @status_back_window
  3963.     refresh_card_zone_label
  3964.     @card_zone_label.show if @card_zone_label
  3965.   end
  3966.  
  3967.   #--------------------------------------------------------------------------
  3968.   # Add end turn command and remove prior command (overwrite)
  3969.   #--------------------------------------------------------------------------
  3970.   def create_actor_command_window
  3971.     @actor_command_window = Window_ActorCommand.new
  3972.     @actor_command_window.viewport = @info_viewport
  3973.     @actor_command_window.set_handler(:attack,      method(:command_attack))
  3974.     @actor_command_window.set_handler(:skill,       method(:command_look_hand))
  3975.     @actor_command_window.set_handler(:guard,       method(:command_guard))
  3976.     @actor_command_window.set_handler(:item,        method(:command_item))
  3977.     @actor_command_window.set_handler(:look_grave,  method(:command_look_grave))
  3978.     @actor_command_window.set_handler(:look_banish, method(:command_look_banish))
  3979.     @actor_command_window.set_handler(:look_extra,  method(:command_look_extra))
  3980.     @actor_command_window.set_handler(:look_field,  method(:command_look_field))
  3981.     @actor_command_window.set_handler(:end_turn,    method(:command_end_turn))
  3982.     @actor_command_window.x = Graphics.width
  3983.   end
  3984.  
  3985.   #--------------------------------------------------------------------------
  3986.   # Handles the Look Grave command
  3987.   #--------------------------------------------------------------------------
  3988.   def command_look_grave
  3989.     actor = BattleManager.actor
  3990.     return unless actor
  3991.     actor.current_zone = :graveyard
  3992.     @skill_window.instance_variable_set(:@actor, actor)
  3993.     @skill_window.make_item_list
  3994.     @skill_window.refresh
  3995.     @skill_window.select(0) if @skill_window.item_max > 0
  3996.     @skill_window.show
  3997.     @skill_window.activate
  3998.     refresh_card_zone_label
  3999.     @card_zone_label.show
  4000.   end
  4001.  
  4002.   #--------------------------------------------------------------------------
  4003.   # Handles the Look Banish command
  4004.   #--------------------------------------------------------------------------
  4005.   def command_look_banish
  4006.     actor = BattleManager.actor
  4007.     return unless actor
  4008.     actor.current_zone = :banish
  4009.     @skill_window.instance_variable_set(:@actor, actor)
  4010.     @skill_window.make_item_list
  4011.     @skill_window.refresh
  4012.     @skill_window.select(0) if @skill_window.item_max > 0
  4013.     @skill_window.show
  4014.     @skill_window.activate
  4015.     refresh_card_zone_label
  4016.     @card_zone_label.show
  4017.   end
  4018.  
  4019.   #--------------------------------------------------------------------------
  4020.   # Handles the Look Extra command
  4021.   #--------------------------------------------------------------------------
  4022.   def command_look_extra
  4023.     actor = BattleManager.actor
  4024.     return unless actor
  4025.     actor.current_zone = :extra
  4026.     @skill_window.instance_variable_set(:@actor, actor)
  4027.     @skill_window.make_item_list
  4028.     @skill_window.refresh
  4029.     @skill_window.select(0) if @skill_window.item_max > 0
  4030.     @skill_window.show
  4031.     @skill_window.activate
  4032.     refresh_card_zone_label
  4033.     @card_zone_label.show
  4034.   end
  4035.  
  4036.   #--------------------------------------------------------------------------
  4037.   # Handles the Look Deck command
  4038.   #--------------------------------------------------------------------------
  4039.   def command_look_deck
  4040.     actor = BattleManager.actor
  4041.     return unless actor
  4042.     actor.current_zone = :deck
  4043.     @skill_window.instance_variable_set(:@actor, actor)
  4044.     @skill_window.make_item_list
  4045.     @skill_window.refresh
  4046.     @skill_window.select(0) if @skill_window.item_max > 0
  4047.     @skill_window.show
  4048.     @skill_window.activate
  4049.     refresh_card_zone_label
  4050.     @card_zone_label.show
  4051.   end
  4052.  
  4053.   #--------------------------------------------------------------------------
  4054.   # Handles the Look Field command
  4055.   #--------------------------------------------------------------------------
  4056.   def command_look_field
  4057.     actor = BattleManager.actor
  4058.     return unless actor
  4059.     actor.current_zone = :field
  4060.     @skill_window_backup ||= {
  4061.     x: @skill_window.x,
  4062.     y: @skill_window.y,
  4063.     width: @skill_window.width,
  4064.     height: @skill_window.height
  4065.     }
  4066.     @skill_window.instance_variable_set(:@actor, actor)
  4067.     @skill_window.hide
  4068.     @skill_window.deactivate
  4069.     @skill_window.x = 0
  4070.     @skill_window.y = 0
  4071.     @skill_window.width = Graphics.width / 2
  4072.     @skill_window.height = Graphics.height
  4073.     @skill_window.refresh
  4074.     @skill_window.select(0) if @skill_window.item_max > 0
  4075.     create_field_ui unless @field_ui_created
  4076.     refresh_card_zone_label
  4077.     @card_zone_label.show
  4078.     create_field_label
  4079.   end
  4080.  
  4081.   def create_field_label
  4082.     @field_zone_label = Window_CardZoneLabel.new(0, 0)
  4083.     @field_zone_label.set_text("Field", 1015)
  4084.   end
  4085.  
  4086.   def create_field_ui
  4087.     @field_map_window = Window_FieldMap.new(0, 0, BattleManager.actor)
  4088.     @field_map_window.actor = BattleManager.actor
  4089.     @field_preview_window = Window_FieldPreview.new(Graphics.width / 2, 0)
  4090.     @field_map_window.preview_window = @field_preview_window
  4091.     @field_map_window.update_preview
  4092.     @field_map_window.set_handler(:cancel, method(:on_field_cancel))
  4093.     @field_map_window.set_handler(:ok, method(:on_field_ok))
  4094.     @field_ui_created = true
  4095.   end
  4096.  
  4097.   def on_field_cancel
  4098.     if @targeting_mode
  4099.       effect_name = @temp_effect_data[1]
  4100.       effect_data = PC27::CG::CARD_EFFECT[effect_name]
  4101.       type = effect_data ? effect_data[0] : nil
  4102.       if type == :takem
  4103.         Sound.play_buzzer
  4104.         @field_map_window.activate
  4105.         return
  4106.       else
  4107.         if @active_effect_card
  4108.           eff = @active_effect_card.field_impact
  4109.           BattleManager.actor.used_turn_effects.delete(eff) if eff
  4110.           if @active_effect_card.note =~ /<shared_opt:\s*(.+?)>/i
  4111.             BattleManager.actor.used_turn_effects.delete($1.to_s.strip)
  4112.           end
  4113.         end
  4114.         @targeting_mode = false
  4115.         $game_message.clear
  4116.       end
  4117.     end
  4118.     actor = BattleManager.actor
  4119.     actor.current_zone = nil
  4120.     if @field_map_window
  4121.       @field_map_window.dispose
  4122.       @field_map_window = nil
  4123.     end
  4124.     if @field_preview_window
  4125.       @field_preview_window.dispose
  4126.       @field_preview_window = nil
  4127.     end
  4128.     @field_ui_created = false
  4129.     restore_skill_window_layout
  4130.     refresh_card_zone_label
  4131.     @field_zone_label.hide if @field_zone_label
  4132.     @card_zone_label.hide if @card_zone_label
  4133.     @actor_command_window.show
  4134.     @actor_command_window.activate
  4135.   end
  4136.  
  4137.   def restore_skill_window_layout
  4138.     return unless @skill_window_backup
  4139.     @skill_window.x      = @skill_window_backup[:x]
  4140.     @skill_window.y      = @skill_window_backup[:y]
  4141.     @skill_window.width  = @skill_window_backup[:width]
  4142.     @skill_window.height = @skill_window_backup[:height]
  4143.     @skill_window.hide
  4144.     @skill_window.deactivate
  4145.   end
  4146.  
  4147.   def update_field_preview
  4148.     return unless @skill_window.active
  4149.     card = @skill_window.item
  4150.   end
  4151.  
  4152.   #--------------------------------------------------------------------------
  4153.   # Handles the Look Hand command
  4154.   #--------------------------------------------------------------------------
  4155.   def command_look_hand
  4156.     actor = BattleManager.actor
  4157.     return unless actor
  4158.     if @party_command_window
  4159.       @party_command_window.hide
  4160.       @party_command_window.deactivate
  4161.     end
  4162.     actor.current_zone = :hand
  4163.     @skill_window.instance_variable_set(:@actor, actor)
  4164.     @skill_window.make_item_list
  4165.     @skill_window.refresh
  4166.     @skill_window.select(0) if @skill_window.item_max > 0
  4167.     @skill_window.show
  4168.     @skill_window.activate
  4169.     refresh_card_zone_label
  4170.     @card_zone_label.show
  4171.   end
  4172.  
  4173.   #--------------------------------------------------------------------------
  4174.   # Clears actions and ends the actors turn
  4175.   #--------------------------------------------------------------------------
  4176.   def command_end_turn
  4177.     if BattleManager.actor && BattleManager.actor.respond_to?(:used_turn_effects)
  4178.       BattleManager.actor.used_turn_effects.clear if BattleManager.actor.used_turn_effects
  4179.     end
  4180.     ($game_party.battle_members + $game_troop.members).each do |battler|
  4181.       next unless battler.alive?
  4182.       battler.states.each do |state|
  4183.         if state.note =~ /<end_phase>/i
  4184.           battler.remove_state(state.id)
  4185.         end
  4186.       end
  4187.     end
  4188.     @return_to_hand_flag = false
  4189.     BattleManager.actor.clear_actions
  4190.     next_command
  4191.   end
  4192.  
  4193.   #--------------------------------------------------------------------------
  4194.   # Prevents messages from closing windows (overwrite)
  4195.   #--------------------------------------------------------------------------
  4196.   def update_message_open
  4197.   end
  4198.  
  4199.   #--------------------------------------------------------------------------
  4200.   # Shows a different Help Window in Battle (alias)
  4201.   #--------------------------------------------------------------------------
  4202.   alias pc27_cardgame_scenebattle_create_help_window          create_help_window
  4203.   def create_help_window
  4204.     pc27_cardgame_scenebattle_create_help_window
  4205.     @cardhelp_window = Window_CardHelp.new
  4206.     @cardhelp_window.visible = false
  4207.     @cardhelp_window.opacity = 0
  4208.     @cardhelp_window.contents_opacity = 0
  4209.     @help_window.hide if @help_window
  4210.     @help_window.opacity = 0 if @help_window
  4211.     @help_window.contents_opacity = 0 if @help_window
  4212.   end
  4213.  
  4214.   #--------------------------------------------------------------------------
  4215.   # Create Skill Window with card help window instead of normal help window (overwrite)
  4216.   #--------------------------------------------------------------------------
  4217.   def create_skill_window
  4218.     @skill_window = Window_BattleSkill.new(@cardhelp_window, @info_viewport)
  4219.     @skill_window.set_handler(:ok,     method(:on_skill_ok))
  4220.     @skill_window.set_handler(:cancel, method(:on_skill_cancel))
  4221.   end
  4222.  
  4223.   #--------------------------------------------------------------------------
  4224.   # Create Extra Window with card help window instead of normal help window (overwrite)
  4225.   #--------------------------------------------------------------------------
  4226.   def create_extra_window
  4227.     @extra_window = Window_BattleSkill.new(@cardhelp_window, @info_viewport)
  4228.     @extra_window.set_handler(:ok,     method(:on_skill_ok))
  4229.     @extra_window.set_handler(:cancel, method(:on_skill_cancel))
  4230.   end
  4231.  
  4232.   #--------------------------------------------------------------------------
  4233.   # Creates the window needed to select cards (new)
  4234.   #--------------------------------------------------------------------------
  4235.   def create_card_select_window
  4236.     @card_select_window = Window_CardSelect.new(@cardhelp_window, @info_viewport)
  4237.   end
  4238.  
  4239.   #--------------------------------------------------------------------------
  4240.   # Adds the select window to the scene (alias)
  4241.   #--------------------------------------------------------------------------
  4242.   alias pc27_cardgame_scenebattle_create_all_windows          create_all_windows
  4243.   def create_all_windows
  4244.     pc27_cardgame_scenebattle_create_all_windows
  4245.     create_card_select_window
  4246.     create_card_zone_label
  4247.   end
  4248.  
  4249.   def create_card_zone_label
  4250.     @card_zone_label = Window_CardZoneLabel.new(
  4251.       @skill_window.x,
  4252.       @skill_window.y
  4253.     )
  4254.     @card_zone_label.hide
  4255.   end
  4256.  
  4257.   alias pc27_cardgame_on_skill_cancel on_skill_cancel
  4258.   def on_skill_cancel
  4259.     refresh_card_zone_label
  4260.     @card_zone_label.hide if @card_zone_label
  4261.     @field_zone_label.hide if @field_zone_label
  4262.     pc27_cardgame_on_skill_cancel
  4263.   end
  4264.  
  4265.   alias pc27_cg_start_cmd_select_fix start_actor_command_selection
  4266.   def start_actor_command_selection
  4267.     if @card_select_window && @card_select_window.visible
  4268.       return
  4269.     elsif @return_to_hand_flag
  4270.       @return_to_hand_flag = false
  4271.       command_look_hand
  4272.       if @card_zone_label
  4273.         refresh_card_zone_label
  4274.         @card_zone_label.visible = true
  4275.         @card_zone_label.show
  4276.       end
  4277.     else
  4278.       pc27_cg_start_cmd_select_fix
  4279.     end
  4280.   end
  4281.  
  4282.   alias pc27_cardgame_scene_battle_update update
  4283.   def update
  4284.     pc27_cardgame_scene_battle_update
  4285.     update_field_preview
  4286.     return unless SceneManager.scene_is?(Scene_Battle)
  4287.     return unless BattleManager.in_turn?
  4288.     return unless BattleManager.actor
  4289.     return unless BattleManager.actor.input
  4290.     update_card_zone_label_visibility
  4291.   end
  4292.  
  4293.   def update_card_zone_label_visibility
  4294.     return unless @card_zone_label
  4295.     return unless @skill_window
  4296.     if @skill_window.visible
  4297.       @card_zone_label.show
  4298.     else
  4299.       refresh_card_zone_label
  4300.       @card_zone_label.hide
  4301.     end
  4302.   end
  4303.  
  4304.   def refresh_card_zone_label
  4305.     return unless @card_zone_label
  4306.     actor = BattleManager.actor
  4307.     return unless actor
  4308.     text = case actor.current_zone
  4309.     when :hand             then "Hand"
  4310.     when :grave            then "Graveyard"
  4311.     when :field            then "Field"
  4312.     when :banish           then "Banished"
  4313.     when :extra            then "Extra Deck"
  4314.     when :deck             then "Deck"
  4315.     else ""
  4316.     end
  4317.     zone = BattleManager.actor.current_zone
  4318.     data = ZONE_LABELS[zone]
  4319.     return unless data
  4320.     text, icon = data
  4321.     @card_zone_label.set_text(text, icon)
  4322.   end
  4323.  
  4324.   def card_has_grave_effect?(skill)
  4325.     skill.note =~ /<grave[-_ ]?effect/i || skill.note =~ /<grave/i
  4326.   end
  4327.  
  4328.   def monster_has_hand_effect?(skill)
  4329.     skill.note =~ /<hand[-_ ]?effect>/i || skill.note =~ /<hand/i
  4330.   end
  4331.  
  4332.   #--------------------------------------------------------------------------
  4333.   # On Skill Ok checks for card costs
  4334.   #--------------------------------------------------------------------------  
  4335.   alias pc27_cardgame_scene_battle_on_skill_ok                       on_skill_ok
  4336.   def on_skill_ok
  4337.     @subject = BattleManager.actor
  4338.     @skill = @skill_window.item
  4339.     @selected_card = @skill
  4340.     case @subject.current_zone
  4341.     when :hand
  4342.       handle_hand_skill(@selected_card)
  4343.     when :field
  4344.       handle_field_skill(@selected_card)
  4345.     when :grave
  4346.       handle_graveyard_skill(@selected_card)
  4347.     else
  4348.     end
  4349.   end
  4350.  
  4351.   def resolve_card_effect(skill)
  4352.     if skill.cost
  4353.       invoke_card_cost(skill.cost)
  4354.     else
  4355.       skill_select(1)
  4356.     end
  4357.   end
  4358.  
  4359.   def show_advanced_options(options, disabled_indices, &block)
  4360.     @advanced_options_window = Window_CardOptions.new(options, disabled_indices)
  4361.     result = nil
  4362.     @advanced_options_window.set_handler(:ok, Proc.new { result = @advanced_options_window.index })
  4363.     @advanced_options_window.set_handler(:cancel, Proc.new { result = options.index("Cancel") || (options.size - 1) })
  4364.     loop do
  4365.       update_basic if respond_to?(:update_basic)
  4366.       if result != nil
  4367.         @advanced_options_window.dispose
  4368.         @advanced_options_window = nil
  4369.         block.call(result)
  4370.         break
  4371.       end
  4372.     end
  4373.   end
  4374.  
  4375.   def handle_hand_skill(skill)
  4376.     @selected_card = skill
  4377.     case skill.type
  4378.     when 1
  4379.       options = []
  4380.       disabled_indices = []
  4381.       has_space = (skill.subtype == 27 || BattleManager.actor.st_zone_available?)
  4382.       can_act = true
  4383.       impact_name = nil
  4384.       if skill.note =~ /<card impact:\s*(.+?)>/i || skill.note =~ /<spell_impact:\s*(.+?)>/i || skill.note =~ /<hand_impact:\s*(.+?)>/i
  4385.         impact_name = $1.to_s.strip
  4386.       end
  4387.       if impact_name
  4388.         can_act = false unless BattleManager.actor.can_activate_impact?(impact_name)
  4389.       end
  4390.       can_act = false unless BattleManager.actor.custom_req_met?(skill) && BattleManager.actor.can_pay_card_cost?(skill)
  4391.       options << "Activate"
  4392.       disabled_indices << (options.size - 1) unless can_act
  4393.       options << "Set" if has_space
  4394.       options << "Cancel"
  4395.       show_advanced_options(options, disabled_indices) do |choice|
  4396.         selected = options[choice]
  4397.         case selected
  4398.         when "Activate"
  4399.           $game_message.choice_proc = nil
  4400.           $game_message.clear
  4401.           if @message_window
  4402.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4403.             choice_window.openness = 0 if choice_window
  4404.           end
  4405.           placed_index = BattleManager.actor.spell_to_field?(skill)
  4406.           if placed_index
  4407.            
  4408.             # =========================================================
  4409.             # FIX DE CLONACIÓN: INDEPENDENCIA DE OBJETOS EN CAMPO
  4410.             # =========================================================
  4411.             cloned_skill = skill.dup
  4412.             if placed_index == :field
  4413.               BattleManager.actor.field_zone = cloned_skill
  4414.             else
  4415.               BattleManager.actor.st_zones[placed_index] = cloned_skill
  4416.             end
  4417.             idx_f = BattleManager.actor.field.index(skill)
  4418.             BattleManager.actor.field[idx_f] = cloned_skill if idx_f
  4419.             skill = cloned_skill # Reasignamos la variable para el resto del código
  4420.             # =========================================================
  4421.  
  4422.             @active_effect_zone_type = (skill.subtype == 27 ? :fieldspell : :spell)
  4423.             @active_effect_zone_index = (placed_index == :field ? nil : placed_index)
  4424.             @field_map_window.refresh if @field_map_window
  4425.             @field_preview_window.set_card(nil) if @field_preview_window
  4426.             @skill_window.hide
  4427.             @skill_window.deactivate
  4428.             @card_zone_label.hide if @card_zone_label
  4429.            
  4430.             if impact_name
  4431.               resolve_card_effect(skill)
  4432.             else
  4433.               @active_effect_card = nil
  4434.               @current_processing_card = nil
  4435.               $temp_active_card = nil
  4436.               $chain_memory_card = nil
  4437.               show_set_card_visuals rescue nil
  4438.               @skill_window.make_item_list
  4439.               @skill_window.refresh
  4440.               @skill_window.show
  4441.               @skill_window.activate
  4442.               if @card_zone_label
  4443.                 refresh_card_zone_label
  4444.                 @card_zone_label.visible = true
  4445.                 @card_zone_label.show
  4446.               end
  4447.             end
  4448.           else
  4449.             Sound.play_buzzer
  4450.             @skill_window.activate
  4451.           end
  4452.         when "Set"
  4453.           $game_message.choice_proc = nil
  4454.           $game_message.clear
  4455.           if @message_window
  4456.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4457.             choice_window.openness = 0 if choice_window
  4458.           end
  4459.           @active_effect_card = nil
  4460.           @current_processing_card = nil
  4461.           $temp_active_card = nil
  4462.           $chain_memory_card = nil
  4463.           idx_hand = BattleManager.actor.hand.index { |c| c.equal?(skill) }
  4464.           success = false
  4465.          
  4466.           cloned_skill = skill.dup # FIX CLONACIÓN
  4467.          
  4468.           if skill.subtype == 27
  4469.             if BattleManager.actor.field_zone != nil
  4470.               BattleManager.actor.grave.push(BattleManager.actor.field_zone)
  4471.               BattleManager.actor.field.delete(BattleManager.actor.field_zone)
  4472.             end
  4473.             BattleManager.actor.hand.delete_at(idx_hand) if idx_hand
  4474.             BattleManager.actor.field_zone = cloned_skill
  4475.             BattleManager.actor.field.push(cloned_skill)
  4476.             BattleManager.actor.field_zone_state = { face_down: true, set_this_turn: true }
  4477.             success = true
  4478.           else
  4479.             idx = BattleManager.actor.st_zones.index(nil)
  4480.             if idx && idx_hand
  4481.               BattleManager.actor.hand.delete_at(idx_hand)
  4482.               BattleManager.actor.st_zones[idx] = cloned_skill
  4483.               BattleManager.actor.field.push(cloned_skill)
  4484.               BattleManager.actor.st_zones_state ||= []
  4485.               BattleManager.actor.st_zones_state[idx] = { face_down: true, set_this_turn: true }
  4486.               success = true
  4487.             end
  4488.           end
  4489.           if success
  4490.             @field_map_window.refresh if @field_map_window
  4491.             @field_preview_window.set_card(nil) if @field_preview_window
  4492.             show_set_card_visuals
  4493.             @skill_window.make_item_list
  4494.             @skill_window.refresh
  4495.             @skill_window.show
  4496.             @skill_window.activate
  4497.             if @card_zone_label
  4498.               refresh_card_zone_label
  4499.               @card_zone_label.visible = true
  4500.               @card_zone_label.show
  4501.             end
  4502.           else
  4503.             Sound.play_buzzer
  4504.             @skill_window.activate
  4505.           end
  4506.         when "Cancel"
  4507.           $game_message.choice_proc = nil
  4508.           $game_message.clear
  4509.           @skill_window.activate
  4510.         end
  4511.       end
  4512.     when 2
  4513.       show_advanced_options(["Set", "Cancel"], []) do |choice|
  4514.         if choice == 0
  4515.           $game_message.choice_proc = nil
  4516.           $game_message.clear
  4517.           if @message_window
  4518.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4519.             choice_window.openness = 0 if choice_window
  4520.           end
  4521.           @active_effect_card = nil
  4522.           @current_processing_card = nil
  4523.           $temp_active_card = nil
  4524.           $chain_memory_card = nil
  4525.           idx_hand = BattleManager.actor.hand.index { |c| c.equal?(skill) }
  4526.           idx = BattleManager.actor.st_zones.index(nil)
  4527.           if idx && idx_hand
  4528.             cloned_skill = skill.dup # FIX CLONACIÓN
  4529.             BattleManager.actor.hand.delete_at(idx_hand)
  4530.             BattleManager.actor.st_zones[idx] = cloned_skill
  4531.             BattleManager.actor.field.push(cloned_skill)
  4532.             BattleManager.actor.st_zones_state ||= []
  4533.             BattleManager.actor.st_zones_state[idx] = { face_down: true, set_this_turn: true }
  4534.             @field_map_window.refresh if @field_map_window
  4535.             @field_preview_window.set_card(nil) if @field_preview_window
  4536.             show_set_card_visuals
  4537.             @skill_window.make_item_list
  4538.             @skill_window.refresh
  4539.             @skill_window.show
  4540.             @skill_window.activate
  4541.             if @card_zone_label
  4542.               refresh_card_zone_label
  4543.               @card_zone_label.visible = true
  4544.               @card_zone_label.show
  4545.             end
  4546.           else
  4547.             Sound.play_buzzer
  4548.             @skill_window.activate
  4549.           end
  4550.         else
  4551.           $game_message.choice_proc = nil
  4552.           $game_message.clear
  4553.           @skill_window.activate
  4554.         end
  4555.       end
  4556.     when 3
  4557.       special_eff = nil
  4558.       is_special_only = false
  4559.       is_special = false
  4560.       if skill.note =~ /<special_only:\s*(.+?)>/i
  4561.         is_special_only = true
  4562.         special_eff = $1.to_s.strip
  4563.       elsif skill.note =~ /<special:\s*(.+?)>/i
  4564.         is_special = true
  4565.         special_eff = $1.to_s.strip
  4566.       end
  4567.       options = []
  4568.       disabled_indices = []
  4569.       if !is_special_only && @subject.tp >= skill.tp_cost
  4570.         if BattleManager.actor.free_monster_zone_available?
  4571.           options << "Normal Summon (ATK)"
  4572.           options << "Normal Summon (DEF)"
  4573.           options << "Set"
  4574.         end
  4575.       end
  4576.       if special_eff && BattleManager.actor.free_monster_zone_available?
  4577.         if BattleManager.actor.meets_activation_requirements?(skill, special_eff)
  4578.           options << "Special Summon"
  4579.           if @subject.state?(18)
  4580.             disabled_indices << (options.size - 1)
  4581.           end
  4582.         end
  4583.       end
  4584.       if skill.hand_impact && BattleManager.actor.can_activate_impact?(skill.hand_impact) && BattleManager.actor.custom_req_met?(skill) && BattleManager.actor.can_pay_card_cost?(skill)
  4585.         options << "Activate"
  4586.       end
  4587.       options << "Cancel"
  4588.       show_advanced_options(options, disabled_indices) do |choice|
  4589.         selected = options[choice]
  4590.         case selected
  4591.         when "Normal Summon (ATK)", "Normal Summon (DEF)", "Set"
  4592.           $game_message.choice_proc = nil
  4593.           $game_message.clear
  4594.           if @message_window
  4595.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4596.             choice_window.openness = 0 if choice_window
  4597.           end
  4598.           @last_summon_is_set = (selected == "Set")
  4599.           zone_index = BattleManager.actor.card_to_field?(skill.id)
  4600.           if !zone_index.nil?
  4601.            
  4602.             # FIX CLONACIÓN MONSTRUOS
  4603.             cloned_skill = skill.dup
  4604.             BattleManager.actor.monster_zones[zone_index] = cloned_skill
  4605.             idx_f = BattleManager.actor.field.index { |c| c && c.id == skill.id }
  4606.             BattleManager.actor.field[idx_f] = cloned_skill if idx_f
  4607.             skill = cloned_skill
  4608.            
  4609.             state = BattleManager.actor.monster_zones_state[zone_index]
  4610.             if selected == "Normal Summon (ATK)"
  4611.               state[:position] = :attack
  4612.               state[:face_down] = false
  4613.             elsif selected == "Normal Summon (DEF)"
  4614.               state[:position] = :defense
  4615.               state[:face_down] = false
  4616.             elsif selected == "Set"
  4617.               state[:position] = :defense
  4618.               state[:face_down] = true
  4619.             end
  4620.             state[:changed_this_turn] = true
  4621.             @field_map_window.refresh if @field_map_window
  4622.             @field_preview_window.set_card(nil) if @field_preview_window
  4623.             @skill_window.hide
  4624.             @card_zone_label.hide if @card_zone_label
  4625.             if selected == "Set"
  4626.               @active_effect_card = nil
  4627.               @current_processing_card = nil
  4628.               $temp_active_card = nil
  4629.               $chain_memory_card = nil
  4630.               show_set_card_visuals
  4631.             else
  4632.               show_special_summon_visuals(skill)
  4633.             end
  4634.             @current_card_action = :summon
  4635.             skill_select(1)
  4636.           else
  4637.             Sound.play_buzzer
  4638.             @skill_window.activate
  4639.           end
  4640.         when "Special Summon"
  4641.           $game_message.choice_proc = nil
  4642.           $game_message.clear
  4643.           if @message_window
  4644.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4645.             choice_window.openness = 0 if choice_window
  4646.           end
  4647.           @field_map_window.refresh if @field_map_window
  4648.           @skill_window.hide.deactivate
  4649.           @card_zone_label.hide if @card_zone_label
  4650.           @active_effect_card = skill
  4651.           @current_processing_card = skill
  4652.           $temp_active_card = skill
  4653.           $chain_memory_card = skill
  4654.           invoke_card_impact(special_eff)
  4655.         when "Activate"
  4656.           $game_message.choice_proc = nil
  4657.           $game_message.clear
  4658.           if @message_window
  4659.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4660.             choice_window.openness = 0 if choice_window
  4661.           end
  4662.           BattleManager.actor.card_to_stack?(skill.id)
  4663.           @current_card_action = :hand_effect
  4664.           resolve_card_effect(skill)
  4665.         when "Cancel"
  4666.           $game_message.choice_proc = nil
  4667.           $game_message.clear
  4668.           @skill_window.activate
  4669.         end
  4670.       end
  4671.     end
  4672.   end
  4673.  
  4674.   def show_options(choices, cancel_type, &block)
  4675.     choices.each { |s| $game_message.choices.push(s) }
  4676.     $game_message.choice_cancel_type = cancel_type
  4677.     $game_message.choice_proc = Proc.new { |n| block.call(n) }
  4678.   end
  4679.    
  4680.   def skill_select(value)
  4681.     @skill_window.hide if @skill_window.visible
  4682.     @card_zone_label.hide if @card_zone_label.visible
  4683.     $game_message.clear
  4684.     $game_message.visible = false
  4685.     if @skill.cost
  4686.       invoke_card_cost(@skill.cost)
  4687.       @skill_window.hide
  4688.       @card_zone_label.hide
  4689.     else
  4690.       effect = @skill.handeff if value == 0
  4691.       effect = @skill.id
  4692.       @current_card_action = :hand_effect if value == 0
  4693.       @current_card_action = :summon if value == 1  
  4694.       BattleManager.actor.input.set_skill(effect)
  4695.       BattleManager.actor.last_skill.object = @skill
  4696.       if !@skill.need_selection?
  4697.         @skill_window.hide
  4698.         @card_zone_label.hide
  4699.         next_command
  4700.       elsif @skill.for_opponent?
  4701.         select_enemy_selection
  4702.       else
  4703.         select_actor_selection
  4704.       end
  4705.     end
  4706.   end
  4707.  
  4708.   def skill_field_select(value)
  4709.     $game_message.clear
  4710.     $game_message.visible = false
  4711.     if @skill.cost
  4712.       invoke_card_cost(@skill.cost)
  4713.       @skill_window.hide
  4714.       @card_zone_label.hide
  4715.     else
  4716.       @skill.id = @skill.fieldeff if value == 1
  4717.       BattleManager.actor.input.set_skill(@skill.id)
  4718.       BattleManager.actor.last_skill.object = @skill
  4719.       if !@skill.need_selection?
  4720.         @skill_window.hide
  4721.         @card_zone_label.hide
  4722.         next_command
  4723.       elsif @skill.for_opponent?
  4724.         select_enemy_selection
  4725.       else
  4726.         select_actor_selection
  4727.       end
  4728.     end
  4729.   end
  4730.  
  4731.   def on_enemy_cancel
  4732.     restore_taken_cards(:cancel)
  4733.     BattleManager.actor.hand += BattleManager.actor.stack
  4734.     BattleManager.actor.stack = []
  4735.     ygo_rescue_canceled_card
  4736.     @enemy_window.hide
  4737.     @skill_window.hide
  4738.     @card_zone_label.hide if @card_zone_label
  4739.     @item_window.hide
  4740.     @status_window.show
  4741.     @actor_command_window.activate
  4742.     refresh_card_zone_label if respond_to?(:refresh_card_zone_label)
  4743.     @card_zone_label.hide if @card_zone_label
  4744.   end
  4745.  
  4746.   def on_actor_cancel
  4747.     restore_taken_cards(:cancel)
  4748.     BattleManager.actor.hand += BattleManager.actor.stack
  4749.     BattleManager.actor.stack = []
  4750.     ygo_rescue_canceled_card
  4751.     @actor_window.hide
  4752.     @skill_window.hide
  4753.     @item_window.hide
  4754.     @status_window.show
  4755.     @actor_command_window.activate
  4756.     refresh_card_zone_label if respond_to?(:refresh_card_zone_label)
  4757.     @card_zone_label.hide if @card_zone_label
  4758.   end
  4759.  
  4760.   def ygo_rescue_canceled_card
  4761.     actor = BattleManager.actor
  4762.     return unless actor
  4763.     card = $game_temp.battle_aid || actor.input.item || @skill || @active_effect_card rescue nil
  4764.     return unless card
  4765.     if actor.respond_to?(:st_zones) && actor.st_zones
  4766.       idx = actor.st_zones.index { |c| c && c.equal?(card) }
  4767.       if idx
  4768.         actor.st_zones[idx] = nil
  4769.         actor.hand << card unless actor.hand.include?(card)
  4770.         idx_field = actor.field.index { |c| c && c.equal?(card) }
  4771.         actor.field.delete_at(idx_field) if idx_field
  4772.       end
  4773.     end
  4774.     if actor.respond_to?(:monster_zones) && actor.monster_zones
  4775.       idx = actor.monster_zones.index { |c| c && c.equal?(card) }
  4776.       if idx
  4777.         actor.monster_zones[idx] = nil
  4778.         actor.hand << card unless actor.hand.include?(card)
  4779.         idx_field = actor.field.index { |c| c && c.equal?(card) }
  4780.         actor.field.delete_at(idx_field) if idx_field
  4781.       end
  4782.     end
  4783.     @minifield_window.refresh if defined?(@minifield_window) && @minifield_window
  4784.     @field_map_window.refresh if defined?(@field_map_window) && @field_map_window
  4785.   end
  4786.  
  4787.   #-----------------------------------------------------------------------------
  4788.   # Changes Next Command so it performs Actions (alias)
  4789.   #-----------------------------------------------------------------------------
  4790.   alias pc27_cardgame_scenebattle_next_command                      next_command
  4791.   def next_command
  4792.     return if @card_select_window.active
  4793.     if action?
  4794.       evaluate_item(BattleManager.actor, BattleManager.actor.input.item)
  4795.       perform_action
  4796.     else
  4797.       pc27_cardgame_scenebattle_next_command
  4798.     end
  4799.   end
  4800.  
  4801.   #-----------------------------------------------------------------------------
  4802.   # Disables stuff based on used skill (new)
  4803.   #-----------------------------------------------------------------------------
  4804.   def evaluate_item(actor, item)
  4805.     case item.id
  4806.     when actor.attack_skill_id
  4807.       actor.turn_actions[:can_attack] = false
  4808.       actor.turn_actions[:can_guard]  = false
  4809.       actor.turn_actions[:actions]    -= 1 unless actor.turn_actions[:actions] == -1
  4810.     when actor.guard_skill_id
  4811.       actor.turn_actions[:can_attack] = false
  4812.       actor.turn_actions[:can_guard]  = false
  4813.       actor.turn_actions[:actions]    -= 1 unless actor.turn_actions[:actions] == -1
  4814.     end
  4815.   end
  4816.  
  4817.   #--------------------------------------------------------------------------
  4818.   # Checks if there is an Action to use (new)
  4819.   #--------------------------------------------------------------------------
  4820.   def action?
  4821.     return false if BattleManager.actor.nil?
  4822.     return false if BattleManager.actor.input.nil?
  4823.     action = BattleManager.actor.input.item
  4824.     return false if action.nil?
  4825.     return true
  4826.   end
  4827.  
  4828.   #--------------------------------------------------------------------------
  4829.   # Performs Action (new)
  4830.   #--------------------------------------------------------------------------
  4831.   def perform_action
  4832.     @subject = BattleManager.actor
  4833.     hide_instant_action_windows
  4834.     if @subject.current_action.valid?
  4835.       execute_action
  4836.     end
  4837.     process_event
  4838.     loop do
  4839.       @subject.remove_current_action
  4840.       break if $game_troop.all_dead?
  4841.       break unless @subject.current_action
  4842.       @subject.current_action.prepare
  4843.       execute_action if @subject.current_action.valid?
  4844.     end
  4845.     process_action_end
  4846.     @subject.make_actions
  4847.     @subject = nil
  4848.     show_instant_action_windows
  4849.     refresh_card_zone_label
  4850.     @card_zone_label.hide
  4851.   end
  4852.  
  4853.   def set_card
  4854.     @subject = BattleManager.actor
  4855.     @subject.field += @subject.stack
  4856.     @subject.stack = []
  4857.     restore_taken_cards(:proceed)
  4858.     @skill_window.hide
  4859.     refresh_card_zone_label
  4860.     @field_zone_label.hide
  4861.     @card_zone_label.hide
  4862.     @actor_command_window.activate
  4863.   end
  4864.  
  4865.   #--------------------------------------------------------------------------
  4866.   # From Yanflys Instantcast (new)
  4867.   #--------------------------------------------------------------------------
  4868.   def hide_instant_action_windows
  4869.     if $imported["YEA-BattleEngine"]
  4870.       @info_viewport.visible = true
  4871.       @status_aid_window.hide
  4872.       @status_window.show
  4873.       @actor_command_window.show
  4874.     end
  4875.   end
  4876.  
  4877.   #--------------------------------------------------------------------------
  4878.   # Activates actor (new)
  4879.   #--------------------------------------------------------------------------
  4880.   def show_instant_action_windows
  4881.     if $imported["YEA-BattleEngine"]
  4882.       @info_viewport.visible = true
  4883.     end
  4884.     start_actor_command_selection unless @card_select_window.active
  4885.     @card_zone_label.hide if @card_zone_label.visible
  4886.     status_redraw_target(BattleManager.actor)
  4887.     next_command unless BattleManager.actor.inputable?
  4888.   end
  4889.  
  4890.   #--------------------------------------------------------------------------
  4891.   # Redrawas target after action (new)
  4892.   #--------------------------------------------------------------------------
  4893.   def status_redraw_target(target)
  4894.     return unless target.actor?
  4895.     @status_window.draw_item($game_party.battle_members.index(target))
  4896.   end
  4897.  
  4898.   #--------------------------------------------------------------------------
  4899.   # Handles card costs
  4900.   #--------------------------------------------------------------------------
  4901.   def invoke_card_cost(effect_name)
  4902.     effect_array = PC27::CG::CARD_EFFECT[effect_name]
  4903.     if effect_array[0] == :draw
  4904.       card_effect_draw_cards(effect_array[1], effect_array[2], effect_array[3])
  4905.       BattleManager.actor.input.set_skill(@skill.id)
  4906.       BattleManager.actor.last_skill.object = @skill
  4907.       if !@skill.need_selection?
  4908.         @skill_window.hide
  4909.         @card_zone_label.hide
  4910.         next_command
  4911.       elsif @skill.for_opponent?
  4912.         select_enemy_selection
  4913.       else
  4914.         select_actor_selection
  4915.       end
  4916.     else
  4917.       $game_message.background = 1
  4918.       $game_message.position   = 1
  4919.       $game_message.add(PC27::CG::CARD_EFF_TEXT)
  4920.       prepare_select_window(:cost, effect_array[0])
  4921.       @temp_effect_data = [] unless @temp_effect_data
  4922.       @temp_effect_data[0] = :cost
  4923.       process_select_cards(effect_name)
  4924.     end
  4925.   end
  4926.  
  4927.   #-----------------------------------------------------------------------------
  4928.   # Monster's inherent summon
  4929.   #-----------------------------------------------------------------------------
  4930.   def invoke_inherent_summon(card, effect_name)
  4931.     array = PC27::CG::CARD_EFFECT[effect_name]
  4932.     prepare_select_window(:cost, array[0])
  4933.     @temp_effect_data = [] unless @temp_effect_data
  4934.     @temp_effect_data[0] = :cost
  4935.     @temp_effect_data[1] = effect_name
  4936.     @inherent_summon_pending = card
  4937.     process_select_cards(effect_name)
  4938.   end
  4939.  
  4940.   #-----------------------------------------------------------------------------
  4941.   # Evaluates card effects and puts the stack to the grave (alias)
  4942.   #-----------------------------------------------------------------------------
  4943.   alias pc27_cardgame_scenebattle_use_item use_item
  4944.   def use_item
  4945.     pc27_cardgame_scenebattle_use_item
  4946.     return unless @subject.is_a?(Game_Actor)
  4947.     card = @subject.current_action.item
  4948.     if card && card.id == 1
  4949.       @subject.used_turn_effects ||= []
  4950.       @subject.used_turn_effects << "attack_declared"
  4951.     end
  4952.     effect_name = nil
  4953.     if card.type == 3
  4954.       if @current_card_action == :hand_effect
  4955.         effect_name = card.hand_impact
  4956.       elsif @current_card_action != :summon
  4957.         effect_name = card.impact
  4958.       end
  4959.     else
  4960.       effect_name = card.impact
  4961.     end
  4962.     invoke_card_impact(effect_name) if effect_name
  4963.     if card.type == 3 && @current_card_action == :summon
  4964.       is_face_down = card.instance_variable_get(:@face_down)
  4965.       if !@last_summon_is_set && card.summon_impact && BattleManager.actor.can_activate_summon_effect?(card)
  4966.         $game_message.clear
  4967.         if @message_window
  4968.           @message_window.openness = 0
  4969.           @message_window.visible = true
  4970.         end
  4971.         $game_message.background = 1
  4972.         $game_message.position = 1  
  4973.         $game_message.add("Do you want to activate the effect of")
  4974.         $game_message.add('"' + card.name + '"?')
  4975.         @status_window.hide if @status_window
  4976.         @status_back_window.hide if @status_back_window
  4977.         show_options(["Yes", "No"], 2) do |choice|
  4978.           @status_window.show if @status_window
  4979.           @status_back_window.show if @status_back_window
  4980.           $game_message.choice_proc = nil
  4981.           $game_message.clear
  4982.           if @message_window
  4983.             choice_window = @message_window.instance_variable_get(:@choice_window)
  4984.             choice_window.openness = 0 if choice_window
  4985.           end
  4986.           if choice == 0
  4987.             BattleManager.actor.used_turn_effects ||= []
  4988.             BattleManager.actor.used_turn_effects << card.summon_impact
  4989.             if card.note =~ /<shared_opt:\s*(.+?)>/i
  4990.               BattleManager.actor.used_turn_effects << $1.to_s.strip
  4991.             end
  4992.             @active_effect_owner = BattleManager.actor
  4993.             @active_effect_card = card
  4994.             $temp_active_card = card
  4995.             $chain_memory_card = card
  4996.             @current_processing_card = card
  4997.             invoke_card_impact(card.summon_impact)
  4998.           else
  4999.             @return_to_hand_flag = true
  5000.             if @actor_command_window
  5001.               @actor_command_window.hide.deactivate
  5002.             end
  5003.             command_look_hand
  5004.           end
  5005.         end
  5006.         wait_for_message
  5007.         return
  5008.       else
  5009.         @return_to_hand_flag = true
  5010.       end
  5011.     end
  5012.     if @subject.stack.size > 0
  5013.       @subject.grave += @subject.stack
  5014.       @subject.stack = []
  5015.     end
  5016.     if !effect_name && @subject.current_zone == :hand
  5017.       @return_to_hand_flag = true
  5018.     end
  5019.   end
  5020.  
  5021.   #-----------------------------------------------------------------------------
  5022.   # Intercepta el menú de comandos para volver a la mano si es necesario
  5023.   #-----------------------------------------------------------------------------
  5024.   alias pc27_cg_start_actor_command_selection start_actor_command_selection
  5025.   def start_actor_command_selection
  5026.     if @return_to_hand_flag
  5027.       @return_to_hand_flag = false
  5028.       command_look_hand
  5029.       if @card_zone_label
  5030.         refresh_card_zone_label
  5031.         @card_zone_label.visible = true
  5032.         @card_zone_label.show
  5033.       end
  5034.     else
  5035.       pc27_cg_start_actor_command_selection
  5036.     end
  5037.   end
  5038.  
  5039.   #-----------------------------------------------------------------------------
  5040.   # Forzamos la vigilancia de la etiqueta en el bucle principal de la escena
  5041.   #-----------------------------------------------------------------------------
  5042.   alias pc27_cg_scenebattle_update update
  5043.   def update
  5044.     pc27_cg_scenebattle_update
  5045.     if @field_ui_created && @skill_window && (@skill_window.visible || @skill_window.active)
  5046.       @skill_window.hide
  5047.       @skill_window.deactivate
  5048.     end
  5049.     if @card_select_window && @card_select_window.active && @skill_window && (@skill_window.visible || @skill_window.active)
  5050.       @skill_window.hide
  5051.       @skill_window.deactivate
  5052.     end
  5053.     if @skill_window && @skill_window.visible && @card_zone_label
  5054.       update_card_zone_label_visibility
  5055.     end
  5056.   end
  5057.  
  5058.   #-----------------------------------------------------------------------------
  5059.   # Card effects
  5060.   #-----------------------------------------------------------------------------
  5061.   def invoke_card_impact(effect_name)
  5062.     return unless effect_name
  5063.     @active_effect_card = @current_processing_card if @current_processing_card
  5064.     if @active_effect_card && @active_effect_card.is_a?(RPG::UsableItem) && @last_state_card_id != @active_effect_card.object_id
  5065.       @last_state_card_id = @active_effect_card.object_id
  5066.       player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  5067.       if player && player.alive?
  5068.         @active_effect_card.effects.each do |eff|
  5069.           player.add_state(eff.data_id) if eff.code == 21
  5070.         end
  5071.       end
  5072.     end
  5073.     array = PC27::CG::CARD_EFFECT[effect_name]
  5074.     if array[0] == :draw
  5075.       card_effect_draw_cards(array[1], array[2], array[3])
  5076.       next_eff = array.find { |e| e.is_a?(String) && PC27::CG::CARD_EFFECT.key?(e) }
  5077.       if next_eff
  5078.         invoke_card_impact(next_eff)
  5079.       else
  5080.         finish_proceed_selection
  5081.       end
  5082.     elsif array[0] == :none
  5083.       finish_proceed_selection
  5084.     elsif array[0] == :quick_reactive
  5085.       process_quick_reactive(array)
  5086.     elsif array[0] == :add_state
  5087.       target_type = array[2]
  5088.       state_id = array[3]
  5089.       player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  5090.       if target_type == :user
  5091.         player.add_state(state_id)
  5092.         RPG::SE.new("Skill2", 80, 100).play
  5093.       end
  5094.       finish_proceed_selection
  5095.     else
  5096.       prepare_select_window(:impact, array[0])
  5097.       @temp_effect_data = [] unless @temp_effect_data
  5098.       @temp_effect_data[0] = :impact
  5099.       process_select_cards(effect_name)
  5100.     end
  5101.   end
  5102.  
  5103.   def process_quick_reactive(array)
  5104.     player = @current_quick_owner || $game_party.battle_members[0]
  5105.     icon = @active_effect_card ? @active_effect_card.icon_index : 0
  5106.     total_heal = 0
  5107.     total_damage = 0
  5108.     array.each do |instruction|
  5109.       case instruction.to_s
  5110.       when "negate_attack"
  5111.         @attack_negated = true
  5112.       when "heal_atk"
  5113.         if @current_quick_subject
  5114.           total_heal += @current_quick_subject.atk
  5115.         end
  5116.       when /^heal_(\d+)$/i
  5117.         total_heal += $1.to_i
  5118.       when /^heal_(\d+)_x_(\d+)_grave$/i
  5119.         multiplier = $1.to_i
  5120.         target_card_id = $2.to_i
  5121.         if player && player.respond_to?(:grave)
  5122.           cards_in_grave = player.grave.count { |c| c && c.id == target_card_id }
  5123.           if @active_effect_card && @active_effect_card.id == target_card_id
  5124.             cards_in_grave -= 1
  5125.           end
  5126.           if cards_in_grave > 0
  5127.             total_heal += (multiplier * cards_in_grave)
  5128.           end
  5129.         end
  5130.       when /^damage_(\d+)$/i
  5131.         total_damage += $1.to_i
  5132.       when /^damage_(\d+)_x_(\d+)_grave$/i
  5133.         multiplier = $1.to_i
  5134.         target_card_id = $2.to_i
  5135.         enemy = $game_troop.members[0]
  5136.         if player && player.respond_to?(:grave) && enemy && enemy.alive?
  5137.           cards_in_grave = player.grave.count { |c| c && c.id == target_card_id }
  5138.           if @active_effect_card && @active_effect_card.id == target_card_id
  5139.             cards_in_grave -= 1
  5140.           end
  5141.           if cards_in_grave > 0
  5142.             total_damage += (multiplier * cards_in_grave)
  5143.           end
  5144.         end
  5145.       end
  5146.     end
  5147.     if total_heal > 0 && player
  5148.       player.hp = player.hp + total_heal
  5149.       player.damage.push([-total_heal, "HP", false, icon])
  5150.     end
  5151.     enemy = $game_troop.members[0]
  5152.     if total_damage > 0 && enemy && enemy.alive?
  5153.       enemy.hp = [enemy.hp - total_damage, 0].max
  5154.       enemy.damage.push([total_damage, "HP", false, icon])
  5155.     end
  5156.     finish_proceed_selection if respond_to?(:finish_proceed_selection)
  5157.   end
  5158.   #-----------------------------------------------------------------------------
  5159.   def card_effect_draw_cards(nr, prob, trg)
  5160.     need_wait = false
  5161.     drawer = @active_effect_owner || (@subject && @subject.actor? ? @subject : nil) || BattleManager.actor || $game_party.battle_members[0]
  5162.       if trg == :self
  5163.       if rand(100) < prob
  5164.         drawer.draw_cards(nr)
  5165.         need_wait = true
  5166.       end
  5167.     elsif trg == :target
  5168.       if @subject && @subject.current_action
  5169.         targets = @subject.current_action.make_targets.compact
  5170.         for target in targets
  5171.           if rand(100) < prob
  5172.             target.draw_cards(nr)
  5173.             need_wait = true
  5174.           end
  5175.         end
  5176.       end
  5177.     end
  5178.     wait_for_message if need_wait
  5179.   end
  5180.    
  5181.   #-----------------------------------------------------------------------------
  5182.   def process_select_cards(effect_name)
  5183.     $game_message.clear
  5184.     $game_message.visible = false
  5185.     @card_select_window.zone_tags = []
  5186.     @message_window.deactivate if @message_window
  5187.     @message_window.hide if @message_window
  5188.     @skill_window.hide
  5189.     @skill_window.deactivate
  5190.     @actor_command_window.deactivate if @actor_command_window
  5191.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  5192.     @card_select_window.actor = player
  5193.     actor = player
  5194.     active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5195.     @active_effect_card = active_c
  5196.     array = PC27::CG::CARD_EFFECT[effect_name]
  5197.     ui_filter = array[4]
  5198.     ui_active = active_c
  5199.     ui_sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  5200.     ui_actor = actor
  5201.     @card_select_window.define_singleton_method(:enable?) do |item, source_tag = nil, z_idx = nil|
  5202.       return false if item.nil?
  5203.       src = source_tag
  5204.       if src.nil? && self.zone_tags && self.cards
  5205.         idx = self.cards.index(item)
  5206.         src = self.zone_tags[idx] if idx
  5207.       end
  5208.       return ui_actor.match_complex_filter?(item, ui_filter, ui_active, ui_sim_lvl, z_idx, src)
  5209.     end
  5210.     @temp_effect_data[1] = effect_name
  5211.     @temp_effect_data[2] = 0
  5212.     set_select_filter(array)
  5213.     @card_select_window.dry_run_eff2 = nil
  5214.     if active_c && active_c.note =~ /<req_dry_run:\s*#{effect_name}\s*,\s*([^,>]+)(?:\s*,\s*([^>]+))?>/i
  5215.       target_eff = $2 ? $2.to_s.strip : $1.to_s.strip
  5216.       @card_select_window.dry_run_eff2 = target_eff
  5217.     end
  5218.     case array[2]
  5219.     when :self_banished
  5220.       @temp_effect_data[3] = [$chain_memory_card] if $chain_memory_card
  5221.       restore_taken_cards(:proceed)
  5222.       return
  5223.     when :hand_or_field
  5224.       actor.current_zone = :hand_or_field
  5225.       @field_zone_label.hide if @field_zone_label
  5226.       @card_select_window.zone_tags = []
  5227.       @card_select_window.cards = []
  5228.       active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5229.       filter = array[4]
  5230.       exclude = false
  5231.       if filter.is_a?(Array)
  5232.         if filter[0].is_a?(Array)
  5233.           exclude = filter.any? { |f| f[11] == :exclude_active }
  5234.         else
  5235.           exclude = (filter[11] == :exclude_active)
  5236.         end
  5237.       end
  5238.       dry_run_filter = nil
  5239.       dry_run_zone = nil
  5240.       if @card_select_window.dry_run_eff2
  5241.         eff2_data = PC27::CG::CARD_EFFECT[@card_select_window.dry_run_eff2]
  5242.         if eff2_data
  5243.           dry_run_filter = eff2_data[4]
  5244.           dry_run_zone = eff2_data[2]
  5245.         end
  5246.       end
  5247.       check_validity = lambda do |card|
  5248.         next false if card.nil?
  5249.         next false if exclude && active_c && card.id == active_c.id
  5250.         if dry_run_filter && dry_run_zone == :deck
  5251.           has_future_target = actor.deck.any? do |d_card|
  5252.             next false if d_card.nil?
  5253.             actor.match_complex_filter?(d_card, dry_run_filter, active_c, card.level)
  5254.           end
  5255.           next false unless has_future_target
  5256.         end
  5257.         true
  5258.       end
  5259.       actor.hand.each do |card|
  5260.         if check_validity.call(card) && @card_select_window.enable?(card, :hand)
  5261.           @card_select_window.cards << card
  5262.           @card_select_window.zone_tags << :hand
  5263.         end
  5264.       end
  5265.       actor.monster_zones.each_with_index do |card, idx|
  5266.         next unless card
  5267.         if check_validity.call(card) && @card_select_window.enable?(card, :field, idx)
  5268.           @card_select_window.cards << card
  5269.           @card_select_window.zone_tags << :field
  5270.         end
  5271.       end
  5272.     when :hand_or_deck
  5273.       actor.current_zone = :hand_or_deck
  5274.       @card_select_window.zone_tags = []
  5275.       @card_select_window.cards = []
  5276.       filter = array[4]
  5277.       active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5278.       sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  5279.       actor.hand.each do |card|
  5280.         next if card.nil?
  5281.         if actor.match_complex_filter?(card, filter, active_c, sim_lvl)
  5282.           @card_select_window.cards << card
  5283.           @card_select_window.zone_tags << :hand
  5284.         end
  5285.       end
  5286.       actor.deck.each do |card|
  5287.         next if card.nil?
  5288.         if actor.match_complex_filter?(card, filter, active_c, sim_lvl)
  5289.           @card_select_window.cards << card
  5290.           @card_select_window.zone_tags << :deck
  5291.         end
  5292.       end
  5293.     when :hand_or_grave
  5294.       actor.current_zone = :hand_or_grave
  5295.       @field_zone_label.hide if @field_zone_label
  5296.       @card_select_window.zone_tags = []
  5297.       @card_select_window.cards = []
  5298.       active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5299.       filter = array[4]
  5300.       exclude = false
  5301.       if filter.is_a?(Array)
  5302.         if filter[0].is_a?(Array)
  5303.           exclude = filter.any? { |f| f[11] == :exclude_active }
  5304.         else
  5305.           exclude = (filter[11] == :exclude_active)
  5306.         end
  5307.       end
  5308.       dry_run_filter = nil
  5309.       dry_run_zone = nil
  5310.       if @card_select_window.dry_run_eff2
  5311.         eff2_data = PC27::CG::CARD_EFFECT[@card_select_window.dry_run_eff2]
  5312.         if eff2_data
  5313.           dry_run_filter = eff2_data[4]
  5314.           dry_run_zone = eff2_data[2]
  5315.         end
  5316.       end
  5317.       check_validity = lambda do |card|
  5318.         next false if card.nil?
  5319.         next false if exclude && active_c && card.id == active_c.id
  5320.         if dry_run_filter && (dry_run_zone == :deck || dry_run_zone == :hand_or_deck)
  5321.           has_future_target = actor.deck.any? do |d_card|
  5322.             next false if d_card.nil?
  5323.             actor.match_complex_filter?(d_card, dry_run_filter, active_c, card.level)
  5324.           end
  5325.           if dry_run_zone == :hand_or_deck && !has_future_target
  5326.             has_future_target = actor.hand.any? do |h_card|
  5327.               next false if h_card.nil?
  5328.               next false if h_card == card
  5329.               actor.match_complex_filter?(h_card, dry_run_filter, active_c, card.level)
  5330.             end
  5331.           end
  5332.           next false unless has_future_target
  5333.         end
  5334.         true
  5335.       end
  5336.       actor.hand.each do |card|
  5337.         if check_validity.call(card) && @card_select_window.enable?(card)
  5338.           @card_select_window.cards << card
  5339.           @card_select_window.zone_tags << :hand
  5340.         end
  5341.       end
  5342.       actor.grave.each do |card|
  5343.         if check_validity.call(card) && @card_select_window.enable?(card)
  5344.           @card_select_window.cards << card
  5345.           @card_select_window.zone_tags << :grave
  5346.         end
  5347.       end
  5348.     when :special_summon_self
  5349.       @temp_effect_data[3] = [$chain_memory_card] if $chain_memory_card
  5350.       restore_taken_cards(:proceed)
  5351.       return
  5352.     when :deck
  5353.       actor.current_zone = :deck
  5354.       @card_select_window.zone_tags = []
  5355.       @card_select_window.cards = []
  5356.       filter = array[4]
  5357.       deck = @card_select_window.actor.deck
  5358.       selectable = []
  5359.       active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5360.       sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  5361.       deck.each do |card|
  5362.         next if card.nil?
  5363.         if actor.match_complex_filter?(card, filter, active_c, sim_lvl)
  5364.           selectable << card
  5365.         end
  5366.       end
  5367.       amn = (array[1] == 0 ? selectable.size : array[1])
  5368.       selectable.first(amn).each do |card|
  5369.         idx = deck.index(card)
  5370.         deck.delete_at(idx) if idx
  5371.         @card_select_window.cards.push(card)
  5372.         @card_select_window.zone_tags.push(:deck)
  5373.       end
  5374.     when :hand
  5375.       actor.current_zone = :hand
  5376.       @card_select_window.zone_tags = []
  5377.       @card_select_window.cards = []
  5378.       filter = array[4]
  5379.       active_c = @active_effect_card || $temp_active_card || $chain_memory_card
  5380.       sim_lvl = $chain_memory_card ? $chain_memory_card.level : nil
  5381.       selectable = []
  5382.       actor.hand.each do |card|
  5383.         next if card.nil?
  5384.         if actor.match_complex_filter?(card, filter, active_c, sim_lvl)
  5385.           selectable << card
  5386.         end
  5387.       end
  5388.       amn = (array[1] == 0 ? selectable.size : array[1])
  5389.       selectable.first(amn).each do |card|
  5390.         idx = actor.hand.index(card)
  5391.         actor.hand.delete_at(idx) if idx
  5392.         @card_select_window.cards << card
  5393.         @card_select_window.zone_tags << :hand
  5394.       end
  5395.     when :grave
  5396.       actor.current_zone = :grave
  5397.       @field_zone_label.hide if @field_zone_label
  5398.       grave_array = @card_select_window.actor.grave
  5399.       selectable = []
  5400.       grave_array.each do |card|
  5401.         next if card.nil?
  5402.         selectable << card if @card_select_window.enable?(card)
  5403.       end
  5404.       amn = (array[1] == 0 ? selectable.size : array[1])
  5405.       selectable.first(amn).each do |card|
  5406.         grave_array.delete(card)
  5407.         @card_select_window.cards.push(card)
  5408.       end
  5409.     when :banish
  5410.       actor.current_zone = :banish
  5411.       (array[1] == 0 ? amn = @card_select_window.actor.banish.size : amn = array[1])
  5412.       amn.times do
  5413.         next if @card_select_window.actor.banish.size == 0
  5414.         @card_select_window.cards.push(@card_select_window.actor.banish.shift)
  5415.       end
  5416.     when :extra
  5417.       actor.current_zone = :extra
  5418.       (array[1] == 0 ? amn = @card_select_window.actor.extra.size : amn = array[1])
  5419.       amn.times do
  5420.         next if @card_select_window.actor.extra.size == 0
  5421.         @card_select_window.cards.push(@card_select_window.actor.extra.shift)
  5422.       end
  5423.     when :field
  5424.       @targeting_mode = true
  5425.       @targeting_filter = array[4]
  5426.       @temp_effect_data[1] = effect_name
  5427.       @temp_effect_data[2] = 0
  5428.       @card_select_window.hide.deactivate
  5429.       @skill_window.hide.deactivate
  5430.       @card_zone_label.hide if @card_zone_label
  5431.       @field_map_window.show.activate
  5432.       @field_preview_window.show if @field_preview_window
  5433.       $game_message.clear
  5434.       $game_message.background = 1
  5435.       $game_message.position = 1
  5436.       $game_message.add("Select a target on your field.")
  5437.       if @message_window
  5438.         @message_window.openness = 255
  5439.         @message_window.visible = true
  5440.       end
  5441.       return
  5442.     end
  5443.     @card_select_window.make_item_list
  5444.     dest = array[5]
  5445.     if [:special_summon, :special_summon_def, :special_summon_atk].include?(dest)
  5446.       @card_select_window.cards.reject! do |card|
  5447.         (card.note =~ /<special_only:/i) && !card.properly_summoned
  5448.       end
  5449.     end
  5450.     if [:special_summon, :special_summon_def, :special_summon_atk].include?(array[5]) && !actor.free_monster_zone_available?
  5451.       Sound.play_buzzer
  5452.       $game_message.clear
  5453.       $game_message.background = 1
  5454.       $game_message.position = 1
  5455.       $game_message.add("No monster zones available.")
  5456.       if @message_window
  5457.         @message_window.openness = 255
  5458.         @message_window.show
  5459.       end
  5460.       wait_for_message
  5461.       restore_selected_cards
  5462.       @temp_effect_data = []
  5463.       finish_proceed_selection
  5464.       return
  5465.     end
  5466.     valid_targets = @card_select_window.cards.size > 0
  5467.     if !valid_targets
  5468.       Sound.play_buzzer
  5469.       $game_message.clear
  5470.       $game_message.background = 1
  5471.       $game_message.position = 1
  5472.       $game_message.add("No targets available.")
  5473.       if @message_window
  5474.         @message_window.openness = 255
  5475.         @message_window.show
  5476.       end
  5477.       wait_for_message
  5478.       restore_selected_cards
  5479.       @temp_effect_data = []
  5480.       finish_proceed_selection
  5481.       return
  5482.     end
  5483.     @card_select_window.refresh
  5484.     @card_select_window.show
  5485.     @card_select_window.activate
  5486.     refresh_card_zone_label
  5487.     @card_zone_label.show if @card_zone_label
  5488.   end
  5489.  
  5490.   #-----------------------------------------------------------------------------
  5491.   def set_select_filter(array)
  5492.     if array[4]
  5493.       @card_select_window.filter = array[4]
  5494.     elsif array[0] == :view
  5495.       @card_select_window.filter = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
  5496.     else
  5497.       @card_select_window.filter = [0,0,0,0,0,0,0,0,0,0,0]
  5498.     end
  5499.   end
  5500.  
  5501.   #-----------------------------------------------------------------------------
  5502.   def select_cards_ok_sort
  5503.     if @card_select_window.cards.size == 0
  5504.       select_cancel
  5505.       return
  5506.     end
  5507.     if @card_select_window.sort_index != -1
  5508.       swap_cards(@card_select_window.sort_index, @card_select_window.index)
  5509.       @card_select_window.sort_index = -1
  5510.       @card_select_window.make_item_list
  5511.       @card_select_window.refresh
  5512.     else
  5513.       @card_select_window.sort_index = @card_select_window.index
  5514.     end
  5515.     @card_select_window.activate
  5516.   end
  5517.  
  5518.   #-----------------------------------------------------------------------------
  5519.   def select_cards_ok_take
  5520.     if @card_select_window.cards.size == 0
  5521.       select_cancel
  5522.       return
  5523.     end
  5524.     card = @card_select_window.item
  5525.     player = @card_select_window.actor
  5526.     if card
  5527.       $chain_memory_card = card
  5528.       @temp_effect_data[3] = [] unless @temp_effect_data[3]
  5529.       @temp_effect_data[3].push(card) unless card.frame == 5
  5530.       idx_window = @card_select_window.cards.index(card)
  5531.       @card_select_window.cards.delete_at(idx_window) if idx_window
  5532.       source = @card_select_window.zone_tags[@card_select_window.index]
  5533.       card.instance_variable_set(:@temp_source_zone, source)
  5534.       if source == :hand
  5535.         idx_hand = player.hand.index(card)
  5536.         player.hand.delete_at(idx_hand) if idx_hand
  5537.       elsif source == :field
  5538.         idx_field = player.monster_zones.index(card)
  5539.         player.monster_zones[idx_field] = nil if idx_field
  5540.         idx_f = player.field.index(card)
  5541.         player.field.delete_at(idx_f) if idx_f
  5542.       elsif source == :grave
  5543.         idx_grave = player.grave.index(card)
  5544.         player.grave.delete_at(idx_grave) if idx_grave
  5545.       elsif source == :deck
  5546.         idx_deck = player.deck.index(card)
  5547.         player.deck.delete_at(idx_deck) if idx_deck
  5548.       elsif source == :banish
  5549.         idx_banish = player.banish.index(card)
  5550.         player.banish.delete_at(idx_banish) if idx_banish
  5551.       elsif source == :extra
  5552.         idx_extra = player.extra.index(card)
  5553.         player.extra.delete_at(idx_extra) if idx_extra
  5554.       end
  5555.     end
  5556.     @temp_effect_data[2] += 1
  5557.     if PC27::CG::CARD_EFFECT[@temp_effect_data[1]][6] != @temp_effect_data[2]
  5558.       @card_select_window.make_item_list
  5559.       @card_select_window.refresh
  5560.       @card_select_window.activate
  5561.     else
  5562.       proceed_selection
  5563.     end
  5564.   end
  5565. #-----------------------------------------------------------------------------
  5566.   def proceed_selection
  5567.     @added_card_name = nil
  5568.     if @temp_effect_data && @temp_effect_data[3] && !@temp_effect_data[3].empty?
  5569.       @added_card_name = @temp_effect_data[3][0].name
  5570.     end
  5571.     @card_select_window.hide.deactivate
  5572.     @card_select_window.sort_index = -1
  5573.     restore_selected_cards
  5574.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  5575.     unless @current_card_action == :field_effect
  5576.       player.current_zone = :hand
  5577.     end
  5578.     if @temp_effect_data[0] == :cost
  5579.       effect_data = PC27::CG::CARD_EFFECT[@temp_effect_data[1]]
  5580.       is_chain = effect_data && effect_data[5].to_s.include?("chain")
  5581.       restore_taken_cards(:proceed)
  5582.       if @is_interrupt_effect
  5583.         puts "[DEBUG] Costo de Quick Effect pagado. Saltando next_command e invocando impacto."
  5584.         skill_to_use = @skill || @active_effect_card
  5585.         if skill_to_use && player.respond_to?(:pay_skill_cost)
  5586.           player.pay_skill_cost(skill_to_use)
  5587.           RPG::SE.new("Magic1", 80, 100).play rescue nil
  5588.           player.popup_destroyed_card(skill_to_use.icon_index, "Activated!") if player.respond_to?(:popup_destroyed_card)
  5589.         end
  5590.         impact_to_use = nil
  5591.         if skill_to_use.note =~ /<card impact:\s*(.+?)>/i ||
  5592.            skill_to_use.note =~ /<spell_impact:\s*(.+?)>/i ||
  5593.            skill_to_use.note =~ /<field_impact:\s*(.+?)>/i
  5594.           impact_to_use = $1.to_s.strip.downcase
  5595.         end
  5596.         @temp_effect_data = []
  5597.         invoke_card_impact(impact_to_use) if impact_to_use
  5598.       else
  5599.         unless is_chain
  5600.           if player && player.respond_to?(:input) && player.input
  5601.             player.input.set_skill(@skill.id)
  5602.             player.last_skill.object = @skill
  5603.           end
  5604.           @temp_effect_data = []
  5605.           next_command
  5606.         end
  5607.       end
  5608.     elsif @temp_effect_data[0] == :impact
  5609.       unless restore_taken_cards(:proceed)
  5610.         finish_proceed_selection
  5611.       end
  5612.     end
  5613.   end
  5614.  
  5615.   def finish_proceed_selection
  5616.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  5617.     if @temp_effect_data && @temp_effect_data[2] == :deck
  5618.       player.shuffle_deck(true)
  5619.     end
  5620.     if @selected_card && [24, 25, 29, 31].include?(@selected_card.subtype)
  5621.       idx_to_clear = nil
  5622.       if @active_effect_zone_type == :spell && @active_effect_zone_index
  5623.         if player.st_zones[@active_effect_zone_index] && player.st_zones[@active_effect_zone_index].id == @selected_card.id
  5624.           idx_to_clear = @active_effect_zone_index
  5625.         end
  5626.       end
  5627.       if idx_to_clear.nil?
  5628.         idx_to_clear = player.st_zones.index { |c| c && c.equal?(@selected_card) }
  5629.       end
  5630.       if idx_to_clear && player.st_zones[idx_to_clear]
  5631.         actual_card = player.st_zones[idx_to_clear]
  5632.         player.st_zones[idx_to_clear] = nil
  5633.         idx_field = player.field.index { |c| c && c.equal?(@selected_card) }
  5634.         player.field.delete_at(idx_field) if idx_field
  5635.         player.grave.push(actual_card)
  5636.         @field_map_window.refresh if defined?(@field_map_window) && @field_map_window
  5637.         @minifield_window.refresh if defined?(@minifield_window) && @minifield_window
  5638.       end
  5639.     end
  5640.     if player.stack.size > 0
  5641.       player.grave += player.stack
  5642.       player.stack = []
  5643.     end
  5644.     if @temp_effect_data && @temp_effect_data[1]
  5645.       effect_data = PC27::CG::CARD_EFFECT[@temp_effect_data[1]]
  5646.       if effect_data && effect_data[2] == :deck
  5647.         @deck_was_searched = true
  5648.         dest_id = @last_chosen_destination || effect_data[5] || :hand
  5649.         if [:special_summon, :inherent_landing, :field].include?(dest_id)
  5650.           @shuffle_message = "Shuffling Deck..."
  5651.         elsif @added_card_name
  5652.           if dest_id == :grave
  5653.             @shuffle_message = "#{@added_card_name} sent to the Graveyard! / Shuffling Deck"
  5654.           else
  5655.             @shuffle_message = "#{@added_card_name} added to the Hand! / Shuffling Deck"
  5656.           end
  5657.         else
  5658.           @shuffle_message = "Shuffling Deck..."
  5659.         end
  5660.         @last_chosen_destination = nil
  5661.       end
  5662.     end
  5663.     @temp_effect_data = []
  5664.     if @field_map_window
  5665.       @field_map_window.dispose
  5666.       @field_map_window = nil
  5667.     end
  5668.     if @field_preview_window
  5669.       @field_preview_window.dispose
  5670.       @field_preview_window = nil
  5671.     end
  5672.     @field_ui_created = false
  5673.     @field_zone_label.hide if @field_zone_label
  5674.     restore_skill_window_layout
  5675.     if @pending_triggers && @pending_triggers.size > 0
  5676.       check_pending_triggers
  5677.       return
  5678.     end
  5679.     card_to_trigger = @specially_summoned_card
  5680.     @specially_summoned_card = nil
  5681.     if card_to_trigger && card_to_trigger.summon_impact && player.can_activate_summon_effect?(card_to_trigger)
  5682.       @is_interrupt_effect = true if @active_effect_owner
  5683.       $game_message.clear
  5684.       if @message_window
  5685.         @message_window.openness = 0
  5686.         @message_window.visible = true
  5687.       end
  5688.       $game_message.background = 1
  5689.       $game_message.position = 1  
  5690.       $game_message.add("Do you want to activate the effect of")
  5691.       $game_message.add('"' + card_to_trigger.name + '"?')
  5692.       @status_window.hide if @status_window
  5693.       @status_back_window.hide if @status_back_window
  5694.       show_options(["Yes", "No"], 2) do |choice|
  5695.         @status_window.show if @status_window
  5696.         @status_back_window.show if @status_back_window
  5697.         $game_message.choice_proc = nil
  5698.         $game_message.clear
  5699.         if @message_window
  5700.           choice_window = @message_window.instance_variable_get(:@choice_window)
  5701.           choice_window.openness = 0 if choice_window
  5702.         end
  5703.         if choice == 0
  5704.           player.used_turn_effects ||= []
  5705.           player.used_turn_effects << card_to_trigger.summon_impact
  5706.           if card_to_trigger.note =~ /<shared_opt:\s*(.+?)>/i
  5707.             player.used_turn_effects << $1.to_s.strip
  5708.           end
  5709.           @active_effect_owner = player
  5710.           @active_effect_card = card_to_trigger
  5711.           $temp_active_card = card_to_trigger
  5712.           $chain_memory_card = card_to_trigger
  5713.           @current_processing_card = card_to_trigger
  5714.           invoke_card_impact(card_to_trigger.summon_impact)
  5715.         else
  5716.           execute_finish_proceed_cleanup
  5717.         end
  5718.       end
  5719.       wait_for_message
  5720.       return
  5721.     end
  5722.     execute_finish_proceed_cleanup
  5723.     if (@pending_triggers.nil? || @pending_triggers.empty?) && !$game_message.busy?
  5724.       cmd_active = @actor_command_window && @actor_command_window.active
  5725.       crd_active = @card_select_window && @card_select_window.active
  5726.       fld_active = @field_map_window && @field_map_window.active
  5727.       skl_active = @skill_window && @skill_window.active
  5728.       if !cmd_active && !crd_active && !fld_active && !skl_active
  5729.         @actor_command_window.show.activate if @actor_command_window
  5730.       end
  5731.     end
  5732.   end
  5733.    
  5734.   def execute_finish_proceed_cleanup
  5735.     puts "[DEBUG] Ejecutando execute_finish_proceed_cleanup. Interrupt flag: #{@is_interrupt_effect}"
  5736.     @card_select_window.hide.deactivate if @card_select_window
  5737.     @skill_window.hide.deactivate if @skill_window
  5738.     player = @active_effect_owner || $game_party.members[0]
  5739.     if @deck_was_searched
  5740.       player.is_official_shuffle = true
  5741.       player.shuffle_deck(true)
  5742.       player.is_official_shuffle = false
  5743.       @deck_was_searched = false
  5744.     else
  5745.       player.shuffle_deck(false)
  5746.     end
  5747.     @last_summon_is_set = false
  5748.     @current_card_action = nil
  5749.     @active_effect_card = nil
  5750.     @current_processing_card = nil
  5751.     $game_message.clear
  5752.     @message_window.close if @message_window
  5753.     @log_window.clear if @log_window
  5754.     @status_window.show if @status_window
  5755.     @status_back_window.show if @status_back_window
  5756.     @info_viewport.visible = true if @info_viewport
  5757.     if @is_interrupt_effect
  5758.       puts "[DEBUG] Limpiando UI post-Quick Effect. Devolviendo control al motor."
  5759.       @is_interrupt_effect = false
  5760.       @active_effect_owner = nil
  5761.       @return_to_hand_flag = false
  5762.     else
  5763.       if BattleManager.actor
  5764.         @return_to_hand_flag = true
  5765.         @actor_command_window.hide.deactivate if @actor_command_window
  5766.         command_look_hand
  5767.       elsif !BattleManager.in_turn?
  5768.         @party_command_window.show if @party_command_window && @party_command_window.active
  5769.         @actor_command_window.show if @actor_command_window && @actor_command_window.active
  5770.       end
  5771.     end
  5772.   end
  5773.  
  5774.   def process_banish_triggers(taken_cards)
  5775.     @pending_triggers ||= []
  5776.     taken_cards.each do |c|
  5777.       next unless c
  5778.       next if c.instance_variable_get(:@banished_face_down)
  5779.       c.instance_variable_set(:@temp_source_zone, nil)
  5780.       eff = nil
  5781.       mandatory = true
  5782.       if c.note =~ /<trigger_banish_effect:\s*(.+?)>/i
  5783.         eff = $1.to_s.strip
  5784.       elsif c.note =~ /<trigger_banish_effect_optional:\s*(.+?)>/i
  5785.         eff = $1.to_s.strip
  5786.         mandatory = false
  5787.       end
  5788.       if eff
  5789.         actor = BattleManager.actor
  5790.         next if actor.state?(18) && actor.leads_to_special_summon?(eff)
  5791.         next if actor.hopt_blocked?(c, eff)
  5792.         @pending_triggers << { card: c, effect: eff, expected_zone: :banish, mandatory: mandatory }
  5793.       end
  5794.     end
  5795.   end
  5796.  
  5797.   def check_pending_triggers
  5798.     @pending_triggers ||= []
  5799.     if @pending_triggers.size > 0
  5800.       trigger = @pending_triggers.shift
  5801.       card = trigger[:card]
  5802.       effect_name = trigger[:effect]
  5803.       expected_zone = trigger[:expected_zone] || :grave
  5804.       player = trigger[:owner] || $game_party.members[0]
  5805.       if player.state?(18) && player.leads_to_special_summon?(effect_name)
  5806.         finish_proceed_selection
  5807.         return
  5808.       end
  5809.       if player.hopt_blocked?(card, effect_name)
  5810.         finish_proceed_selection
  5811.         return
  5812.       end
  5813.       zone_array = case expected_zone
  5814.                    when :grave then player.grave
  5815.                    when :hand then player.hand
  5816.                    when :deck then player.deck
  5817.                    when :banish then player.banish
  5818.                    when :extra then player.extra
  5819.                    else []
  5820.                    end
  5821.       unless zone_array.include?(card)
  5822.         finish_proceed_selection
  5823.         return
  5824.       end
  5825.       if trigger[:mandatory] == false
  5826.         $game_message.clear
  5827.         if @message_window
  5828.           @message_window.openness = 0
  5829.           @message_window.visible = true
  5830.         end
  5831.         $game_message.background = 1
  5832.         $game_message.position = 1
  5833.         $game_message.add("Do you want to activate the effect of")
  5834.         $game_message.add('"' + card.name + '"?')
  5835.         @status_window.hide if @status_window
  5836.         @status_back_window.hide if @status_back_window
  5837.         show_options(["Yes", "No"], 2) do |choice|
  5838.           @status_window.show if @status_window
  5839.           @status_back_window.show if @status_back_window
  5840.           $game_message.choice_proc = nil
  5841.           $game_message.clear
  5842.           if @message_window
  5843.             choice_window = @message_window.instance_variable_get(:@choice_window)
  5844.             choice_window.openness = 0 if choice_window
  5845.           end
  5846.           if choice == 0
  5847.             execute_pending_trigger(player, card, effect_name)
  5848.           else
  5849.             finish_proceed_selection
  5850.           end
  5851.         end
  5852.         wait_for_message
  5853.         return
  5854.       else
  5855.         execute_pending_trigger(player, card, effect_name)
  5856.       end
  5857.     end
  5858.   end
  5859.  
  5860.   def execute_pending_trigger(player, card, effect_name)
  5861.     player.used_turn_effects ||= []
  5862.     player.used_turn_effects << effect_name
  5863.     @is_interrupt_effect = true
  5864.     @active_effect_owner = player
  5865.     @active_effect_card = card
  5866.     $temp_active_card = card
  5867.     $chain_memory_card = card
  5868.     @current_processing_card = card
  5869.     RPG::SE.new("Skill2", 80, 100).play
  5870.     $game_message.clear
  5871.     if @message_window
  5872.       @message_window.openness = 255
  5873.       @message_window.visible = true
  5874.     end
  5875.     $game_message.background = 1
  5876.     $game_message.position = 1  
  5877.     $game_message.add("The effect of " + card.name + " activates!")
  5878.     wait_for_message
  5879.     @current_card_action = :field_effect
  5880.     if effect_name.upcase.include?("LUNATIC_")
  5881.       player.invoke_lunatic_card(effect_name, card)
  5882.       finish_proceed_selection if respond_to?(:finish_proceed_selection)
  5883.     else
  5884.       invoke_card_impact(effect_name)
  5885.     end
  5886.   end
  5887.  
  5888.   def select_cancel
  5889.     @card_zone_label.hide
  5890.     @card_select_window.hide.deactivate
  5891.     @card_select_window.sort_index = -1
  5892.     restore_taken_cards(:cancel)
  5893.     restore_selected_cards
  5894.     if @field_map_window
  5895.       @field_map_window.dispose
  5896.       @field_map_window = nil
  5897.     end
  5898.     if @field_preview_window
  5899.       @field_preview_window.dispose
  5900.       @field_preview_window = nil
  5901.     end
  5902.     @field_ui_created = false
  5903.     @field_zone_label.hide if @field_zone_label
  5904.     restore_skill_window_layout
  5905.     if BattleManager.actor
  5906.       BattleManager.actor.current_zone = :hand
  5907.     end
  5908.     refresh_card_zone_label
  5909.     if @temp_effect_data[0] == :cost
  5910.       BattleManager.actor.hand += BattleManager.actor.stack
  5911.       BattleManager.actor.stack = []
  5912.       on_skill_cancel
  5913.     elsif @temp_effect_data[0] == :impact
  5914.       @return_to_hand_flag = true
  5915.       command_look_hand
  5916.     end
  5917.   end
  5918.  
  5919.   def restore_taken_cards(target)
  5920.     return false unless @temp_effect_data && @temp_effect_data[3] && @temp_effect_data[3] != []
  5921.     effect_name = @temp_effect_data[1]
  5922.     effect_data = PC27::CG::CARD_EFFECT[effect_name]
  5923.     destination = effect_data ? effect_data[5] : nil
  5924.     if @card_zone_label
  5925.       @card_zone_label.hide
  5926.       @card_zone_label.visible = false
  5927.     end
  5928.     if target == :proceed
  5929.       if effect_data && effect_data[6].is_a?(Numeric)
  5930.         required_amount = effect_data[6]
  5931.         if @temp_effect_data[3].size < required_amount
  5932.           target = :cancel
  5933.         end
  5934.       end
  5935.     end
  5936.     if target == :proceed
  5937.       if destination == :inherent_landing
  5938.         taken_cards = @temp_effect_data[3].dup
  5939.         @temp_effect_data[3] = []
  5940.         process_banish_triggers(taken_cards)
  5941.         @card_select_window.actor.banish += taken_cards
  5942.         if @active_effect_card
  5943.           hand_card = BattleManager.actor.hand.find { |c| c && c.id == @active_effect_card.id }
  5944.           if hand_card && BattleManager.actor.free_monster_zone_available?
  5945.             idx = BattleManager.actor.hand.index(hand_card)
  5946.             BattleManager.actor.hand.delete_at(idx) if idx
  5947.             hand_card.properly_summoned = true
  5948.             BattleManager.actor.field << hand_card
  5949.             empty_index = BattleManager.actor.monster_zones.index(nil)
  5950.             BattleManager.actor.monster_zones[empty_index] = hand_card
  5951.             BattleManager.actor.monster_zones_state[empty_index][:changed_this_turn] = true
  5952.             @field_map_window.refresh if @field_map_window
  5953.             show_special_summon_visuals(hand_card, true)
  5954.             @specially_summoned_card = hand_card
  5955.           end
  5956.         end
  5957.         @active_effect_card = nil
  5958.         check_pending_triggers if @pending_triggers && @pending_triggers.size > 0
  5959.         finish_proceed_selection
  5960.         return true
  5961.       end
  5962.       if destination == :choice_hand_grave
  5963.         taken_cards = @temp_effect_data[3].dup
  5964.         @temp_effect_data[3] = []
  5965.         $game_message.clear
  5966.         if @message_window
  5967.           @message_window.openness = 0
  5968.           @message_window.visible = true
  5969.         end
  5970.         $game_message.background = 1
  5971.         $game_message.position = 1
  5972.         card_name = taken_cards[0] ? taken_cards[0].name : "this card"
  5973.         $game_message.add("Where do you want to send")
  5974.         $game_message.add('"' + card_name + '"?')
  5975.         @status_window.hide if @status_window
  5976.         @status_back_window.hide if @status_back_window
  5977.         show_options(["Add to Hand", "Send to Graveyard"], 2) do |choice|
  5978.           @status_window.show if @status_window
  5979.           @status_back_window.show if @status_back_window
  5980.           $game_message.choice_proc = nil
  5981.           $game_message.clear
  5982.           if @message_window
  5983.             choice_window = @message_window.instance_variable_get(:@choice_window)
  5984.             choice_window.openness = 0 if choice_window
  5985.           end
  5986.           if choice == 0
  5987.             @card_select_window.actor.hand += taken_cards
  5988.             @last_chosen_destination = :hand
  5989.           else
  5990.             @card_select_window.actor.grave += taken_cards
  5991.             @last_chosen_destination = :grave
  5992.           end
  5993.           finish_proceed_selection
  5994.         end
  5995.         return true
  5996.       end
  5997.       if destination == :destroy_and_chain || destination == :send_to_grave_and_chain
  5998.         taken_cards = @temp_effect_data[3].dup
  5999.         @temp_effect_data[3] = []
  6000.         @temp_effect_data[4] = taken_cards[0]
  6001.         $chain_memory_card = taken_cards[0]
  6002.         @pending_triggers ||= []
  6003.         actor = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  6004.         taken_cards.each do |c|
  6005.           source = c.instance_variable_get(:@temp_source_zone)
  6006.           c.instance_variable_set(:@temp_source_zone, nil)
  6007.           if destination == :destroy_and_chain && c.note =~ /<trigger_destroy_effect:\s*(.+?)>/i
  6008.             eff = $1.to_s.strip
  6009.             next if actor.state?(18) && actor.leads_to_special_summon?(eff)
  6010.             @pending_triggers << { card: c, effect: eff, expected_zone: :grave, owner: actor }
  6011.           end
  6012.           if c.note =~ /<trigger_sent_field_grave:\s*(.+?)>/i
  6013.             if source == :monster || source == :spell || source == :fieldspell
  6014.               eff = $1.to_s.strip
  6015.               @pending_triggers << { card: c, effect: eff, expected_zone: :grave, owner: actor }
  6016.             end
  6017.           end
  6018.         end
  6019.         actor.grave += taken_cards
  6020.         effect_data = PC27::CG::CARD_EFFECT[@temp_effect_data[1]] rescue nil
  6021.         next_effect = effect_data ? (effect_data[7] || effect_data[6]) : nil
  6022.         $game_message.clear
  6023.         @current_card_action = :field_effect
  6024.         actor.current_zone = :grave
  6025.         refresh_card_zone_label if respond_to?(:refresh_card_zone_label)
  6026.         if next_effect.is_a?(String)
  6027.           invoke_card_impact(next_effect)
  6028.         else
  6029.           check_pending_triggers if respond_to?(:check_pending_triggers)
  6030.         end
  6031.         return true
  6032.       end
  6033.       if destination == :special_summon_self
  6034.         card_to_summon = @temp_effect_data[3][0]
  6035.         @temp_effect_data[3] = []
  6036.         player = @card_select_window.actor
  6037.         source_zone = player.banish.include?(card_to_summon) ? player.banish : (player.grave.include?(card_to_summon) ? player.grave : nil)
  6038.         if card_to_summon && player.free_monster_zone_available? && source_zone
  6039.           atk_buff = 0
  6040.           def_buff = 0
  6041.           level_buff = 0
  6042.           regex = /<self_summon_buff:\s*(atk|def|level)\s*,\s*(banish|grave|field|hand)\s*,\s*(type|subtype|element)\s*,\s*(\d+)\s*,\s*(\d+)>/i
  6043.           card_to_summon.note.scan(regex) do |stat, zone, f_type, f_val, multiplier|
  6044.             target_array = case zone.downcase
  6045.                            when "banish" then player.banish
  6046.                            when "grave" then player.grave
  6047.                            when "field" then player.field
  6048.                            when "hand" then player.hand
  6049.                            else []
  6050.                            end
  6051.             match_count = target_array.count do |c|
  6052.               next false unless c
  6053.               next false if c == card_to_summon
  6054.               case f_type.downcase
  6055.               when "subtype" then c.respond_to?(:subtype) && c.subtype.to_s == f_val
  6056.               when "type" then c.respond_to?(:type) && c.type.to_s == f_val
  6057.               when "element" then c.respond_to?(:damage) && c.attribute.to_s == f_val
  6058.               else false
  6059.               end
  6060.             end
  6061.             total_buff = match_count * multiplier.to_i
  6062.             case stat.downcase
  6063.             when "atk" then atk_buff += total_buff
  6064.             when "def" then def_buff += total_buff
  6065.             when "level" then level_buff += total_buff
  6066.             end
  6067.           end
  6068.           empty_index = player.monster_zones.index(nil)
  6069.           player.monster_zones_state[empty_index] = {
  6070.             face_down: false,
  6071.             changed_this_turn: true,
  6072.             personal_atk_buff: atk_buff,
  6073.             personal_def_buff: def_buff,
  6074.             personal_level_buff: level_buff
  6075.           }
  6076.           state = player.monster_zones_state[empty_index]
  6077.           source_zone.delete_at(source_zone.index(card_to_summon))
  6078.           state[:face_down] = false
  6079.           state[:changed_this_turn] = true
  6080.           state[:personal_atk_buff] = atk_buff
  6081.           state[:personal_def_buff] = def_buff
  6082.           state[:personal_level_buff] = level_buff
  6083.           idx = source_zone.index(card_to_summon)
  6084.           source_zone.delete_at(idx) if idx
  6085.           options = ["Summon in Attack", "Summon in Defense"]
  6086.           show_options(options, 2) do |choice|
  6087.             $game_message.choice_proc = nil
  6088.             $game_message.clear
  6089.             if @message_window
  6090.               choice_window = @message_window.instance_variable_get(:@choice_window)
  6091.               choice_window.openness = 0 if choice_window
  6092.             end
  6093.             state[:position] = (choice == 0) ? :attack : :defense
  6094.             player.monster_zones[empty_index] = card_to_summon
  6095.             player.field << card_to_summon
  6096.             @field_map_window.refresh if @field_map_window
  6097.             show_special_summon_visuals(card_to_summon)
  6098.             @specially_summoned_card = card_to_summon
  6099.             finish_proceed_selection
  6100.           end
  6101.           return true
  6102.         else
  6103.           Sound.play_buzzer
  6104.           finish_proceed_selection
  6105.           return true
  6106.         end
  6107.       end
  6108.       if destination == :banish_and_chain
  6109.         taken_cards = @temp_effect_data[3].dup
  6110.         @temp_effect_data[3] = []
  6111.         @temp_effect_data[4] = taken_cards[0]
  6112.         $chain_memory_card = taken_cards[0]
  6113.         @pending_triggers ||= []
  6114.         taken_cards.each do |c|
  6115.           c.instance_variable_set(:@temp_source_zone, nil)
  6116.           if c.note =~ /<trigger_banish_effect:\s*(.+?)>/i
  6117.             @pending_triggers << { card: c, effect: $1.to_s.strip, expected_zone: :banish }
  6118.           end
  6119.         end
  6120.         @card_select_window.actor.banish += taken_cards
  6121.         process_banish_triggers(taken_cards)
  6122.         effect_data = PC27::CG::CARD_EFFECT[@temp_effect_data[1]]
  6123.         next_effect = effect_data[7] || effect_data[6]
  6124.         $game_message.clear
  6125.         if @temp_effect_data[0] == :cost
  6126.           invoke_card_cost(next_effect) if next_effect.is_a?(String)
  6127.         else
  6128.           @current_card_action = :field_effect
  6129.           @card_select_window.actor.current_zone = :banish
  6130.           refresh_card_zone_label if respond_to?(:refresh_card_zone_label)
  6131.           invoke_card_impact(next_effect) if next_effect.is_a?(String)
  6132.         end
  6133.         return true
  6134.       end
  6135.       if destination == :special_summon || destination == :special_summon_ignore || destination == :special_summon_def || destination == :special_summon_atk
  6136.         taken_cards = @temp_effect_data[3].dup
  6137.         @temp_effect_data[3] = []
  6138.         player = @card_select_window.actor
  6139.         if player.free_monster_zone_available?
  6140.           card_to_summon = taken_cards[0]
  6141.           empty_index = player.monster_zones.index(nil)
  6142.           state = player.monster_zones_state[empty_index]
  6143.           state[:face_down] = false
  6144.           @last_summon_is_set = false
  6145.           if destination == :special_summon_def
  6146.             state[:position] = :defense
  6147.             state[:changed_this_turn] = true
  6148.             player.monster_zones[empty_index] = card_to_summon
  6149.             player.field << card_to_summon
  6150.             @field_map_window.refresh if @field_map_window
  6151.             show_special_summon_visuals(card_to_summon)
  6152.             $game_message.clear
  6153.             @specially_summoned_card = card_to_summon
  6154.             finish_proceed_selection
  6155.           elsif destination == :special_summon_atk
  6156.             state[:position] = :attack
  6157.             state[:changed_this_turn] = true
  6158.             player.monster_zones[empty_index] = card_to_summon
  6159.             player.field << card_to_summon
  6160.             @field_map_window.refresh if @field_map_window
  6161.             show_special_summon_visuals(card_to_summon)
  6162.             $game_message.clear
  6163.             @specially_summoned_card = card_to_summon
  6164.             finish_proceed_selection
  6165.           else
  6166.             options = ["Summon in Attack", "Summon in Defense"]
  6167.             show_options(options, 2) do |choice|
  6168.               $game_message.choice_proc = nil
  6169.               $game_message.clear
  6170.               if @message_window
  6171.                 choice_window = @message_window.instance_variable_get(:@choice_window)
  6172.                 choice_window.openness = 0 if choice_window
  6173.               end
  6174.               state[:position] = (choice == 0) ? :attack : :defense
  6175.               state[:changed_this_turn] = true
  6176.               player.monster_zones[empty_index] = card_to_summon
  6177.               player.field << card_to_summon
  6178.               @field_map_window.refresh if @field_map_window
  6179.               show_special_summon_visuals(card_to_summon)
  6180.               @specially_summoned_card = card_to_summon
  6181.               finish_proceed_selection
  6182.             end
  6183.             return true
  6184.           end
  6185.         else
  6186.           player.grave += taken_cards
  6187.           Sound.play_buzzer
  6188.           finish_proceed_selection
  6189.         end
  6190.         return true
  6191.       end
  6192.       taken_cards = @temp_effect_data[3].dup
  6193.       taken_cards.each { |c| c.instance_variable_set(:@temp_source_zone, nil) if c.instance_variable_defined?(:@temp_source_zone) }
  6194.       case destination
  6195.         when :hand   then @card_select_window.actor.hand += taken_cards
  6196.         when :deck  
  6197.           @card_select_window.actor.deck += taken_cards
  6198.           @card_select_window.actor.shuffle_deck(false)
  6199.         when :grave  then @card_select_window.actor.grave += taken_cards
  6200.         when :banish
  6201.           process_banish_triggers(taken_cards)
  6202.           @card_select_window.actor.banish += taken_cards
  6203.         when :extra  then @card_select_window.actor.extra += taken_cards
  6204.         when :field  then @card_select_window.actor.field += taken_cards
  6205.       end
  6206.     end
  6207.     if target == :cancel
  6208.       @active_effect_card = nil
  6209.       @temp_effect_data[3].each do |card|
  6210.         src = card.instance_variable_get(:@temp_source_zone)
  6211.         case src
  6212.         when :field
  6213.           empty_index = @card_select_window.actor.monster_zones.index(nil)
  6214.           @card_select_window.actor.monster_zones[empty_index] = card if empty_index
  6215.           @card_select_window.actor.field.push(card)
  6216.         when :hand   then @card_select_window.actor.hand.push(card)
  6217.         when :deck   then @card_select_window.actor.deck.push(card)
  6218.         when :grave  then @card_select_window.actor.grave.push(card)
  6219.         when :banish then @card_select_window.actor.banish.push(card)
  6220.         when :extra  then @card_select_window.actor.extra.push(card)
  6221.         end
  6222.         card.instance_variable_set(:@temp_source_zone, nil)
  6223.       end
  6224.     end
  6225.     if @temp_effect_data[0] == :cost
  6226.       @temp_effect_data[3] = []
  6227.       return true
  6228.     end
  6229.     @temp_effect_data[3] = []
  6230.     finish_proceed_selection
  6231.     check_pending_triggers if @pending_triggers && @pending_triggers.size > 0
  6232.     return true
  6233.   end
  6234.  
  6235.   #-----------------------------------------------------------------------------
  6236.   def restore_selected_cards
  6237.     array = PC27::CG::CARD_EFFECT[@temp_effect_data[1]]
  6238.     case array[3]
  6239.     when :hand_or_field
  6240.       @card_select_window.cards = []
  6241.       return
  6242.     when :deck
  6243.       @card_select_window.cards.reverse.each { |card|
  6244.         @card_select_window.actor.deck.unshift(card)
  6245.       }
  6246.       @card_select_window.actor.shuffle_deck if array[0] == :take && @card_select_window.actor.deck.size != 0
  6247.       @status_window.refresh
  6248.     when :hand
  6249.       @card_select_window.cards.reverse.each { |card|
  6250.         @card_select_window.actor.hand.unshift(card)
  6251.       }
  6252.     when :grave
  6253.       @card_select_window.cards.reverse.each { |card|
  6254.         @card_select_window.actor.grave.unshift(card)
  6255.       }
  6256.     when :banish
  6257.       @card_select_window.cards.reverse.each { |card|
  6258.         @card_select_window.actor.banish.unshift(card)
  6259.       }
  6260.     when :extra
  6261.       @card_select_window.cards.reverse.each { |card|
  6262.         @card_select_window.actor.extra.unshift(card)
  6263.       }
  6264.     when :field
  6265.       @card_select_window.cards.reverse.each { |card|
  6266.         @card_select_window.actor.field.unshift(card)
  6267.       }
  6268.     end
  6269.     @card_select_window.cards = []
  6270.   end
  6271.  
  6272.   #-----------------------------------------------------------------------------
  6273.   def prepare_select_window(moment, type)
  6274.     if moment == :impact
  6275.       if    type == :view
  6276.         @card_select_window.set_handler(:ok,   method(:no_method))
  6277.         @card_select_window.set_handler(:cancel, method(:select_cancel))
  6278.       elsif type == :sort
  6279.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_sort))
  6280.         @card_select_window.set_handler(:cancel, method(:select_cancel))
  6281.       elsif type == :take
  6282.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_take))
  6283.         @card_select_window.set_handler(:cancel, method(:proceed_selection))
  6284.       elsif type == :takem
  6285.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_take))
  6286.         @card_select_window.set_handler(:cancel, method(:no_method))
  6287.       end
  6288.     elsif moment == :cost
  6289.       if    type == :view
  6290.         @card_select_window.set_handler(:ok,   method(:no_method))
  6291.         @card_select_window.set_handler(:cancel, method(:proceed_selection))
  6292.       elsif type == :sort
  6293.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_sort))
  6294.         @card_select_window.set_handler(:cancel, method(:proceed_selection))
  6295.       elsif type == :take
  6296.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_take))
  6297.         @card_select_window.set_handler(:cancel, method(:select_cancel))
  6298.       elsif type == :takem
  6299.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_take))
  6300.         @card_select_window.set_handler(:cancel, method(:no_method))
  6301.       elsif type == :takeup
  6302.         @card_select_window.set_handler(:ok,   method(:select_cards_ok_take))
  6303.         @card_select_window.set_handler(:cancel, method(:proceed_selection))
  6304.       end
  6305.     end
  6306.   end
  6307.  
  6308.   def no_method
  6309.     @card_select_window.activate
  6310.   end
  6311.  
  6312.   alias pc27_ygo_execute_action_cleanup execute_action
  6313.   def execute_action
  6314.     pc27_ygo_execute_action_cleanup
  6315.     actor = @subject
  6316.     item = actor.current_action.item rescue nil
  6317.     return unless actor && actor.is_a?(Game_Actor) && item
  6318.     if item.subtype == 24 || item.subtype == 25
  6319.       idx = @active_effect_zone_index || actor.st_zones.index { |c| c && c.equal?(item) }
  6320.       if idx && actor.st_zones[idx]
  6321.         card_on_field = actor.st_zones[idx]
  6322.         actor.st_zones[idx] = nil
  6323.         idx_field = actor.field.index { |c| c && c.equal?(card_on_field) }; actor.field.delete_at(idx_field) if idx_field
  6324.         actor.grave.push(card_on_field)
  6325.         @field_map_window.refresh if @field_map_window
  6326.         @minifield_window.refresh if defined?(@minifield_window) && @minifield_window
  6327.       end
  6328.     end
  6329.   end
  6330.        
  6331.   #-----------------------------------------------------------------------------
  6332.   def swap_cards(index1, index2)
  6333.     @card_select_window.cards[index1], @card_select_window.cards[index2] =
  6334.     @card_select_window.cards[index2], @card_select_window.cards[index1]
  6335.   end
  6336.  
  6337.   alias _trace_on_skill_ok on_skill_ok
  6338.   def on_skill_ok
  6339.     trace_call("on_skill_ok")
  6340.     _trace_on_skill_ok
  6341.   end
  6342.  
  6343.   alias _trace_handle_hand_skill handle_hand_skill
  6344.   def handle_hand_skill(skill)
  6345.     trace_call("handle_hand_skill")
  6346.     _trace_handle_hand_skill(skill)
  6347.   end
  6348.  
  6349.   alias _trace_invoke_card_cost invoke_card_cost
  6350.   def invoke_card_cost(effect)
  6351.     trace_call("invoke_card_cost (#{effect})")
  6352.     _trace_invoke_card_cost(effect)
  6353.   end
  6354.  
  6355.   alias _trace_resolve_card_effect resolve_card_effect
  6356.   def resolve_card_effect(skill)
  6357.     @last_state_card_id = nil
  6358.     if skill.is_a?(RPG::UsableItem) && @last_state_card_id != skill.object_id
  6359.       @last_state_card_id = skill.object_id
  6360.       player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  6361.       if player && player.alive?
  6362.         skill.effects.each { |eff| player.add_state(eff.data_id) if eff.code == 21 }
  6363.       end
  6364.     end
  6365.     impact = nil
  6366.     if skill.note =~ /<card impact:\s*(.+?)>/i || skill.note =~ /<spell_impact:\s*(.+?)>/i || skill.note =~ /<field_impact:\s*(.+?)>/i || skill.note =~ /<hand_impact:\s*(.+?)>/i
  6367.       impact = $1.to_s.strip
  6368.     end
  6369.     if skill.cost
  6370.       invoke_card_cost(skill.cost)
  6371.     elsif impact
  6372.       if @current_card_action == :field_effect
  6373.         player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  6374.         if player && player.respond_to?(:pay_skill_cost)
  6375.           player.pay_skill_cost(skill)
  6376.           RPG::SE.new("Magic1", 80, 100).play rescue nil
  6377.           player.popup_destroyed_card(skill.icon_index, "Activated!") if player.respond_to?(:popup_destroyed_card)
  6378.         end
  6379.       end
  6380.       invoke_card_impact(impact)
  6381.     else
  6382.       skill_select(1)
  6383.     end
  6384.   end
  6385.  
  6386.   alias _trace_process_select_cards process_select_cards
  6387.   def process_select_cards(effect)
  6388.     trace_call("process_select_cards (#{effect})")
  6389.     _trace_process_select_cards(effect)
  6390.   end
  6391.  
  6392.   alias pc27_coin_invoke_impact invoke_card_impact unless method_defined?(:pc27_coin_invoke_impact)
  6393.   def invoke_card_impact(effect_name)
  6394.     # Buscamos cuál es la carta que está intentando resolver un efecto ahora mismo
  6395.     card = @active_effect_card || @skill || $chain_memory_card || @selected_card
  6396.    
  6397.     if card
  6398.       # 1. ESCÁNER DE MONEDAS (<coin data>)
  6399.       regex_coin = /<coin data:\s*#{Regexp.escape(effect_name.to_s)}\s*,\s*(\d+)>(.*?)<\/coin data>/im
  6400.       if card.note =~ regex_coin
  6401.         num_coins = $1.to_i
  6402.         conditions_text = $2.strip
  6403.         process_coin_toss(num_coins, conditions_text)
  6404.         return # Cortamos acá
  6405.       end
  6406.  
  6407.       # 2. ESCÁNER DE CONDICIONES (<condition data>)
  6408.       regex_cond = /<condition data:\s*#{Regexp.escape(effect_name.to_s)}\s*>(.*?)<\/condition data>/im
  6409.       if card.note =~ regex_cond
  6410.         conditions_text = $1.strip
  6411.         process_conditional_impact(conditions_text)
  6412.         return # Cortamos acá
  6413.       end
  6414.     end
  6415.    
  6416.     # 3. IMPACTO NORMAL (Si no es moneda ni condición)
  6417.     pc27_coin_invoke_impact(effect_name)
  6418.   end
  6419.  
  6420.   #==============================================================================
  6421.   # CANDADO DE LIMPIEZA (Evita que la carta se borre antes de tiempo)
  6422.   #==============================================================================
  6423.   alias pc27_macro_finish_proceed finish_proceed_selection unless method_defined?(:pc27_macro_finish_proceed)
  6424.   def finish_proceed_selection
  6425.     return if @suppress_finish_proceed # Si el candado está puesto, cancela la limpieza
  6426.     pc27_macro_finish_proceed
  6427.   end
  6428.  
  6429.   #==============================================================================
  6430.   # PROCESADOR DE IMPACTOS CONDICIONALES (VERSIÓN BLINDADA)
  6431.   #==============================================================================
  6432.   def process_conditional_impact(conditions_text)
  6433.     player = @active_effect_owner || BattleManager.actor || $game_party.members[0]
  6434.    
  6435.     # 1. Creamos una lista para anotar todos los impactos que aprueben el chequeo
  6436.     impacts_to_run = []
  6437.    
  6438.     conditions_text.split("\n").each do |line|
  6439.       next if line.strip.empty?
  6440.      
  6441.       if line =~ /(.+?)\s*:\s*(.+)/i
  6442.         condition_str = $1.strip
  6443.         impact_to_execute = $2.strip.downcase
  6444.        
  6445.         is_valid = false
  6446.        
  6447.         if condition_str.downcase == "always"
  6448.           is_valid = true
  6449.         else
  6450.           # Evaluamos la matemática en vivo
  6451.           begin
  6452.             is_valid = player.instance_eval(condition_str)
  6453.           rescue => e
  6454.             puts "[DEBUG] Error leyendo condition data: #{e.message}"
  6455.           end
  6456.         end
  6457.        
  6458.         # Si da luz verde, lo anotamos en la lista de espera
  6459.         impacts_to_run << impact_to_execute if is_valid
  6460.       end
  6461.     end
  6462.    
  6463.     # 2. PONEMOS EL CANDADO: ¡Nadie toca la carta de la mesa!
  6464.     @suppress_finish_proceed = true
  6465.    
  6466.     # 3. Ejecutamos todos los impactos aprobados uno por uno
  6467.     impacts_to_run.each_with_index do |imp, idx|
  6468.       invoke_card_impact(imp)
  6469.       # Hacemos la pausa dramática solo si queda otro impacto por ejecutarse
  6470.       20.times { update_basic } if SceneManager.scene_is?(Scene_Battle) && idx < impacts_to_run.size - 1
  6471.     end
  6472.    
  6473.     # 4. SACAMOS EL CANDADO
  6474.     @suppress_finish_proceed = false
  6475.    
  6476.     # 5. Ahora sí, soltamos los frenos y limpiamos la trampa al cementerio
  6477.     @is_interrupt_effect = false
  6478.     finish_proceed_selection if respond_to?(:finish_proceed_selection)
  6479.   end
  6480.  
  6481. end
  6482.  
  6483. #===============================================================================
  6484. # Scene_Deck
  6485. #===============================================================================
  6486.  
  6487. class Scene_Deck < Scene_MenuBase
  6488.   include PC27::CG
  6489.   #-----------------------------------------------------------------------------
  6490.   # Start
  6491.   #-----------------------------------------------------------------------------
  6492.   def start
  6493.     super()
  6494.     create_filter_window
  6495.     create_deck_window
  6496.     create_extra_window
  6497.     create_coll_window
  6498.     create_text_window
  6499.     create_card_window
  6500.     create_info_window
  6501.     @extra_window.deactivate
  6502.     @coll_window.deactivate
  6503.     @deck_window.activate
  6504.     @deck_window.select(0)
  6505.     update_text_window
  6506.   end
  6507.  
  6508.   def return_scene
  6509.     if $game_party.menu_actor.deck.size <= 39
  6510.       RPG::SE.new("Buzzer1", 80, 100).play
  6511.       $game_message.add('Your deck has less than 40 cards.')
  6512.       SceneManager.return if $game_party.menu_actor.deck.size == 0
  6513.     else
  6514.       SceneManager.return
  6515.     end
  6516.   end
  6517.  
  6518.   #-----------------------------------------------------------------------------
  6519.   # Creates a window to display deck cards
  6520.   #-----------------------------------------------------------------------------
  6521.   def create_deck_window
  6522.     @deck_window = Window_Deck.new(0,64)
  6523.     @deck_window.set_handler(:cancel,   method(:return_scene))
  6524.     @deck_window.set_handler(:ok,       method(:deck_to_coll))
  6525.   end
  6526.  
  6527.   #-----------------------------------------------------------------------------
  6528.   # Creates a window to display extra deck cards
  6529.   #-----------------------------------------------------------------------------
  6530.   def create_extra_window
  6531.     @extra_window = Window_Extra.new(0,280)
  6532.     @extra_window.set_handler(:cancel,   method(:return_scene))
  6533.     @extra_window.set_handler(:ok,       method(:extra_to_coll))
  6534.   end
  6535.  
  6536.   #-----------------------------------------------------------------------------
  6537.   # Creates a window to display collection cards
  6538.   #-----------------------------------------------------------------------------
  6539.   def create_coll_window
  6540.     @coll_window = Window_Coll.new((Graphics.width - 210)*0.5, 64)
  6541.     @coll_window.set_handler(:cancel,   method(:return_scene))
  6542.     @coll_window.set_handler(:ok,       method(:coll_to_deck))
  6543.     @coll_window.deactivate
  6544.   end
  6545.  
  6546.   #-----------------------------------------------------------------------------
  6547.   # Creates the window that shows where you are
  6548.   #-----------------------------------------------------------------------------
  6549.   def create_text_window
  6550.     @text_window = Window_Base.new(0, 0, Graphics.width - 210, 64)
  6551.     update_text_window
  6552.   end
  6553.  
  6554.   #-----------------------------------------------------------------------------
  6555.   # Creates the window that shows the currently selected card
  6556.   #-----------------------------------------------------------------------------
  6557.   def create_card_window
  6558.     @card_window = Window_CardHelp.new()
  6559.     @card_window.windowskin = Cache.system(CUSTOM_CARD_BG) if CUSTOM_CARD_BG
  6560.     @card_window.tone.set(*PC27::CG::ELEMENT_COLOR[0])
  6561.     @card_window.back_opacity = PC27::CG::CARD_BACK_OP
  6562.   end
  6563.  
  6564.   #-----------------------------------------------------------------------------
  6565.   # Creates a info window
  6566.   #-----------------------------------------------------------------------------
  6567.   def create_info_window
  6568.     height = Graphics.height - 284
  6569.     @info_window = Window_Base.new(Graphics.width - 210, Graphics.height - height, 210, height)
  6570.     @info_window.contents.font.size = 22
  6571.     @info_window.change_color(@info_window.normal_color)
  6572.     @info_window.draw_text(0, 0, 180, 20, "(Z/Enter): Move Card")
  6573.     @info_window.draw_text(0, 20, 180, 20, "(X/Esc): Exit")
  6574.     @info_window.draw_text(0, 40, 180, 20, "(A): Filter")
  6575.     @info_window.draw_text(0, 60, 180, 20, "(Q/W): Change Actor")
  6576.     @info_window.draw_text(0, 80, 180, 20, "(Shift): Main <-> Extra")
  6577.   end
  6578.  
  6579.   #-----------------------------------------------------------------------------
  6580.   # Creates a window to filter cards
  6581.   #-----------------------------------------------------------------------------
  6582.   def create_filter_window
  6583.     @filter_window = Window_Filter.new(0,Graphics.height * 0.5 - 48)
  6584.     @filter_window.set_handler(:cancel,   method(:close_filter))
  6585.     @filter_window.set_handler(:ok,       method(:change_filter))
  6586.     @filter_window.z = 150
  6587.     @filter_window.hide
  6588.     @filter_window.deactivate
  6589.   end
  6590.  
  6591.   #-----------------------------------------------------------------------------
  6592.   # Changes between collection and deck window
  6593.   #-----------------------------------------------------------------------------    
  6594.   def change_to_coll
  6595.     @coll_window.activate
  6596.     @deck_window.deactivate
  6597.     @extra_window.deactivate
  6598.     Sound.play_cursor
  6599.     Input.update
  6600.     update_text_window
  6601.   end
  6602.      
  6603.   def change_to_deck
  6604.     @deck_window.activate
  6605.     @coll_window.deactivate
  6606.     @extra_window.deactivate
  6607.     Sound.play_cursor
  6608.     Input.update
  6609.     update_text_window
  6610.   end
  6611.  
  6612.   def change_to_extra
  6613.     @deck_window.deactivate
  6614.     @coll_window.deactivate
  6615.     @extra_window.activate
  6616.     Sound.play_cursor
  6617.     Input.update
  6618.     update_text_window
  6619.   end
  6620.  
  6621.   #-----------------------------------------------------------------------------
  6622.   # Adds commands to exchange cards
  6623.   #-----------------------------------------------------------------------------
  6624.   def coll_to_deck
  6625.     for skill in $game_party.coll
  6626.       /coll(?<skill_id>\d+)/ =~ @coll_window.current_symbol.to_s
  6627.       skill_id  = skill_id.to_i
  6628.       max_cards = (MAX_SAME_EXCEP.has_key?(skill_id) ? MAX_SAME_EXCEP[skill_id] : MAX_SAME_CARDS)
  6629.       if skill.id == skill_id && skill.type == 4
  6630.         if $game_party.menu_actor.extra.size >= MAX_EXTRA_SIZE
  6631.           RPG::SE.new("Buzzer1", 80, 100).play
  6632.           return
  6633.         end
  6634.         if $game_party.menu_actor.extra.count{ |skill| skill.id == skill_id } >= max_cards
  6635.           RPG::SE.new("Buzzer1", 80, 100).play
  6636.           return
  6637.         end
  6638.         $game_party.menu_actor.extra.push(
  6639.         $game_party.coll.slice!($game_party.coll.index(skill)))
  6640.         break
  6641.       elsif skill.id == skill_id
  6642.         if $game_party.menu_actor.deck.size >= MAX_DECK_SIZE
  6643.           RPG::SE.new("Buzzer1", 80, 100).play
  6644.           return
  6645.         end
  6646.         if $game_party.menu_actor.deck.count{ |skill| skill.id == skill_id } >= max_cards
  6647.           RPG::SE.new("Buzzer1", 80, 100).play
  6648.           return
  6649.         end
  6650.         $game_party.menu_actor.deck.push(
  6651.         $game_party.coll.slice!($game_party.coll.index(skill)))
  6652.         break
  6653.       end
  6654.     end
  6655.     RPG::SE.new("Cursor1", 80, 100).play
  6656.     @deck_window.refresh
  6657.     @extra_window.refresh
  6658.     @coll_window.refresh
  6659.     update_text_window
  6660.   end
  6661.  
  6662.   def deck_to_coll
  6663.     /deck(?<skill_id>\d+)/ =~ @deck_window.current_symbol.to_s
  6664.     skill_id = skill_id.to_i
  6665.     for skill in $game_party.menu_actor.deck
  6666.       if skill.id == skill_id
  6667.         $game_party.coll.push(
  6668.         $game_party.menu_actor.deck.slice!($game_party.menu_actor.deck.index(skill)))
  6669.         break
  6670.       end
  6671.     end
  6672.     RPG::SE.new("Cursor1", 80, 100).play
  6673.     @deck_window.refresh
  6674.     @coll_window.refresh
  6675.     update_text_window
  6676.   end
  6677.  
  6678.   def extra_to_coll
  6679.     /extra(?<skill_id>\d+)/ =~ @extra_window.current_symbol.to_s
  6680.     skill_id = skill_id.to_i
  6681.     for skill in $game_party.menu_actor.extra
  6682.       if skill.id == skill_id
  6683.         $game_party.coll.push(
  6684.         $game_party.menu_actor.extra.slice!($game_party.menu_actor.extra.index(skill)))
  6685.         break
  6686.       end
  6687.     end
  6688.     RPG::SE.new("Cursor1", 80, 100).play
  6689.     @extra_window.refresh
  6690.     @coll_window.refresh
  6691.     update_text_window
  6692.   end
  6693.  
  6694.   #-----------------------------------------------------------------------------
  6695.   # Activates filter
  6696.   #-----------------------------------------------------------------------------
  6697.   def activate_filter
  6698.     @filter_window.show
  6699.     @filter_window.activate
  6700.     @deck_window.deactivate
  6701.     @extra_window.deactivate
  6702.     @coll_window.deactivate
  6703.   end
  6704.  
  6705.   #-----------------------------------------------------------------------------
  6706.   # Changes filter settings
  6707.   #-----------------------------------------------------------------------------
  6708.   def change_filter
  6709.     case @filter_window.current_symbol
  6710.     when :filter_element
  6711.       @coll_window.filter[0]   += 1
  6712.       @filter_window.filter[0] += 1
  6713.       @coll_window.filter[0]    = 0 if $data_system.elements.size == @coll_window.filter[0]
  6714.       @filter_window.filter[0]  = 0 if $data_system.elements.size == @filter_window.filter[0]
  6715.     when :filter_type
  6716.       @coll_window.filter[1]   += 1
  6717.       @filter_window.filter[1] += 1
  6718.       @coll_window.filter[1]    = 0 unless TYPE.has_key?(@coll_window.filter[1])
  6719.       @filter_window.filter[1]  = 0 unless TYPE.has_key?(@filter_window.filter[1])
  6720.     when :filter_rarity
  6721.       @coll_window.filter[2]   += 1
  6722.       @filter_window.filter[2] += 1
  6723.       @coll_window.filter[2]    = 0 unless RARITY.has_key?(@coll_window.filter[2])
  6724.       @filter_window.filter[2]  = 0 unless RARITY.has_key?(@filter_window.filter[2])
  6725.     when :filter_subtype
  6726.       @coll_window.filter[3]   += 1
  6727.       @filter_window.filter[3] += 1
  6728.       @coll_window.filter[3]    = 0 unless SUBTYPE.has_key?(@coll_window.filter[3])
  6729.       @filter_window.filter[3]  = 0 unless SUBTYPE.has_key?(@filter_window.filter[3])
  6730.     when :filter_level
  6731.       @coll_window.filter[4]   += 1
  6732.       @filter_window.filter[4] += 1
  6733.       @coll_window.filter[4]    = 0 unless LEVEL.has_key?(@coll_window.filter[4])
  6734.       @filter_window.filter[4]  = 0 unless LEVEL.has_key?(@filter_window.filter[4])
  6735.     when :filter_normeff
  6736.       @coll_window.filter[5]   += 1
  6737.       @filter_window.filter[5] += 1
  6738.       @coll_window.filter[5]    = 0 unless NORMEFF.has_key?(@coll_window.filter[5])
  6739.       @filter_window.filter[5]  = 0 unless NORMEFF.has_key?(@filter_window.filter[5])
  6740.     when :filter_frame
  6741.       @coll_window.filter[6]   += 1
  6742.       @filter_window.filter[6] += 1
  6743.       @coll_window.filter[6]    = 0 unless FRAME.has_key?(@coll_window.filter[6])
  6744.       @filter_window.filter[6]  = 0 unless FRAME.has_key?(@filter_window.filter[6])
  6745.     when :filter_archetype
  6746.       @coll_window.filter[7]   += 1
  6747.       @filter_window.filter[7] += 1
  6748.       @coll_window.filter[7]    = 0 unless ARCHETYPE.has_key?(@coll_window.filter[7])
  6749.       @filter_window.filter[7]  = 0 unless ARCHETYPE.has_key?(@filter_window.filter[7])
  6750.     when :filter_atk
  6751.       @coll_window.filter[8]   += 1
  6752.       @filter_window.filter[8] += 1
  6753.       @coll_window.filter[8]    = 0 unless ATK.has_key?(@coll_window.filter[8])
  6754.       @filter_window.filter[8]  = 0 unless ATK.has_key?(@filter_window.filter[8])
  6755.       when :filter_DEF
  6756.       @coll_window.filter[9]   += 1
  6757.       @filter_window.filter[9] += 1
  6758.       @coll_window.filter[9]    = 0 unless DEF.has_key?(@coll_window.filter[9])
  6759.       @filter_window.filter[9]  = 0 unless DEF.has_key?(@filter_window.filter[9])
  6760.     when :filter_special
  6761.       @coll_window.filter[10]   += 1
  6762.       @filter_window.filter[10] += 1
  6763.       @coll_window.filter[10]    = 0 unless SPECIAL.has_key?(@coll_window.filter[10])
  6764.       @filter_window.filter[10]  = 0 unless SPECIAL.has_key?(@filter_window.filter[10])
  6765.     when :confirm
  6766.       $game_party.coll += $game_party.menu_actor.deck + $game_party.menu_actor.extra
  6767.       $game_party.menu_actor.deck = []
  6768.       $game_party.menu_actor.extra = []
  6769.       @filter_window.confirm = false
  6770.       @filter_window.refresh
  6771.     when :call_confirm
  6772.       @filter_window.confirm = true
  6773.       @filter_window.refresh
  6774.     end
  6775.     @filter_window.refresh
  6776.     @coll_window.refresh
  6777.     @deck_window.refresh
  6778.     @extra_window.refresh
  6779.     update_text_window
  6780.   end
  6781.  
  6782.   #-----------------------------------------------------------------------------
  6783.   # Deativates filter
  6784.   #-----------------------------------------------------------------------------
  6785.   def close_filter
  6786.     @filter_window.hide
  6787.     @filter_window.deactivate
  6788.     @coll_window.activate
  6789.     @coll_window.select(0)
  6790.   end
  6791.  
  6792.   def next_actor
  6793.     if $game_party.menu_actor.deck.size <= 39
  6794.       RPG::SE.new("Buzzer1", 80, 100).play
  6795.       $game_message.add('Your deck has less than 40 cards.')
  6796.     else
  6797.       @actor = $game_party.menu_actor_next
  6798.       on_actor_change
  6799.     end
  6800.   end
  6801.  
  6802.   def prev_actor
  6803.     if $game_party.menu_actor.deck.size <= 39
  6804.       RPG::SE.new("Buzzer1", 80, 100).play
  6805.       $game_message.add('Your deck has less than 40 cards.')
  6806.     else
  6807.       @actor = $game_party.menu_actor_prev
  6808.       on_actor_change
  6809.     end
  6810.   end
  6811.    
  6812.   #-----------------------------------------------------------------------------
  6813.   # * Frame Update
  6814.   #-----------------------------------------------------------------------------
  6815.   def update
  6816.     super()
  6817.     if @filter_window.visible == false
  6818.       if Input.trigger?(:A)
  6819.         if @deck_window.active
  6820.           change_to_extra
  6821.         elsif @extra_window.active
  6822.           change_to_deck
  6823.         else
  6824.           change_to_extra
  6825.         end
  6826.       end
  6827.       change_to_coll  if Input.trigger?(:RIGHT) && !@coll_window.active
  6828.       change_to_deck  if Input.trigger?(:LEFT)  && @coll_window.active
  6829.       activate_filter if Input.trigger?(:X)
  6830.       next_actor      if Input.trigger?(:R)
  6831.       prev_actor      if Input.trigger?(:L)
  6832.     end
  6833.     if @coll_window.active
  6834.       symbol = @coll_window.current_symbol
  6835.     elsif @deck_window.active
  6836.       symbol = @deck_window.current_symbol
  6837.     elsif @extra_window.active
  6838.       symbol = @extra_window.current_symbol
  6839.     end
  6840.     if symbol != @symbol
  6841.       @symbol = symbol
  6842.       id_string = @symbol.to_s[/\d+/]
  6843.       skill = (id_string ? $data_skills[id_string.to_i] : nil)
  6844.       @card_window.set_item(skill)
  6845.     end  
  6846.   end
  6847.  
  6848.   def on_actor_change
  6849.     RPG::SE.new("Cursor2", 80, 100).play
  6850.     @deck_window.refresh
  6851.     @extra_window.refresh
  6852.     @deck_window.select(0)
  6853.     @extra_window.select(0)
  6854.     @coll_window.select(0)
  6855.     update_text_window
  6856.   end
  6857.  
  6858.   def update_text_window
  6859.     return unless @text_window
  6860.     @text_window.contents.clear
  6861.     @text_window.contents.font.size = 20
  6862.     active_win = :none
  6863.     active_win = :deck if @deck_window && @deck_window.active
  6864.     active_win = :extra if @extra_window && @extra_window.active
  6865.     active_win = :coll if @coll_window && @coll_window.active
  6866.     hl_color = @text_window.text_color(14)
  6867.     nm_color = @text_window.normal_color
  6868.     current_actor = @actor || $game_party.menu_actor
  6869.     @text_window.change_color(active_win == :deck ? hl_color : nm_color)
  6870.     @text_window.draw_text(0, -4, 90, 24, "Main Deck:")
  6871.     @text_window.change_color(nm_color)
  6872.     @text_window.draw_text(90, -4, 60, 24, "#{current_actor.deck.size}/#{MAX_DECK_SIZE}")
  6873.     @text_window.change_color(active_win == :extra ? hl_color : nm_color)
  6874.     @text_window.draw_text(0, 18, 90, 24, "Extra Deck:")
  6875.     @text_window.change_color(nm_color)
  6876.     @text_window.draw_text(90, 18, 60, 24, "#{current_actor.extra.size}/#{MAX_EXTRA_SIZE}")
  6877.     coll_x = 160
  6878.     @text_window.change_color(active_win == :coll ? hl_color : nm_color)
  6879.     @text_window.draw_text(coll_x, -4, 90, 24, "Collection:")
  6880.     @text_window.change_color(nm_color)
  6881.     @text_window.draw_text(coll_x + 90, -4, 50, 24, "#{$game_party.coll.size}")
  6882.     @text_window.change_color(hl_color)
  6883.     @text_window.draw_text(coll_x, 18, 140, 24, current_actor.name, 0)
  6884.   end
  6885.  
  6886. end
  6887.  
  6888. class Window_Deck < Window_Command
  6889.  
  6890.   #--------------------------------------------------------------------------
  6891.   # Makes Command List
  6892.   #--------------------------------------------------------------------------
  6893.   def make_command_list
  6894.     add_deck_commands
  6895.   end
  6896.  
  6897.   #--------------------------------------------------------------------------
  6898.   # Add Card Commands to List
  6899.   #--------------------------------------------------------------------------
  6900.   def add_deck_commands
  6901.     $game_party.menu_actor.sort_cards(:deck)
  6902.     deck = $game_party.menu_actor.deck
  6903.     if deck.empty?
  6904.       add_command("---", :nothing)
  6905.       return
  6906.     end
  6907.     hash = {}
  6908.     for skill in deck
  6909.       if hash.has_key?(skill.id)
  6910.         hash[skill.id] += 1
  6911.       else
  6912.         hash[skill.id] = 1
  6913.       end
  6914.     end
  6915.     hash.each_key do |id|
  6916.       add_command(hash[id].to_s + "x " + $data_skills[id].name, ("deck" + id.to_s).to_sym, true, id)
  6917.     end
  6918.   end
  6919.  
  6920.   def draw_item(index)
  6921.     change_color(normal_color, command_enabled?(index))
  6922.     rect = item_rect_for_text(index)
  6923.     card_id = @list[index][:ext]
  6924.     if card_id && card_id.is_a?(Integer)
  6925.       icon_index = $data_skills[card_id].icon_index
  6926.       draw_icon(icon_index, rect.x, rect.y)
  6927.       rect.x += 28
  6928.       rect.width -= 28  
  6929.     end
  6930.     draw_text(rect, command_name(index), alignment)
  6931.   end
  6932.  
  6933.   #--------------------------------------------------------------------------
  6934.   # Get Window Height
  6935.   #--------------------------------------------------------------------------
  6936.   def window_height
  6937.     Graphics.height - 200
  6938.   end
  6939.  
  6940.   #--------------------------------------------------------------------------
  6941.   # Get Window width
  6942.   #--------------------------------------------------------------------------
  6943.   def window_width
  6944.     (Graphics.width - 210) * 0.5
  6945.   end
  6946.  
  6947.   #--------------------------------------------------------------------------
  6948.   # Overwrite process_ok so the window won't be disabled
  6949.   #--------------------------------------------------------------------------
  6950.   def process_ok
  6951.     if current_item_enabled?
  6952.       Input.update
  6953.       call_ok_handler
  6954.       select(index - 1) unless current_symbol
  6955.     else
  6956.       Sound.play_buzzer
  6957.     end
  6958.   end
  6959.  
  6960. end
  6961.  
  6962. class Window_Extra < Window_Command
  6963.  
  6964.   #--------------------------------------------------------------------------
  6965.   # Makes Command List
  6966.   #--------------------------------------------------------------------------
  6967.   def make_command_list
  6968.     add_extra_commands
  6969.   end
  6970.  
  6971.   #--------------------------------------------------------------------------
  6972.   # Add Card Commands to List
  6973.   #--------------------------------------------------------------------------
  6974.   def add_extra_commands
  6975.     $game_party.menu_actor.sort_cards(:extra)
  6976.     extra = $game_party.menu_actor.extra
  6977.     if extra.empty?
  6978.       add_command("---", :nothing)
  6979.       return
  6980.     end
  6981.     hash = {}
  6982.     for skill in extra
  6983.       if hash.has_key?(skill.id)
  6984.         hash[skill.id] += 1
  6985.       else
  6986.         hash[skill.id] = 1
  6987.       end
  6988.     end
  6989.     hash.each_key do |id|
  6990.       add_command(hash[id].to_s + "x " + $data_skills[id].name, ("extra" + id.to_s).to_sym, true, id)
  6991.     end
  6992.   end
  6993.  
  6994.   def draw_item(index)
  6995.     change_color(normal_color, command_enabled?(index))
  6996.     rect = item_rect_for_text(index)
  6997.     card_id = @list[index][:ext]
  6998.     if card_id && card_id.is_a?(Integer)
  6999.       icon_index = $data_skills[card_id].icon_index
  7000.       draw_icon(icon_index, rect.x, rect.y)
  7001.       rect.x += 28
  7002.       rect.width -= 28  
  7003.     end
  7004.     draw_text(rect, command_name(index), alignment)
  7005.   end
  7006.  
  7007.   #--------------------------------------------------------------------------
  7008.   # Get Window Height
  7009.   #--------------------------------------------------------------------------
  7010.   def window_height
  7011.     136
  7012.   end
  7013.  
  7014.   #--------------------------------------------------------------------------
  7015.   # Get Window width
  7016.   #--------------------------------------------------------------------------
  7017.   def window_width
  7018.     (Graphics.width - 210) * 0.5
  7019.   end
  7020.  
  7021.   #--------------------------------------------------------------------------
  7022.   # Overwrite process_ok so the window won't be disabled
  7023.   #--------------------------------------------------------------------------
  7024.   def process_ok
  7025.     if current_item_enabled?
  7026.       Input.update
  7027.       call_ok_handler
  7028.       select(index - 1) unless current_symbol
  7029.     else
  7030.       Sound.play_buzzer
  7031.     end
  7032.   end
  7033.  
  7034. end
  7035.  
  7036. class Window_Coll < Window_Command
  7037.  
  7038.   attr_accessor :filter
  7039.    
  7040.   def initialize(width, heigth)
  7041.     @filter = [0,0,0,0,0,0,0,0,0,0,0]
  7042.     super(width, heigth)
  7043.   end
  7044.  
  7045.   #--------------------------------------------------------------------------
  7046.   # Makes Command List
  7047.   #--------------------------------------------------------------------------
  7048.   def make_command_list
  7049.     add_coll_commands
  7050.   end
  7051.  
  7052.   #--------------------------------------------------------------------------
  7053.   # Add Collection Commands to List
  7054.   #--------------------------------------------------------------------------
  7055.   def add_coll_commands
  7056.     $game_party.coll.sort!{|a,b| a.id <=> b.id}
  7057.     coll = $game_party.coll
  7058.     if coll.empty?
  7059.       add_command("---", :nothing)
  7060.       return
  7061.     end
  7062.     hash = {}
  7063.     for skill in coll
  7064.       next unless match_filter?(skill)
  7065.       if hash.has_key?(skill.id)
  7066.         hash[skill.id] += 1
  7067.       else
  7068.         hash[skill.id] = 1
  7069.       end
  7070.     end
  7071.     hash.each_key do |id|
  7072.       add_command(hash[id].to_s + "x " + $data_skills[id].name, ("coll" + id.to_s).to_sym, true, id)
  7073.     end
  7074.   end
  7075.  
  7076.   def draw_item(index)
  7077.     change_color(normal_color, command_enabled?(index))
  7078.     rect = item_rect_for_text(index)
  7079.     card_id = @list[index][:ext]
  7080.     if card_id && card_id.is_a?(Integer)
  7081.       icon_index = $data_skills[card_id].icon_index
  7082.       draw_icon(icon_index, rect.x, rect.y)
  7083.       rect.x += 28
  7084.       rect.width -= 28  
  7085.     end
  7086.     draw_text(rect, command_name(index), alignment)
  7087.   end
  7088.  
  7089.   #--------------------------------------------------------------------------
  7090.   # Checks if a card meets the specified filter criteria
  7091.   #--------------------------------------------------------------------------
  7092.   def match_filter?(skill)
  7093.     return false unless skill.attribute          == @filter[0] || @filter[0] == 0
  7094.     return false unless skill.type               == @filter[1] || @filter[1] == 0
  7095.     return false unless skill.rarity             == @filter[2] || @filter[2] == 0
  7096.     return false unless skill.subtype            == @filter[3] || @filter[3] == 0
  7097.     return false unless skill.level              == @filter[4] || @filter[4] == 0
  7098.     return false unless skill.normeff            == @filter[5] || @filter[5] == 0
  7099.     return false unless skill.frame              == @filter[6] || @filter[6] == 0
  7100.     return false unless skill.archetype          == @filter[7] || @filter[7] == 0
  7101.     return false unless skill.atk                == @filter[8] || @filter[8] == 0
  7102.     return false unless skill.def                == @filter[9] || @filter[9] == 0
  7103.     return false unless skill.special            == @filter[10] || @filter[10] == 0
  7104.     return true
  7105.   end
  7106.  
  7107.  
  7108.   #--------------------------------------------------------------------------
  7109.   # Get Window Height
  7110.   #--------------------------------------------------------------------------
  7111.   def window_height
  7112.     Graphics.height - 64
  7113.   end
  7114.  
  7115.   #--------------------------------------------------------------------------
  7116.   # Get Window width
  7117.   #--------------------------------------------------------------------------
  7118.   def window_width
  7119.     (Graphics.width - 210) * 0.5
  7120.   end
  7121.  
  7122.   def process_ok
  7123.     if current_item_enabled?
  7124.       Input.update
  7125.       call_ok_handler
  7126.       select(index - 1) unless current_symbol
  7127.     else
  7128.       Sound.play_buzzer
  7129.     end
  7130.   end
  7131.  
  7132. end
  7133.  
  7134. class Window_Filter < Window_Command
  7135.   include PC27::CG
  7136.  
  7137.   attr_accessor :filter
  7138.   attr_accessor :confirm
  7139.  
  7140.   def initialize(width, heigth)
  7141.     @filter = [0,0,0,0,0,0,0,0,0,0,0]
  7142.     @confirm = false
  7143.     super(width, heigth)
  7144.     hide
  7145.   end
  7146.    
  7147.   #--------------------------------------------------------------------------
  7148.   # Makes Command List
  7149.   #--------------------------------------------------------------------------
  7150.   def make_command_list
  7151.     add_filter_commands
  7152.   end
  7153.  
  7154.   #--------------------------------------------------------------------------
  7155.   # Add Collection Commands to List
  7156.   #--------------------------------------------------------------------------
  7157.   def add_filter_commands
  7158.     add_command(FILTER_TEXT + F_ELEMENT + (@filter[0]==0 ? "---" : $data_system.elements[@filter[0]] ), :filter_element)
  7159.     add_command(FILTER_TEXT + F_TYPE    + (@filter[1]==0 ? "---" : TYPE[@filter[1]]),        :filter_type)
  7160.     add_command(FILTER_TEXT + F_RARITY  + (@filter[2]==0 ? "---" : RARITY[@filter[2]][1]),   :filter_rarity)
  7161.     add_command(FILTER_TEXT + F_SUBTYPE + (@filter[3]==0 ? "---" : SUBTYPE[@filter[3]][1]),   :filter_subtype)
  7162.     add_command(FILTER_TEXT + F_LEVEL   + (@filter[4]==0 ? "---" : LEVEL[@filter[4]][1]),   :filter_level)
  7163.     add_empty_deck_command
  7164.   end
  7165.   #--------------------------------------------------------------------------
  7166.   # Get Window Height
  7167.   #--------------------------------------------------------------------------
  7168.   def window_height
  7169.     96
  7170.   end
  7171.   #--------------------------------------------------------------------------
  7172.   # Get Window width
  7173.   #--------------------------------------------------------------------------
  7174.   def window_width
  7175.     Graphics.width
  7176.   end
  7177.  
  7178.   def process_ok
  7179.     if current_item_enabled?
  7180.       RPG::SE.new("Cursor1", 80, 100)
  7181.       Input.update
  7182.       call_ok_handler
  7183.     else
  7184.       Sound.play_buzzer
  7185.     end
  7186.   end
  7187.  
  7188.   def add_empty_deck_command
  7189.     if @confirm
  7190.       add_command(CONFIRM, :confirm)
  7191.     else
  7192.       add_command(EMPTY_DECK, :call_confirm)
  7193.     end
  7194.   end
  7195.  
  7196.   def hide
  7197.     @confirm = false
  7198.     super
  7199.   end
  7200. end
  7201.  
  7202. class Window_MenuCommand < Window_Command
  7203.  
  7204.   #--------------------------------------------------------------------------
  7205.   # Replaces skills with Deck (overwrite)
  7206.   #--------------------------------------------------------------------------
  7207.  
  7208.   def add_main_commands
  7209.     add_command(Vocab::item,    :item,   main_commands_enabled)
  7210.     add_command(PC27::CG::DECK, :deck,   main_commands_enabled)
  7211.     add_command(Vocab::equip,   :equip,  main_commands_enabled)
  7212.     add_command(Vocab::status,  :status, main_commands_enabled)
  7213.   end
  7214.  
  7215. end
  7216.  
  7217. class Scene_Menu < Scene_MenuBase  
  7218.  
  7219.   #--------------------------------------------------------------------------
  7220.   # Adds a handler to access the deck scene (alias)
  7221.   #--------------------------------------------------------------------------
  7222.   alias pc27_cardgame_scenemenu_create_cmd_wdw             create_command_window
  7223.   def create_command_window
  7224.     pc27_cardgame_scenemenu_create_cmd_wdw
  7225.     @command_window.set_handler(:deck,    method(:command_personal))
  7226.   end
  7227.  
  7228.   #--------------------------------------------------------------------------
  7229.   # Adds :deck to the personal command process (alias)
  7230.   #--------------------------------------------------------------------------
  7231.   alias pc27_cardgame_scenemenu_on_personal_ok                    on_personal_ok
  7232.   def on_personal_ok
  7233.     pc27_cardgame_scenemenu_on_personal_ok
  7234.     case @command_window.current_symbol
  7235.     when :deck
  7236.       SceneManager.call(Scene_Deck)
  7237.     end
  7238.   end
  7239.  
  7240. end
  7241.  
  7242. class Window_CardOptions < Window_Command
  7243.   def initialize(options, disabled_indices)
  7244.     @options = options
  7245.     @disabled_indices = disabled_indices
  7246.     super(0, 0)
  7247.     self.x = Graphics.width - 240
  7248.     ideal_y = 274
  7249.     max_y = Graphics.height - 114 - self.window_height
  7250.     self.y = [ideal_y, max_y].min
  7251.     self.z = 500
  7252.   end
  7253.  
  7254.   def window_width; return 240; end
  7255.  
  7256.   def make_command_list
  7257.     @options.each_with_index do |opt, i|
  7258.       add_command(opt, :ok, !@disabled_indices.include?(i))
  7259.     end
  7260.   end
  7261. end
  7262. ################################################################################
  7263. #                            End of script                                     #
  7264. ################################################################################
Add Comment
Please, Sign In to add comment