
# -> If NoEdit is present then return
#execute if entity @s[tag=OverlyRealistic.Animal.CantEdit] run \
    return fail

# -> On ground prop placement RNG, place a prop
execute unless score #or.has_used_spawn_egg OverlyRealistic.General.ArbitraryData matches 1 \
    if predicate overly_realistic:animal/animal_place_prop_rng \
    summon marker run \
        function overly_realistic:general/props/placement/prop_place_from_animal_spawn

# -> On force-despawn RNG, remove entity
execute unless score #or.has_used_spawn_egg OverlyRealistic.General.ArbitraryData matches 1 \
    if predicate overly_realistic:animal/animal_force_despawn_on_creation_rng run \
        return run \
            function overly_realistic:animals/force_remove

# -> Set tempt range
attribute @s tempt_range modifier add overly_realistic:animal_no_tempt_range -100.0 add_value

# -> Set camera distance
attribute @s camera_distance modifier add overly_realistic:animal_no_camera_distance -100.0 add_value

# -> If entity is a pig and aggro animals are enabled and hog RNG passes, return and operate
execute if entity @s[type=pig] \
    if score #or.setting.should_aggro_animals_spawn OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    unless predicate overly_realistic:animal/animal_is_baby \
    if predicate overly_realistic:animal/hog_spawn_rng run \
        return run \
            function overly_realistic:animals/hogs/pig_passed_as_hog

# -> Get randomized array entry
execute store result storage overly_realistic:temp_values generic_value_0 int 1 run \
    random value 0..19

# -> Apply genetics
function overly_realistic:animals/genetics/get_animal_genetics_entry with storage overly_realistic:temp_values

# -> Tag entity
tag @s add OverlyRealistic.CommonAnimalChecked
tag @s add OverlyRealistic.Animal.IsAnimal

# -> Apply vars
execute store result score @s OverlyRealistic.Animal.Stamina run \
    random value 100..1500

# -> If entity is a polar bear then add buffs
execute if entity @s[type=polar_bear] run \
    return run \
        function overly_realistic:animals/operations/polar_bear_buff

# -> Apply modifiers
execute if score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    attribute @s max_health modifier add overly_realistic:animal_default_max_health -0.25 add_multiplied_total

# -> Apply modifiers for 'nerfed aggro animals'
execute if score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    attribute @s max_health modifier add overly_realistic:animal_default_max_health -0.15 add_multiplied_total

# -> Maximize health
effect give @s regeneration 2 255 true
effect give @s instant_health 1 255 true
data modify entity @s Health set value 999

# -> Set chicken nerf
execute if entity @s[type=chicken] run \
    function overly_realistic:animals/operations/chicken_nerf

# -> If aggro animals are disabled then return
execute if score #or.setting.should_aggro_animals_spawn OverlyRealistic.Settings.ArbitrarySetting matches 0 run \
    return run \
        function overly_realistic:animals/operations/add_marker_to_passive_animal

# -> Aggressiveness RNG
execute unless predicate overly_realistic:animal/common_animal_aggressive_rng run \
    return run \
        function overly_realistic:animals/operations/add_marker_to_passive_animal

# -> If the animal is a chicken, on RNG, prevent aggressiveness
execute if entity @s[type=chicken] \
    if predicate overly_realistic:animal/chicken_pacify_rng run \
        return run \
            function overly_realistic:animals/operations/add_marker_to_passive_animal

# -> If animal is a baby then return
execute if predicate overly_realistic:animal/animal_is_baby run \
    return run \
        function overly_realistic:animals/animal_is_baby_tag

# -> Nerfed aggro animals RNG
execute if score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    unless predicate overly_realistic:animal/nerfed_aggro_animal_spawn_rng run \
        return run \
            function overly_realistic:animals/operations/add_marker_to_passive_animal

# -> Tag entity
tag @s add OverlyRealistic.CommonAnimalAggressive

# -> If entity is a wolf then set AngerTime
data modify entity @s[type=wolf] AngerTime set value 2147483647

# -> Apply modifiers
execute unless score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    attribute @s max_health modifier add overly_realistic:animal_aggressive_max_health 8 add_value
execute unless score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 run \
    attribute @s knockback_resistance modifier add overly_realistic:animal_aggressive_knockback_resistance 0.05 add_value
attribute @s movement_speed modifier add overly_realistic:animal_aggressive_movement_speed 0.01 add_value

# -> Create targeter entity
summon piglin_brute ~ ~ ~ {equipment:{mainhand:{id:"poisonous_potato",count:1,components:{item_model:"minecraft:air"}}},drop_chances:{mainhand:0.000},Silent:1b,Invulnerable:1b,CustomNameVisible:0b,DeathLootTable:"minecraft:empty",Team:"overly_realistic.targeters",PersistenceRequired:1b,IsImmuneToZombification:1b,Tags:["OverlyRealistic.TempEntity","OverlyRealistic.TargeterEntity","OverlyRealistic.Animal.AnimalHolderEntity"],CustomName:"Wild Animal",active_effects:[{id:"minecraft:invisibility",amplifier:0,duration:-1,show_particles:0b}],attributes:[{id:"minecraft:scale",base:0.0625},{base:0.0,id:"attack_damage"}]}

# -> Targeter rider
ride @n[type=piglin_brute,tag=OverlyRealistic.TempEntity,dx=0,dy=0,dz=0] mount @s

# -> Set modularized attack damage
execute on passengers run \
    item replace entity @s weapon.mainhand with poisonous_potato\
        [\
            custom_data=\
                {\
                    or_edited_item: 1b,\
                    or_no_edit: 1b,\
                    or_is_animal_attack_holder: 1b\
                },\
            !consumable,\
            !food,\
            item_model="minecraft:air",\
            item_name=\
                {\
                    text:" "\
                },\
            enchantment_glint_override=false,\
            enchantments=\
                {\
                    "overly_realistic:or_internal_animal_attack_holders/or_internal_attack_specialized_0":1,\
                    "overly_realistic:or_internal_animal_follow_range_holders/or_internal_follow_range_specialized_0":1\
                }\
        ]

# -> Set predator gene set
execute on passengers run \
    function overly_realistic:animals/genetics/get_animal_genetics_entry_predator with storage overly_realistic:temp_values

# -> Untag entity
execute on passengers run \
    tag @s remove OverlyRealistic.TempEntity

# -> Set chicken nerf
execute if entity @s[type=chicken] \
    on passengers \
    if entity @s[type=piglin_brute] run \
        attribute @s attack_damage modifier add overly_realistic:chicken_nerf_attack_damage -0.75 add_multiplied_total

# -> Reduce animal damage
execute unless score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    unless entity @s[type=chicken] \
    on passengers \
    if entity @s[type=piglin_brute] run \
        attribute @s attack_damage modifier add overly_realistic:nerfed_aggro_animal_attack_damage -0.38 add_multiplied_total

# -> If 'nerfed aggro animals' is on then nerf attack damage
execute if score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    on passengers \
    if entity @s[type=piglin_brute] run \
        attribute @s attack_damage modifier add overly_realistic:nerfed_aggro_animal_attack_damage -0.68 add_multiplied_total

# -> If 'nerfed aggro animals' is on then nerf follow range
execute if score #or.setting.nerfed_aggro_animals OverlyRealistic.Settings.ArbitrarySetting matches 1 \
    on passengers run \
        attribute @s[type=piglin_brute] follow_range modifier add overly_realistic:nerfed_aggro_animal_follow_range -0.5 add_multiplied_total

# -> Get entity scale
execute store result score #or.equation_part_1 OverlyRealistic.General.ArbitraryData run \
    attribute @s scale get 100

# -> Tribal fear RNG
execute if predicate overly_realistic:animal/animal_fear_smart_rng_big_animal run \
    tag @s add OverlyRealistic.Animal.TribalFearAggroAnimal

# --> Smart fear RNG

# -> Small scaled animal
execute if score #or.equation_part_1 OverlyRealistic.General.ArbitraryData matches ..98 run \
    return run \
        execute if predicate overly_realistic:animal/animal_fear_smart_rng_small_animal run \
            tag @s add OverlyRealistic.Animal.SmartFearAggroAnimal

# -> Normal & big scaled animal
execute if predicate overly_realistic:animal/animal_fear_smart_rng_big_animal run \
    tag @s add OverlyRealistic.Animal.SmartFearAggroAnimal
