Simplify trap stone definition
[minetest_pyramids/tsm_pyramids.git] / mummy.lua
blob56afefa5308b7b499796134a0b632cb83f636f1d
1 local S = minetest.get_translator("tsm_pyramids")
3 local mummy_walk_limit = 1
4 local mummy_chillaxin_speed = 1
5 local mummy_animation_speed = 10
6 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
7 local mummy_animation_blend = 0
9 -- Default player appearance
10 local mummy_mesh = "tsm_pyramids_mummy.x"
11 local mummy_texture = {"tsm_pyramids_mummy.png"}
12 local mummy_hp = 20
13 local mummy_drop = "default:papyrus"
15 local sound_normal = "mummy"
16 local sound_hit = "mummy_hurt"
17 local sound_dead = "mummy_death"
19 local spawner_check_range = 17
20 local spawner_max_mobs = 6
22 local function get_animations()
23 return {
24 stand_START = 74,
25 stand_END = 74,
26 sit_START = 81,
27 sit_END = 160,
28 lay_START = 162,
29 lay_END = 166,
30 walk_START = 74,
31 walk_END = 105,
32 mine_START = 74,
33 mine_END = 105,
34 walk_mine_START = 74,
35 walk_mine_END = 105
37 end
39 local npc_model = {}
40 local npc_anim = {}
41 local npc_sneak = {}
42 local ANIM_STAND = 1
43 local ANIM_SIT = 2
44 local ANIM_LAY = 3
45 local ANIM_WALK = 4
46 local ANIM_WALK_MINE = 5
47 local ANIM_MINE = 6
49 local function hit(self)
50 local prop = {
51 mesh = mummy_mesh,
52 textures = {"tsm_pyramids_mummy.png^tsm_pyramids_hit.png"},
54 self.object:set_properties(prop)
55 minetest.after(0.4, function(self)
56 local prop = {textures = mummy_texture,}
57 if self ~= nil and self.object ~= nil then
58 self.object:set_properties(prop)
59 end
60 end, self)
61 end
63 local function mummy_update_visuals_def(self)
64 npc_anim = 0 -- Animation will be set further below immediately
65 local prop = {
66 mesh = mummy_mesh,
67 textures = mummy_texture,
69 self.object:set_properties(prop)
70 end
72 local MUMMY_DEF = {
73 hp_max = mummy_hp,
74 physical = true,
75 collisionbox = {-0.4, -0.01, -0.4, 0.4, 1.9, 0.4},
76 visual = "mesh",
77 visual_size = {x=8,y=8},
78 mesh = mummy_mesh,
79 textures = mummy_texture,
80 makes_footstep_sound = true,
81 npc_anim = 0,
82 timer = 0,
83 turn_timer = 0,
84 vec = 0,
85 yaw = 0,
86 yawwer = 0,
87 state = 1,
88 jump_timer = 0,
89 punch_timer = 0,
90 sound_timer = 0,
91 envdmg_timer = 0,
92 attacker = "",
93 attacking_timer = 0,
96 local spawner_DEF = {
97 hp_max = 1,
98 physical = false,
99 pointable = false,
100 visual = "mesh",
101 visual_size = {x=3.3,y=3.3},
102 mesh = mummy_mesh,
103 textures = mummy_texture,
104 makes_footstep_sound = false,
105 timer = 0,
106 automatic_rotate = math.pi * 2.9,
109 spawner_DEF.on_activate = function(self)
110 mummy_update_visuals_def(self)
111 self.object:set_velocity({x=0, y=0, z=0})
112 self.object:set_acceleration({x=0, y=0, z=0})
113 self.object:set_armor_groups({immortal=1})
117 spawner_DEF.on_step = function(self, dtime)
118 self.timer = self.timer + 0.01
119 local n = minetest.get_node_or_nil(self.object:get_pos())
120 if self.timer > 1 then
121 if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then
122 self.object:remove()
127 spawner_DEF.on_punch = function(self, hitter)
131 MUMMY_DEF.on_activate = function(self)
132 mummy_update_visuals_def(self)
133 self.anim = get_animations()
134 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
135 self.npc_anim = ANIM_STAND
136 self.object:set_acceleration({x=0,y=-20,z=0})--20
137 self.state = 1
138 self.object:set_armor_groups({fleshy=130})
141 MUMMY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
143 self.attacker = puncher
145 if puncher ~= nil then
146 minetest.sound_play(sound_hit, {pos = self.object:get_pos(), loop = false, max_hear_distance = 10, gain = 0.4})
147 if time_from_last_punch >= 0.45 then
148 hit(self)
149 self.direction = {x=self.object:get_velocity().x, y=self.object:get_velocity().y, z=self.object:get_velocity().z}
150 self.punch_timer = 0
151 self.object:set_velocity({x=dir.x*mummy_chillaxin_speed,y=5,z=dir.z*mummy_chillaxin_speed})
152 if self.state == 1 then
153 self.state = 8
154 elseif self.state >= 2 then
155 self.state = 9
161 MUMMY_DEF.on_death = function(self, killer)
162 minetest.sound_play(sound_dead, {pos = self.object:get_pos(), max_hear_distance = 10 , gain = 0.3})
163 -- Drop item on death
164 local count = math.random(0,3)
165 if count > 0 then
166 local pos = self.object:get_pos()
167 pos.y = pos.y + 1.0
168 minetest.add_item(pos, mummy_drop .. " " .. count)
172 local cnt1 = 0
173 local cnt2 = 0
175 MUMMY_DEF.on_step = function(self, dtime)
176 self.timer = self.timer + 0.01
177 self.turn_timer = self.turn_timer + 0.01
178 self.jump_timer = self.jump_timer + 0.01
179 self.punch_timer = self.punch_timer + 0.01
180 self.attacking_timer = self.attacking_timer + 0.01
181 self.sound_timer = self.sound_timer + dtime + 0.01
183 local current_pos = self.object:get_pos()
184 local current_node = minetest.get_node(current_pos)
185 if self.time_passed == nil then
186 self.time_passed = 0
189 local def = minetest.registered_nodes[current_node.name]
190 local dps = def.damage_per_second
191 local dmg = 0
192 if dps ~= nil and dps > 0 then
193 dmg = dps
195 -- Damage from node
196 if dmg < 4 and (minetest.get_item_group(current_node.name, "water") ~= 0 or minetest.get_item_group(current_node.name, "lava") ~= 0) then
197 dmg = 4
199 -- Damage by suffocation
200 if (def.walkable == nil or def.walkable == true)
201 and (def.drowning == nil or def.drowning == 0)
202 and (def.damage_per_second == nil or def.damage_per_second <= 0)
203 and (def.collision_box == nil or def.collision_box.type == "regular")
204 and (def.node_box == nil or def.node_box.type == "regular")
205 and (def.groups and def.groups.disable_suffocation ~= 1) then
206 dmg = dmg + 1
208 self.envdmg_timer = self.envdmg_timer + dtime
209 if dmg > 0 then
210 if self.envdmg_timer >= 1 then
211 self.envdmg_timer = 0
212 self.object:set_hp(self.object:get_hp()-dmg)
213 if self.object:get_hp() <= 0 then
214 if self.on_death then
215 self.on_death(self)
217 self.object:remove()
218 else
219 hit(self)
220 self.sound_timer = 0
221 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.4})
224 else
225 self.time_passed = 0
228 --update moving state every 1 or 2 seconds
229 if self.state < 3 then
230 if self.timer > math.random(1,2) then
231 if self.attacker == "" then
232 self.state = math.random(1,2)
233 else self.state = 1 end
234 self.timer = 0
238 --play sound
239 if self.sound_timer > math.random(5,35) then
240 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2})
241 self.sound_timer = 0
244 --after punched
245 if self.state >= 8 then
246 if self.punch_timer > 0.15 then
247 if self.state == 9 then
248 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=-20,z=self.direction.z*mummy_chillaxin_speed})
249 self.state = 2
250 elseif self.state == 8 then
251 self.object:set_velocity({x=0,y=-20,z=0})
252 self.state = 1
257 --STANDING
258 if self.state == 1 then
259 self.yawwer = true
260 self.attacker = ""
261 for _,object in ipairs(minetest.get_objects_inside_radius(self.object:get_pos(), 4)) do
262 if object:is_player() then
263 self.yawwer = false
264 local NPC = self.object:get_pos()
265 local PLAYER = object:get_pos()
266 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
267 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
268 if PLAYER.x > NPC.x then
269 self.yaw = self.yaw + math.pi
271 self.yaw = self.yaw - 2
272 self.object:set_yaw(self.yaw)
273 self.attacker = object
277 if self.attacker == "" and self.turn_timer > math.random(1,4) then
278 self.yaw = 360 * math.random()
279 self.object:set_yaw(self.yaw)
280 self.turn_timer = 0
281 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
283 self.object:set_velocity({x=0,y=self.object:get_velocity().y,z=0})
284 if self.npc_anim ~= ANIM_STAND then
285 self.anim = get_animations()
286 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
287 self.npc_anim = ANIM_STAND
289 if self.attacker ~= "" then
290 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
291 self.state = 2
294 --WALKING
295 if self.state == 2 then
297 if self.direction ~= nil then
298 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=self.object:get_velocity().y,z=self.direction.z*mummy_chillaxin_speed})
300 if self.turn_timer > math.random(1,4) and not self.attacker then
301 self.yaw = 360 * math.random()
302 self.object:set_yaw(self.yaw)
303 self.turn_timer = 0
304 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
306 if self.npc_anim ~= ANIM_WALK then
307 self.anim = get_animations()
308 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, mummy_animation_speed, mummy_animation_blend)
309 self.npc_anim = ANIM_WALK
312 if self.attacker ~= "" and minetest.settings:get_bool("enable_damage") then
313 local s = self.object:get_pos()
314 local p = self.attacker:get_pos()
315 if (s ~= nil and p ~= nil) then
316 local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
318 if dist < 2 and self.attacking_timer > 0.6 then
319 self.attacker:punch(self.object, 1.0, {
320 full_punch_interval=1.0,
321 damage_groups = {fleshy=1}
323 self.attacking_timer = 0
330 minetest.register_entity("tsm_pyramids:mummy", MUMMY_DEF)
331 minetest.register_entity("tsm_pyramids:mummy_spawner", spawner_DEF)
334 --spawn-egg/spawner
336 minetest.register_craftitem("tsm_pyramids:spawn_egg", {
337 description = S("Mummy Spawn Egg"),
338 _doc_items_longdesc = S("Can be used to create a hostile mummy."),
339 _doc_items_usagehelp = S("Place the egg to create a mummy on this spot. Careful, it will probably attack immediately!"),
340 inventory_image = "tsm_pyramids_mummy_egg.png",
341 liquids_pointable = false,
342 stack_max = 99,
343 on_place = function(itemstack, placer, pointed_thing)
344 if pointed_thing.type == "node" then
345 minetest.add_entity(pointed_thing.above,"tsm_pyramids:mummy")
346 if not minetest.settings:get_bool("creative_mode") then itemstack:take_item() end
347 return itemstack
349 end,
353 -- Spawn a mummy at position
354 function tsm_pyramids.spawn_mummy_at(pos, number)
355 local node = minetest.get_node(pos)
356 if node.name ~= "air" then
357 return
359 for _=1, number do
360 minetest.add_entity(pos,"tsm_pyramids:mummy")
364 local spawnersounds
365 if default.node_sound_metal_defaults then
366 spawnersounds = default.node_sound_metal_defaults()
367 else
368 spawnersounds = default.node_sound_stone_defaults()
371 minetest.register_node("tsm_pyramids:spawner_mummy", {
372 description = S("Mummy Spawner"),
373 _doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."),
374 paramtype = "light",
375 tiles = {"tsm_pyramids_spawner.png"},
376 is_ground_content = false,
377 drawtype = "allfaces",
378 groups = {cracky=1,level=1},
379 drop = "",
380 on_construct = function(pos)
381 pos.y = pos.y - 0.28
382 minetest.add_entity(pos,"tsm_pyramids:mummy_spawner")
383 end,
384 on_destruct = function(pos)
385 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
386 if not obj:is_player() then
387 if obj ~= nil and obj:get_luaentity().name == "tsm_pyramids:mummy_spawner" then
388 obj:remove()
392 end,
393 sounds = spawnersounds,
396 -- Attempt to spawn a mummy at a random appropriate position around pos.
397 -- Criteria:
398 -- * Must be close to pos
399 -- * Not in sunlight
400 -- * Must be air on top of a non-air block
401 -- * No more than 6 mummies in area
402 -- * Player must be near is player_near_required is true
403 function tsm_pyramids.attempt_mummy_spawn(pos, player_near_required)
404 local player_near = false
405 local mobs = 0
406 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_check_range)) do
407 if obj:is_player() then
408 player_near = true
409 else
410 if obj:get_luaentity() and obj:get_luaentity().name == "tsm_pyramids:mummy" then
411 mobs = mobs + 1
415 if player_near or (not player_near_required) then
416 if mobs < spawner_max_mobs then
417 local offset = {x=5,y=2,z=5}
418 local nposses = minetest.find_nodes_in_area(vector.subtract(pos, offset), vector.add(pos,offset), "air")
419 local tries = math.min(6, #nposses)
420 for i=1, tries do
421 local r = math.random(1, #nposses)
422 local npos = nposses[r]
423 -- Check if mummy has 2 nodes of free space
424 local two_space = false
425 -- Check if mummy has something to walk on
426 local footing = false
427 -- Find the lowest node
428 for y=-1, -5, -1 do
429 npos.y = npos.y - 1
430 local below = minetest.get_node(npos)
431 if minetest.registered_items[below.name].liquidtype ~= "none" then
432 break
434 if below.name ~= "air" then
435 if y < -1 then
436 two_space = true
438 npos.y = npos.y + 1
439 footing = true
440 break
443 local light = minetest.get_node_light(npos, 0.5)
444 if not two_space then
445 local above = minetest.get_node({x=npos.x, y=npos.y+1, z=npos.z})
446 if above.name == "air" then
447 two_space = true
450 if footing and two_space and light < 15 then
451 tsm_pyramids.spawn_mummy_at(npos, 1)
452 break
453 else
454 table.remove(nposses, r)
461 if not minetest.settings:get_bool("only_peaceful_mobs") then
462 minetest.register_abm({
463 nodenames = {"tsm_pyramids:spawner_mummy"},
464 interval = 2.0,
465 chance = 20,
466 action = function(pos, node, active_object_count, active_object_count_wider)
467 tsm_pyramids.attempt_mummy_spawn(pos, true)
468 end,
472 if minetest.get_modpath("awards") then
473 awards.register_achievement("tsm_pyramids_no_mummy_spawner", {
474 title = S("No more mummies!"),
475 description = S("Destroy a mummy spawner by digging."),
476 secret = true,
477 icon = "tsm_pyramids_spawner.png",
478 trigger = {
479 type = "dig",
480 node = "tsm_pyramids:spawner_mummy",
481 target = 1