Advertisement
SSTrihan

Message System XP

Mar 13th, 2022
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.02 KB | None | 0 0
  1. #==============================================================================
  2. # Trilobytes - Message System XP v1.00
  3. #
  4. # - Updated: 14/03/22
  5. #==============================================================================
  6.  
  7. module TLB_MSG
  8.   ROW_VAR = 21
  9.  
  10.   def self.rows_for_text
  11.     return 4 if ROW_VAR <= 0 || $game_variables[ROW_VAR] <= 0
  12.     return $game_variables[ROW_VAR]
  13.   end
  14. end # TLB_MSG
  15.  
  16. #==============================================================================
  17. # Version history
  18. #
  19. # 14/03/22 - Finished script
  20. #==============================================================================
  21. # Introduction
  22. # ------------
  23. # This script introduces to RPG Maker XP most of the text codes added to VX
  24. # Ace by Yanfly's Ace Message System. Some of them aren't applicable to XP,
  25. # such as those related to waiting (since XP shows all text at once) and faces
  26. # (since XP doesn't use them).
  27. #==============================================================================
  28. # Instructions
  29. # ------------
  30. # In XP's Script Editor, insert a new line above Main, give it a descriptive
  31. # name, and paste this script into the large window on the right. Then click
  32. # Apply to save the changes.
  33. #
  34. # Text Codes
  35. # ----------
  36. #   Code:           Result:
  37. #     \p[x]         - The name of the party member in slot x.
  38. #     \g            - The name of the currency unit.
  39. #     \c[x]         - Changes text colour to x.
  40. #     \i[x]         - Draws the icon with the filename x.
  41. #     \{            - Increases text size by 8.
  42. #     \}            - Decreases text size by 8.
  43. #     \$            - Opens the gold display window (was previously \g)
  44. #
  45. #   Name Window:    Result:
  46. #     \n<x>         - Opens a name window with the string x on the left side.
  47. #     \nc<x>        - Opens a name window with the string x in the centre.
  48. #     \nr<x>        - Opens a name window with the string x on the right side.
  49. #
  50. #   Position:       Result:
  51. #     \px[x]        - Places the following text at X position x.
  52. #     \py[x]        - Places the following text at Y position x.
  53. #
  54. #   Picture:        Result:
  55. #     \pic[x]       - Draws picture x from the Graphics/Pictures folder.
  56. #
  57. #   Outline:        Result:
  58. #     \oc[x]        - Sets the outline colour to x (as defined in windowskin)
  59. #     \oo[x]        - Sets outline opacity to x (0-255)
  60. #
  61. #   Font:           Result:
  62. #     \fn[x]        - Sets the font name to x.
  63. #     \fz[x]        - Sets the font size to x.
  64. #     \fb           - Toggles font's bold setting.
  65. #     \fi           - Toggles font's italic setting.
  66. #     \fo           - Toggles font's outline.
  67. #     \fs           - Toggles font's shadow.
  68. #     \fr           - Resets all font changes.
  69. #
  70. #   Actor:          Result:
  71. #     \ac[x]        - The name of actor x's class.
  72. #
  73. #     Note: Using 0 or a negative number for x  will use party index instead
  74. #           of actor ID.
  75. #
  76. #   Names:          Result:
  77. #     \nc[x]        - The name of class x.
  78. #     \ni[x]        - The name of item x.
  79. #     \nw[x]        - The name of weapon x.
  80. #     \na[x]        - The name of armour x.
  81. #     \nt[x]        - The name of state x.
  82. #
  83. #   Icon Names:     Result:
  84. #     \ii[x]        - The name of item x, with icon.
  85. #     \iw[x]        - The name of weapon x, with icon.
  86. #     \ia[x]        - The name of armour x, with icon.
  87. #     \is[x]        - The name of skill x, with icon.
  88. #==============================================================================
  89. #
  90. #  Special thanks to Yanfly for the codes used in the VX Ace message system.
  91. #
  92. #==============================================================================
  93.  
  94. class Interpreter
  95.   #--------------------------------------------------------------------------
  96.   # * Show Text (overwrite)
  97.   #--------------------------------------------------------------------------
  98.   def command_101
  99.     # If other text has been set to message_text
  100.     if $game_temp.message_text != nil
  101.       # End
  102.       return false
  103.     end
  104.     # Set message end waiting flag and callback
  105.     @message_waiting = true
  106.     $game_temp.message_proc = Proc.new { @message_waiting = false }
  107.     # Set message text on first line
  108.     $game_temp.message_text = @list[@index].parameters[0] + "\n"
  109.     line_count = 1
  110.     # Loop
  111.     loop do
  112.       # Workaround for lines in a box that don't fit in the limit
  113.       if @list[@index+1].code == 401 && line_count >= TLB_MSG.rows_for_text
  114.           @list.insert(@index+2, RPG::EventCommand.new(101, @list[@index+1].indent, @list[@index+1].parameters))
  115.           @list.delete_at(@index+1)
  116.           return true
  117.       # If next event command text is on the second line or after
  118.       elsif @list[@index+1].code == 401 || (@list[@index+1].code == 101 && TLB_MSG.rows_for_text >= 4)
  119.         # Add the second line or after to message_text
  120.         #if @list[@index+1].code == 401
  121.           $game_temp.message_text += @list[@index+1].parameters[0] + "\n"
  122.           line_count += 1
  123.         #end
  124.       # If event command is not on the second line or after
  125.       else
  126.         # If next event command is show choices
  127.         if @list[@index+1].code == 102
  128.           # If choices fit on screen
  129.           if @list[@index+1].parameters[0].size <= 4 - line_count
  130.             # Advance index
  131.             @index += 1
  132.             # Choices setup
  133.             $game_temp.choice_start = line_count
  134.             setup_choices(@list[@index].parameters)
  135.           end
  136.         # If next event command is input number
  137.         elsif @list[@index+1].code == 103
  138.           # If number input window fits on screen
  139.           if line_count < 4
  140.             # Advance index
  141.             @index += 1
  142.             # Number input setup
  143.             $game_temp.num_input_start = line_count
  144.             $game_temp.num_input_variable_id = @list[@index].parameters[0]
  145.             $game_temp.num_input_digits_max = @list[@index].parameters[1]
  146.           end
  147.         end
  148.         # Continue
  149.         return true
  150.       end
  151.       # Advance index
  152.       return true if line_count > TLB_MSG.rows_for_text
  153.       @index += 1
  154.     end
  155.   end
  156. end
  157.  
  158. class Bitmap
  159.   def draw_outline_text(*args)
  160.     if args[0].is_a?(Rect)
  161.       x, y, width, height = args[0].x, args[0].y, args[0].width, args[0].height
  162.       text = args[1]
  163.       alignment = args[2] ? args[2] : 0
  164.     else
  165.       x, y, width, height, text = args
  166.       alignment = args[5] ? args[5] : 0
  167.     end
  168.     draw_text(x+1, y, width, height, text, alignment)
  169.     draw_text(x-1, y, width, height, text, alignment)
  170.     draw_text(x, y+1, width, height, text, alignment)
  171.     draw_text(x, y-1, width, height, text, alignment)
  172.     draw_text(x, y, width, height, text, alignment)
  173.   end
  174. end
  175.  
  176. class Window_Name < Window_Base
  177.   #--------------------------------------------------------------------------
  178.   # * Object Initialization
  179.   #--------------------------------------------------------------------------
  180.   def initialize(message_window, text, position)
  181.     @message_window = message_window
  182.     width = @message_window.contents.text_size(text).width + 40
  183.     x = @message_window.x
  184.     case position
  185.     when 1:
  186.       x = @message_window.x + @message_window.width - @message_window.width / 2 - width / 2
  187.     when 2;
  188.       x = @message_window.x + @message_window.width - width
  189.     end
  190.     super(x, @message_window.y - 64, width, 64)
  191.     self.contents = Bitmap.new(width - 32, height - 32)
  192.     self.back_opacity = 160
  193.     @text = text.clone
  194.     @position = position
  195.     refresh
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # * Refresh
  199.   #--------------------------------------------------------------------------
  200.   def refresh
  201.     self.contents.clear
  202.     self.contents.font.color = normal_color
  203.     self.contents.draw_text(4, 0, 120, 32, @text.to_s)
  204.   end
  205. end
  206.  
  207. class Window_Message < Window_Selectable
  208.   def create_name_window(text, position)
  209.     if @name_window == nil
  210.       @name_window = Window_Name.new(self, text, position)
  211.     end
  212.   end
  213.  
  214.   def reset_font_settings
  215.     self.contents.font.name = Font.default_name
  216.     self.contents.font.size = Font.default_size
  217.     self.contents.font.bold = Font.default_bold
  218.     self.contents.font.italic = Font.default_italic
  219.     self.contents.font.color = Font.default_color
  220.   end
  221.  
  222.   def strip_string(text)
  223.     return text.sub!(/\[(.*)\]/, "")
  224.   end
  225.  
  226.   def refresh
  227.     self.height = TLB_MSG.rows_for_text * 32 + 32
  228.     self.y = 480 - self.height - 16
  229.     if @name_window != nil
  230.       @name_window.y = self.y - 64
  231.     end
  232.     self.contents = Bitmap.new(self.width - 32, self.height - 32)
  233.     self.contents.clear
  234.     self.contents.font.color = normal_color
  235.     x = y = 0
  236.     y_override = nil
  237.     outline = false
  238.     shadow = false
  239.     out_color = Color.new(0, 0, 0, 255)
  240.     @cursor_width = 0
  241.     # Indent if choice
  242.     if $game_temp.choice_start == 0
  243.       x = 8
  244.     end
  245.     # If waiting for a message to be displayed
  246.     if $game_temp.message_text != nil
  247.       text = $game_temp.message_text
  248.       # Control text processing
  249.       begin
  250.         last_text = text.clone
  251.         text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
  252.       end until text == last_text
  253.       # Change "\\\\" to "\000" for convenience
  254.       text.gsub!(/\\\\/) { "\000" }
  255.       text.gsub!(/\\[Nn]\[([0-9]+)\]/) do
  256.         $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
  257.       end
  258.       text.gsub!(/\\[Pp]\[([0-9]+)\]/) do
  259.         $game_party.actors[$1.to_i] != nil ? $game_party.actors[$1.to_i].name : ""
  260.       end
  261.       text.gsub!(/\\[Gg]/) do
  262.         $data_system.words.gold
  263.       end
  264.       text.gsub!(/\\ac\[(-?[0-9]+)\]/) do
  265.         id = $1.to_i
  266.         if id > 0 && $game_actors[id] != nil
  267.           $data_classes[$game_actors[id].class_id].name
  268.         elsif id <= 0 && $game_party.actors[-id] != nil
  269.           $data_classes[$game_party.actors[-id].class_id].name
  270.         else
  271.           ""
  272.         end
  273.       end
  274.       text.gsub!(/\\nc\[([0-9]+)\]/) do
  275.         id = $1.to_i
  276.         $data_classes[id] != nil ? $data_classes[id].name : ""
  277.       end
  278.       text.gsub!(/\\ni\[([0-9]+)\]/) do
  279.         id = $1.to_i
  280.         $data_items[id] != nil ? $data_items[id].name : ""
  281.       end
  282.       text.gsub!(/\\nw\[([0-9]+)\]/) do
  283.         id = $1.to_i
  284.         $data_weapons[id] != nil ? $data_weapons[id].name : ""
  285.       end
  286.       text.gsub!(/\\na\[([0-9]+)\]/) do
  287.         id = $1.to_i
  288.         $data_armors[id] != nil ? $data_armors[id].name : ""
  289.       end
  290.       text.gsub!(/\\ns\[([0-9]+)\]/) do
  291.         id = $1.to_i
  292.         $data_skills[id] != nil ? $data_skills[id].name : ""
  293.       end
  294.       text.gsub!(/\\nt\[([0-9]+)\]/) do
  295.         id = $1.to_i
  296.         $data_states[id] != nil ? $data_states[id].name : ""
  297.       end
  298.       # Change "\\C" to "\001" and "\\G" to "\002"
  299.       text.gsub!(/\\[Cc]\[([0-9]+)\]/) { "\001[#{$1}]" }
  300.       text.gsub!(/\\\$/) { "\002" }
  301.       text.gsub!(/\\[Ii]\[(.*)\]/) { "\003[#{$1}]" }
  302.       text.gsub!(/\\\{/) { "\004" }
  303.       text.gsub!(/\\\}/) { "\005" }
  304.       text.gsub!(/\\n\<(.*)\>/) { "\006[#{$1}]" }
  305.       text.gsub!(/\\nc\<(.*)\>/) { "\007[#{$1}]" }
  306.       text.gsub!(/\\nr\<(.*)\>/) { "\010[#{$1}]" }
  307.       text.gsub!(/\\px\[([0-9]+)\]/) { "\013[#{$1}]" }
  308.       text.gsub!(/\\py\[([0-9]+)\]/) { "\014[#{$1}]" }
  309.       text.gsub!(/\\pic\[(.*)\]/) { "\015[#{$1}]" }
  310.       text.gsub!(/\\oc\[([0-9]+)\]/) { "\016[#{$1}]" }
  311.       text.gsub!(/\\oo\[([0-9]+)\]/) { "\017[#{$1}]" }
  312.       text.gsub!(/\\fr/) { "\020" }
  313.       text.gsub!(/\\fz\[([0-9]+)\]/) { "\021[#{$1}]" }
  314.       text.gsub!(/\\fn\[(.*)\]/) { "\022[#{$1}]" }
  315.       text.gsub!(/\\fb/) { "\023" }
  316.       text.gsub!(/\\fi/) { "\024" }
  317.       text.gsub!(/\\fo/) { "\025" }
  318.       text.gsub!(/\\fs/) { "\026" }
  319.       text.gsub!(/\\ii\[([0-9]+)\]/) { "\027[#{$1}]" }
  320.       text.gsub!(/\\iw\[([0-9]+)\]/) { "\030[#{$1}]" }
  321.       text.gsub!(/\\ia\[([0-9]+)\]/) { "\031[#{$1}]" }
  322.       text.gsub!(/\\is\[([0-9]+)\]/) { "\032[#{$1}]" }
  323.       # Get 1 text character in c (loop until unable to get text)
  324.       while ((c = text.slice!(/./m)) != nil)
  325.         # If \\
  326.         if c == "\000"
  327.           # Return to original text
  328.           c = "\\"
  329.         end
  330.         # If \C[n]
  331.         if c == "\001"
  332.           # Change text color
  333.           text.sub!(/\[([0-9]+)\]/, "")
  334.           color = $1.to_i
  335.           if color >= 0 and color <= 7
  336.             self.contents.font.color = text_color(color)
  337.           end
  338.           # go to next text
  339.           next
  340.         end
  341.         # If \G
  342.         if c == "\002"
  343.           # Make gold window
  344.           if @gold_window == nil
  345.             @gold_window = Window_Gold.new
  346.             @gold_window.x = 560 - @gold_window.width
  347.             if $game_temp.in_battle
  348.               @gold_window.y = 192
  349.             else
  350.               @gold_window.y = self.y >= 128 ? 32 : 384
  351.             end
  352.             @gold_window.opacity = self.opacity
  353.             @gold_window.back_opacity = self.back_opacity
  354.           end
  355.           # go to next text
  356.           next
  357.         end
  358.         if c == "\003"
  359.           text.sub!(/\[(.*)\]/, "")
  360.           icon_name = $1.to_s
  361.           bitmap = RPG::Cache.icon(icon_name)
  362.           self.contents.blt(x, 32 * y + 4, bitmap, Rect.new(0, 0, 24, 24))
  363.           x += 12
  364.           next
  365.         end
  366.         if c == "\004"
  367.           self.contents.font.size += 8 if self.contents.font.size <= 64
  368.           next
  369.         end
  370.         if c == "\005"
  371.           self.contents.font.size -= 8 if self.contents.font.size >= 16
  372.           next
  373.         end
  374.         if c == "\006"
  375.           text.sub!(/\[(.*)\]/, "")
  376.           name_text = $1.to_s
  377.           create_name_window(name_text, 0)
  378.           next
  379.         end
  380.         if c == "\007"
  381.           text.sub!(/\[(.*)\]/, "")
  382.           name_text = $1.to_s
  383.           create_name_window(name_text, 1)
  384.           next
  385.         end
  386.         if c == "\010"
  387.           text.sub!(/\[(.*)\]/, "")
  388.           name_text = $1.to_s
  389.           create_name_window(name_text, 2)
  390.           next
  391.         end
  392.         if c == "\013"
  393.           text.sub!(/\[([0-9]+)\]/, "")
  394.           x = $1.to_i
  395.           next
  396.         end
  397.         if c == "\014"
  398.           text.sub!(/\[([0-9]+)\]/, "")
  399.           y_override = $1.to_i
  400.           next
  401.         end
  402.         if c == "\015"
  403.           text.sub!(/\[(.*)\]/, "")
  404.           bitmap = RPG::Cache.picture($1.to_s)
  405.           cw = bitmap.width
  406.           ch = bitmap.height
  407.           src_rect = Rect.new(0, 0, cw, ch)
  408.           self.contents.blt(x, y, bitmap, src_rect)
  409.           next
  410.         end
  411.         if c == "\016"
  412.           text.sub!(/\[([0-9]+)\]/, "")
  413.           outline = true
  414.           out_color = text_color($1.to_i)
  415.           next
  416.         end
  417.         if c == "\017"
  418.           text.sub!(/\[([0-9]+)\]/, "")
  419.           outline = true
  420.           out_color.alpha = $1.to_i
  421.           next
  422.         end
  423.         if c == "\020"
  424.           reset_font_settings
  425.           outline = false
  426.           shadow = false
  427.           out_color = Color.new(0, 0, 0, 255)
  428.           next
  429.         end
  430.         if c == "\021"
  431.           text.sub!(/\[([0-9]+)\]/, "")
  432.           self.contents.font.size = $1.to_i
  433.           next
  434.         end
  435.         if c == "\022"
  436.           text.sub!(/\[(.*)\]/, "")
  437.           self.contents.font.name = $1.to_s
  438.           next
  439.         end
  440.         if c == "\023"
  441.           self.contents.font.bold = !self.contents.font.bold
  442.           next
  443.         end
  444.         if c == "\024"
  445.           self.contents.font.italic = !self.contents.font.italic
  446.           next
  447.         end
  448.         if c == "\025"
  449.           outline = !outline
  450.           next
  451.         end
  452.         if c == "\026"
  453.           shadow = !shadow
  454.           next
  455.         end
  456.         if c == "\027"
  457.           text.sub!(/\[([0-9]+)\]/, "")
  458.           item = $data_items[$1.to_i]
  459.           if item != nil
  460.             bitmap = RPG::Cache.icon(item.icon_name)
  461.             self.contents.blt(x, (y_override || (32 * y)) + 4, bitmap, Rect.new(0, 0, 24, 24))
  462.             text = item.name + text
  463.             x += 24
  464.           end
  465.           next
  466.         end
  467.         if c == "\030"
  468.           text.sub!(/\[([0-9]+)\]/, "")
  469.           item = $data_weapons[$1.to_i]
  470.           if item != nil
  471.             bitmap = RPG::Cache.icon(item.icon_name)
  472.             self.contents.blt(x, (y_override || (32 * y)) + 4, bitmap, Rect.new(0, 0, 24, 24))
  473.             text = item.name + text
  474.             x += 24
  475.           end
  476.           next
  477.         end
  478.         if c == "\031"
  479.           text.sub!(/\[([0-9]+)\]/, "")
  480.           item = $data_armors[$1.to_i]
  481.           if item != nil
  482.             bitmap = RPG::Cache.icon(item.icon_name)
  483.             self.contents.blt(x, (y_override || (32 * y)) + 4, bitmap, Rect.new(0, 0, 24, 24))
  484.             text = item.name + text
  485.             x += 24
  486.           end
  487.           next
  488.         end
  489.         if c == "\032"
  490.           text.sub!(/\[([0-9]+)\]/, "")
  491.           item = $data_skills[$1.to_i]
  492.           if item != nil
  493.             bitmap = RPG::Cache.icon(item.icon_name)
  494.             self.contents.blt(x, (y_override || (32 * y)) + 4, bitmap, Rect.new(0, 0, 24, 24))
  495.             text = item.name + text
  496.             x += 24
  497.           end
  498.           next
  499.         end
  500.         # If new line text
  501.         if c == "\n"
  502.           # Update cursor width if choice
  503.           if y >= $game_temp.choice_start
  504.             @cursor_width = [@cursor_width, x].max
  505.           end
  506.           # Add 1 to y
  507.           y += 1
  508.           x = 0
  509.           # Indent if choice
  510.           if y >= $game_temp.choice_start
  511.             x = 8
  512.           end
  513.           y_override += 32 if y_override
  514.           # go to next text
  515.           next
  516.         end
  517.         # Draw text
  518.         if shadow
  519.           color = self.contents.font.color.dup
  520.           self.contents.font.color = Color.new(0, 0, 0)
  521.           self.contents.draw_text(4 + x, (y_override || (32 * y)) + 1, 40, 32, c)
  522.           self.contents.font.color = color
  523.         end
  524.         if outline
  525.           color = self.contents.font.color.dup
  526.           self.contents.font.color = out_color
  527.           self.contents.draw_outline_text(4 + x, y_override || (32 * y), 40, 32, c)
  528.           self.contents.font.color = color
  529.         else
  530.           self.contents.draw_text(4 + x, y_override || 32 * y, 40, 32, c)
  531.         end
  532.         # Add x to drawn text width
  533.         x += self.contents.text_size(c).width
  534.       end
  535.     end
  536.     # If choice
  537.     if $game_temp.choice_max > 0
  538.       @item_max = $game_temp.choice_max
  539.       self.active = true
  540.       self.index = 0
  541.     end
  542.     # If number input
  543.     if $game_temp.num_input_variable_id > 0
  544.       digits_max = $game_temp.num_input_digits_max
  545.       number = $game_variables[$game_temp.num_input_variable_id]
  546.       @input_number_window = Window_InputNumber.new(digits_max)
  547.       @input_number_window.number = number
  548.       @input_number_window.x = self.x + 8
  549.       @input_number_window.y = self.y + $game_temp.num_input_start * 32
  550.     end
  551.   end
  552.  
  553.   alias :tlb_msg_update :update unless $@
  554.   def update
  555.     tlb_msg_update
  556.     if !self.visible && @name_window != nil
  557.       @name_window.dispose
  558.       @name_window = nil
  559.     end
  560.   end
  561. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement