Advertisement
Black_Mage

Autosave RMXP Script

Feb 15th, 2014
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. class Scene_Title
  2. def main
  3. # If battle test
  4. if $BTEST
  5. battle_test
  6. return
  7. end
  8. # Load database
  9. $data_actors = load_data("Data/Actors.rxdata")
  10. $data_classes = load_data("Data/Classes.rxdata")
  11. $data_skills = load_data("Data/Skills.rxdata")
  12. $data_items = load_data("Data/Items.rxdata")
  13. $data_weapons = load_data("Data/Weapons.rxdata")
  14. $data_armors = load_data("Data/Armors.rxdata")
  15. $data_enemies = load_data("Data/Enemies.rxdata")
  16. $data_troops = load_data("Data/Troops.rxdata")
  17. $data_states = load_data("Data/States.rxdata")
  18. $data_animations = load_data("Data/Animations.rxdata")
  19. $data_tilesets = load_data("Data/Tilesets.rxdata")
  20. $data_common_events = load_data("Data/CommonEvents.rxdata")
  21. $data_system = load_data("Data/System.rxdata")
  22. # Make system object
  23. $game_system = Game_System.new
  24. # Make title graphic
  25. @sprite = Sprite.new
  26. @sprite.bitmap = RPG::Cache.title($data_system.title_name)
  27. # Make command window
  28. s1 = "Play"
  29. s2 = "Exit"
  30. @command_window = Window_Command.new(192, [s1, s2])
  31. @command_window.back_opacity = 160
  32. @command_window.x = 320 - @command_window.width / 2
  33. @command_window.y = 288
  34. # Play title BGM
  35. $game_system.bgm_play($data_system.title_bgm)
  36. # Stop playing ME and BGS
  37. Audio.me_stop
  38. Audio.bgs_stop
  39. # Execute transition
  40. Graphics.transition
  41. # Main loop
  42. loop do
  43. # Update game screen
  44. Graphics.update
  45. # Update input information
  46. Input.update
  47. # Frame update
  48. update
  49. # Abort loop if screen is changed
  50. if $scene != self
  51. break
  52. end
  53. end
  54. # Prepare for transition
  55. Graphics.freeze
  56. # Dispose of command window
  57. @command_window.dispose
  58. # Dispose of title graphic
  59. @sprite.bitmap.dispose
  60. @sprite.dispose
  61. end
  62. #--------------------------------------------------------------------------
  63. # * Frame Update
  64. #--------------------------------------------------------------------------
  65. def update
  66. # Update command window
  67. @command_window.update
  68. # If C button was pressed
  69. if Input.trigger?(Input::C)
  70. # Branch by command window cursor position
  71. case @command_window.index
  72. when 0 # New game
  73. command_new_game
  74. when 1 # Shutdown
  75. command_shutdown
  76. end
  77. end
  78. end
  79. #--------------------------------------------------------------------------
  80. # * Command: New Game
  81. #--------------------------------------------------------------------------
  82. def command_new_game
  83. if FileTest.exist?("Save.rxdata")
  84. # Play decision SE
  85. $game_system.se_play($data_system.decision_se)
  86. # Switch to load screen
  87. $scene = Auto_Load2.new
  88. else
  89. # Play decision SE
  90. $game_system.se_play($data_system.decision_se)
  91. # Stop BGM
  92. Audio.bgm_stop
  93. # Reset frame count for measuring play time
  94. Graphics.frame_count = 0
  95. # Make each type of game object
  96. $game_temp = Game_Temp.new
  97. $game_system = Game_System.new
  98. $game_switches = Game_Switches.new
  99. $game_variables = Game_Variables.new
  100. $game_self_switches = Game_SelfSwitches.new
  101. $game_screen = Game_Screen.new
  102. $game_actors = Game_Actors.new
  103. $game_party = Game_Party.new
  104. $game_troop = Game_Troop.new
  105. $game_map = Game_Map.new
  106. $game_player = Game_Player.new
  107. # Set up initial party
  108. $game_party.setup_starting_members
  109. # Set up initial map position
  110. $game_map.setup($data_system.start_map_id)
  111. # Move player to initial position
  112. $game_player.moveto($data_system.start_x, $data_system.start_y)
  113. # Refresh player
  114. $game_player.refresh
  115. # Run automatic change for BGM and BGS set with map
  116. $game_map.autoplay
  117. # Update map (run parallel process event)
  118. $game_map.update
  119. # Switch to map screen
  120. $scene = Scene_Map.new
  121. end
  122. end
  123. #--------------------------------------------------------------------------
  124. # * Command: Shutdown
  125. #--------------------------------------------------------------------------
  126. def command_shutdown
  127. # Play decision SE
  128. $game_system.se_play($data_system.decision_se)
  129. # Fade out BGM, BGS, and ME
  130. Audio.bgm_fade(800)
  131. Audio.bgs_fade(800)
  132. Audio.me_fade(800)
  133. # Shutdown
  134. $scene = nil
  135. end
  136. end
  137.  
  138.  
  139. #==============================================================================
  140. # ** Scene_File
  141. #------------------------------------------------------------------------------
  142. # This is a superclass for the save screen and load screen.
  143. #==============================================================================
  144.  
  145. class Auto_Load
  146. #--------------------------------------------------------------------------
  147. # * Object Initialization
  148. # help_text : text string shown in the help window
  149. #--------------------------------------------------------------------------
  150. def initialize(help_text)
  151. @help_text = ""
  152. end
  153. #--------------------------------------------------------------------------
  154. # * Main Processing
  155. #--------------------------------------------------------------------------
  156. def main
  157. # Select last file to be operated
  158. @file_index = $game_temp.last_file_index
  159. # Execute transition
  160. Graphics.transition
  161. # Main loop
  162. loop do
  163. on_decision(make_filename(@file_index))
  164. $game_temp.last_file_index = @file_index
  165. # Abort loop if screen is changed
  166. if $scene != self
  167. break
  168. end
  169. end
  170. # Prepare for transition
  171. Graphics.freeze
  172. end
  173. #--------------------------------------------------------------------------
  174. # * Make File Name
  175. # file_index : save file index (0-3)
  176. #--------------------------------------------------------------------------
  177. def make_filename(file_index)
  178. return "Save.rxdata"
  179. end
  180. end
  181.  
  182.  
  183. #==============================================================================
  184. # ** Scene_Load
  185. #------------------------------------------------------------------------------
  186. # This class performs load screen processing.
  187. #==============================================================================
  188.  
  189. class Auto_Load2 < Auto_Load
  190. #--------------------------------------------------------------------------
  191. # * Object Initialization
  192. #--------------------------------------------------------------------------
  193. def initialize
  194. # Remake temporary object
  195. $game_temp = Game_Temp.new
  196. # Timestamp selects new file
  197. $game_temp.last_file_index = 0
  198. latest_time = Time.at(0)
  199. for i in 0..3
  200. filename = make_filename(i)
  201. if FileTest.exist?(filename)
  202. file = File.open(filename, "r")
  203. if file.mtime > latest_time
  204. latest_time = file.mtime
  205. $game_temp.last_file_index = i
  206. end
  207. file.close
  208. end
  209. end
  210. super("Which file would you like to load?")
  211. end
  212. #--------------------------------------------------------------------------
  213. # * Decision Processing
  214. #--------------------------------------------------------------------------
  215. def on_decision(filename)
  216. # If file doesn't exist
  217. unless FileTest.exist?(filename)
  218. # Play buzzer SE
  219. $game_system.se_play($data_system.buzzer_se)
  220. return
  221. end
  222. # Play load SE
  223. $game_system.se_play($data_system.load_se)
  224. # Read save data
  225. file = File.open(filename, "rb")
  226. read_save_data(file)
  227. file.close
  228. # Restore BGM and BGS
  229. $game_system.bgm_play($game_system.playing_bgm)
  230. $game_system.bgs_play($game_system.playing_bgs)
  231. # Update map (run parallel process event)
  232. $game_map.update
  233. # Switch to map screen
  234. $scene = Scene_Map.new
  235. end
  236. #--------------------------------------------------------------------------
  237. # * Read Save Data
  238. # file : file object for reading (opened)
  239. #--------------------------------------------------------------------------
  240. def read_save_data(file)
  241. # Read character data for drawing save file
  242. characters = Marshal.load(file)
  243. # Read frame count for measuring play time
  244. Graphics.frame_count = Marshal.load(file)
  245. # Read each type of game object
  246. $game_system = Marshal.load(file)
  247. $game_switches = Marshal.load(file)
  248. $game_variables = Marshal.load(file)
  249. $game_self_switches = Marshal.load(file)
  250. $game_screen = Marshal.load(file)
  251. $game_actors = Marshal.load(file)
  252. $game_party = Marshal.load(file)
  253. $game_troop = Marshal.load(file)
  254. $game_map = Marshal.load(file)
  255. $game_player = Marshal.load(file)
  256. # If magic number is different from when saving
  257. # (if editing was added with editor)
  258. if $game_system.magic_number != $data_system.magic_number
  259. # Load map
  260. $game_map.setup($game_map.map_id)
  261. $game_player.center($game_player.x, $game_player.y)
  262. end
  263. # Refresh party members
  264. $game_party.refresh
  265. end
  266. end
  267.  
  268.  
  269. #==============================================================================
  270. # ** Scene_Save
  271. #------------------------------------------------------------------------------
  272. # This class performs save screen processing.
  273. #==============================================================================
  274. class Auto_Save
  275. def initialize
  276. end
  277. def main
  278. # Execute transition
  279. # Graphics.transition
  280. on_decision("Save.rxdata")
  281. # Prepare for transition
  282. Graphics.freeze
  283. $scene = Scene_Map.new
  284. end
  285. #--------------------------------------------------------------------------
  286. # * Decision Processing
  287. #--------------------------------------------------------------------------
  288. def on_decision(filename)
  289. file = File.open(filename, "wb")
  290. write_save_data(file)
  291. file.close
  292. end
  293. #--------------------------------------------------------------------------
  294. # * Write Save Data
  295. # file : write file object (opened)
  296. #--------------------------------------------------------------------------
  297. def write_save_data(file)
  298. # Make character data for drawing save file
  299. characters = []
  300. for i in 0...$game_party.actors.size
  301. actor = $game_party.actors[i]
  302. characters.push([actor.character_name, actor.character_hue])
  303. end
  304. # Write character data for drawing save file
  305. Marshal.dump(characters, file)
  306. # Wrire frame count for measuring play time
  307. Marshal.dump(Graphics.frame_count, file)
  308. # Increase save count by 1
  309. $game_system.save_count += 1
  310. # Save magic number
  311. # (A random value will be written each time saving with editor)
  312. $game_system.magic_number = $data_system.magic_number
  313. # Write each type of game object
  314. Marshal.dump($game_system, file)
  315. Marshal.dump($game_switches, file)
  316. Marshal.dump($game_variables, file)
  317. Marshal.dump($game_self_switches, file)
  318. Marshal.dump($game_screen, file)
  319. Marshal.dump($game_actors, file)
  320. Marshal.dump($game_party, file)
  321. Marshal.dump($game_troop, file)
  322. Marshal.dump($game_map, file)
  323. Marshal.dump($game_player, file)
  324. end
  325. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement