]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/interaction.lua
0ffbd10c70eda5022a5a4270263d11834a3dd934
[Crafter.git] / mods / mob / api / interaction.lua
1 --
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
8                         if wear then
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})
12                                 end
13                                 puncher:set_wielded_item(itemstack)
14                         end
15                 end
16         end
17
18         --critical effect particles
19         mob_register.do_critical_particles = function(pos)
20                 minetest.add_particlespawner({
21                         amount = 40,
22                         time = 0.001,
23                         minpos = pos,
24                         maxpos = pos,
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},
29                         minexptime = 1.1,
30                         maxexptime = 1.5,
31                         minsize = 1,
32                         maxsize = 2,
33                         collisiondetection = false,
34                         vertical = false,
35                         texture = "critical.png",
36                 })
37         end
38         
39         mob_register.collision_detection = function(self)
40                 local pos = self.object:get_pos()
41                 --do collision detection from the base of the mob
42                 
43                 local collisionbox = self.object:get_properties().collisionbox
44
45                 pos.y = pos.y + collisionbox[2]
46                 
47                 local collision_boundary = collisionbox[4]
48
49                 local radius = collision_boundary
50
51                 if collisionbox[5] > collision_boundary then
52                         radius = collisionbox[5]
53                 end
54
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
60
61                                 local pos2 = object:get_pos()
62                                 
63                                 local object_collisionbox = object:get_properties().collisionbox
64
65                                 pos2.y = pos2.y + object_collisionbox[2]
66
67                                 local object_collision_boundary = object_collisionbox[4]
68
69
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
73
74                                 local y_top_diff = (pos.y + collisionbox[5]) - pos2.y
75
76
77                                 
78
79                                 local distance = vector.distance(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
80
81                                 if distance <= collision_boundary + object_collision_boundary and y_base_diff >= 0 and y_top_diff >= 0 then
82
83                                         local dir = vector.direction(pos,pos2)
84                                         dir.y = 0
85                                         
86                                         --eliminate mob being stuck in corners
87                                         if dir.x == 0 and dir.z == 0 then
88                                                 dir = vector.new(math.random(-1,1)*math.random(),0,math.random(-1,1)*math.random())
89                                         end
90                                         
91                                         local velocity = vector.multiply(dir,1.1)
92                                         
93                                         local vel1 = vector.multiply(velocity, -1)
94                                         local vel2 = velocity
95                                         self.object:add_velocity(vel1)
96                                         
97                                         if object:is_player() then
98                                                 object:add_player_velocity(vel2)
99                                         else
100                                                 object:add_velocity(vel2)
101                                         end
102                                 end
103                         end
104                 end
105         end
106         if def.takes_fall_damage == nil or def.takes_fall_damage == true then
107                 mob_register.fall_damage = function(self)
108                         local vel = self.object:get_velocity()
109                         if vel and self.oldvel then
110                                 if self.oldvel.y < -7 and vel.y == 0 then
111                                         local damage = math.abs(self.oldvel.y + 7)
112                                         damage = math.floor(damage/1.5)
113                                         self.object:punch(self.object, 2, 
114                                                 {
115                                                 full_punch_interval=1.5,
116                                                 damage_groups = {damage=damage},
117                                                 })
118                                 end
119                         end
120                         self.oldvel = vel
121                 end
122         end
123
124         --this controls what happens when the mob gets punched
125         mob_register.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
126                 local hp = self.hp
127                 local vel = self.object:get_velocity()
128                 local hurt = tool_capabilities.damage_groups.damage
129                 
130                 if not hurt then
131                         hurt = 1
132                 end
133                 
134                 local critical = false
135                 
136                 --criticals
137                 local pos = self.object:get_pos()
138                 if puncher:is_player() then
139                         local puncher_vel = puncher:get_player_velocity().y
140                         if puncher_vel < 0 then
141                                 hurt = hurt * 1.5
142                                 critical = true
143                         end
144                 end
145                 
146                 local hp = hp-hurt
147
148                 if (self.punched_timer <= 0 and hp > 1) and not self.dead then
149                         self.object:set_texture_mod("^[colorize:"..self.damage_color..":130")
150                         self.hurt_color_timer = 0.25
151                         if puncher ~= self.object then
152                                 self.punched_timer = 0.8
153                                 if self.attacked_hostile then
154                                         self.hostile = true
155                                         self.hostile_timer = 20
156                                         if self.group_attack == true then
157                                                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
158                                                         if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
159                                                                 object:get_luaentity().hostile = true
160                                                                 object:get_luaentity().hostile_timer = 20
161                                                         end
162                                                 end
163                                         end
164                                 end
165                         end
166                         
167                         --critical effect
168                         if critical == true then
169                                 self.do_critical_particles(pos)
170                                 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
171                         end
172                         minetest.sound_play(self.hurt_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(100,140)/100})
173                         
174                         self.hp = hp
175                         
176                         self.direction = vector.multiply(dir,-1)
177                         dir = vector.multiply(dir,10)
178                         if vel.y <= 0 then
179                                 dir.y = 4
180                         else
181                                 dir.y = 0
182                         end
183                         
184                         
185                         self.object:add_velocity(dir)
186                         self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
187                 elseif (self.punched_timer <= 0 and self.death_animation_timer == 0) then
188                         self.object:set_texture_mod("^[colorize:"..self.damage_color..":130")
189                         self.hurt_color_timer = 0.25
190                         if puncher ~= self.object then
191                                 self.punched_timer = 0.8
192                                 if self.attacked_hostile then
193                                         self.hostile = true
194                                         self.hostile_timer = 20
195                                         if self.group_attack == true then
196                                                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
197                                                         if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
198                                                                 object:get_luaentity().hostile = true
199                                                                 object:get_luaentity().hostile_timer = 20
200                                                         end
201                                                 end
202                                         end
203                                 end
204                         end
205                         self.death_animation_timer = 1
206                         self.dead = true
207                         
208                         --critical effect
209                         if critical == true then
210                                 self.do_critical_particles(pos)
211                                 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
212                         end
213                         minetest.sound_play(self.die_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(80,100)/100})
214                         self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
215                 end
216         end
217
218         --this is what happens when a mob dies
219         mob_register.on_death = function(self, killer)
220                 local pos = self.object:get_pos()
221                 --pos.y = pos.y + 0.4
222                 minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
223                 minetest.add_particlespawner({
224                         amount = 40,
225                         time = 0.001,
226                         minpos = pos,
227                         maxpos = pos,
228                         minvel = vector.new(-5,-5,-5),
229                         maxvel = vector.new(5,5,5),
230                         minacc = {x=0, y=0, z=0},
231                         maxacc = {x=0, y=0, z=0},
232                         minexptime = 1.1,
233                         maxexptime = 1.5,
234                         minsize = 1,
235                         maxsize = 2,
236                         collisiondetection = false,
237                         vertical = false,
238                         texture = "smoke.png",
239                 })
240                 
241                 --only throw items if registered
242                 if self.item_drop then
243                         --detect if multiple items are going to be added
244                         if self.item_max then
245                                 local data_item_amount = math.random(self.item_minimum, self.item_max)
246                                 for i = 1 ,data_item_amount do
247                                         minetest.throw_item(vector.new(pos.x,pos.y+0.1,pos.z),self.item_drop)
248                                 end
249                         else
250                                 minetest.throw_item(vector.new(pos.x,pos.y+0.1,pos.z),self.item_drop)
251                         end
252                 end
253                         
254                 global_mob_amount = global_mob_amount - 1
255                 print("Mobs Died. Current Mobs: "..global_mob_amount)
256                 
257                 if self.custom_on_death then
258                         self.custom_on_death(self)
259                 end
260
261                 self.object:remove()
262         end
263         
264         --the pig will look for and at players
265         mob_register.look_around = function(self,dtime)
266                 local pos = self.object:get_pos()
267                 
268                 if self.die_in_light and self.die_in_light_level and self.die_in_light == true then
269                         local light_level = minetest.get_node_light(pos)
270                         if light_level then
271                                 if (self.die_in_light == true and light_level > self.die_in_light_level) then
272                                         local damage = self.hp
273                                         self.object:punch(self.object, 2, 
274                                                 {
275                                                 full_punch_interval=1.5,
276                                                 damage_groups = {damage=damage},
277                                         })
278                                 end
279                         end
280                 end
281                 
282                 --STARE O_O
283                 --and follow!
284                 self.following = false
285                 local player_found = false
286
287                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
288                         if object:is_player() and player_found == false and object:get_hp() > 0 then
289                                 --look at player's camera
290                                 local pos2 = object:get_pos()
291                                 pos2.y = pos2.y + 1.625
292                                 
293                                 player_found = true
294                                 
295                                 if self.head_bone then
296                                         self.move_head(self,pos2,dtime)
297                                 end
298                                 
299                                 --print(self.hostile)
300                                 if self.hostile == true then
301                                         local distance = vector.distance(pos,pos2)
302                                         self.following_pos = vector.new(pos2.x,pos2.y-1.625,pos2.z)
303
304                                         --punch the player
305                                         if self.attack_type == "punch" then
306                                                 if distance < 2.5 and self.punch_timer <= 0 and object:get_hp() > 0 then
307                                                         local line_of_sight = minetest.line_of_sight(pos, pos2)
308                                                         if line_of_sight == true then
309                                                                 self.punch_timer = 1
310                                                                 object:punch(self.object, 2, 
311                                                                         {
312                                                                         full_punch_interval=1.5,
313                                                                         damage_groups = {fleshy=2},
314                                                                 },vector.direction(pos,pos2))
315                                                         end
316                                                 end
317                                         elseif self.attack_type == "explode" then
318                                                 --mob will not explode if it cannot see you
319                                                 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
320                                                         
321                                                         if not self.tnt_timer then
322                                                                 minetest.sound_play("tnt_ignite", {object = self.object, gain = 1.0,})
323                                                                 self.tnt_timer = self.explosion_time
324                                                                 self.tnt_tick_timer  = 0.2
325                                                                 self.tnt_mod_state = 1
326                                                                 self.object:set_texture_mod("^[colorize:white:130")
327                                                         end
328                                                 end
329                                         elseif self.attack_type == "projectile" then
330                                                 if not self.projectile_timer then
331                                                         self.projectile_timer = self.projectile_timer_cooldown
332                                                 end
333                                                 if self.projectile_timer <= 0 then
334                                                         self.projectile_timer = self.projectile_timer_cooldown
335                                                         
336                                                         local obj = minetest.add_entity(vector.new(pos.x,pos.y+self.object:get_properties().collisionbox[5],pos.z), self.projectile_type)
337                                                         if obj then
338                                                                 local dir = vector.multiply(vector.direction(pos,vector.new(pos2.x,pos2.y-3,pos2.z)), 50)
339                                                                 obj:set_velocity(dir)
340                                                                 obj:get_luaentity().timer = 2
341                                                                 obj:get_luaentity().owner = self.object
342                                                         end
343                                                 end
344                                         end
345                                         --smart
346                                         if self.path_data and table.getn(self.path_data) > 0 then
347                                                 self.direction = vector.direction(vector.new(pos.x,0,pos.z), vector.new(self.path_data[1].x,0,self.path_data[1].z))
348                                         --dumb
349                                         else
350                                                 self.direction = vector.direction(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
351                                         end
352                                         self.speed = self.max_speed
353                                         self.following = true
354                                 end
355                                 --only look at one player
356                                 break
357                         end
358                 end
359                 --stare straight if not found
360                 if player_found == false then
361                         if self.move_head then
362                                 self.move_head(self,nil,dtime)
363                         end
364                         if self.following_pos then
365                                 self.following_pos = nil
366                         end
367                         if self.manage_hostile_timer then
368                                 self.manage_hostile_timer(self,dtime)
369                         end
370                 end
371         end
372         
373         return(mob_register)
374 end