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