Refactor room.lua a bit
[minetest_pyramids/tsm_pyramids.git] / mummy.lua
blob2009864b4508f38de0831a248771eead7af6b5b8
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_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()
56 local prop = {textures = mummy_texture,}
57 self.object:set_properties(prop)
58 end)
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 mesh = mummy_mesh,
65 textures = mummy_texture,
67 self.object:set_properties(prop)
68 end
70 local MUMMY_DEF = {
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 attacker = "",
89 attacking_timer = 0,
90 mob_name = "mummy"
93 local spawner_DEF = {
94 hp_max = 1,
95 physical = true,
96 collisionbox = {0,0,0,0,0,0},
97 visual = "mesh",
98 visual_size = {x=3.3,y=3.3},
99 mesh = mummy_mesh,
100 textures = mummy_texture,
101 makes_footstep_sound = false,
102 timer = 0,
103 automatic_rotate = math.pi * 2.9,
104 m_name = "dummy"
107 spawner_DEF.on_activate = function(self)
108 mummy_update_visuals_def(self)
109 self.object:set_velocity({x=0, y=0, z=0})
110 self.object:set_acceleration({x=0, y=0, z=0})
111 self.object:set_armor_groups({immortal=1})
115 spawner_DEF.on_step = function(self, dtime)
116 self.timer = self.timer + 0.01
117 local n = minetest.get_node_or_nil(self.object:get_pos())
118 if self.timer > 1 then
119 if n and n.name and n.name ~= "tsm_pyramids:spawner_mummy" then
120 self.object:remove()
125 spawner_DEF.on_punch = function(self, hitter)
129 MUMMY_DEF.on_activate = function(self)
130 mummy_update_visuals_def(self)
131 self.anim = get_animations()
132 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
133 self.npc_anim = ANIM_STAND
134 self.object:set_acceleration({x=0,y=-20,z=0})--20
135 self.state = 1
136 self.object:set_hp(mummy_hp)
137 self.object:set_armor_groups({fleshy=130})
140 MUMMY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
142 self.attacker = puncher
144 if puncher ~= nil then
145 local sound = sound_hit
146 if self.object:get_hp() == 0 then sound = sound_dead end
147 minetest.sound_play(sound, {to_player = puncher:get_player_name(), loop = false, gain = 0.3})
148 if time_from_last_punch >= 0.45 then
149 hit(self)
150 self.direction = {x=self.object:get_velocity().x, y=self.object:get_velocity().y, z=self.object:get_velocity().z}
151 self.punch_timer = 0
152 self.object:set_velocity({x=dir.x*mummy_chillaxin_speed,y=5,z=dir.z*mummy_chillaxin_speed})
153 if self.state == 1 then
154 self.state = 8
155 elseif self.state >= 2 then
156 self.state = 9
161 if self.object:get_hp() == 0 then
162 local obj = minetest.add_item(self.object:get_pos(), mummy_drop.." "..math.random(0,3))
166 local cnt1 = 0
167 local cnt2 = 0
169 MUMMY_DEF.on_step = function(self, dtime)
170 self.timer = self.timer + 0.01
171 self.turn_timer = self.turn_timer + 0.01
172 self.jump_timer = self.jump_timer + 0.01
173 self.punch_timer = self.punch_timer + 0.01
174 self.attacking_timer = self.attacking_timer + 0.01
175 self.sound_timer = self.sound_timer + 0.01
177 local current_pos = self.object:get_pos()
178 local current_node = minetest.get_node(current_pos)
179 if self.time_passed == nil then
180 self.time_passed = 0
183 if self.object:get_hp() == 0 then
184 minetest.sound_play(sound_dead, {pos = current_pos, max_hear_distance = 10 , gain = 0.3})
185 self.object:remove()
187 if current_node.name == "default:water_source" or current_node.name == "default:water_flowing" or current_node.name == "default:lava_source" or current_node.name == "default:lava_flowing" then
188 self.sound_timer = self.sound_timer + dtime
189 if self.sound_timer >= 0.8 then
190 self.sound_timer = 0
191 self.object:set_hp(self.object:get_hp()-5)
192 hit(self)
193 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.3})
195 else
196 self.time_passed = 0
199 --update moving state every 1 or 2 seconds
200 if self.state < 3 then
201 if self.timer > math.random(1,2) then
202 if self.attacker == "" then
203 self.state = math.random(1,2)
204 else self.state = 1 end
205 self.timer = 0
209 --play sound
210 if self.sound_timer > math.random(5,35) then
211 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2})
212 self.sound_timer = 0
215 --after punched
216 if self.state >= 8 then
217 if self.punch_timer > 0.15 then
218 if self.state == 9 then
219 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=-20,z=self.direction.z*mummy_chillaxin_speed})
220 self.state = 2
221 elseif self.state == 8 then
222 self.object:set_velocity({x=0,y=-20,z=0})
223 self.state = 1
228 --STANDING
229 if self.state == 1 then
230 self.yawwer = true
231 self.attacker = ""
232 for _,object in ipairs(minetest.get_objects_inside_radius(self.object:get_pos(), 4)) do
233 if object:is_player() then
234 self.yawwer = false
235 local NPC = self.object:get_pos()
236 local PLAYER = object:get_pos()
237 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
238 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
239 if PLAYER.x > NPC.x then
240 self.yaw = self.yaw + math.pi
242 self.yaw = self.yaw - 2
243 self.object:set_yaw(self.yaw)
244 self.attacker = object
248 if self.attacker == "" and self.turn_timer > math.random(1,4) then
249 self.yaw = 360 * math.random()
250 self.object:set_yaw(self.yaw)
251 self.turn_timer = 0
252 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
254 self.object:set_velocity({x=0,y=self.object:get_velocity().y,z=0})
255 if self.npc_anim ~= ANIM_STAND then
256 self.anim = get_animations()
257 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
258 self.npc_anim = ANIM_STAND
260 if self.attacker ~= "" then
261 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
262 self.state = 2
265 --WALKING
266 if self.state == 2 then
268 if self.direction ~= nil then
269 self.object:set_velocity({x=self.direction.x*mummy_chillaxin_speed,y=self.object:get_velocity().y,z=self.direction.z*mummy_chillaxin_speed})
271 if self.turn_timer > math.random(1,4) and not self.attacker then
272 self.yaw = 360 * math.random()
273 self.object:set_yaw(self.yaw)
274 self.turn_timer = 0
275 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
277 if self.npc_anim ~= ANIM_WALK then
278 self.anim = get_animations()
279 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, mummy_animation_speed, mummy_animation_blend)
280 self.npc_anim = ANIM_WALK
283 if self.attacker ~= "" and minetest.settings:get_bool("enable_damage") then
284 local s = self.object:get_pos()
285 local p = self.attacker:get_pos()
286 if (s ~= nil and p ~= nil) then
287 local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
289 if dist < 2 and self.attacking_timer > 0.6 then
290 self.attacker:punch(self.object, 1.0, {
291 full_punch_interval=1.0,
292 damage_groups = {fleshy=1}
294 self.attacking_timer = 0
301 minetest.register_entity("tsm_pyramids:mummy", MUMMY_DEF)
302 minetest.register_entity("tsm_pyramids:mummy_spawner", spawner_DEF)
305 --spawn-egg/spawner
307 minetest.register_craftitem("tsm_pyramids:spawn_egg", {
308 description = S("Mummy Spawn Egg"),
309 _doc_items_longdesc = S("Can be used to create a hostile mummy."),
310 _doc_items_usagehelp = S("Place the egg to create a mummy on this spot. Careful, it will probably attack immediately!"),
311 inventory_image = "tsm_pyramids_mummy_egg.png",
312 liquids_pointable = false,
313 stack_max = 99,
314 on_place = function(itemstack, placer, pointed_thing)
315 if pointed_thing.type == "node" then
316 minetest.add_entity(pointed_thing.above,"tsm_pyramids:mummy")
317 if not minetest.settings:get_bool("creative_mode") then itemstack:take_item() end
318 return itemstack
320 end,
324 function tsm_pyramids.spawn_mummy (pos, number)
325 for i=0,number do
326 minetest.add_entity(pos,"tsm_pyramids:mummy")
330 local spawnersounds
331 if default.node_sound_metal_defaults then
332 spawnersounds = default.node_sound_metal_defaults()
333 else
334 spawnersounds = default.node_sound_stone_defaults()
337 minetest.register_node("tsm_pyramids:spawner_mummy", {
338 description = S("Mummy Spawner"),
339 _doc_items_longdesc = S("A mummy spawner causes hostile mummies to appear in its vicinity as long it exists."),
340 paramtype = "light",
341 tiles = {"tsm_pyramids_spawner.png"},
342 is_ground_content = false,
343 drawtype = "allfaces",
344 groups = {cracky=1,level=1},
345 drop = "",
346 on_construct = function(pos)
347 pos.y = pos.y - 0.28
348 minetest.add_entity(pos,"tsm_pyramids:mummy_spawner")
349 end,
350 on_destruct = function(pos)
351 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
352 if not obj:is_player() then
353 if obj ~= nil and obj:get_luaentity().m_name == "dummy" then
354 obj:remove()
358 end,
359 sounds = spawnersounds,
361 if not minetest.settings:get_bool("only_peaceful_mobs") then
362 minetest.register_abm({
363 nodenames = {"tsm_pyramids:spawner_mummy"},
364 interval = 2.0,
365 chance = 20,
366 action = function(pos, node, active_object_count, active_object_count_wider)
367 local player_near = false
368 local mobs = 0
369 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, spawner_range)) do
370 if obj:is_player() then
371 player_near = true
372 else
373 if obj:get_luaentity() and obj:get_luaentity().mob_name == "mummy" then
374 mobs = mobs + 1
378 if player_near then
379 if mobs < spawner_max_mobs then
380 pos.x = pos.x+1
381 local p = minetest.find_node_near(pos, 5, {"air"})
382 minetest.add_entity(p,"tsm_pyramids:mummy")
389 if minetest.get_modpath("awards") then
390 awards.register_achievement("tsm_pyramids_no_mummy_spawner", {
391 title = S("No more mummies!"),
392 description = S("Destroy a mummy spawner by digging."),
393 secret = true,
394 icon = "tsm_pyramids_spawner.png",
395 trigger = {
396 type = "dig",
397 node = "tsm_pyramids:spawner_mummy",
398 target = 1