local mod_name = minetest.get_current_modname()
local mod_path = minetest.get_modpath(mod_name)
local S = minetest.get_translator(mod_name)

local damage_groups = {
    pierce=1,
    blunt=5,
    projectile=1,
}

local csize = 0.3
minetest.register_entity("pmb_kelar:rock_ENTITY", {
    initial_properties = {
        visual = "mesh",
        mesh = "pmb_kelar_rock.b3d",
        textures = {"pmb_kelar_rock.png"},
        stepheight = 0,
        hp_max = 1,
        physical = false,
        pointable = false,
        collisionbox = {
            -csize, -csize, -csize, csize, csize, csize,
        },
    },
    on_step = function(self, dtime)
        if self._age > 10 then
            
            self.object:remove()
        end
        pmb_combat.projectile.on_step(self, dtime, {})
    end,
    _prj_can_collide = function(self, pointed_thing)
        if pointed_thing.type == "node" then
            local node = minetest.get_node(pointed_thing.under)
            local def = minetest.registered_nodes[node.name]
            if def and def.walkable then
                return true
            end
        else
            return true
        end
    end,
    _prj_on_collide = function(self, pointed_thing)
        if pointed_thing.type == "node" then
            self.object:set_velocity(vector.new(0,0,0))
        elseif (not self._prj_skip_objects) and pointed_thing.ref and pointed_thing.ref ~= self._prj_owner then
            local tar = pointed_thing.ref
            tar:punch(
                self._prj_owner or self.object,
                1,
                {
                    damage_groups = self._prj_damage_groups,
                    full_punch_interval = 1,
                },
                self._avg_vel
            )
            self.object:set_velocity(vector.new(0,0,0))
            minetest.sound_play(("pmb_kelar_rock_hit"), {
                gain = 1.0,
                pos = self._last_pos,
                object = self.object,
                max_hear_distance = 20,
                pitch = (math.random()*0.1) + 0.90
            })
            self._prj_skip_objects = true
        end
        self.object:remove()
        return true
    end,
    -- on_punch = function(self)
    --     self.object:remove()
    -- end,
    on_activate = function(self, staticdata, dtime_s)
        local data = minetest.deserialize(staticdata)
        if data then
            for key, val in pairs(data) do
                self[key] = val
            end
            if data._rotation then
                self.object:set_rotation(data._rotation)
            end
        end
        self._pmb_particle_id = minetest.add_particlespawner({
            amount = 100,
            time = 0,
            attached = self.object,
            node = {name = "pmb_stone:cobble_moss_1"},
            minsize = 0.2,
            maxsize = 3,
            minexptime = 0.1,
            maxexptime = 0.2,
        })
        self._pmb_sound_id = minetest.sound_play(("pmb_wind_rush_loop"), {
            gain = 0.1,
            object = self.object,
            max_hear_distance = 20,
            pitch = 2,
        })
    end,
    get_staticdata = function(self)
        local data = {}
        for i, key in pairs(self._pmb_staticdata_load_list) do
            if key and self[key] ~= nil then
                data[key] = self[key]
            end
        end
        data._age = self._age
        data.vel = self.object:get_velocity()
        return minetest.serialize(data)
    end,
    on_deactivate = function(self, removal)
        if self._pmb_sound_id then
            minetest.sound_fade(self._pmb_sound_id, 3, 0)
        end
    end,
    _pmb_staticdata_load_list = {
        "_age",
        "_rotation",
        "_prj_dir",
    },
    _age = 0,
    _prj_gravity = 9 * 1.6,
    _prj_damage_groups = table.copy(damage_groups),
    _prj_do_collision_checks = true,
})

