#==============================================================================
# ** TDS Vehicle Maps
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  This script gives vehicles a map that can be accessed while on the vehicle.
#------------------------------------------------------------------------------
#  * Features: 
#  Gives vehicles a map.
#  Vehicle maps can be changed at any time.
#  Allows you to return directly to the vehicle from a map.
#------------------------------------------------------------------------------
#  * Instructions:
#  To set a Vehicle map use this in a script call from an event:
#
#  set_vehicle_map(vehicle, map_id, x, y, direction = 2)
#
#  vehicle   =  Vehicle type (0: boat, 1: ship, 2: airship)
#  map_id    = map id
#  x         = x-coordinate
#  y         = y-coordinate
#  direction = direction  (2,4,6,8)  
#
#  Example:
#
#  set_vehicle_map(1, 9, 8, 5, 2)
#
#
#  To return to a vehicle use this in a script call from an event.
#  
#  return_to_vehicle(vehicle)
#
#  vehicle   =  Vehicle type (0: boat, 1: ship, 2: airship)
#
#  Example:
#
#  return_to_vehicle(2)
#
#
#  To remove a vehicle map information, use this in a script call from an event:
#
#  clear_vehicle_map(vehicle) 
# 
#  vehicle   : Vehicle type (-1: all 0: boat, 1: ship, 2: airship) 
# 
#  Example:
#
#  clear_vehicle_map(1)
#------------------------------------------------------------------------------
#  * Notes:
#  To access a Vehicle Map press the "Shift" key.
#------------------------------------------------------------------------------
#  * New Methods:
#  Game_Vehicle:
#  - current_location
#    ^ Method used to get a vehicles current location.
#
#  Game_Player:
#  - can_transfer_to_vehicle_map?
#    ^ Method used to determine if it's possible to go to a vehicle map.
#  - setup_vehicle_map_transfer
#    ^ Method used to setup a transfer from the vehicle to it's map.
#  - return_to_vehicle_transfer(vehicle)
#    ^ Method used to return the player to a vehicle from a map.
#
#  Game_Interpreter:
#  - set_vehicle_map(vehicle, map_id, x, y, direction = 2)
#    ^ Method used to set a vehicles map and placing information.
#  - clear_vehicle_map(vehicle) 
#    ^ Method used to remove vehicle map information.
#  - return_to_vehicle(vehicle)
#    ^ Method used to return the player to a vehicle from a map.
#
#  Scene_Map:
#  - update_vehicle_map_transfer
#    ^ Method that waits for input to transfer player to a vehicle map.
#  - update_transfer_to_vehicle
#    ^ Method to smoothly transfer into a vehicle from a map.
#------------------------------------------------------------------------------
#  * Aliased Methods:
#  Game_Player:
#  - initialize
#    ^ Aliased to initialize vehicle map values hash
#
#  Scene_Map:
#  - update
#    ^ Aliased to wait for input to transfer into a Vehicle map.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================

#==============================================================================
# ** Game_Vehicle
#------------------------------------------------------------------------------
#  This class handles vehicles. It's used within the Game_Map class. If there
# are no vehicles on the current map, the coordinates is set to (-1,-1).
#==============================================================================

class Game_Vehicle < Game_Character
 #--------------------------------------------------------------------------
  # * Get Vehicle Current Position Values
  #--------------------------------------------------------------------------
  def current_location
    # Return Vehicle Map ID, X and Y Values
    return @map_id, @x, @y, @direction
  end  
end

#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles maps. It includes event starting determinants and map
# scrolling functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor   :vehicle_maps               # Vehicle Maps Properties Hash
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias tds_vehicle_map_game_player_initialize      initialize      unless $@  
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    # Run Original Method
    tds_vehicle_map_game_player_initialize 
    # Vehicle Maps Properties Hash
    @vehicle_maps = {}
  end
  #--------------------------------------------------------------------------
  # * Determine if it's possible to transfer into a vehicle map
  #--------------------------------------------------------------------------
  def can_transfer_to_vehicle_map?
    # Return False if moving
    return false if moving?
    # Return false if not in vehicle
    return false if !in_vehicle?
    # Return false if Vehicle Map Information is nil
    return false if @vehicle_maps[@vehicle_type] == nil
    # Return true
    return true
  end
  #--------------------------------------------------------------------------
  # * Setup Vehicle Map Transfer
  #--------------------------------------------------------------------------
  def setup_vehicle_map_transfer
    # Get off Vehicle processing
    $game_map.vehicles[@vehicle_type].get_off
    # Transfer information
    t_inf = @vehicle_maps[@vehicle_type]    
    # Erase vehicle type
    @vehicle_type = -1
    # Reserve Player Map Transfter
    reserve_transfer(t_inf[0], t_inf[1], t_inf[2], t_inf[3]) 
    # Remove transparency 
    @transparent = false                     
    # Passage OFF       
    @through = false
    # Reset Move Speed to normal walking
    @move_speed = 4    
  end  
  #--------------------------------------------------------------------------
  # * Return to Vehicle Transfer
  #     vehicle   : Vehicle type (0: boat, 1: ship, 2: airship)  
  #--------------------------------------------------------------------------
  def return_to_vehicle_transfer(vehicle)    
    # Map Transfer Information
    t_inf = $game_map.vehicles[vehicle].current_location    
    # Reserve Player Map Transfter
    reserve_transfer(t_inf[0], t_inf[1], t_inf[2], t_inf[3])    
    # Vehicle Case
    case vehicle
    when 0 ; get_on_boat
    when 1 ; get_on_ship
    when 2 ; get_on_airship
    end    
    # Set Walking BGM to map BGM    
    @walking_bgm = load_data(sprintf("Data/Map%03d.rvdata", t_inf[0])).bgm
  end  
end


#==============================================================================
# ** Game_Interpreter
#------------------------------------------------------------------------------
#  An interpreter for executing event commands. This class is used within the
# Game_Map, Game_Troop, and Game_Event classes.
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # * Set a Map for a Vehicle
  #     vehicle   : Vehicle type (0: boat, 1: ship, 2: airship)
  #     map_id    : map id
  #     x         : x-coordinate
  #     y         : y-coordinate
  #     direction : direction  (2,4,6,8)  
  #--------------------------------------------------------------------------
  def set_vehicle_map(vehicle, map_id, x, y, direction = 2)
    # Set Vehicle Map Information
    $game_player.vehicle_maps[vehicle] = [map_id, x, y, direction]
  end
  #--------------------------------------------------------------------------
  # * Removes Vehicle Map Information
  #     vehicle   : Vehicle type (-1: all 0: boat, 1: ship, 2: airship)
  #--------------------------------------------------------------------------
  def clear_vehicle_map(vehicle) 
    # If Vehicle type is less than 0 (Remove All)
    if vehicle < 0
      # Remove all Vehicle Map Information
      $game_player.vehicle_maps.clear 
    else
      # Remove Vehicle Information From Hash
      $game_player.vehicle_maps.delete(vehicle)     
    end     
  end  
  #--------------------------------------------------------------------------
  # * Return to a Vehicle
  #     vehicle   : Vehicle type (0: boat, 1: ship, 2: airship)
  #--------------------------------------------------------------------------
  def return_to_vehicle(vehicle)
    # If on Scene Map
    if $scene.is_a?(Scene_Map)      
      # Fadeout Screen
      $scene.fadeout(30)
      # Return Player to Vehicle Process
      $game_player.return_to_vehicle_transfer(vehicle)
      # Loop 25 times(Frames)
      (25).times do
        # Update Basic
        $scene.update_basic
        # Update Game Player
        $game_player.update
        # Update Transfer to Vehicle
        $scene.update_transfer_to_vehicle        
      end
      # Fade In Screen
      $scene.fadein(30)
    end
  end
end


#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs the map screen processing.
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------
  alias tds_vehicle_map_scene_map_update          update            unless $@  
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Run Original Method
    tds_vehicle_map_scene_map_update
    # Update Vehnicle Map Transfer
    update_vehicle_map_transfer    
  end
  #--------------------------------------------------------------------------
  # * Update Vehicle Map Transfer
  #--------------------------------------------------------------------------
  def update_vehicle_map_transfer    
    # Return if displaying a message
    return if $game_message.visible 
    # Return if Game Map Interpreter is running
    return if $game_map.interpreter.running?
    # Return if Game Player cannot transfer into a vehicle map
    return if !$game_player.can_transfer_to_vehicle_map?
    # If Input Trigger Shift
    if Input.trigger?(Input::A)      
      # Fadeout Screen
      fadeout(30)
      # Transfer Player to Vehicle Map
      $game_player.setup_vehicle_map_transfer 
      # Loop 20 times(Frames)
      (20).times do
        # Update Basic
        update_basic
        # Update Player Map Transfer
        update_transfer_player
      end
      # Fade In Screen
      fadein(30)
    end    
  end
  #--------------------------------------------------------------------------
  # * Player Transfer to Vehicle Processing
  #--------------------------------------------------------------------------
  def update_transfer_to_vehicle
    return unless $game_player.transfer?
    @spriteset.dispose              # Dispose of sprite set
    $game_player.perform_transfer   # Execute player transfer
    $game_map.update
    Graphics.wait(15)
    @spriteset = Spriteset_Map.new  # Recreate sprite set
    Input.update
  end  
end