Fix global variable warnings; push to 0.4.2
[minetest_pyramids.git] / mummy.lua
blobc1188e577b9a1e576f0fcc15dacee2f281b45ef1
1 --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 = "pyramids_mummy.x"
11 local mummy_texture = {"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 function hit(self)
50 local prop = {
51 mesh = mummy_mesh,
52 textures = {"pyramids_mummy.png^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 function mummy_update_visuals_def(self)
62 --local name = get_player_name()
63 --visual = default_model_def
64 npc_anim = 0 -- Animation will be set further below immediately
65 --npc_sneak[name] = false
66 local prop = {
67 mesh = mummy_mesh,
68 textures = mummy_texture,
69 --visual_size = {x=1, y=1, z=1},
71 self.object:set_properties(prop)
72 end
74 MUMMY_DEF = {
75 physical = true,
76 collisionbox = {-0.4, -0.01, -0.4, 0.4, 1.9, 0.4},
77 visual = "mesh",
78 visual_size = {x=8,y=8},
79 mesh = mummy_mesh,
80 textures = mummy_texture,
81 makes_footstep_sound = true,
82 npc_anim = 0,
83 timer = 0,
84 turn_timer = 0,
85 vec = 0,
86 yaw = 0,
87 yawwer = 0,
88 state = 1,
89 jump_timer = 0,
90 punch_timer = 0,
91 sound_timer = 0,
92 attacker = "",
93 attacking_timer = 0,
94 mob_name = "mummy"
97 spawner_DEF = {
98 hp_max = 1,
99 physical = true,
100 collisionbox = {0,0,0,0,0,0},
101 visual = "mesh",
102 visual_size = {x=3.3,y=3.3},
103 mesh = mummy_mesh,
104 textures = mummy_texture,
105 makes_footstep_sound = false,
106 timer = 0,
107 automatic_rotate = math.pi * 2.9,
108 m_name = "dummy"
111 spawner_DEF.on_activate = function(self)
112 mummy_update_visuals_def(self)
113 self.object:setvelocity({x=0, y=0, z=0})
114 self.object:setacceleration({x=0, y=0, z=0})
115 self.object:set_armor_groups({immortal=1})
119 spawner_DEF.on_step = function(self, dtime)
120 self.timer = self.timer + 0.01
121 local n = minetest.get_node_or_nil(self.object:getpos())
122 if self.timer > 1 then
123 if n and n.name and n.name ~= "pyramids:spawner_mummy" then
124 self.object:remove()
129 spawner_DEF.on_punch = function(self, hitter)
133 MUMMY_DEF.on_activate = function(self)
134 mummy_update_visuals_def(self)
135 self.anim = get_animations()
136 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
137 self.npc_anim = ANIM_STAND
138 self.object:setacceleration({x=0,y=-20,z=0})--20
139 self.state = 1
140 self.object:set_hp(mummy_hp)
141 self.object:set_armor_groups({fleshy=130})
144 MUMMY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
146 --attack as group
147 --[[for _,object in ipairs(minetest.env:get_objects_inside_radius(self.object:getpos(), 5)) do
148 if not object:is_player() then
149 if object:get_luaentity().name == "peaceful_npc:npc_def" then
150 object:get_luaentity().state = 3
151 object:get_luaentity().attacker = puncher:get_player_name()
154 end]]
156 --if self.state ~= 3 then
157 --self.state = 3
158 self.attacker = puncher--:get_player_name()
159 --end
161 if puncher ~= nil then
162 local sound = sound_hit
163 if self.object:get_hp() == 0 then sound = sound_dead end
164 minetest.sound_play(sound, {to_player = puncher:get_player_name(), loop = false, gain = 0.3})
165 if time_from_last_punch >= 0.45 then
166 hit(self)
167 --local dir = puncher:get_look_dir()
168 --self.direction = dir
169 self.direction = {x=self.object:getvelocity().x, y=self.object:getvelocity().y, z=self.object:getvelocity().z}
170 self.punch_timer = 0
171 self.object:setvelocity({x=dir.x*mummy_chillaxin_speed,y=5,z=dir.z*mummy_chillaxin_speed})--self.object:setvelocity({x=dir.x*4,y=5,z=dir.z*4})
172 if self.state == 1 then
173 self.state = 8
174 elseif self.state >= 2 then
175 self.state = 9
180 if self.object:get_hp() == 0 then
181 local obj = minetest.env:add_item(self.object:getpos(), mummy_drop.." "..math.random(0,3))
185 local cnt1 = 0
186 local cnt2 = 0
188 MUMMY_DEF.on_step = function(self, dtime)
189 self.timer = self.timer + 0.01
190 self.turn_timer = self.turn_timer + 0.01
191 self.jump_timer = self.jump_timer + 0.01
192 self.punch_timer = self.punch_timer + 0.01
193 self.attacking_timer = self.attacking_timer + 0.01
194 self.sound_timer = self.sound_timer + 0.01
196 local current_pos = self.object:getpos()
197 local current_node = minetest.env:get_node(current_pos)
198 if self.time_passed == nil then
199 self.time_passed = 0
202 --self.time_passed = self.time_passed + dtime
203 if self.object:get_hp() == 0 then-- self.object:remove() end
205 --if self.time_passed >= 5 then
206 minetest.sound_play(sound_dead, {pos = current_pos, max_hear_distance = 10 , gain = 0.3})
207 self.object:remove()
208 end--else
209 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
210 --self.time_passed = self.time_passed + dtime
211 self.sound_timer = self.sound_timer + dtime
212 if self.sound_timer >= 0.8 then
213 self.sound_timer = 0
214 self.object:set_hp(self.object:get_hp()-5)
215 hit(self)
216 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.3})
218 else
219 self.time_passed = 0
221 --end
223 --update moving state every 1 or 2 seconds
224 if self.state < 3 then
225 if self.timer > math.random(1,2) then
226 if self.attacker == "" then
227 self.state = math.random(1,2)
228 else self.state = 1 end
229 self.timer = 0
233 --play sound
234 if self.sound_timer > math.random(5,35) then
235 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2})
236 self.sound_timer = 0
239 --after punched
240 if self.state >= 8 then
241 if self.punch_timer > 0.15 then
242 --self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
243 if self.state == 9 then
244 self.object:setvelocity({x=self.direction.x*mummy_chillaxin_speed,y=-20,z=self.direction.z*mummy_chillaxin_speed})
245 self.state = 2
246 elseif self.state == 8 then
247 self.object:setvelocity({x=0,y=-20,z=0})
248 self.state = 1
253 --STANDING
254 if self.state == 1 then
255 self.yawwer = true
256 self.attacker = ""
257 for _,object in ipairs(minetest.env:get_objects_inside_radius(self.object:getpos(), 4)) do
258 if object:is_player() then
259 self.yawwer = false
260 local NPC = self.object:getpos()
261 local PLAYER = object:getpos()
262 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
263 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
264 if PLAYER.x > NPC.x then
265 self.yaw = self.yaw + math.pi
267 self.yaw = self.yaw - 2
268 self.object:setyaw(self.yaw)
269 self.attacker = object--:get_player_name()
273 if self.attacker == "" and self.turn_timer > math.random(1,4) then--and yawwer == true then
274 self.yaw = 360 * math.random()
275 self.object:setyaw(self.yaw)
276 self.turn_timer = 0
277 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
279 self.object:setvelocity({x=0,y=self.object:getvelocity().y,z=0})
280 if self.npc_anim ~= ANIM_STAND then
281 self.anim = get_animations()
282 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, mummy_animation_speed, mummy_animation_blend)
283 self.npc_anim = ANIM_STAND
285 if self.attacker ~= "" then
286 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
287 self.state = 2
290 --WALKING
291 if self.state == 2 then
293 if self.direction ~= nil then
294 self.object:setvelocity({x=self.direction.x*mummy_chillaxin_speed,y=self.object:getvelocity().y,z=self.direction.z*mummy_chillaxin_speed})
296 if self.turn_timer > math.random(1,4) and not self.attacker then
297 self.yaw = 360 * math.random()
298 self.object:setyaw(self.yaw)
299 self.turn_timer = 0
300 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
301 --self.object:setvelocity({x=self.direction.x,y=self.object:getvelocity().y,z=direction.z})
302 --self.object:setacceleration(self.direction)
304 if self.npc_anim ~= ANIM_WALK then
305 self.anim = get_animations()
306 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, mummy_animation_speed, mummy_animation_blend)
307 self.npc_anim = ANIM_WALK
309 --[[jump
310 if self.direction ~= nil then
311 if self.jump_timer > 0.3 then
312 if minetest.env:get_node({x=self.object:getpos().x + self.direction.x,y=self.object:getpos().y-1,z=self.object:getpos().z + self.direction.z}).name ~= "air" then
313 self.object:setvelocity({x=self.object:getvelocity().x,y=5,z=self.object:getvelocity().z})
314 self.jump_timer = 0
317 end]]
319 if self.attacker ~= "" and minetest.setting_getbool("enable_damage") then
320 local s = self.object:getpos()
321 local p = self.attacker:getpos()
322 if (s ~= nil and p ~= nil) then
323 local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
325 if dist < 2 and self.attacking_timer > 0.6 then
326 self.attacker:punch(self.object, 1.0, {
327 full_punch_interval=1.0,
328 damage_groups = {fleshy=1}
330 self.attacking_timer = 0
337 minetest.register_entity("pyramids:mummy", MUMMY_DEF)
338 minetest.register_entity("pyramids:mummy_spawner", spawner_DEF)
341 --spawn-egg/spawner
343 minetest.register_craftitem("pyramids:spawn_egg", {
344 description = "Mummy spawn-egg",
345 inventory_image = "pyramids_mummy_egg.png",
346 liquids_pointable = false,
347 stack_max = 99,
348 on_place = function(itemstack, placer, pointed_thing)
349 if pointed_thing.type == "node" then
350 minetest.env:add_entity(pointed_thing.above,"pyramids:mummy")
351 if not minetest.setting_getbool("creative_mode") then itemstack:take_item() end
352 return itemstack
354 end,
358 function pyramids.spawn_mummy (pos, number)
359 for i=0,number do
360 minetest.env:add_entity(pos,"pyramids:mummy")
364 minetest.register_node("pyramids:spawner_mummy", {
365 description = "Mummy spawner",
366 paramtype = "light",
367 tiles = {"pyramids_spawner.png"},
368 is_ground_content = true,
369 drawtype = "allfaces",--_optional",
370 groups = {cracky=1,level=1},
371 drop = "",
372 on_construct = function(pos)
373 pos.y = pos.y - 0.28
374 minetest.env:add_entity(pos,"pyramids:mummy_spawner")
375 end,
376 on_destruct = function(pos)
377 for _,obj in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do
378 if not obj:is_player() then
379 if obj ~= nil and obj:get_luaentity().m_name == "dummy" then
380 obj:remove()
386 if not minetest.setting_getbool("only_peaceful_mobs") then
387 minetest.register_abm({
388 nodenames = {"pyramids:spawner_mummy"},
389 interval = 2.0,
390 chance = 20,
391 action = function(pos, node, active_object_count, active_object_count_wider)
392 local player_near = false
393 local mobs = 0
394 for _,obj in ipairs(minetest.env:get_objects_inside_radius(pos, spawner_range)) do
395 if obj:is_player() then
396 player_near = true
397 else
398 if obj:get_luaentity().mob_name == "mummy" then mobs = mobs + 1 end
401 if player_near then
402 if mobs < spawner_max_mobs then
403 pos.x = pos.x+1
404 local p = minetest.find_node_near(pos, 5, {"air"})
405 minetest.env:add_entity(p,"pyramids:mummy")