Advertisement
Deukhoofd

Trade

Jul 2nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 29.47 KB | None | 0 0
  1.  
  2. ################################################################################
  3. #-------------------------------------------------------------------------------
  4. #Author: Alexandre
  5. #Simple trade system.
  6. #-------------------------------------------------------------------------------
  7. ################################################################################
  8. class Scene_Trade
  9.   ################################################################################
  10.   #-------------------------------------------------------------------------------
  11.   #Author: Alexandre
  12.   #Lets initialise the scene:
  13.   #@list is the main trade background
  14.   #@info is the background that appears when confirming the trade
  15.   #-------------------------------------------------------------------------------
  16.   ################################################################################
  17.   def initialize(user)
  18.     #--------
  19.     #necessary to make sure both users have started the trade scene before
  20.     #proceeding with the trade
  21.     #    $network.send("<TRA start>")
  22.     #    loop do
  23.     #      Graphics.update
  24.     #      Input.update
  25.     #      message = $network.listen
  26.     #      case message
  27.     #      when /<TRA start>/ then break
  28.     #      when /<TRA dead>/ then $scene = Scene_Map.new; break
  29.     #      end
  30.     #    end
  31.     #--------
  32.     @username = user
  33.     @partysent = false
  34.     @pokemonselected = false
  35.     @theirparty = nil
  36.     @theirpartylength = 0
  37.     @theirchosen = nil
  38.     @index = 1
  39.     @viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
  40.     @overlay = SpriteWrapper.new(@viewport)
  41.     @overlay.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  42.     @overlay.z = 1000005
  43.     @overlay2 = SpriteWrapper.new(@viewport)
  44.     @overlay2.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  45.     @overlay2.z = 1000009
  46.     @overlay3 = SpriteWrapper.new(@viewport)
  47.     @overlay3.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  48.     @overlay3.z = 1000009
  49.     @list = SpriteWrapper.new(@viewport)
  50.     @list.visible = true
  51.     @list.bitmap = BitmapCache.load_bitmap("Graphics/Pictures/tradebackground.png")
  52.     @list.z = 1000004
  53.     @selector = SpriteWrapper.new(@viewport)
  54.     @selector.visible = true
  55.     @selector.bitmap = BitmapCache.load_bitmap("Graphics/Pictures/boxpoint1.png")
  56.     @selector.z = 1000006
  57.     @waiting = Window_AdvancedTextPokemon.new("Waiting...")
  58.     @waiting.visible = false
  59.     @waiting.width = 120
  60.     @waiting.x = Graphics.width / 2 - 50
  61.     @waiting.y = 160
  62.     @sprite = []
  63.     @spritex = []
  64.     @sprites = {}
  65.     @info = SpriteWrapper.new(@viewport)
  66.     @info.visible = true
  67.     @info.bitmap = BitmapCache.load_bitmap("Graphics/Pictures/tradebottom.png")
  68.     @info.z = 1000008
  69.     @info.visible = false
  70.     @accepted = false
  71.     @received = false
  72.     @listreceived = false
  73.   end
  74.  
  75.   ################################################################################
  76.   #-------------------------------------------------------------------------------
  77.   #Main procedure of the Scene, contains the loop that keeps it alive. When B is
  78.   #pressed, the Trade dead message is sent to the server and the scene is disposed
  79.   #-------------------------------------------------------------------------------
  80.   ################################################################################
  81.   def main
  82.     mylist
  83.     Graphics.transition
  84.     loop do
  85.       Graphics.update
  86.       Input.update
  87.       update
  88.       if $scene != self
  89.         break
  90.       end
  91.       if Input.trigger?(Input::B)
  92.         $scene = Connect.new
  93.         $network.send("<TRA|dead>")
  94.       end
  95.     end
  96.     Graphics.freeze
  97.     @waiting.dispose
  98.     @viewport.dispose
  99.     @list.dispose
  100.     @overlay.dispose
  101.     @overlay2.dispose
  102.     @overlay3.dispose
  103.     @sprite[0].dispose if @sprite[0] != nil
  104.     @sprite[1].dispose if @sprite[1] != nil
  105.     @sprite[2].dispose if @sprite[2] != nil
  106.     @sprite[3].dispose if @sprite[3] != nil
  107.     @sprite[4].dispose if @sprite[4] != nil
  108.     @sprite[5].dispose if @sprite[5] != nil
  109.     @spritex[0].dispose if @spritex[0] != nil
  110.     @spritex[1].dispose if @spritex[1] != nil
  111.     @spritex[2].dispose if @spritex[2] != nil
  112.     @spritex[3].dispose if @spritex[3] != nil
  113.     @spritex[4].dispose if @spritex[4] != nil
  114.     @spritex[5].dispose if @spritex[5] != nil
  115.     @sprites["mypokemon"].dispose if @sprites["mypokemon"] != nil
  116.     @sprites["theirpokemon"].dispose if @sprites["theirpokemon"] != nil
  117.   end
  118.  
  119.   ################################################################################
  120.   #-------------------------------------------------------------------------------
  121.   #Just a procedure to update the scene and check for any incoming messages
  122.   #-------------------------------------------------------------------------------
  123.   ################################################################################
  124.   def update
  125.     their_info if @received == true and @pokemonselected == true
  126.     their_list if @listreceived == true
  127.     message = $network.listen
  128.     handle(message)
  129.     @sprite[0].update if @sprite[0] != nil
  130.     @sprite[1].update if @sprite[1] != nil
  131.     @sprite[2].update if @sprite[2] != nil
  132.     @sprite[3].update if @sprite[3] != nil
  133.     @sprite[4].update if @sprite[4] != nil
  134.     @sprite[5].update if @sprite[5] != nil
  135.     @spritex[0].update if @spritex[0] != nil
  136.     @spritex[1].update if @spritex[1] != nil
  137.     @spritex[2].update if @spritex[2] != nil
  138.     @spritex[3].update if @spritex[3] != nil
  139.     @spritex[4].update if @spritex[4] != nil
  140.     @spritex[5].update if @spritex[5] != nil
  141.     @viewport.update
  142.     @overlay.update
  143.     @overlay2.update
  144.     @overlay3.update
  145.     @selector.update
  146.     check_input
  147.     if @pokemonselected == false and @theirparty != nil
  148.       update_selector_input
  149.       update_selector
  150.     end
  151.     @waiting.update
  152.   end
  153.  
  154.   ################################################################################
  155.   #-------------------------------------------------------------------------------
  156.   #Listens to incoming messages and determines what to do when trade messages are
  157.   #received.
  158.   #-------------------------------------------------------------------------------
  159.   ################################################################################
  160.   def handle(message)
  161.     case message
  162.     when /<TRA party=(.*)>/ #$Trainer.party dump
  163.       theirparty($1) if @theirparty == nil
  164.     when /<TRA offer=(.*)>/ #Trainer.party[@index - 1] dump
  165.       receiveoffer($1) if @theirchosen == nil
  166.     when /<TRA accepted>/
  167.       execute_trade if @accepted == true
  168.     when /<TRA declined>/ then trade_declined
  169.     when /<TRA dead>/
  170.       Kernel.pbMessage("The user exited the trade.")
  171.       $scene = Connect.new
  172.     end
  173.   end
  174.  
  175.   ################################################################################
  176.   #-------------------------------------------------------------------------------
  177.   #Checks for input from C to select a pokemon or show the summary.
  178.   #-------------------------------------------------------------------------------
  179.   ################################################################################
  180.   def check_input
  181.     #Player's pokemon
  182.     if Input.trigger?(Input::C) and @pokemonselected == false
  183.       if @index >= 1 and @index <= $Trainer.party.length
  184.         commands = [_INTL("Offer"), _INTL("Summary"), _INTL("Cancel")]
  185.         choice = Kernel.pbMessage(_INTL("What do you want to do?"), commands)
  186.         if choice == 0
  187.           serialized = [JSON.encode($Trainer.party[@index - 1])].pack("m").delete("\n")
  188.           $network.send("<TRA|offer=" + serialized + ">")
  189.           show_information
  190.           @waiting.visible = true
  191.           @pokemonselected = true
  192.         elsif choice == 1
  193.           scene = PokemonSummaryScene.new
  194.           screen = PokemonSummary.new(scene)
  195.           screen.pbStartScreen($Trainer.party, @index - 1)
  196.         elsif choice == 2
  197.           #do nothing
  198.         end
  199.       else
  200.         #Other user's pokemon
  201.         commands = [_INTL("Summary"), _INTL("Cancel")]
  202.         choice = Kernel.pbMessage(_INTL("What do you want to do?"), commands)
  203.         if choice == 0
  204.           scene = PokemonSummaryScene.new
  205.           screen = PokemonSummary.new(scene)
  206.           screen.pbStartScreen(@theirparty, @index - 1 - $Trainer.party.length)
  207.         elsif choice == 1
  208.           #donothing
  209.         end
  210.       end
  211.     end
  212.   end
  213.  
  214.   ################################################################################
  215.   #-------------------------------------------------------------------------------
  216.   #Checks for left and right input to move selector.
  217.   #-------------------------------------------------------------------------------
  218.   ################################################################################
  219.   def update_selector_input
  220.     @index += 1 if Input.trigger?(Input::RIGHT)
  221.     @index += 1 if Input.trigger?(Input::DOWN)
  222.     @index -= 1 if Input.trigger?(Input::LEFT)
  223.     @index -= 1 if Input.trigger?(Input::UP)
  224.     @index = 1 if @index > $Trainer.party.length + @theirparty.length
  225.     @index = $Trainer.party.length + @theirparty.length if @index <= 0
  226.   end
  227.  
  228.   ################################################################################
  229.   #-------------------------------------------------------------------------------
  230.   #Updates the position of the selector.
  231.   #-------------------------------------------------------------------------------
  232.   ################################################################################
  233.   def update_selector
  234.     case @index
  235.     when 1 then @selector.x = 46; @selector.y = 40
  236.     when 2 then @selector.x = 135; @selector.y = 40
  237.     when 3 then @selector.x = 46; @selector.y = 120
  238.     when 4 then @selector.x = 135; @selector.y = 120
  239.     when 5 then @selector.x = 46; @selector.y = 220
  240.     when 6 then @selector.x = 135; @selector.y = 220
  241.     when 7 then @selector.x = 299; @selector.y = 40
  242.     when 8 then @selector.x = 388; @selector.y = 40
  243.     when 9 then @selector.x = 299; @selector.y = 120
  244.     when 10 then @selector.x = 388; @selector.y = 120
  245.     when 11 then @selector.x = 299; @selector.y = 220
  246.     when 12 then @selector.x = 388; @selector.y = 220
  247.     end
  248.   end
  249.  
  250.   ################################################################################
  251.   #-------------------------------------------------------------------------------
  252.   #Receives the other user's party.
  253.   #-------------------------------------------------------------------------------
  254.   ################################################################################
  255.   def theirparty(data)
  256.     @theirparty = Marshal.load(data.unpack("m")[0]) #load serialised party data
  257.     @listreceived = true
  258.   end
  259.  
  260.   ################################################################################
  261.   #-------------------------------------------------------------------------------
  262.   #Display the other uer's party.
  263.   #-------------------------------------------------------------------------------
  264.   ################################################################################
  265.   def their_list
  266.     @listreceived = false
  267.  
  268.     @theirpartylength = 1 if @theirparty[0] != nil
  269.     @theirpartylength = 2 if @theirparty[1] != nil
  270.     @theirpartylength = 3 if @theirparty[2] != nil
  271.     @theirpartylength = 4 if @theirparty[3] != nil
  272.     @theirpartylength = 5 if @theirparty[4] != nil
  273.     @theirpartylength = 6 if @theirparty[5] != nil
  274.     for i in 0..@theirpartylength - 1
  275.       @spritex[i] = PokemonTradeIcon.new(@theirparty[i].species, @theirparty[i].eggsteps, @theirparty[i].personalID, false, @viewport)
  276.     end
  277.     @spritex[0].x = 284 if @spritex[0] != nil
  278.     @spritex[0].y = 55 if @spritex[0] != nil
  279.     @spritex[1].x = 373 if @spritex[1] != nil
  280.     @spritex[1].y = 55 if @spritex[1] != nil
  281.     @spritex[2].x = 284 if @spritex[2] != nil
  282.     @spritex[2].y = 135 if @spritex[2] != nil
  283.     @spritex[3].x = 373 if @spritex[3] != nil
  284.     @spritex[3].y = 135 if @spritex[3] != nil
  285.     @spritex[4].x = 284 if @spritex[4] != nil
  286.     @spritex[4].y = 235 if @spritex[4] != nil
  287.     @spritex[5].x = 373 if @spritex[5] != nil
  288.     @spritex[5].y = 235 if @spritex[5] != nil
  289.     @spritex[0].z = 1000005 if @spritex[0] != nil
  290.     @spritex[1].z = 1000005 if @spritex[1] != nil
  291.     @spritex[2].z = 1000005 if @spritex[2] != nil
  292.     @spritex[3].z = 1000005 if @spritex[3] != nil
  293.     @spritex[4].z = 1000005 if @spritex[4] != nil
  294.     @spritex[5].z = 1000005 if @spritex[5] != nil
  295.   end
  296.  
  297.   ################################################################################
  298.   #-------------------------------------------------------------------------------
  299.   #Show's information about your chosen pokemon.
  300.   #-------------------------------------------------------------------------------
  301.   ################################################################################
  302.   def show_information
  303.     @waiting.visible = false
  304.     @info.visible = true
  305.     pkmn = $Trainer.party[@index - 1]
  306.     itemname = pkmn.item == 0 ? _INTL("NONE") : PBItems.getName(pkmn.item)
  307.     imagepos = []
  308.     @overlay2.bitmap.clear
  309.     @typebitmap = BitmapCache.load_bitmap(_INTL("Graphics/Pictures/types.png"))
  310.     @sprites["mypokemon"] = SpriteWrapper.new(@viewport)
  311.     @sprites["mypokemon"].bitmap = pbLoadTradeBitmap(pkmn.species, pkmn.eggsteps, pkmn.personalID, pkmn.trainerID)
  312.     @sprites["mypokemon"].mirror = true
  313.     pbPositionPokemonSprite(@sprites["mypokemon"], -10, 24)
  314.     @sprites["mypokemon"].z = 1000009
  315.     @sprites["mypokemon"].visible = true
  316.     @chosenpokemon = false
  317.     move0 = pkmn.moves[0].id == 0 ? "--" : PBMoves.getName(pkmn.moves[0].id)
  318.     move1 = pkmn.moves[1].id == 0 ? "--" : PBMoves.getName(pkmn.moves[1].id)
  319.     move2 = pkmn.moves[2].id == 0 ? "--" : PBMoves.getName(pkmn.moves[2].id)
  320.     move3 = pkmn.moves[3].id == 0 ? "--" : PBMoves.getName(pkmn.moves[3].id)
  321.     textpositions = [
  322.       [_INTL("#{pkmn.name}"), 40, 12, 2, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  323.       [_INTL("Lv: #{pkmn.level}"), 105, 0, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  324.       [_INTL("HP: #{pkmn.hp}/#{pkmn.totalhp}"), 105, 11, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  325.       [_INTL("Attack: #{pkmn.attack}"), 105, 22, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  326.       [_INTL("Defense: #{pkmn.defense}"), 105, 33, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  327.       [_INTL("Speed: #{pkmn.speed}"), 105, 44, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  328.       [_INTL("Sp. Att: #{pkmn.spatk}"), 105, 55, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  329.       [_INTL("Sp. Def: #{pkmn.spdef}"), 105, 66, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  330.       [_INTL("Nature: #{PBNatures.getName(pkmn.nature)}"), 105, 77, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  331.       [_INTL("Ability: #{PBAbilities.getName(pkmn.ability)}"), 105, 88, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  332.       [_INTL("Move 1: #{move0}"), 105, 99, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  333.       [_INTL("Move 2: #{move1}"), 105, 110, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  334.       [_INTL("Move 3: #{move2}"), 105, 121, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  335.       [_INTL("Move 4: #{move3}"), 105, 132, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  336.       [_INTL("Item: #{itemname}"), 2, 27, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  337.     ]
  338.     if pkmn.gender == 0
  339.       textpositions.push([_INTL("♂f0 "), 90, 12, false, Color.new(120, 184, 248), Color.new(0, 120, 248)])
  340.     elsif pkmn.gender == 1
  341.       textpositions.push([_INTL("♀f0 "), 90, 12, false, Color.new(248, 128, 128), Color.new(168, 24, 24)])
  342.     end
  343.     pbSetExtraSmallFont(@overlay2.bitmap)
  344.     pbDrawTextPositions(@overlay2.bitmap, textpositions)
  345.     type1rect = Rect.new(0, pkmn.type1 * 28, 64, 28)
  346.     type2rect = Rect.new(0, pkmn.type2 * 28, 64, 28)
  347.     @overlay2.bitmap.blt(170, 4, @typebitmap, type1rect)
  348.     @overlay2.bitmap.blt(170, 4 + 28, @typebitmap, type2rect) if pkmn.type1 != pkmn.type2
  349.     ballimage = sprintf("Graphics/Pictures/ball%02d_0.png", pkmn.ballused)
  350.     imagepos.push([ballimage, -4, 124, 0, 0, -1, -1])
  351.     pbDrawImagePositions(@overlay2.bitmap, imagepos)
  352.   end
  353.  
  354.   ################################################################################
  355.   #-------------------------------------------------------------------------------
  356.   #Display's the user's party.
  357.   #-------------------------------------------------------------------------------
  358.   ################################################################################
  359.   def mylist
  360.     textpos = [
  361.       [_INTL("#{$network.username}'s list"), 91, 14, 2, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  362.       [_INTL("#{@username}'s list"), 390, 14, 2, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  363.     ]
  364.     @overlay.bitmap.font.name = ["Pokemon Emerald Small", "Arial Narrow", "Arial"]
  365.     pbDrawTextPositions(@overlay.bitmap, textpos)
  366.     if @partysent == false
  367.       #we must serialie the data in order to send the whole class then encode
  368.       #in base 64 (and delete the newline that the packing causes) in order for
  369.       #server not to go beserk (serialised data is binary, server does not understand
  370.       #how to receive this data as it is, encoding in base 64 avoids this)
  371.       party = [Marshal.dump($Trainer.party)].pack("m").delete("\n")
  372.       $network.send("<TRA|party=" + party + ">")
  373.       @partysent = true
  374.     end
  375.     for i in 0..$Trainer.party.length - 1
  376.       @sprite[i] = PokemonTradeIcon.new($Trainer.party[i].species, $Trainer.party[i].eggsteps, $Trainer.party[i].personalID, false, @viewport)
  377.     end
  378.     @sprite[0].x = 31 if @sprite[0] != nil
  379.     @sprite[0].y = 55 if @sprite[0] != nil
  380.     @sprite[1].x = 120 if @sprite[1] != nil
  381.     @sprite[1].y = 55 if @sprite[1] != nil
  382.     @sprite[2].x = 31 if @sprite[2] != nil
  383.     @sprite[2].y = 135 if @sprite[2] != nil
  384.     @sprite[3].x = 120 if @sprite[3] != nil
  385.     @sprite[3].y = 135 if @sprite[3] != nil
  386.     @sprite[4].x = 31 if @sprite[4] != nil
  387.     @sprite[4].y = 235 if @sprite[4] != nil
  388.     @sprite[5].x = 120 if @sprite[5] != nil
  389.     @sprite[5].y = 235 if @sprite[5] != nil
  390.     @sprite[0].z = 1000005 if @sprite[0] != nil
  391.     @sprite[1].z = 1000005 if @sprite[1] != nil
  392.     @sprite[2].z = 1000005 if @sprite[2] != nil
  393.     @sprite[3].z = 1000005 if @sprite[3] != nil
  394.     @sprite[4].z = 1000005 if @sprite[4] != nil
  395.     @sprite[5].z = 1000005 if @sprite[5] != nil
  396.   end
  397.  
  398.   ################################################################################
  399.   #-------------------------------------------------------------------------------
  400.   #Receives the data for the other user's chosen pokemon.
  401.   #-------------------------------------------------------------------------------
  402.   ################################################################################
  403.   def receiveoffer(data)
  404.     @theirchosen = GTSHandler.objecttobattler(JSON.decode(data.unpack("m")[0])) #decode base 64 and load serialised data
  405.     @received = true
  406.   end
  407.  
  408.   ################################################################################
  409.   #-------------------------------------------------------------------------------
  410.   #Displays the information about the other user's chosen pokemon.
  411.   #-------------------------------------------------------------------------------
  412.   ################################################################################
  413.   def their_info
  414.     @received = false
  415.     @waiting.visible = false
  416.     itemname = @theirchosen.item == 0 ? _INTL("NONE") : PBItems.getName(@theirchosen.item)
  417.     @sprites["theirpokemon"].dispose if @sprites["theirpokemon"] != nil
  418.     @sprites["theirpokemon"] = SpriteWrapper.new(@viewport)
  419.     @sprites["theirpokemon"].bitmap = pbLoadTradeBitmap(@theirchosen.species, @theirchosen.eggsteps, @theirchosen.personalID, @theirchosen.trainerID)
  420.     pbPositionPokemonSprite(@sprites["theirpokemon"], 368, 24)
  421.     @sprites["theirpokemon"].z = 1000009
  422.     @sprites["theirpokemon"].visible = true
  423.     @overlay3.bitmap.clear
  424.     imagepos2 = []
  425.     @typebitmap2 = BitmapCache.load_bitmap(_INTL("Graphics/Pictures/types.png"))
  426.     move0x = @theirchosen.moves[0] == 0 ? "--" : PBMoves.getName(@theirchosen.moves[0].id)
  427.     move1x = @theirchosen.moves[1] == 0 ? "--" : PBMoves.getName(@theirchosen.moves[1].id)
  428.     move2x = @theirchosen.moves[2] == 0 ? "--" : PBMoves.getName(@theirchosen.moves[2].id)
  429.     move3x = @theirchosen.moves[3] == 0 ? "--" : PBMoves.getName(@theirchosen.moves[3].id)
  430.     textpositions2 = [
  431.       [_INTL("{1}", @theirchosen.name), 440, 12, 2, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  432.       [_INTL("Lv: {1}", @theirchosen.level), 257, 0, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  433.       [_INTL("HP: {1}/{2}", @theirchosen.hp, @theirchosen.totalhp), 257, 11, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  434.       [_INTL("Attack: {1}", @theirchosen.attack), 257, 22, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  435.       [_INTL("Defense: {1}", @theirchosen.defense), 257, 33, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  436.       [_INTL("Speed: {1}", @theirchosen.speed), 257, 44, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  437.       [_INTL("Sp. Att: {1}", @theirchosen.spatk), 257, 55, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  438.       [_INTL("Sp. Def: {1}", @theirchosen.spdef), 257, 66, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  439.       [_INTL("Nature: {1}", PBNatures.getName(@theirchosen.nature)), 257, 77, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  440.       [_INTL("Ability: {1}", PBAbilities.getName(@theirchosen.ability)), 257, 88, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  441.       [_INTL("Move 1: {1}", move0x), 257, 99, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  442.       [_INTL("Move 2: {1}", move1x), 257, 110, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  443.       [_INTL("Move 3: {1}", move2x), 257, 121, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  444.       [_INTL("Move 4: {1}", move3x), 257, 132, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  445.       [_INTL("Item: {1}", itemname), 383, 27, 0, Color.new(255, 255, 255), Color.new(0, 0, 0)],
  446.     ]
  447.     if @theirchosen.gender == 0
  448.       textpositions2.push([_INTL("♂f0 "), 390, 12, false, Color.new(120, 184, 248), Color.new(0, 120, 248)])
  449.     elsif @theirchosen.gender == 1
  450.       textpositions2.push([_INTL("♀f0 "), 390, 12, false, Color.new(248, 128, 128), Color.new(168, 24, 24)])
  451.     end
  452.     pbSetExtraSmallFont(@overlay3.bitmap)
  453.     pbDrawTextPositions(@overlay3.bitmap, textpositions2)
  454.     type1rect2 = Rect.new(0, (@theirchosen.type1) * 28, 64, 28)
  455.     type2rect2 = Rect.new(0, (@theirchosen.type2) * 28, 64, 28)
  456.     @overlay3.bitmap.blt(320, 4, @typebitmap2, type1rect2)
  457.     @overlay3.bitmap.blt(320, 4 + 28, @typebitmap2, type2rect2) if @theirchosen.type1 != @theirchosen.type2
  458.     ballimage = sprintf("Graphics/Pictures/ball%02d_0.png", @theirchosen.ballused)
  459.     imagepos2.push([ballimage, 452, 124, 0, 0, -1, -1])
  460.     pbDrawImagePositions(@overlay2.bitmap, imagepos2)
  461.     if Kernel.pbConfirmMessage(_INTL("Trade your #{$Trainer.party[@index - 1].name} for their #{@theirchosen.name}?"))
  462.       @waiting.visible = true
  463.       $network.send("<TRA|accepted>")
  464.       @accepted = true
  465.     else
  466.       trade_declined
  467.       $network.send("<TRA|declined>")
  468.     end
  469.   end
  470.  
  471.   ################################################################################
  472.   #-------------------------------------------------------------------------------
  473.   #Procedure that is called when the other player declines the trade.
  474.   #-------------------------------------------------------------------------------
  475.   ################################################################################
  476.   def trade_declined
  477.     @waiting.visible = false
  478.     @info.visible = false
  479.     @sprites["mypokemon"].dispose
  480.     @sprites["theirpokemon"].dispose
  481.     @overlay2.bitmap.clear
  482.     @overlay3.bitmap.clear
  483.     @pokemonselected = false
  484.     @theirchosen = nil
  485.     @accepted = false
  486.     @received = false
  487.     @listreceived = false
  488.   end
  489.  
  490.   ################################################################################
  491.   #-------------------------------------------------------------------------------
  492.   #Excutes the trade, this is where the pokemon chosen is modified to the new one.
  493.   #-------------------------------------------------------------------------------
  494.   ################################################################################
  495.   def execute_trade
  496.     @waiting.visible = false
  497.     old = $Trainer.party[@index - 1]
  498.     $Trainer.party[@index - 1] = @theirchosen
  499.     pbSave
  500.     #backtotrade
  501.     evo = PokemonTradeScene.new
  502.     evo.pbStartScreen(old, @theirchosen, $network.username, @username)
  503.     evo.pbTrade(true)
  504.     evo.pbEndScreen
  505.     $Trainer.seen[@theirchosen.species] = true
  506.     $Trainer.owned[@theirchosen.species] = true
  507.  
  508.     $network.send("<TRA|dead>")
  509.     pbSave
  510.     Kernel.pbMessage("Saved the game!")
  511.     $scene = Connect.new
  512.   end
  513. end
  514.  
  515. ################################################################################
  516. #-------------------------------------------------------------------------------
  517. #Other Essentials based classes and methods needed for the scene to function
  518. #-------------------------------------------------------------------------------
  519. ################################################################################
  520. class PokemonTradeIcon < SpriteWrapper
  521.   attr_accessor :selected
  522.   attr_accessor :active
  523.   attr_reader :pokemon
  524.  
  525.   def initialize(pokemon, eggsteps, personalID, active, viewport = nil)
  526.     super(viewport)
  527.     @eggsteps = eggsteps
  528.     @personalID = personalID
  529.     @animbitmap = nil
  530.     @frames = [
  531.       Rect.new(0, 0, 64, 64),
  532.       Rect.new(64, 0, 64, 64),
  533.     ]
  534.     @active = active
  535.     @selected = false
  536.     @animframe = 0
  537.     self.pokemon = pokemon
  538.     @frame = 0
  539.     @pokemon = pokemon
  540.     @spriteX = self.x
  541.     @spriteY = self.y
  542.     @updating = false
  543.   end
  544.  
  545.   def width
  546.     return 300
  547.   end
  548.  
  549.   def height
  550.     return 300
  551.   end
  552.  
  553.   def pokemon=(value)
  554.     @animbitmap.dispose if @animbitmap
  555.     @animbitmap = pbLoadTradeIcon(value, @eggsteps, @personalID)
  556.     self.bitmap = @animbitmap
  557.     self.src_rect = @frames[@animframe]
  558.   end
  559.  
  560.   def dispose
  561.     @animbitmap.dispose
  562.     super
  563.   end
  564.  
  565.   def update
  566.     @updating = true
  567.     super
  568.     frameskip = 5
  569.     if frameskip == -1
  570.       @animframe = 0
  571.       self.src_rect = @frames[@animframe]
  572.     else
  573.       @frame += 1
  574.       @frame = 0 if @frame > 100
  575.       if @frame >= frameskip
  576.         @animframe = (@animframe == 1) ? 0 : 1
  577.         self.src_rect = @frames[@animframe]
  578.         @frame = 0
  579.       end
  580.     end
  581.     if self.selected
  582.       if !self.active
  583.         self.x = @spriteX + 8
  584.         self.y = (@animframe == 0) ? @spriteY - 6 : @spriteY + 2
  585.       else
  586.         self.x = @spriteX
  587.         self.y = (@animframe == 0) ? @spriteY + 2 : @spriteY + 10
  588.       end
  589.     end
  590.   end
  591.  
  592.   def x=(value)
  593.     super
  594.     @spriteX = value if !@updating
  595.   end
  596.  
  597.   def y=(value)
  598.     super
  599.     @spriteY = value if !@updating
  600.   end
  601. end
  602.  
  603. def pbLoadTradeIcon(pokemon, eggsteps, personalID)
  604.   return BitmapCache.load_bitmap(pbPokemonTradeFile(pokemon, eggsteps, personalID))
  605. end
  606.  
  607. def pbPokemonTradeFile(pokemon, eggsteps, personalID)
  608.   # return sprintf("Graphics/Pictures/ball00.png")
  609.  
  610.   @pokemon = pokemon.to_i
  611.   @eggsteps = eggsteps.to_i
  612.   @personalID = personalID.to_i
  613.   # if @eggsteps > 1
  614.   # Special case for eggs
  615.   #  return sprintf("Graphics/Pictures/iconEgg.png")
  616.   # end
  617.   if isConst?(@pokemon, PBSpecies, :UNOWN)
  618.     # Special case for Unown
  619.     d = @personalID & 3
  620.     d |= ((@personalID >> 8) & 3) << 2
  621.     d |= ((@personalID >> 16) & 3) << 4
  622.     d |= ((@personalID >> 24) & 3) << 6
  623.     d %= 28  # index of letter : ABCDEFGHIJKLMNOPQRSTUVWXYZ!?
  624.     filename = sprintf("Graphics/Icons/icon%03d_%02d.png", @pokemon, d)
  625.     begin
  626.       load_data(filename)
  627.       return filename
  628.     rescue Errno::ENOENT, Errno::EACCES, RGSSError
  629.       # File not found, just fall back
  630.     end
  631.   end
  632.   return sprintf("Graphics/Icons/icon%03d.png", pokemon)
  633. end
  634.  
  635. def pbLoadTradeBitmap(species, eggsteps, personalID, trainerID)
  636.   return pbLoadTradeBitmapSpecies(species, eggsteps, personalID, trainerID)
  637. end
  638.  
  639. def pbLoadTradeBitmapSpecies(species, eggsteps, personalID, trainerID)
  640.   return BitmapCache.load_bitmap(
  641.            sprintf("Graphics/Pictures/ball00.png")
  642.          )
  643. =begin
  644.   if eggsteps.to_i > 0
  645.    return BitmapCache.load_bitmap(
  646.      sprintf("Graphics/Pictures/egg.png"))
  647.   end
  648.   bitmapFileName=sprintf("Graphics/Battlers/%03d%s.png",species,pbcheckShiny(personalID,trainerID) ? "s" : "")
  649.   if isConst?(species,PBSpecies,:SPINDA)
  650.    bitmap=Bitmap.new(bitmapFileName)
  651.    pbTradeSpindaSpots(pokemon,bitmap)
  652.    return bitmap
  653.   elsif isConst?(species,PBSpecies,:UNOWN)
  654.    d=personalID&3
  655.    d|=((personalID>>8)&3)<<2
  656.    d|=((personalID>>16)&3)<<4
  657.    d|=((personalID>>24)&3)<<6
  658.    d%=28 # index of letter : ABCDEFGHIJKLMNOPQRSTUVWXYZ!?
  659.    begin
  660.     # Load special bitmap if found
  661.     # Example: 201b_02 for the letter C
  662.     return BitmapCache.load_bitmap(
  663.       sprintf("Graphics/Battlers/%03d%s_%02d.png",species,
  664.        pbcheckShiny(personalID,trainerID) ? "s" : "",d)
  665.     )
  666.    rescue
  667.     # Load plain bitmap as usual (see below)
  668.    end
  669.  end
  670.   return BitmapCache.load_bitmap(bitmapFileName)
  671. =end
  672. end
  673.  
  674. def pbSetExtraSmallFont(bitmap)
  675.   bitmap.font.name = ["Pokemon Emerald Small", "Arial Narrow", "Arial"]
  676.   bitmap.font.size = 16
  677. end
  678.  
  679. def pbcheckShiny(personalID, trainerID)
  680.   a = personalID.to_i ^ trainerID.to_i
  681.   b = a & 0xFFFF
  682.   c = (a >> 16) & 0xFFFF
  683.   d = b ^ c
  684.   return (d < 8)
  685. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement