11base_mobs = {}
22function base_mobs :register_mob (name , def )
3+ if (def .type == " monster" or def .type == " defensive" ) and (not def .damage or def .damage <= 0 ) then
4+ minetest .log (" error" , " The mob " .. name .. " with type '" .. def .type .. " ' has no attack damage." )
5+ end
6+
37 minetest .register_entity (name , {
48 hp_max = def .hp_max ,
59 physical = true ,
10+ aggressive = false ,
611 collisionbox = def .collisionbox ,
712 collide_with_objects = def .collide_with_objects ,
813 visual = def .visual ,
@@ -22,7 +27,6 @@ function base_mobs:register_mob(name, def)
2227 drawtype = def .drawtype ,
2328 on_rightclick = def .on_rightclick ,
2429 type = def .type ,
25- attack_type = def .attack_type ,
2630 sounds = def .sounds ,
2731 animation = def .animation ,
2832 follow = def .follow or " " ,
@@ -53,7 +57,7 @@ function base_mobs:register_mob(name, def)
5357 if type == " walk" then
5458 vel = self .walk_velocity
5559 elseif type == " run" then
56- vel = self .run_velocity
60+ vel = self .run_velocity or self . walk_velocity
5761 end
5862 return vel
5963 end ,
@@ -181,7 +185,7 @@ function base_mobs:register_mob(name, def)
181185 end
182186
183187 local p , vec , dist = false , false , false
184- if self .type == " monster " and not self .to_player then
188+ if self .aggressive and not self .to_player then
185189 for _ ,player in ipairs (minetest .get_connected_players ()) do
186190 p = player :getpos ()
187191 vec = vector .subtract (p , my_pos )
@@ -208,7 +212,7 @@ function base_mobs:register_mob(name, def)
208212 end
209213 end
210214
211- if self .to_player and self .type ~= " monster " then
215+ if self .to_player and not self .aggressive then
212216 if self .to_player :get_wielded_item ():get_name () ~= self .follow then
213217 self .to_player = nil
214218 self :set_animation (" stand" )
@@ -237,16 +241,17 @@ function base_mobs:register_mob(name, def)
237241
238242 -- Target reached
239243 if dist <= 2.2 then
240- if self .type == " monster" then
241- self :set_animation (" punch" )
242- minetest .sound_play (" base_mobs_punch" , {object = self .object , gain = 1 })
243- self .to_player :punch (self .object , 1.0 , {
244- full_punch_interval = 1.0 ,
245- damage_groups = {fleshy = self .damage }
246- }, vec )
247- else
244+ if not self .aggressive then
248245 self :set_animation (" stand" )
246+ return
249247 end
248+
249+ self :set_animation (" punch" )
250+ minetest .sound_play (" base_mobs_punch" , {object = self .object , gain = 1 })
251+ self .to_player :punch (self .object , 1.0 , {
252+ full_punch_interval = 1.0 ,
253+ damage_groups = {fleshy = self .damage }
254+ }, vec )
250255 return
251256 end
252257 -- Else shoot.
@@ -260,7 +265,7 @@ function base_mobs:register_mob(name, def)
260265 end
261266 self .object :setyaw (yaw )
262267
263- if self .type == " monster " then
268+ if self .aggressive then
264269 self :set_animation (" run" )
265270 else
266271 self :set_animation (" walk" )
@@ -274,7 +279,7 @@ function base_mobs:register_mob(name, def)
274279 other_state = " stand"
275280 end
276281
277- local r = math.random (30 )
282+ local r = math.random (60 )
278283 if r == 10 or (r == 11 and other_state == " walk" ) then
279284 self :set_animation (other_state )
280285 elseif r <= 4 then
@@ -284,6 +289,9 @@ function base_mobs:register_mob(name, def)
284289 end ,
285290
286291 on_activate = function (self , staticdata , dtime_s )
292+ if self .type == " monster" then
293+ self .aggressive = true
294+ end
287295 self .object :set_armor_groups ({fleshy = self .armor })
288296 self .object :setacceleration ({x = 0 , y = - 10 , z = 0 })
289297 self :set_animation (" stand" )
@@ -308,34 +316,41 @@ function base_mobs:register_mob(name, def)
308316 get_staticdata = function (self )
309317 local tmp = {
310318 lifetimer = self .lifetimer ,
311- tamed = self .tamed ,
319+ tamed = self .tamed
312320 }
313321 return minetest .serialize (tmp )
314322 end ,
315323
316324 on_punch = function (self , hitter )
317325 local hp = self .object :get_hp ()
318- if hp >= 1 then
319- minetest .sound_play (" player_damage" , {object = self .object , gain = 0.25 })
320- minetest .sound_play (" hit" , {pos = hitter :getpos (), gain = 0.4 })
321- end
322- local y = self .object :getvelocity ().y
323- if y == 0 and self .state == " walk" then
324- self .object :setvelocity ({x = 0 , y = y + 4 , z = 0 })
325- self :set_velocity (self .walk_velocity )
326- end
327- if hp > 0 then
328- return
329- end
326+ local is_player = (hitter and hitter :is_player ())
330327
331- if hitter and hitter : is_player () and hitter :get_inventory () then
328+ if hp <= 0 and is_player and hitter :get_inventory () then
332329 minetest .sound_play (" player_death" , {object = self .object , gain = 0.4 })
333330 minetest .sound_play (" hit_death" , {pos = hitter :getpos (), gain = 0.4 })
334331 for _ ,drop in ipairs (self .drops ) do
335332 if math.random (drop .chance ) == 1 then
336333 hitter :get_inventory ():add_item (" main" , ItemStack (drop .name .. " " .. math.random (drop .min , drop .max )))
337334 end
338335 end
336+ return
337+ end
338+
339+ local vel = self .object :getvelocity ()
340+ if vel .y == 0 and (self .state == " stand" or self .state == " walk" ) then
341+ vel .y = 4
342+ self .object :setvelocity (vel )
343+ self :set_animation (" walk" )
344+ end
345+
346+ minetest .sound_play (" player_damage" , {object = self .object , gain = 0.25 })
347+ minetest .sound_play (" hit" , {pos = hitter :getpos (), gain = 0.4 })
348+
349+ if self .type == " defensive" then
350+ if is_player then
351+ self .to_player = hitter
352+ end
353+ self .aggressive = true
339354 end
340355 end ,
341356
0 commit comments