Wavescripts

Wave's Playing Cards

Aug 23rd, 2014
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 20.32 KB | None | 0 0
  1. ############################################
  2. #                                          #
  3. #          PLAYING CARDS SCRIPT            #
  4. #                                          #
  5. #       v1.0 for RPG Maker VX Ace          #
  6. #                                          #
  7. # Created by Jason "Wavelength" Commander  #
  8. #                                          #
  9. ############################################
  10.  
  11. # "Never lost a fair game... or played one."
  12. #                                     ~ Twisted Fate, League of Legends
  13.  
  14.  
  15. ############################################
  16. #                                          #
  17. #           ABOUT THIS SCRIPT              #
  18. #                                          #
  19. ############################################
  20.  
  21. # This script allows you to pick random cards from a deck,
  22. #        store information about the card you chose to an
  23. #        easily-accessible variable, and display the chosen
  24. #        card to the player (via text, pictures, or both).
  25. #        You can also prevent that card from being chosen again
  26. #        until you "shuffle" the deck.
  27.  
  28. # Here's what the Playing Cards script allows you to do that
  29. #        you couldn't otherwise do with simple eventing:
  30. #
  31. #       * Remove cards from the deck as they are drawn, if desired
  32. #
  33. #       * Get information about the value and suit of a
  34. #         randomly-drawn card using a single-line script call
  35. #
  36. #       * Show a picture of that randomly-drawn card using a
  37. #         single-line script call
  38. #
  39. #       * Show the name (or value) of that randomly-drawn card
  40. #         in a single message box
  41. #
  42. #       * Automatically convert a playing card's value for use in
  43. #         a minigame; e.g., making all Jacks, Queens, & Kings worth
  44. #         10 when playing Blackjack
  45. #
  46. #       * Save hundreds or even thousands of event commands worth
  47. #         of effort and processing power when making card games
  48. #
  49. #       * Create your own deck of cards with different numbers or
  50. #         suits (or even get rid of numbers or suits altogether),
  51. #         and use the functionality of this script for your deck
  52. #
  53. # And don't forget that the logic underlying the script can be
  54. #       useful in all sorts of RNG-type applications, not just
  55. #       card games!
  56.  
  57. # This script is compatible with all other scripts.    
  58.  
  59.  
  60. ############################################
  61. #                                          #
  62. #              TERMS OF USE                #
  63. #                                          #
  64. ############################################
  65.  
  66. #  Free to use for non-commercial projects - just credit me
  67.  
  68. #  A license is required to use this script in commercial or
  69. #       semi-commercial projects.  Please visit my website
  70. #       wavescripts.wordpress.com for more info about obtaining
  71. #       a license for my scripts.
  72.  
  73. #  You may freely share or modify this script, but you cannot
  74. #       sell it (even if modified) without my written permission
  75.  
  76. #  Please preserve the header (with version #) and terms of use;
  77. #       besides that, feel free to remove any commenting you please
  78.  
  79.  
  80. ############################################
  81. #                                          #
  82. #         HOW TO USE THIS SCRIPT           #
  83. #                                          #
  84. ############################################
  85.  
  86.  
  87. # First, set values for the variables in the "Playing Cards Setup"
  88. #       section below.  If you want to change the composition of
  89. #       the deck, change the way that card IDs are converted to
  90. #       usable values, or show pictures of cards that are selected,
  91. #       you should also visit the "Advanced Setup" section below.
  92.  
  93. # You can use a single-line script call in the Event Editor (or
  94. #       combine several script calls into a single script block)
  95. #       to draw a card, show a picture of the last card drawn,
  96. #       or change the cards available in the deck.
  97. #
  98. # Here are the script calls that this script provides:
  99. #
  100. #   1. $game_party.draw_card(game)
  101. #
  102. #   Randomly draws a card from the deck, and automatically stores
  103. #   information about the card that was drawn.  If the "Removal"
  104. #   option is set to True, the drawn card is removed from the deck.
  105. #   * "game" is an optional integer that specifies which "Conv_Value"
  106. #   array (if any) will be used to automatically convert the value of
  107. #   the card.
  108. #   Aliases for draw_card: d_c
  109. #
  110. #   2. $game_party.show_card(picture_number, x, y)
  111. #
  112. #   Shows a picture of the last card that was drawn.  If you are using
  113. #   this command, you will need to import a picture for each card into
  114. #   your Graphics/Pictures folder.
  115. #   * The name of the picture should be Cards_1.png, Cards_2.png, etc.,
  116. #   unless you have changed the Pic_prefix or Pic_extension options.
  117. #   * "picture_number", "x", and "y" are all optional integers that specify
  118. #   the Picture Number, x-coordinate, and y-coordinate for the picture (if
  119. #   not provided, the default values will be used).
  120. #   * To erase the picture of the card, simply use the "Erase Picture" event
  121. #   command and use the picture_number that you chose here.
  122. #   Aliases for show_card: s_c
  123.  
  124. #   3. $game_party.pullcard(card)
  125. #
  126. #   Removes a card from the deck so that it may not be drawn until added
  127. #   back into the deck.
  128. #   * The "card" integer is used to specify which card to remove.  For
  129. #   example, in a standard deck, $game_party.pullcard(1) will remove the
  130. #   2 of Spades from the deck, while $game_party.addback(52) will remove
  131. #   the Ace of Hearts.
  132. #   Aliases for pullcard: pull_card, removecard, remove_card
  133.  
  134. #   4. $game_party.addback(card)
  135. #
  136. #   Adds a card into the deck so that it may once again be drawn.
  137. #   * The "card" integer is used to specify which card to add.
  138. #   Aliases for addback: add_back, addcard, add_card
  139.  
  140. #   5. $game_party.shuffle
  141. #
  142. #   Adds all cards back into the deck.  You'll probably want to use
  143. #   this command at the beginning or end of a hand in most games.
  144. #   Aliases for shuffle: (none)
  145.  
  146. #   6. $game_party.cards_remaining(variable)
  147. #
  148. #   Returns the number of cards remaining in the deck.  This could
  149. #   be useful if you need your minigame to shuffle the deck when there
  150. #   are a certain number of cards (or no cards) remaning.
  151. #   * "variable" is an optional integer that specifies a Variable to
  152. #   which save the number of cards remaining, in case you need to use
  153. #   it later.
  154. #   Aliases for cards_remaining: c_r
  155.  
  156. # After using the "draw_card" method, you will have access to the
  157. #       card's ID (position in the deck), value, suit.  Use whatever
  158. #       variable you specified in Card_holder below to access the
  159. #       card's ID, Value_holder for its value, and Suit_holder for
  160. #       its suit.  (Name can also be accessed, if necessary, using
  161. #       $game_actors[10].name - replace 10 with the value you specified
  162. #       for Name_holder.)  If you need to preserve the values, simply
  163. #       use event commands to copy th variables' values before using the
  164. #       draw_card method again.
  165.  
  166. # You can use \V[x] (for Card Holder, Value Holder, and Suit Holder)
  167. #       and \N[x] (for Name Holder) inside Message Boxes to show the
  168. #       ID, Value, Suit, or Name of the last card that was drawn.
  169.  
  170. # If you try to draw a card when there are no cards left in the deck,
  171. #       a special value -999 will be displayed for Card ID/Value/Suit
  172. #       and a special value "OUT OF CARDS" will be displayed for Card Name.
  173. #       In general, you should design your game so that cards are shuffled
  174. #       as soon as (or before) the last card is drawn from the deck.
  175.  
  176.  
  177. module Cards
  178.  
  179. ############################################
  180. #                                          #
  181. #          PLAYING CARDS SETUP             #
  182. #                                          #
  183. ############################################
  184.  
  185. # *** You must correctly set these values for the script to work properly. ***
  186.  
  187. # Choose which variables and actor names will be used to store information
  188. #       about the last card selected.  For example, if you want Variable 208
  189. #       to store information about about the Suit of the card that was chosen,
  190. #       then change the Suit_holder from 43 to 208.
  191.  
  192. # These variables will come in handy as you can use simple eventing logic to
  193. #       make things happen based on the value, suit, or identity of the card
  194. #       that was chosen after the draw_card call.
  195.  
  196. # * Card_holder is the variable used to hold info about the ID ("position"
  197. #       in the deck) for the chosen card.  (In a standard deck of playing
  198. #       cards, the 2 of Spades has a Card_Holder value of "1", the 2 of
  199. #       Clubs has a value of "2", the Ace of Spades "49", and the Ace of
  200. #       Hearts "52".)
  201.  
  202.   Card_holder = 41
  203.  
  204. # * Value_holder is the variable used to hold info about the "number" on
  205. #       your card.  (In a standard deck of playing cards, the 2 of Spades
  206. #       and 2 of Clubs both have a Value_holder value of "1", the 3 of
  207. #       Spades and the 3 of Clubs have a Value_holder value of "2", and
  208. #       the Ace of Spades and Ace of Hearts have a value of "13".  If you
  209. #       want to change these values)
  210.  
  211.   Value_holder = 42
  212.  
  213. # * Suit_holder is the variable used to hold info about the suit of your
  214. #       card.  (In a standard deck of playing cards, the 2 of Spades and
  215. #       Ace of Spades both have a Value_holder value of "1", while the
  216. #       2 of Hearts and Ace of Hearts both have a value of "4".)
  217.  
  218.   Suit_holder = 43
  219.  
  220. # * Name_holder is the ID of the ACTOR in your database whose name is used
  221. #       to hold the name of your card (e.g. "the 7 of Diamonds").  The
  222. #       reason this is stored in a name is to make it easy to display the
  223. #       name of the chosen card in a message box.  Make sure you choose
  224. #       an Actor ID of an Actor that exists in your database, but that you
  225. #       are not using anywhere in your game.
  226.  
  227.   Name_holder = 10
  228.  
  229. # Leave the Removal option as 'true' if cards should be removed from the deck
  230. #       once they have been drawn.  (They can be added back at any time using
  231. #       "shuffle" or "addback" method calls.)  Change this option to 'false'
  232. #       if you want cards to remain in the deck even after they are drawn.
  233.  
  234.   Removal = true
  235.  
  236. ############################################
  237. #                                          #
  238. #             ADVANCED SETUP               #
  239. #                                          #
  240. ############################################
  241.  
  242. # If you've done the above correctly, this script will work properly for a
  243. #       standard 52-card deck of playing cards.  Everything from here on is
  244. #       entirely optional and is used to change the composition of your game's
  245. #       deck of cards or customize the system to your liking.
  246.  
  247. # Choose the number of cards that you want in your deck.
  248.  
  249.   Deck_size = 52
  250.  
  251. # Enter the names of suits in your deck.  The size of this array is
  252. #       not fixed at 4 - you can add or remove suits as you please.
  253.  
  254.   Suits = ["Spades","Clubs","Diamonds","Hearts"]
  255.  
  256. # Enter the names of values of cards in your deck, such as "Ace",
  257. #       "2", and "3".  The size of this array is not fixed at 13 -
  258. #       you can add or remove values as your please.
  259.  
  260.   Values = ["2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"]
  261.  
  262. # In some games, you may want to automatically convert the value of a card
  263. #       before you return it to the Value_holder variable.  Support for doing
  264. #       this in up to five games is given here, and you can add more if you
  265. #       want by modifying the draw_card method in my script.
  266.  
  267. # A good example of when you want to do this is to create a Blackjack game.
  268. #       I have set this up for you here using Conv_Value_3, so if you want
  269. #       to get the "Blackjack Value" of a card, simply pass in "3" as the
  270. #       first argument of the draw_card script call.
  271.  
  272. # IMPORTANT: Be sure that these arrays have the same length as your "Values"
  273. #       array above.
  274.  
  275.   Conv_Value_1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
  276.   Conv_Value_2 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
  277.   Conv_Value_3 = [2,3,4,5,6,7,8,9,10,10,10,10,11]
  278.   Conv_Value_4 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
  279.   Conv_Value_5 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
  280.  
  281. # Choose defaults for whether a card should be displayed to the player once
  282. #       it's chosen, how many seconds to display it for which Picture Number
  283. #       the card occupies, and the x and y coordinates to display the card at.
  284.  
  285. # If you choose to use the "show_card" method, these variables define the
  286. #       default Picture Number, x-coordinate, and y-coordinate that will be
  287. #       used if you don't explicitly define these values when calling the
  288. #       method.
  289.  
  290.   Picture_id = 21 # NOTE: This is "Number" on the 'Show Picture' event command.
  291.   Default_x = 197
  292.   Default_y = 103
  293.  
  294. # If you choose to use the "show_card" method to show pictures of the selected
  295. #       card, specify here how you will name the pictures.  If you don't change
  296. #       these options, you will want to name your cards "Cards_1.png",
  297. #       "Cards_2.png", etc.  Be sure your pictures are in the Graphics/Pictures
  298. #       folder.
  299.  
  300.   Pic_prefix = "Cards_"
  301.   Pic_extension = ".png" # NOTE: This should be ".png" or ".jpg"
  302.  
  303. # Design the template for naming your cards using the "namestring" variable.
  304. #       NOTE: If you don't understand what's going on here, I strongly advise
  305. #       leaving this option alone.  But if you do know what you're doing, you
  306. #       can use this method to hard-code display strings such as "A Joker" if
  307. #       a certain combination of value and suit is picked.
  308.  
  309.   def self.makename(value, suit)
  310.     namestring = ("the " + value + " of " + suit)
  311.     return namestring
  312.   end
  313.  
  314. end
  315.  
  316. ############################################
  317. #                                          #
  318. #               ICKY CODE!                 #
  319. #                                          #
  320. ############################################
  321.  
  322. # Everything from here on represents the inner workings of the script.
  323. #       Please don't alter anything from here on unless you are an
  324. #       advanced scripter yourself (in which case, have at it!)  
  325.  
  326. class Game_Party < Game_Unit
  327.  
  328.   #--------------------------------------------------------------------------
  329.   # * Public Instance Variables
  330.   #--------------------------------------------------------------------------
  331.   attr_reader   :deck                 # Array of Cards available for the Draw
  332.  
  333.   #--------------------------------------------------------------------------
  334.   # * Object Initialization
  335.   #--------------------------------------------------------------------------
  336.   alias :init_with_deck :initialize
  337.   def initialize
  338.     init_with_deck
  339.     shuffle
  340.   end
  341.  
  342.   #--------------------------------------------------------------------------
  343.   # * "Shuffle" the deck so that all cards are available for the Draw
  344.   #--------------------------------------------------------------------------
  345.   def shuffle
  346.     @deck = []
  347.     for i in 1..Cards::Deck_size
  348.       deck.push(i)
  349.     end
  350.   end
  351.  
  352.   #--------------------------------------------------------------------------
  353.   # * Add a specific card back into the array of Available Cards
  354.   #--------------------------------------------------------------------------
  355.   def add_back(card)
  356.     @deck.push(card)
  357.     @deck.sort!
  358.   end
  359.  
  360.   #--------------------------------------------------------------------------
  361.   # * Other acceptable ways to call "add_back"
  362.   #--------------------------------------------------------------------------
  363.   def addback(card)
  364.     add_back(card)
  365.   end
  366.   def add_card(card)
  367.     add_back(card)
  368.   end
  369.   def addcard(card)
  370.     add_back(card)
  371.   end
  372.  
  373.   #--------------------------------------------------------------------------
  374.   # * Remove a specific card from the array of Available Cards
  375.   #--------------------------------------------------------------------------
  376.   def pull_card(card)
  377.     @deck.delete(card)
  378.   end
  379.  
  380.   #--------------------------------------------------------------------------
  381.   # * Other acceptable ways to call "pull_card"
  382.   #--------------------------------------------------------------------------
  383.   def pullcard(card)
  384.     pull_card(card)
  385.   end
  386.   def remove_card(card)
  387.     pull_card(card)
  388.   end
  389.   def removecard(card)
  390.     pull_card(card)
  391.   end
  392.  
  393.   #--------------------------------------------------------------------------
  394.   # * Draw a Random Card out of cards that have not already been Drawn
  395.   #--------------------------------------------------------------------------
  396.   def draw_card(autoconv_type = 0)
  397.     if @deck.size <= 0 # A catch in case all cards have been drawn
  398.       $game_variables[Cards::Card_holder] = -999
  399.       $game_variables[Cards::Value_holder] = -999
  400.       $game_variables[Cards::Suit_holder] = -999
  401.       $game_actors[Cards::Name_holder].name = "OUT OF CARDS"
  402.       return
  403.     else
  404.       pick = (rand(@deck.size)).to_i
  405.       chosen = deck[pick]
  406.       if Cards::Removal == true
  407.         deck.delete_at(pick)
  408.       end
  409.     end
  410.     $game_variables[Cards::Card_holder] = chosen
  411.     $game_variables[Cards::Value_holder] = convert(chosen, 1)
  412.     $game_variables[Cards::Suit_holder] = convert(chosen, 2)
  413.     $game_actors[Cards::Name_holder].name = convert(chosen, 3)
  414.     divisor = (Cards::Suits).size
  415.     case autoconv_type
  416.     when 1
  417.       $game_variables[Cards::Value_holder] = Cards::Conv_Value_1[((chosen - 1) / divisor).to_i]
  418.     when 2
  419.       $game_variables[Cards::Value_holder] = Cards::Conv_Value_2[((chosen - 1) / divisor).to_i]
  420.     when 3
  421.       $game_variables[Cards::Value_holder] = Cards::Conv_Value_3[((chosen - 1) / divisor).to_i]
  422.     when 4
  423.       $game_variables[Cards::Value_holder] = Cards::Conv_Value_4[((chosen - 1) / divisor).to_i]
  424.     when 5
  425.       $game_variables[Cards::Value_holder] = Cards::Conv_Value_5[((chosen - 1) / divisor).to_i]
  426.     else
  427.     end
  428.   end
  429.  
  430.   #--------------------------------------------------------------------------
  431.   # * Provide a more concise way to call the Draw Card method
  432.   #--------------------------------------------------------------------------
  433.   def d_c(autoconv_type = 0)
  434.     draw_card(autoconv_type)
  435.   end
  436.   #--------------------------------------------------------------------------
  437.   # * Convert a Card Position into a Value, Type, or Name.
  438.   #--------------------------------------------------------------------------
  439.   def convert(chosen, type)
  440.     divisor = (Cards::Suits).size
  441.     value = Cards::Values[((chosen - 1) / divisor).to_i]
  442.     suit = Cards::Suits[((chosen - 1) % divisor)]
  443.     name = Cards.makename(value, suit)
  444.     case type
  445.     when 1
  446.       return ((((chosen - 1) / divisor).to_i) + 1)
  447.     when 2
  448.       return (((chosen - 1) % divisor) + 1)
  449.     when 3
  450.       return name
  451.     end
  452.   end
  453.  
  454.   #--------------------------------------------------------------------------
  455.   # * Display the Chosen Card onscreen.
  456.   #--------------------------------------------------------------------------
  457.   def show_card(pic_id = -999, x_coord = -999, y_coord = -999)
  458.     chosen = $game_variables[Cards::Card_holder]
  459.     pic_string = Cards::Pic_prefix + chosen.to_s + Cards::Pic_extension
  460.     if pic_id == -999; pic_id = Cards::Picture_id; end
  461.     if x_coord == -999; x_coord = Cards::Default_x; end
  462.     if y_coord == -999; y_coord = Cards::Default_y; end
  463.     unless chosen == -999
  464.       $game_map.screen.pictures[pic_id].show(pic_string, 0, x_coord, y_coord, 100, 100, 255, 0)
  465.     end
  466.   end
  467.  
  468.   #--------------------------------------------------------------------------
  469.   # * Provide a more concise way to call the Show Card method.
  470.   #--------------------------------------------------------------------------
  471.   def s_c(pic_id = -999, x_coord = -999, y_coord = -999)
  472.     show_card(pic_id, x_coord, y_coord)
  473.   end
  474.  
  475.   #--------------------------------------------------------------------------
  476.   # * Add a specific card back into the array of Available Cards
  477.   #--------------------------------------------------------------------------
  478.   def cards_remaining(slot = -999)
  479.     unless slot == -999
  480.       $game_variables[slot] = @deck.size
  481.     end
  482.     return @deck.size
  483.   end
  484.  
  485.   #--------------------------------------------------------------------------
  486.   # * Provide a more concise way to call the Cards Remaining method.
  487.   #--------------------------------------------------------------------------
  488.   def c_r(slot)
  489.     cards_remaining(slot)
  490.   end
  491.  
  492. end
Advertisement
Add Comment
Please, Sign In to add comment