Advertisement
Zeriab

[RMXP][Static] Dev tool - Extract event information

Oct 20th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.49 KB | None | 0 0
  1. EXTRACT_TEXT                 = false
  2. EXTRACT_CONDITIONAL_BRANCHES = true
  3. EXTRACT_CONTROL_SWITCHES     = true
  4. EXTRACT_CONTROL_VARIABLES    = true
  5.  
  6. EXTRACT_CODES = []
  7. EXTRACT_CODES << 101 if EXTRACT_TEXT
  8. EXTRACT_CODES << 102 if EXTRACT_TEXT
  9. EXTRACT_CODES << 401 if EXTRACT_TEXT
  10. EXTRACT_CODES << 111 if EXTRACT_CONDITIONAL_BRANCHES
  11. EXTRACT_CODES << 121 if EXTRACT_CONTROL_SWITCHES
  12. EXTRACT_CODES << 122 if EXTRACT_CONTROL_VARIABLES
  13.  
  14. def update_sprite
  15.   if @sprite.nil?
  16.     @sprite = Sprite.new
  17.     @sprite.bitmap = Bitmap.new(640, 120)
  18.   end
  19.   current = $current
  20.   unless current == $last
  21.     $last = current
  22.     @sprite.bitmap.clear
  23.     unless current.nil?
  24.       @sprite.bitmap.draw_text(8,4,600,32,$last.map)
  25.       @sprite.bitmap.draw_text(8,40,600,32,$last.event)
  26.     end
  27.   end
  28. end
  29.  
  30. ##
  31. # A separate thread that will run and make sure the Graphics.update is updated
  32. # every now and then.
  33. #
  34. Thread.new {
  35.   loop do
  36.     # Lets the thread sleep for a while to minimize CPU usage
  37.     sleep 0.1
  38.     # Update the sprite
  39.     update_sprite
  40.     # Update the graphics
  41.     Graphics.update
  42.   end
  43. }
  44.  
  45. Current = Struct.new(:map, :event)
  46. $current = nil
  47.  
  48. Map = Struct.new(:name, :map_id, :events)
  49. Event = Struct.new(:name, :event_id, :pages)
  50. Page = Struct.new(:number, :list)
  51. ListElement = Struct.new(:message, :type)
  52.  
  53. ######
  54. ######
  55.  
  56. class Map
  57.   def write_to_stream(io)
  58.     io.print "\r\n########################################"
  59.     io.print "\r\n# Map #{map_id} ~ #{name}  (#{events.size} events)\r\n"
  60.     io.print "########################################\r\n"
  61.     for event in events
  62.       event.write_to_stream(io)
  63.     end
  64.   end
  65. end
  66.  
  67. class Event
  68.   def write_to_stream(io)
  69.     io.print "\r\n-= Event #{event_id} - #{name} =-\r\n"
  70.     for page in pages
  71.       page.write_to_stream(io)
  72.     end
  73.   end
  74. end
  75.  
  76. class Page
  77.   def write_to_stream(io)
  78.     io.print "Page #{number}:\r\n"
  79.     for element in list
  80.       element.write_to_stream(io)
  81.       io.print "\r\n"
  82.     end
  83.   end
  84. end
  85.  
  86. class ListElement
  87.   def write_to_stream(io)
  88.     io.print message
  89.   end
  90. end
  91.  
  92. ######
  93. def process_list(list)
  94.   # Go through the event commands in the list
  95.   rlist = [] # resulting list
  96.   command_index = 0
  97.   while command_index < list.size
  98.     command_index = process_event_command(list, command_index, rlist)
  99.     command_index += 1
  100.   end
  101.   return rlist
  102. end
  103.  
  104. def process_event_command(list, index, rlist)
  105.   command_index = index
  106.   command = list[command_index]
  107.   if (!EXTRACT_CODES.include?(command.code))
  108.     # Skip
  109.     return command_index;
  110.   end
  111.   # Handle code
  112.   case command.code
  113.   when 101
  114.     message = command.parameters[0].rstrip
  115.     loop do
  116.       break unless command_index + 1 < list.size
  117.       if list[command_index+1].code == 401
  118.         command_index += 1
  119.         message += " " + list[command_index].parameters[0].rstrip
  120.       elsif list[command_index+1].code == 102
  121.         command_index += 1
  122.         for text in list[command_index].parameters[0]
  123.           message += "\r\n" + text.rstrip
  124.         end
  125.       else
  126.         break
  127.       end
  128.     end
  129.     rlist << ListElement.new(message, 'text')
  130.   when 102
  131.     message = ""
  132.     for text in list[command_index].parameters[0]
  133.       message += "\r\n" + text.rstrip
  134.     end
  135.     rlist << ListElement.new(message, 'choice')
  136.   when 111
  137.     type = command.parameters[0] == 1 ? 'variable' : 'switch'
  138.     message = "Conditional branch using #{type}[#{command.parameters[0]}]"
  139.     rlist << ListElement.new(message, 'conditional')
  140.   when 121
  141.     range = command.parameters[0] .. command.parameters[1]
  142.     if command.parameters[0] != command.parameters[1]
  143.       range_str = range.to_s
  144.     else
  145.       range_str = command.parameters[0].to_s
  146.     end
  147.     message = "Control switches #{range_str}"
  148.     rlist << ListElement.new(message, 'control_switch')
  149.   when 122
  150.     range = command.parameters[0] .. command.parameters[1]
  151.     if command.parameters[0] != command.parameters[1]
  152.       range_str = range.to_s
  153.     else
  154.       range_str = command.parameters[0].to_s
  155.     end
  156.     message = "Control variables #{range_str}"
  157.     rlist << ListElement.new(message, 'control_variable')
  158.   end
  159.   return command_index
  160. end
  161. ######
  162.  
  163. class CommonEvent < Struct.new(:name, :event_id, :list)
  164.   def write_to_stream(io)
  165.     io.print "\r\n-= Common Event #{event_id} - #{name} =-\r\n"
  166.     for element in list
  167.       element.write_to_stream(io)
  168.       io.print "\r\n"
  169.     end
  170.   end
  171. end
  172.  
  173. common_events = load_data('Data/CommonEvents.rxdata')
  174. ces = []
  175. for ce in common_events.compact
  176.   # Look at which event is currently processing
  177.   cur = Current.new("Processing common events...",
  178.                     "Common event #{ce.id} - #{ce.name}")
  179.   $current = cur
  180.   # Process the list
  181.   list = process_list(ce.list)
  182.   unless list.empty?
  183.     # Create struct
  184.     current_ce = CommonEvent.new(ce.name, ce.id, list)
  185.     ces << current_ce
  186.   end
  187. end
  188.  
  189. ######
  190. ######
  191.  
  192. class Troop < Event
  193.   def write_to_stream(io)
  194.     io.print "\r\n-= Troop #{event_id} - #{name} =-\r\n"
  195.     for page in pages
  196.       page.write_to_stream(io)
  197.     end
  198.   end
  199. end
  200.  
  201. troops = load_data('Data\Troops.rxdata')
  202.  
  203. battle_events = []
  204. for event in troops.compact
  205.   # Look at which event is currently processing
  206.   cur = Current.new("Processing battle events...",
  207.                     "Troop " + event.id.to_s + " - " + event.name)
  208.   $current = cur
  209.   # Create the event struct
  210.   current_event = Troop.new(event.name, event.id, [])
  211.   # Create pages
  212.   event.pages.each_with_index do |page, page_index|
  213.     list = process_list(page.list)
  214.     unless list.empty?
  215.       current_page = Page.new(page_index+1, list)
  216.       current_event.pages << current_page
  217.     end
  218.   end
  219.   # Let's disregard useless events
  220.   battle_events << current_event unless current_event.pages.empty?
  221. end
  222.  
  223. ######
  224. ######
  225.  
  226. infos = load_data('Data\MapInfos.rxdata')
  227.  
  228. maps = []
  229. for map_id, map_info in infos.sort
  230.   # Create map struct
  231.   current_map = Map.new(map_info.name, map_id, [])
  232.   maps << current_map
  233.   map = load_data(sprintf("Data/Map%03d.rxdata", map_id))
  234.   # Create event structs
  235.   for event_id, event in map.events.sort
  236.     # Look at which event is currently processing
  237.     cur = Current.new("Map " + map_id.to_s + " - " + map_info.name,
  238.                       "Event " + event_id.to_s + " - " + event.name)
  239.     $current = cur
  240.     # Create the event struct
  241.     current_event = Event.new(event.name, event_id, [])
  242.     # Create pages
  243.     event.pages.each_with_index do |page, page_index|
  244.       list = process_list(page.list)
  245.       unless list.empty?
  246.         current_page = Page.new(page_index+1, list)
  247.         # Let's disregard useless pages
  248.         current_event.pages << current_page
  249.       end
  250.     end
  251.     # Let's disregard useless events
  252.     current_map.events << current_event unless current_event.pages.empty?
  253.   end
  254. end
  255.  
  256. # Update status info
  257. cur = Current.new("All maps have been processed",
  258.                   "Writing to a text file")
  259. $current = cur
  260.  
  261. File.open('Dialogue_Map.txt', 'wb') {|f|
  262.   f.print "THIS FILE CONTAINS THE DIALOGUE EXTRACTED FROM THE MAPS\r\n\r\n"
  263.   for map in maps
  264.     map.write_to_stream(f)
  265.   end
  266. }
  267.  
  268. File.open('Dialogue_Common.txt', 'wb') {|f|
  269.   f.print "THIS FILE CONTAINS THE DIALOGUE EXTRACTED FROM THE COMMON EVENTS\r\n\r\n"
  270.   for ce in ces
  271.     ce.write_to_stream(f)
  272.   end
  273. }
  274.  
  275. File.open('Dialogue_Battle.txt', 'wb') {|f|
  276.   f.print "THIS FILE CONTAINS THE DIALOGUE EXTRACTED FROM THE BATTLE EVENTS\r\n\r\n"
  277.   for be in battle_events
  278.     be.write_to_stream(f)
  279.   end
  280. }
  281. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement