#==============================================================
#FF3-styled Enemies' Division and Transformation
#Orochii Zouveleki, as the scripter
#
#Special thanks to Queex's Tagnotes, and TywinLannister because his
#BOF_EnemySkills were my... well they made me wonder about this and that
#and stuff. No more commentaries haha... anyway.
#
#Part of Orochii's own testing, dont sell or rent it,
#idea inspired (or copied?) from FF3's dividing monsters,
#from NES (or DS, but it doesn't have the oldie magic :3).
#==============================================================
#¿WTHIT? (WTH's this)
#==============================================================
#Simply consists on including a tag on database notes that makes the monster duplicate itself
#when attacked or when some spells are used on it. Or vice-versa, it will not duplicate if
#not attacked with specific weapons or when used determined spells.
 
#*When the monster divides, it will *create* a duplicate of itself, same stats, same HP
#(if hp property isn't present, it will instead use maxHP).
#Later on, the divided monsters will have possibility to use percentages of stats (HP included)
#but for now, it is at is it.
#And also will be the summoning, so don't worry, that's in my mind :3.
#
#Out of spamming, you can play (if comprehension==null) some hours FF3 and fight against Hazazel/Chronos
#monsters, as they are the hated and goddamned dividing monsters! (and a host of others). And I HATE Them.
 
#The other functionality is transforming, later I will add the element stuff.
#==============================================================
#USING METHOD
#==============================================================
#The script works by tag inclusions in the database note fields (spell and monster).
#Mainly, the tags are: <divide>, <reverse_divide>, <ultradivide> and <transform>, that
#will be explained shortly.
#Also there's a little default configuration (search "module OROCHII_MONSTERS", without quotes).
#The config fields are a little self explanatory, if not, there's a little description above them.
 
#===========                       DIVIDIN' MONSTERS                       ===========#
#--<divide>
#When used in the notefield, the monster will be divided. The "extra properties" are explained below.
 
#-<divide hp>
#When <divide hp>, the monster will copy the original monster's HP (not monster's original HP ;P).
 
#-<divide hp ID>
#As the tagnotes module has some... limitations (less complicated for me, not THAT complicated for you),
#you must include hp tag (if hp tag isn't "hp", it will "ignore it" ;D, sou dun wuh-rie).
#The id "property" specifies the element ID that will use for exception (dividing nullificator).
 
#-<reverse_divide>
#When used (along with <divide> tag), it will cause a reverse-functionality on the divide property. Instead of
#ID being exception for dividing, it will be the exception for not dividing (hope it doesn't sound weird).
 
#<ultradivide>
#Used along with <divide> tag. Not compatible with <reverse_divide> (nor "hp" property) by now (some "if" nonsense, that's the error
¬¬, I will fix it later :P, as code itself is... uhh).
#Ultradivide removes the death restriction, as the monsters normally will not divide when dying.
 
#===========                 TRANSFORMIN' MONSTERS                 ===========#
#-<transform ID>
#When included in the enemy's notefield, the monster will divide when attacked.
#When included instead in skill's notefield, it will act as transforming spell. It doesn't
#works with heroes/allies, as it's not implemented by now (MAYBE later), and also the
#hero can transform enemies, but by now there's no restriction (later it will be by element
or state).
 
#Transformation works in maker itself (without this script), but I wanted to add some
#little and easy-to-use functionalities. Hope that doesn't make things boring... hahaha.
 
#==============================================================
#Install Notes - The real part is here >:D - Joking... of course:
#==============================================================
#   Nº1-Put script above Main (see non-printed-nor-released documentation, figure 1),
#   -BUT remember to include the Queex's Tagnotes above this script.
#   -That's it, now use tags on notefields and stuff and you're ready to be rumble'd!
#(not really, at least if your game is well-balanced in terms of monsterVSheroes stats ;D).
 
#==============================================================
#----------------------------------------------------------------------------------------------------------------------------
#Espero que les agrade ;D. (Hope you don't dislike it, I'm more pesimistic today xD).
#
#                                                       C'EST FINIS!
#==============================================================
module OROCHII_MONSTERS
  #Element used as exception for dividing (used as default value).
  DIVIDE_ELEMENT = 2
  #Minimum HP percentage for monster to divide.
  MIN_HP = 10
  #Maximum of enemies (Use 8 if you haven't done something with the battlestatus window :P,
  #I will not make anything with this as compatibility is my name). LATER will be the MAX_ACTIVE_ENEMIES,
  #self explanatory right?
  MAX_ENEMIES = 24 #<-You can use any number, but...
  #Distance unity, for randomized position calculation.
  DISTANCE_UNITY = 16
  #Maximum random X value. Remember: It will be multiplied by DISTANCE_UNITY!
  RANDOM_X = 20
  #Maximum random Y value. Remember: It will be multiplied by DISTANCE_UNITY!
  RANDOM_Y = 5
end
 
class Spriteset_Battle
  def enemy_sprites
    return @enemy_sprites
  end
  def viewport1
    return @viewport1
  end
end
 
class Scene_Battle
  def add_battler(enemy)
    @spriteset.enemy_sprites.push(Sprite_Battler.new(@spriteset.viewport1, enemy))
  end
end
 
class Game_Battler
 
  include OROCHII_MONSTERS
  if not include?(TAGNOTE)
    include TAGNOTE
  end
 
  #--------------------------------------------------------------------------
  # alias listings
  #--------------------------------------------------------------------------
  alias zorochii_make_obj_damage_value make_obj_damage_value
  alias zorochii_make_attack_damage_value make_attack_damage_value
 
  #--------------------------------------------------------------------------
  # * Calculation of Damage Caused by Skills or Items
  #--------------------------------------------------------------------------
  def make_obj_damage_value(user, obj)
    zorochii_make_obj_damage_value(user, obj)
    if self.is_a?(Game_Enemy)
      make_transformation(0,obj.id)
      item_note=$data_enemies[self.enemy_id].note
      if has_tag?(item_note,"divide")
        if obj.element_set.include?(OROCHII_MONSTERS::DIVIDE_ELEMENT)
          divide_monster
        end          
      end      
    end
  end
 
  #--------------------------------------------------------------------------
  # * Calculation of Damage From Normal Attack
  #--------------------------------------------------------------------------
  def make_attack_damage_value(attacker)
    zorochii_make_attack_damage_value(attacker)
    if self.is_a?(Game_Enemy)
      make_transformation(1)
      item_note=$data_enemies[self.enemy_id].note
      if has_tag?(item_note,"reverse_divide")
        if has_tag?(item_note,"divide")
          if get_additional_tag(item_note,"divide",2) != nil
            if attacker.element_set.include?(get_additional_tag(item_note,"divide",1))
              divide_monster if self.hp > @hp_damage or has_tag?(item_note,"ultradivide")
            end        
          else
            if attacker.element_set.include?(OROCHII_MONSTERS::DIVIDE_ELEMENT)
              divide_monster if self.hp > @hp_damage or has_tag?(item_note,"ultradivide")
            end        
          end
        end
      else        
        if has_tag?(item_note,"divide")
          if get_additional_tag(item_note,"divide",2) != nil
            if not attacker.element_set.include?(get_additional_tag(item_note,"divide",1))
              divide_monster if self.hp > @hp_damage or has_tag?(item_note,"ultradivide")
            end        
          else
            if not attacker.element_set.include?(OROCHII_MONSTERS::DIVIDE_ELEMENT)
              divide_monster if self.hp > @hp_damage or has_tag?(item_note,"ultradivide")
            end        
          end
        end
      end
    end
  end
  def make_transformation(type,id=0)
    if type == 0
      item_note=$data_skills[id].note
    elsif type == 1
      item_note=$data_enemies[self.enemy_id].note
    end
    enemy = $game_troop.members[self.index]
    if enemy != nil and has_tag?(item_note,"transform")
      enemy.transform(get_tag(item_note,"transform").to_i)
      $game_troop.make_unique_names
    end
  end
  def divide_monster
    if $game_troop.members.size < OROCHII_MONSTERS::MAX_ENEMIES
      hp_perc = 100*self.hp / self.maxhp
      if hp_perc >= OROCHII_MONSTERS::MIN_HP
        enemy = Game_Enemy.new($game_troop.members.size, self.enemy_id)
        item_note=$data_enemies[self.enemy_id].note
        if has_tag_value?(item_note,"divide","hp") and not has_tag?(item_note,"ultradivide")
          enemy.hp=self.hp - @hp_damage
        elsif has_tag_value?(item_note,"divide","hp") and has_tag?(item_note,"ultradivide")
          if self.hp > @hp_damage
            enemy.hp=self.hp - @hp_damage
          else
            enemy.hp=1
          end
        end
        enemy.hidden = self.hidden
        enemy.immortal = self.immortal
        dunit = OROCHII_MONSTERS::DISTANCE_UNITY
        ran_x = OROCHII_MONSTERS::RANDOM_X
        ran_y = OROCHII_MONSTERS::RANDOM_Y
        enemy.screen_x = self.screen_x + (rand(ran_x)*dunit) - (rand(ran_x)*dunit)
        if enemy.screen_x < 0
          enemy.screen_x +=ran_x*dunit
        elsif enemy.screen_x > 544
          enemy.screen_x -=ran_x*dunit
        end
        enemy.screen_y = self.screen_y + (rand(ran_y)*dunit) - (rand(ran_y)*dunit)
        if enemy.screen_y < 0
          enemy.screen_x +=ran_y*dunit
        elsif enemy.screen_y > 416
          enemy.screen_x -=ran_y*dunit
        end
        enemy.screen_z
        $game_troop.members.push(enemy)
        $game_troop.make_unique_names
        $scene.add_battler(enemy)
        $game_troop.update
      end
    end
  end
end