neonblack

Lockpick Script V1.2

Apr 16th, 2012
537
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###--------------------------------------------------------------------------###
  2. #  Lockpick script                                                             #
  3. #  Version 1.2                                                                 #
  4. #                                                                              #
  5. #      Credits:                                                                #
  6. #  Original code by: Neonblack                                                 #
  7. #  Modified by: Jesse120                                                       #
  8. #                                                                              #
  9. #  This work is licensed under the Creative Commons Attribution-NonCommercial  #
  10. #  3.0 Unported License. To view a copy of this license, visit                 #
  11. #  http://creativecommons.org/licenses/by-nc/3.0/.                             #
  12. #  Permissions beyond the scope of this license are available at               #
  13. #  http://cphouseset.wordpress.com/liscense-and-terms-of-use/.                 #
  14. #                                                                              #
  15. #      Contact:                                                                #
  16. #  NeonBlack - neonblack23@live.com (e-mail) or "neonblack23" on skype         #
  17. ###--------------------------------------------------------------------------###
  18.  
  19. ###--------------------------------------------------------------------------###
  20. #      Revision information:                                                   #
  21. #  V.Alpha - 2.11.2012                                                         #
  22. #   Wrote main script                                                          #
  23. #  V1.0 - 2.12.2012                                                            #
  24. #   Debbugged, polished, and documented script                                 #
  25. #  V1.1 - 4.7.2012                                                             #
  26. #   Modified so that breaking a pick is dependent on an in-game switch         #
  27. #  V1.2 - 4.15.2012                                                            #
  28. #   Added "Gold Lockpick"                                                      #
  29. #   Added to Jesse's modification                                              #
  30. #   Added "breaking locks"                                                     #
  31. #   Slight cleanup and maintenance                                             #
  32. ###--------------------------------------------------------------------------###
  33.  
  34. ###--------------------------------------------------------------------------###
  35. #      Compatibility:                                                          #
  36. #  New Scene entirely; should run with just about everything.                  #
  37. ###--------------------------------------------------------------------------###
  38.  
  39. ###--------------------------------------------------------------------------###
  40. #      Instructions:                                                           #
  41. #  Place this script in the "Materials" section of the scripts above main.     #
  42. #  This script is NOT plug and play and requires additional graphics and       #
  43. #  several setting changes to work properly.  Please be sure you have          #
  44. #  imported the required graphics before continuing.                           #
  45. #                                                                              #
  46. #  To use, place a script call in an event and use the following script call:  #
  47. #    lockpick(x, y)                                                            #
  48. #  Define "x" as a numeric value that represents the difficulty of the lock    #
  49. #  where lower numbers are easier to pick.  It is recommended to only use      #
  50. #  values ranging form 2-8 because 1 seems to be too easy and 9 seems to be    #
  51. #  too hard.  The "y" argument is a little more complex.  "y" is simply the    #
  52. #  variable that holds the "lock durability".  If "y" is defined, breaking a   #
  53. #  pick will break the lock instead.  The durability of the lock will be       #
  54. #  stored in variable "y" so that the player cannot leave the lock and return  #
  55. #  with full durability.  If the variable contains a value of "0" when         #
  56. #  lockpicking starts, the variable will be changed to the default durability  #
  57. #  for picks.  A variable with the value -1 is considered a broken lock.       #
  58. #  Note that even with a gold lockpick or with lockpick breaking disabled,     #
  59. #  locks can still be broken.  To make a lock "unbreakable", set the variable  #
  60. #  to a value lower than -100 as -1 to -99 are used to check breaking.         #
  61. #                                                                              #
  62. #  When a lock is picked, one of three different numbers will be returned in   #
  63. #  pre-determined variable.  The numbers are as follows.                       #
  64. #    1 - Returned if the player picks the lock.                                #
  65. #    2 - Returned if the player cancels lockpicking.                           #
  66. #    3 - Returned if the player breaks all their picks or has none.            #
  67. #                                                                              #
  68. #  The player must have at least one of a pre-determined item in order to      #
  69. #  pick a lock.  The item is determined in the config section.                 #
  70. ###--------------------------------------------------------------------------###
  71.  
  72. ###--------------------------------------------------------------------------###
  73. #      Config:                                                                 #
  74. #  These are the default values used by several of the functions in the        #
  75. #  script.  You may change these values as you find your game requires in      #
  76. #  order to give the player a better playing experience based on your game.    #
  77. #                                                                              #
  78. module CP          #  Do not edit                                              #
  79. module LOCKPICK    #   these two lines                                         #
  80. #                                                                              #
  81. ###-----                                                                -----###
  82. # The main game settings are below.  These include mose of the sound effects   #
  83. # and variable settings in the script.                                         #
  84. #                                                                              #
  85. module SETTINGS  #  Do not edit this line                                      #
  86. #                                                                              #
  87. # The ID number of picks in the database.  The player must have a least 1      #
  88. # lockpick in their inventory before they can pick locks.                      #
  89. PICK_ITEM = 22 # Default = 22                                                  #
  90. #                                                                              #
  91. # These are the golden lockpick settings.  A golden lockpick will not break    #
  92. # and is used by default if it is in the inventory.  It can be diabled if you  #
  93. # do not want to use it.                                                       #
  94. USE_G_PICK = true # Default = true                                             #
  95. G_PICK_ITEM = 23  # Default = 23                                               #
  96. #                                                                              #
  97. # The variable that returns the result of lockpicking success.                 #
  98. VARIABLE = 1 # Default = 1                                                     #
  99. #                                                                              #
  100. # The sound effect, volume, and pitch played when starting to pick a lock.     #
  101. LOCK_SOUND = "Switch2" # Default = "Switch2"                                   #
  102. LOCK_VOLUME = 60       # Default = 60                                          #
  103. LOCK_PITCH = 110       # Default = 110                                         #
  104. #                                                                              #
  105. # The sound effect, volume, and pitch played when unlocking a lock.            #
  106. UNLOCK_SOUND = "Key" # Default = "Key"                                         #
  107. UNLOCK_VOLUME = 80   # Default = 80                                            #
  108. UNLOCK_PITCH = 100   # Default = 100                                           #
  109. #                                                                              #
  110. # The sound effect, volume, and pitch played when breaking a pick.             #
  111. BREAK_SOUND = "Sword2" # Default = "Sword2"                                    #
  112. BREAK_VOLUME = 60      # Default = 60                                          #
  113. BREAK_PITCH = 130      # Default = 130                                         #
  114. #                                                                              #
  115. # Determines the switch for breaking picks and the durability lockpicks have.  #
  116. # Higher values in durability will take longer to break.  If the switch is     #
  117. # turned on, picks can be broken.  If "BREAK_PICKS" is set to true, the        #
  118. # switch is turned on by default.                                              #
  119. BREAK_PICK_SWITCH = 1 # Default = 1                                            #
  120. BREAK_PICKS = true    # Default = true                                         #
  121. DURABILITY = 90       # Default = 90                                           #
  122. #                                                                              #
  123. # A dialog box can be drawn in the bottom left corner with the remaining       #
  124. # picks in your possession.  Setting this to false will not show that box.     #
  125. # You may also set the text to be shown in the box.                            #
  126. SHOW_REMAINING = true    # Default = true                                      #
  127. ITEM_NAME = "Lockpicks:" # Default = "Lockpicks:"                              #
  128. #                                                                              #
  129. end  #  Don't edit this line either.                                           #
  130. #                                                                              #
  131. ###-----                                                                -----###
  132. # The settings for the graphics are below.  Use these to specify the graphics  #
  133. # used and the X and Y offsets from the middle of the screen.  Note that all   #
  134. # graphics must exist in the "pictures" folder.                                #
  135. #                                                                              #
  136. module LOCK  #  Don't touch this line                                          #
  137. #                                                                              #
  138. # Settings for the lock graphic                                                #
  139. X_OFFSET = 0     # Default = 0                                                 #
  140. Y_OFFSET = 0     # Default = 0                                                 #
  141. GRAPHIC = "Lock" # Default = "Lock"                                            #
  142. #                                                                              #
  143. end          #  Don't touch                                                    #
  144. module PICK  #  these two lines                                                #
  145. #                                                                              #
  146. # Settings for the pick graphic.                                               #
  147. X_OFFSET = 0     # Default = 0                                                 #
  148. Y_OFFSET = 30    # Default = 0                                                 #
  149. GRAPHIC = "Pick" # Default = "Pick"                                            #
  150. #                                                                              #
  151. end          #  Don't touch                                                    #
  152. module KEY   #  these two lines                                                #
  153. #                                                                              #
  154. # Settings for the "key" graphic.                                              #
  155. X_OFFSET = 0    # Default = 0                                                  #
  156. Y_OFFSET = -20  # Default = 0                                                  #
  157. GRAPHIC = "Key" # Default = "Key"                                              #
  158. #                                                                              #
  159. end  #  Don't touch                                                            #
  160. end  #  these three                                                            #
  161. end  #  lines                                                                  #
  162. ###--------------------------------------------------------------------------###
  163.  
  164.  
  165. ###--------------------------------------------------------------------------###
  166. #  The following lines are the actual core code of the script.  While you are  #
  167. #  certainly invited to look, modifying it may result in undesirable results.  #
  168. #  Modify at your own risk!                                                    #
  169. ###--------------------------------------------------------------------------###
  170.  
  171. $imported = {} if $imported == nil
  172. $imported["CP_LOCKPICK"] = true
  173.  
  174. ##-----
  175. ## Allows for ease of access to the script from an event.
  176. ##-----
  177. class Game_Interpreter
  178.   def lockpick(diffi, door = nil)
  179.     $scene = Scene_Lockpick.new(diffi, door)
  180.     @wait_count = 1
  181.   end
  182. end
  183.  
  184. ##-----
  185. ## Draws the window for the number of remaining picks.
  186. ##-----
  187. class Window_Picks < Window_Base
  188.   def initialize
  189.     super(0, 360, 160, WLH + 32)
  190.     refresh
  191.   end
  192.  
  193.   def draw_picks(value, x, y, width)
  194.     pick_name = CP::LOCKPICK::SETTINGS::ITEM_NAME
  195.     cx = contents.text_size(pick_name).width
  196.     self.contents.font.color = normal_color
  197.     self.contents.draw_text(x+cx+2, y, width-cx-2, WLH, value)
  198.     self.contents.font.color = system_color
  199.     self.contents.draw_text(x, y, width, WLH, pick_name)
  200.   end
  201.  
  202.   def refresh
  203.     self.contents.clear
  204.     itemnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  205.     draw_picks($game_party.item_number($data_items[itemnum]), 4, 0, 120)
  206.   end
  207. end
  208.  
  209.  
  210. ##-----
  211. ## The bread and butter of the script.
  212. ##-----
  213. class Scene_Lockpick < Scene_Base
  214.  
  215.   ##-----
  216.   ## Initialize scene.  Sets difficulty and number of picks available.
  217.   ##-----
  218.   def initialize(diffi, door = nil)
  219.     @diffi = diffi
  220.     @door = nil
  221.     @door = $game_variables[door] unless door == nil
  222.     @doorvar = door
  223.     @door = CP::LOCKPICK::SETTINGS::DURABILITY if @door == 0
  224.     @key_rotation = 0
  225.     @pick_rotation = 90
  226.     @zone = rand(90) * 2
  227.     @wobble = 0
  228.     @durability = CP::LOCKPICK::SETTINGS::DURABILITY
  229.     @did_turn = false
  230.    
  231.     picksnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  232.     gpicknum = CP::LOCKPICK::SETTINGS::G_PICK_ITEM
  233.     usegp = CP::LOCKPICK::SETTINGS::USE_G_PICK
  234.     @haspicks = true if $game_party.has_item?($data_items[picksnum])
  235.     @haspicks = true if $game_party.has_item?($data_items[gpicknum]) and usegp
  236.     @haspicks = false if @door == -1
  237.   end
  238.  
  239.   ##-----
  240.   ## Start scene.  Draws items on screen.
  241.   ##-----
  242.   def start
  243.     super
  244.     create_menu_background
  245.     @picks_window = Window_Picks.new if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  246.     @picks_window.z = 4 if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  247.     create_lock
  248.     create_key
  249.     create_pick if @haspicks
  250.     key_math
  251.   end
  252.  
  253.   ##-----
  254.   ## Terminate scene.  Removes drawn items.
  255.   ##-----
  256.   def terminate
  257.     super
  258.     dispose_menu_background
  259.     @picks_window.dispose if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  260.     @lock_sprite.dispose
  261.     @key_sprite.dispose
  262.     @pick_sprite.dispose if @haspicks
  263.   end
  264.  
  265.   ##-----
  266.   ## Update scene.  Calls the scene's flesh and blood.
  267.   ##-----
  268.   def update
  269.     super
  270.     update_pick_command
  271.     update_key_position
  272.     update_pick_position if @haspicks
  273.   end
  274.  
  275.   ##-----
  276.   ## Draws the lock at the start of the scene.
  277.   ##-----
  278.   def create_lock
  279.     @lock_sprite = Sprite.new(@viewport1)
  280.     @lock_sprite.bitmap = Cache.picture(CP::LOCKPICK::LOCK::GRAPHIC)
  281.     @lock_sprite.ox = @lock_sprite.width/2
  282.     @lock_sprite.oy = @lock_sprite.height/2
  283.     @lock_sprite.x = Graphics.width/2 + CP::LOCKPICK::LOCK::X_OFFSET
  284.     @lock_sprite.y = Graphics.height/2 + CP::LOCKPICK::LOCK::Y_OFFSET
  285.     @lock_sprite.z = 1
  286.   end
  287.  
  288.   ##-----
  289.   ## Draws the key at the start of the scene.
  290.   ##-----
  291.   def create_key
  292.     @key_sprite = Sprite.new(@viewport1)
  293.     @key_sprite.bitmap = Cache.picture(CP::LOCKPICK::KEY::GRAPHIC)
  294.     @key_sprite.ox = @key_sprite.width/2
  295.     @key_sprite.oy = @key_sprite.height/2
  296.     @key_sprite.x = Graphics.width/2 + CP::LOCKPICK::KEY::X_OFFSET
  297.     @key_sprite.y = Graphics.height/2 + CP::LOCKPICK::KEY::Y_OFFSET
  298.     @key_sprite.z = 3
  299.     @k_rotate = @key_rotation
  300.     @key_sprite.angle = @k_rotate * -1
  301.   end
  302.  
  303.   ##-----
  304.   ## Updates the rotation of the key each frame.
  305.   ##-----
  306.   def update_key_position
  307.     return if @key_rotation == @k_rotate
  308.     @k_rotate = @key_rotation
  309.     @key_sprite.angle = @k_rotate * -1
  310.   end
  311.  
  312.   ##-----
  313.   ## Draws the pick at the start of the scene.
  314.   ##-----
  315.   def create_pick
  316.     @pick_sprite = Sprite.new(@viewport1)
  317.     @pick_sprite.bitmap = Cache.picture(CP::LOCKPICK::PICK::GRAPHIC)
  318.     @pick_sprite.ox = @pick_sprite.width/2
  319.     @pick_sprite.oy = @pick_sprite.width/2
  320.     @pick_sprite.x = Graphics.width/2 + CP::LOCKPICK::PICK::X_OFFSET
  321.     @pick_sprite.y = Graphics.height/2 + CP::LOCKPICK::PICK::Y_OFFSET
  322.     @pick_sprite.z = 2
  323.     @p_rotate = @pick_rotation
  324.     @pick_sprite.angle = @p_rotate - 90
  325.   end
  326.  
  327.   ##-----
  328.   ## Updates the rotation of the pick each frame.
  329.   ##-----
  330.   def update_pick_position
  331.     return if @pick_rotation == @p_rotate and @wobble == @shake
  332.     @p_rotate = @pick_rotation
  333.     @shake = @wobble
  334.     @pick_sprite.angle = @p_rotate - 90 + @shake
  335.   end
  336.  
  337.   ##-----
  338.   ## Quick thing to make the wait method work I guess.
  339.   ##-----
  340.   def update_basic
  341.     Graphics.update
  342.     Input.update
  343.   end
  344.  
  345.   ##-----
  346.   ## Aforementioned wait method.
  347.   ##-----
  348.   def wait(dur)
  349.     for i in 0...dur
  350.       update_basic
  351.     end
  352.   end
  353.  
  354.   ##-----
  355.   ## Method called when lock is picked successfully.
  356.   ##-----
  357.   def lock_picked
  358.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  359.     $game_variables[@doorvar] = @door unless @door == nil
  360.     $game_variables[variable] = 1
  361.     update_key_position
  362.     wait(20)
  363.     picking_end
  364.   end
  365.  
  366.   ##-----
  367.   ## Method called when lockpicking is cancelled.
  368.   ##-----
  369.   def lock_stopped
  370.     Sound.play_cancel
  371.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  372.     $game_variables[@doorvar] = @door unless @door == nil
  373.     $game_variables[variable] = 2
  374.     picking_end
  375.   end
  376.  
  377.   ##-----
  378.   ## Method called when lockpicking is failed.
  379.   ##-----
  380.   def no_picks
  381.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  382.     $game_variables[@doorvar] = @door unless @door == nil
  383.     $game_variables[variable] = 3
  384.     picking_end
  385.   end
  386.  
  387.   ##-----
  388.   ## End method that returns to the map.
  389.   ##-----
  390.   def picking_end
  391.     $scene = Scene_Map.new
  392.   end
  393.  
  394.   ##-----
  395.   ## Method containing all the key commands and input.
  396.   ##-----
  397.   def update_pick_command
  398.     if Input.trigger?(Input::B) ##----- Cancel
  399.       lock_stopped
  400.     elsif Input.trigger?(Input::C) ##----- Key turning input
  401.       @did_turn = true
  402.       if @haspicks
  403.         lsnd = CP::LOCKPICK::SETTINGS::LOCK_SOUND
  404.         lvol = CP::LOCKPICK::SETTINGS::LOCK_VOLUME
  405.         lpit = CP::LOCKPICK::SETTINGS::LOCK_PITCH
  406.         Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  407.       else
  408.         no_picks
  409.       end
  410.     elsif Input.press?(Input::C) and @did_turn
  411.       unless @key_rotation > @max_turn - 2
  412.         @key_rotation += 2
  413.       else
  414.         pick_dura
  415.       end
  416.       if @key_rotation == 90
  417.         lsnd = CP::LOCKPICK::SETTINGS::UNLOCK_SOUND
  418.         lvol = CP::LOCKPICK::SETTINGS::UNLOCK_VOLUME
  419.         lpit = CP::LOCKPICK::SETTINGS::UNLOCK_PITCH
  420.         Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  421.         lock_picked
  422.       end
  423.     else ##----- Lockpick movement below
  424.       @wobble = 0 unless @wobble == 0
  425.       @key_rotation -= 2 unless @key_rotation == 0
  426.       @key_rotation = 0 if @key_rotation < 0
  427.       if Input.press?(Input::RIGHT)
  428.         @pick_rotation += 2 unless @pick_rotation == 180
  429.         key_math
  430.       elsif Input.press?(Input::LEFT)
  431.         @pick_rotation -= 2 unless @pick_rotation == 0
  432.         key_math
  433.       end
  434.     end
  435.   end
  436.  
  437.   ##-----
  438.   ## Calculates the math allowing the key to turn.
  439.   ##-----
  440.   def key_math
  441.     if ((@zone-4)..(@zone+4)) === @pick_rotation
  442.       @max_turn = 90
  443.     else
  444.       check_spot = @pick_rotation - @zone
  445.       check_spot *= -1 if check_spot < 0
  446.       check_spot -= 4
  447.       check_spot *= @diffi
  448.       @max_turn = 90 - check_spot
  449.       @max_turn = 0 if @max_turn < 0
  450.     end
  451.   end
  452.  
  453.   ##-----
  454.   ## Checks the pick's durability with each step.
  455.   ##-----
  456.   def pick_dura
  457.     @wobble = rand(5) - 2
  458.     if @door != nil
  459.       @door -= @diffi
  460.       snap_pick if @door < 1 and @door > -100
  461.     elsif $game_switches[CP::LOCKPICK::SETTINGS::BREAK_PICK_SWITCH]
  462.       gpicknum = CP::LOCKPICK::SETTINGS::G_PICK_ITEM
  463.       usegp = CP::LOCKPICK::SETTINGS::USE_G_PICK
  464.       unless $game_party.has_item?($data_items[gpicknum]) and usegp
  465.         @durability -= @diffi
  466.         snap_pick if @durability < 1
  467.       end
  468.     end
  469.   end
  470.  
  471.   ##-----
  472.   ## Snaps the pick if durability is 0 or lower.
  473.   ##-----
  474.   def snap_pick
  475.     lsnd = CP::LOCKPICK::SETTINGS::BREAK_SOUND
  476.     lvol = CP::LOCKPICK::SETTINGS::BREAK_VOLUME
  477.     lpit = CP::LOCKPICK::SETTINGS::BREAK_PITCH
  478.     Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  479.     for i in 0...5
  480.       @pick_sprite.y += 3
  481.       update_basic
  482.     end
  483.     wait(10)
  484.     unless @door == nil
  485.       @door = -1 if @door < 1
  486.       return no_picks
  487.     end
  488.     change_pick
  489.   end
  490.  
  491.   ##-----
  492.   ## Removes a pick and prepares to change it.
  493.   ##-----
  494.   def change_pick
  495.     itemnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  496.     $game_party.lose_item($data_items[itemnum], 1)
  497.     @picks_window.refresh if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  498.     unless $game_party.has_item?($data_items[itemnum]) and @door != -1
  499.       no_picks
  500.     else
  501.       new_pick
  502.     end
  503.   end
  504.  
  505.   ##-----
  506.   ## Places a new pick if one is present.
  507.   ##-----
  508.   def new_pick
  509.     @key_rotation = 0
  510.     @pick_rotation = 90
  511.     @wobble = 0
  512.     @durability = CP::LOCKPICK::SETTINGS::DURABILITY
  513.     @pick_sprite.dispose
  514.     create_pick
  515.     update_key_position
  516.     wait(10)
  517.   end
  518.  
  519. end
  520.  
  521. class Scene_Title < Scene_Base
  522.   alias cp_lp_create_game_objects create_game_objects unless $@
  523.   def create_game_objects
  524.     cp_lp_create_game_objects
  525.     onoroff = CP::LOCKPICK::SETTINGS::BREAK_PICKS
  526.     $game_switches[CP::LOCKPICK::SETTINGS::BREAK_PICK_SWITCH] = onoroff
  527.   end
  528. end
  529.  
  530. ##----------------------------------------------------------------------------##
  531. ##  END OF SCRIPT                                                             ##
  532. ##----------------------------------------------------------------------------##
RAW Paste Data