2 mobs.create_interaction_functions = function(def,mob_register)
3 --the sword wear mechanic
4 mob_register.add_sword_wear = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
5 if puncher:is_player() then
6 local itemstack = puncher:get_wielded_item()
7 local wear = itemstack:get_definition().mob_hit_wear
9 itemstack:add_wear(wear)
10 if itemstack:get_name() == "" then
11 minetest.sound_play("tool_break",{to_player = puncher:get_player_name(),gain=0.4})
13 puncher:set_wielded_item(itemstack)
18 --critical effect particles
19 mob_register.do_critical_particles = function(pos)
20 minetest.add_particlespawner({
25 minvel = vector.new(-2,-2,-2),
26 maxvel = vector.new(2,8,2),
27 minacc = {x=0, y=4, z=0},
28 maxacc = {x=0, y=12, z=0},
33 collisiondetection = false,
35 texture = "critical.png",
39 mob_register.collision_detection = function(self)
40 local pos = self.object:get_pos()
41 --do collision detection from the base of the mob
43 local collisionbox = self.object:get_properties().collisionbox
45 pos.y = pos.y + collisionbox[2]
47 local collision_boundary = collisionbox[4]
49 local radius = collision_boundary
51 if collisionbox[5] > collision_boundary then
52 radius = collisionbox[5]
55 for _,object in ipairs(minetest.get_objects_inside_radius(pos, radius*1.25)) do
56 if object ~= self.object and (object:is_player() or object:get_luaentity().mob == true) and
57 --don't collide with rider, rider don't collide with thing
58 (not object:get_attach() or (object:get_attach() and object:get_attach() ~= self.object)) and
59 (not self.object:get_attach() or (self.object:get_attach() and self.object:get_attach() ~= object)) then
61 local pos2 = object:get_pos()
63 local object_collisionbox = object:get_properties().collisionbox
65 pos2.y = pos2.y + object_collisionbox[2]
67 local object_collision_boundary = object_collisionbox[4]
70 --this is checking the difference of the object collided with's possision
71 --if positive top of other object is inside (y axis) of current object
72 local y_base_diff = (pos2.y + object_collisionbox[5]) - pos.y
74 local y_top_diff = (pos.y + collisionbox[5]) - pos2.y
77 local distance = vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
79 if distance <= collision_boundary + object_collision_boundary and y_base_diff >= 0 and y_top_diff >= 0 then
81 local dir = vector.direction(pos,pos2)
84 --eliminate mob being stuck in corners
85 if dir.x == 0 and dir.z == 0 then
86 dir = vector.new(math.random(-1,1)*math.random(),0,math.random(-1,1)*math.random())
89 local velocity = vector.multiply(dir,1.1)
91 local vel1 = vector.multiply(velocity, -1)
93 self.object:add_velocity(vel1)
95 if object:is_player() then
96 object:add_player_velocity(vel2)
98 object:add_velocity(vel2)
104 if def.takes_fall_damage == nil or def.takes_fall_damage == true then
105 mob_register.fall_damage = function(self)
106 local vel = self.object:get_velocity()
107 if vel and self.oldvel then
108 if self.oldvel.y < -7 and vel.y == 0 then
109 local damage = math.abs(self.oldvel.y + 7)
110 damage = math.floor(damage/1.5)
111 self.object:punch(self.object, 2,
113 full_punch_interval=1.5,
114 damage_groups = {damage=damage},
122 --this controls what happens when the mob gets punched
123 mob_register.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
125 local vel = self.object:get_velocity()
126 local hurt = tool_capabilities.damage_groups.damage
132 local critical = false
135 local pos = self.object:get_pos()
136 if puncher:is_player() then
137 local puncher_vel = puncher:get_player_velocity().y
138 if puncher_vel < 0 then
146 if (self.punched_timer <= 0 and hp > 1) and not self.dead then
147 self.object:set_texture_mod("^[colorize:"..self.damage_color..":130")
148 self.hurt_color_timer = 0.25
149 if puncher ~= self.object then
150 self.punched_timer = 0.25
151 if self.attacked_hostile then
153 self.hostile_timer = 20
154 if self.group_attack == true then
155 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
157 if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
158 object:get_luaentity().hostile = true
159 object:get_luaentity().hostile_timer = 20
165 self.scared_timer = 10
167 if self.custom_on_punch then
168 self.custom_on_punch(self)
173 if critical == true then
174 self.do_critical_particles(pos)
175 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
177 minetest.sound_play(self.hurt_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(100,140)/100})
181 dir = vector.multiply(dir,10)
189 self.object:add_velocity(dir)
190 self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
191 elseif (self.punched_timer <= 0 and self.death_animation_timer == 0) then
192 self.object:set_texture_mod("^[colorize:"..self.damage_color..":130")
193 self.hurt_color_timer = 0.25
194 if puncher ~= self.object then
195 self.punched_timer = 0.25
196 if self.attacked_hostile then
198 self.hostile_timer = 20
199 if self.group_attack == true then
200 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
201 if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
202 object:get_luaentity().hostile = true
203 object:get_luaentity().hostile_timer = 20
208 if self.custom_on_punch then
209 self.custom_on_punch(self)
212 self.death_animation_timer = 1
216 if critical == true then
217 self.do_critical_particles(pos)
218 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
220 minetest.sound_play(self.die_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(80,100)/100})
221 self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
225 --this is what happens when a mob dies
226 mob_register.on_death = function(self, killer)
227 local pos = self.object:get_pos()
229 minetest.throw_experience(pos,math.ceil(def.hp/5)+math.random(0,1))
231 --pos.y = pos.y + 0.4
232 minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
233 minetest.add_particlespawner({
238 minvel = vector.new(-5,-5,-5),
239 maxvel = vector.new(5,5,5),
240 minacc = {x=0, y=0, z=0},
241 maxacc = {x=0, y=0, z=0},
246 collisiondetection = false,
248 texture = "smoke.png",
251 --only throw items if registered
252 if self.item_drop then
254 if type(self.item_drop) == "string" then
255 item = self.item_drop
256 elseif type(self.item_drop) == "table" then
257 item = self.item_drop[math.random(1,table.getn(self.item_drop))]
259 --detect if multiple items are going to be added
260 if self.item_max then
261 local data_item_amount = math.random(self.item_minimum, self.item_max)
262 for i = 1 ,data_item_amount do
263 minetest.throw_item(vector.new(pos.x,pos.y+0.1,pos.z),item)
266 minetest.throw_item(vector.new(pos.x,pos.y+0.1,pos.z),item)
270 global_mob_amount = global_mob_amount - 1
271 --print("Mobs Died. Current Mobs: "..global_mob_amount)
273 if self.custom_on_death then
274 self.custom_on_death(self)
280 --the pig will look for and at players
281 mob_register.look_around = function(self,dtime)
282 local pos = self.object:get_pos()
284 if self.die_in_light and self.die_in_light_level and self.die_in_light == true then
285 local light_level = minetest.get_node_light(pos)
287 if (self.die_in_light == true and light_level > self.die_in_light_level) then
288 self.object:punch(self.object, 2,
290 full_punch_interval=1.5,
291 damage_groups = {damage=2},
299 self.following = false
300 local player_found = false
302 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
303 if object:is_player() and player_found == false and object:get_hp() > 0 then
304 --look at player's camera
305 local pos2 = object:get_pos()
306 pos2.y = pos2.y + 1.625
310 if self.head_bone then
311 self.move_head(self,pos2,dtime)
314 --print(self.hostile)
315 if self.hostile == true then
316 local distance = vector.distance(pos,pos2)
317 self.following_pos = vector.new(pos2.x,pos2.y-1.625,pos2.z)
320 if self.attack_type == "punch" then
321 if distance < 2.5 and self.punch_timer <= 0 and object:get_hp() > 0 then
322 local meta = object:get_meta()
323 local player_punch_timer = meta:get_float("player_punch_timer")
324 if player_punch_timer <= 0 then
325 local line_of_sight = minetest.line_of_sight(pos, pos2)
326 if line_of_sight == true then
327 self.punch_timer = 0.25
328 object:punch(self.object, 2,
330 full_punch_interval=1.5,
331 damage_groups = {damage=self.attack_damage},
332 },vector.direction(pos,pos2))
336 elseif self.attack_type == "explode" then
337 --mob will not explode if it cannot see you
338 if distance < self.explosion_radius and minetest.line_of_sight(vector.new(pos.x,pos.y+self.object:get_properties().collisionbox[5],pos.z), pos2) then
340 if not self.tnt_timer then
341 minetest.sound_play("tnt_ignite", {object = self.object, gain = 1.0,})
342 self.tnt_timer = self.explosion_time
343 self.tnt_tick_timer = 0.2
344 self.tnt_mod_state = 1
345 self.object:set_texture_mod("^[colorize:white:130")
348 elseif self.attack_type == "projectile" then
349 if not self.projectile_timer then
350 self.projectile_timer = self.projectile_timer_cooldown
352 if self.projectile_timer <= 0 then
353 self.projectile_timer = self.projectile_timer_cooldown
355 local obj = minetest.add_entity(vector.new(pos.x,pos.y+self.object:get_properties().collisionbox[5],pos.z), self.projectile_type)
357 local dir = vector.multiply(vector.direction(pos,vector.new(pos2.x,pos2.y-3,pos2.z)), 50)
358 obj:set_velocity(dir)
359 obj:get_luaentity().timer = 2
360 obj:get_luaentity().owner = self.object
365 if self.path_data and table.getn(self.path_data) > 0 then
366 self.direction = vector.direction(vector.new(pos.x,0,pos.z), vector.new(self.path_data[1].x,0,self.path_data[1].z))
369 self.direction = vector.direction(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
371 self.speed = self.max_speed
372 self.following = true
373 elseif self.scared == true then
374 self.speed = self.max_speed
375 self.direction = vector.direction(vector.new(pos2.x,0,pos2.z),vector.new(pos.x,0,pos.z))
376 self.scared_timer = 10
378 --only look at one player
382 --stare straight if not found
383 if player_found == false then
384 if self.move_head then
385 self.move_head(self,nil,dtime)
387 if self.following_pos then
388 self.following_pos = nil
390 if self.manage_hostile_timer then
391 self.manage_hostile_timer(self,dtime)