]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/spawning.lua
A test
[Crafter.git] / mods / mob / spawning.lua
1 --this is where mob spawning is defined
2
3 --spawn mob in a square doughnut shaped radius
4 local chance = 20
5 local tick = 0.2
6 local timer = 0
7 --inner and outer part of square donut radius
8 local inner = 24
9 local outer = 128
10
11 --for debug testing to isolate mobs
12 local spawn = true
13
14 minetest.register_globalstep(function(dtime)
15       if spawn then
16       timer = timer + dtime
17       if timer >= tick and math.random(1,chance) == chance then
18             --print("ticking")
19             timer = 0
20             --check through players
21             for _,player in ipairs(minetest.get_connected_players()) do
22                   --don't spawn near dead players
23                   if player:get_hp() > 0 then
24                   
25                         --local mob_number = math.random(0,2)
26                         
27                         local int = {-1,1}
28                         local pos = vector.floor(vector.add(player:getpos(),0.5))
29                         
30                         local x,z
31                         
32                         --this is used to determine the axis buffer from the player
33                         local axis = math.random(0,1)
34                         
35                         --cast towards the direction
36                         if axis == 0 then --x
37                               x = pos.x + math.random(inner,outer)*int[math.random(1,2)]
38                               z = pos.z + math.random(-outer,outer)
39                         else --z
40                               z = pos.z + math.random(inner,outer)*int[math.random(1,2)]
41                               x = pos.x + math.random(-outer,outer)
42                         end
43                         
44                         
45                         local spawner = minetest.find_nodes_in_area_under_air(vector.new(x,pos.y-32,z), vector.new(x,pos.y+32,z), {"main:grass","main:sand","main:water"})
46                                           
47                         --print(dump(spawner))
48                         if table.getn(spawner) > 0 then
49                               local mob_pos = spawner[1]
50                               mob_pos.y = mob_pos.y + 1
51                               --print("Spawning at: "..minetest.pos_to_string(mob_pos))
52                               minetest.add_entity(mob_pos,"mob:pig")
53                         end
54                   end
55             end
56       elseif timer > tick then
57             timer = 0
58       end
59       end
60 end)