Version 0.3
[minetest_pyramids.git] / mummy.lua
blob9c77a7439fbbb9c1cee0c3941bde2076c269034c
1 --pyramids = {}
3 walk_limit = 1
4 chillaxin_speed = 1
5 animation_speed = 10
6 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
7 animation_blend = 0
9 -- Default player appearance
10 mummy_mesh = "pyramids_mummy.x"
11 mummy_texture = {"pyramids_mummy.png"}
12 mummy_hp = 20
13 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 function get_animations_def()
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 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 prop = {textures = mummy_texture,}
57 self.object:set_properties(prop)
58 end)
59 end
61 function npc_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 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 NPC_ENTITY_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 npc_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
123 spawner_DEF.on_punch = function(self, hitter)
127 NPC_ENTITY_DEF.on_activate = function(self)
128 npc_update_visuals_def(self)
129 self.anim = get_animations_def()
130 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, animation_speed, animation_blend)
131 self.npc_anim = ANIM_STAND
132 self.object:setacceleration({x=0,y=-20,z=0})--20
133 self.state = 1
134 self.object:set_hp(mummy_hp)
135 self.object:set_armor_groups({fleshy=130})
138 NPC_ENTITY_DEF.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
140 --attack as group
141 --[[for _,object in ipairs(minetest.env:get_objects_inside_radius(self.object:getpos(), 5)) do
142 if not object:is_player() then
143 if object:get_luaentity().name == "peaceful_npc:npc_def" then
144 object:get_luaentity().state = 3
145 object:get_luaentity().attacker = puncher:get_player_name()
148 end]]
150 --if self.state ~= 3 then
151 --self.state = 3
152 self.attacker = puncher--:get_player_name()
153 --end
155 if puncher ~= nil then
156 local sound = sound_hit
157 if self.object:get_hp() == 0 then sound = sound_dead end
158 minetest.sound_play(sound, {to_player = puncher:get_player_name(), loop = false, gain = 0.3})
159 if time_from_last_punch >= 0.45 then
160 hit(self)
161 --local dir = puncher:get_look_dir()
162 --self.direction = dir
163 self.direction = {x=self.object:getvelocity().x, y=self.object:getvelocity().y, z=self.object:getvelocity().z}
164 self.punch_timer = 0
165 self.object:setvelocity({x=dir.x*chillaxin_speed,y=5,z=dir.z*chillaxin_speed})--self.object:setvelocity({x=dir.x*4,y=5,z=dir.z*4})
166 if self.state == 1 then
167 self.state = 8
168 elseif self.state >= 2 then
169 self.state = 9
174 if self.object:get_hp() == 0 then
175 local obj = minetest.env:add_item(self.object:getpos(), mummy_drop.." "..math.random(0,3))
179 local cnt1 = 0
180 local cnt2 = 0
182 NPC_ENTITY_DEF.on_step = function(self, dtime)
183 self.timer = self.timer + 0.01
184 self.turn_timer = self.turn_timer + 0.01
185 self.jump_timer = self.jump_timer + 0.01
186 self.punch_timer = self.punch_timer + 0.01
187 self.attacking_timer = self.attacking_timer + 0.01
188 self.sound_timer = self.sound_timer + 0.01
190 local current_pos = self.object:getpos()
191 local current_node = minetest.env:get_node(current_pos)
192 if self.time_passed == nil then
193 self.time_passed = 0
196 --self.time_passed = self.time_passed + dtime
197 if self.object:get_hp() == 0 then-- self.object:remove() end
199 --if self.time_passed >= 5 then
200 minetest.sound_play(sound_dead, {pos = current_pos, max_hear_distance = 10 , gain = 0.3})
201 self.object:remove()
202 end--else
203 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
204 --self.time_passed = self.time_passed + dtime
205 self.sound_timer = self.sound_timer + dtime
206 if self.sound_timer >= 0.8 then
207 self.sound_timer = 0
208 self.object:set_hp(self.object:get_hp()-5)
209 hit(self)
210 minetest.sound_play(sound_hit, {pos = current_pos, max_hear_distance = 10, gain = 0.3})
212 else
213 self.time_passed = 0
215 --end
217 --update moving state every 1 or 2 seconds
218 if self.state < 3 then
219 if self.timer > math.random(1,2) then
220 if self.attacker == "" then
221 self.state = math.random(1,2)
222 else self.state = 1 end
223 self.timer = 0
227 --play sound
228 if self.sound_timer > math.random(5,35) then
229 minetest.sound_play(sound_normal, {pos = current_pos, max_hear_distance = 10, gain = 0.2})
230 self.sound_timer = 0
233 --after punched
234 if self.state >= 8 then
235 if self.punch_timer > 0.15 then
236 --self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
237 if self.state == 9 then
238 self.object:setvelocity({x=self.direction.x*chillaxin_speed,y=-20,z=self.direction.z*chillaxin_speed})
239 self.state = 2
240 elseif self.state == 8 then
241 self.object:setvelocity({x=0,y=-20,z=0})
242 self.state = 1
247 --STANDING
248 if self.state == 1 then
249 self.yawwer = true
250 self.attacker = ""
251 for _,object in ipairs(minetest.env:get_objects_inside_radius(self.object:getpos(), 4)) do
252 if object:is_player() then
253 self.yawwer = false
254 NPC = self.object:getpos()
255 PLAYER = object:getpos()
256 self.vec = {x=PLAYER.x-NPC.x, y=PLAYER.y-NPC.y, z=PLAYER.z-NPC.z}
257 self.yaw = math.atan(self.vec.z/self.vec.x)+math.pi^2
258 if PLAYER.x > NPC.x then
259 self.yaw = self.yaw + math.pi
261 self.yaw = self.yaw - 2
262 self.object:setyaw(self.yaw)
263 self.attacker = object--:get_player_name()
267 if self.attacker == "" and self.turn_timer > math.random(1,4) then--and yawwer == true then
268 self.yaw = 360 * math.random()
269 self.object:setyaw(self.yaw)
270 self.turn_timer = 0
271 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
273 self.object:setvelocity({x=0,y=self.object:getvelocity().y,z=0})
274 if self.npc_anim ~= ANIM_STAND then
275 self.anim = get_animations_def()
276 self.object:set_animation({x=self.anim.stand_START,y=self.anim.stand_END}, animation_speed, animation_blend)
277 self.npc_anim = ANIM_STAND
279 if self.attacker ~= "" then
280 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
281 self.state = 2
284 --WALKING
285 if self.state == 2 then
287 if self.direction ~= nil then
288 self.object:setvelocity({x=self.direction.x*chillaxin_speed,y=self.object:getvelocity().y,z=self.direction.z*chillaxin_speed})
290 if self.turn_timer > math.random(1,4) and not self.attacker then
291 self.yaw = 360 * math.random()
292 self.object:setyaw(self.yaw)
293 self.turn_timer = 0
294 self.direction = {x = math.sin(self.yaw)*-1, y = -20, z = math.cos(self.yaw)}
295 --self.object:setvelocity({x=self.direction.x,y=self.object:getvelocity().y,z=direction.z})
296 --self.object:setacceleration(self.direction)
298 if self.npc_anim ~= ANIM_WALK then
299 self.anim = get_animations_def()
300 self.object:set_animation({x=self.anim.walk_START,y=self.anim.walk_END}, animation_speed, animation_blend)
301 self.npc_anim = ANIM_WALK
303 --[[jump
304 if self.direction ~= nil then
305 if self.jump_timer > 0.3 then
306 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
307 self.object:setvelocity({x=self.object:getvelocity().x,y=5,z=self.object:getvelocity().z})
308 self.jump_timer = 0
311 end]]
313 if self.attacker ~= "" and minetest.setting_getbool("enable_damage") then
314 local s = self.object:getpos()
315 local p = self.attacker:getpos()
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
329 minetest.register_entity("pyramids:mummy", NPC_ENTITY_DEF)
330 minetest.register_entity("pyramids:mummy_spawner", spawner_DEF)
333 --spawn-egg/spawner
335 minetest.register_craftitem("pyramids:spawn_egg", {
336 description = "Mummy spawn-egg",
337 inventory_image = "pyramids_mummy_egg.png",
338 liquids_pointable = false,
339 stack_max = 99,
340 on_place = function(itemstack, placer, pointed_thing)
341 if pointed_thing.type == "node" then
342 minetest.env:add_entity(pointed_thing.above,"pyramids:mummy")
343 if not minetest.setting_getbool("creative_mode") then itemstack:take_item() end
344 return itemstack
346 end,
350 function pyramids.spawn_mummy (pos, number)
351 for i=0,number do
352 minetest.env:add_entity(pos,"pyramids:mummy")
356 minetest.register_node("pyramids:spawner_mummy", {
357 description = "Mummy spawner",
358 paramtype = "light",
359 tiles = {"pyramids_spawner.png"},
360 is_ground_content = true,
361 drawtype = "allfaces",--_optional",
362 groups = {cracky=1,level=1},
363 drop = "",
364 on_construct = function(pos)
365 pos.y = pos.y - 0.28
366 minetest.env:add_entity(pos,"pyramids:mummy_spawner")
367 end,
368 on_destruct = function(pos)
369 for _,obj in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do
370 if not obj:is_player() then
371 if obj ~= nil and obj:get_luaentity().m_name == "dummy" then
372 obj:remove()
378 if not minetest.setting_getbool("only_peaceful_mobs") then
379 minetest.register_abm({
380 nodenames = {"pyramids:spawner_mummy"},
381 interval = 2.0,
382 chance = 20,
383 action = function(pos, node, active_object_count, active_object_count_wider)
384 local player_near = false
385 local mobs = 0
386 for _,obj in ipairs(minetest.env:get_objects_inside_radius(pos, spawner_range)) do
387 if obj:is_player() then
388 player_near = true
389 else
390 if obj:get_luaentity().mob_name == "mummy" then mobs = mobs + 1 end
393 if player_near then
394 if mobs < spawner_max_mobs then
395 pos.x = pos.x+1
396 local p = minetest.find_node_near(pos, 5, {"air"})
397 minetest.env:add_entity(p,"pyramids:mummy")