# Put any overrides/extensions to RGSS modules here

module RPG
  module Cache
    # Character Sprites
    def self.character(filename, hue)
  	  filename = filename.downcase
      # Oneshot: Convert "niko" filenames to "en"
      if $game_switches[160] && filename.start_with?("niko")
        filename.gsub!(/niko/, "en")
      end

      # Equinox: Add "_glow" if switch is on, unless file doesn't exist
      if $game_switches[427]
        if File.exists?("Graphics/Characters/" + filename + "_glow.png")
          filename += "_glow"
        end
      end

      # Equinox: Add permanent "fix" to missing character textures
      # But exclude intentionally blank ones
      if filename != ""
        if !File.exists?("Graphics/Characters/" + filename + ".png")
          filename = "_"
        end
      end

      # Original Oneshot load command
      self.load_bitmap("Graphics/Characters/", filename, hue)
    end

    # Face Sprites
    def self.face(filename)
      #lowercasing facepic name to be more case insensitive
      #to catch the few instances where we use "Niko" instead of "niko"
      filename = filename.downcase

      #april fools!
      t = Time.now
      if t.month == 4 && t.day == 1 && filename.start_with?("niko")
        filename = "af"
      end

      # Oneshot: Convert "niko" filenames to "en"
      if $game_switches[160] && filename.start_with?("niko")
        filename.gsub!(/niko/, "en")
      end

      # Equinox: Add "_glow" if switch is on, unless file doesn't exist
      if $game_switches[427]
        if File.exists?("Graphics/Faces/" + filename + "_glow.png")
          filename += "_glow"
        end
      end

      # Equinox: If the face doesn't exist. Use a "missing texture" face
      # Permanently solves any issues with a wrong or missing face name
      if !File.exists?("Graphics/Faces/" + filename + ".png")
        filename = "_.png"
      end

      self.load_bitmap("Graphics/Faces/", filename)
    end

    def self.menu(filename)
      self.load_bitmap("Graphics/Menus/", filename)
    end
    def self.lightmap(filename)
      self.load_bitmap("Graphics/Lightmaps/", filename)
    end
    def self.light(filename)
      self.load_bitmap("Graphics/Lights/", filename)
    end
    def self.misc(filename)
      self.load_bitmap("Graphics/Misc/", filename)
    end
    
    # Equinox addition - "Large"
    def self.large(filename)
      self.load_bitmap("Graphics/Large/", filename)
    end
  end
end

class Tone
  def +(o)
    Tone.new(self.red + o.red, self.green + o.green, self.blue + o.blue, self.gray + o.gray)
  end

  def *(s)
    Tone.new(self.red * s, self.green * s, self.blue * s, self.gray * s)
  end

  def blank?
    self.red == 0 && self.green == 0 && self.blue == 0 && self.gray == 0
  end
end
