Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # Kadafi - Checkpoint System
- # Version : 1.0
- # Contact : http://forums.rpgmakerweb.com/index.php?/user/33654-kadafi/
- #==============================================================================
- ($imported ||= {})[:Kadafi_CheckpointSystem] = true
- #==============================================================================
- # CHANGE LOGS:
- # -----------------------------------------------------------------------------
- # 2017.10.21 - Finished script
- #==============================================================================
- =begin
- Introduction :
- This script create a checkpoint system.
- How to Use :
- Edit the configuration below.
- Script Call :
- DataManager.save_checkpoint # Save Checkpoint
- DataManager.delete_checkpoint # Delete Checkpoint
- Terms of Use :
- 1. Credit me as Kadafi (Optional)
- =end
- #==============================================================================
- # Configuration
- #==============================================================================
- module Kadafi
- #--------------------------------------------------------------------------
- # * Game Over Window Width in Scene_GameOver
- #--------------------------------------------------------------------------
- GameOverWindow_Width = 180
- #--------------------------------------------------------------------------
- # * Load Checkpoint Vocab
- #--------------------------------------------------------------------------
- LoadCheckpoint_Vocab = "Load Checkpoint"
- #--------------------------------------------------------------------------
- # * Return Title Vocab
- #--------------------------------------------------------------------------
- ReturnTitle_Vocab = "Return Title"
- end
- #==============================================================================
- # Don't edit below this line unless you know what to do.
- #==============================================================================
- #==============================================================================
- # ** DataManager
- #==============================================================================
- module DataManager
- #--------------------------------------------------------------------------
- # * Module Instance Variables
- #--------------------------------------------------------------------------
- @last_checkpointfile_index = 0 # most recently accessed file
- #--------------------------------------------------------------------------
- # * Determine Existence of Checkpoint File
- #--------------------------------------------------------------------------
- def self.checkpoint_file_exists?
- !Dir.glob('Checkpoint*.rvdata2').empty?
- end
- #--------------------------------------------------------------------------
- # * Maximum Number of Checkpoint Files
- #--------------------------------------------------------------------------
- def self.checkpointfile_max
- return 1
- end
- #--------------------------------------------------------------------------
- # * Create Checkpoint Filename
- # index : File Index
- #--------------------------------------------------------------------------
- def self.make_checkpoint_filename(index)
- sprintf("Checkpoint%02d.rvdata2", index + 1)
- end
- #--------------------------------------------------------------------------
- # * Execute Save Checkpoint
- #--------------------------------------------------------------------------
- def self.save_checkpoint
- begin
- save_checkpoint_without_rescue(0)
- rescue
- delete_checkpoint_file(0)
- false
- end
- end
- #--------------------------------------------------------------------------
- # * Execute Load Checkpoint
- #--------------------------------------------------------------------------
- def self.load_checkpoint
- load_checkpoint_without_rescue(0) rescue false
- end
- #--------------------------------------------------------------------------
- # * Load Checkpoint Header
- #--------------------------------------------------------------------------
- def self.load_header_2(index)
- load_header_2_without_rescue_2(index) rescue nil
- end
- #--------------------------------------------------------------------------
- # * Execute Save Checkpoint (No Exception Processing)
- #--------------------------------------------------------------------------
- def self.save_checkpoint_without_rescue(index)
- File.open(make_checkpoint_filename(index), "wb") do |file|
- $game_system.on_before_save
- Marshal.dump(make_checkpoint_header, file)
- Marshal.dump(make_checkpoint_contents, file)
- @last_checkpointfile_index = index
- end
- return true
- end
- #--------------------------------------------------------------------------
- # * Execute Load Checkpoint (No Exception Processing)
- #--------------------------------------------------------------------------
- def self.load_checkpoint_without_rescue(index)
- File.open(make_checkpoint_filename(index), "rb") do |file|
- Marshal.load(file)
- extract_checkpoint_contents(Marshal.load(file))
- reload_map_if_updated_2
- @last_checkpointfile_index = index
- end
- return true
- end
- #--------------------------------------------------------------------------
- # * Load Checkpoint Header (No Exception Processing)
- #--------------------------------------------------------------------------
- def self.load_header_2_without_rescue_2(index)
- File.open(make_checkpoint_filename(index), "rb") do |file|
- return Marshal.load(file)
- end
- return nil
- end
- #--------------------------------------------------------------------------
- # * Delete Checkpoint File
- #--------------------------------------------------------------------------
- def self.delete_checkpoint_file(index)
- File.delete(make_checkpoint_filename(index)) rescue nil
- end
- #--------------------------------------------------------------------------
- # * Delete Checkpoint
- #--------------------------------------------------------------------------
- def self.delete_checkpoint
- File.delete(make_checkpoint_filename(0)) rescue nil
- end
- #--------------------------------------------------------------------------
- # * Create Checkpoint Header
- #--------------------------------------------------------------------------
- def self.make_checkpoint_header
- header = {}
- header[:characters] = $game_party.characters_for_savefile
- header[:playtime_s] = $game_system.playtime_s
- header
- end
- #--------------------------------------------------------------------------
- # * Create Checkpoint Contents
- #--------------------------------------------------------------------------
- def self.make_checkpoint_contents
- contents = {}
- contents[:system] = $game_system
- contents[:timer] = $game_timer
- contents[:message] = $game_message
- contents[:switches] = $game_switches
- contents[:variables] = $game_variables
- contents[:self_switches] = $game_self_switches
- contents[:actors] = $game_actors
- contents[:party] = $game_party
- contents[:troop] = $game_troop
- #~ contents[:map] = $game_map
- contents[:player] = $game_player
- contents
- end
- #--------------------------------------------------------------------------
- # * Extract Checkpoint Contents
- #--------------------------------------------------------------------------
- def self.extract_checkpoint_contents(contents)
- $game_system = contents[:system]
- $game_timer = contents[:timer]
- $game_message = contents[:message]
- $game_switches = contents[:switches]
- $game_variables = contents[:variables]
- $game_self_switches = contents[:self_switches]
- $game_actors = contents[:actors]
- $game_party = contents[:party]
- $game_troop = contents[:troop]
- #~ $game_map = contents[:map]
- $game_player = contents[:player]
- end
- #--------------------------------------------------------------------------
- # * Reload Map if Data Is Updated
- #--------------------------------------------------------------------------
- def self.reload_map_if_updated_2
- if $game_system.version_id != $data_system.version_id
- $game_map.setup($game_map.map_id)
- $game_player.center($game_player.x, $game_player.y)
- $game_player.make_encounter_count
- end
- end
- #--------------------------------------------------------------------------
- # * Get Update Date of Checkpoint File
- #--------------------------------------------------------------------------
- def self.checkpointfile_time_stamp(index)
- File.mtime(make_checkpoint_filename(index)) rescue Time.at(0)
- end
- #--------------------------------------------------------------------------
- # * Get File Index with Latest Update Date
- #--------------------------------------------------------------------------
- def self.latest_checkpointfile_index
- checkpointfile_max.times.max_by {|i| checkpointfile_time_stamp(i) }
- end
- #--------------------------------------------------------------------------
- # * Get Index of File Most Recently Accessed
- #--------------------------------------------------------------------------
- def self.last_checkpointfile_index
- @last_checkpointfile_index
- end
- end
- class Scene_Gameover < Scene_Base
- alias old_start_1 start
- def start
- old_start_1
- create_command_window
- end
- # overwrites update method
- # prevents returning to title screen upon button press
- def update
- super
- end
- def pre_terminate
- super
- close_command_window
- end
- def create_command_window
- @command_window = Window_GameOver.new
- set_handlers
- end
- def set_handlers
- set_handlers_first
- @command_window.set_handler(:load, method(:command_load_checkpoint))
- @command_window.set_handler(:to_title, method(:goto_title))
- @command_window.set_handler(:shutdown, method(:command_shutdown))
- set_handlers_last
- end
- def set_handlers_first; end
- def set_handlers_last; end
- def close_command_window
- @command_window.close if @command_window
- update until @command_window.close?
- end
- alias old_goto_title_1 goto_title
- def goto_title
- fadeout_all
- old_goto_title_1
- end
- def command_load_checkpoint
- if DataManager.load_checkpoint
- Sound.play_load
- fadeout_all
- $game_system.on_after_load
- SceneManager.goto(Scene_Map)
- else
- Sound.play_buzzer
- @command_window.activate
- end
- end
- def command_shutdown
- SceneManager.exit
- end
- end
- class Window_GameOver < Window_Command
- def initialize
- super(0, 0)
- update_placement
- self.openness = 0
- open
- end
- def window_width
- return Kadafi::GameOverWindow_Width
- end
- def update_placement
- self.x = [(Graphics.width - width)/2, 0].max
- self.y = [(Graphics.height - height)/1.1, 0].max
- end
- def alignment
- return 1
- end
- def make_command_list
- add_commands_first
- add_command(Kadafi::LoadCheckpoint_Vocab, :load, can_load_checkpoint?)
- add_command(Kadafi::ReturnTitle_Vocab, :to_title)
- #~ add_command(Vocab::shutdown, :shutdown)
- add_commands_last
- end
- def add_commands_first; end
- def add_commands_last; end
- def can_load_checkpoint?
- DataManager.checkpoint_file_exists?
- end
- end
Add Comment
Please, Sign In to add comment