Use TRUE ephemeral sounds
[minetest_pyramids/tsm_pyramids.git] / mummy.lua
blob2076dde67d63058b63a744a5bea9b91ae80d7ccd
1 local S = minetest.get_translator("tsm_pyramids")
3 local mod_cmi = minetest.get_modpath("cmi") ~= nil
5 local mummy_walk_limit = 1
6 local mummy_chillaxin_speed = 1
7 local mummy_animation_speed = 10
8 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
9 local mummy_animation_blend = 0
11 -- Default player appearance
12 local mummy_mesh = "tsm_pyramids_mummy.x"
13 local mummy_texture = {"tsm_pyramids_mummy.png"}
14 local mummy_hp = 20
15 local mummy_drop = "default:papyrus"
17 local sound_normal = "mummy"
18 local sound_hit = "mummy_hurt"
19 local sound_dead = "mummy_death"
21 local spawner_check_range = 17
22 local spawner_max_mobs = 6
24 local function get_animations()
25 return {
26 stand_START = 74,
27 stand_END = 74,
28 sit_START = 81,
29 sit_END = 160,
30 lay_START = 162,
31 lay_END = 166,
32 walk_START = 74,
33 walk_END = 105,
34 mine_START = 74,
35 mine_END = 105,
36 walk_mine_START = 74,
37 walk_mine_END = 105
39 end
41 local npc_model = {}
42 local npc_anim = {}
43 local npc_sneak = {}
44 local ANIM_STAND = 1
45 local ANIM_SIT = 2
46 local ANIM_LAY = 3
47 local ANIM_WALK = 4
48 local ANIM_WALK_MINE = 5
49 local ANIM_MINE = 6
51 local function hit(self)
52 local prop = {
53 mesh = mummy_mesh,
54 textures = {"tsm_pyramids_mummy.png^tsm_pyramids_hit.png"},
56 self.object:set_properties(prop)
57 minetest.after(0.4, function(self)
58 local prop = {textures = mummy_texture,}
59 if self ~= nil and self.object ~= nil then
60 self.object:set_properties(prop)
61 end
62 end, self)
63 end
65 local function mummy_update_visuals_def(self)
66 npc_anim = 0 -- Animation will be set further below immediately
67 local prop = {
68 mesh = mummy_mesh,
69 textures = mummy_texture,
71 self.object:set_properties(prop)
72 end
74 local MUMMY_DEF = {
75 hp_max = mummy_hp,
76 physical = true,
77 collisionbox = {-0.4, -0.01, -0.4, 0.4, 1.9, 0.4},
78 visual = "mesh",
79 visual_size = {x=8,y=8},
80 mesh = mummy_mesh,
81 textures = mummy_texture,
82 makes_footstep_sound = true,
83 npc_anim = 0,
84 timer = 0,
85 turn_timer = 0,
86 vec = 0,
87 yaw = 0,
88 yawwer = 0,
89 state = 1,
90 jump_timer = 0,
91 punch_timer = 0,
92 sound_timer = 0,
93 envdmg_timer = 0,
94 attacker = "",
95 attacking_timer = 0,
97 -- CMI stuff
98 -- Track last cause of damage for cmi.notify_die
99 last_damage_cause = { type = "unknown" },
100 _cmi_is_mob = true,
101 description = S("Mummy"),
104 local spawner_DEF = {
105 hp_max = 1,
106 physical = false,
107 pointable = false,
108 visual = "mesh",
109 visual_size = {x=3.3,y=3.3},
110 mesh = mummy_mesh,
111 textures = mummy_texture,
112 makes_footstep_sound = false,
113 timer = 0,
114 automatic_rotate = math.pi * 2.9,
117 spawner_DEF.on_activate = function(self)
118 mummy_update_visuals_def(self)
119 self.object:set_velocity({x=0, y=0, z=0})
120 self.object:set_acceleration({x=0, y=0, z=0})
121 self.object:set_armor_groups({immortal=1})
125 spawner_DEF.on_step = function(self, dtime)
126 self.timer = self.timer + 0.01
127 local n = minetest.get_node_or_nil(self.object:get_pos())
128 if self.timer > 1 then
129 if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then
130 self.object:remove()
135 spawner_DEF.on_punch = function(self, hitter)
139 MUMMY_DEF.on_activate = function(self, staticdata, dtime_s)
140 if mod_cmi then
141 cmi.notify_activate(self, dtime_s)
143 mummy_update_visuals_def(self)
144 self.anim = get_animations()
145 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
146 self.npc_anim = ANIM_STAND
147 self.object:set_acceleration({x=0,y=-20,z=0})--20
148 self.state = 1
149 self.object:set_armor_groups({fleshy=130})
152 MUMMY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
153 if mod_cmi then
154 cmi.notify_punch(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
156 self.attacker = puncher
158 if damage > 0 then
159 self.last_damage = {
160 type = "punch",
161 puncher = puncher,
164 if puncher ~= nil then
165 minetest.sound_play(sound_hit, {pos = self.object:get_pos(), loop = false, max_hear_distance = 10, gain = 0.4}, true)
166 if time_from_last_punch >= 0.45 then
167 hit(self)
168 self.direction = {x=self.object:get_velocity().x, y=self.object:get_velocity().y, z=self.object:get_velocity().z}
169 self.punch_timer = 0
170 self.object:set_velocity({x=dir.x*mummy_chillaxin_speed,y=5,z=dir.z*mummy_chillaxin_speed})
171 if self.state == 1 then
172 self.state = 8
173 elseif self.state >= 2 then
174 self.state = 9
180 MUMMY_DEF.on_death = function(self, killer)
181 minetest.sound_play(sound_dead, {pos = self.object:get_pos(), max_hear_distance = 10 , gain = 0.3}, true)
182 -- Drop item on death
183 local count = math.random(0,3)
184 if count > 0 then
185 local pos = self.object:get_pos()
186 pos.y = pos.y + 1.0
187 minetest.add_item(pos, mummy_drop .. " " .. count)
189 if mod_cmi then
190 cmi.notify_die(self, self.last_damage)
194 MUMMY_DEF.on_step = function(self, dtime)
195 if mod_cmi then
196 cmi.notify_step(self, dtime)
198 self.timer = self.timer + 0.01
199 self.turn_timer = self.turn_timer + 0.01
200 self.jump_timer = self.jump_timer + 0.01
201 self.punch_timer = self.punch_timer + 0.01
202 self.attacking_timer = self.attacking_timer + 0.01
203 self.sound_timer = self.sound_timer + dtime + 0.01
205 local current_pos = self.object:get_pos()
206 local current_node = minetest.get_node(current_pos)
207 if self.time_passed == nil then
208 self.time_passed = 0
211 -- Environment damage
212 local def = minetest.registered_nodes[current_node.name]
213 local dps = def.damage_per_second
214 local dmg = 0
215 local dmg_node, dmg_pos
216 if dps ~= nil and dps > 0 then
217 dmg = dps
219 -- Damage from node
220 if dmg < 4 and (minetest.get_item_group(current_node.name, "water") ~= 0 or minetest.get_item_group(current_node.name, "lava") ~= 0) then
221 dmg = 4
223 -- Damage by suffocation
224 if (def.walkable == nil or def.walkable == true)
225 and (def.drowning == nil or def.drowning == 0)
226 and (def.damage_per_second == nil or def.damage_per_second <= 0)
227 and (def.collision_box == nil or def.collision_box.type == "regular")
228 and (def.node_box == nil or def.node_box.type == "regular")
229 and (def.groups and def.groups.disable_suffocation ~= 1) then
230 dmg = dmg + 1
232 self.envdmg_timer = self.envdmg_timer + dtime
233 if dmg > 0 then
234 if self.envdmg_timer >= 1 then
235 self.envdmg_timer = 0
236 self.object:set_hp(self.object:get_hp()-dmg)
237 self.last_damage = {
238 type = "environment",
239 pos = current_pos,
240 node = current_node,
242 if self.object:get_hp() <= 0 then
243 if self.on_death then
244 self.on_death(self)
246 self.object:remove()
247 else
248 hit(self)
249 self.sound_timer = 0
250 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.4}, true)
253 else
254 self.time_passed = 0
257 --update moving state every 1 or 2 seconds
258 if self.state < 3 then
259 if self.timer > math.random(1,2) then
260 if self.attacker == "" then
261 self.state = math.random(1,2)
262 else self.state = 1 end
263 self.timer = 0
267 --play sound
268 if self.sound_timer > math.random(5,35) then
269 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2}, true)
270 self.sound_timer = 0
273 --after punched
274 if self.state >= 8 then
275 if self.punch_timer > 0.15 then
276 if self.state == 9 then
277 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=-20,z=self.direction.z*mummy_chillaxin_speed})
278 self.state = 2
279 elseif self.state == 8 then
280 self.object:set_velocity({x=0,y=-20,z=0})
281 self.state = 1
286 --STANDING
287 if self.state == 1 then
288 self.yawwer = true
289 self.attacker = ""
290 for _,object in ipairs(minetest.get_objects_inside_radius(self.object:get_pos(), 4)) do
291 if object:is_player() then
292 self.yawwer = false
293 local NPC = self.object:get_pos()
294 local PLAYER = object:get_pos()
295 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
296 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
297 if PLAYER.x > NPC.x then
298 self.yaw = self.yaw + math.pi
300 self.yaw = self.yaw - 2
301 self.object:set_yaw(self.yaw)
302 self.attacker = object
306 if self.attacker == "" and self.turn_timer > math.random(1,4) then
307 self.yaw = 360 * math.random()
308 self.object:set_yaw(self.yaw)
309 self.turn_timer = 0
310 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
312 self.object:set_velocity({x=0,y=self.object:get_velocity().y,z=0})
313 if self.npc_anim ~= ANIM_STAND then
314 self.anim = get_animations()
315 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
316 self.npc_anim = ANIM_STAND
318 if self.attacker ~= "" then
319 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
320 self.state = 2
323 --WALKING
324 if self.state == 2 then
326 if self.direction ~= nil then
327 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=self.object:get_velocity().y,z=self.direction.z*mummy_chillaxin_speed})
329 if self.turn_timer > math.random(1,4) and not self.attacker then
330 self.yaw = 360 * math.random()
331 self.object:set_yaw(self.yaw)
332 self.turn_timer = 0
333 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
335 if self.npc_anim ~= ANIM_WALK then
336 self.anim = get_animations()
337 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, mummy_animation_speed, mummy_animation_blend)
338 self.npc_anim = ANIM_WALK
341 if self.attacker ~= "" and minetest.settings:get_bool("enable_damage") then
342 local s = self.object:get_pos()
343 local p = self.attacker:get_pos()
344 if (s ~= nil and p ~= nil) then
345 local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
347 if dist < 2 and self.attacking_timer > 0.6 then
348 self.attacker:punch(self.object, 1.0, {
349 full_punch_interval=1.0,
350 damage_groups = {fleshy=1}
352 self.attacking_timer = 0
359 minetest.register_entity("tsm_pyramids:mummy", MUMMY_DEF)
360 minetest.register_entity("tsm_pyramids:mummy_spawner", spawner_DEF)
363 --spawn-egg/spawner
365 minetest.register_craftitem("tsm_pyramids:spawn_egg", {
366 description = S("Mummy Spawn Egg"),
367 _doc_items_longdesc = S("Can be used to create a hostile mummy."),
368 _doc_items_usagehelp = S("Place the egg to create a mummy on this spot. Careful, it will probably attack immediately!"),
369 inventory_image = "tsm_pyramids_mummy_egg.png",
370 liquids_pointable = false,
371 stack_max = 99,
372 on_place = function(itemstack, placer, pointed_thing)
373 if pointed_thing.type ~= "node" then
374 return itemstack
377 -- am I clicking on something with existing on_rightclick function?
378 local node = minetest.get_node(pointed_thing.under)
379 if placer and not placer:get_player_control().sneak then
380 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
381 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack) or itemstack
385 minetest.add_entity(pointed_thing.above,"tsm_pyramids:mummy")
386 if not minetest.settings:get_bool("creative_mode") then
387 itemstack:take_item()
389 return itemstack
390 end,
394 -- Spawn a mummy at position
395 function tsm_pyramids.spawn_mummy_at(pos, number)
396 local node = minetest.get_node(pos)
397 if node.name ~= "air" then
398 return
400 for _=1, number do
401 minetest.add_entity(pos,"tsm_pyramids:mummy")
405 local spawnersounds
406 if default.node_sound_metal_defaults then
407 spawnersounds = default.node_sound_metal_defaults()
408 else
409 spawnersounds = default.node_sound_stone_defaults()
412 minetest.register_node("tsm_pyramids:spawner_mummy", {
413 description = S("Mummy Spawner"),
414 _doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."),
415 paramtype = "light",
416 tiles = {"tsm_pyramids_spawner.png"},
417 is_ground_content = false,
418 drawtype = "allfaces",
419 groups = {cracky=1,level=1},
420 drop = "",
421 on_construct = function(pos)
422 pos.y = pos.y - 0.28
423 minetest.add_entity(pos,"tsm_pyramids:mummy_spawner")
424 end,
425 on_destruct = function(pos)
426 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
427 if not obj:is_player() then
428 if obj ~= nil and obj:get_luaentity().name == "tsm_pyramids:mummy_spawner" then
429 obj:remove()
433 end,
434 sounds = spawnersounds,
437 -- Attempt to spawn a mummy at a random appropriate position around pos.
438 -- Criteria:
439 -- * Must be close to pos
440 -- * Not in sunlight
441 -- * Must be air on top of a non-air block
442 -- * No more than 6 mummies in area
443 -- * Player must be near is player_near_required is true
444 function tsm_pyramids.attempt_mummy_spawn(pos, player_near_required)
445 local player_near = false
446 local mobs = 0
447 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_check_range)) do
448 if obj:is_player() then
449 player_near = true
450 else
451 if obj:get_luaentity() and obj:get_luaentity().name == "tsm_pyramids:mummy" then
452 mobs = mobs + 1
456 if player_near or (not player_near_required) then
457 if mobs < spawner_max_mobs then
458 local offset = {x=5,y=2,z=5}
459 local nposses = minetest.find_nodes_in_area(vector.subtract(pos, offset), vector.add(pos,offset), "air")
460 local tries = math.min(6, #nposses)
461 for i=1, tries do
462 local r = math.random(1, #nposses)
463 local npos = nposses[r]
464 -- Check if mummy has 2 nodes of free space
465 local two_space = false
466 -- Check if mummy has something to walk on
467 local footing = false
468 -- Find the lowest node
469 for y=-1, -5, -1 do
470 npos.y = npos.y - 1
471 local below = minetest.get_node(npos)
472 if minetest.registered_items[below.name].liquidtype ~= "none" then
473 break
475 if below.name ~= "air" then
476 if y < -1 then
477 two_space = true
479 npos.y = npos.y + 1
480 footing = true
481 break
484 local light = minetest.get_node_light(npos, 0.5)
485 if not two_space then
486 local above = minetest.get_node({x=npos.x, y=npos.y+1, z=npos.z})
487 if above.name == "air" then
488 two_space = true
491 if footing and two_space and light < 15 then
492 tsm_pyramids.spawn_mummy_at(npos, 1)
493 break
494 else
495 table.remove(nposses, r)
502 if not minetest.settings:get_bool("only_peaceful_mobs") then
503 minetest.register_abm({
504 nodenames = {"tsm_pyramids:spawner_mummy"},
505 interval = 2.0,
506 chance = 20,
507 action = function(pos, node, active_object_count, active_object_count_wider)
508 tsm_pyramids.attempt_mummy_spawn(pos, true)
509 end,
513 if minetest.get_modpath("awards") then
514 awards.register_achievement("tsm_pyramids_no_mummy_spawner", {
515 title = S("No more mummies!"),
516 description = S("Destroy a mummy spawner by digging."),
517 secret = true,
518 icon = "tsm_pyramids_spawner.png",
519 trigger = {
520 type = "dig",
521 node = "tsm_pyramids:spawner_mummy",
522 target = 1