#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Blizz-ABS Event Anti-Lag (ABSEAL) by Blizzard
# Version: 3.0b
# Type: Performance Improving System
# Date: 25.10.2006
# Date v2.0: 16.8.2007
# Date v3.0: 6.5.2008
# Date v3.0b: 14.7.2008
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  This work is protected by the following license:
# #----------------------------------------------------------------------------
# #  
# #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
# #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
# #  
# #  You are free:
# #  
# #  to Share - to copy, distribute and transmit the work
# #  to Remix - to adapt the work
# #  
# #  Under the following conditions:
# #  
# #  Attribution. You must attribute the work in the manner specified by the
# #  author or licensor (but not in any way that suggests that they endorse you
# #  or your use of the work).
# #  
# #  Noncommercial. You may not use this work for commercial purposes.
# #  
# #  Share alike. If you alter, transform, or build upon this work, you may
# #  distribute the resulting work only under the same or similar license to
# #  this one.
# #  
# #  - For any reuse or distribution, you must make clear to others the license
# #    terms of this work. The best way to do this is with a link to this web
# #    page.
# #  
# #  - Any of the above conditions can be waived if you get permission from the
# #    copyright holder.
# #  
# #  - Nothing in this license impairs or restricts the author's moral rights.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
# IMPORTANT NOTE:
# 
#   This EAL is a simplified version of Blizz-ABS's original EAL. If you are
#   using Blizz-ABS, please remove this script. Blizz-ABS has the full version
#   of Blizz-ABSEAL built-in.
# 
# 
# Compatibility:
# 
#   99% compatible with SDK v1.x. 80% compatible with SDK v2.x. Can cause
#   incompatibility issues with map graphic manipulating scripts. This script
#   comes UNDER all scripts that manipulate the Sprite_Character class.
#   Can cause problems with Scripts that manipulate instances of the
#   Sprite_Character class from the "outside".
# 
# 
# Advantages compared to other Anti-Lag Systems:
# 
#   - much more compatible
#   - faster and better processing
#   - about 5 times less code
#   - built-in option to disable in specific maps
#   - maximum possible performance
# 
# new in 2.0:
#   - an incredible performance improvement was achieved through a new system
# 
# new in 3.0:
#   - increased performance improvement to the highest possible level
#   - new smart system: no need to configure the strength anymore
# 
# new in 3.0b:
#   - disallows the usage of this script if Blizz-ABS is already installed
#   - now beta
# 
# 
# Explanation:
# 
#   This script will decrease the lag caused by events on the map by disabling
#   the update of events beyond the visible screen when their sprite is not
#   visible on the screen anymore. All graphical sprites for events are
#   substituted with a controller that handles those sprites. If an event's
#   update is disabled, the event's sprite will be disposed and completely
#   removed from the memory. "Auto-Start" and "Parallel Process" events are NOT
#   affected by this script. If a character is moving outside of the screen, he
#   will be updated as event, but not as sprite.
# 
# 
# Configuration:
# 
#   NO_ABSEAL_IDS - Add any map IDs and separate them with commas if you want
#                   to turn off Blizz-ABSEAL in those maps.
#       AUTO_KILL - If you turn this on, all events without a spriteset will be
#                   stopped updating automatically. If you need invisible
#                   events, use any spriteset with opacity = 0 in this case or
#                   even better a 8x4 pixel-sized empty dummy spriteset. This
#                   does not affect "Auto-Start" and "Parallel Processes".
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

NO_ABSEAL_IDS = []
AUTO_KILL = false

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

if $BlizzABS
  raise 'Blizz-ABS was detected! Please remove the additional Blizz-ABSEAL!'
end

#==============================================================================
# Game_Character
#==============================================================================

class Game_Character
  
  attr_accessor :sprite_size
  
  alias init_abseal_later initialize
  def initialize
    @sprite_size = [0, 1600, 0, 1600]
    init_abseal_later
  end
  
end
  
#==============================================================================
# Game_Event
#==============================================================================

class Game_Event < Game_Character
  
  alias upd_player_abseal_later update
  def update
    upd_player_abseal_later if self.update? || self.moving?
  end
  
  def update?
    return true if NO_ABSEAL_IDS.include?($game_map.map_id)
    return true if [3, 4].include?(self.trigger)
    return false if AUTO_KILL && @character_name == '' && @tile_id < 384
    return false if @real_x >= $game_map.display_x + 20 * 128 - @sprite_size[0]
    return false if @real_x < $game_map.display_x - @sprite_size[1]
    return false if @real_y >= $game_map.display_y + 15 * 128 - @sprite_size[2]
    return false if @real_y < $game_map.display_y - @sprite_size[3]
    return true
  end
  
end

#==============================================================================
# Control_Sprite_Character
#==============================================================================

class Control_Sprite_Character
  
  attr_reader :character
  
  def initialize(viewport, character = nil)
    self.character = character
    @viewport = viewport
  end
  
  def character=(char)
    @character = char
    @sprite.character = char unless @sprite == nil
  end
  
  def update
    if @character != nil && (@character.is_a?(Game_Event) &&
        @character.update? || !@character.is_a?(Game_Event))
      if @sprite == nil || @sprite.disposed?
        @sprite = Sprite_Character_ABSEAL_ed.new(@viewport, @character)
      else
        @sprite.update
      end
      if @sprite.bitmap != nil
        @character.sprite_size = [64-@sprite.src_rect.width*2,
            60+@sprite.src_rect.width*2, 128-@sprite.src_rect.height*4, 124]
        if @character.is_a?(Game_Event) && !@character.update?
          @sprite.dispose unless @sprite.disposed?
          @sprite = nil
        end
      else
        @character.size = [0, 124, 0, 124]
      end
    elsif @sprite != nil
      @sprite.dispose unless @sprite.disposed?
      @sprite = nil
    end
  end
  
  def dispose
    unless @sprite == nil || @sprite.disposed?
      @sprite.dispose
      @sprite = nil
    end
  end
  
end

#==============================================================================
# Sprite_Character_ABSEAL_ed
#==============================================================================

class Sprite_Character_ABSEAL_ed < Sprite_Character
  
end

#==============================================================================
# Sprite_Character
#==============================================================================

class Sprite_Character < Control_Sprite_Character
  
end
