neonblack

Lockpick Script V1.1 ACE

Sep 11th, 2012
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##----------------------------------------------------------------------------##
  2. ## Lockpicking Script v1.1
  3. ## Created by Neon Black
  4. ##
  5. ## For both commercial and non-commercial use as long as credit is given to
  6. ## Neon Black and any additional authors.  Licensed under Creative Commons
  7. ## CC BY 3.0 - http://creativecommons.org/licenses/by/3.0/.
  8. ##----------------------------------------------------------------------------##
  9.                                                                               ##
  10. ##----------------------------------------------------------------------------##
  11. ##    Revision Info:
  12. ## v1.1 - 3.3.2013
  13. ##  Cleanup and debug for official Ace release
  14. ## v1.0 - 9.11.2012
  15. ##  Converted from VX script
  16. ##----------------------------------------------------------------------------##
  17.                                                                               ##
  18. $imported ||= {}                                                              ##
  19. $imported["CP_LOCKPICK"] = 1.1                                                ##
  20.                                                                               ##
  21. ##----------------------------------------------------------------------------##
  22. ##     VXA Notes:
  23. ## This is the VXA version of my lockpicking script ported from VX.  There were
  24. ## a few modifications made to allow it to run properly, however the script
  25. ## remains mostly unchanged.
  26. ##
  27. ##     Instructions:
  28. ## Place this script in the "Materials" section of the scripts above main.
  29. ## This script is NOT plug and play and requires additional graphics and
  30. ## several setting changes to work properly.  Please be sure you have imported
  31. ## the required graphics before continuing.
  32. ##
  33. ## To use, place a script call in an event and use the following script call:
  34. ##
  35. ##   Lockpick.start(x[, y])
  36. ## Define "x" as a numeric value that represents the difficulty of the lock
  37. ## where lower numbers are easier to pick.  It is recommended to only use
  38. ## values ranging form 2-8 because 1 seems to be too easy and 9 seems to be too
  39. ## hard.  The "y" argument is a little more complex but is completely optional.
  40. ## "y" is simply the variable that holds the "lock durability".  If "y" is
  41. ## defined, breaking a pick will break the lock instead.  The durability of the
  42. ## lock will be stored in variable "y" so that the player cannot leave the lock
  43. ## and return with full durability.  If the variable contains a value of "0"
  44. ## when lockpicking starts, the variable will be changed to the default
  45. ## durability for picks.  A variable with the value -1 is considered a broken
  46. ## lock.  Note that even with a gold lockpick or with lockpick breaking
  47. ## disabled, locks can still be broken.  To make a lock "unbreakable", set the
  48. ## variable to a value lower than -100 as -1 to -99 are used to check breaking
  49. ##
  50. ## When a lock is picked, one of three different numbers will be returned in
  51. ## pre-determined variable.  The numbers are as follows.
  52. ##   1 - Returned if the player picks the lock.
  53. ##   2 - Returned if the player cancels lockpicking.
  54. ##   3 - Returned if the player breaks all their picks or has none.
  55. ##
  56. ## The player must have at least one of a pre-determined item in order to pick
  57. ## a lock.  The item is determined in the config section.
  58. ##----------------------------------------------------------------------------##
  59.                                                                               ##
  60. module CP       # Do not touch                                                ##
  61. module LOCKPICK #  these lines.                                               ##
  62.                                                                               ##
  63. ##----------------------------------------------------------------------------##
  64. ##    Config:
  65. ## The config options are below.  You can set these depending on the flavour of
  66. ## your game.  Each option is explained in a bit more detail above it.
  67. ##
  68. ##------
  69. # The main game settings are below.  These include mose of the sound effects and
  70. # variable settings in the script.
  71. module SETTINGS
  72.  
  73. # The ID number of picks in the database.  The player must have a least 1
  74. # lockpick in their inventory before they can pick locks.
  75. PICK_ITEM = 22
  76.  
  77. # These are the golden lockpick settings.  A golden lockpick will not break and
  78. # is used by default if it is in the inventory.  It can be diabled if you do not
  79. # want to use it.
  80. USE_G_PICK = false
  81. G_PICK_ITEM = 23
  82.  
  83. # The variable that returns the result of lockpicking success.
  84. VARIABLE = 1
  85.  
  86. # The sound effect, volume, and pitch played when starting to pick a lock.
  87. LOCK_SOUND = "Switch2"
  88. LOCK_VOLUME = 60
  89. LOCK_PITCH = 110
  90.  
  91. # The sound effect, volume, and pitch played when unlocking a lock.
  92. UNLOCK_SOUND = "Key"
  93. UNLOCK_VOLUME = 80
  94. UNLOCK_PITCH = 100
  95.  
  96. # The sound effect, volume, and pitch played when breaking a pick.
  97. BREAK_SOUND = "Sword2"
  98. BREAK_VOLUME = 60
  99. BREAK_PITCH = 130
  100.  
  101. # Determines the switch for breaking picks and the durability lockpicks have.
  102. # Higher values in durability will take longer to break.  If the switch is
  103. # turned on, picks can be broken.  If "BREAK_PICKS" is set to true, the switch
  104. # is turned on by default.
  105. BREAK_PICK_SWITCH = 1
  106. BREAK_PICKS = true
  107. DURABILITY = 90
  108.  
  109. # A dialog box can be drawn in the bottom left corner with the remaining picks
  110. # in your possession.  Setting this to false will not show that box.  You may
  111. # also set the text to be shown in the box.
  112. SHOW_REMAINING = true
  113. ITEM_NAME = "Lockpicks:"
  114.  
  115. end
  116. ##------
  117. # The settings for the graphics are below.  Use these to specify the graphics
  118. # used and the X and Y offsets from the middle of the screen.  Note that all
  119. # graphics must exist in the "pictures" folder.
  120. module LOCK
  121.  
  122. # Settings for the lock graphic.
  123. X_OFFSET = 0
  124. Y_OFFSET = 0
  125. GRAPHIC = "Lock"
  126.  
  127. end
  128. module PICK
  129.  
  130. # Settings for the pick graphic.
  131. X_OFFSET = 0
  132. Y_OFFSET = 30
  133. GRAPHIC = "Pick"
  134.  
  135. end
  136. module KEY
  137.  
  138. # Settings for the "key" graphic.
  139. X_OFFSET = 0
  140. Y_OFFSET = -20
  141. GRAPHIC = "Key"
  142.  
  143. end
  144. ##----------------------------------------------------------------------------##
  145.                                                                               ##
  146.                                                                               ##
  147. ##----------------------------------------------------------------------------##
  148. ## The following lines are the actual core code of the script.  While you are
  149. ## certainly invited to look, modifying it may result in undesirable results.
  150. ## Modify at your own risk!
  151. ###----------------------------------------------------------------------------
  152.  
  153.  
  154. end
  155. end
  156.  
  157. class Window_Picks < Window_Base
  158.   def initialize
  159.     super(0, Graphics.height - fitting_height(1), 160, fitting_height(1))
  160.     refresh
  161.   end
  162.  
  163.   def draw_picks(value, x, y, width)
  164.     pick_name = CP::LOCKPICK::SETTINGS::ITEM_NAME
  165.     cx = contents.text_size(pick_name).width
  166.     self.contents.font.color = normal_color
  167.     self.contents.draw_text(x+cx+2, y, width-cx-2, 24, value)
  168.     self.contents.font.color = system_color
  169.     self.contents.draw_text(x, y, width, 24, pick_name)
  170.   end
  171.  
  172.   def refresh
  173.     self.contents.clear
  174.     itemnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  175.     draw_picks($game_party.item_number($data_items[itemnum]), 4, 0,
  176.                contents.width - 8)
  177.   end
  178. end
  179.  
  180. class Lockpick < Scene_MenuBase
  181.   def self.start(diffi, door_var = nil)
  182.     SceneManager.call(Lockpick)
  183.     SceneManager.scene.prepare(diffi, door_var)
  184.     Fiber.yield
  185.   end
  186.  
  187.   def prepare(diffi, door_var)
  188.     @diffi = diffi
  189.     @door = $game_variables[door_var] unless door_var.nil?
  190.     @doorvar = door_var
  191.     @door = CP::LOCKPICK::SETTINGS::DURABILITY if @door == 0
  192.     @key_rotation = 0
  193.     @pick_rotation = 90
  194.     @zone = rand(90) * 2
  195.     @wobble = 0
  196.     @durability = CP::LOCKPICK::SETTINGS::DURABILITY
  197.     @did_turn = false
  198.    
  199.     picksnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  200.     gpicknum = CP::LOCKPICK::SETTINGS::G_PICK_ITEM
  201.     usegp = CP::LOCKPICK::SETTINGS::USE_G_PICK
  202.     @haspicks = true if $game_party.has_item?($data_items[picksnum])
  203.     @haspicks = true if $game_party.has_item?($data_items[gpicknum]) and usegp
  204.     @haspicks = false if @door == -1
  205.   end
  206.  
  207.   def start ## Start scene.  Draws items on screen.
  208.     super
  209.     @picks_window = Window_Picks.new if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  210.     @picks_window.z = 4 if @picks_window
  211.     create_lock
  212.     create_key
  213.     create_pick if @haspicks
  214.     key_math
  215.   end
  216.  
  217.   def terminate
  218.     super
  219.     @lock_sprite.dispose
  220.     @key_sprite.dispose
  221.     @pick_sprite.dispose if @pick_sprite
  222.   end
  223.  
  224.   def update
  225.     super
  226.     update_pick_command
  227.     update_key_position
  228.     update_pick_position if @haspicks
  229.   end
  230.  
  231.   def create_lock
  232.     @lock_sprite = Sprite.new
  233.     @lock_sprite.bitmap = Cache.picture(CP::LOCKPICK::LOCK::GRAPHIC)
  234.     @lock_sprite.ox = @lock_sprite.width/2
  235.     @lock_sprite.oy = @lock_sprite.height/2
  236.     @lock_sprite.x = Graphics.width/2 + CP::LOCKPICK::LOCK::X_OFFSET
  237.     @lock_sprite.y = Graphics.height/2 + CP::LOCKPICK::LOCK::Y_OFFSET
  238.     @lock_sprite.z = 1
  239.   end
  240.  
  241.   def create_key
  242.     @key_sprite = Sprite.new
  243.     @key_sprite.bitmap = Cache.picture(CP::LOCKPICK::KEY::GRAPHIC)
  244.     @key_sprite.ox = @key_sprite.width/2
  245.     @key_sprite.oy = @key_sprite.height/2
  246.     @key_sprite.x = Graphics.width/2 + CP::LOCKPICK::KEY::X_OFFSET
  247.     @key_sprite.y = Graphics.height/2 + CP::LOCKPICK::KEY::Y_OFFSET
  248.     @key_sprite.z = 3
  249.     @k_rotate = @key_rotation
  250.     @key_sprite.angle = @k_rotate * -1
  251.   end
  252.  
  253.   def update_key_position
  254.     return if @key_rotation == @k_rotate
  255.     @k_rotate = @key_rotation
  256.     @key_sprite.angle = @k_rotate * -1
  257.   end
  258.  
  259.   def create_pick
  260.     @pick_sprite = Sprite.new
  261.     @pick_sprite.bitmap = Cache.picture(CP::LOCKPICK::PICK::GRAPHIC)
  262.     @pick_sprite.ox = @pick_sprite.width/2
  263.     @pick_sprite.oy = @pick_sprite.width/2
  264.     @pick_sprite.x = Graphics.width/2 + CP::LOCKPICK::PICK::X_OFFSET
  265.     @pick_sprite.y = Graphics.height/2 + CP::LOCKPICK::PICK::Y_OFFSET
  266.     @pick_sprite.z = 2
  267.     @p_rotate = @pick_rotation
  268.     @pick_sprite.angle = @p_rotate - 90
  269.   end
  270.  
  271.   def update_pick_position
  272.     return if @pick_rotation == @p_rotate and @wobble == @shake
  273.     @p_rotate = @pick_rotation
  274.     @shake = @wobble
  275.     @pick_sprite.angle = @p_rotate - 90 + @shake
  276.   end
  277.    
  278.   def wait(dur)
  279.     for i in 0...dur
  280.       update_basic
  281.     end
  282.   end
  283.  
  284.   def lock_picked
  285.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  286.     $game_variables[@doorvar] = @door unless @door == nil
  287.     $game_variables[variable] = 1
  288.     update_key_position
  289.     wait(20)
  290.     picking_end
  291.   end
  292.  
  293.   def lock_stopped
  294.     Sound.play_cancel
  295.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  296.     $game_variables[@doorvar] = @door unless @door == nil
  297.     $game_variables[variable] = 2
  298.     picking_end
  299.   end
  300.  
  301.   def no_picks
  302.     variable = CP::LOCKPICK::SETTINGS::VARIABLE
  303.     $game_variables[@doorvar] = @door unless @door == nil
  304.     $game_variables[variable] = 3
  305.     picking_end
  306.   end
  307.  
  308.   def picking_end
  309.     SceneManager.return
  310.   end
  311.  
  312.   def update_pick_command
  313.     if Input.trigger?(:B) ##----- Cancel
  314.       lock_stopped
  315.     elsif Input.trigger?(:C) ##----- Key turning input
  316.       @did_turn = true
  317.       if @haspicks
  318.         lsnd = CP::LOCKPICK::SETTINGS::LOCK_SOUND
  319.         lvol = CP::LOCKPICK::SETTINGS::LOCK_VOLUME
  320.         lpit = CP::LOCKPICK::SETTINGS::LOCK_PITCH
  321.         Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  322.       else
  323.         no_picks
  324.       end
  325.     elsif Input.press?(:C) and @did_turn
  326.       unless @key_rotation > @max_turn - 2
  327.         @key_rotation += 2
  328.       else
  329.         pick_dura
  330.       end
  331.       if @key_rotation == 90
  332.         lsnd = CP::LOCKPICK::SETTINGS::UNLOCK_SOUND
  333.         lvol = CP::LOCKPICK::SETTINGS::UNLOCK_VOLUME
  334.         lpit = CP::LOCKPICK::SETTINGS::UNLOCK_PITCH
  335.         Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  336.         lock_picked
  337.       end
  338.     else ##----- Lockpick movement below
  339.       @wobble = 0 unless @wobble == 0
  340.       @key_rotation -= 2 unless @key_rotation == 0
  341.       @key_rotation = 0 if @key_rotation < 0
  342.       if Input.press?(:RIGHT)
  343.         @pick_rotation += 2 unless @pick_rotation == 180
  344.         key_math
  345.       elsif Input.press?(:LEFT)
  346.         @pick_rotation -= 2 unless @pick_rotation == 0
  347.         key_math
  348.       end
  349.     end
  350.   end
  351.  
  352.   def key_math
  353.     if ((@zone-4)..(@zone+4)) === @pick_rotation
  354.       @max_turn = 90
  355.     else
  356.       check_spot = @pick_rotation - @zone
  357.       check_spot *= -1 if check_spot < 0
  358.       check_spot -= 4
  359.       check_spot *= @diffi
  360.       @max_turn = 90 - check_spot
  361.       @max_turn = 0 if @max_turn < 0
  362.     end
  363.   end
  364.  
  365.   def pick_dura  ## Checks the pick's durability with each step.
  366.     @wobble = rand(5) - 2
  367.     if @door != nil
  368.       @door -= @diffi
  369.       snap_pick if @door < 1 and @door > -100
  370.     elsif $game_switches[CP::LOCKPICK::SETTINGS::BREAK_PICK_SWITCH]
  371.       gpicknum = CP::LOCKPICK::SETTINGS::G_PICK_ITEM
  372.       usegp = CP::LOCKPICK::SETTINGS::USE_G_PICK
  373.       unless $game_party.has_item?($data_items[gpicknum]) and usegp
  374.         @durability -= @diffi
  375.         snap_pick if @durability < 1
  376.       end
  377.     end
  378.   end
  379.  
  380.   def snap_pick  ## Snaps the pick if durability is 0 or lower.
  381.     lsnd = CP::LOCKPICK::SETTINGS::BREAK_SOUND
  382.     lvol = CP::LOCKPICK::SETTINGS::BREAK_VOLUME
  383.     lpit = CP::LOCKPICK::SETTINGS::BREAK_PITCH
  384.     Audio.se_play("Audio/SE/" + lsnd, lvol, lpit)
  385.     for i in 0...5
  386.       @pick_sprite.y += 3
  387.       update_basic
  388.     end
  389.     wait(10)
  390.     unless @door == nil
  391.       @door = -1 if @door < 1
  392.       return no_picks
  393.     end
  394.     change_pick
  395.   end
  396.  
  397.   def change_pick  ## Removes a pick and prepares to change it.
  398.     itemnum = CP::LOCKPICK::SETTINGS::PICK_ITEM
  399.     $game_party.lose_item($data_items[itemnum], 1)
  400.     @picks_window.refresh if CP::LOCKPICK::SETTINGS::SHOW_REMAINING
  401.     unless $game_party.has_item?($data_items[itemnum]) and @door != -1
  402.       no_picks
  403.     else
  404.       new_pick
  405.     end
  406.   end
  407.  
  408.   def new_pick  ## Places a new pick if one is present.
  409.     @key_rotation = 0
  410.     @pick_rotation = 90
  411.     @wobble = 0
  412.     @durability = CP::LOCKPICK::SETTINGS::DURABILITY
  413.     @pick_sprite.dispose
  414.     create_pick
  415.     update_key_position
  416.     wait(10)
  417.   end
  418. end
  419.  
  420. module DataManager
  421.   class << self
  422.     alias :cp_lockpick_cgo :create_game_objects
  423.   end
  424.  
  425.   def self.create_game_objects
  426.     cp_lockpick_cgo
  427.     onoroff = CP::LOCKPICK::SETTINGS::BREAK_PICKS
  428.     $game_switches[CP::LOCKPICK::SETTINGS::BREAK_PICK_SWITCH] = onoroff
  429.   end
  430. end
  431.  
  432.  
  433. ###--------------------------------------------------------------------------###
  434. #  End of script.                                                              #
  435. ###--------------------------------------------------------------------------###
RAW Paste Data