#==============================================================================
    # ** Scene_CTS Title Script
    #------------------------------------------------------------------------------
    # 
    #  =/ ©opyright  Markus Lechner -alias markusmks
    #  =/ Version = 1.00
    # | Instructions:
    # | Es handelt sich um ein Custom Title Script (Kurz: CTS)
    # | Hiermit lassen sich schnell bewegliche, gefüllte Title Screens erstellen.
    # | Man braucht sich nur die Daten aus den Optionen umändern und schon hat man
    # | so ziemlich alles.
    # | 
    # | Einfügen:
    # |   Um es recht unkompliziert zu halten, es muss über all 
    # |   Scene_CTS.new statt Scene_Title.new stehen. Also in:
    # |   Main           > line 11
    # |   Scene_End      > line 84
    # |   Scene_Map      > line 84
    # |   Scene_Gameover > line 56
    # |   Scene_Battle 1 > line 286
    # | 
    # |   - Oder du löscht Scene_Title und benennst Scene_CTS einfach danach um.
    # |     Einfacher zum Einbauen...
    # | 
    # | $cts_back       / Hintergrundbild, wenn nil wird standard verwendet.
    # |                   Wenn ein String eingefügt wird, wird das Bild aus dem
    # |                   Picture Ordner entnommen.
    # | $cts_right_back / Rechtes Einblende Bild ( Hinter Command_Window )
    # | $cts_moving     / Bewegung des Bildes > Richtungen unten gezeigt...
    # | 
    # | $cts_command_y  / Höhe von Auswahl Fenster (Command_Window)
    # | 
    # | $cts_layer_x  / Man kann 9 Layers einfügen, wovon Layer 8,9 über alles andere
    # |                 ist. Bsp: $cts_layer_8 = "bild.png"
    # |                 _8 ist unter dem Command_Window / _9 ist darüber
    # | 
    # | 
    # | $cts_cursor     / Cursor Grafik
    # | $cts_cursor_ani_size / Für bewegliche Cursor > Bilder immer in 32x32 halten.
    # |             Die einzellnen Grafiken in einer Bild-Datei horizontal einfügen.
    # | 
    # | $cts_cursor_speed / Geschwindigkeit, wie schnell der Cursor die Animationen
    # |                     durchführt. Je höher desto langsamer.
    # | 
    # | $cts_show_transition  / Fade out bein Spiel start Aktivieren od. Deaktivieren
    # | $cts_transition_speed / Fade out geschwindigkeit > Je niedriger desto langsamer
    # |
    # | Zusatzfunktionen:
    # |   Es lassen sich direkt im Title Screen die Variablen sowie Switches ändern.
    # |   Wenn benötigt kann man auch das Vermögen ändern... (Gold)
    # |   Switch/Variablen = F5
    # |   Gold             = F6
    # |
    # | Info:
    # |   Alle Grafiken müssen sich in [Spiele_Ordner/Graphics/Pictures/]
    # |   befinden, also im Picture Ordner.
    # |
    # $ 
    # $ Bemerkung:
    # $   Dieses Script baut auf die Community auf und darf somit frei verwendet
    # $   werden, solang die Ersteller-Daten nicht verändert werden.
    # $   Schreibfehler, Bugs bleiben dem Script überlassen, Sie werden jedoch auf
    # $   Anfrage (Beschrängt) gerne verbessert.
    # $   Mit diesem Anschreiben stelle ich mir keine Verpflichtungen gegenüber
    # $   diesem Script, der Support steht auch der Community zu.
    # $   Mit diesen Worten, Viel Spaß mit diesem Script!
    # $ 
    # | 
    # | -©- Markus Lechner alias markusmks
    #==============================================================================
    # OPTIONEN --------------------------------------------------------------------
    $cts_back       = "1.png"      # nil ( Hintergrund Bild Bsp: "Bild.png" )
    $cts_right_back = "1.png" # Anzeige Bild
    $cts_moving     = 1,2,4              # 3 (0=Nichts,1=Rauf,2=Runter,3=Links,4=Rechts)
    $cts_command_y  = 350             # 350| Command Fenster Y Anzeige
    $cts_new_game = "Start the pain"        # "Neues Spiel"
    $cts_continue = "Continue the pain" # "Fortsetzen"
    $cts_end_game = "End of pain" # "Beenden"
    $cts_layer_1  = "1.png"     # Bsp. Layer Grafik 1 (640x480)
    $cts_layer_2  = "1.png"     # Bsp. Layer Grafik 8
    $cts_layer_3  = "1.png"     # Bsp. Layer Grafik 2
    $cts_layer_4  = "1.png"       # Bsp. Layer Grafik 7
      # $cts_layer_x || x austauschen mit einer Zahl von 1-9
    $cts_cursor   = "cursor.png"      # Cursor Bild ( 32x32 )
    $cts_cursor_ani_size = 4          # 4 | Animations Größe (Ablauf der Bilder)
    $cts_cursor_speed = 3             # 3 | Cursor Ablauf Geschwindigkeit
    $cts_show_transition  = true      # true | fadet Screen bei Spiel start ab.
    $cts_transition_speed = 6         # 6 | Fadeout Zeit / Je niedriger desto langsamer
    # OPTIONEN ####################################################################
    class Scene_CTS
      #--------------------------------------------------------------------------
      # * Main Processing
      #--------------------------------------------------------------------------
      def main
        if $BTEST
          battle_test
          return
        end
        $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")
        $game_switches      = Game_Switches.new
        $game_variables     = Game_Variables.new
        $game_party         = Game_Party.new
        $game_system        = Game_System.new
        @sprite = Plane.new
        if $cts_back == nil
          back_picture = RPG:

ache.title($data_system.title_name)
        else
          back_picture = RPG:

ache.picture($cts_back)
        end
        @sprite.bitmap = back_picture
        @sprite_move_timer = 0
        @moving_actions = 0
        s1 = $cts_new_game
        s2 = $cts_continue
        s3 = $cts_end_game
        @layer_graphics = []
        for i in 1..9
          script = "$cts_layer_"+i.to_s+" != nil"
          result = eval(script)
          @layer_graphics.push(i) if result == true
        end
        for num in @layer_graphics
          graphic = eval("$cts_layer_"+num.to_s)
          eval("@cts_sprite_"+num.to_s+' = Sprite.new')
          eval("@cts_sprite_"+num.to_s+'.bitmap = RPG:

ache.picture(graphic)')
          eval("@cts_sprite_"+num.to_s+'.z = 99')  if num == 8
          eval("@cts_sprite_"+num.to_s+'.z = 106') if num == 9
        end
        @command_window = Window_Command.new(192, [s1, s2, s3])
        @command_window.back_opacity = 160
        @command_window.x = 640 - @command_window.width
        @command_window.y = $cts_command_y
        @command_window.opacity = 0
        @command_window.contents_opacity = 0
        @command_window.active = false
        @cts_cursor = Sprite.new
        @cts_cursor.bitmap = RPG:

ache.picture($cts_cursor.to_s)
        @cts_cursor.src_rect.set(0, 0, 32, 32)
        @cts_cursor.z = 105
        @cts_cursor.x = 600
        @cts_cursor.y = $cts_command_y+8
        @cts_cursor.opacity = 0
        @cts_cursor_movin = 1
        @cts_cursor_movin_timer = 0
        @continue_enabled = false
        @rigth_back_sprite = Sprite.new
        @rigth_back_sprite.bitmap = RPG:

ache.picture($cts_right_back.to_s)
        @rigth_back_sprite.x = 640
        @wait_timer = 0
        if $cts_show_transition
          @transition_graph = Sprite.new
          @transition_graph.bitmap = Bitmap.new(640,480)
          @transition_graph.bitmap.fill_rect(0,0,640,480,Color.new(0,0,0))
          @transition_graph.opacity = 0
          @transition_graph.z = 9999
        end
        for i in 0..3
          if FileTest.exist?("Save#{i+1}.rxdata")
            @continue_enabled = true
          end
        end
        if @continue_enabled
          @command_window.index = 1
        else
          @command_window.disable_item(1)
        end
        if $DEBUG
          @left_window = Window_DebugLeft.new
          @left_window.opacity = 220
          @left_window.z = 500
          @right_window = Window_DebugRight.new
          @right_window.opacity = 220
          @right_window.z = 500
          @help_window = Window_Base.new(192, 352, 448, 128)
          @help_window.contents = Bitmap.new(406, 96)
          @help_window.opacity = 220
          @help_window.z = 500
          @left_window.active = true
          @right_window.active = false
          @left_window.visible = false
          @right_window.visible = false
          @help_window.visible = false
          @gold_window = Window_Base.new(170,208,300,128)
          @gold_window.contents = Bitmap.new(268,96)
          @gold_window.contents.draw_text(0,0,268,32,"Gold:")
          @gold_window.contents.draw_text(0,0,268,32,$game_party.gold.to_s,2)
          @gold_window.z = 505
          @gold_window.opacity = 192
          @gold_window.visible = false
          @gold_window_trans = Sprite.new
          @gold_window_trans.bitmap = Bitmap.new(640,480)
          @gold_window_trans.bitmap.fill_rect(0,0,640,480,Color.new(0,0,0,190))
          @gold_window_trans.z = 500
          @gold_window_trans.visible = false
        end
        @msc_active = false
        @msc_active_gold = false
        $game_system.bgm_play($data_system.title_bgm)
        Audio.me_stop
        Audio.bgs_stop
        Graphics.transition
        loop do
          Graphics.update
          Input.update
          update
          if $scene != self
            break
          end
        end
        Graphics.freeze
        @command_window.dispose
        @sprite.bitmap.dispose
        @sprite.dispose
        for num in @layer_graphics
          eval("@cts_sprite_"+num.to_s+'.bitmap.dispose')
          eval("@cts_sprite_"+num.to_s+'.dispose')
        end
        if $DEBUG
          @left_window.dispose
          @right_window.dispose
          @help_window.dispose
        end
        @cts_cursor.bitmap.dispose
        @cts_cursor.dispose
        @rigth_back_sprite.bitmap.dispose
        @rigth_back_sprite.dispose
        if $cts_show_transition
          @transition_graph.bitmap.dispose
          @transition_graph.dispose
        end
      end
      #--------------------------------------------------------------------------
      # * Frame Update
      #--------------------------------------------------------------------------
      def update
        if $DEBUG
          if Input.press?(Input::F5) or @msc_active == true 
            return if @msc_active_gold == true
            @left_window.visible = true
            @right_window.visible = true
            @help_window.visible = true
            update_msc
            return
          else
            @left_window.visible = false
            @right_window.visible = false
            @help_window.visible = false
          end
          if Input.press?(Input::F6) or @msc_active_gold == true
            return if @msc_active == true
            update_gold
            return
          end
        end
        update_cursor
        @sprite_move_timer+=1
        if @sprite_move_timer % 2 == 0
          case $cts_moving
          when 1
            @sprite.oy += 1
          when 2
            @sprite.oy -= 1
          when 3
            @sprite.ox += 1
          when 4
            @sprite.ox -= 1
          end
        end
        case @moving_actions
        when 0
          if @rigth_back_sprite.x > 640 - @rigth_back_sprite.bitmap.width
            @rigth_back_sprite.x -= 5
          end
          if @rigth_back_sprite.x <= 640 - @rigth_back_sprite.bitmap.width
            @rigth_back_sprite.x = 640 - @rigth_back_sprite.bitmap.width
            @moving_actions += 1
          end
        when 1
          if @command_window.contents_opacity <= 255
            @command_window.contents_opacity += 10
            @cts_cursor.opacity += 12
            @command_window.active = true
          else
            @command_window.contents_opacity = 255
          end
        when 2
          if @command_window.contents_opacity >= 0
            @command_window.contents_opacity -= 10
            @cts_cursor.opacity -= 12
            @command_window.active = false
            @rigth_back_sprite.opacity -= 10
            @rigth_back_sprite.x += 1
            @wait_timer += 1
            if $cts_show_transition
              @transition_graph.opacity = @wait_timer*$cts_transition_speed
            else
              @wait_timer += 1
            end
          end
          if @wait_timer >= 50 
            if $cts_show_transition and not @transition_graph.opacity >= 255
              return
            end
            case @command_window.index
            when 0  # New game
              command_new_game
            when 1  # Continue
              command_continue
            when 2  # Shutdown
              command_shutdown
            end
          end
        end
        # Update command window
        @command_window.update
        # If C button was pressed
        if Input.trigger?(Input:

)
          @moving_actions = 2
          if @command_window.index != 1
            $game_system.se_play($data_system.decision_se)
          end
          if @command_window.index == 1
            unless @continue_enabled
              $game_system.se_play($data_system.buzzer_se)
              @moving_actions = 1
              return
            end
            $game_system.se_play($data_system.decision_se)
          end
          # Branch by command window cursor position
        end
      end
      #--------------------------------------------------------------------------
      # * Update Main Script Config
      #--------------------------------------------------------------------------
      def update_msc
        @msc_active = true
        @right_window.mode = @left_window.mode
        @right_window.top_id = @left_window.top_id
        @left_window.update
        @right_window.update
        if @left_window.active
          update_left
          return
        end
        if @right_window.active
          update_right
          return
        end
      end
      #--------------------------------------------------------------------------
      # * Update Main Script Config
      #--------------------------------------------------------------------------
      def update_gold
        @msc_active_gold = true
        @gold_window_trans.visible = true
        @gold_window.visible = true
        @gold_window.contents.clear
        if Input.trigger?(Input:

)
          # Play cancel SE
          $game_system.se_play($data_system.cancel_se)
          # Switch to map screen
          @gold_window.visible = false
          @gold_window_trans.visible = false
          @msc_active_gold = false
          return
        end
        color = Color.new(100,100,255,100)
        if Input.repeat?(Input::UP)
          $game_system.se_play($data_system.cursor_se)
          @gold_window.contents.fill_rect(0,64,89,32,color)
          $game_party.gain_gold(20)
        end
        if Input.repeat?(Input:

OWN)
          $game_system.se_play($data_system.cursor_se)
          @gold_window.contents.fill_rect(97,64,101,32,color)
          $game_party.lose_gold(20)
        end
        if Input.repeat?(Input::RIGHT)
          $game_system.se_play($data_system.cursor_se)
          @gold_window.contents.fill_rect(90,32,101,32,color)
          $game_party.gain_gold(5)
        end
        if Input.repeat?(Input::LEFT)
          $game_system.se_play($data_system.cursor_se)
          @gold_window.contents.fill_rect(0,32,77,32,color)
          $game_party.lose_gold(5)
        end
        @gold_window.contents.draw_text(0,0,268,32,"Gold:")
        @gold_window.contents.draw_text(0,0,268,32,$game_party.gold.to_s,2)
        @gold_window.contents.fill_rect(0,30,268,3,Color.new(0,0,0))
        @gold_window.contents.draw_text(0,32,268,32,"Links : -5   Rechts : +5")
        @gold_window.contents.draw_text(0,64,268,32,"Rauf : +20  Runter : -20")
      end
      #--------------------------------------------------------------------------
      # * Frame Update (when left window is active)
      #--------------------------------------------------------------------------
      def update_left
        # If B button was pressed
        if Input.trigger?(Input:

)
          # Play cancel SE
          $game_system.se_play($data_system.cancel_se)
          # Switch to map screen
          @msc_active = false
          return
        end
        # If C button was pressed
        if Input.trigger?(Input:

)
          # Play decision SE
          $game_system.se_play($data_system.decision_se)
          # Display help
          if @left_window.mode == 0
            text1 = "C (Enter) : Ein / Aus"
            @help_window.contents.draw_text(4, 0, 406, 32, text1)
          else
            text1 = "Links : -1   Rechts : +1"
            text2 = "L (RildRauf) : -10"
            text3 = "R (BildRunter) : +10"
            @help_window.contents.draw_text(4, 0, 406, 32, text1)
            @help_window.contents.draw_text(4, 32, 406, 32, text2)
            @help_window.contents.draw_text(4, 64, 406, 32, text3)
          end
          # Activate right window
          @left_window.active = false
          @right_window.active = true
          @right_window.index = 0
          return
        end
      end
      #--------------------------------------------------------------------------
      # * Frame Update (when right window is active)
      #--------------------------------------------------------------------------
      def update_right
        # If B button was pressed
        if Input.trigger?(Input:

)
          # Play cancel SE
          $game_system.se_play($data_system.cancel_se)
          # Activate left window
          @left_window.active = true
          @right_window.active = false
          @right_window.index = -1
          # Erase help
          @help_window.contents.clear
          return
        end
        # Get selected switch / variable ID
        current_id = @right_window.top_id + @right_window.index
        # If switch
        if @right_window.mode == 0
          # If C button was pressed
          if Input.trigger?(Input:

)
            # Play decision SE
            $game_system.se_play($data_system.decision_se)
            # Reverse ON / OFF
            $game_switches[current_id] = (not $game_switches[current_id])
            @right_window.refresh
            return
          end
        end
        # If variable
        if @right_window.mode == 1
          # If right button was pressed
          if Input.repeat?(Input::RIGHT)
            # Play cursor SE
            $game_system.se_play($data_system.cursor_se)
            # Increase variables by 1
            $game_variables[current_id] += 1
            # Maximum limit check
            if $game_variables[current_id] > 99999999
              $game_variables[current_id] = 99999999
            end
            @right_window.refresh
            return
          end
          # If left button was pressed
          if Input.repeat?(Input::LEFT)
            # Play cursor SE
            $game_system.se_play($data_system.cursor_se)
            # Decrease variables by 1
            $game_variables[current_id] -= 1
            # Minimum limit check
            if $game_variables[current_id] < -99999999
              $game_variables[current_id] = -99999999
            end
            @right_window.refresh
            return
          end
          # If R button was pressed
          if Input.repeat?(Input::R)
            # Play cursor SE
            $game_system.se_play($data_system.cursor_se)
            # Increase variables by 10
            $game_variables[current_id] += 10
            # Maximum limit check
            if $game_variables[current_id] > 99999999
              $game_variables[current_id] = 99999999
            end
            @right_window.refresh
            return
          end
          # If L button was pressed
          if Input.repeat?(Input::L)
            # Play cursor SE
            $game_system.se_play($data_system.cursor_se)
            # Decrease variables by 10
            $game_variables[current_id] -= 10
            # Minimum limit check
            if $game_variables[current_id] < -99999999
              $game_variables[current_id] = -99999999
            end
            @right_window.refresh
            return
          end
        end
      end
      #--------------------------------------------------------------------------
      # * Cursor Move Update
      #--------------------------------------------------------------------------
      def update_cursor
        y = ($cts_command_y+8) + (@command_window.index*32)
        if @cts_cursor.y < y
          @cts_cursor.y = y - (y-@cts_cursor.y) / 2
        elsif @cts_cursor.y > y
          @cts_cursor.y = y + (@cts_cursor.y-y) / 2
        end
        @cts_cursor_movin_timer += 1
        if @cts_cursor_movin_timer % $cts_cursor_speed == 0
          if $cts_cursor_ani_size > 1
            x = (@cts_cursor_movin-1)*32
            @cts_cursor.src_rect.set(x, 0, 32, 32)
            @cts_cursor_movin += 1
          end
          if @cts_cursor_movin >= $cts_cursor_ani_size+1
            @cts_cursor_movin = 1
          end
        end
      end
      #--------------------------------------------------------------------------
      # * Command: New Game
      #--------------------------------------------------------------------------
      def command_new_game
        # Stop BGM
        Audio.bgm_stop
        # Reset frame count for measuring play time
        Graphics.frame_count = 0
        # Make each type of game object
        $game_temp          = Game_Temp.new
        $game_system        = Game_System.new
        $game_self_switches = Game_SelfSwitches.new
        $game_screen        = Game_Screen.new
        $game_actors        = Game_Actors.new
        $game_troop         = Game_Troop.new
        $game_map           = Game_Map.new
        $game_player        = Game_Player.new
        # Set up initial party
        $game_party.setup_starting_members
        # Set up initial map position
        $game_map.setup($data_system.start_map_id)
        # Move player to initial position
        $game_player.moveto($data_system.start_x, $data_system.start_y)
        # Refresh player
        $game_player.refresh
        # Run automatic change for BGM and BGS set with map
        $game_map.autoplay
        # Update map (run parallel process event)
        $game_map.update
        # Switch to map screen
        $scene = Scene_Map.new
      end
      #--------------------------------------------------------------------------
      # * Command: Continue
      #--------------------------------------------------------------------------
      def command_continue
        # Switch to load screen
        $scene = Scene_Load.new
      end
      #--------------------------------------------------------------------------
      # * Command: Shutdown
      #--------------------------------------------------------------------------
      def command_shutdown
        # Fade out BGM, BGS, and ME
        Audio.bgm_fade(800)
        Audio.bgs_fade(800)
        Audio.me_fade(800)
        # Shutdown
        $scene = nil
      end
      #--------------------------------------------------------------------------
      # * Battle Test
      #--------------------------------------------------------------------------
      def battle_test
        # Load database (for battle test)
        $data_actors        = load_data("Data/BT_Actors.rxdata")
        $data_classes       = load_data("Data/BT_Classes.rxdata")
        $data_skills        = load_data("Data/BT_Skills.rxdata")
        $data_items         = load_data("Data/BT_Items.rxdata")
        $data_weapons       = load_data("Data/BT_Weapons.rxdata")
        $data_armors        = load_data("Data/BT_Armors.rxdata")
        $data_enemies       = load_data("Data/BT_Enemies.rxdata")
        $data_troops        = load_data("Data/BT_Troops.rxdata")
        $data_states        = load_data("Data/BT_States.rxdata")
        $data_animations    = load_data("Data/BT_Animations.rxdata")
        $data_tilesets      = load_data("Data/BT_Tilesets.rxdata")
        $data_common_events = load_data("Data/BT_CommonEvents.rxdata")
        $data_system        = load_data("Data/BT_System.rxdata")
        # Reset frame count for measuring play time
        Graphics.frame_count = 0
        # Make each game object
        $game_temp          = Game_Temp.new
        $game_system        = Game_System.new
        $game_switches      = Game_Switches.new
        $game_variables     = Game_Variables.new
        $game_self_switches = Game_SelfSwitches.new
        $game_screen        = Game_Screen.new
        $game_actors        = Game_Actors.new
        $game_party         = Game_Party.new
        $game_troop         = Game_Troop.new
        $game_map           = Game_Map.new
        $game_player        = Game_Player.new
        # Set up party for battle test
        $game_party.setup_battle_test_members
        # Set troop ID, can escape flag, and battleback
        $game_temp.battle_troop_id = $data_system.test_troop_id
        $game_temp.battle_can_escape = true
        $game_map.battleback_name = $data_system.battleback_name
        # Play battle start SE
        $game_system.se_play($data_system.battle_start_se)
        # Play battle BGM
        $game_system.bgm_play($game_system.battle_bgm)
        # Switch to battle screen
        $scene = Scene_Battle.new
      end
    end