diff --git a/api.lua b/api.lua index ff22ff9..2c0294f 100644 --- a/api.lua +++ b/api.lua @@ -18,7 +18,7 @@ end -- Global table mobs = { - mod = "redo", version = "20250201", + mod = "redo", version = "20250204", spawning_mobs = {}, translate = S, node_snow = has(minetest.registered_aliases["mapgen_snow"]) or has("mcl_core:snow") or has("default:snow") or "air", @@ -1382,6 +1382,35 @@ function mob_class:replace(pos) end end +-- look directly around mob to see if it can pickup any dropped items + +function mob_class:check_item_pickup(pos) + + if not self.on_pick_up or not self.pick_up or #self.pick_up == 0 then return end + + for _,o in pairs(minetest.get_objects_inside_radius(pos, 2)) do + + local l = o:get_luaentity() + + if l and l.name == "__builtin:item" then + + for k,v in pairs(self.pick_up) do + + if self.on_pick_up and l.itemstring:find(v) then + + local r = self.on_pick_up(self, l) + + if r and r.is_empty and not r:is_empty() then + l.itemstring = r:to_string() + elseif r and r.is_empty and r:is_empty() then + o:remove() + end + end + end + end + end +end + -- check if daytime and also if mob is docile during daylight hours function mob_class:day_docile() @@ -3169,6 +3198,9 @@ function mob_class:on_step(dtime, moveresult) -- node replace check (cow eats grass etc.) self:replace(pos) + + -- dropped item pickup check + self:check_item_pickup(pos) end -- knockback timer @@ -3328,6 +3360,8 @@ function mobs:register_mob(name, def) replace_what = def.replace_what, replace_with = def.replace_with, replace_offset = def.replace_offset, + pick_up = def.pick_up, + on_pick_up = def.on_pick_up, reach = def.reach, texture_list = def.textures, texture_mods = def.texture_mods or "", diff --git a/api.txt b/api.txt index 82cda95..8b92a41 100644 --- a/api.txt +++ b/api.txt @@ -310,6 +310,25 @@ eating. properties. (DEPRECATED, use on_replace to make changes). +Pickup Items +------------ + + 'pick_up' table of itemstrings the mob will pick up. + 'on_pick_up' function that will be called on item pickup - arguments are + (self, itemstring) and can return nil or a a modified itemstack e.g. + +on_pick_up = function(self, itemstring) + + local istack = ItemStack(entity.itemstring) + + print("-- took", istack:get_name()) + + istack:take_item(1) + + return istack +end, + + Custom Definition Functions --------------------------- diff --git a/readme.MD b/readme.MD index 2cbf867..dbbf636 100644 --- a/readme.MD +++ b/readme.MD @@ -31,6 +31,8 @@ https://forum.minetest.net/viewtopic.php?f=11&t=9917 * Added {eatable} group to food items and HP in description * Fixed timer bug when attacking * Fixed fall damage check when riding mob +* Calculate damage before do_punch function called +* Add function to check for dropped items and for mob to do something with them ### Version 1.61