Remove giant useless .gitignore
[minetest_pyramids/tsm_pyramids.git] / mummy.lua
blobf1a7ec456d3361db10b79b85806e2a2046e89469
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 self.object:set_texture_mod("^tsm_pyramids_hit.png")
53 minetest.after(0.4, function(self)
54 local prop = {textures = mummy_texture,}
55 if self ~= nil and self.object ~= nil then
56 self.object:set_texture_mod("")
57 end
58 end, self)
59 end
61 local function mummy_update_visuals_def(self)
62 npc_anim = 0 -- Animation will be set further below immediately
63 local prop = {
64 textures = mummy_texture,
66 self.object:set_properties(prop)
67 end
69 local MUMMY_DEF = {
70 hp_max = mummy_hp,
71 physical = true,
72 collisionbox = {-0.4, -0.01, -0.4, 0.4, 1.9, 0.4},
73 visual = "mesh",
74 visual_size = {x=8,y=8},
75 mesh = mummy_mesh,
76 textures = mummy_texture,
77 makes_footstep_sound = true,
78 npc_anim = 0,
79 timer = 0,
80 turn_timer = 0,
81 vec = 0,
82 yaw = 0,
83 yawwer = 0,
84 state = 1,
85 jump_timer = 0,
86 punch_timer = 0,
87 sound_timer = 0,
88 envdmg_timer = 0,
89 attacker = "",
90 attacking_timer = 0,
92 -- CMI stuff
93 -- Track last cause of damage for cmi.notify_die
94 last_damage_cause = { type = "unknown" },
95 _cmi_is_mob = true,
96 description = S("Mummy"),
99 local spawner_DEF = {
100 hp_max = 1,
101 physical = false,
102 pointable = false,
103 visual = "mesh",
104 visual_size = {x=3.3,y=3.3},
105 mesh = mummy_mesh,
106 textures = mummy_texture,
107 makes_footstep_sound = false,
108 timer = 0,
109 automatic_rotate = math.pi * 2.9,
112 spawner_DEF.on_activate = function(self)
113 mummy_update_visuals_def(self)
114 self.object:set_velocity({x=0, y=0, z=0})
115 self.object:set_acceleration({x=0, y=0, z=0})
116 self.object:set_armor_groups({immortal=1})
120 spawner_DEF.on_step = function(self, dtime)
121 self.timer = self.timer + 0.01
122 local n = minetest.get_node_or_nil(self.object:get_pos())
123 if self.timer > 1 then
124 if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then
125 self.object:remove()
126 return
131 spawner_DEF.on_punch = function(self, hitter)
135 MUMMY_DEF.on_activate = function(self, staticdata, dtime_s)
136 if mod_cmi then
137 cmi.notify_activate(self, dtime_s)
139 mummy_update_visuals_def(self)
140 self.anim = get_animations()
141 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
142 self.npc_anim = ANIM_STAND
143 self.object:set_acceleration({x=0,y=-20,z=0})--20
144 self.state = 1
145 self.object:set_armor_groups({fleshy=130})
148 MUMMY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
149 if mod_cmi then
150 cmi.notify_punch(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
152 self.attacker = puncher
154 if damage and damage > 0 then
155 self.last_damage = {
156 type = "punch",
157 puncher = puncher,
160 if puncher ~= nil then
161 minetest.sound_play(sound_hit, {pos = self.object:get_pos(), loop = false, max_hear_distance = 10, gain = 0.4}, true)
162 if time_from_last_punch >= 0.45 then
163 hit(self)
164 self.direction = {x=self.object:get_velocity().x, y=self.object:get_velocity().y, z=self.object:get_velocity().z}
165 self.punch_timer = 0
166 self.object:set_velocity({x=dir.x*mummy_chillaxin_speed,y=5,z=dir.z*mummy_chillaxin_speed})
167 if self.state == 1 then
168 self.state = 8
169 elseif self.state >= 2 then
170 self.state = 9
176 MUMMY_DEF.on_death = function(self, killer)
177 minetest.sound_play(sound_dead, {pos = self.object:get_pos(), max_hear_distance = 10 , gain = 0.3}, true)
178 -- Drop item on death
179 local count = math.random(0,3)
180 if count > 0 then
181 local pos = self.object:get_pos()
182 pos.y = pos.y + 1.0
183 minetest.add_item(pos, mummy_drop .. " " .. count)
185 if mod_cmi then
186 cmi.notify_die(self, self.last_damage)
190 MUMMY_DEF.on_step = function(self, dtime)
191 if mod_cmi then
192 cmi.notify_step(self, dtime)
194 self.timer = self.timer + 0.01
195 self.turn_timer = self.turn_timer + 0.01
196 self.jump_timer = self.jump_timer + 0.01
197 self.punch_timer = self.punch_timer + 0.01
198 self.attacking_timer = self.attacking_timer + 0.01
199 self.sound_timer = self.sound_timer + dtime + 0.01
201 local current_pos = self.object:get_pos()
202 local current_node = minetest.get_node(current_pos)
203 if self.time_passed == nil then
204 self.time_passed = 0
207 -- Environment damage
208 local def = minetest.registered_nodes[current_node.name]
209 local dps = def.damage_per_second
210 local dmg = 0
211 local dmg_node, dmg_pos
212 if dps ~= nil and dps > 0 then
213 dmg = dps
215 -- Damage from node
216 if dmg < 4 and (minetest.get_item_group(current_node.name, "water") ~= 0 or minetest.get_item_group(current_node.name, "lava") ~= 0) then
217 dmg = 4
219 -- Damage by suffocation
220 if (def.walkable == nil or def.walkable == true)
221 and (def.drowning == nil or def.drowning == 0)
222 and (def.damage_per_second == nil or def.damage_per_second <= 0)
223 and (def.collision_box == nil or def.collision_box.type == "regular")
224 and (def.node_box == nil or def.node_box.type == "regular")
225 and (def.groups and def.groups.disable_suffocation ~= 1) then
226 dmg = dmg + 1
228 self.envdmg_timer = self.envdmg_timer + dtime
229 if dmg > 0 then
230 if self.envdmg_timer >= 1 then
231 local new_hp = self.object:get_hp() - dmg
232 if new_hp <= 0 then
233 if self.on_death then
234 self.on_death(self)
236 self.object:remove()
237 return
238 else
239 self.envdmg_timer = 0
240 self.object:set_hp(new_hp)
241 self.last_damage = {
242 type = "environment",
243 pos = current_pos,
244 node = current_node,
246 hit(self)
247 self.sound_timer = 0
248 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.4}, true)
251 else
252 self.time_passed = 0
255 --update moving state every 1 or 2 seconds
256 if self.state < 3 then
257 if self.timer > math.random(1,2) then
258 if self.attacker == "" then
259 self.state = math.random(1,2)
260 else self.state = 1 end
261 self.timer = 0
265 --play sound
266 if self.sound_timer > math.random(5,35) then
267 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2}, true)
268 self.sound_timer = 0
271 --after punched
272 if self.state >= 8 then
273 if self.punch_timer > 0.15 then
274 if self.state == 9 then
275 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=-20,z=self.direction.z*mummy_chillaxin_speed})
276 self.state = 2
277 elseif self.state == 8 then
278 self.object:set_velocity({x=0,y=-20,z=0})
279 self.state = 1
284 --STANDING
285 if self.state == 1 then
286 self.yawwer = true
287 self.attacker = ""
288 for _,object in ipairs(minetest.get_objects_inside_radius(self.object:get_pos(), 4)) do
289 if object:is_player() then
290 self.yawwer = false
291 local NPC = self.object:get_pos()
292 local PLAYER = object:get_pos()
293 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
294 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
295 if PLAYER.x > NPC.x then
296 self.yaw = self.yaw + math.pi
298 self.yaw = self.yaw - 2
299 self.object:set_yaw(self.yaw)
300 self.attacker = object
304 if self.attacker == "" and self.turn_timer > math.random(1,4) then
305 self.yaw = 360 * math.random()
306 self.object:set_yaw(self.yaw)
307 self.turn_timer = 0
308 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
310 self.object:set_velocity({x=0,y=self.object:get_velocity().y,z=0})
311 if self.npc_anim ~= ANIM_STAND then
312 self.anim = get_animations()
313 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
314 self.npc_anim = ANIM_STAND
316 if self.attacker ~= "" then
317 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
318 self.state = 2
321 --WALKING
322 if self.state == 2 then
324 if self.direction ~= nil then
325 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=self.object:get_velocity().y,z=self.direction.z*mummy_chillaxin_speed})
327 if self.turn_timer > math.random(1,4) and not self.attacker then
328 self.yaw = 360 * math.random()
329 self.object:set_yaw(self.yaw)
330 self.turn_timer = 0
331 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
333 if self.npc_anim ~= ANIM_WALK then
334 self.anim = get_animations()
335 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, mummy_animation_speed, mummy_animation_blend)
336 self.npc_anim = ANIM_WALK
339 if self.attacker ~= "" and minetest.settings:get_bool("enable_damage") then
340 local s = self.object:get_pos()
341 local p = self.attacker:get_pos()
342 if (s ~= nil and p ~= nil) then
343 local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
345 if dist < 2 and self.attacking_timer > 0.6 then
346 self.attacker:punch(self.object, 1.0, {
347 full_punch_interval=1.0,
348 damage_groups = {fleshy=1}
350 self.attacking_timer = 0
357 minetest.register_entity("tsm_pyramids:mummy", MUMMY_DEF)
358 minetest.register_entity("tsm_pyramids:mummy_spawner", spawner_DEF)
361 --spawn-egg/spawner
363 minetest.register_craftitem("tsm_pyramids:spawn_egg", {
364 description = S("Mummy Spawn Egg"),
365 _doc_items_longdesc = S("Can be used to create a hostile mummy."),
366 _doc_items_usagehelp = S("Place the egg to create a mummy on this spot. Careful, it will probably attack immediately!"),
367 inventory_image = "tsm_pyramids_mummy_egg.png",
368 liquids_pointable = false,
369 stack_max = 99,
370 on_place = function(itemstack, placer, pointed_thing)
371 if pointed_thing.type ~= "node" then
372 return itemstack
375 -- am I clicking on something with existing on_rightclick function?
376 local node = minetest.get_node(pointed_thing.under)
377 if placer and not placer:get_player_control().sneak then
378 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
379 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack) or itemstack
383 minetest.add_entity(pointed_thing.above,"tsm_pyramids:mummy")
384 if not minetest.settings:get_bool("creative_mode") then
385 itemstack:take_item()
387 return itemstack
388 end,
392 -- Spawn a mummy at position
393 function tsm_pyramids.spawn_mummy_at(pos, number)
394 local node = minetest.get_node(pos)
395 if node.name ~= "air" then
396 return
398 for _=1, number do
399 minetest.add_entity(pos,"tsm_pyramids:mummy")
403 local spawnersounds
404 if default.node_sound_metal_defaults then
405 spawnersounds = default.node_sound_metal_defaults()
406 else
407 spawnersounds = default.node_sound_stone_defaults()
410 minetest.register_node("tsm_pyramids:spawner_mummy", {
411 description = S("Mummy Spawner"),
412 _doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."),
413 paramtype = "light",
414 tiles = {"tsm_pyramids_spawner.png"},
415 is_ground_content = false,
416 drawtype = "allfaces",
417 groups = {cracky=1,level=1},
418 drop = "",
419 on_construct = function(pos)
420 pos.y = pos.y - 0.28
421 minetest.add_entity(pos,"tsm_pyramids:mummy_spawner")
422 end,
423 on_destruct = function(pos)
424 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
425 if not obj:is_player() then
426 if obj ~= nil and obj:get_luaentity().name == "tsm_pyramids:mummy_spawner" then
427 obj:remove()
431 end,
432 sounds = spawnersounds,
435 -- Attempt to spawn a mummy at a random appropriate position around pos.
436 -- Criteria:
437 -- * Must be close to pos
438 -- * Not in sunlight
439 -- * Must be air on top of a non-air block
440 -- * No more than 6 mummies in area
441 -- * Player must be near is player_near_required is true
442 function tsm_pyramids.attempt_mummy_spawn(pos, player_near_required)
443 local player_near = false
444 local mobs = 0
445 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_check_range)) do
446 if obj:is_player() then
447 player_near = true
448 else
449 if obj:get_luaentity() and obj:get_luaentity().name == "tsm_pyramids:mummy" then
450 mobs = mobs + 1
454 if player_near or (not player_near_required) then
455 if mobs < spawner_max_mobs then
456 local offset = {x=5,y=2,z=5}
457 local nposses = minetest.find_nodes_in_area(vector.subtract(pos, offset), vector.add(pos,offset), "air")
458 local tries = math.min(6, #nposses)
459 for i=1, tries do
460 local r = math.random(1, #nposses)
461 local npos = nposses[r]
462 -- Check if mummy has 2 nodes of free space
463 local two_space = false
464 -- Check if mummy has something to walk on
465 local footing = false
466 -- Find the lowest node
467 for y=-1, -5, -1 do
468 npos.y = npos.y - 1
469 local below = minetest.get_node(npos)
470 if minetest.registered_items[below.name].liquidtype ~= "none" then
471 break
473 if below.name ~= "air" then
474 if y < -1 then
475 two_space = true
477 npos.y = npos.y + 1
478 footing = true
479 break
482 local light = minetest.get_node_light(npos, 0.5)
483 if not two_space then
484 local above = minetest.get_node({x=npos.x, y=npos.y+1, z=npos.z})
485 if above.name == "air" then
486 two_space = true
489 if footing and two_space and light < 15 then
490 tsm_pyramids.spawn_mummy_at(npos, 1)
491 break
492 else
493 table.remove(nposses, r)
500 if not minetest.settings:get_bool("only_peaceful_mobs") then
501 minetest.register_abm({
502 nodenames = {"tsm_pyramids:spawner_mummy"},
503 interval = 2.0,
504 chance = 20,
505 action = function(pos, node, active_object_count, active_object_count_wider)
506 tsm_pyramids.attempt_mummy_spawn(pos, true)
507 end,
511 if minetest.get_modpath("awards") then
512 awards.register_achievement("tsm_pyramids_no_mummy_spawner", {
513 title = S("No more mummies!"),
514 description = S("Destroy a mummy spawner by digging."),
515 secret = true,
516 icon = "tsm_pyramids_spawner.png",
517 trigger = {
518 type = "dig",
519 node = "tsm_pyramids:spawner_mummy",
520 target = 1