Kakakadafi

Kadafi - Checkpoint System

Oct 20th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.47 KB | None | 0 0
  1. #==============================================================================
  2. # Kadafi - Checkpoint System
  3. # Version : 1.0
  4. # Contact : http://forums.rpgmakerweb.com/index.php?/user/33654-kadafi/
  5. #==============================================================================
  6. ($imported ||= {})[:Kadafi_CheckpointSystem] = true
  7. #==============================================================================
  8. # CHANGE LOGS:
  9. # -----------------------------------------------------------------------------
  10. # 2017.10.21 - Finished script
  11. #==============================================================================
  12. =begin
  13.  
  14.   Introduction :
  15.   This script create a checkpoint system.
  16.  
  17.   How to Use :
  18.   Edit the configuration below.
  19.  
  20.   Script Call :
  21.   DataManager.save_checkpoint     # Save Checkpoint
  22.   DataManager.delete_checkpoint   # Delete Checkpoint
  23.  
  24.   Terms of Use :
  25.   1. Credit me as Kadafi (Optional)
  26.  
  27. =end
  28. #==============================================================================
  29. # Configuration
  30. #==============================================================================
  31. module Kadafi
  32.   #--------------------------------------------------------------------------
  33.   # * Game Over Window Width in Scene_GameOver
  34.   #--------------------------------------------------------------------------
  35.   GameOverWindow_Width = 180
  36.   #--------------------------------------------------------------------------
  37.   # * Load Checkpoint Vocab
  38.   #--------------------------------------------------------------------------
  39.   LoadCheckpoint_Vocab = "Load Checkpoint"
  40.   #--------------------------------------------------------------------------
  41.   # * Return Title Vocab
  42.   #--------------------------------------------------------------------------
  43.   ReturnTitle_Vocab = "Return Title"
  44. end
  45. #==============================================================================
  46. # Don't edit below this line unless you know what to do.
  47. #==============================================================================
  48.  
  49. #==============================================================================
  50. # ** DataManager
  51. #==============================================================================
  52.  
  53. module DataManager
  54.   #--------------------------------------------------------------------------
  55.   # * Module Instance Variables
  56.   #--------------------------------------------------------------------------
  57.   @last_checkpointfile_index = 0                # most recently accessed file
  58.   #--------------------------------------------------------------------------
  59.   # * Determine Existence of Checkpoint File
  60.   #--------------------------------------------------------------------------
  61.   def self.checkpoint_file_exists?
  62.     !Dir.glob('Checkpoint*.rvdata2').empty?
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Maximum Number of Checkpoint Files
  66.   #--------------------------------------------------------------------------
  67.   def self.checkpointfile_max
  68.     return 1
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Create Checkpoint Filename
  72.   #     index : File Index
  73.   #--------------------------------------------------------------------------
  74.   def self.make_checkpoint_filename(index)
  75.     sprintf("Checkpoint%02d.rvdata2", index + 1)
  76.   end
  77.   #--------------------------------------------------------------------------
  78.   # * Execute Save Checkpoint
  79.   #--------------------------------------------------------------------------
  80.   def self.save_checkpoint
  81.     begin
  82.       save_checkpoint_without_rescue(0)
  83.     rescue
  84.       delete_checkpoint_file(0)
  85.       false
  86.     end
  87.   end
  88.   #--------------------------------------------------------------------------
  89.   # * Execute Load Checkpoint
  90.   #--------------------------------------------------------------------------
  91.   def self.load_checkpoint
  92.     load_checkpoint_without_rescue(0) rescue false
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Load Checkpoint Header
  96.   #--------------------------------------------------------------------------
  97.   def self.load_header_2(index)
  98.     load_header_2_without_rescue_2(index) rescue nil
  99.   end
  100.   #--------------------------------------------------------------------------
  101.   # * Execute Save Checkpoint (No Exception Processing)
  102.   #--------------------------------------------------------------------------
  103.   def self.save_checkpoint_without_rescue(index)
  104.     File.open(make_checkpoint_filename(index), "wb") do |file|
  105.       $game_system.on_before_save
  106.       Marshal.dump(make_checkpoint_header, file)
  107.       Marshal.dump(make_checkpoint_contents, file)
  108.       @last_checkpointfile_index = index
  109.     end
  110.     return true
  111.   end
  112.   #--------------------------------------------------------------------------
  113.   # * Execute Load Checkpoint (No Exception Processing)
  114.   #--------------------------------------------------------------------------
  115.   def self.load_checkpoint_without_rescue(index)
  116.     File.open(make_checkpoint_filename(index), "rb") do |file|
  117.       Marshal.load(file)
  118.       extract_checkpoint_contents(Marshal.load(file))
  119.       reload_map_if_updated_2
  120.       @last_checkpointfile_index = index
  121.     end
  122.     return true
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # * Load Checkpoint Header (No Exception Processing)
  126.   #--------------------------------------------------------------------------
  127.   def self.load_header_2_without_rescue_2(index)
  128.     File.open(make_checkpoint_filename(index), "rb") do |file|
  129.       return Marshal.load(file)
  130.     end
  131.     return nil
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # * Delete Checkpoint File
  135.   #--------------------------------------------------------------------------
  136.   def self.delete_checkpoint_file(index)
  137.     File.delete(make_checkpoint_filename(index)) rescue nil
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # * Delete Checkpoint
  141.   #--------------------------------------------------------------------------
  142.   def self.delete_checkpoint
  143.     File.delete(make_checkpoint_filename(0)) rescue nil
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # * Create Checkpoint Header
  147.   #--------------------------------------------------------------------------
  148.   def self.make_checkpoint_header
  149.     header = {}
  150.     header[:characters] = $game_party.characters_for_savefile
  151.     header[:playtime_s] = $game_system.playtime_s
  152.     header
  153.   end
  154.   #--------------------------------------------------------------------------
  155.   # * Create Checkpoint Contents
  156.   #--------------------------------------------------------------------------
  157.   def self.make_checkpoint_contents
  158.     contents = {}
  159.     contents[:system]        = $game_system
  160.     contents[:timer]         = $game_timer
  161.     contents[:message]       = $game_message
  162.     contents[:switches]      = $game_switches
  163.     contents[:variables]     = $game_variables
  164.     contents[:self_switches] = $game_self_switches
  165.     contents[:actors]        = $game_actors
  166.     contents[:party]         = $game_party
  167.     contents[:troop]         = $game_troop
  168. #~     contents[:map]           = $game_map
  169.     contents[:player]        = $game_player
  170.     contents
  171.   end
  172.   #--------------------------------------------------------------------------
  173.   # * Extract Checkpoint Contents
  174.   #--------------------------------------------------------------------------
  175.   def self.extract_checkpoint_contents(contents)
  176.     $game_system        = contents[:system]
  177.     $game_timer         = contents[:timer]
  178.     $game_message       = contents[:message]
  179.     $game_switches      = contents[:switches]
  180.     $game_variables     = contents[:variables]
  181.     $game_self_switches = contents[:self_switches]
  182.     $game_actors        = contents[:actors]
  183.     $game_party         = contents[:party]
  184.     $game_troop         = contents[:troop]
  185. #~     $game_map           = contents[:map]
  186.     $game_player        = contents[:player]
  187.   end
  188.   #--------------------------------------------------------------------------
  189.   # * Reload Map if Data Is Updated
  190.   #--------------------------------------------------------------------------
  191.   def self.reload_map_if_updated_2
  192.     if $game_system.version_id != $data_system.version_id
  193.       $game_map.setup($game_map.map_id)
  194.       $game_player.center($game_player.x, $game_player.y)
  195.       $game_player.make_encounter_count
  196.     end
  197.   end
  198.   #--------------------------------------------------------------------------
  199.   # * Get Update Date of Checkpoint File
  200.   #--------------------------------------------------------------------------
  201.   def self.checkpointfile_time_stamp(index)
  202.     File.mtime(make_checkpoint_filename(index)) rescue Time.at(0)
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # * Get File Index with Latest Update Date
  206.   #--------------------------------------------------------------------------
  207.   def self.latest_checkpointfile_index
  208.     checkpointfile_max.times.max_by {|i| checkpointfile_time_stamp(i) }
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * Get Index of File Most Recently Accessed
  212.   #--------------------------------------------------------------------------
  213.   def self.last_checkpointfile_index
  214.     @last_checkpointfile_index
  215.   end
  216. end
  217.  
  218. class Scene_Gameover < Scene_Base
  219.   alias old_start_1 start
  220.   def start
  221.     old_start_1
  222.     create_command_window
  223.   end
  224.  
  225.   # overwrites update method
  226.   # prevents returning to title screen upon button press
  227.   def update
  228.     super
  229.   end
  230.  
  231.   def pre_terminate
  232.     super
  233.     close_command_window
  234.   end
  235.  
  236.   def create_command_window
  237.     @command_window = Window_GameOver.new
  238.     set_handlers
  239.   end
  240.  
  241.   def set_handlers
  242.     set_handlers_first
  243.     @command_window.set_handler(:load, method(:command_load_checkpoint))
  244.     @command_window.set_handler(:to_title, method(:goto_title))
  245.     @command_window.set_handler(:shutdown, method(:command_shutdown))
  246.     set_handlers_last
  247.   end
  248.  
  249.   def set_handlers_first; end
  250.   def set_handlers_last; end
  251.  
  252.   def close_command_window
  253.     @command_window.close if @command_window
  254.     update until @command_window.close?
  255.   end
  256.  
  257.   alias old_goto_title_1 goto_title
  258.   def goto_title
  259.     fadeout_all
  260.     old_goto_title_1
  261.   end
  262.  
  263.   def command_load_checkpoint
  264.     if DataManager.load_checkpoint
  265.       Sound.play_load
  266.       fadeout_all
  267.       $game_system.on_after_load
  268.       SceneManager.goto(Scene_Map)
  269.     else
  270.       Sound.play_buzzer
  271.       @command_window.activate
  272.     end
  273.   end
  274.  
  275.   def command_shutdown
  276.     SceneManager.exit
  277.   end
  278. end
  279.  
  280. class Window_GameOver < Window_Command
  281.   def initialize
  282.     super(0, 0)
  283.     update_placement
  284.     self.openness = 0
  285.     open
  286.   end
  287.  
  288.   def window_width
  289.     return Kadafi::GameOverWindow_Width
  290.   end
  291.  
  292.   def update_placement
  293.     self.x = [(Graphics.width - width)/2, 0].max
  294.     self.y = [(Graphics.height - height)/1.1, 0].max
  295.   end
  296.  
  297.   def alignment
  298.     return 1
  299.   end
  300.  
  301.   def make_command_list
  302.     add_commands_first
  303.     add_command(Kadafi::LoadCheckpoint_Vocab, :load, can_load_checkpoint?)
  304.     add_command(Kadafi::ReturnTitle_Vocab,    :to_title)
  305. #~     add_command(Vocab::shutdown,  :shutdown)
  306.     add_commands_last
  307.   end
  308.  
  309.   def add_commands_first; end
  310.   def add_commands_last; end
  311.  
  312.   def can_load_checkpoint?
  313.     DataManager.checkpoint_file_exists?
  314.   end
  315. end
Add Comment
Please, Sign In to add comment