
if minetest.get_modpath("mobs") and not mobs.mod and mobs.mod ~= "redo" then
   minetest.log("error", "[mobs_crocs] mobs redo API not found!")
   return
end

-- local variables
local l_skins = {
   {"mobs_water_crocodile_1.png"},
   {"mobs_water_crocodile_2.png"}
}

local l_anims = {
   speed_normal = 24,   speed_run = 24,
   stand_start = 0,   stand_end = 80,
   walk_start = 85,   walk_end = 110,
   run_start = 85,      run_end = 110,
   punch_start = 115,   punch_end = 145
}

local l_model = "mobs_water_crocodile.b3d"
local l_sounds = {random = "croco"}
local l_egg_texture = "default_grass.png"
local l_spawn_chance = 60000

-- load settings
local ENABLE_WALKERS = minetest.settings:get_bool("mobs_crocs.enable_walkers", true)
local ENABLE_FLOATERS = minetest.settings:get_bool("mobs_crocs.enable_floaters", true)
local ENABLE_SWIMMERS = minetest.settings:get_bool("mobs_crocs.enable_swimmers", true)

if not ENABLE_WALKERS then
   l_spawn_chance = l_spawn_chance - 20000
end
if not ENABLE_FLOATERS then
   l_spawn_chance = l_spawn_chance - 20000
end
if not ENABLE_SWIMMERS then
   l_spawn_chance = l_spawn_chance - 20000
end

-- no float
if ENABLE_WALKERS then

   mobs:register_mob("mobs_crocs:crocodile", {
      type = "monster",
      attack_type = "dogfight",
      damage = 8,
      damage_max = 12,
      damage_chance = 80,
      reach = 3,
      hp_min = 20,
      hp_max = 25,
      armor = 50,
      collisionbox = {-0.85, 0, -0.85, 0.85, .5, 0.85},
      drawtype = "front",
      visual = "mesh",
      mesh = l_model,
      textures = l_skins,
      visual_size = {x = 4, y = 4},
      sounds = l_sounds,
      fly = false,
      floats = 0,
      stepheight = 1,
      view_range = 10,
      water_damage = 0,
      lava_damage = 10,
      light_damage = 0,
      animation = l_anims,
      drops = {
         {name = "mobs:meat_raw", chance = 1, min = 1, max = 3},
         {name = "mobs:leather", chance = 1, min = 0, max = 2},
      },
   })

   mobs:spawn({
      name = "mobs_crocs:crocodile",
      nodes = {
         "default:dirt_with_grass", "default:dirt",
         "default:jungle_grass", "default:sand"
      },
      neighbors = {
         "default:water_flowing", "default:water_source",
         "default:papyrus", "dryplants:juncus", "dryplants:reedmace"
      },
      interval = 30,
      chance = l_spawn_chance,
      min_height = 0,
      max_height = 10,
   })

   mobs:register_egg("mobs_crocs:crocodile", "Crocodile", l_egg_texture, 1)
end

-- float
if ENABLE_FLOATERS then

   mobs:register_mob("mobs_crocs:crocodile_float", {
      type = "monster",
      attack_type = "dogfight",
      damage = 8,
      damage_max = 12,
      damage_chance = 80,
      reach = 2,
      hp_min = 20,
      hp_max = 25,
      armor = 50,
      collisionbox = {-0.638, -0.23, -0.638, 0.638, .5, 0.638},
      drawtype = "front",
      visual = "mesh",
      mesh = l_model,
      textures = l_skins,
      visual_size = {x = 3, y = 3},
      sounds = l_sounds,
      fly = false,
      stepheight = 1,
      view_range = 10,
      water_damage = 0,
      lava_damage = 10,
      light_damage = 0,
      animation = l_anims,
      drops = {
         {name = "mobs:meat_raw", chance = 1, min = 1, max = 3},
         {name = "mobs:leather", chance = 1, min = 0, max = 2},
      },
      custom_attack = function(self, p)
         local pos = self.object:get_pos()
         self.alligator_king = self.attack
         local new_pos = {x=pos.x, y=pos.y-.5, z=pos.z}
         local node = minetest.get_node(new_pos).name
         if minetest.get_item_group(node, 'water') > 0 then
            self.attack:setpos(new_pos)
            self.attack:set_physics_override({gravity=3, speed=.2})
            self.object:setpos(new_pos)
         end
         return true
      end,
      on_die = function(self, pos)
         if self.alligator_king then
            armor:set_player_armor(self.alligator_king)
         end
      end
   })

   mobs:spawn({
      name = "mobs_crocs:crocodile_float",
      nodes = {"default:water_flowing","default:water_source"},
      neighbors = {
         "default:dirt_with_grass", "default:jungle_grass", "default:sand",
         "default:dirt", "default:papyrus", "group:seaplants",
         "dryplants:juncus", "dryplants:reedmace"
      },
      interval = 30,
      chance = l_spawn_chance,
      min_height = -3,
      max_height = 10,
   })

   mobs:register_egg("mobs_crocs:crocodile_float", "Crocodile (floater)",
      l_egg_texture, 1)
end

-- swim
if ENABLE_SWIMMERS then

   mobs:register_mob("mobs_crocs:crocodile_swim", {
      type = "monster",
      attack_type = "dogfight",
      damage = 8,
      damage_max = 12,
      damage_chance = 80,
      reach = 2,
      hp_min = 20,
      hp_max = 25,
      armor = 50,
      collisionbox = {-0.638, -0.23, -0.638, 0.638, .5, 0.638},
      drawtype = "front",
      visual = "mesh",
      mesh = l_model,
      textures = l_skins,
      visual_size = {x = 3, y = 3},
      sounds = l_sounds,
      fly = true,
      fly_in = "group:water",
      fall_speed = -1,
      floats = 0,
      view_range = 10,
      water_damage = 0,
      lava_damage = 10,
      light_damage = 0,
      animation = l_anims,
      drops = {
         {name = "mobs:meat_raw", chance = 1, min = 1, max = 3},
         {name = "mobs:leather", chance = 1, min = 0, max = 2},
      },
      custom_attack = function(self, p)
         local pos = self.object:get_pos()
         local new_pos = {x=pos.x, y=pos.y-.5, z=pos.z}
         local node = minetest.get_node(new_pos).name
         if minetest.get_item_group(node, 'water') > 0 then
            self.attack:setpos(new_pos)
            self.attack:set_physics_override({gravity=3, speed=.2})
            self.object:setpos(new_pos)
         end
         return true
      end,
      on_die = function(self, pos)
         if self.attack then
            armor:set_player_armor(self.attack)
         end
      end
   })

   mobs:spawn({
      name = "mobs_crocs:crocodile_swim",
      nodes = {"default:water_flowing","default:water_source"},
      neighbors = {"default:sand","default:dirt","group:seaplants"},
      interval = 30,
      chance = l_spawn_chance,
      min_height = -8,
      max_height = 10,
   })

   mobs:register_egg("mobs_crocs:crocodile_swim", "Crocodile (swimmer)",
      l_egg_texture, 1)
end
