

-- Copyright (C) 2020 2026 PsycoJaker

-- This file is part of UwU Minetest Mod.

-- UwU Mod is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.

-- UwU Mod is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.

-- You should have received a copy of the GNU Affero General Public License
-- along with UwU Mod.  If not, see <https://www.gnu.org/licenses/>.


-- contrib by KingXKong

-- uwu/pet.lua

local S = core.get_translator(core.get_current_modname())
local mob_class = mcl_mobs.mob_class

local item_to_crystal = ""

local uwu_pet = {
	description = S("UwU Pet"),
	type = "animal",
	spawn_class = "passive",
	hp_min = 10,
	hp_max = 10,
	xp_min = 0,
	xp_max = 0,
	damage = 0,
	reach = 1,
	attack_type = "none",
	collisionbox = {-0.3,0,-0.3, 0.3,0.8,0.3},
	visual = "mesh",
	mesh = "uwu_pet.gltf",
	backface_culling = false,
	textures = {{
		"uwu_pet.png",
	}},
	-- brillo en la oscuridad
	glow = 10,
	light_source = 13,
	visual_size = {x=1, y=1},
	makes_footstep_sound = true,
	movement_speed = 3,
	follow = {
		"uwu:crystal"
	},
	sounds = {
		random = {name="fairy", gain=0.05, chance=120},
		damage = {name="uwu", gain=0.2},
		death = {name="uwu", gain=0.5},
		distance = 10,
	},
	animation = {
		stand_start = 0, stand_end = 1, stand_speed = 1,
		walk_start = 1.25, walk_end = 3.25, walk_speed = 2,
		die_start = 3.75, die_end = 6, die_speed = 1, die_loop = false,
	},
}

------------------------------------------------
-- IA simple
------------------------------------------------

uwu_pet.ai_functions = {
	mob_class.check_following,
	mob_class.check_pace,
}

------------------------------------------------
-- Partículas permanentes
------------------------------------------------

function uwu_pet:do_custom()
	local pos = self.object:get_pos()
	if not pos then return end
	core.add_particle({
		pos = vector.add(pos, {
			x = math.random(-2,2)/10,
			y = math.random(0,8)/10,
			z = math.random(-2,2)/10
		}),
		velocity = {x=0,y=0.5,z=0},
		acceleration = {x=0,y=0,z=0},
		expirationtime = 0.5,
		amount = 1,
		size = 1.5,
		texture = "uwu_particle.png",
		glow = 10
	})
end

------------------------------------------------
-- Dar cristal
------------------------------------------------

-- Retorna true con probabilidad N / max_n
local function prob_bool_proporcional(n, max_n)
	local max_n = max_n or 100
	if type(n) == "number" and n >= 1 and
	type(max_n) == "number" and max_n >= n then
		return math.random() < (n / max_n)
	end
	return false
end

function uwu_pet:on_rightclick(clicker)
	if not clicker or not clicker:is_player() then
		return
	end
	local special_drop_prob = 75
	local itemstack = clicker:get_wielded_item()
	local item_name = itemstack:get_name()
	local is_food, is_bad_food, energy = uwu.check_food(itemstack)
	if is_food then
		local pos = self.object:get_pos()
		-- crystal drop
		local drop_crystal = prob_bool_proporcional(energy, 25) -- bool
		if drop_crystal then
			core.add_item(pos, "uwu:crystal")
			-- special item drops
			if math.random(1, special_drop_prob) == 1 then
				core.add_item(pos, "mcl_core:emerald")
			end
			if math.random(1, special_drop_prob*(2)) == 1 then
				core.add_item(pos, "mcl_core:diamond")
			end
			if math.random(1, special_drop_prob*(3)) == 1 then
				core.add_item(pos, "mcl_totems:totem")
			end
			-- crystal drop sound
			core.sound_play("uwu_crystal", {
				pos = pos,
				gain = 0.5,
				max_hear_distance = 16,
				pitch = 0.9 + math.random() * 0.2
			})
		end
		-- eat sound
		core.sound_play("mcl_hunger_bite", {pos = pos, gain = 0.2})
		if not is_bad_food then
			uwu.particles(pos, energy)
		end
		-- eat food item
		if not core.is_creative_enabled(clicker:get_player_name()) then
			itemstack:take_item()
			clicker:set_wielded_item(itemstack)
		end
		return true
	end
end

------------------------------------------------
-- registrar mob
------------------------------------------------

mcl_mobs.register_mob("uwu:pet", uwu_pet)
-- spawn egg
mcl_mobs.register_egg("uwu:pet", S("UwU Pet"), "#e955cf", "#fff298", 0)
