#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
# ** Glitchfinder's Screen Size Editor                           [RPG Maker XP]
#    Version 1.00
#------------------------------------------------------------------------------
#  This script is meant to allow the programmer to change the screen size of
#  the RPG Maker XP window.
#==============================================================================
# * Version History
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#   Version 1.00 ------------------------------------------------- (????-??-??)
#     - Initial version
#     - Author: Glitchfinder
#==============================================================================
# * Instructions
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Place this script above Main, and below the default scripts. (I
#  realize this is obvious to most, but some people don't get it.)
#
#  To change the default screen size, simply edit the Screen_Data module, which
#  uses the following format:
#  [width (in tiles), height (in tiles), width, height]
#
#  To edit the size of the screen in game, simply use the following function:
#  Screen_Size.change_screen_size(width, height)
#  The width and height must be the number of tiles that you want the screen to
#  display in each direction.
#==============================================================================
# * Glitchfinder's Advice
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script is meant for people with a medium to high level of scripting
#  knowledge and ability. If you are unsure of your abilities, or don't know
#  how to script, then it would be a good idea to avoid this script.
#
#  If you are editing the default screen size in the Screen_Data module, the
#  second width and height should be the same as the first width and height
#  multiplied by 32, so that the player will display properly on the map.
#
#  If you make the screen size larger than the default, you must make sure that
#  all of your maps are at least that large, or you will get a fatal error.
#
#  If you use this script, you must be prepared to create your own custom
#  menus, titles, gameovers, etc. to fit the new screen size.
#==============================================================================
# * Contact
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  Glitchfinder, the author of this script, may be contacted through his
#  website, found at http://www.glitchkey.com
#
#  You may also find Glitchfinder at http://www.hbgames.org
#==============================================================================
# * Usage
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  This script may be used with the following terms and conditions:
#
#    1. This script is free to use in any noncommercial project. If you wish to
#       use this script in a commercial (paid) project, please contact
#       Glitchfinder at his website.
#    2. This script may only be hosted at the following domains:
#         http://www.glitchkey.com
#         http://www.hbgames.org
#    3. If you wish to host this script elsewhere, please contact Glitchfinder.
#    4. If you wish to translate this script, please contact Glitchfinder. He
#       will need the web address that you plan to host the script at, as well
#       as the language this script is being translated to.
#    5. This header must remain intact at all times.
#    6. Glitchfinder remains the sole owner of this code. He may modify or
#       revoke this license at any time, for any reason.
#    7. Any code derived from code within this script is owned by Glitchfinder,
#       and you must have his permission to publish, host, or distribute his
#       code.
#    8. This license applies to all code derived from the code within this
#       script.
#    9. If you use this script within your project, you must include visible
#       credit to Glitchfinder, within reason.
#≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

#==============================================================================
# ** Screen_Size
#------------------------------------------------------------------------------
#  This module handles the information regarding screen size. It is used within
#  the Screen_Size, Game_Map, Game_Player, Spriteset_Map, and Spriteset_Battle
#  classes.
#==============================================================================
 
module Screen_Data
    Screen_Data = [7, 8, 200, 250]
end
 
#==============================================================================
# ** Screen_Size
#------------------------------------------------------------------------------
#  This class handles the screen size. It is used within the Game_Map,
#  Game_Actor, Spriteset_Map, and Spriteset_Battle classes.
#==============================================================================
 
class Screen_Size
  #--------------------------------------------------------------------------
  # * Change Screen Size
  #--------------------------------------------------------------------------
  def change_screen_size(width, height)
    window_width = width * 32
    window_height = height * 32
    Screen_Data::Screen_Data[0] = width
    Screen_Data::Screen_Data[1] = height
    Screen_Data::Screen_Data[2] = window_width
    Screen_Data::Screen_Data[3] = window_height
    self.resize_window((width * 32), (height * 32))
  end
  #--------------------------------------------------------------------------
  # * Handle Window Size Changes
  #--------------------------------------------------------------------------
  def resize_window(width, height)
    getSystemMetrics = Win32API.new('user32', 'GetSystemMetrics', 'I', 'I')
    moveWindow = Win32API.new('user32','MoveWindow',
      ['l','i','i','i','i','l'],'l')
    findWindowEx = Win32API.new('user32','FindWindowEx',['l','l','p','p'],'i')
    window = findWindowEx.call(0,0,"RGSS Player",0)
    screenwidth = getSystemMetrics.call(0)
    screenheight = getSystemMetrics.call(1)
    windowwidth = width + 6
    windowheight = height + 32
    moveWindow.call(window,(screenwidth - windowwidth) / 2,
      (screenheight - windowheight) / 2,windowwidth,windowheight, 2)
  end
end
 
#==============================================================================
# ** Game_Map
#------------------------------------------------------------------------------
#  This class handles the map. It includes scrolling and passable determining
#  functions. Refer to "$game_map" for the instance of this class.
#==============================================================================
 
class Game_Map
  #--------------------------------------------------------------------------
  # * Scroll Down
  #     distance : scroll distance
  #--------------------------------------------------------------------------
  def scroll_down(distance)
    @display_y = [@display_y + distance,
      (self.height - Screen_Data::Screen_Data[1].to_s.to_i) * 128].min
  end
  #--------------------------------------------------------------------------
  # * Scroll Right
  #     distance : scroll distance
  #--------------------------------------------------------------------------
  def scroll_right(distance)
    screen = Screen_Size.new
    @display_x = [@display_x + distance,
      (self.width - Screen_Data::Screen_Data[0].to_s.to_i) * 128].min
    end
  end
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. Its functions include event starting
#  determinants and map scrolling. Refer to "$game_player" for the one
#  instance of this class.
#==============================================================================
 
class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias glitchfinder_screen_resizer_initialize initialize
  def initialize
    width = (Screen_Data::Screen_Data[2].to_s.to_i / 2)
    height = (Screen_Data::Screen_Data[3].to_s.to_i / 2)
    @center_x = (width - 16) * 4
    @center_y = (height - 16) * 4
    glitchfinder_screen_resizer_initialize
  end
  #--------------------------------------------------------------------------
  # * Set Map Display Position to Center of Screen
  #--------------------------------------------------------------------------
  def center(x, y)
    max_x = ($game_map.width - Screen_Data::Screen_Data[0].to_s.to_i) * 128
    max_y = ($game_map.height - Screen_Data::Screen_Data[1].to_s.to_i) * 128
    $game_map.display_x = [0, [x * 128 - @center_x, max_x].min].max
    $game_map.display_y = [0, [y * 128 - @center_y, max_y].min].max
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Remember whether or not moving in local variables
    last_moving = moving?
    # If moving, event running, move route forcing, and message window
    # display are all not occurring
    unless moving? or $game_system.map_interpreter.running? or
           @move_route_forcing or $game_temp.message_window_showing
      # Move player in the direction the directional button is being pressed
      case Input.dir4
      when 2
        move_down
      when 4
        move_left
      when 6
        move_right
      when 8
        move_up
      end
    end
    # Remember coordinates in local variables
    last_real_x = @real_x
    last_real_y = @real_y
    super
    # If character moves down and is positioned lower than the center
    # of the screen
    if @real_y > last_real_y and @real_y - $game_map.display_y > @center_y
      # Scroll map down
      $game_map.scroll_down(@real_y - last_real_y)
    end
    # If character moves left and is positioned more let on-screen than
    # center
    if @real_x < last_real_x and @real_x - $game_map.display_x < @center_x
      # Scroll map left
      $game_map.scroll_left(last_real_x - @real_x)
    end
    # If character moves right and is positioned more right on-screen than
    # center
    if @real_x > last_real_x and @real_x - $game_map.display_x > @center_x
      # Scroll map right
      $game_map.scroll_right(@real_x - last_real_x)
    end
    # If character moves up and is positioned higher than the center
    # of the screen
    if @real_y < last_real_y and @real_y - $game_map.display_y < @center_y
      # Scroll map up
      $game_map.scroll_up(last_real_y - @real_y)
    end
    # If not moving
    unless moving?
      # If player was moving last time
      if last_moving
        # Event determinant is via touch of same position event
        result = check_event_trigger_here([1,2])
        # If event which started does not exist
        if result == false
          # Disregard if debug mode is ON and ctrl key was pressed
          unless $DEBUG and Input.press?(Input::CTRL)
            # Encounter countdown
            if @encounter_count > 0
              @encounter_count -= 1
            end
          end
        end
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Same position and front event determinant
        check_event_trigger_here([0])
        check_event_trigger_there([0,1,2])
      end
    end
  end
end
 
#==============================================================================
# ** Spriteset_Map
#------------------------------------------------------------------------------
#  This class brings together map screen sprites, tilemaps, etc.
#  It's used within the Scene_Map class.
#==============================================================================
 
class Spriteset_Map
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    width = Screen_Data::Screen_Data[2].to_s.to_i
    height = Screen_Data::Screen_Data[3].to_s.to_i
    # Make viewports
    @viewport1 = Viewport.new(0, 0, width, height)
    @viewport2 = Viewport.new(0, 0, width, height)
    @viewport3 = Viewport.new(0, 0, width, height)
    @viewport2.z = 200
    @viewport3.z = 5000
    # Make tilemap
    @tilemap = Tilemap.new(@viewport1)
    @tilemap.tileset = RPG::Cache.tileset($game_map.tileset_name)
    for i in 0..6
      autotile_name = $game_map.autotile_names[i]
      @tilemap.autotiles[i] = RPG::Cache.autotile(autotile_name)
    end
    @tilemap.map_data = $game_map.data
    @tilemap.priorities = $game_map.priorities
    # Make panorama plane
    @panorama = Plane.new(@viewport1)
    @panorama.z = -1000
    # Make fog plane
    @fog = Plane.new(@viewport1)
    @fog.z = 3000
    # Make character sprites
    @character_sprites = []
    for i in $game_map.events.keys.sort
      sprite = Sprite_Character.new(@viewport1, $game_map.events[i])
      @character_sprites.push(sprite)
    end
    @character_sprites.push(Sprite_Character.new(@viewport1, $game_player))
    # Make weather
    @weather = RPG::Weather.new(@viewport1)
    # Make picture sprites
    @picture_sprites = []
    for i in 1..50
      @picture_sprites.push(Sprite_Picture.new(@viewport2,
        $game_screen.pictures[i]))
    end
    # Make timer sprite
    @timer_sprite = Sprite_Timer.new
    # Frame update
    update
  end
end
 
#==============================================================================
# ** Spriteset_Battle
#------------------------------------------------------------------------------
#  This class brings together battle screen sprites. It's used within
#  the Scene_Battle class.
#==============================================================================
 
class Spriteset_Battle
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    width = Screen_Data::Screen_Data[2].to_s.to_i
    height = Screen_Data::Screen_Data[3].to_s.to_i
    # Make viewports
    @viewport1 = Viewport.new(0, 0, width, ((height * 2) / 3).to_i)
    @viewport2 = Viewport.new(0, 0, width, height)
    @viewport3 = Viewport.new(0, 0, width, height)
    @viewport4 = Viewport.new(0, 0, width, height)
    @viewport2.z = 101
    @viewport3.z = 200
    @viewport4.z = 5000
    # Make battleback sprite
    @battleback_sprite = Sprite.new(@viewport1)
    # Make enemy sprites
    @enemy_sprites = []
    for enemy in $game_troop.enemies.reverse
      @enemy_sprites.push(Sprite_Battler.new(@viewport1, enemy))
    end
    # Make actor sprites
    @actor_sprites = []
    @actor_sprites.push(Sprite_Battler.new(@viewport2))
    @actor_sprites.push(Sprite_Battler.new(@viewport2))
    @actor_sprites.push(Sprite_Battler.new(@viewport2))
    @actor_sprites.push(Sprite_Battler.new(@viewport2))
    # Make weather
    @weather = RPG::Weather.new(@viewport1)
    # Make picture sprites
    @picture_sprites = []
    for i in 51..100
      @picture_sprites.push(Sprite_Picture.new(@viewport3,
        $game_screen.pictures[i]))
    end
    # Make timer sprite
    @timer_sprite = Sprite_Timer.new
    # Frame update
    update
  end
end
 
#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs title screen processing.
#==============================================================================
 
class Scene_Title
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # If battle test
    if $BTEST
      battle_test
      return
    end
    # Load database
    $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_troops        = load_data("Data/Troops.rxdata")
    $data_states        = load_data("Data/States.rxdata")
    $data_animations    = load_data("Data/Animations.rxdata")
    $data_tilesets      = load_data("Data/Tilesets.rxdata")
    $data_common_events = load_data("Data/CommonEvents.rxdata")
    $data_system        = load_data("Data/System.rxdata")
    # Make system object
    $game_system = Game_System.new
    # Make title graphic
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.title($data_system.title_name)
    # Make command window
    s1 = "New Game"
    s2 = "Continue"
    s3 = "Shutdown"
    @command_window = Window_Command.new(192, [s1, s2, s3])
    @command_window.back_opacity = 160
    @command_window.x = ((Screen_Data::Screen_Data[2].to_s.to_i / 2) -
                        (@command_window.width / 2))
    num = (((Screen_Data::Screen_Data[3].to_s.to_i - 128) * 9) / 11).to_i
    @command_window.y = num
    # Continue enabled determinant
    # Check if at least one save file exists
    # If enabled, make @continue_enabled true; if disabled, make it false
    @continue_enabled = false
    for i in 0..3
      if FileTest.exist?("Save#{i+1}.rxdata")
        @continue_enabled = true
      end
    end
    # If continue is enabled, move cursor to "Continue"
    # If disabled, display "Continue" text in gray
    if @continue_enabled
      @command_window.index = 1
    else
      @command_window.disable_item(1)
    end
    # Play title BGM
    $game_system.bgm_play($data_system.title_bgm)
    # Stop playing ME and BGS
    Audio.me_stop
    Audio.bgs_stop
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of command window
    @command_window.dispose
    # Dispose of title graphic
    @sprite.bitmap.dispose
    @sprite.dispose
  end
end
 
#==============================================================================
# ** Screen_Resizer
#------------------------------------------------------------------------------
#  This will begin the screen size change.
#==============================================================================
begin
  width = Screen_Data::Screen_Data[0].to_s.to_i
  height = Screen_Data::Screen_Data[1].to_s.to_i
  screen = Screen_Size.new
  screen.change_screen_size(width, height)
end