Advertisement
Guest User

Messages

a guest
Sep 24th, 2014
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 42.17 KB | None | 0 0
  1. class Game_Temp
  2.   attr_writer :message_window_showing
  3.   attr_writer :player_transferring
  4.   attr_writer :transition_processing
  5.  
  6.   def message_window_showing
  7.     @message_window_showing=false if !@message_window_showing
  8.     return @message_window_showing
  9.   end
  10.  
  11.   def player_transferring
  12.     @player_transferring=false if !@player_transferring
  13.     return @player_transferring
  14.   end
  15.  
  16.   def transition_processing
  17.     @transition_processing=false if !@transition_processing
  18.     return @transition_processing
  19.   end
  20. end
  21.  
  22.  
  23.  
  24. class Game_Message
  25.   attr_writer :background, :visible
  26.   def visible; return @visible ? @visible : false; end
  27.   def background; return @background ? @background : 0; end
  28. end
  29.  
  30.  
  31.  
  32. class Game_System
  33.   attr_writer :message_position
  34.  
  35.   def message_position
  36.     @message_position=2 if !@message_position
  37.     return @message_position
  38.   end
  39. end
  40.  
  41.  
  42.  
  43. #########
  44.  
  45. class Scene_Map
  46.   def updatemini
  47.     oldmws=$game_temp.message_window_showing
  48.     oldvis=$game_message ? $game_message.visible : false
  49.     $game_temp.message_window_showing=true
  50.     $game_message.visible=true if $game_message
  51.     loop do
  52.       $game_map.update
  53.       $game_player.update
  54.       $game_system.update
  55.       if $game_screen
  56.         $game_screen.update
  57.       else
  58.         $game_map.screen.update
  59.       end
  60.       unless $game_temp.player_transferring
  61.         break
  62.       end
  63.       transfer_player
  64.       if $game_temp.transition_processing
  65.         break
  66.       end
  67.     end
  68.     $game_temp.message_window_showing=oldmws
  69.     $game_message.visible=oldvis if $game_message
  70.     @spriteset.update if @spriteset
  71.     @message_window.update if @message_window
  72.   end
  73. end
  74.  
  75.  
  76.  
  77. class Scene_Battle
  78.   def updatemini
  79.     if self.respond_to?("update_basic")
  80.       update_basic(true)
  81.       update_info_viewport                  # Update information viewport
  82.       if $game_message && $game_message.visible
  83.         @info_viewport.visible = false
  84.         @message_window.visible = true
  85.       end
  86.     else
  87.       oldmws=$game_temp.message_window_showing
  88.       $game_temp.message_window_showing=true
  89.       # Update system (timer) and screen
  90.       $game_system.update
  91.       if $game_screen
  92.         $game_screen.update
  93.       else
  94.         $game_map.screen.update
  95.       end    
  96.       # If timer has reached 0
  97.       if $game_system.timer_working and $game_system.timer == 0
  98.         # Abort battle
  99.         $game_temp.battle_abort = true
  100.       end
  101.       # Update windows
  102.       @help_window.update if @help_window
  103.       @party_command_window.update if @party_command_window
  104.       @actor_command_window.update if @actor_command_window
  105.       @status_window.update if @status_window
  106.       $game_temp.message_window_showing=oldmws
  107.       @message_window.update if @message_window
  108.       # Update sprite set
  109.       @spriteset.update if @spriteset
  110.     end
  111.   end
  112. end
  113.  
  114.  
  115.  
  116. def pbMapInterpreterRunning?
  117.   interp=pbMapInterpreter
  118.   return interp && interp.running?
  119. end
  120.  
  121. def pbMapInterpreter
  122.   if $game_map && $game_map.respond_to?("interpreter")
  123.     return $game_map.interpreter
  124.   elsif $game_system
  125.     return $game_system.map_interpreter
  126.   end
  127.   return nil
  128. end
  129.  
  130. def pbRefreshSceneMap
  131.   if $scene && $scene.is_a?(Scene_Map)
  132.     if $scene.respond_to?("miniupdate")
  133.       $scene.miniupdate
  134.     else
  135.       $scene.updatemini
  136.     end
  137.   elsif $scene && $scene.is_a?(Scene_Battle)
  138.     $scene.updatemini
  139.   end
  140. end
  141.  
  142. def pbUpdateSceneMap
  143.   if $scene && $scene.is_a?(Scene_Map) && !pbIsFaded?
  144.     if $scene.respond_to?("miniupdate")
  145.       $scene.miniupdate
  146.     else
  147.       $scene.updatemini
  148.     end
  149.   elsif $scene && $scene.is_a?(Scene_Battle)
  150.     $scene.updatemini
  151.   end
  152. end
  153. #########
  154.  
  155. def pbCsvField!(str)
  156.   ret=""
  157.   str.sub!(/\A\s*/,"")
  158.   if str[0,1]=="\""
  159.     str[0,1]=""
  160.     escaped=false
  161.     fieldbytes=0
  162.     str.scan(/./) do |s|
  163.       fieldbytes+=s.length
  164.       break if s=="\"" && !escaped
  165.       if s=="\\" && !escaped
  166.         escaped=true
  167.       else
  168.         ret+=s
  169.         escaped=false
  170.       end
  171.     end
  172.     str[0,fieldbytes]=""
  173.     if !str[/\A\s*,/] && !str[/\A\s*$/]
  174.       raise _INTL("Invalid quoted field (in: {1})",ret)
  175.     end
  176.     str[0,str.length]=$~.post_match
  177.   else
  178.     if str[/,/]
  179.       str[0,str.length]=$~.post_match
  180.       ret=$~.pre_match
  181.     else
  182.       ret=str.clone
  183.       str[0,str.length]=""
  184.     end
  185.     ret.gsub!(/\s+$/,"")
  186.   end
  187.   return ret
  188. end
  189.  
  190. def pbCsvPosInt!(str)
  191.   ret=pbCsvField!(str)
  192.   if !ret[/\A\d+$/]
  193.     raise _INTL("Field {1} is not a positive integer",ret)
  194.   end
  195.   return ret.to_i
  196. end
  197.  
  198. def pbEventCommentInput(*args)
  199.   parameters = []
  200.   list = *args[0].list # Event or event page
  201.   elements = *args[1] # Number of elements
  202.   trigger = *args[2] # Trigger
  203.   return nil if list == nil
  204.   return nil unless list.is_a?(Array)
  205.   for item in list
  206.     next unless item.code == 108 || item.code == 408
  207.     if item.parameters[0] == trigger
  208.       start = list.index(item) + 1
  209.       finish = start + elements
  210.       for id in start...finish
  211.         next if !list[id]
  212.         parameters.push(list[id].parameters[0])
  213.       end
  214.       return parameters
  215.     end
  216.   end
  217.   return nil
  218. end
  219.  
  220. # Gets the value of a variable.
  221. def pbGet(id)
  222.   return 0 if !id || !$game_variables
  223.   return $game_variables[id]
  224. end
  225.  
  226. # Sets the value of a variable.
  227. def pbSet(id,value)
  228.   if id && id>=0
  229.     $game_variables[id]=value if $game_variables
  230.     $game_map.need_refresh = true if $game_map
  231.   end
  232. end
  233.  
  234. def pbCurrentEventCommentInput(elements,trigger)
  235.   return nil if !pbMapInterpreterRunning?
  236.   event=pbMapInterpreter.get_character(0)
  237.   return nil if !event
  238.   return pbEventCommentInput(event,elements,trigger)
  239. end
  240.  
  241.  
  242.  
  243. module InterpreterMixin
  244.   def pbGlobalLock # Freezes all events on the map (for use at the beginning of common events)
  245.     for event in $game_map.events.values
  246.       event.minilock
  247.     end
  248.   end
  249.  
  250.   def pbGlobalUnlock # Unfreezes all events on the map (for use at the end of common events)
  251.     for event in $game_map.events.values
  252.       event.unlock
  253.     end
  254.   end
  255.  
  256.   def pbRepeatAbove(index)
  257.     index=@list[index].indent
  258.     loop do
  259.       index-=1
  260.       if @list[index].indent==indent
  261.         return index+1
  262.       end
  263.     end
  264.   end
  265.  
  266.   def pbBreakLoop(index)
  267.     indent = @list[index].indent
  268.     temp_index=index
  269.     # Copy index to temporary variables
  270.     loop do
  271.       # Advance index
  272.       temp_index += 1
  273.       # If a fitting loop was not found
  274.       if temp_index >= @list.size-1
  275.         return index+1
  276.       end
  277.       if @list[temp_index].code == 413 and @list[temp_index].indent < indent
  278.         return temp_index+1
  279.       end
  280.     end
  281.   end
  282.  
  283.   def pbJumpToLabel(index,label_name)
  284.     temp_index = 0
  285.     loop do
  286.       if temp_index >= @list.size-1
  287.         return index+1
  288.       end
  289.       if @list[temp_index].code == 118 and
  290.          @list[temp_index].parameters[0] == label_name
  291.         return temp_index+1
  292.       end
  293.       temp_index += 1
  294.     end
  295.   end
  296.  
  297. # Gets the next index in the interpreter, ignoring
  298. # certain events between messages
  299.   def pbNextIndex(index)
  300.     return -1 if !@list || @list.length==0
  301.     i=index+1
  302.     loop do
  303.       if i>=@list.length-1
  304.         return i
  305.       end
  306.       code=@list[i].code
  307.       case code
  308.         when 118, 108, 408 # Label, Comment
  309.           i+=1
  310.         when 413 # Repeat Above
  311.           i=pbRepeatAbove(i)
  312.         when 113 # Break Loop
  313.           i=pbBreakLoop(i)
  314.         when 119 # Jump to Label
  315.           newI=pbJumpToLabel(i,@list[i].parameters[0])
  316.           if newI>i
  317.             i=newI
  318.           else
  319.             i+=1
  320.           end
  321.         else
  322.           return i
  323.       end    
  324.     end
  325.   end
  326.  
  327. # Helper function that shows a picture in a script.  To be used in
  328. # a script event command.
  329.   def pbShowPicture(number,name,origin,x,y,zoomX=100,zoomY=100,opacity=255,blendType=0)
  330.     number = number + ($game_temp.in_battle ? 50 : 0)
  331.     $game_screen.pictures[number].show(name,origin,
  332.        x, y, zoomX,zoomY,opacity,blendType)
  333.   end
  334.  
  335. # Erases an event and adds it to the list of erased events so that
  336. # it can stay erased when the game is saved then loaded again.  To be used in
  337. # a script event command.
  338.   def pbEraseThisEvent
  339.     if $game_map.events[@event_id]
  340.       $game_map.events[@event_id].erase
  341.       $PokemonMap.addErasedEvent(@event_id) if $PokemonMap
  342.     end
  343.     @index+=1
  344.     return true
  345.   end
  346.  
  347. # Runs a common event.  To be used in a script event command.
  348.   def pbCommonEvent(id)
  349.     if $game_temp.in_battle
  350.       $game_temp.common_event_id = id
  351.     else
  352.       commonEvent = $data_common_events[id]
  353.       $game_system.battle_interpreter.setup(commonEvent.list, 0)
  354.     end
  355.   end
  356.  
  357. # Sets another event's self switch (eg. pbSetSelfSwitch(20,"A",true) ).
  358. # To be used in a script event command.
  359.   def pbSetSelfSwitch(event,swtch,value)
  360.     $game_self_switches[[@map_id,event,swtch]]=value
  361.     $game_map.need_refresh = true
  362.   end
  363.  
  364. # Must use this approach to share the methods because the methods already
  365. # defined in a class override those defined in an included module
  366.   CustomEventCommands=<<_END_
  367.  
  368.   def command_242
  369.     pbBGMFade(pbParams[0])
  370.     return true
  371.   end
  372.  
  373.   def command_246
  374.     pbBGSFade(pbParams[0])
  375.     return true
  376.   end
  377.  
  378.   def command_251
  379.     pbSEStop()
  380.     return true
  381.   end
  382.  
  383.   def command_241
  384.     pbBGMPlay(pbParams[0])
  385.     return true
  386.   end
  387.  
  388.   def command_245
  389.     pbBGSPlay(pbParams[0])
  390.     return true
  391.   end
  392.  
  393.   def command_249
  394.     pbMEPlay(pbParams[0])
  395.     return true
  396.   end
  397.  
  398.   def command_250
  399.     pbSEPlay(pbParams[0])
  400.     return true
  401.   end
  402. _END_
  403. end
  404.  
  405.  
  406.  
  407. def pbButtonInputProcessing(variableNumber=0,timeoutFrames=0)
  408.   ret=0
  409.   loop do
  410.     Graphics.update
  411.     Input.update
  412.     pbUpdateSceneMap
  413.     for i in 1..18
  414.       if Input.trigger?(i)
  415.         ret=i
  416.       end
  417.     end
  418.     break if ret!=0
  419.     if timeoutFrames && timeoutFrames>0
  420.       i+=1
  421.       break if i>=timeoutFrames
  422.     end
  423.   end
  424.   Input.update
  425.   if variableNumber && variableNumber>0
  426.     $game_variables[variableNumber]=ret
  427.     $game_map.need_refresh = true if $game_map
  428.   end
  429.   return ret
  430. end
  431.  
  432.  
  433.  
  434. class Game_Temp
  435.   attr_accessor :background
  436. end
  437.  
  438.  
  439.  
  440. class Game_Interpreter
  441.   include InterpreterMixin
  442.   eval(InterpreterMixin::CustomEventCommands)
  443.   @@immediateDisplayAfterWait=false
  444.   @buttonInput=false
  445.  
  446.   def pbParams
  447.     return @params
  448.   end
  449.  
  450.   def command_105
  451.     return false if @buttonInput
  452.     @buttonInput=true
  453.     pbButtonInputProcessing(@list[@index].parameters[0])
  454.     @buttonInput=false
  455.     @index+=1
  456.     return true
  457.   end
  458.  
  459.   def command_101
  460.     if $game_temp.message_window_showing
  461.       return false
  462.     end
  463.     $game_message=Game_Message.new if !$game_message
  464.     message=""
  465.     commands=nil
  466.     numInputVar=nil
  467.     numInputDigitsMax=nil
  468.     text=""
  469.     facename=@list[@index].parameters[0]
  470.     faceindex=@list[@index].parameters[1]
  471.     if facename && facename!=""
  472.       text+="\\ff[#{facename},#{faceindex}]"
  473.     end
  474.     if $game_message
  475.       $game_message.background=@list[@index].parameters[2]
  476.     end
  477.     $game_system.message_position=@list[@index].parameters[3]
  478.     message+=text
  479.     messageend=""
  480.     loop do
  481.       nextIndex=pbNextIndex(@index)
  482.       code=@list[nextIndex].code
  483.       if code == 401
  484.         text=@list[nextIndex].parameters[0]
  485.         text+=" " if text!="" && text[text.length-1,1]!=" "
  486.         message+=text
  487.         @index=nextIndex
  488.       else
  489.         if code == 102
  490.           commands=@list[nextIndex].parameters
  491.           @index=nextIndex
  492.         elsif code == 106 && @@immediateDisplayAfterWait
  493.           params=@list[nextIndex].parameters
  494.           if params[0]<=10
  495.             nextcode=@list[nextIndex+1].code
  496.             if nextcode==101||nextcode==102||nextcode==103
  497.               @index=nextIndex
  498.             else
  499.               break
  500.             end
  501.           else
  502.             break
  503.           end
  504.         elsif code == 103
  505.           numInputVar=@list[nextIndex].parameters[0]
  506.           numInputDigitsMax=@list[nextIndex].parameters[1]
  507.           @index=nextIndex
  508.         elsif code == 101
  509.           messageend="\1"
  510.         end
  511.         break
  512.       end
  513.     end
  514.     message=_MAPINTL($game_map.map_id,message)
  515.     @message_waiting=true
  516.     if commands
  517.       cmdlist=[]
  518.       for cmd in commands[0]
  519.         cmdlist.push(_MAPINTL($game_map.map_id,cmd))
  520.       end
  521.       command=Kernel.pbMessage(message+messageend,cmdlist,commands[1])
  522.       @branch[@list[@index].indent] = command
  523.     elsif numInputVar
  524.       params=ChooseNumberParams.new
  525.       params.setMaxDigits(numInputDigitsMax)
  526.       params.setDefaultValue($game_variables[numInputVar])
  527.       $game_variables[numInputVar]=Kernel.pbMessageChooseNumber(message+messageend,params)
  528.       $game_map.need_refresh = true if $game_map
  529.     else
  530.       Kernel.pbMessage(message+messageend)
  531.     end
  532.     @message_waiting=false
  533.     return true
  534.   end
  535.  
  536.   def command_102
  537.     @message_waiting=true
  538.     command=Kernel.pbShowCommands(nil,@list[@index].parameters[0],@list[@index].parameters[1])
  539.     @message_waiting=false
  540.     @branch[@list[@index].indent] = command
  541.     Input.update # Must call Input.update again to avoid extra triggers
  542.     return true
  543.   end
  544.  
  545.   def command_103
  546.     varnumber=@list[@index].parameters[0]
  547.     @message_waiting=true
  548.     params=ChooseNumberParams.new
  549.     params.setMaxDigits(@list[@index].parameters[1])
  550.     params.setDefaultValue($game_variables[varnumber])
  551.     $game_variables[varnumber]=Kernel.pbChooseNumber(nil,params)
  552.     $game_map.need_refresh = true if $game_map
  553.     @message_waiting=false
  554.     return true
  555.   end
  556. end
  557.  
  558.  
  559.  
  560. class Interpreter
  561.   include InterpreterMixin
  562.   eval(InterpreterMixin::CustomEventCommands)
  563.   @@immediateDisplayAfterWait=false
  564.   @buttonInput=false
  565.  
  566.   def pbParams
  567.     return @parameters
  568.   end
  569.  
  570.   def command_105
  571.     return false if @buttonInput
  572.     @buttonInput=true
  573.     pbButtonInputProcessing(@list[@index].parameters[0])
  574.     @buttonInput=false
  575.     @index+=1
  576.     return true
  577.   end
  578.  
  579.   def command_101
  580.     if $game_temp.message_window_showing
  581.       return false
  582.     end
  583.     message=""
  584.     commands=nil
  585.     numInputVar=nil
  586.     numInputDigitsMax=nil
  587.     text=""
  588.     firstText=nil
  589.     if @list[@index].parameters.length==1
  590.       text+=@list[@index].parameters[0]
  591.       firstText=@list[@index].parameters[0]
  592.       text+=" " if text[text.length-1,1]!=" "
  593.       message+=text
  594.     else
  595.       facename=@list[@index].parameters[0]
  596.       faceindex=@list[@index].parameters[1]
  597.       if facename && facename!=""
  598.         text+="\\ff[#{facename},#{faceindex}]"
  599.         message+=text
  600.       end
  601.     end
  602.     messageend=""
  603.     loop do
  604.       nextIndex=pbNextIndex(@index)
  605.       code=@list[nextIndex].code
  606.       if code == 401
  607.         text=@list[nextIndex].parameters[0]
  608.         text+=" " if text[text.length-1,1]!=" "
  609.         message+=text
  610.         @index=nextIndex
  611.       else
  612.         if code == 102
  613.           commands=@list[nextIndex].parameters
  614.           @index=nextIndex
  615.         elsif code == 106 && @@immediateDisplayAfterWait
  616.           params=@list[nextIndex].parameters
  617.           if params[0]<=10
  618.             nextcode=@list[nextIndex+1].code
  619.             if nextcode==101||nextcode==102||nextcode==103
  620.               @index=nextIndex
  621.             else
  622.               break
  623.             end
  624.           else
  625.             break
  626.           end
  627.         elsif code == 103
  628.           numInputVar=@list[nextIndex].parameters[0]
  629.           numInputDigitsMax=@list[nextIndex].parameters[1]
  630.           @index=nextIndex
  631.         elsif code == 101
  632.           if @list[@index].parameters.length==1
  633.             text=@list[@index].parameters[0]
  634.             if text[/\A\\ignr/] && text==firstText
  635.               text+=" " if text[text.length-1,1]!=" "
  636.               message+=text
  637.               @index=nextIndex
  638.               continue
  639.             end
  640.           end
  641.           messageend="\1"
  642.         end
  643.         break
  644.       end
  645.     end
  646.     @message_waiting=true # needed to allow parallel process events to work while
  647.                           # a message is displayed
  648.     message=_MAPINTL($game_map.map_id,message)
  649.     if commands
  650.       cmdlist=[]
  651.       for cmd in commands[0]
  652.         cmdlist.push(_MAPINTL($game_map.map_id,cmd))
  653.       end
  654.       command=Kernel.pbMessage(message+messageend,cmdlist,commands[1])
  655.       @branch[@list[@index].indent] = command
  656.     elsif numInputVar
  657.       params=ChooseNumberParams.new
  658.       params.setMaxDigits(numInputDigitsMax)
  659.       params.setDefaultValue($game_variables[numInputVar])
  660.       $game_variables[numInputVar]=Kernel.pbMessageChooseNumber(message+messageend,params)
  661.       $game_map.need_refresh = true if $game_map
  662.     else
  663.       Kernel.pbMessage(message+messageend,nil)
  664.     end
  665.     @message_waiting=false
  666.     return true
  667.   end
  668.  
  669.   def command_102
  670.     @message_waiting=true
  671.     command=Kernel.pbShowCommands(nil,@list[@index].parameters[0],@list[@index].parameters[1])
  672.     @message_waiting=false
  673.     @branch[@list[@index].indent] = command
  674.     Input.update # Must call Input.update again to avoid extra triggers
  675.     return true
  676.   end
  677.  
  678.   def command_103
  679.     varnumber=@list[@index].parameters[0]
  680.     @message_waiting=true
  681.     params=ChooseNumberParams.new
  682.     params.setMaxDigits(@list[@index].parameters[1])
  683.     params.setDefaultValue($game_variables[varnumber])
  684.     $game_variables[varnumber]=Kernel.pbChooseNumber(nil,params)
  685.     $game_map.need_refresh = true if $game_map
  686.     @message_waiting=false
  687.     return true
  688.   end
  689. end
  690.  
  691.  
  692.  
  693. class ChooseNumberParams
  694.   def initialize
  695.     @maxDigits=0
  696.     @minNumber=0
  697.     @maxNumber=0
  698.     @skin=nil
  699.     @messageSkin=nil
  700.     @negativesAllowed=false
  701.     @initialNumber=0
  702.     @cancelNumber=nil
  703.   end
  704.  
  705.   def setMessageSkin(value)
  706.     @messageSkin=value
  707.   end
  708.  
  709.   def messageSkin # Set the full path for the message's window skin
  710.     @messageSkin
  711.   end
  712.  
  713.   def setSkin(value)
  714.     @skin=value
  715.   end
  716.  
  717.   def skin
  718.     @skin
  719.   end
  720.  
  721.   def setNegativesAllowed(value)
  722.     @negativeAllowed=value
  723.   end
  724.  
  725.   def negativesAllowed
  726.     @negativeAllowed ? true : false
  727.   end
  728.  
  729.   def setRange(minNumber,maxNumber)
  730.     maxNumber=minNumber if minNumber>maxNumber
  731.     @maxDigits=0
  732.     @minNumber=minNumber
  733.     @maxNumber=maxNumber
  734.   end
  735.  
  736.   def setDefaultValue(number)
  737.     @initialNumber=number
  738.     @cancelNumber=nil
  739.   end
  740.  
  741.   def setInitialValue(number)
  742.     @initialNumber=number
  743.   end
  744.  
  745.   def setCancelValue(number)
  746.     @cancelNumber=number
  747.   end
  748.  
  749.   def initialNumber
  750.     return clamp(@initialNumber,self.minNumber,self.maxNumber)
  751.   end
  752.  
  753.   def cancelNumber
  754.     return @cancelNumber ? @cancelNumber : self.initialNumber
  755.   end
  756.  
  757.   def minNumber
  758.     ret=0
  759.     if @maxDigits>0
  760.       ret=-((10**@maxDigits)-1)
  761.     elsif
  762.       ret=@minNumber
  763.     end
  764.     ret=0 if !@negativeAllowed && ret<0
  765.     return ret
  766.   end
  767.  
  768.   def maxNumber
  769.     ret=0
  770.     if @maxDigits>0
  771.       ret=((10**@maxDigits)-1)
  772.     elsif
  773.       ret=@maxNumber
  774.     end
  775.     ret=0 if !@negativeAllowed && ret<0
  776.     return ret
  777.   end
  778.  
  779.   def setMaxDigits(value)
  780.     @maxDigits=[1,value].max
  781.   end
  782.  
  783.   def maxDigits
  784.     if @maxDigits>0
  785.       return @maxDigits
  786.     else
  787.       return [numDigits(self.minNumber),numDigits(self.maxNumber)].max
  788.     end
  789.   end
  790.  
  791.   private
  792.  
  793.   def clamp(v,mn,mx)
  794.     return v<mn ? mn : (v>mx ? mx : v)
  795.   end
  796.  
  797.   def numDigits(number)
  798.     ans = 1
  799.     number=number.abs
  800.     while number >= 10
  801.       ans+=1
  802.       number/=10
  803.     end
  804.     return ans
  805.   end
  806. end
  807.  
  808.  
  809.  
  810. def pbChooseNumber(msgwindow,params)
  811.   return 0 if !params
  812.   ret=0
  813.   maximum=params.maxNumber
  814.   minimum=params.minNumber
  815.   defaultNumber=params.initialNumber
  816.   cancelNumber=params.cancelNumber
  817.   cmdwindow=Window_InputNumberPokemon.new(params.maxDigits)
  818.   cmdwindow.z=99999
  819.   cmdwindow.visible=true
  820.   cmdwindow.setSkin(params.skin) if params.skin
  821.   cmdwindow.sign=params.negativesAllowed # must be set before number
  822.   cmdwindow.number=defaultNumber
  823.   curnumber=defaultNumber
  824.   pbPositionNearMsgWindow(cmdwindow,msgwindow,:right)
  825.   command=0
  826.   loop do
  827.     Graphics.update
  828.     Input.update
  829.     pbUpdateSceneMap
  830.     cmdwindow.update
  831.     msgwindow.update if msgwindow
  832.     yield if block_given?
  833.     if Input.trigger?(Input::C)
  834.       ret=cmdwindow.number
  835.       if ret>maximum
  836.         pbPlayBuzzerSE()
  837.       elsif ret<minimum
  838.         pbPlayBuzzerSE()
  839.       else
  840.         pbPlayDecisionSE()
  841.         break
  842.       end
  843.     elsif Input.trigger?(Input::B)
  844.       pbPlayCancelSE()
  845.       ret=cancelNumber
  846.       break
  847.     end
  848.   end
  849.   cmdwindow.dispose
  850.   Input.update
  851.   return ret
  852. end
  853.  
  854. def Kernel.pbShowCommandsWithHelp(msgwindow,commands,help,cmdIfCancel=0,defaultCmd=0)
  855.   msgwin=msgwindow
  856.   if !msgwindow
  857.     msgwin=Kernel.pbCreateMessageWindow(nil)
  858.   end
  859.   oldlbl=msgwin.letterbyletter
  860.   msgwin.letterbyletter=false
  861.   if commands
  862.     cmdwindow=Window_CommandPokemonEx.new(commands)
  863.     cmdwindow.z=99999
  864.     cmdwindow.visible=true
  865.     cmdwindow.resizeToFit(cmdwindow.commands)
  866.     cmdwindow.height=msgwin.y if cmdwindow.height>msgwin.y
  867.     cmdwindow.index=defaultCmd
  868.     command=0
  869.     msgwin.text=help[cmdwindow.index]
  870.     msgwin.width=msgwin.width # Necessary evil to make it use the proper margins.
  871.     loop do
  872.       Graphics.update
  873.       Input.update
  874.       oldindex=cmdwindow.index
  875.       cmdwindow.update
  876.       if oldindex!=cmdwindow.index
  877.         msgwin.text=help[cmdwindow.index]
  878.       end
  879.       msgwin.update
  880.       yield if block_given?
  881.       if Input.trigger?(Input::B)
  882.         if cmdIfCancel>0
  883.           command=cmdIfCancel-1
  884.           break
  885.         elsif cmdIfCancel<0
  886.           command=cmdIfCancel
  887.           break
  888.         end
  889.       end
  890.       if Input.trigger?(Input::C)
  891.         command=cmdwindow.index
  892.         break
  893.       end
  894.       pbUpdateSceneMap
  895.     end
  896.     ret=command
  897.     cmdwindow.dispose
  898.     Input.update
  899.   end
  900.   msgwin.letterbyletter=oldlbl
  901.   if !msgwindow
  902.     msgwin.dispose
  903.   end
  904.   return ret
  905. end
  906.  
  907. def Kernel.pbShowCommands(msgwindow,commands=nil,cmdIfCancel=0,defaultCmd=0)
  908.   ret=0
  909.   if commands
  910.     cmdwindow=Window_CommandPokemonEx.new(commands)
  911.     cmdwindow.z=99999
  912.     cmdwindow.visible=true
  913.     cmdwindow.resizeToFit(cmdwindow.commands)
  914.     pbPositionNearMsgWindow(cmdwindow,msgwindow,:right)
  915.     cmdwindow.index=defaultCmd
  916.     command=0
  917.     loop do
  918.       Graphics.update
  919.       Input.update
  920.       cmdwindow.update
  921.       msgwindow.update if msgwindow
  922.       yield if block_given?
  923.       if Input.trigger?(Input::B)
  924.         if cmdIfCancel>0
  925.           command=cmdIfCancel-1
  926.           break
  927.         elsif cmdIfCancel<0
  928.           command=cmdIfCancel
  929.           break
  930.         end
  931.       end
  932.       if Input.trigger?(Input::C)
  933.         command=cmdwindow.index
  934.         break
  935.       end
  936.       pbUpdateSceneMap
  937.     end
  938.     ret=command
  939.     cmdwindow.dispose
  940.     Input.update
  941.   end
  942.   return ret
  943. end
  944.  
  945. def pbPositionFaceWindow(facewindow,msgwindow)
  946.   return if !facewindow
  947.   if msgwindow
  948.     if facewindow.height<=msgwindow.height
  949.       facewindow.y=msgwindow.y
  950.     else
  951.       facewindow.y=msgwindow.y+msgwindow.height-facewindow.height
  952.     end
  953.     facewindow.x=Graphics.width-facewindow.width
  954.     msgwindow.x=0
  955.     msgwindow.width=Graphics.width-facewindow.width
  956.   else
  957.     facewindow.height=Graphics.height if facewindow.height>Graphics.height
  958.     facewindow.x=0
  959.     facewindow.y=0
  960.   end
  961. end
  962.  
  963. def pbPositionNearMsgWindow(cmdwindow,msgwindow,side)
  964.   return if !cmdwindow
  965.   if msgwindow
  966.     height=[cmdwindow.height,Graphics.height-msgwindow.height].min
  967.     if cmdwindow.height!=height
  968.       cmdwindow.height=height
  969.     end
  970.     cmdwindow.y=msgwindow.y-cmdwindow.height
  971.     if cmdwindow.y<0
  972.       cmdwindow.y=msgwindow.y+msgwindow.height
  973.       if cmdwindow.y+cmdwindow.height>Graphics.height
  974.         cmdwindow.y=msgwindow.y-cmdwindow.height
  975.       end
  976.     end
  977.     case side
  978.       when :left
  979.         cmdwindow.x=msgwindow.x
  980.       when :right
  981.         cmdwindow.x=msgwindow.x+msgwindow.width-cmdwindow.width
  982.       else
  983.         cmdwindow.x=msgwindow.x+msgwindow.width-cmdwindow.width
  984.     end
  985.   else
  986.     cmdwindow.height=Graphics.height if cmdwindow.height>Graphics.height
  987.     cmdwindow.x=0
  988.     cmdwindow.y=0
  989.   end
  990. end
  991.  
  992. def pbGetBasicMapNameFromId(id)
  993.   begin
  994.     map = pbLoadRxData("Data/MapInfos")
  995.     return "" if !map
  996.     return map[id].name
  997.     rescue
  998.     return ""
  999.   end
  1000. end
  1001.  
  1002. def pbGetMapNameFromId(id)
  1003.   map=pbGetBasicMapNameFromId(id)
  1004.   if $Trainer
  1005.     map.gsub!(/\\PN/,$Trainer.name)
  1006.   end
  1007.   return map
  1008. end
  1009.  
  1010. def Kernel.pbCustomMessage(message,skin=nil,newx=nil,newwidth=nil,&block)
  1011.   ret=0
  1012.   msgwindow=Kernel.pbCreateMessageWindow(nil,skin)
  1013.   msgwindow.x=newx if newx!=nil
  1014.   if newwidth!=nil
  1015.     msgwindow.width=newwidth
  1016.   else
  1017.   msgwindow.width=Graphics.width-msgwindow.x
  1018. end
  1019.   Kernel.pbMessageDisplay(msgwindow,message,&block)
  1020.   Kernel.pbDisposeMessageWindow(msgwindow)
  1021.   Input.update
  1022.   return ret
  1023. end
  1024.  
  1025. def Kernel.pbMessage(message,commands=nil,cmdIfCancel=0,skin=nil,defaultCmd=0,&block)
  1026.   ret=0
  1027.   msgwindow=Kernel.pbCreateMessageWindow(nil,skin)
  1028.   if commands
  1029.     ret=Kernel.pbMessageDisplay(msgwindow,message,true,
  1030.        proc {|msgwindow|
  1031.        next Kernel.pbShowCommands(msgwindow,commands,cmdIfCancel,defaultCmd,&block)
  1032.     },&block)
  1033.   else
  1034.     Kernel.pbMessageDisplay(msgwindow,message,&block)
  1035.   end
  1036.   Kernel.pbDisposeMessageWindow(msgwindow)
  1037.   Input.update
  1038.   return ret
  1039. end
  1040.  
  1041. def Kernel.pbMessageChooseNumber(message,params,&block)
  1042.   msgwindow=Kernel.pbCreateMessageWindow(nil,params.messageSkin)
  1043.   ret=Kernel.pbMessageDisplay(msgwindow,message,true,
  1044.      proc {|msgwindow|
  1045.      next Kernel.pbChooseNumber(msgwindow,params,&block)
  1046.   },&block)
  1047.   Kernel.pbDisposeMessageWindow(msgwindow)
  1048.   return ret
  1049. end
  1050.  
  1051. def Kernel.pbConfirmMessage(message,&block)
  1052.   return (Kernel.pbMessage(message,[_INTL("Yes"),_INTL("No")],2,&block)==0)
  1053. end
  1054.  
  1055. def Kernel.pbConfirmMessageSerious(message,&block)
  1056.   return (Kernel.pbMessage(message,[_INTL("No"),_INTL("Yes")],1,&block)==1)
  1057. end
  1058.  
  1059. def Kernel.pbCreateStatusWindow(viewport=nil)
  1060.   msgwindow=Window_AdvancedTextPokemon.new("")
  1061.   if !viewport
  1062.     msgwindow.z=99999
  1063.   else
  1064.     msgwindow.viewport=viewport
  1065.   end
  1066.   msgwindow.visible=false
  1067.   msgwindow.letterbyletter=false
  1068.   pbBottomLeftLines(msgwindow,2)
  1069.   skinfile=MessageConfig.pbGetSpeechFrame()
  1070.   msgwindow.setSkin(skinfile)
  1071.   return msgwindow
  1072. end
  1073.  
  1074. def Kernel.pbCreateMessageWindow(viewport=nil,skin=nil)
  1075.   msgwindow=Window_AdvancedTextPokemon.new("")
  1076.   if !viewport
  1077.     msgwindow.z=99999
  1078.   else
  1079.     msgwindow.viewport=viewport
  1080.   end
  1081.   msgwindow.visible=true
  1082.   msgwindow.letterbyletter=true
  1083.   msgwindow.back_opacity=MessageConfig::WindowOpacity
  1084.   pbBottomLeftLines(msgwindow,2)
  1085.   $game_temp.message_window_showing=true if $game_temp
  1086.   $game_message.visible=true if $game_message
  1087.   skin=MessageConfig.pbGetSpeechFrame() if !skin
  1088.   msgwindow.setSkin(skin)
  1089.   return msgwindow
  1090. end
  1091.  
  1092. def Kernel.pbDisposeMessageWindow(msgwindow)
  1093.   $game_temp.message_window_showing=false if $game_temp
  1094.   $game_message.visible=false if $game_message
  1095.   msgwindow.dispose
  1096. end
  1097.  
  1098.  
  1099.  
  1100. class FaceWindowVX < SpriteWindow_Base
  1101.   def initialize(face)
  1102.     super(0,0,128,128)
  1103.     faceinfo=face.split(",")
  1104.     facefile=pbResolveBitmap("Graphics/Faces/"+faceinfo[0])
  1105.     facefile=pbResolveBitmap("Graphics/Pictures/"+faceinfo[0]) if !facefile
  1106.     self.contents.dispose if self.contents
  1107.     @faceIndex=faceinfo[1].to_i
  1108.     @facebitmaptmp=AnimatedBitmap.new(facefile)
  1109.     @facebitmap=BitmapWrapper.new(96,96)
  1110.     @facebitmap.blt(0,0,@facebitmaptmp.bitmap,Rect.new(
  1111.        (@faceIndex % 4) * 96,
  1112.        (@faceIndex / 4) * 96, 96, 96
  1113.     ))
  1114.     self.contents=@facebitmap
  1115.   end
  1116.  
  1117.   def update
  1118.     super
  1119.     if @facebitmaptmp.totalFrames>1
  1120.       @facebitmaptmp.update
  1121.       @facebitmap.blt(0,0,@facebitmaptmp.bitmap,Rect.new(
  1122.          (@faceIndex % 4) * 96,
  1123.          (@faceIndex / 4) * 96, 96, 96
  1124.       ))
  1125.     end
  1126.   end
  1127.  
  1128.   def dispose
  1129.     @facebitmaptmp.dispose
  1130.     @facebitmap.dispose if @facebitmap
  1131.     super
  1132.   end
  1133. end
  1134.  
  1135.  
  1136.  
  1137. def itemIconTag(item)
  1138.   return "" if !item
  1139.   if item.respond_to?("icon_name")
  1140.     return sprintf("<icon=%s>",item.icon_name)
  1141.   else
  1142.     ix=item.icon_index % 16 * 24
  1143.     iy=item.icon_index / 16 * 24
  1144.     return sprintf("<img=Graphics/System/Iconset|%d|%d|24|24>",ix,iy)
  1145.   end
  1146. end
  1147.  
  1148. def getSkinColor(windowskin,color,isDarkSkin)
  1149.   if !windowskin || windowskin.disposed? ||
  1150.      windowskin.width!=128 || windowskin.height!=128
  1151.     textcolors=[
  1152.        isDarkSkin ? shadowc3tag(MessageConfig::LIGHTTEXTBASE, MessageConfig::LIGHTTEXTSHADOW) :
  1153.                     shadowc3tag(MessageConfig::DARKTEXTBASE, MessageConfig::DARKTEXTSHADOW),
  1154.        "<c2=7E105D08>",
  1155.        "<c2=421F2117>",
  1156.        "<c2=43F022E8>",
  1157.        "<c2=7FF05EE8>",
  1158.        "<c2=7E1F5D17>",
  1159.        "<c2=43FF22F7>",
  1160.        "<c2=63184210>",
  1161.        "<c2=7FFF5EF7>"
  1162.     ]
  1163.     color=0 if color>textcolors.length
  1164.     return textcolors[color]
  1165.   else # VX windowskin
  1166.     color=0 if color>=32
  1167.     x = 64 + (color % 8) * 8
  1168.     y = 96 + (color / 8) * 8
  1169.     pixel=windowskin.get_pixel(x, y)
  1170.     return shadowctagFromColor(pixel)
  1171.   end
  1172. end
  1173.  
  1174. # internal function
  1175. def pbRepositionMessageWindow(msgwindow, linecount=2)
  1176.   msgwindow.height=32*linecount+msgwindow.borderY
  1177.   msgwindow.y=(Graphics.height)-(msgwindow.height)
  1178.   if $game_temp && $game_temp.in_battle && !$scene.respond_to?("update_basic")
  1179.     msgwindow.y=0
  1180.   elsif $game_system && $game_system.respond_to?("message_position")
  1181.     case $game_system.message_position
  1182.       when 0  # up
  1183.         msgwindow.y=0
  1184.       when 1  # middle
  1185.         msgwindow.y=(Graphics.height/2)-(msgwindow.height/2)
  1186.       when 2
  1187.        msgwindow.y=(Graphics.height)-(msgwindow.height)
  1188.     end
  1189.   end
  1190.   if $game_system && $game_system.respond_to?("message_frame")
  1191.     if $game_system.message_frame != 0
  1192.       msgwindow.opacity = 0
  1193.     end
  1194.   end
  1195.   if $game_message
  1196.     case $game_message.background
  1197.       when 1  # dim
  1198.         msgwindow.opacity=0
  1199.       when 2  # transparent
  1200.         msgwindow.opacity=0
  1201.     end
  1202.   end
  1203. end
  1204.  
  1205. # internal function
  1206. def pbUpdateMsgWindowPos(msgwindow,event,eventChanged=false)
  1207.   if event
  1208.     if eventChanged
  1209.       msgwindow.resizeToFit2(msgwindow.text,Graphics.width*2/3,msgwindow.height)
  1210.     end
  1211.     msgwindow.y=event.screen_y-48-msgwindow.height  
  1212.     if msgwindow.y<0
  1213.       msgwindow.y=event.screen_y+24
  1214.     end
  1215.     msgwindow.x=event.screen_x-(msgwindow.width/2)
  1216.     msgwindow.x=0 if msgwindow.x<0
  1217.     if msgwindow.x>Graphics.width-msgwindow.width
  1218.       msgwindow.x=Graphics.width-msgwindow.width
  1219.     end
  1220.   else
  1221.     curwidth=msgwindow.width
  1222.     if curwidth!=Graphics.width
  1223.       msgwindow.width=Graphics.width
  1224.       msgwindow.width=Graphics.width    
  1225.     end
  1226.   end
  1227. end
  1228.  
  1229. # internal function
  1230.  
  1231. def pbGetGoldString
  1232.   moneyString=""
  1233.   if $Trainer
  1234.     moneyString=_INTL("${1}",$Trainer.money)
  1235.   else
  1236.     if $data_system.respond_to?("words")
  1237.       moneyString=_INTL("{1} {2}",$game_party.gold,$data_system.words.gold)
  1238.     else
  1239.       moneyString=_INTL("{1} {2}",$game_party.gold,Vocab.gold)        
  1240.     end
  1241.   end
  1242.   return moneyString
  1243. end
  1244.  
  1245. def pbDisplayGoldWindow(msgwindow)
  1246.   moneyString=pbGetGoldString()
  1247.   goldwindow=Window_AdvancedTextPokemon.new(_INTL("Money:\n<ar>{1}</ar>",moneyString))
  1248.   goldwindow.setSkin("Graphics/Windowskins/goldskin")
  1249.   goldwindow.resizeToFit(goldwindow.text,Graphics.width)
  1250.   goldwindow.width=160 if goldwindow.width<=160
  1251.   if msgwindow.y==0
  1252.     goldwindow.y=Graphics.height-goldwindow.height
  1253.   else
  1254.     goldwindow.y=0
  1255.   end
  1256.   goldwindow.viewport=msgwindow.viewport
  1257.   goldwindow.z=msgwindow.z
  1258.   return goldwindow
  1259. end
  1260.  
  1261. def pbDisplayCoinsWindow(msgwindow,goldwindow)
  1262.   coinString=($PokemonGlobal) ? $PokemonGlobal.coins : "0"
  1263.   coinwindow=Window_AdvancedTextPokemon.new(_INTL("Coins:\n<ar>{1}</ar>",coinString))
  1264.   coinwindow.setSkin("Graphics/Windowskins/goldskin")
  1265.   coinwindow.resizeToFit(coinwindow.text,Graphics.width)
  1266.   coinwindow.width=160 if coinwindow.width<=160
  1267.   if msgwindow.y==0
  1268.     coinwindow.y=(goldwindow) ? goldwindow.y-coinwindow.height : Graphics.height-coinwindow.height
  1269.   else
  1270.     coinwindow.y=(goldwindow) ? goldwindow.height : 0
  1271.   end
  1272.   coinwindow.viewport=msgwindow.viewport
  1273.   coinwindow.z=msgwindow.z
  1274.   return coinwindow
  1275. end
  1276.  
  1277. def pbRecord(arg); end
  1278.  
  1279. def pbMessageWaitForInput(msgwindow,frames,showPause=false)
  1280.   return if !frames || frames<=0
  1281.   if msgwindow && showPause
  1282.     msgwindow.startPause
  1283.   end
  1284.   frames.times do
  1285.     Graphics.update
  1286.     Input.update
  1287.     msgwindow.update if msgwindow
  1288.     pbUpdateSceneMap
  1289.     if Input.trigger?(Input::C) || Input.trigger?(Input::B)
  1290.       break
  1291.     end
  1292.   end
  1293.   if msgwindow && showPause
  1294.     msgwindow.stopPause
  1295.   end
  1296. end
  1297.  
  1298. def Kernel.pbMessageDisplay(msgwindow,message,letterbyletter=true,commandProc=nil)
  1299.   return if !msgwindow
  1300.   oldletterbyletter=msgwindow.letterbyletter
  1301.   msgwindow.letterbyletter=(letterbyletter ? true : false)
  1302.   ret=nil
  1303.   count=0
  1304.   commands=nil
  1305.   facewindow=nil
  1306.   goldwindow=nil
  1307.   coinwindow=nil
  1308.   cmdvariable=0
  1309.   cmdIfCancel=0
  1310.   msgwindow.waitcount=0
  1311.   autoresume=false
  1312.   text=message.clone
  1313.   msgback=nil
  1314.   linecount=(Graphics.height>400) ? 3 : 2
  1315.   ### Text replacement
  1316.   text.gsub!(/\\\\/,"\5")
  1317.   if $game_actors
  1318.     text.gsub!(/\\[Nn]\[([1-8])\]/){
  1319.        m=$1.to_i
  1320.        next $game_actors[m].name
  1321.     }
  1322.   end
  1323.   text.gsub!(/\\[Ss][Ii][Gg][Nn]\[([^\]]*)\]/){
  1324.      next "\\op\\cl\\ts[]\\w["+$1+"]"
  1325.   }
  1326.   text.gsub!(/\\[Pp][Nn]/,$Trainer.name) if $Trainer
  1327.   text.gsub!(/\\[Pp][Mm]/,_INTL("${1}",$Trainer.money)) if $Trainer
  1328.   text.gsub!(/\\[Nn]/,"\n")
  1329.   text.gsub!(/\\\[([0-9A-Fa-f]{8,8})\]/){ "<c2="+$1+">" }
  1330.   text.gsub!(/\\[Pp][Gg]/,"\\b") if $Trainer && $Trainer.gender==0
  1331.   text.gsub!(/\\[Pp][Gg]/,"\\r") if $Trainer && $Trainer.gender==1
  1332.   text.gsub!(/\\[Pp][Oo][Gg]/,"\\r") if $Trainer && $Trainer.gender==0
  1333.   text.gsub!(/\\[Pp][Oo][Gg]/,"\\b") if $Trainer && $Trainer.gender==1
  1334.   text.gsub!(/\\[Pp][Gg]/,"")
  1335.   text.gsub!(/\\[Pp][Oo][Gg]/,"")
  1336.   text.gsub!(/\\[Bb]/,"<c2=6546675A>")
  1337.   text.gsub!(/\\[Rr]/,"<c2=043C675A>")
  1338.   text.gsub!(/\\1/,"\1")
  1339.   colortag=""
  1340.   isDarkSkin=isDarkWindowskin(msgwindow.windowskin)
  1341.   if ($game_message && $game_message.background>0) ||
  1342.      ($game_system && $game_system.respond_to?("message_frame") &&
  1343.       $game_system.message_frame != 0)
  1344.     colortag=getSkinColor(msgwindow.windowskin,0,true)
  1345.   else
  1346.     colortag=getSkinColor(msgwindow.windowskin,0,isDarkSkin)
  1347.   end
  1348.   text.gsub!(/\\[Cc]\[([0-9]+)\]/){
  1349.      m=$1.to_i
  1350.      next getSkinColor(msgwindow.windowskin,m,isDarkSkin)
  1351.   }
  1352.   begin
  1353.     last_text = text.clone
  1354.     text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
  1355.   end until text == last_text
  1356.   begin
  1357.     last_text = text.clone
  1358.     text.gsub!(/\\[Ll]\[([0-9]+)\]/) {
  1359.        linecount=[1,$1.to_i].max;
  1360.        next ""
  1361.     }
  1362.   end until text == last_text
  1363.   text=colortag+text
  1364.   ### Controls
  1365.   textchunks=[]
  1366.   controls=[]
  1367.   while text[/(?:\\([WwFf]|[Ff][Ff]|[Tt][Ss]|[Cc][Ll]|[Mm][Ee]|[Ss][Ee]|[Ww][Tt]|[Ww][Tt][Nn][Pp]|[Cc][Hh]|[Qq])\[([^\]]*)\]|\\([Gg]|[Cc][Nn]|[Ww][Dd]|[Ww][Mm]|[Oo][Pp]|[Cc][Ll]|[Ww][Uu]|[\.]|[\|]|[\!]|[\x5E])())/i]
  1368.     textchunks.push($~.pre_match)
  1369.     if $~[1]
  1370.       controls.push([$~[1].downcase,$~[2],-1])
  1371.     else
  1372.       controls.push([$~[3].downcase,"",-1])
  1373.     end
  1374.     text=$~.post_match
  1375.   end
  1376.   textchunks.push(text)
  1377.   for chunk in textchunks
  1378.     chunk.gsub!(/\005/,"\\")
  1379.   end
  1380.   textlen=0
  1381.   for i in 0...controls.length
  1382.     control=controls[i][0]
  1383.     if control=="wt" || control=="wtnp" || control=="." || control=="|"
  1384.       textchunks[i]+="\2"
  1385.     elsif control=="!"
  1386.       textchunks[i]+="\1"
  1387.     end
  1388.     textlen+=toUnformattedText(textchunks[i]).scan(/./m).length
  1389.     controls[i][2]=textlen
  1390.   end
  1391.   text=textchunks.join("")
  1392.   unformattedText=toUnformattedText(text)
  1393.   signWaitCount=0
  1394.   haveSpecialClose=false
  1395.   specialCloseSE=""
  1396.   for i in 0...controls.length
  1397.     control=controls[i][0]
  1398.     param=controls[i][1]
  1399.     if control=="f"
  1400.       facewindow.dispose if facewindow
  1401.       facewindow=PictureWindow.new("Graphics/Pictures/#{param}")
  1402.     elsif control=="op"
  1403.       signWaitCount=21
  1404.     elsif control=="cl"
  1405.       text=text.sub(/\001\z/,"") # fix: '$' can match end of line as well
  1406.       haveSpecialClose=true
  1407.       specialCloseSE=param
  1408.     elsif control=="se" && controls[i][2]==0
  1409.       startSE=param
  1410.       controls[i]=nil
  1411.     elsif control=="ff"
  1412.       facewindow.dispose if facewindow
  1413.       facewindow=FaceWindowVX.new(param)
  1414.     elsif control=="q"
  1415.       yval=param.to_i
  1416.     elsif control=="ch"
  1417.       cmds=param.clone
  1418.       cmdvariable=pbCsvPosInt!(cmds)
  1419.       cmdIfCancel=pbCsvField!(cmds).to_i
  1420.       commands=[]
  1421.       while cmds.length>0
  1422.         commands.push(pbCsvField!(cmds))
  1423.       end
  1424.     elsif control=="wtnp" || control=="^"
  1425.       text=text.sub(/\001\z/,"") # fix: '$' can match end of line as well
  1426.     end
  1427.   end
  1428.   if startSE!=nil
  1429.     pbSEPlay(pbStringToAudioFile(startSE))
  1430.   elsif signWaitCount==0 && letterbyletter
  1431.     pbPlayDecisionSE()
  1432.   end
  1433.   ########## Position message window  ##############
  1434.   pbRepositionMessageWindow(msgwindow,linecount)
  1435.   if $game_message && $game_message.background==1
  1436.     msgback=IconSprite.new(0,msgwindow.y,msgwindow.viewport)
  1437.     msgback.z=msgwindow.z-1
  1438.     msgback.setBitmap("Graphics/System/MessageBack")
  1439.   end
  1440.   if facewindow
  1441.     pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1442.     facewindow.viewport=msgwindow.viewport
  1443.     facewindow.z=msgwindow.z
  1444.   end
  1445.   atTop=(msgwindow.y==0)
  1446.   ########## Show text #############################
  1447.   msgwindow.text=text
  1448.   Graphics.frame_reset if Graphics.frame_rate>40
  1449.   begin
  1450.     if signWaitCount>0
  1451.       signWaitCount-=1
  1452.       if atTop
  1453.         msgwindow.y=-(msgwindow.height*(signWaitCount)/20)
  1454.       else
  1455.         msgwindow.y=Graphics.height-(msgwindow.height*(20-signWaitCount)/20)
  1456.       end
  1457.     end
  1458.     for i in 0...controls.length
  1459.       if controls[i] && controls[i][2]<=msgwindow.position && msgwindow.waitcount==0
  1460.         control=controls[i][0]
  1461.         param=controls[i][1]
  1462.         if control=="f"
  1463.           facewindow.dispose if facewindow
  1464.           facewindow=PictureWindow.new("Graphics/Pictures/#{param}")
  1465.           pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1466.           facewindow.viewport=msgwindow.viewport
  1467.           facewindow.z=msgwindow.z
  1468.         elsif control=="ts"
  1469.           if param==""
  1470.             msgwindow.textspeed=-999
  1471.           else
  1472.             msgwindow.textspeed=param.to_i
  1473.           end
  1474.         elsif control=="ff"
  1475.           facewindow.dispose if facewindow
  1476.           facewindow=FaceWindowVX.new(param)
  1477.           pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1478.           facewindow.viewport=msgwindow.viewport
  1479.           facewindow.z=msgwindow.z
  1480.         elsif control=="g" # Display gold window
  1481.           goldwindow.dispose if goldwindow
  1482.           goldwindow=pbDisplayGoldWindow(msgwindow)
  1483.         elsif control=="cn" # Display coins window
  1484.           coinwindow.dispose if coinwindow
  1485.           coinwindow=pbDisplayCoinsWindow(msgwindow,goldwindow)
  1486.         elsif control=="wu"
  1487.           msgwindow.y=0
  1488.           atTop=true
  1489.           msgback.y=msgwindow.y if msgback
  1490.           pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1491.           msgwindow.y=-(msgwindow.height*(signWaitCount)/20)
  1492.         elsif control=="wm"
  1493.           atTop=false
  1494.           msgwindow.y=(Graphics.height/2)-(msgwindow.height/2)
  1495.           msgback.y=msgwindow.y if msgback
  1496.           pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1497.         elsif control=="wd"
  1498.           atTop=false
  1499.           msgwindow.y=(Graphics.height)-(msgwindow.height)
  1500.           msgback.y=msgwindow.y if msgback
  1501.           pbPositionNearMsgWindow(facewindow,msgwindow,:left)
  1502.           msgwindow.y=Graphics.height-(msgwindow.height*(20-signWaitCount)/20)
  1503.         elsif control=="."
  1504.           msgwindow.waitcount+=Graphics.frame_rate/4
  1505.         elsif control=="|"
  1506.           msgwindow.waitcount+=Graphics.frame_rate
  1507.         elsif control=="wt" # Wait
  1508.           param=param.sub(/\A\s+/,"").sub(/\s+\z/,"")
  1509.           msgwindow.waitcount+=param.to_i*2
  1510.         elsif control=="w" # Windowskin
  1511.           if param==""
  1512.             msgwindow.windowskin=nil
  1513.           else
  1514.             msgwindow.setSkin("Graphics/Windowskins/#{param}")
  1515.           end
  1516.           msgwindow.width=msgwindow.width  # Necessary evil
  1517.         elsif control=="^" # Wait, no pause
  1518.           autoresume=true
  1519.         elsif control=="wtnp" # Wait, no pause
  1520.           param=param.sub(/\A\s+/,"").sub(/\s+\z/,"")
  1521.           msgwindow.waitcount=param.to_i*2
  1522.           autoresume=true
  1523.         elsif control=="se" # Play SE
  1524.           pbSEPlay(pbStringToAudioFile(param))
  1525.         elsif control=="me" # Play ME
  1526.           pbMEPlay(pbStringToAudioFile(param))
  1527.         end
  1528.         controls[i]=nil
  1529.       end
  1530.     end
  1531.     if yval
  1532.         msgwindow.y=yval
  1533.     end
  1534.     break if !letterbyletter
  1535.     Graphics.update
  1536.     Input.update
  1537.     facewindow.update if facewindow
  1538.     if $DEBUG && Input.trigger?(Input::F6)
  1539.       pbRecord(unformattedText)
  1540.     end
  1541.     if autoresume && msgwindow.waitcount==0
  1542.       msgwindow.resume if msgwindow.busy?
  1543.       break if !msgwindow.busy?
  1544.     end
  1545.     if (Input.trigger?(Input::C) || Input.trigger?(Input::B))
  1546.       if msgwindow.busy?
  1547.         pbPlayDecisionSE() if msgwindow.pausing?
  1548.         msgwindow.resume
  1549.       else
  1550.         break if signWaitCount==0
  1551.       end
  1552.     end
  1553.     pbUpdateSceneMap
  1554.     msgwindow.update
  1555.     yield if block_given?
  1556.   end until (!letterbyletter || commandProc || commands) && !msgwindow.busy?
  1557.   Input.update # Must call Input.update again to avoid extra triggers
  1558.   msgwindow.letterbyletter=oldletterbyletter
  1559.   if commands
  1560.     $game_variables[cmdvariable]=Kernel.pbShowCommands(
  1561.        msgwindow,commands,cmdIfCancel)
  1562.     $game_map.need_refresh = true if $game_map
  1563.   end
  1564.   if commandProc
  1565.     ret=commandProc.call(msgwindow)
  1566.   end
  1567.   msgback.dispose if msgback
  1568.   goldwindow.dispose if goldwindow
  1569.   coinwindow.dispose if coinwindow
  1570.   facewindow.dispose if facewindow
  1571.   if haveSpecialClose
  1572.     pbSEPlay(pbStringToAudioFile(specialCloseSE))
  1573.     atTop=(msgwindow.y==0)
  1574.     for i in 0..20
  1575.       if atTop
  1576.         msgwindow.y=-(msgwindow.height*(i)/20)
  1577.       else
  1578.         msgwindow.y=Graphics.height-(msgwindow.height*(20-i)/20)
  1579.       end
  1580.       Graphics.update
  1581.       Input.update
  1582.       pbUpdateSceneMap
  1583.       msgwindow.update
  1584.     end
  1585.   end
  1586.   return ret
  1587. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement