]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/interaction.lua
42c4296edecb3c3b92af09e237a7afbcbf37f922
[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                 pos.y = pos.y - self.object:get_properties().collisionbox[2]
43                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.collision_boundary)) do
44                         if object:is_player() or object:get_luaentity().mob == true then
45                                 local pos2 = object:get_pos()
46                                 
47                                 local dir = vector.direction(pos,pos2)
48                                 dir.y = 0
49                                 
50                                 --eliminate mob being stuck in corners
51                                 if dir.x == 0 and dir.z == 0 then
52                                         dir = vector.new(math.random(-1,1)*math.random(),0,math.random(-1,1)*math.random())
53                                 end
54                                 
55                                 local velocity = vector.multiply(dir,1.1)
56                                 
57                                 vel1 = vector.multiply(velocity, -1)
58                                 vel2 = velocity
59                                 self.object:add_velocity(vel1)
60                                 
61                                 if object:is_player() then
62                                         object:add_player_velocity(vel2)
63                                 else
64                                         object:add_velocity(vel2)
65                                 end
66                         end
67                 end
68         end
69         
70         mob_register.fall_damage = function(self)
71                 local vel = self.object:get_velocity()
72                 if vel and self.oldvel then
73                    if self.oldvel.y < -7 and vel.y == 0 then
74                           local damage = math.abs(self.oldvel.y + 7)
75                           damage = math.floor(damage/1.5)
76                           self.object:punch(self.object, 2, 
77                                  {
78                                   full_punch_interval=1.5,
79                                   damage_groups = {damage=damage},
80                                  })
81                    end
82                 end
83                 self.oldvel = vel
84         end
85
86         --this controls what happens when the mob gets punched
87         mob_register.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
88                 local hp = self.hp
89                 local vel = self.object:get_velocity()
90                 local hurt = tool_capabilities.damage_groups.damage
91                 
92                 if not hurt then
93                         hurt = 1
94                 end
95                 
96                 local critical = false
97                 
98                 --criticals
99                 local pos = self.object:get_pos()
100                 if puncher:is_player() then
101                         local puncher_vel = puncher:get_player_velocity().y
102                         if puncher_vel < 0 then
103                                 hurt = hurt * 1.5
104                                 critical = true
105                         end
106                 end
107                 
108                 local hp = hp-hurt
109                 
110                 if (self.punched_timer <= 0 and hp > 1) and not self.dead then
111                         if puncher ~= self.object and self.attacked_hostile then
112                                 self.hostile = true
113                                 if self.group_attack == true then
114                                         for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
115                                                 if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
116                                                         object:get_luaentity().hostile = true
117                                                         object:get_luaentity().hostile_timer = 20
118                                                 end
119                                         end
120                                 end
121                                 self.hostile_timer = 20
122                                 self.punched_timer = 0.8
123                         end
124                         
125                         --critical effect
126                         if critical == true then
127                                 self.do_critical_particles(pos)
128                                 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
129                         end
130                         minetest.sound_play(self.hurt_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(100,140)/100})
131                         
132                         self.hp = hp
133                         
134                         self.direction = vector.multiply(dir,-1)
135                         dir = vector.multiply(dir,10)
136                         if vel.y <= 0 then
137                                 dir.y = 4
138                         else
139                                 dir.y = 0
140                         end
141                         
142                         
143                         self.object:add_velocity(dir)
144                         self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
145                 elseif (self.punched_timer <= 0 and self.death_animation_timer == 0) then
146                         if puncher ~= self.object and self.attacked_hostile then
147                                 if self.group_attack == true then
148                                         for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
149                                                 if not object:is_player() and object:get_luaentity() and object:get_luaentity().mobname == self.mobname then
150                                                         object:get_luaentity().hostile = true
151                                                         object:get_luaentity().hostile_timer = 20
152                                                 end
153                                         end
154                                 end
155                         end
156                         self.death_animation_timer = 1
157                         self.dead = true
158                         
159                         --critical effect
160                         if critical == true then
161                                 self.do_critical_particles(pos)
162                                 minetest.sound_play("critical", {object=self.object, gain = 0.1, max_hear_distance = 10,pitch = math.random(80,100)/100})
163                         end
164                         minetest.sound_play(self.die_sound, {object=self.object, gain = 1.0, max_hear_distance = 10,pitch = math.random(80,100)/100})
165                         
166                         self.object:set_texture_mod("^[colorize:red:130")
167                         self.add_sword_wear(self, puncher, time_from_last_punch, tool_capabilities, dir)
168                 end
169         end
170
171         --this is what happens when a mob dies
172         mob_register.on_death = function(self, killer)
173                 local pos = self.object:getpos()
174                 --pos.y = pos.y + 0.4
175                 minetest.sound_play("mob_die", {pos = pos, gain = 1.0})
176                 minetest.add_particlespawner({
177                         amount = 40,
178                         time = 0.001,
179                         minpos = pos,
180                         maxpos = pos,
181                         minvel = vector.new(-5,-5,-5),
182                         maxvel = vector.new(5,5,5),
183                         minacc = {x=0, y=0, z=0},
184                         maxacc = {x=0, y=0, z=0},
185                         minexptime = 1.1,
186                         maxexptime = 1.5,
187                         minsize = 1,
188                         maxsize = 2,
189                         collisiondetection = false,
190                         vertical = false,
191                         texture = "smoke.png",
192                 })
193                 
194                 --only throw items if registered
195                 if self.item_drop then
196                         --detect if multiple items are going to be added
197                         if self.item_amount  and self.item_minumum then
198                                 local data_item_amount = math.random(self.item_minimum, self.item_amount)
199                                 for i = self.item_minimum,data_item_amount do
200                                         minetest.throw_item(pos,self.item_drop)
201                                 end
202                         else
203                                 minetest.throw_item(pos,self.item_drop)
204                         end
205                 end
206                         
207                 global_mob_amount = global_mob_amount - 1
208                 print("Mobs Died. Current Mobs: "..global_mob_amount)
209                 if self.child and self.child:get_luaentity() then
210                         self.child:get_luaentity().parent = nil
211                 end
212                 
213                 self.object:remove()
214         end
215         
216         --the pig will look for and at players
217         mob_register.look_around = function(self,dtime)
218                 local pos = self.object:get_pos()
219                 
220                 if self.die_in_light and self.die_in_light_level and self.die_in_light == true then
221                         local light_level = minetest.get_node_light(pos)
222                         if light_level then
223                                 if (self.die_in_light == true and light_level > self.die_in_light_level) then
224                                         local damage = self.hp
225                                         self.object:punch(self.object, 2, 
226                                                 {
227                                                 full_punch_interval=1.5,
228                                                 damage_groups = {damage=damage},
229                                         })
230                                 end
231                         end
232                 end
233                 
234                 --STARE O_O
235                 --and follow!
236                 self.following = false
237                 local player_found = false
238                 for _,object in ipairs(minetest.get_objects_inside_radius(pos, self.view_distance)) do
239                         if object:is_player() and player_found == false and object:get_hp() > 0 then
240                                 --look at player's camera
241                                 local pos2 = object:get_pos()
242                                 pos2.y = pos2.y + 1.625
243                                 
244                                 player_found = true
245                                 
246                                 if self.head_bone then
247                                         self.move_head(self,pos2,dtime)
248                                 end
249                                 
250                                 
251                                 if self.hostile == true then
252                                         
253                                         self.direction = vector.direction(vector.new(pos.x,0,pos.z),vector.new(pos2.x,0,pos2.z))
254                                         local distance = vector.distance(pos,pos2)
255                                         
256                                         
257                                         --punch the player
258                                         if self.attack_type == "punch" then
259                                                 if distance < 2.5 and self.punch_timer <= 0 and object:get_hp() > 0 then
260                                                         local line_of_sight = minetest.line_of_sight(pos, pos2)
261                                                         if line_of_sight == true then
262                                                                 self.punch_timer = 1
263                                                                 object:punch(self.object, 2, 
264                                                                         {
265                                                                         full_punch_interval=1.5,
266                                                                         damage_groups = {fleshy=2},
267                                                                 },vector.direction(pos,pos2))
268                                                         end
269                                                 end
270                                         elseif self.attack_type == "explode" then
271                                                 if distance <  self.explosion_radius then
272                                                         
273                                                         if not self.tnt_timer then
274                                                                 minetest.sound_play("tnt_ignite", {object = self.object, gain = 1.0,})
275                                                                 self.tnt_timer = self.explosion_time
276                                                                 self.object:set_texture_mod("^[colorize:white:130")
277                                                         end
278                                                 end
279                                         elseif self.attack_type == "projectile" then
280                                                 if not self.projectile_timer then
281                                                         self.projectile_timer = self.projectile_timer_cooldown
282                                                 end
283                                                 if self.projectile_timer <= 0 then
284                                                         self.projectile_timer = self.projectile_timer_cooldown
285                                                         
286                                                         local obj = minetest.add_entity(pos, self.projectile_type)
287                                                         if obj then
288                                                                 local dir = vector.multiply(vector.direction(pos,pos2), 50)
289                                                                 obj:set_velocity(dir)
290                                                                 obj:get_luaentity().timer = 2
291                                                         end
292                                                 end
293                                         end
294                                         --self.speed = distance * 4
295                                         --if self.speed > self.max_speed then
296                                         self.speed = self.max_speed
297                                         --end
298                                         self.following = true
299                                 end
300                                 --only look at one player
301                                 break
302                         end
303                 end
304                 --stare straight if not found
305                 if player_found == false then
306                         --self.move_head(self,nil,dtime)
307                         if self.manage_hostile_timer then
308                                 self.manage_hostile_timer(self,dtime)
309                         end
310                 end
311         end
312         
313         return(mob_register)
314 end