• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Scripting Question] BWStorage Error Pokémon Essentials BW 3.1.1

  • 68
    Posts
    11
    Years
    • Seen Nov 12, 2023
    Hello,

    I use the Essentials BW 3.1.1 from Klein, and I don't know why, but sometime the Storage Script gives me an error. If I start a new game, it works for some time.

    Code:
    Exception: RuntimeError
    Message: Script error within event 8, map 14 (Aventia):
    Exception: TypeError
    Message: Section128:1586:in `clone'can't clone NilClass
    ***Full script:
    pbPokeCenterPC
    
    Interpreter:243:in `pbExecuteScript'
    BW_Storage:1586:in `initialize'
    BW_Storage:2142:in `new'
    BW_Storage:2142:in `initialize'
    BW_Storage:2138:in `each'
    BW_Storage:2138:in `initialize'
    BW_Storage:2276:in `new'
    BW_Storage:2276:in `pbStartBox'
    BW_Storage:772:in `pbStartScreen'
    BW_Storage:3991:in `access'
    
    Interpreter:276:in `pbExecuteScript'
    Interpreter:1606:in `command_355'
    Interpreter:494:in `execute_command'
    Interpreter:193:in `update'
    Interpreter:106:in `loop'
    Interpreter:198:in `update'
    Scene_Map:104:in `follow_update'
    Scene_Map:102:in `loop'
    Scene_Map:115:in `follow_update'
    Follow:1509:in `update'

    Code:
    class Window_CommandPokemonStorage < Window_DrawableCommand
      attr_reader :commands
    
      def initialize(commands,width=nil)
        @starting=true
        @commands=[]
        dims=[]
        @command=AnimatedBitmap.new("Graphics/Pictures/Boxes/command")
        super(0,0,32,32)
        [email protected]/2 + 4
        getAutoDims(commands,dims,width)
        self.width=dims[0]
        self.height=dims[1]
        @commands=commands
        self.active=true
        self.baseColor=Color.new(255,255,255)
        self.shadowColor=Color.new(140,140,140)
        self.windowskin=nil
        refresh
        @starting=false
      end
    
      def self.newWithSize(commands,x,y,width,height,viewport=nil)
        ret=self.new(commands,width)
        ret.x=x
        ret.y=y
        ret.width=width
        ret.height=height
        ret.viewport=viewport
        return ret
      end
    
      def self.newEmpty(x,y,width,height,viewport=nil)
        ret=self.new([],width)
        ret.x=x
        ret.y=y
        ret.width=width
        ret.height=height
        ret.viewport=viewport
        return ret
      end
    
      def index=(value)
        super
        refresh if !@starting
      end
    
      def commands=(value)
        @commands=value
        @item_max=commands.length  
        self.update_cursor_rect
        self.refresh
      end
      
      def width=(value)
        value=value+(value/2)
        super
        if !@starting
          self.index=self.index
          self.update_cursor_rect
        end
      end
    
      def height=(value)
        super
        if !@starting
          self.index=self.index
          self.update_cursor_rect
        end
      end
    
      def resizeToFit(commands,width=nil)
        dims=[]
        getAutoDims(commands,dims,width)
        self.width=dims[0]
        self.height=dims[1]
      end
    
      def itemCount
        return @commands ? @commands.length : 0
      end
    
      def drawCursor(index,rect)
        return Rect.new(rect.x+10,rect.y,rect.width,rect.height)
      end
      
      
      def drawItem(index,count,rect)
        pbSetSystemFont(self.contents) if @starting
        rect=drawCursor(index,rect)
        recty=0
        if self.index==index
          [email protected]/2
        end
        
        pbCopyBitmapRect(self.contents,@command.bitmap,rect.x+8,rect.y,
        Rect.new(0,recty,@command.bitmap.width,@command.bitmap.height/2))
        
        pbDrawTextPositions(self.contents,[[@commands[index],rect.width/2,
        rect.y+6,2,self.baseColor,self.shadowColor]])
        
      end
         
      def dispose
        @command.dispose
        super
      end
      
      def update
        super
        
        if @commands
          [email protected]
          hmo=(@command.bitmap.height/2)*@commands.length
          if $mouse.inArea?(self.x+26,self.y+14,wmo,hmo) && !$mouse.isStatic?
           ny=$mouse.y-(self.y+14)
           ny-=self.viewport.rect.y if self.viewport
           nindex=ny/@row_height
           self.index=nindex if nindex!=self.index
          end
        end
       
      end
      
      def refresh
        @item_max=itemCount()
        dwidth=self.width-self.borderX
        dheight=self.height-self.borderY
        self.contents=pbDoEnsureBitmap(self.contents,dwidth,dheight)
        self.contents.clear
        for i in 0...@item_max
          if i<self.top_item || i>self.top_item+self.page_item_max
            next
          end
          drawItem(i,@item_max,itemRect(i))
        end
      end
      
    end
    
    
    class PokemonBox
      attr_reader :pokemon
      attr_accessor :name
      attr_accessor :background
    
      def initialize(name,maxPokemon=30)
        @pokemon=[]
        @name=name
        @background=nil
        for i in 0...maxPokemon
          @pokemon[i]=nil
        end
      end
    
      def full?
        return (@pokemon.nitems==self.length)
      end
    
      def nitems
        return @pokemon.nitems
      end
    
      def length
        return @pokemon.length
      end
    
      def each
        @pokemon.each{|item| yield item}
      end
    
      def []=(i,value)
        @pokemon[i]=value
      end
    
      def [](i)
        return @pokemon[i]
      end
    end
    
    
    
    class PokemonStorage
      attr_reader :boxes
      attr_reader :party
      attr_accessor :currentBox
    
      def maxBoxes
        return @boxes.length
      end
    
      def party
        $Trainer.party
      end
    
      def party=(value)
        raise ArgumentError.new("Not supported")
      end
    
    #  MARKINGCHARS=["●","▲","■","♥","★","☀"] # star rombo
      MARKINGCHARS=["A","B","C","D","E","F"]
      
      def initialize(maxBoxes=STORAGEBOXES,maxPokemon=30)
        @boxes=[]
        for i in 0...maxBoxes
          ip1=i+1
          @boxes[i]=PokemonBox.new(_ISPRINTF("Box {1:d}",ip1),maxPokemon)
          backid=i%24
          @boxes[i].background="box#{backid}"
        end
        @currentBox=0
        @boxmode=-1
      end
    
      def maxPokemon(box)
        return 0 if box>=self.maxBoxes
        return box<0 ? 6 : self[box].length
      end
    
      def [](x,y=nil)
        if y==nil
          return (x==-1) ? self.party : @boxes[x]
        else
          for i in @boxes
            raise "Box is a Pokémon, not a box" if i.is_a?(PokeBattle_Pokemon)
          end
          return (x==-1) ? self.party[y] : @boxes[x][y]
        end
      end
    
      def []=(x,y,value)
        if x==-1
          self.party[y]=value
        else
          @boxes[x][y]=value
        end
      end
    
      def full?
        for i in 0...self.maxBoxes
          return false if !@boxes[i].full?
        end
        return true
      end
    
      def pbFirstFreePos(box)
        if box==-1
          ret=self.party.nitems
          return (ret==6) ? -1 : ret
        else
          for i in 0...maxPokemon(box)
            return i if !self[box,i]
          end
          return -1
        end
      end
    
      def pbCopy(boxDst,indexDst,boxSrc,indexSrc)
        if indexDst<0 && boxDst<self.maxBoxes
          found=false
          for i in 0...maxPokemon(boxDst)
            if !self[boxDst,i]
              found=true
              indexDst=i
              break
            end
          end
          return false if !found
        end
        if boxDst==-1
          if self.party.nitems>=6
            return false
          end
          self.party[self.party.length]=self[boxSrc,indexSrc]
          self.party.compact!
        else
          if !self[boxSrc,indexSrc]
            raise "Trying to copy nil to storage" # not localized
          end
          self[boxSrc,indexSrc].heal
          self[boxDst,indexDst]=self[boxSrc,indexSrc]
        end
        return true
      end
    
      def pbMove(boxDst,indexDst,boxSrc,indexSrc)
        return false if !pbCopy(boxDst,indexDst,boxSrc,indexSrc)
        pbDelete(boxSrc,indexSrc)
        return true
      end
    
      def pbMoveCaughtToParty(pkmn)
        if self.party.nitems>=6
          return false
        end
        self.party[self.party.length]=pkmn
      end
    
      def pbMoveCaughtToBox(pkmn,box)
        for i in 0...maxPokemon(box)
          if self[box,i]==nil
            pkmn.heal if box>=0
            self[box,i]=pkmn
            return true
          end
        end
        return false
      end
    
      def pbStoreCaught(pkmn)
        for i in 0...maxPokemon(@currentBox)
          if self[@currentBox,i]==nil
            self[@currentBox,i]=pkmn
            return @currentBox
          end
        end
        for j in 0...self.maxBoxes
          for i in 0...maxPokemon(j)
            if self[j,i]==nil
              self[j,i]=pkmn
              @currentBox=j
              return @currentBox
            end
          end
        end
        return -1
      end
    
      def pbDelete(box,index)
        if self[box,index]
          self[box,index]=nil
          if box==-1
            self.party.compact!
          end
        end
      end
    end
    
    
    
    class PokemonStorageWithParty < PokemonStorage
      def party
        return @party
      end
    
      def party=(value)
        @party=party
      end
    
      def initialize(maxBoxes=24,maxPokemon=30,party=nil)
        super(maxBoxes,maxPokemon)
        if party
          @party=party
        else
          @party=[]
        end
      end
    end
    
    
    
    class PokemonStorageScreen
      attr_reader :scene
      attr_reader :storage
    
      def initialize(scene,storage)
        @scene=scene
        @storage=storage
        @pbHeldPokemon=nil
      end
    
      def pbConfirm(str)
        return (pbShowCommands(str,[_INTL("Yes"),_INTL("No")])==0)
      end
    
      def pbRelease(selected,heldpoke)
        box=selected[0]
        index=selected[1]
        pokemon=(heldpoke)?heldpoke:@storage[box,index]
        return if !pokemon
        if pokemon.isEgg?
          pbDisplay(_INTL("You can't release an Egg."))
          return false
        elsif pokemon.mail
          pbDisplay(_INTL("Please remove the mail."))
          return false
        end
        if box==-1 && pbAbleCount<=1 && pbAble?(pokemon) && !heldpoke
          pbDisplay(_INTL("That's your last Pokémon!"))
          return
        end
        command=pbShowCommands(_INTL("Release this Pokémon?"),[_INTL("No"),_INTL("Yes")])
        if command==1
          pkmnname=pokemon.name
          @scene.pbRelease(selected,heldpoke)
          if heldpoke
            @heldpkmn=nil
          else
            @storage.pbDelete(box,index)
          end
          @scene.pbRefresh
          pbDisplay(_INTL("{1} was released.",pkmnname))
          pbDisplay(_INTL("Bye-bye, {1}!",pkmnname))
          @scene.pbRefresh
        end
        return
      end
    
      def pbMark(selected,heldpoke)
        @scene.pbMark(selected,heldpoke)
      end
    
      def pbAble?(pokemon)
        pokemon && !pokemon.isEgg? && pokemon.hp>0
      end
    
      def pbAbleCount
        count=0
        for p in @storage.party
          count+=1 if pbAble?(p)
        end
        return count
      end
    
      def pbStore(selected,heldpoke)
        box=selected[0]
        index=selected[1]
        if box!=-1
          raise _INTL("Can't deposit from box...")
        end   
        if pbAbleCount<=1 && pbAble?(@storage[box,index]) && !heldpoke
          pbDisplay(_INTL("That's your last Pokémon!"))
        elsif @storage[box,index].mail
          pbDisplay(_INTL("Please remove the Mail."))
        else
          loop do
            [email protected](_INTL("Deposit in which Box?"))
            if destbox>=0
              success=false
              [email protected](destbox)
              if firstfree<0
                pbDisplay(_INTL("The Box is full."))
                next
              end
              @scene.pbStore(selected,heldpoke,destbox,firstfree)
              if heldpoke
                @storage.pbMoveCaughtToBox(heldpoke,destbox)
                @heldpkmn=nil
              else
                @storage.pbMove(destbox,-1,-1,index)
              end
            end
            break
          end
          @scene.pbRefresh
        end
      end
    
      def pbWithdraw(selected,heldpoke)
        box=selected[0]
        index=selected[1]
        if box==-1
          raise _INTL("Can't withdraw from party...");
        end
        if @storage.party.nitems>=6
          pbDisplay(_INTL("Your party's full!"))
          return false
        end
        @scene.pbWithdraw(selected,heldpoke,@storage.party.length)
        if heldpoke
          @storage.pbMoveCaughtToParty(heldpoke)
          @heldpkmn=nil
        else
          @storage.pbMove(-1,-1,box,index)
        end
        @scene.pbRefresh
        return true
      end
    
      def pbDisplay(message)
        @scene.pbDisplay(message)
      end
    
      def pbSummary(selected,heldpoke)
        @scene.pbSummary(selected,heldpoke)
      end
    
      def pbHold(selected)
        box=selected[0]
        index=selected[1]
        if box==-1 && pbAble?(@storage[box,index]) && pbAbleCount<=1
          pbDisplay(_INTL("That's your last Pokémon!"))
          return
        end
        @scene.pbHold(selected)
        @heldpkmn=@storage[box,index]
        @storage.pbDelete(box,index) 
        @scene.pbRefresh
      end
    
      def pbSwap(selected)
        box=selected[0]
        index=selected[1]
        if !@storage[box,index]
          raise _INTL("Position {1},{2} is empty...",box,index)
        end
        if box==-1 && pbAble?(@storage[box,index]) && pbAbleCount<=1 && !pbAble?(@heldpkmn)
          pbDisplay(_INTL("That's your last Pokémon!"))
          return false
        end
        if box!=-1 && @heldpkmn.mail
          pbDisplay("Please remove the mail.")
          return false
        end
        @scene.pbSwap(selected,@heldpkmn)
        @heldpkmn.heal if box>=0
        tmp=@storage[box,index]
        @storage[box,index]=@heldpkmn
        @heldpkmn=tmp
        @scene.pbRefresh
        return true
      end
    
      def pbPlace(selected)
        box=selected[0]
        index=selected[1]
        if @storage[box,index]
          raise _INTL("Position {1},{2} is not empty...",box,index)
        end
        if box!=-1 && index>[email protected](box)
          pbDisplay("Can't place that there.")
          return
        end
        if box!=-1 && @heldpkmn.mail
          pbDisplay("Please remove the mail.")
          return
        end
        @heldpkmn.heal if box>=0
        @scene.pbPlace(selected,@heldpkmn)
        @storage[box,index]=@heldpkmn
        if box==-1
          @storage.party.compact!
        end
        @scene.pbRefresh
        @heldpkmn=nil
      end
    
      def pbItem(selected,heldpoke)
        box=selected[0]
        index=selected[1]
        pokemon=(heldpoke) ? heldpoke : @storage[box,index]
        if pokemon.isEgg?
          pbDisplay(_INTL("Eggs can't hold items."))
          return
        elsif pokemon.mail
          pbDisplay(_INTL("Please remove the mail."))
          return
        end
        if pokemon.item>0
          itemname=PBItems.getName(pokemon.item)
          if pbConfirm(_INTL("Take this {1}?",itemname))
            if !$PokemonBag.pbStoreItem(pokemon.item)
              pbDisplay(_INTL("Can't store the {1}.",itemname))
            else
              pbDisplay(_INTL("Took the {1}.",itemname))
              pokemon.setItem(0)
              @scene.pbHardRefresh
            end
          end
        else
          item=scene.pbChooseItem($PokemonBag)
          if item>0
            itemname=PBItems.getName(item)
            pokemon.setItem(item)
            $PokemonBag.pbDeleteItem(item)
            pbDisplay(_INTL("{1} is now being held.",itemname))
            @scene.pbHardRefresh
          end
        end
      end
    
      def pbHeldPokemon
        return @heldpkmn
      end
    
    =begin
      commands=[
         _INTL("WITHDRAW POKéMON"),
         _INTL("DEPOSIT POKéMON"),
         _INTL("MOVE POKéMON"),
         _INTL("MOVE ITEMS"),
         _INTL("SEE YA!")
      ]
      helptext=[
         _INTL("Move Pokémon stored in boxes to your party."),
         _INTL("Store Pokémon in your party in Boxes."),
         _INTL("Organize the Pokémon in Boxes and in your party."),
         _INTL("Move items held by any Pokémon in a Box and your party."),
         _INTL("Return to the previous menu."),
      ]
      command=pbShowCommandsAndHelp(commands,helptext)
    =end
    
      def pbShowCommands(msg,commands)
        return @scene.pbShowCommands(msg,commands)
      end
    
      def pbShowCommandsStorage(msg,commands)
        return @scene.pbShowCommandsStorage(msg,commands)
      end
      
      def pbBoxCommands
        commands=[
           _INTL("Jump"),
           _INTL("Wallpaper"),
           _INTL("Name"),
           _INTL("Cancel"),
        ]
        command=pbShowCommands(
           _INTL("What do you want to do?"),commands)
        case command
        when 0
          [email protected](_INTL("Jump to which Box?"))
          if destbox>=0
            @scene.pbJumpToBox(destbox)
          end
        when 1
          commands=[
             _INTL("Forest"),
             _INTL("City"),
             _INTL("Desert"),
             _INTL("Savanna"),
             _INTL("Crag"),
             _INTL("Volcano"),
             _INTL("Snow"),
             _INTL("Cave"),
             _INTL("Beach"),
             _INTL("Seafloor"),
             _INTL("River"),
             _INTL("Sky"),
             _INTL("Poké Center"),
             _INTL("Machine"),
             _INTL("Checks"),
             _INTL("Simple"),
             _INTL("Heart"),
             _INTL("Soul"),
             _INTL("Retro"),
             _INTL("Compete"),
             _INTL("Trio"),
             _INTL("Pika"),
             _INTL("Kimono Girl"),
             _INTL("Rocket")
          ]
          wpaper=pbShowCommands(_INTL("Pick the wallpaper."),commands)
          if wpaper>=0
            @scene.pbChangeBackground(wpaper)
          end
        when 2
          @scene.pbBoxName(_INTL("Box name?"),0,12)
        end
      end
    
      def pbChoosePokemon(party=nil)
        @heldpkmn=nil
        @scene.pbStartBox(self,2)
        retval=nil
        loop do
          [email protected](@storage.party)
          if selected && selected[0]==-3 # Close box
            if pbConfirm(_INTL("Exit from the Box?"))
              break
            else
              next
            end
          end
          if selected==nil
            if pbConfirm(_INTL("Continue Box operations?"))
              next
            else
              break
            end
          elsif selected[0]==-4 # Box name
            pbBoxCommands
          else
            pokemon=@storage[selected[0],selected[1]]
            next if !pokemon
            commands=[
               _INTL("Select"),
               _INTL("Summary"),
               _INTL("Withdraw"),
               _INTL("Item"),
               _INTL("Mark")
            ]
            commands.push(_INTL("Cancel"))
            commands[2]=_INTL("Store") if selected[0]==-1
            helptext=_INTL("{1} is selected.",pokemon.name)
            command=pbShowCommands(helptext,commands)
            case command
            when 0 # Move/Shift/Place
              if pokemon
                retval=selected
                break
              end
            when 1 # Summary
              pbSummary(selected,nil)
            when 2 # Withdraw
              if selected[0]==-1
                pbStore(selected,nil)
              else
                pbWithdraw(selected,nil)
              end
            when 3 # Item
              pbItem(selected,nil)
            when 4 # Mark
              pbMark(selected,nil)
            end
          end
        end
        @scene.pbCloseBox
        return retval
      end
    
      def pbStartScreen(command)
        @heldpkmn=nil
        if command==0
    ### WITHDRAW ###################################################################
          @scene.pbStartBox(self,command)
          loop do
            [email protected](@storage.party)
            if selected && selected[0]==-3 # Close box
              if pbConfirm(_INTL("Exit from the Box?"))
                break
              else
                next
              end
            end
            if selected && selected[0]==-2 # Party Pokémon
              pbDisplay(_INTL("Which one will you take?"))
              next
            end
            if selected && selected[0]==-4 # Box name
              pbBoxCommands
              next
            end
            if selected==nil
              if pbConfirm(_INTL("Continue Box operations?"))
                next
              else
                break
              end
            else
              pokemon=@storage[selected[0],selected[1]]
              next if !pokemon
              command=pbShowCommandsStorage(
                 _INTL("{1} is selected.",pokemon.name),[_INTL("Withdraw"),
                 _INTL("Summary"),_INTL("Mark"),_INTL("Release"),_INTL("Cancel")])
              case command
              when 0 # Withdraw
                pbWithdraw(selected,nil)
              when 1 # Summary
                pbSummary(selected,nil)
              when 2 # Mark
                pbMark(selected,nil)
              when 3 # Release
                pbRelease(selected,nil)
              end
            end
          end
          @scene.pbCloseBox
        elsif command==1
    ### DEPOSIT ####################################################################
          @scene.pbStartBox(self,command)
          loop do
            [email protected](@storage.party)
            if selected==-3 # Close box
              if pbConfirm(_INTL("Exit from the Box?"))
                break
              else
                next
              end
            end
            if selected<0
              if pbConfirm(_INTL("Continue Box operations?"))
                next
              else
                break
              end
            else
              pokemon=@storage[-1,selected]
              next if !pokemon
              command=pbShowCommandsStorage(
                 _INTL("{1} is selected.",pokemon.name),[_INTL("Store"),
                 _INTL("Summary"),_INTL("Mark"),_INTL("Release"),_INTL("Cancel")])
              case command
              when 0 # Store
                pbStore([-1,selected],nil)
              when 1 # Summary
                pbSummary([-1,selected],nil)
              when 2 # Mark
                pbMark([-1,selected],nil)
              when 3 # Release
                pbRelease([-1,selected],nil)
              end
            end
          end
          @scene.pbCloseBox
        elsif command==2
    ### MOVE #######################################################################
          @scene.pbStartBox(self,command)
          loop do
            [email protected](@storage.party)
            if selected && selected[0]==-3 # Close box
              if pbHeldPokemon
                pbDisplay(_INTL("You're holding a Pokémon!"))
                next
              end
              if pbConfirm(_INTL("Exit from the Box?"))
                break
              else
                next
              end
            end
            if selected==nil
              if pbHeldPokemon
                pbDisplay(_INTL("You're holding a Pokémon!"))
                next
              end
              if pbConfirm(_INTL("Continue Box operations?"))
                next
              else
                break
              end
            elsif selected[0]==-4 # Box name
              pbBoxCommands
              
            else
              pokemon=@storage[selected[0],selected[1]]
              commands=[
                 _INTL("Move"),
                 _INTL("Summary"),
                # _INTL("Withdraw"),
                 _INTL("Item"),
                 _INTL("Mark"),
                 _INTL("Release")
              ]
              #commands.push(_INTL("Debug")) if $DEBUG
              commands.push(_INTL("Cancel"))
              heldpoke=pbHeldPokemon
              if heldpoke
                helptext=_INTL("{1} is selected.",heldpoke.name)
                commands[0]=pokemon ? _INTL("Shift") : _INTL("Place")
              elsif pokemon
                helptext=_INTL("{1} is selected.",pokemon.name)
                commands[0]=_INTL("Move")
              else
                next
              end
              command=pbShowCommandsStorage(helptext,commands)
              case command
              when 0 # Move/Shift/Place
                if @heldpkmn && pokemon
                  pbSwap(selected)
                elsif @heldpkmn
                  pbPlace(selected)
                else
                  pbHold(selected)
                end
              when 1 # Summary
                pbSummary(selected,@heldpkmn)
             # when 2 # Withdraw
             #   if selected[0]==-1
             #     pbStore(selected,@heldpkmn)
             #   else
             #     pbWithdraw(selected,@heldpkmn)
             #   end
              when 2 # Item
                pbItem(selected,@heldpkmn)
              when 3 # Mark
                pbMark(selected,@heldpkmn)
              when 4 # Release
                pbRelease(selected,@heldpkmn)
              when 6
                if $DEBUG
                  pkmn=@heldpkmn ? @heldpkmn : pokemon
                  debugMenu(selected,pkmn,heldpoke)
                else
                  break
                end
              end
            end
          end
          @scene.pbCloseBox
        elsif command==3
          @scene.pbStartBox(self,command)
          @scene.pbCloseBox
        end
      end
    
      def debugMenu(selected,pkmn,heldpoke)
        command=0
        loop do
          [email protected](_INTL("Do what with {1}?",pkmn.name),[
             _INTL("Level"),
             _INTL("Species"),
             _INTL("Moves"),
             _INTL("Gender"),
             _INTL("Ability"),
             _INTL("Nature"),
             _INTL("Shininess"),
             _INTL("Form"),
             _INTL("Happiness"),
             _INTL("EV/IV/pID"),
             _INTL("Pokérus"),
             _INTL("Ownership"),
             _INTL("Nickname"),
             _INTL("Poké Ball"),
             _INTL("Ribbons"),
             _INTL("Egg"),
             _INTL("Shadow Pokémon"),
             #_INTL("Make Mystery Gift"),
             _INTL("Duplicate"),
             _INTL("Delete"),
             _INTL("Cancel")
          ],command)
          case command
          ### Cancel ###
          when -1, 20
            break
          ### Level ###
          when 0
            params=ChooseNumberParams.new
            params.setRange(1,PBExperience::MAXLEVEL)
            params.setDefaultValue(pkmn.level)
            level=Kernel.pbMessageChooseNumber(
               _INTL("Set the Pokémon's level (max. {1}).",PBExperience::MAXLEVEL),params)
            if level!=pkmn.level
              pkmn.level=level
              pkmn.calcStats
              pbDisplay(_INTL("{1}'s level was set to {2}.",pkmn.name,pkmn.level))
              @scene.pbHardRefresh
            end
          ### Species ###
          when 1
            species=pbChooseSpecies(pkmn.species)
            if species!=0
              oldspeciesname=PBSpecies.getName(pkmn.species)
              pkmn.species=species
              pkmn.calcStats
              oldname=pkmn.name
              pkmn.name=PBSpecies.getName(pkmn.species) if pkmn.name==oldspeciesname
              pbDisplay(_INTL("{1}'s species was changed to {2}.",oldname,PBSpecies.getName(pkmn.species)))
              pbSeenForm(pkmn)
              @scene.pbHardRefresh
            end
          ### Moves ###
          when 2
            cmd=0
            loop do
              [email protected](_INTL("Do what with {1}?",pkmn.name),[
                 _INTL("Teach move"),
                 _INTL("Forget move"),
                 _INTL("Reset movelist"),
                 _INTL("Reset initial moves")],cmd)
              # Break
              if cmd==-1
                break
              # Teach move
              elsif cmd==0
                move=pbChooseMoveList
                if move!=0
                  pbLearnMove(pkmn,move)
                  @scene.pbHardRefresh
                end
              # Forget Move
              elsif cmd==1
                pbChooseMove(pkmn,1,2)
                if pbGet(1)>=0
                  pbDeleteMove(pkmn,pbGet(1))
                  pbDisplay(_INTL("{1} forgot {2}.",pkmn.name,pbGet(2)))
                  @scene.pbHardRefresh
                end
              # Reset Movelist
              elsif cmd==2
                pkmn.resetMoves
                pbDisplay(_INTL("{1}'s moves were reset.",pkmn.name))
                @scene.pbHardRefresh
              # Reset initial moves
              elsif cmd==3
                pkmn.pbRecordFirstMoves
                pbDisplay(_INTL("{1}'s moves were set as its first-known moves.",pkmn.name))
                @scene.pbHardRefresh
              end
            end
          ### Gender ###
          when 3
            if pkmn.gender==2
              pbDisplay(_INTL("{1} is genderless.",pkmn.name))
            else
              cmd=0
              loop do
                oldgender=(pkmn.isMale?) ? _INTL("male") : _INTL("female")
                msg=[_INTL("Gender {1} is natural.",oldgender),
                     _INTL("Gender {1} is being forced.",oldgender)][pkmn.genderflag ? 1 : 0]
                [email protected](msg,[
                   _INTL("Make male"),
                   _INTL("Make female"),
                   _INTL("Remove override")],cmd)
                # Break
                if cmd==-1
                  break
                # Make male
                elsif cmd==0
                  pkmn.setGender(0)
                  if pkmn.isMale?
                    pbDisplay(_INTL("{1} is now male.",pkmn.name))
                  else
                    pbDisplay(_INTL("{1}'s gender couldn't be changed.",pkmn.name))
                  end
                # Make female
                elsif cmd==1
                  pkmn.setGender(1)
                  if pkmn.isFemale?
                    pbDisplay(_INTL("{1} is now female.",pkmn.name))
                  else
                    pbDisplay(_INTL("{1}'s gender couldn't be changed.",pkmn.name))
                  end
                # Remove override
                elsif cmd==2
                  pkmn.genderflag=nil
                  pbDisplay(_INTL("Gender override removed."))
                end
                pbSeenForm(pkmn)
                @scene.pbHardRefresh
              end
            end
          ### Ability ###
          when 4
            cmd=0
            loop do
              abils=pkmn.getAbilityList
              oldabil=PBAbilities.getName(pkmn.ability)
              commands=[]
              for i in 0...abils[0].length
                commands.push((abils[1][i]<2 ? "" : "(H) ")+PBAbilities.getName(abils[0][i]))
              end
              commands.push(_INTL("Remove override"))
              msg=[_INTL("Ability {1} is natural.",oldabil),
                   _INTL("Ability {1} is being forced.",oldabil)][pkmn.abilityflag ? 1 : 0]
              [email protected](msg,commands,cmd)
              # Break
              if cmd==-1
                break
              # Set ability override
              elsif cmd>=0 && cmd<abils[0].length
                pkmn.setAbility(abils[1][cmd])
              # Remove override
              elsif cmd==abils[0].length
                pkmn.abilityflag=nil
              end
              @scene.pbHardRefresh
            end
          ### Nature ###
          when 5
            cmd=0
            loop do
              oldnature=PBNatures.getName(pkmn.nature)
              commands=[]
              (PBNatures.getCount).times do |i|
                commands.push(PBNatures.getName(i))
              end
              commands.push(_INTL("Remove override"))
              msg=[_INTL("Nature {1} is natural.",oldnature),
                   _INTL("Nature {1} is being forced.",oldnature)][pkmn.natureflag ? 1 : 0]
              [email protected](msg,commands,cmd)
              # Break
              if cmd==-1
                break
              # Set nature override
              elsif cmd>=0 && cmd<PBNatures.getCount
                pkmn.setNature(cmd)
                pkmn.calcStats
              # Remove override
              elsif cmd==PBNatures.getCount
                pkmn.natureflag=nil
              end
              @scene.pbHardRefresh
            end
          ### Shininess ###
          when 6
            cmd=0
            loop do
              oldshiny=(pkmn.isShiny?) ? _INTL("shiny") : _INTL("normal")
              msg=[_INTL("Shininess ({1}) is natural.",oldshiny),
                   _INTL("Shininess ({1}) is being forced.",oldshiny)][pkmn.shinyflag!=nil ? 1 : 0]
              [email protected](msg,[
                   _INTL("Make shiny"),
                   _INTL("Make normal"),
                   _INTL("Remove override")],cmd)
              # Break
              if cmd==-1
                break
              # Make shiny
              elsif cmd==0
                pkmn.makeShiny
              # Make normal
              elsif cmd==1
                pkmn.makeNotShiny
              # Remove override
              elsif cmd==2
                pkmn.shinyflag=nil
              end
              @scene.pbHardRefresh
            end
          ### Form ###
          when 7
            params=ChooseNumberParams.new
            params.setRange(0,100)
            params.setDefaultValue(pkmn.form)
            f=Kernel.pbMessageChooseNumber(_INTL("Set the Pokémon's form."),params)
            if f!=pkmn.form
              pkmn.form=f
              pbDisplay(_INTL("{1}'s form was set to {2}.",pkmn.name,pkmn.form))
              pbSeenForm(pkmn)
              @scene.pbHardRefresh
            end
          ### Happiness ###
          when 8
            params=ChooseNumberParams.new
            params.setRange(0,255)
            params.setDefaultValue(pkmn.happiness)
            h=Kernel.pbMessageChooseNumber(
               _INTL("Set the Pokémon's happiness (max. 255)."),params)
            if h!=pkmn.happiness
              pkmn.happiness=h
              pbDisplay(_INTL("{1}'s happiness was set to {2}.",pkmn.name,pkmn.happiness))
              @scene.pbHardRefresh
            end
          ### EV/IV/pID ###
          when 9
            stats=[_INTL("HP"),_INTL("Attack"),_INTL("Defense"),
                   _INTL("Speed"),_INTL("Sp. Attack"),_INTL("Sp. Defense")]
            cmd=0
            loop do
              persid=sprintf("0x%08X",pkmn.personalID)
              [email protected](_INTL("Personal ID is {1}.",persid),[
                 _INTL("Set EVs"),
                 _INTL("Set IVs"),
                 _INTL("Randomise pID")],cmd)
              case cmd
              # Break
              when -1
                break
              # Set EVs
              when 0
                cmd2=0
                loop do
                  evcommands=[]
                  for i in 0...stats.length
                    evcommands.push(stats[i]+" (#{pkmn.ev[i]})")
                  end
                  [email protected](_INTL("Change which EV?"),evcommands,cmd2)
                  if cmd2==-1
                    break
                  elsif cmd2>=0 && cmd2<stats.length
                    params=ChooseNumberParams.new
                    params.setRange(0,255)
                    params.setDefaultValue(pkmn.ev[cmd2])
                    params.setCancelValue(pkmn.ev[cmd2])
                    f=Kernel.pbMessageChooseNumber(
                       _INTL("Set the EV for {1} (max. 255).",stats[cmd2]),params)
                    pkmn.ev[cmd2]=f
                    pkmn.calcStats
                    @scene.pbHardRefresh
                  end
                end
              # Set IVs
              when 1
                cmd2=0
                loop do
                  hiddenpower=pbHiddenPower(pkmn.iv)
                  msg=_INTL("Hidden Power:\n{1}, power {2}.",PBTypes.getName(hiddenpower[0]),hiddenpower[1])
                  ivcommands=[]
                  for i in 0...stats.length
                    ivcommands.push(stats[i]+" (#{pkmn.iv[i]})")
                  end
                  ivcommands.push(_INTL("Randomise all"))
                  [email protected](msg,ivcommands,cmd2)
                  if cmd2==-1
                    break
                  elsif cmd2>=0 && cmd2<stats.length
                    params=ChooseNumberParams.new
                    params.setRange(0,31)
                    params.setDefaultValue(pkmn.iv[cmd2])
                    params.setCancelValue(pkmn.iv[cmd2])
                    f=Kernel.pbMessageChooseNumber(
                       _INTL("Set the IV for {1} (max. 31).",stats[cmd2]),params)
                    pkmn.iv[cmd2]=f
                    pkmn.calcStats
                    @scene.pbHardRefresh
                  elsif cmd2==ivcommands.length-1
                    pkmn.iv[0]=rand(32)
                    pkmn.iv[1]=rand(32)
                    pkmn.iv[2]=rand(32)
                    pkmn.iv[3]=rand(32)
                    pkmn.iv[4]=rand(32)
                    pkmn.iv[5]=rand(32)
                    pkmn.calcStats
                    @scene.pbHardRefresh
                  end
                end
              # Randomise pID
              when 2
                pkmn.personalID=rand(256)
                pkmn.personalID|=rand(256)<<8
                pkmn.personalID|=rand(256)<<16
                pkmn.personalID|=rand(256)<<24
                pkmn.calcStats
                @scene.pbHardRefresh
              end
            end
          ### Pokérus ###
          when 10
            cmd=0
            loop do
              pokerus=(pkmn.pokerus) ? pkmn.pokerus : 0
              msg=[_INTL("{1} doesn't have Pokérus.",pkmn.name),
                   _INTL("Has strain {1}, infectious for {2} more days.",pokerus/16,pokerus%16),
                   _INTL("Has strain {1}, not infectious.",pokerus/16)][pkmn.pokerusStage]
              [email protected](msg,[
                   _INTL("Give random strain"),
                   _INTL("Make not infectious"),
                   _INTL("Clear Pokérus")],cmd)
              # Break
              if cmd==-1
                break
              # Give random strain
              elsif cmd==0
                pkmn.givePokerus
              # Make not infectious
              elsif cmd==1
                strain=pokerus/16
                p=strain<<4
                pkmn.pokerus=p
              # Clear Pokérus
              elsif cmd==2
                pkmn.pokerus=0
              end
            end
          ### Ownership ###
          when 11
            cmd=0
            loop do
              gender=[_INTL("Male"),_INTL("Female"),_INTL("Unknown")][pkmn.otgender]
              msg=[_INTL("Player's Pokémon\n{1}\n{2}\n{3} ({4})",pkmn.ot,gender,pkmn.publicID,pkmn.trainerID),
                   _INTL("Foreign Pokémon\n{1}\n{2}\n{3} ({4})",pkmn.ot,gender,pkmn.publicID,pkmn.trainerID)
                  ][pkmn.isForeign?($Trainer) ? 1 : 0]
              [email protected](msg,[
                   _INTL("Make player's"),
                   _INTL("Set OT's name"),
                   _INTL("Set OT's gender"),
                   _INTL("Random foreign ID"),
                   _INTL("Set foreign ID")],cmd)
              # Break
              if cmd==-1
                break
              # Make player's
              elsif cmd==0
                pkmn.trainerID=$Trainer.id
                pkmn.ot=$Trainer.name
                pkmn.otgender=$Trainer.gender
              # Set OT's name
              elsif cmd==1
                newot=pbEnterPlayerName(_INTL("{1}'s OT's name?",pkmn.name),1,7)
                pkmn.ot=newot
              # Set OT's gender
              elsif cmd==2
                [email protected](_INTL("Set OT's gender."),
                   [_INTL("Male"),_INTL("Female"),_INTL("Unknown")])
                pkmn.otgender=cmd2 if cmd2>=0
              # Random foreign ID
              elsif cmd==3
                pkmn.trainerID=$Trainer.getForeignID
              # Set foreign ID
              elsif cmd==4
                params=ChooseNumberParams.new
                params.setRange(0,65535)
                params.setDefaultValue(pkmn.publicID)
                val=Kernel.pbMessageChooseNumber(
                   _INTL("Set the new ID (max. 65535)."),params)
                pkmn.trainerID=val
                pkmn.trainerID|=val<<16
              end
            end
          ### Nickname ###
          when 12
            cmd=0
            loop do
              speciesname=PBSpecies.getName(pkmn.species)
              msg=[_INTL("{1} has the nickname {2}.",speciesname,pkmn.name),
                   _INTL("{1} has no nickname.",speciesname)][pkmn.name==speciesname ? 1 : 0]
              [email protected](msg,[
                   _INTL("Rename"),
                   _INTL("Erase name")],cmd)
              # Break
              if cmd==-1
                break
              # Rename
              elsif cmd==0
                newname=pbEnterPokemonName(_INTL("{1}'s nickname?",speciesname),0,10,"",pkmn)
                pkmn.name=(newname=="") ? speciesname : newname
                @scene.pbHardRefresh
              # Erase name
              elsif cmd==1
                pkmn.name=speciesname
              end
            end
          ### Poké Ball ###
          when 13
            cmd=0
            loop do
              oldball=PBItems.getName(pbBallTypeToBall(pkmn.ballused))
              commands=[]; balls=[]
              for key in $BallTypes.keys
                item=getID(PBItems,$BallTypes[key])
                balls.push([key,PBItems.getName(item)]) if item && item>0
              end
              balls.sort! {|a,b| a[1]<=>b[1]}
              for i in 0...commands.length
                cmd=i if pkmn.ballused==balls[i][0]
              end
              for i in balls
                commands.push(i[1])
              end
              [email protected](_INTL("{1} used.",oldball),commands,cmd)
              if cmd==-1
                break
              else
                pkmn.ballused=balls[cmd][0]
              end
            end
          ### Ribbons ###
          when 14
            cmd=0
            loop do
              commands=[]
              for i in 1..PBRibbons.maxValue
                commands.push(_INTL("{1} {2}",
                   pkmn.hasRibbon?(i) ? "[X]" : "[  ]",PBRibbons.getName(i)))
              end
              [email protected](_INTL("{1} ribbons.",pkmn.ribbonCount),commands,cmd)
              if cmd==-1
                break
              elsif cmd>=0 && cmd<commands.length
                if pkmn.hasRibbon?(cmd+1)
                  pkmn.takeRibbon(cmd+1)
                else
                  pkmn.giveRibbon(cmd+1)
                end
              end
            end
          ### Egg ###
          when 15
            cmd=0
            loop do
              msg=[_INTL("Not an egg"),
                   _INTL("Egg with eggsteps: {1}.",pkmn.eggsteps)][pkmn.isEgg? ? 1 : 0]
              [email protected](msg,[
                   _INTL("Make egg"),
                   _INTL("Make Pokémon"),
                   _INTL("Set eggsteps to 1")],cmd)
              # Break
              if cmd==-1
                break
              # Make egg
              elsif cmd==0
                if pbHasEgg?(pkmn.species) ||
                   pbConfirm(_INTL("{1} cannot be an egg. Make egg anyway?",PBSpecies.getName(pkmn.species)))
                  pkmn.level=EGGINITIALLEVEL
                  pkmn.calcStats
                  pkmn.name=_INTL("Egg")
                  dexdata=pbOpenDexData
                  pbDexDataOffset(dexdata,pkmn.species,21)
                  pkmn.eggsteps=dexdata.fgetw
                  dexdata.close
                  pkmn.hatchedMap=0
                  pkmn.obtainMode=1
                  @scene.pbHardRefresh
                end
              # Make Pokémon
              elsif cmd==1
                pkmn.name=PBSpecies.getName(pkmn.species)
                pkmn.eggsteps=0
                pkmn.hatchedMap=0
                pkmn.obtainMode=0
                @scene.pbHardRefresh
              # Set eggsteps to 1
              elsif cmd==2
                pkmn.eggsteps=1 if pkmn.eggsteps>0
              end
            end
          ### Shadow Pokémon ###
          when 16
            cmd=0
            loop do
              msg=[_INTL("Not a Shadow Pokémon."),
                   _INTL("Heart gauge is {1}.",pkmn.heartgauge)][(pkmn.isShadow? rescue false) ? 1 : 0]
              [email protected](msg,[
                 _INTL("Make Shadow"),
                 _INTL("Lower heart gauge")],cmd)
              # Break
              if cmd==-1
                break
              # Make Shadow
              elsif cmd==0
                if !(pkmn.isShadow? rescue false) && pkmn.respond_to?("makeShadow")
                  pkmn.makeShadow
                  pbDisplay(_INTL("{1} is now a Shadow Pokémon.",pkmn.name))
                  @scene.pbHardRefresh
                else
                  pbDisplay(_INTL("{1} is already a Shadow Pokémon.",pkmn.name))
                end
              # Lower heart gauge
              elsif cmd==1
                if (pkmn.isShadow? rescue false)
                  prev=pkmn.heartgauge
                  pkmn.adjustHeart(-700)
                  Kernel.pbMessage(_INTL("{1}'s heart gauge was lowered from {2} to {3} (now stage {4}).",
                     pkmn.name,prev,pkmn.heartgauge,pkmn.heartStage))
                  pbReadyToPurify(pkmn)
                else
                  Kernel.pbMessage(_INTL("{1} is not a Shadow Pokémon.",pkmn.name))
                end
              end
            end
          ### Make Mystery Gift ###
          when 17
           ## pbCreateMysteryGift(0,pkmn)
          ### Duplicate ###
          when 17
            if pbConfirm(_INTL("Are you sure you want to copy this Pokémon?"))
              clonedpkmn=pkmn.clone
              clonedpkmn.iv=pkmn.iv.clone
              clonedpkmn.ev=pkmn.ev.clone
              if @storage.pbMoveCaughtToParty(clonedpkmn)
                if selected[0]!=-1
                  pbDisplay(_INTL("The duplicated Pokémon was moved to your party."))
                end
              else
                [email protected]
                [email protected](clonedpkmn)
                if newbox<0
                  pbDisplay(_INTL("All boxes are full."))
                elsif newbox!=oldbox
                  pbDisplay(_INTL("The duplicated Pokémon was moved to box \"{1}.\"",@storage[newbox].name))
                  @storage.currentBox=oldbox
                end
              end
              @scene.pbHardRefresh
              break
            end
          ### Delete ###
          when 18
            if pbConfirm(_INTL("Are you sure you want to delete this Pokémon?"))
              @scene.pbRelease(selected,heldpoke)
              if heldpoke
                @heldpkmn=nil
              else
                @storage.pbDelete(selected[0],selected[1])
              end
              @scene.pbRefresh
              pbDisplay(_INTL("The Pokémon was deleted."))
              break
            end
          end
        end
      end
    
      def selectPokemon(index)
        pokemon=@storage[@currentbox,index]
        if !pokemon
          return nil
        end
      end
    end
    
    
    
    class Interpolator
      ZOOM_X  = 1
      ZOOM_Y  = 2
      X       = 3
      Y       = 4
      OPACITY = 5
      COLOR   = 6
      WAIT    = 7
    
      def initialize
        @tweening=false
        @tweensteps=[]
        @sprite=nil
        @frames=0
        @step=0
      end
    
      def tweening?
        return @tweening
      end
    
      def tween(sprite,items,frames)
        @tweensteps=[]
        if sprite && !sprite.disposed? && frames>0
          @frames=frames
          @step=0
          @sprite=sprite
          for item in items
            case item[0]
            when ZOOM_X
              @tweensteps[item[0]]=[sprite.zoom_x,item[1]-sprite.zoom_x]
            when ZOOM_Y
              @tweensteps[item[0]]=[sprite.zoom_y,item[1]-sprite.zoom_y]
            when X
              @tweensteps[item[0]]=[sprite.x,item[1]-sprite.x]
            when Y
              @tweensteps[item[0]]=[sprite.y,item[1]-sprite.y]
            when OPACITY
              @tweensteps[item[0]]=[sprite.opacity,item[1]-sprite.opacity]
            when COLOR
              @tweensteps[item[0]]=[sprite.color.clone,Color.new(
                 item[1].red-sprite.color.red,
                 item[1].green-sprite.color.green,
                 item[1].blue-sprite.color.blue,
                 item[1].alpha-sprite.color.alpha
              )]
            end
          end
          @tweening=true
        end
      end
    
      def update
        if @tweening
          t=(@step*1.0)/@frames
          for i in [email protected]
            item=@tweensteps[i]
            next if !item
            case i
            when ZOOM_X
              @sprite.zoom_x=item[0]+item[1]*t
            when ZOOM_Y
              @sprite.zoom_y=item[0]+item[1]*t
            when X
              @sprite.x=item[0]+item[1]*t
            when Y
              @sprite.y=item[0]+item[1]*t
            when OPACITY
              @sprite.opacity=item[0]+item[1]*t
            when COLOR
              @sprite.color=Color.new(
                 item[0].red+item[1].red*t,
                 item[0].green+item[1].green*t,
                 item[0].blue+item[1].blue*t,
                 item[0].alpha+item[1].alpha*t
              )
            end
          end
          @step+=1
          if @step==@frames
            @step=0
            @frames=0
            @tweening=false
          end
        end
      end
    end
    
    
    
    class PokemonBoxIcon < IconSprite
      def initialize(pokemon,viewport=nil,red=false)
        super(0,0,viewport)
        @release=Interpolator.new
        @startRelease=false
        @pokemon=pokemon
        if pokemon
          b=pbPokemonIconFile(pokemon)
          self.setBitmap(b.clone)
          if red
            makeRed
          end
        end
        self.src_rect=Rect.new(0,0,64,64)
      end
    
      def getPoke
        return @pokemon if @pokemon
      end
      
      def makeRed
        np=BitmapCache.load_bitmap(pbPokemonIconFile(@pokemon))
        nb=np.clone
        for x in 0...64
          for y in 0...64
            pixel=nb.get_pixel(x,y)
            nx=nb.get_pixel(x+1,y)
            px=nb.get_pixel(x-1,y)
            
            ny=nb.get_pixel(x,y+1)
            py=nb.get_pixel(x,y-1)
            
            if pixel.alpha==0 && nx.alpha==255 && pixel.red!=255
              nb.fill_rect(x-1,y,1,1,Color.new(255,0,0))
            elsif pixel.alpha==255 && nx.alpha==0 && pixel.red!=255
              nb.fill_rect(x+1,y,1,1,Color.new(255,0,0))
            end
            
            if pixel.alpha==0 && ny.alpha==255 && pixel.red!=255
              nb.fill_rect(x,y-1,1,1,Color.new(255,0,0))
            elsif pixel.alpha==255 && ny.alpha==0 && pixel.red!=255
              nb.fill_rect(x,y+1,1,1,Color.new(255,0,0))
            end
          end
        end
        pbCopyBitmapRect(self.bitmap,nb,0,0,Rect.new(0,0,64,64))
      end
      
      def disposeRed
        for x in 0...64
          for y in 0...64
            pixel=self.bitmap.get_pixel(x,y)
            if pixel.red==255
              self.bitmap.set_pixel(x,y,Color.new(0,0,0,0))
            end
          end
        end
      end
      
      def release
        self.ox=32
        self.oy=32
        self.x+=32
        self.y+=32
        @release.tween(self,[
           [Interpolator::ZOOM_X,0],
           [Interpolator::ZOOM_Y,0],
           [Interpolator::OPACITY,0]
        ],100)
        @startRelease=true
      end
    
      def releasing?
        return @release.tweening?
      end
    
      def update
        super
        @release.update
        self.color=Color.new(0,0,0,0)
        dispose if @startRelease && !releasing?
      end
    end
    
    
    
    class PokemonBoxArrow < SpriteWrapper
      def initialize(viewport=nil)
        super(viewport)
        @frame=0
        @holding=false
        @updating=false
        @grabbingState=0
        @placingState=0
        @heldpkmn=nil
        @swapsprite=nil
        @fist=AnimatedBitmap.new("Graphics/Pictures/Boxes/boxpoint2")
        @point1=AnimatedBitmap.new("Graphics/Pictures/Boxes/boxpoint1")
        @point2=AnimatedBitmap.new("Graphics/Pictures/Boxes/boxpoint2")
        @grab=AnimatedBitmap.new("Graphics/Pictures/Boxes/boxpoint2")
        @currentBitmap=@fist
        @spriteX=self.x
        @spriteY=self.y
        [email protected]
      end
    
      def heldPokemon
        @heldpkmn=nil if @heldpkmn && @heldpkmn.disposed?
        @holding=false if !@heldpkmn
        return @heldpkmn
      end
    
      def visible=(value)
        super
        sprite=heldPokemon
        sprite.visible=value if sprite
      end
    
      def color=(value)
        super
        sprite=heldPokemon
        sprite.color=value if sprite
      end
    
      def dispose
        @fist.dispose
        @point1.dispose
        @point2.dispose
        @grab.dispose
        @heldpkmn.dispose if @heldpkmn
        super
      end
    
      def holding?
        return self.heldPokemon && @holding
      end
    
      def grabbing?
        return @grabbingState>0
      end
    
      def placing?
        return @placingState>0
      end
    
      def x=(value)
        super
        @spriteX=x if !@updating
        heldPokemon.x=self.x-20 if holding?
      end
    
      def y=(value)
        super
        @spriteY=y if !@updating
        heldPokemon.y=self.y-6 if holding?
      end
    
      def setSprite(sprite)
        if holding?
          @heldpkmn=sprite
          @heldpkmn.viewport=self.viewport if @heldpkmn
          @heldpkmn.z=1 if @heldpkmn
          @holding=false if !@heldpkmn
          self.z=2
        end
      end
    
      def deleteSprite
        @holding=false
        if @heldpkmn
          @heldpkmn.dispose
          @heldpkmn=nil
        end
      end
    
      def grab(sprite)
        @grabbingState=1
        @heldpkmn=sprite
        @heldpkmn.viewport=self.viewport
        @heldpkmn.z=1
        self.z=2
      end
    
      def place
        @placingState=1
      end
    
      def release
        if @heldpkmn
          @heldpkmn.release
        end
      end
    
      def update
        @updating=true
        super
        heldpkmn=heldPokemon
        heldpkmn.update if heldpkmn
        self.visible=true if holding? && self.visible!=true
        @fist.update
        @point2.update
        @point1.update
        @grab.update
        [email protected]
        @holding=false if !heldpkmn
        if @grabbingState>0
          if @grabbingState<=8
            @currentBitmap=@grab
            [email protected]
            self.y=@spriteY+(@grabbingState)*2
            @grabbingState+=1
          elsif @grabbingState<=16
            @holding=true
            @currentBitmap=@fist
            [email protected]
            self.y=@spriteY+(16-@grabbingState)*2
            @grabbingState+=1
          else
            @grabbingState=0
          end
        elsif @placingState>0
          if @placingState<=8
            @currentBitmap=@fist
            [email protected]
            self.y=@spriteY+(@placingState)*2
            @placingState+=1
          elsif @placingState<=16
            @holding=false
            @heldpkmn=nil
            @currentBitmap=@grab
            [email protected]
            self.y=@spriteY+(16-@placingState)*2
            @placingState+=1
          else
            @placingState=0
          end
        elsif holding?
          @currentBitmap=@fist
          [email protected]
        else
          self.x=@spriteX
          self.y=@spriteY
          if (@frame/20)==1
            @currentBitmap=@point2
            [email protected]
          else
            @currentBitmap=@point1
            [email protected]
          end
        end
        @frame+=1
        @frame=0 if @frame==40
        @updating=false
      end
    end
    
    
    
    class PokemonBoxPartySprite < SpriteWrapper
      attr_accessor :pokemonsprites
      
      def deletePokemon(index)
        @pokemonsprites[index].dispose
        @pokemonsprites[index]=nil
        @pokemonsprites.compact!
        refresh
      end
    
      def getPokemon(index)
        return @pokemonsprites[index]
      end
    
      def setPokemon(index,sprite)
        @pokemonsprites[index]=sprite
        @pokemonsprites.compact!
        refresh
      end
    
      def grabPokemon(index,arrow)
        sprite=@pokemonsprites[index]
        if sprite
          arrow.grab(sprite)
          @pokemonsprites[index]=nil
          @pokemonsprites.compact!
          refresh
        end
      end
    
      def x=(value)
        super
        refresh
      end
    
      def y=(value)
        super
        refresh
      end
    
      def color=(value)
        super
        for i in 0...6
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].color=pbSrcOver(@pokemonsprites[i].color,value)
          end
        end
        refresh
      end
    
      def visible=(value)
        super
        for i in 0...6
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].visible=value
          end
        end
        refresh
      end
    
      def initialize(party,viewport=nil)
        super(viewport)
        @boxbitmap=AnimatedBitmap.new("Graphics/Pictures/Boxes/boxpartytab")
        @pokemonsprites=[]
        @party=party
        for i in 0...6
          @pokemonsprites[i]=nil
          pokemon=@party[i]
          if pokemon
            @pokemonsprites[i]=PokemonBoxIcon.new(pokemon,viewport)
          end
        end
        @contents=BitmapWrapper.new(172,352)
        self.bitmap=@contents
        self.x=346
        self.y=DEFAULTSCREENHEIGHT-288
        refresh
      end
    
      def dispose
        for i in 0...6
          @pokemonsprites[i].dispose if @pokemonsprites[i]
        end
        @contents.dispose
        @boxbitmap.dispose
        super
      end
    
      def changeOpacity(opacity) # Make pokesprites a bit transparents
        for i in 0...6
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].opacity=opacity
          end
        end
      end
      
      def hardRefresh(op=255)
        for i in 0...6
          @pokemonsprites[i].dispose if @pokemonsprites[i] && @pokemonsprites[i]!=nil
          pokemon=@party[i]
          if pokemon
            @pokemonsprites[i]=PokemonBoxIcon.new(pokemon,viewport)
            @pokemonsprites[i].opacity=op
          end
        end
        refresh
      end
      
      def refresh
        @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,352))
        xvalues=[8,88,8,88,8,88]
        yvalues=[0,16,64,80,128,144]
        for j in 0...6
          @pokemonsprites[j]=nil if @pokemonsprites[j] && @pokemonsprites[j].disposed?
        end
        @pokemonsprites.compact!
        for j in 0...6
          sprite=@pokemonsprites[j]
          if sprite && !sprite.disposed?
            sprite.viewport=self.viewport
            sprite.z=0
            sprite.x=self.x+xvalues[j]
            sprite.y=self.y+yvalues[j]
          end
        end
      end
    
      def update
        super
        for i in 0...6
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].update
          end
        end
      end
    end
    
    
    
    class MosaicPokemonSprite < PokemonSprite
      def initialize(*args)
        super(*args)
        @mosaic=0
        @inrefresh=false
        @mosaicbitmap=nil
        @mosaicbitmap2=nil
        @oldbitmap=self.bitmap
      end
    
      attr_reader :mosaic
    
      def mosaic=(value)
        @mosaic=value
        @mosaic=0 if @mosaic<0
        mosaicRefresh(@oldbitmap)
      end
    
      def dispose
        super
        @mosaicbitmap.dispose if @mosaicbitmap
        @mosaicbitmap=nil
        @mosaicbitmap2.dispose if @mosaicbitmap2
        @mosaicbitmap2=nil
      end
    
      def bitmap=(value)
        super
        mosaicRefresh(value)
      end
    
      def mosaicRefresh(bitmap)
        return if @inrefresh
        @inrefresh=true
        @oldbitmap=bitmap
        if @mosaic<=0 || !@oldbitmap
          @mosaicbitmap.dispose if @mosaicbitmap
          @mosaicbitmap=nil
          @mosaicbitmap2.dispose if @mosaicbitmap2
          @mosaicbitmap2=nil
          self.bitmap=@oldbitmap
        else
          newWidth=[(@oldbitmap.width/@mosaic),1].max
          newHeight=[(@oldbitmap.height/@mosaic),1].max
          @mosaicbitmap2.dispose if @mosaicbitmap2
          @mosaicbitmap=pbDoEnsureBitmap(@mosaicbitmap,newWidth,newHeight)
          @mosaicbitmap.clear
          @mosaicbitmap2=pbDoEnsureBitmap(@mosaicbitmap2,
             @oldbitmap.width,@oldbitmap.height)
          @mosaicbitmap2.clear
          @mosaicbitmap.stretch_blt(Rect.new(0,0,newWidth,newHeight),
             @oldbitmap,@oldbitmap.rect)
          @mosaicbitmap2.stretch_blt(
             Rect.new(-@mosaic/2+1,-@mosaic/2+1,
             @mosaicbitmap2.width,@mosaicbitmap2.height),
             @mosaicbitmap,Rect.new(0,0,newWidth,newHeight))
          self.bitmap=@mosaicbitmap2
        end
        @inrefresh=false
      end
    end
    
    
    
    class AutoMosaicPokemonSprite < MosaicPokemonSprite
      def update
        super
        self.mosaic-=1
      end
    end
    
    
    
    class PokemonBoxSprite < SpriteWrapper
      attr_accessor :refreshBox
      attr_accessor :refreshSprites
      attr_accessor :lefta
      attr_accessor :righta
      attr_accessor :pokemonsprites
    
      def deletePokemon(index)
        @pokemonsprites[index].dispose
        @pokemonsprites[index]=nil
        refresh
      end
    
      def getPokemon(index)
        return @pokemonsprites[index]
      end
    
      def setPokemon(index,sprite)
        @pokemonsprites[index]=sprite
        refresh
      end
    
      def grabPokemon(index,arrow)
        sprite=@pokemonsprites[index]
        if sprite
          arrow.grab(sprite)
          @pokemonsprites[index]=nil
          refresh
        end
      end
    
      def x=(value)
        super
        refresh
      end
    
      def y=(value)
        super
        refresh
      end
    
      def color=(value)
        super
        if @refreshSprites
          @lefta.color=value
          @righta.color=value
          for i in 0...30
            if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
              @pokemonsprites[i].color=value
            end
          end
        end
        refresh
      end
    
      def visible=(value)
        super
        @lefta.visible=value
        @righta.visible=value
        for i in 0...30
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].visible=value
          end
        end
        refresh
      end
    
      def getBoxBitmap
        if !@bg || @bg!=@storage[@boxnumber].background
          curbg=@storage[@boxnumber].background
          if !curbg || curbg.length==0
            boxid=@boxnumber%24
            @bg="box#{boxid}"
          else
            @bg="#{curbg}"
          end
          @boxbitmap.dispose if @boxbitmap
          @boxbitmap=AnimatedBitmap.new("Graphics/Pictures/Boxes/#{@bg}")
        end
      end
    
      def initialize(storage,boxnumber,viewport=nil)
        super(viewport)
        @storage=storage
        @boxnumber=boxnumber
        @refreshBox=true
        @refreshSprites=true
        @bg=nil
        @boxbitmap=nil
        getBoxBitmap
        @pokemonsprites=[]
        for i in 0...30
          @pokemonsprites[i]=nil
          pokemon=@storage[boxnumber,i]
          if pokemon
            @pokemonsprites[i]=PokemonBoxIcon.new(pokemon,viewport)
          else
            @pokemonsprites[i]=PokemonBoxIcon.new(nil,viewport)
          end
        end
        
        @lefta = Sprite.new(viewport)
        @lefta.bitmap = RPG::Cache.picture("Boxes/boxleftright")
        @lefta.src_rect.set(0,0,44,38)
        @lefta.pos(14,38)
    
        @righta = Sprite.new(viewport)
        @righta.bitmap = RPG::Cache.picture("Boxes/boxleftright")
        @righta.src_rect.set(44,0,44,38)
        @righta.pos(276,38)
    
        @contents=BitmapWrapper.new(324,296)
        self.bitmap=@contents
        self.x=13
        self.y=39
        refresh
      end
    
      def dispose
        if !disposed?
          for i in 0...30
            @pokemonsprites[i].dispose if @pokemonsprites[i]
            @pokemonsprites[i]=nil
          end
          @contents.dispose
          @boxbitmap.dispose
          @lefta.dispose
          @righta.dispose
          super
        end
      end
    
      def refresh
        if @refreshBox
          boxname=@storage[@boxnumber].name
          getBoxBitmap
          @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,324,296))
          pbSetSystemFont(@contents)
          [email protected]_size(boxname).width
          xval=162-(widthval/2)-8
          pbDrawShadowText(@contents,xval,4,widthval,32,boxname,
             Color.new(41,41,41),Color.new(132,132,132))
          @refreshBox=false
        end
        @righta.x=self.x+266
        @lefta.x=self.x
        
        yval=self.y+26
        for j in 0...5
          xval=self.x+4
          for k in 0...6
            sprite=@pokemonsprites[j*6+k]
            if sprite && !sprite.disposed?
              sprite.viewport=self.viewport
              sprite.z=0
              sprite.x=xval
              sprite.y=yval
            end
            xval+=48
          end
          yval+=48
        end
      end
    
      def changeOpacity(opacity) # Make pokesprites a bit transparents
        for i in 0...30
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].opacity=opacity
          end
        end
      end
      
      def update
        super
        for i in 0...30
          if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
            @pokemonsprites[i].update
          end
          
        end
      end
    end
    
    
    
    class PokemonStorageScene
      def initialize
        @command=0
      end
    
      def pbStartBox(screen,command)
        @screen=screen
        @storage=screen.storage
        @bgviewport=Viewport.new(0,0,Graphics.width,DEFAULTSCREENHEIGHT)
        @bgviewport.z=99999
        
        @boxviewport=Viewport.new(0,400,Graphics.width,DEFAULTSCREENHEIGHT)
        @boxviewport.z=99999
        
        @boxsidesviewport=Viewport.new(0,400,Graphics.width,DEFAULTSCREENHEIGHT)
        @boxsidesviewport.z=99999
        
        @arrowviewport=Viewport.new(0,400,Graphics.width,DEFAULTSCREENHEIGHT)
        @arrowviewport.z=99999
        
        @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
        @viewport.z=99999
        
        @selection=0
        @partyopen=false
        @sprites={}
        @choseFromParty=false
        @command=command
        @movingmouse=false
        @mouseinfo=[]
        @canchangebox=true
    
        
        @sprites["bg"] = Sprite.new(@bgviewport)
        @sprites["bg"].bitmap = RPG::Cache.picture("Boxes/boxbg")
        
        @sprites["bginfo"] = Sprite.new(@bgviewport)
        @sprites["bginfo"].bitmap = RPG::Cache.picture("Boxes/boxbginfo")
        @sprites["bginfo"].visible=false
        
        @sprites["bg2"] = Sprite.new(@boxviewport)
        @sprites["bg2"].bitmap = RPG::Cache.picture("Boxes/boxbg2")
        @sprites["bg2"].z=999999
    
        @sprites["box"]=PokemonBoxSprite.new(@storage,@storage.currentBox,@boxviewport)
        @sprites["box"].changeOpacity(150) if @command==1
              
        @sprites["overlay"]=BitmapSprite.new(Graphics.width,DEFAULTSCREENHEIGHT,@viewport)
        @sprites["overlay2"]=BitmapSprite.new(Graphics.width,DEFAULTSCREENHEIGHT,@viewport)
        @sprites["overlay2"].bitmap.clear
        pbSetSystemFont(@sprites["overlay2"].bitmap)
        
        @sprites["pokemon"]=AutoMosaicPokemonSprite.new(@viewport)
        pbSetSystemFont(@sprites["overlay"].bitmap)
        @sprites["boxparty"]=PokemonBoxPartySprite.new(@storage.party,@boxsidesviewport)
       
        @sprites["normalbar"]=IconSprite.new(0,DEFAULTSCREENHEIGHT-48,@boxsidesviewport)
        @sprites["normalbar"].setBitmap("Graphics/Pictures/normalbar")
        @sprites["normalbar"].z=9999
    
        @sprites["returnbuttom"]=Sprite.new(@boxsidesviewport)
        @sprites["returnbuttom"].bitmap=Cache.picture("globalicons")
        @sprites["returnbuttom"].x=460; @sprites["returnbuttom"].y=DEFAULTSCREENHEIGHT-74
        @sprites["returnbuttom"].src_rect.set(0, 64, 64, 64) 
        @sprites["returnbuttom"].z=9999
        
        @sprites["showparty"]=Sprite.new(@boxsidesviewport)
        @sprites["showparty"].bitmap=Cache.picture("Boxes/boxicons")
        @sprites["showparty"].y=DEFAULTSCREENHEIGHT-46
        @sprites["showparty"].src_rect.set(0, 0, 192, 44) 
        @sprites["showparty"].z=9999
        @sprites["showparty"].visible=false if @command==0
        
        if command!=1 # Drop down tab only on Deposit
          @sprites["boxparty"].x=346
          @sprites["boxparty"].y=DEFAULTSCREENHEIGHT
        end
        
        @sprites["arrow"]=PokemonBoxArrow.new(@arrowviewport)
        @sprites["arrow"].z+=1
        
        com=[
             _INTL("Withdraw Pokémon"),
             _INTL("Deposit Pokémon"),
             _INTL("Move Pokémon"),
            
          ]
          
          textstrings=[
           [com[command],16,18,false,Color.new(90,82,82),Color.new(165,165,173)]
         ]
    
        pbDrawTextPositions(@sprites["overlay2"].bitmap,textstrings)
    
        if command!=1
          pbSetArrow(@sprites["arrow"],@selection)
          pbUpdateOverlay(@selection)
          pbSetMosaic(@selection)
        else
          pbPartySetArrow(@sprites["arrow"],@selection)
          pbUpdateOverlay(@selection,@storage.party)
          pbSetMosaic(@selection)
        end
        
        pbFadeInAndShow(@sprites)
      end
    
      def pbCloseBox
        pbFadeOutAndHide(@sprites)  
        pbDisposeSpriteHash(@sprites)
        @boxviewport.dispose
        @boxsidesviewport.dispose
        @arrowviewport.dispose
      end
    
      def pbSetArrow(arrow,selection)
        case selection
        when -1, -4, -5 # Box name, move left, move right
          arrow.y=4
          arrow.x=74*2
        when -2 # Party Pokémon
          arrow.y=152*2
          arrow.x=38*2
        when -3 # Close Box
          arrow.y=143*2
          arrow.x=207*2
        else
          arrow.x = ((10+24*(selection%6) ) * 2)+18
          arrow.y = ((19+24*(selection/6) ) * 2)+16
        end
      end
    
      def pbChangeSelection(key,selection)
        case key
        when Input::UP
          if selection==-1 # Box name
            selection=-2 
            if @command==0 && selection==-2 # In withdraw
              selection=25
            end
          elsif selection==-2 # Party
            selection=25
          elsif selection==-3 # Close Box
            selection=28
          else
            selection-=6
            selection=-1 if selection<0
          end
        when Input::DOWN
          if selection==-1 # Box name
            selection=2
          elsif selection==-2 # Party
            selection=-1
          elsif selection==-3 # Close Box
            selection=-1
          else
            selection+=6
            selection=-2 if selection==30||selection==31||selection==32
            selection=-2 if selection==33||selection==34||selection==35
            if @command==0 && selection==-2 # In withdraw
              selection=-1
            end
          end
        when Input::RIGHT
          if selection==-1 # Box name
            selection=-5 # Move to next box
          elsif selection==-2
            selection=-2
          elsif selection==-3
            selection=-2
          else
            selection+=1
            selection=-20 if (selection==6 || selection==12) && @partyopen
            selection=-21 if (selection==18) && @partyopen
            selection=-22 if (selection==24 || selection==30) && @partyopen
            selection-=6 if selection%6==0
          end
        when Input::LEFT
          if selection==-1 # Box name
            selection=-4 # Move to previous box
          elsif selection==-2
            selection=-2
          elsif selection==-3
            selection=-2
          else
            selection-=1
            selection+=6 if selection==-1||selection%6==5
          end
        when 81
          if $mouse.inArea?(6*2,10,272*2,431*2)
          selection=-2
        elsif $mouse.inArea?(10,436,46,42)
          selection=-4
          end
        end
        return selection
      end
    
      def pbPartySetArrow(arrow,selection)
        if selection>=0
          xvalues=[186,227,186,227,186,227,38]
          yvalues=[42,51,74,83,106,115,152]
          arrow.angle=0
          arrow.mirror=false
          arrow.ox=0
          arrow.oy=0
          arrow.x=xvalues[selection]*2
          arrow.y=yvalues[selection]*2
        end
      end
    
      def pbPartyChangeSelection(key,selection)
        case key
        when Input::LEFT
          selection-=1
          if @command!=1
          selection=20 if selection==-1 #and @sprites["arrow"].holding?
          selection=20 if selection==1 #and @sprites["arrow"].holding?
          selection=20 if selection==3 #and @sprites["arrow"].holding?
          end
          selection=6 if selection<0
        when Input::RIGHT
          selection+=1
          selection=0 if selection>6
        when Input::UP
          if selection==6
            selection=5
          else
            selection-=2
            selection=6 if selection<0
          end
        when Input::DOWN
          if selection==6
            selection=0
          else
            selection+=2
            selection=6 if selection>6
          end
        end
        return selection
      end
    
      def pbSelectPartyInternal(party,depositing)
        selection=@selection
        pbPartySetArrow(@sprites["arrow"],selection) if !@movingmouse
        pbUpdateOverlay(selection,party) if !@movingmouse
        lastsel=1
      
        loop do
          Graphics.update
          Input.update
          
          changeBoxesWithMouse
          if @sprites["arrow"].heldPokemon==nil
          selectPokeWithMouse
          
          if @movingmouse
            updateMousePoke 
            
            if !@canchangebox
              @waitbb+=1
              @canchangebox=true if @waitbb==14
            end
            
            if $mouse.inArea?(10,436,46,42) && @canchangebox
              nextbox=(@storage.currentBox==0) ? @storage.maxBoxes-1 : @storage.currentBox-1
              pbSwitchBoxToLeft(nextbox)
              @storage.currentBox=nextbox
              @waitbb=0 
              @canchangebox=false
            elsif $mouse.inArea?(280,436,46,42) && @canchangebox
              nextbox=(@[email protected]) ? 0 : @storage.currentBox+1
              pbSwitchBoxToRight(nextbox)
              @storage.currentBox=nextbox
              @waitbb=0 
              @canchangebox=false
            elsif $mouse.over?(@sprites["showparty"],@sprites["showparty"].bitmap.width,
              @sprites["showparty"].bitmap.height/2)
              @selection=selection
              return -1
            end
              
            if !Input.pressed(Input::Mouse_Left)
              overParty=$mouse.over?(@sprites["boxparty"])
              @mouseinfo[0].z=@mouseinfo[1] # Back to original z
              @mouseinfo[0].disposeRed
              @movingmouse=false
              @canchangebox=true
              boxpos=overParty ? pbGetPartyPosFromMouse : pbGetBoxPosFromMouse
              newpos=boxpos
              seleb=overParty ? -1 : @storage.currentBox
              selectedp=[seleb,newpos]
              pokei=[@mouseinfo[4],@mouseinfo[3]]
      
              if @command==1 && 
                (@storage[seleb,newpos]!=nil or seleb==-1) # Deposit
                boxpos=-1 # Back to party
              end
                 
              if @mouseinfo[4]==-1 && @storage.party.length<=1 &&
                @storage[selectedp[0],selectedp[1]]==nil
                @sprites["boxparty"].setPokemon(@mouseinfo[3],@mouseinfo[0])
                @storage.party[@mouseinfo[3]]=@mouseinfo[2]
                @sprites["boxparty"].hardRefresh
              elsif boxpos==-1 # Back to original position
                @storage[@mouseinfo[4],@mouseinfo[3]]=@mouseinfo[2]
                if @mouseinfo[4][email protected]
                  @sprites["box"].setPokemon(@mouseinfo[3],@mouseinfo[0])
                else
                  @mouseinfo[0].dispose
                end
                @sprites["boxparty"].hardRefresh
              else
                pbPlaceMouse(selectedp,@mouseinfo[0],pokei,@mouseinfo[2])
                return -1 if @command!=-1
              end
              
              if @command==0 # Withdraw
                @sprites["box"].changeOpacity(255)
              elsif @command==1 # Deposit
                @sprites["box"].changeOpacity(150)
                @sprites["boxparty"].hardRefresh
              end
              
              if boxpos!=-1 && newpos==@mouseinfo[3] && 
                @storage.currentBox==@mouseinfo[4]
                return @mouseinfo[3]
              end
              
            end
          end
          
        else # Selected with manual
           if Input.triggerex?(Input::Mouse_Left) &&
             !$mouse.leftClick?(@sprites["showparty"])
              overParty=$mouse.over?(@sprites["boxparty"])
              selection=overParty ? pbGetPartyPosFromMouse : pbGetBoxPosFromMouse
              newpos=boxpos
              seleb=overParty ? -1 : @storage.currentBox
              if overParty
                pbPartySetArrow(@sprites["arrow"],selection)
                return selection
              else
                pbSetArrow(@sprites["arrow"],selection)
                return -1
              end
           end
        end
        
            if $mouse.leftClick?(@sprites["showparty"],@sprites["showparty"].bitmap.width,
              @sprites["showparty"].bitmap.height/2)
              return -1
            end
    
          key=-1
          key=Input::DOWN if Input.repeat?(Input::DOWN)
          key=Input::RIGHT if Input.repeat?(Input::RIGHT)
          key=Input::LEFT if Input.repeat?(Input::LEFT)
          key=Input::UP if Input.repeat?(Input::UP)
          if key>=0
            pbPlayCursorSE() if !@movingmouse
            if @sprites["arrow"].visible==true
              newselection=pbPartyChangeSelection(key,selection)
            else
              @sprites["arrow"].visible=true if !@movingmouse
              newselection=-2
            end
             
            if newselection==-1
              return -1 if !depositing
            elsif newselection==-2
              selection=lastsel
            else
              selection=newselection
            end
            if selection==20
               @selection=selection
              return selection
            end
    
            pbPartySetArrow(@sprites["arrow"],selection)
            
            lastsel=selection if selection>0
            pbUpdateOverlay(selection,party)
            pbSetMosaic(selection)
          end
          pbUpdateSpriteHash(@sprites)
          if Input.trigger?(Input::C)
            if selection>=0 && selection<6
              @selection=selection
              return selection
            elsif selection==6 # Close Box 
              @selection=selection
              return (depositing) ? -3 : -1
            end
          end
          if Input.trigger?(Input::B) || $mouse.leftClick?(@sprites["returnbuttom"],64,64) 
            globalIconAnim(@sprites["returnbuttom"])
            @selection=selection
            return -1
          end
        end
      end
    
      def pbSelectParty(party)
        return pbSelectPartyInternal(party,true)
      end
    
      def pbChangeBackground(wp)
        @sprites["box"].refreshSprites=false
        alpha=0
        Graphics.update
        pbUpdateSpriteHash(@sprites)
        16.times do
          alpha+=16
          Graphics.update
          Input.update
          @sprites["box"].color=Color.new(248,248,248,alpha)
          pbUpdateSpriteHash(@sprites)
        end
        @sprites["box"].refreshBox=true
        @storage[@storage.currentBox].background="box#{wp}"
        4.times do
          Graphics.update
          Input.update
          pbUpdateSpriteHash(@sprites)
        end
        16.times do
          alpha-=16
          Graphics.update
          Input.update
          @sprites["box"].color=Color.new(248,248,248,alpha)
          pbUpdateSpriteHash(@sprites)
        end
        @sprites["box"].refreshSprites=true
      end
    
      def pbSwitchBoxToRight(newbox)
        newbox=PokemonBoxSprite.new(@storage,newbox,@boxviewport)
        if @command==0 && @movingmouse # Withdraw
          newbox.changeOpacity(150)
        elsif @command==1
          newbox.changeOpacity(150)
        end
        newbox.x=256
        Graphics.frame_reset
        begin
          Graphics.update
          Input.update
          @sprites["box"].x-=32
          newbox.x-=32
          pbUpdateSpriteHash(@sprites)
          updateMousePoke if @movingmouse
        end until newbox.x<=13#184
        diff=newbox.x-13#184
        newbox.x=13; @sprites["box"].x-=diff
        @sprites["box"].dispose
        @sprites["box"]=newbox
      end
    
      def pbSwitchBoxToLeft(newbox)
        newbox=PokemonBoxSprite.new(@storage,newbox,@boxviewport)
        if @command==0 && @movingmouse # Withdraw
          newbox.changeOpacity(150)
        elsif @command==1
          newbox.changeOpacity(150)
        end
        newbox.x=-152
        Graphics.frame_reset
        @sprites["box"].lefta.update
        globalIconAnim(@sprites["box"].lefta)
        begin
          Graphics.update
          Input.update
          @sprites["box"].x+=32
          newbox.x+=32
          pbUpdateSpriteHash(@sprites)
          updateMousePoke if @movingmouse
        end until newbox.x>=13#184
        diff=newbox.x-13#184
        newbox.x=13#184; 
        @sprites["box"].x-=diff
        @sprites["box"].dispose
        @sprites["box"]=newbox
      end
    
      def pbJumpToBox(newbox)
        if @storage.currentBox!=newbox
          if newbox>@storage.currentBox
            pbSwitchBoxToRight(newbox)
          else
            pbSwitchBoxToLeft(newbox)
          end
          @storage.currentBox=newbox
        end
      end
    
      def pbBoxName(helptext,minchars,maxchars)
        oldsprites=pbFadeOutAndHide(@sprites)
        ret=pbEnterBoxName(helptext,minchars,maxchars)
        if ret.length>0
          @storage[@storage.currentBox].name=ret
        end
        @sprites["box"].refreshBox=true
        pbRefresh
        pbFadeInAndShow(@sprites,oldsprites)
      end
    
      def pbUpdateOverlay(selection,party=nil)
        overlay=@sprites["overlay"].bitmap
        overlay.clear
        pokemon=nil
        if @screen.pbHeldPokemon
          [email protected]
        elsif selection>=0
          pokemon=(party) ? party[selection] : @storage[@storage.currentBox,selection]
        end
        if !pokemon
          @sprites["pokemon"].visible=false
          @sprites["bginfo"].visible=false
          return
        end
        @sprites["pokemon"].visible=true
        @sprites["bginfo"].visible=true
        speciesname=PBSpecies.getName(pokemon.species)
        naturename=PBNatures.getName(pokemon.nature)
    
        itemname="None"
        if pokemon.item>0
          itemname=PBItems.getName(pokemon.item)
        end
        
        abilityname="No ability"
        if pokemon.ability>0
          abilityname=PBAbilities.getName(pokemon.ability)
        end
        
        base=Color.new(90,82,82)
        shadow=Color.new(165,165,173)
        pokename=pokemon.name
        textstrings=[]
        if !pokemon.isEgg?
          if pokemon.isMale?
            textstrings.push([_INTL("♂"),144,160,false,Color.new(24,112,216),Color.new(136,168,208)])
          elsif pokemon.isFemale?
            textstrings.push([_INTL("♀"),144,160,false,Color.new(248,56,32),Color.new(224,152,144)])
          end
          
          textstrings.push([pokename,16,160,false,base,shadow])
          textstrings.push([_INTL("Lv.{1}",pokemon.level),176,160,false,base,shadow])
          textstrings.push([_ISPRINTF("No.{1:03d}",pokemon.species),16,64,false,base,shadow])
          textstrings.push([_INTL("{1}",speciesname),16,96,false,base,shadow])
          textstrings.push(["NATURE",16,192,false,base,shadow])
          textstrings.push([_INTL("{1}",naturename),32,224,false,base,shadow])
          textstrings.push(["ABILITY",16,256,false,base,shadow])
          textstrings.push([_INTL("{1}",abilityname),32,288,false,base,shadow])
          textstrings.push(["ITEM",16,320,false,base,shadow])
          textstrings.push([_INTL("{1}",itemname),32,352,false,base,shadow])
          
          textstrings.push(["MOVES LEARNED",304,224,false,base,shadow])
    
        for i in 0...pokemon.moves.length
          movename="-----"
          movename=PBMoves.getName(pokemon.moves[i].id) if pokemon.moves[i].id!=0
          textstrings.push([movename,304,256+(i*32),false,base,shadow])
        end
        
      else # Egg
        
          textstrings.push([pokename,16,160,false,base,shadow])
          textstrings.push(["NATURE",16,192,false,base,shadow])
          textstrings.push(["???",32,224,false,base,shadow])
          textstrings.push(["ABILITY",16,256,false,base,shadow])
          textstrings.push(["???",32,288,false,base,shadow])
          textstrings.push(["ITEM",16,320,false,base,shadow])
          textstrings.push([_INTL("{1}",itemname),32,352,false,base,shadow])
          
          textstrings.push(["MOVES LEARNED",304,224,false,base,shadow])
    
          movename="???"
          textstrings.push([movename,304,256+(0*32),false,base,shadow])
        
        end
        pbSetSystemFont(overlay)
        pbDrawTextPositions(overlay,textstrings)
        textstrings.clear
         if !pokemon.isEgg?
        if pokemon.isShiny?
          imagepos=[(["Graphics/Pictures/shiny",156,198,0,0,-1,-1])]
          pbDrawImagePositions(overlay,imagepos)
        end
        
        typebitmap=AnimatedBitmap.new(_INTL("Graphics/Pictures/typesbw"))
        type1rect=Rect.new(0,pokemon.type1*24,64,24)
        type2rect=Rect.new(0,pokemon.type2*24,64,24)
        if pokemon.type1==pokemon.type2
          overlay.blt(144,100,typebitmap.bitmap,type1rect)
        else
          overlay.blt(144,100,typebitmap.bitmap,type1rect)
          overlay.blt(213,100,typebitmap.bitmap,type2rect)
        end
        end
        drawMarkings(overlay,201*2,102*2,128,20,pokemon.markings)
        @sprites["pokemon"].setPokemonBitmap(pokemon)
        pbPositionPokemonSprite(@sprites["pokemon"],350,34)
      end
    
      def pbDropDownPartyTab
        begin
          Graphics.update
          Input.update
          @sprites["boxparty"].y-=16 if !@partyopen
          pbUpdateSpriteHash(@sprites)
          @sprites["showparty"].src_rect.set(0, 44, 192, 44)
          updateMousePoke if @movingmouse
        end until @sprites["boxparty"].y<=DEFAULTSCREENHEIGHT-288
        @partyopen = true
      end
    
      def pbHidePartyTab
        begin
          Graphics.update
          Input.update
          @sprites["boxparty"].y+=16 if @partyopen
          pbUpdateSpriteHash(@sprites)
          @sprites["showparty"].src_rect.set(0, 0, 192, 44)
          updateMousePoke if @movingmouse
        end until @sprites["boxparty"].y>=DEFAULTSCREENHEIGHT
        @partyopen = false
      end
    
      def pbSetMosaic(selection)
        if [email protected]
          if @[email protected] || @selectionForMosaic!=selection
            @sprites["pokemon"].mosaic=0
            @[email protected]
            @selectionForMosaic=selection
          end
        end
      end
    
    =begin
      def pbPlace(selected)
        box=selected[0]
        index=selected[1]
        if @storage[box,index]
          raise _INTL("Position {1},{2} is not empty...",box,index)
        end
        if box!=-1 && index>[email protected](box)
          pbDisplay("Can't place that there.")
          return
        end
        if box!=-1 && @heldpkmn.mail
          pbDisplay("Please remove the mail.")
          return
        end
        @heldpkmn.heal if box>=0
        @scene.pbPlace(selected,@heldpkmn)
        @storage[box,index]=@heldpkmn
        if box==-1
          @storage.party.compact!
        end
        @scene.pbRefresh
        @heldpkmn=nil
      end
    =end
    
      def pbPlaceMouse(selected,mousepoke,poke,mpokeinfo)
        mousepokesprite=mousepoke    
        mousepokei=mpokeinfo
        
        if selected[0]==-1 # Box is party pokémon
          newpokesprite=@sprites["boxparty"].pokemonsprites[selected[1]]
          [email protected][selected[1]]
        else
          newpokesprite=@sprites["box"].pokemonsprites[selected[1]]
          newpoke=@storage[selected[0],selected[1]]
        end
    
        # Change pokes 
        if selected[0]!=-1 # Box != Party Pokémon
          if @storage.currentBox==poke[0] && poke[0]!=-1 # Same box?
            @sprites["box"].setPokemon(poke[1],newpokesprite) 
          else # Moving between boxes
            if @sprites["box"].pokemonsprites[selected[1]]!=nil
              @sprites["box"].pokemonsprites[selected[1]].dispose
            end
          end
          
          # Place new Pokémon
          @sprites["box"].setPokemon(selected[1],mousepokesprite) 
          @storage[@storage.currentBox,selected[1]]=mousepokei
          
          echo("box: #{poke[0]}, pos: #{poke[1]} ")
          echo("- selected - box: #{selected[0]}, pos: #{selected[1]}\n")
        else 
          if poke[0]!=-1
            @sprites["box"].setPokemon(poke[1],newpokesprite)
            @sprites["boxparty"].setPokemon(selected[1],mousepokesprite) 
          else 
            mousepokesprite.dispose
          end
          
          if selected[1]>@storage.party.length
            @storage.party[@storage.party.length]=mousepokei
          else
            @storage.party[selected[1]]=mousepokei
          end
        end
        
        # Put old inbox Pokemon in the old position
        @storage[poke[0],poke[1]]=newpoke if newpoke!=nil
        @sprites["boxparty"].hardRefresh
        @storage.party.compact!
        @sprites["box"].refresh
      end
      
      def pbGetBoxPosFromMouse    
        colum=($mouse.y-466-16)/48
        num=($mouse.x-18)/48
        realpos=(colum*6)+num
        
        # Detect if pos is out of box
        if colum<0 || num<0 || colum>4 || num>5
          return -1
        end
      
        return realpos
      end
      
      def pbGetPartyPosFromMouse  
        if $mouse.inArea?(@sprites["boxparty"].x+10,
          @sprites["boxparty"].y+400+16,60,48)
          return 0
        elsif $mouse.inArea?(@sprites["boxparty"].x+90,
          @sprites["boxparty"].y+400+32,60,48)
          return 1
        elsif $mouse.inArea?(@sprites["boxparty"].x+10,
          @sprites["boxparty"].y+400+80,60,48)
          return 2
        elsif $mouse.inArea?(@sprites["boxparty"].x+90,
          @sprites["boxparty"].y+400+96,60,48)
          return 3
        elsif $mouse.inArea?(@sprites["boxparty"].x+10,
          @sprites["boxparty"].y+400+144,60,48)
          return 4
        elsif $mouse.inArea?(@sprites["boxparty"].x+90,
          @sprites["boxparty"].y+400+160,60,48)
          return 5
        end
        return -1
      end
      
      def clicking?(pokeicon)
        return $mouse.inAreaLeftPress?(pokeicon.x+8,pokeicon.y+408,48,48)
      end
      
      def updateMousePoke
        @mouseinfo[0].z=999999 # Over others Poke icons
        @mouseinfo[0].x=($mouse.x-32)
        @mouseinfo[0].y=($mouse.y-48)
      end
          
      def selectPokeWithMouse
    
        for i in 0...30
          if @sprites["box"].pokemonsprites[i] && !@movingmouse && @command!=1
            pokeicon=@sprites["box"].pokemonsprites[i]
            if pokeicon.getPoke && clicking?(pokeicon)
              @movingmouse=true
              @sprites["arrow"].visible=false
              pkmov=PokemonBoxIcon.new(pokeicon.getPoke,@viewport,true)
              pokeinfo=@storage[@storage.currentBox,i]
              oldz=pokeicon.disposed? ? 999999 : pokeicon.z 
              @mouseinfo=[pkmov,oldz,pokeinfo,i,@storage.currentBox] # Poke Info
              pbUpdateOverlay(i)
              pokeicon.dispose
              @storage[@storage.currentBox,i]=nil
              
              if @command==0 # Withdraw
                @sprites["box"].changeOpacity(150)
              end
              
            end
          end
        end
        
        if (@partyopen && @command!=0) || @command==1
          for i in 0...6
            if @sprites["boxparty"].pokemonsprites[i] && !@movingmouse
              pokeicon=@sprites["boxparty"].pokemonsprites[i]
              if pokeicon.getPoke && clicking?(pokeicon)
                @movingmouse=true
                @sprites["arrow"].visible=false
                pkmov=PokemonBoxIcon.new(pokeicon.getPoke,@viewport,true)
                [email protected][i]
                oldz=pokeicon.disposed? ? 999999 : pokeicon.z 
                @mouseinfo=[pkmov,oldz,pokeinfo,i,-1] # Poke Info
                pbUpdateOverlay(i,@storage.party)
                pokeicon.dispose
                @storage.party[i]=nil
                @sprites["boxparty"].changeOpacity(150) if @command==1
              end
            end
          end
        end
        
      end
        
      def changeBoxesWithMouse
        if $mouse.inAreaLeft?(10,436,46,42) 
          @sprites["arrow"].visible=false
          nextbox=(@storage.currentBox==0) ? @storage.maxBoxes-1 : @storage.currentBox-1
          pbSwitchBoxToLeft(nextbox)
          @storage.currentBox=nextbox
        elsif $mouse.inAreaLeft?(280,436,46,42) 
          @sprites["arrow"].visible=false
          nextbox=(@[email protected]) ? 0 : @storage.currentBox+1
          pbSwitchBoxToRight(nextbox)
          @storage.currentBox=nextbox
        end
      end
      
      def pbSelectBoxInternal(party)
        selection=@selection
        pbSetArrow(@sprites["arrow"],selection)
        pbUpdateOverlay(selection)
        pbSetMosaic(selection)
        selectmouse=false
        oldsel=selection
            
        loop do
          Graphics.update
          Input.update
          key=-1
          key=Input::DOWN if Input.repeat?(Input::DOWN)
          key=Input::RIGHT if Input.repeat?(Input::RIGHT)
          key=Input::LEFT if Input.repeat?(Input::LEFT)
          key=Input::UP if Input.repeat?(Input::UP)
          #key=81 if Input.triggerex?(0x01) # Mouse click
          
          changeBoxesWithMouse
          if @sprites["arrow"].heldPokemon==nil
          selectPokeWithMouse
          
          if @movingmouse
              updateMousePoke
            if !@canchangebox
              @waitbb+=1
              @canchangebox=true if @waitbb==14
            end
            
            if @command==0 && !@partyopen # Withdraw
              @sprites["boxparty"].hardRefresh(100)
              pbDropDownPartyTab
            end
            
            if $mouse.inArea?(10,436,46,42) && @canchangebox
              nextbox=(@storage.currentBox==0) ? @storage.maxBoxes-1 : @storage.currentBox-1
              pbSwitchBoxToLeft(nextbox)
              @storage.currentBox=nextbox
              @waitbb=0 
              @canchangebox=false
            elsif $mouse.inArea?(280,436,46,42) && @canchangebox
              nextbox=(@[email protected]) ? 0 : @storage.currentBox+1
              pbSwitchBoxToRight(nextbox)
              @storage.currentBox=nextbox
              @waitbb=0 
              @canchangebox=false
            elsif @command==2 && $mouse.over?(@sprites["showparty"],@sprites["showparty"].bitmap.width,
              @sprites["showparty"].bitmap.height/2)
              @selection=0 
              if @partyopen; pbHidePartyTab
              else; return [-2,-1]; end
            end
                
            if !Input.pressed(Input::Mouse_Left)
              overParty=$mouse.over?(@sprites["boxparty"])
              @mouseinfo[0].z=@mouseinfo[1] # Back to original z
              @mouseinfo[0].disposeRed
              @movingmouse=false
              @canchangebox=true
              boxpos=overParty ? pbGetPartyPosFromMouse : pbGetBoxPosFromMouse
              newpos=boxpos
              seleb=overParty ? -1 : @storage.currentBox
              
              if @command==0 && seleb!=-1 # Withdraw
                boxpos=-1 # Back to original position
              elsif @command==0 && overParty # Put on party
                seleb=-1
                boxpos=0
              end
              
              selectedp=[seleb,newpos]
              pokei=[@mouseinfo[4],@mouseinfo[3]]
    
              if @mouseinfo[4]==-1 && @storage.party.length<=1 &&
                @storage[selectedp[0],selectedp[1]]==nil
                @sprites["boxparty"].setPokemon(@mouseinfo[3],@mouseinfo[0])
                @storage.party[@mouseinfo[3]]=@mouseinfo[2]
                @sprites["boxparty"].hardRefresh
              elsif boxpos==-1 # Back to original position
                @storage[@mouseinfo[4],@mouseinfo[3]]=@mouseinfo[2]
                if @mouseinfo[4][email protected]
                  @sprites["box"].setPokemon(@mouseinfo[3],@mouseinfo[0])
                else
                  @mouseinfo[0].dispose
                end
              elsif @command==0 && seleb==-1 # Put on party in withdraw
                if @storage.party.length!=6 # If not full
                  @sprites["boxparty"].setPokemon(@storage.party.length,@mouseinfo[0])
                  @storage.party[@storage.party.length]=@mouseinfo[2]
                  @sprites["boxparty"].hardRefresh(100)
                else # Back Pokemon to box if needed
                  @storage[@mouseinfo[4],@mouseinfo[3]]=@mouseinfo[2]
                  if @mouseinfo[4][email protected]
                    @sprites["box"].setPokemon(@mouseinfo[3],@mouseinfo[0])
                  else
                    @mouseinfo[0].dispose
                  end
                end
              else
                pbPlaceMouse(selectedp,@mouseinfo[0],pokei,@mouseinfo[2])
              end
              
              if @command==0 # Withdraw
                @sprites["box"].changeOpacity(255)
                pbHidePartyTab
              end
            
              if boxpos!=-1 && newpos==@mouseinfo[3] && 
                @storage.currentBox==@mouseinfo[4]
                @selection=@mouseinfo[3]
                selection=@selection
              if @mouseinfo[4]==-1
                pbPartySetArrow(@sprites["arrow"],selection)
                return @selection
              else
                pbSetArrow(@sprites["arrow"],selection)
                return [@storage.currentBox, @selection]
              end
              end
            end
          end
            
        else # Selected with manual
           if Input.triggerex?(Input::Mouse_Left) &&
             !$mouse.leftClick?(@sprites["showparty"])
              overParty=$mouse.over?(@sprites["boxparty"])
              selection=overParty ? pbGetPartyPosFromMouse : pbGetBoxPosFromMouse
              newpos=boxpos
              seleb=overParty ? -1 : @storage.currentBox
              if overParty
                pbPartySetArrow(@sprites["arrow"],selection)
                return selection
              else
                pbSetArrow(@sprites["arrow"],selection)
                return [seleb,selection]
              end
           end
        end
        
            if @command==2 && $mouse.leftClick?(@sprites["showparty"],@sprites["showparty"].bitmap.width,
              @sprites["showparty"].bitmap.height/2)
              @selection=0 
              if @partyopen; pbHidePartyTab
              else; return [-2,-1]; end
            end
    
          if key>=0
            pbPlayCursorSE() if !@movingmouse
            if @sprites["arrow"].visible==true
              selection=pbChangeSelection(key,selection)
            else
               @sprites["arrow"].visible=true if !@movingmouse 
            end
            pbSetArrow(@sprites["arrow"],selection)
            nextbox=-1
            
          if @partyopen
            if selection==-20
              @selection=0
              return [-2,-1]
            elsif selection==-21
              @selection=2
              return [-2,-1]
            elsif selection==-22
              @selection=4
              return [-2,-1]
            end
          end
          
            if selection==-4
              nextbox=(@storage.currentBox==0) ? @storage.maxBoxes-1 : @storage.currentBox-1
              pbSwitchBoxToLeft(nextbox)
              @storage.currentBox=nextbox
              selection=-1
            elsif selection==-5
              nextbox=(@[email protected]) ? 0 : @storage.currentBox+1
              pbSwitchBoxToRight(nextbox)
              @storage.currentBox=nextbox
              selection=-1
            end
            selection=-1 if selection==-4 || selection==-5
            pbUpdateOverlay(selection) if @sprites["arrow"].visible==true
            pbSetMosaic(selection)
          end
          pbUpdateSpriteHash(@sprites)
          if Input.trigger?(Input::C) or selectmouse
            if selection>=0
              @selection=selection
              return [@storage.currentBox,selection]
            elsif selection==-1 # Box name 
              @selection=selection
              return [-4,-1]
            elsif selection==-2 # Party Pokémon 
              @selection=0 
              if @partyopen; pbHidePartyTab
              else; return [-2,-1]; end
            elsif selection==-3 # Close Box 
              @selection=selection
              return [-3,-1]
            end
          end
          if Input.trigger?(Input::B) || $mouse.leftClick?(@sprites["returnbuttom"],64,64) 
            globalIconAnim(@sprites["returnbuttom"])
            @selection=selection
            return nil
          end
        end
      end
    
      def pbSelectBox(party)
        if @command==0 # Withdraw
          return pbSelectBoxInternal(party)
        else
          ret=nil
          loop do
            if !@choseFromParty
              ret=pbSelectBoxInternal(party)
            end
            if @choseFromParty || (ret && ret[0]==-2) # Party Pokémon
              if !@choseFromParty
                if !@partyopen
                pbDropDownPartyTab 
               end
              end
              ret=pbSelectPartyInternal(party,false)
              if ret==-1 or ret==-3
                pbHidePartyTab
                @selection=0
                @choseFromParty=false
              elsif ret==20
               # pbHidePartyTab
                @selection=5
                @choseFromParty=false
              else
                @choseFromParty=true
                return [-1,ret]
              end
            else
              @choseFromParty=false
              return ret
            end
          end
        end
      end
    
      def pbHold(selected)
        if selected[0]==-1
          @sprites["boxparty"].grabPokemon(selected[1],@sprites["arrow"])
        else
          @sprites["box"].grabPokemon(selected[1],@sprites["arrow"])
        end
        while @sprites["arrow"].grabbing?
          Graphics.update
          Input.update
          pbUpdateSpriteHash(@sprites)
        end
      end
    
      def pbSwap(selected,heldpoke)
        heldpokesprite=@sprites["arrow"].heldPokemon
        boxpokesprite=nil
        if selected[0]==-1
          boxpokesprite=@sprites["boxparty"].getPokemon(selected[1])
        else
          boxpokesprite=@sprites["box"].getPokemon(selected[1])
        end
        
        if selected[0]==-1
          @sprites["boxparty"].setPokemon(selected[1],heldpokesprite)
        else
          @sprites["box"].setPokemon(selected[1],heldpokesprite)
        end
        @sprites["arrow"].setSprite(boxpokesprite)
        @sprites["pokemon"].mosaic=0
        @[email protected]
        @selectionForMosaic=selected[1]
      end
    
      def pbPlace(selected,heldpoke)
        heldpokesprite=@sprites["arrow"].heldPokemon
        @sprites["arrow"].place
        while @sprites["arrow"].placing?
          Graphics.update
          Input.update
          pbUpdateSpriteHash(@sprites)
        end
        if selected[0]==-1
          @sprites["boxparty"].setPokemon(selected[1],heldpokesprite)
        else
          @sprites["box"].setPokemon(selected[1],heldpokesprite)
          pbHidePartyTab
        end
        @[email protected]
        @selectionForMosaic=selected[1]
      end
    
      def pbChooseItem(bag)
        oldsprites=pbFadeOutAndHide(@sprites)
        scene=PokemonBag_Scene.new
        screen=PokemonBagScreen.new(scene,bag)
        ret=screen.pbGiveItemScreen
        pbFadeInAndShow(@sprites,oldsprites)
        return ret
      end
    
      def pbWithdraw(selected,heldpoke,partyindex)
        if !heldpoke
          pbHold(selected)
        end
        pbDropDownPartyTab
        pbPartySetArrow(@sprites["arrow"],partyindex)
        pbPlace([-1,partyindex],heldpoke)
        pbHidePartyTab
      end
    
      def pbSummary(selected,heldpoke)
        oldsprites=pbFadeOutAndHide(@sprites)
        scene=PokemonSummaryScene.new
        screen=PokemonSummary.new(scene)
        if heldpoke
          screen.pbStartScreen([heldpoke],0)
        elsif selected[0]==-1
          @selection=screen.pbStartScreen(@storage.party,selected[1])
          pbPartySetArrow(@sprites["arrow"],@selection)
          pbUpdateOverlay(@selection,@storage.party)
        else
          @selection=screen.pbStartScreen(@storage.boxes[selected[0]],selected[1])
          pbSetArrow(@sprites["arrow"],@selection)
          pbUpdateOverlay(@selection)
        end
        pbFadeInAndShow(@sprites,oldsprites)
      end
    
      def pbStore(selected,heldpoke,destbox,firstfree)
        if heldpoke
          if [email protected]
            heldpokesprite=@sprites["arrow"].heldPokemon
            @sprites["box"].setPokemon(firstfree,heldpokesprite)
            @sprites["arrow"].setSprite(nil)
          else
            @sprites["arrow"].deleteSprite
          end
        else
          sprite=@sprites["boxparty"].getPokemon(selected[1])
          if [email protected]
            @sprites["box"].setPokemon(firstfree,sprite)
            @sprites["boxparty"].setPokemon(selected[1],nil)
          else
            @sprites["boxparty"].deletePokemon(selected[1])
          end
        end
      end
    
      def drawMarkings(bitmap,x,y,width,height,markings)
        totaltext=""
        oldfontname=bitmap.font.name
        oldfontsize=bitmap.font.size
        oldfontcolor=bitmap.font.color
        bitmap.font.size=21
        bitmap.font.name="PokemonMarks"
        PokemonStorage::MARKINGCHARS.each{|item| totaltext+=item }
        totalsize=bitmap.text_size(totaltext)
        realX=x+(width/2)-(totalsize.width/2)
        realY=y+(height/2)-(totalsize.height/2)
        i=0
        PokemonStorage::MARKINGCHARS.each{|item|
           marked=(markings&(1<<i))!=0
           bitmap.font.color=(marked) ? Color.new(74,66,57) : Color.new(184,184,160)
           itemwidth=bitmap.text_size(item).width
           bitmap.draw_text(realX,realY,itemwidth+2,totalsize.height,item)
           realX+=itemwidth-10
           i+=1
        }
        bitmap.font.name=oldfontname
        bitmap.font.size=oldfontsize
        bitmap.font.color=oldfontcolor
      end
    
      def getMarkingCommands(markings)
        selectedtag="<c=505050>"
        deselectedtag="<c=D0C8B8>"
        commands=[]
        for i in 0...PokemonStorage::MARKINGCHARS.length
          commands.push( ((markings&(1<<i))==0 ? deselectedtag : selectedtag)+"<ac><fn=PokemonMarks>"+PokemonStorage::MARKINGCHARS[i])
        end
        commands.push(_INTL("OK"))
        commands.push(_INTL("Cancel"))   
        return commands
      end
    
      def pbMark(selected,heldpoke)
        ret=0
        msgwindow=Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width-180,32)
        msgwindow.viewport=@viewport
        msgwindow.visible=true
        msgwindow.letterbyletter=false
        msgwindow.resizeHeightToFit(_INTL("Mark your Pokemon."),Graphics.width-180)
        msgwindow.text=_INTL("Mark your Pokemon.")
        pokemon=heldpoke
        if heldpoke
          pokemon=heldpoke
        elsif selected[0]==-1
          [email protected][selected[1]]
        else
          [email protected][selected[0]][selected[1]]
        end
        pbBottomRight(msgwindow)
        selectedtag="<c=505050>"
        deselectedtag="<c=D0C8B8>"
        commands=getMarkingCommands(pokemon.markings)
        cmdwindow=Window_AdvancedCommandPokemon.new(commands)
        cmdwindow.viewport=@viewport
        cmdwindow.visible=true
        cmdwindow.resizeToFit(cmdwindow.commands)
        cmdwindow.width=132
        cmdwindow.height=DEFAULTSCREENHEIGHT-msgwindow.height if cmdwindow.height>DEFAULTSCREENHEIGHT-msgwindow.height
        cmdwindow.update
        
        pbBottomRight(cmdwindow)
        markings=pokemon.markings
        cmdwindow.y-=msgwindow.height
        loop do
          Graphics.update
          Input.update
          if Input.trigger?(Input::B)
            break # cancel
          end
          if Input.trigger?(Input::C)
            if cmdwindow.index==commands.length-1
              break # cancel
            elsif cmdwindow.index==commands.length-2
              pokemon.markings=markings # OK
              break
            elsif cmdwindow.index>=0
              mask=(1<<cmdwindow.index)
              if (markings&mask)==0
                markings|=mask
              else
                markings&=~mask
              end
              commands=getMarkingCommands(markings)
              cmdwindow.commands=commands
            end
          end
          pbUpdateSpriteHash(@sprites)
          msgwindow.update
          cmdwindow.update
        end
        msgwindow.dispose
        cmdwindow.dispose
        Input.update
      end
    
      def pbRefresh
        @sprites["box"].refresh
        @sprites["boxparty"].refresh
      end
    
      def pbHardRefresh
        oldPartyY=@sprites["boxparty"].y
        @sprites["box"].dispose
        @sprites["boxparty"].dispose
        @sprites["box"]=PokemonBoxSprite.new(@storage,@storage.currentBox,@boxviewport)
        @sprites["boxparty"]=PokemonBoxPartySprite.new(@storage.party,@boxsidesviewport)
        @sprites["boxparty"].y=oldPartyY
        @sprites["box"].changeOpacity(150) if @command==1
      end
    
      def pbRelease(selected,heldpoke)
        box=selected[0]
        index=selected[1]
        if heldpoke
          sprite=@sprites["arrow"].heldPokemon
        elsif box==-1
          sprite=@sprites["boxparty"].getPokemon(index)
        else
          sprite=@sprites["box"].getPokemon(index)
        end
        if sprite
          sprite.release
          while sprite.releasing?
            Graphics.update
            sprite.update
            pbUpdateSpriteHash(@sprites)
          end
        end
      end
    
      def pbChooseBox(msg)
        commands=[]
        for i in [email protected]
          box=@storage[i]
          if box
            commands.push(_ISPRINTF("{1:s} ({2:d}/{3:d})",box.name,box.nitems,box.length))
          end
        end
        return pbShowCommands(msg,commands,@storage.currentBox)
      end
    
      def pbShowCommandsStorage(message,commands,index=0)
        ret=0
        msgwindow=Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width,32)
        msgwindow.viewport=@viewport
        msgwindow.visible=true
        msgwindow.letterbyletter=false
        msgwindow.resizeHeightToFit(message,Graphics.width)
        msgwindow.text=message
        pbBottomRight(msgwindow)
        cmdwindow=Window_CommandPokemonStorage.new(commands)
        cmdwindow.viewport=@viewport
        cmdwindow.visible=true
        cmdwindow.resizeToFit(cmdwindow.commands)
        cmdwindow.height=DEFAULTSCREENHEIGHT-msgwindow.height if cmdwindow.height>DEFAULTSCREENHEIGHT-msgwindow.height
        cmdwindow.update
        cmdwindow.index=index
        pbBottomRight(cmdwindow)
        cmdwindow.x+=10
        cmdwindow.y-=msgwindow.height-14
        loop do
          Graphics.update
          Input.update
          inarea=$mouse.inArea?(cmdwindow.x,cmdwindow.y,cmdwindow.width,cmdwindow.height) 
          
          if Input.trigger?(Input::B)
            ret=-1
            break
          end
          if Input.trigger?(Input::C) or (inarea && Input.triggerex?(Input::Mouse_Left))
            ret=cmdwindow.index
            break
          end
          pbUpdateSpriteHash(@sprites)
          msgwindow.update
          cmdwindow.update
        end
        msgwindow.dispose
        cmdwindow.dispose
        Input.update
        return ret
      end
    
      
      def pbShowCommands(message,commands,index=0)
        ret=0
        msgwindow=Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width,32)
        msgwindow.viewport=@viewport
        msgwindow.visible=true
        msgwindow.letterbyletter=false
        msgwindow.resizeHeightToFit(message,Graphics.width)
        msgwindow.text=message
        pbBottomRight(msgwindow)
        cmdwindow=Window_CommandPokemonModern.new(commands)
        cmdwindow.viewport=@viewport
        cmdwindow.visible=true
        cmdwindow.resizeToFit(cmdwindow.commands)
        cmdwindow.height=DEFAULTSCREENHEIGHT-msgwindow.height if cmdwindow.height>DEFAULTSCREENHEIGHT-msgwindow.height
        cmdwindow.update
        cmdwindow.index=index
        pbBottomRight(cmdwindow)
        cmdwindow.x+=10
        cmdwindow.y-=msgwindow.height-14
        loop do
          Graphics.update
          Input.update
          inarea=$mouse.inArea?(cmdwindow.x,cmdwindow.y,cmdwindow.width,cmdwindow.height) 
          
          if Input.trigger?(Input::B)
            ret=-1
            break
          end
          if Input.trigger?(Input::C) or (inarea && Input.triggerex?(Input::Mouse_Left))
            ret=cmdwindow.index
            break
          end
          pbUpdateSpriteHash(@sprites)
          msgwindow.update
          cmdwindow.update
        end
        msgwindow.dispose
        cmdwindow.dispose
        Input.update
        return ret
      end
    
      def pbDisplay(message)
        msgwindow=Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width,32)
        msgwindow.viewport=@viewport
        msgwindow.visible=true
        msgwindow.letterbyletter=false
        msgwindow.resizeHeightToFit(message,Graphics.width)
        msgwindow.text=message
        pbBottomRight(msgwindow)
        loop do
          Graphics.update
          Input.update
          if Input.trigger?(Input::B)
            break
          end
          if Input.trigger?(Input::C) or (Input.triggerex?(Input::Mouse_Left) && $mouse.y>400)
            break
          end
          msgwindow.update
          pbUpdateSpriteHash(@sprites)
        end
        msgwindow.dispose
        Input.update
      end
    end
    
    
    
    ################################################################################
    # Regional Storage scripts
    ################################################################################
    class RegionalStorage
      def initialize
        @storages=[]
        @lastmap=-1
        @rgnmap=-1
      end
    
      def getCurrentStorage
        if !$game_map
          raise _INTL("The player is not on a map, so the region could not be determined.")
        end
        if @lastmap!=$game_map.map_id
          @rgnmap=pbGetCurrentRegion # may access file IO, so caching result
          @lastmap=$game_map.map_id
        end
        if @rgnmap<0
          raise _INTL("The current map has no region set.  Please set the MapPosition metadata setting for this map.")
        end
        if !@storages[@rgnmap]
          @storages[@rgnmap]=PokemonStorage.new
        end
        return @storages[@rgnmap]
      end
    
      def boxes
        return getCurrentStorage.boxes
      end
    
      def party
        return getCurrentStorage.party
      end
    
      def currentBox
        return getCurrentStorage.currentBox
      end
    
      def currentBox=(value)
        getCurrentStorage.currentBox=value
      end
    
      def maxBoxes
        return getCurrentStorage.maxBoxes
      end
    
      def maxPokemon(box)
        return getCurrentStorage.maxPokemon(box)
      end
    
      def [](x,y=nil)
        getCurrentStorage[x,y]
      end
    
      def []=(x,y,value)
        getCurrentStorage[x,y]=value
      end
    
      def full?
        getCurrentStorage.full?
      end
    
      def pbFirstFreePos(box)
        getCurrentStorage.pbFirstFreePos(box)
      end
    
      def pbCopy(boxDst,indexDst,boxSrc,indexSrc)
        getCurrentStorage.pbCopy(boxDst,indexDst,boxSrc,indexSrc)
      end
    
      def pbMove(boxDst,indexDst,boxSrc,indexSrc)
        getCurrentStorage.pbCopy(boxDst,indexDst,boxSrc,indexSrc)
      end
    
      def pbMoveCaughtToParty(pkmn)
        getCurrentStorage.pbMoveCaughtToParty(pkmn) 
      end
    
      def pbMoveCaughtToBox(pkmn,box)
        getCurrentStorage.pbMoveCaughtToBox(pkmn,box)
      end
    
      def pbStoreCaught(pkmn)
        getCurrentStorage.pbStoreCaught(pkmn) 
      end
    
      def pbDelete(box,index)
        getCurrentStorage.pbDelete(pkmn)
      end
    end
    
    
    
    ################################################################################
    # PC menus
    ################################################################################
    def Kernel.pbGetStorageCreator
      creator=pbStorageCreator
      creator=_INTL("Bill") if !creator || creator==""
      return creator
    end
    
    def pbPCItemStorage
      loop do
        command=Kernel.pbShowCommandsWithHelp(nil,
           [_INTL("Withdraw Item"),
           _INTL("Deposit Item"),
           _INTL("Toss Item"),
           _INTL("Exit")],
           [_INTL("Take out items from the PC."),
           _INTL("Store items in the PC."),
           _INTL("Throw away items stored in the PC."),
           _INTL("Go back to the previous menu.")],-1
        )
        if command==0 # Withdraw Item
          if !$PokemonGlobal.pcItemStorage
            $PokemonGlobal.pcItemStorage=PCItemStorage.new
          end
          if $PokemonGlobal.pcItemStorage.empty?
            Kernel.pbMessage(_INTL("There are no items."))
          else
            pbFadeOutIn(99999){
               scene=WithdrawItemScene.new
               screen=PokemonBagScreen.new(scene,$PokemonBag)
               ret=screen.pbWithdrawItemScreen
            }
          end
        elsif command==1 # Deposit Item
          pbFadeOutIn(99999){
             scene=PokemonBag_Scene.new
             screen=PokemonBagScreen.new(scene,$PokemonBag)
             ret=screen.pbDepositItemScreen
          }
        elsif command==2 # Toss Item
          if !$PokemonGlobal.pcItemStorage
            $PokemonGlobal.pcItemStorage=PCItemStorage.new
          end
          if $PokemonGlobal.pcItemStorage.empty?
            Kernel.pbMessage(_INTL("There are no items."))
          else
            pbFadeOutIn(99999){
               scene=TossItemScene.new
               screen=PokemonBagScreen.new(scene,$PokemonBag)
               ret=screen.pbTossItemScreen
            }
          end
        else
          break
        end
      end
    end
    
    def pbPCMailbox
      if !$PokemonGlobal.mailbox || $PokemonGlobal.mailbox.length==0
        Kernel.pbMessage(_INTL("There's no Mail here."))
      else
        loop do
          commands=[]
          for mail in $PokemonGlobal.mailbox
            commands.push(mail.sender)
          end
          commands.push(_INTL("Cancel"))
          command=Kernel.pbShowCommands(nil,commands,-1)
          if command>=0 && command<$PokemonGlobal.mailbox.length
            mailIndex=command
            command=Kernel.pbMessage(_INTL("What do you want to do with {1}'s Mail?",
               $PokemonGlobal.mailbox[mailIndex].sender),[
               _INTL("Read"),
               _INTL("Move to Bag"),
               _INTL("Give"),
               _INTL("Cancel")
               ],-1)
            if command==0 # Read
              pbFadeOutIn(99999){
                 pbDisplayMail($PokemonGlobal.mailbox[mailIndex])
              }
            elsif command==1 # Move to Bag
              if Kernel.pbConfirmMessage(_INTL("The message will be lost.  Is that OK?"))
                if $PokemonBag.pbStoreItem($PokemonGlobal.mailbox[mailIndex].item)
                  Kernel.pbMessage(_INTL("The Mail was returned to the Bag with its message erased."))
                  $PokemonGlobal.mailbox.delete_at(mailIndex)
                else
                  Kernel.pbMessage(_INTL("The Bag is full."))
                end
              end
            elsif command==2 # Give
              pbFadeOutIn(99999){
                 sscene=PokemonScreen_Scene.new
                 sscreen=PokemonScreen.new(sscene,$Trainer.party)
                 sscreen.pbPokemonGiveMailScreen(mailIndex)
              }
            end
          else
            break
          end
        end
      end
    end
    
    def pbTrainerPCMenu
      loop do
        command=Kernel.pbMessageBlack(0,false,_INTL("What do you want to do?"),[
           #_INTL("Item Storage"),
           _INTL("Mailbox"),
           _INTL("Turn Off")
           ],-1)
        if command==0
          pbPCMailbox
          #pbPCItemStorage
        #elsif command==1
          #pbPCMailbox
        else
          break
        end
      end
    end
    
    
    
    module PokemonPCList
      @@pclist=[]
    
      def self.registerPC(pc)
        @@pclist.push(pc)
      end
    
      def self.getCommandList()
        commands=[]
        for pc in @@pclist
          if pc.shouldShow?
            commands.push(pc.name)
          end
        end
        commands.push(_INTL("Log Off"))
        return commands
      end
    
      def self.callCommand(cmd)
        if cmd<0 || cmd>=@@pclist.length
          return false
        end
        i=0
        for pc in @@pclist
          if pc.shouldShow?
            if i==cmd
               pc.access()
               return true
            end
            i+=1
          end
        end
        return false
      end
    end
    
    
    
    def pbTrainerPC
      Kernel.pbMessage(_INTL("\\se[computeropen]{1} booted up the PC.",$Trainer.name))
      pbTrainerPCMenu
      pbSEPlay("computerclose")
    end
    
    
    
    class TrainerPC
      def shouldShow?
        return true
      end
    
      def name
        return _INTL("{1}'s PC",$Trainer.name)
      end
    
      def access
        Kernel.pbMessageBlack(0,false,_INTL("\\se[accesspc]Accessed {1}'s PC.",$Trainer.name))
        pbTrainerPCMenu
      end
    end
    
    
    
    class StorageSystemPC
      def shouldShow?
        return true
      end
    
      def name
        if $PokemonGlobal.seenStorageCreator
          return _INTL("{1}'s PC",Kernel.pbGetStorageCreator)
        else
          return _INTL("Someone's PC")
        end
      end
    
      def access
        Kernel.pbMessageBlack(0,false,_INTL("\\se[accesspc]The Pokémon Storage System was opened."))
        loop do
          command=Kernel.pbShowCommandsWithHelpBlack(nil,
             [_INTL("Withdraw Pokémon"),
             _INTL("Deposit Pokémon"),
             _INTL("Move Pokémon"),
             _INTL("See ya!")],
             [_INTL("Move Pokémon stored in Boxes to your party."),
             _INTL("Store Pokémon in your party in Boxes."),
             _INTL("Organize the Pokémon in Boxes and in your party."),
             _INTL("Return to the previous menu.")],-1
          )
          if command>=0 && command<3
            if command==0 && $PokemonStorage.party.length>=6
              Kernel.pbMessageBlack(0,false,_INTL("Your party is full!"))
              next
            end
            count=0
            for p in $PokemonStorage.party
              count+=1 if p && !p.isEgg? && p.hp>0
            end
            if command==1 && count<=1
              Kernel.pbMessageBlack(0,false,_INTL("Can't deposit the last Pokémon!"))
              next
            end
            pbFadeOutIn(99999){
               scene=PokemonStorageScene.new
               screen=PokemonStorageScreen.new(scene,$PokemonStorage)
               screen.pbStartScreen(command)
            }
          else
            break
          end
        end
      end
    end
    
    
    def pbPokeCenterPC
      Kernel.pbMessageBlack(0,false,
      _INTL("\\se[computeropen]{1} booted up the PC.",$Trainer.name))
      loop do
        commands=PokemonPCList.getCommandList()
        command=Kernel.pbMessageBlack(0,false,_INTL("Which PC should be accessed?"),
           commands,commands.length)
        if !PokemonPCList.callCommand(command)
          break
        end
      end
      pbSEPlay("computerclose")
    end
    
    PokemonPCList.registerPC(StorageSystemPC.new)
    PokemonPCList.registerPC(TrainerPC.new)
     
    Last edited by a moderator:
    Back
    Top