]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/spawning.lua
Add in flying pigs
[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 local spawn_table = {"pig","slime"}
15 local aether_spawn_table = {"flying_pig"}
16 minetest.register_globalstep(function(dtime)
17         if spawn then
18         timer = timer + dtime
19         if timer >= tick and math.random(1,chance) == chance then
20                 --print("ticking")
21                 timer = 0
22                 --check through players
23                 for _,player in ipairs(minetest.get_connected_players()) do
24                         --don't spawn near dead players
25                         if player:get_hp() > 0 then
26                         
27                                 --local mob_number = math.random(0,2)
28                                 
29                                 local int = {-1,1}
30                                 local pos = vector.floor(vector.add(player:getpos(),0.5))
31                                 
32                                 local x,z
33                                 
34                                 --this is used to determine the axis buffer from the player
35                                 local axis = math.random(0,1)
36                                 
37                                 --cast towards the direction
38                                 if axis == 0 then --x
39                                         x = pos.x + math.random(inner,outer)*int[math.random(1,2)]
40                                         z = pos.z + math.random(-outer,outer)
41                                 else --z
42                                         z = pos.z + math.random(inner,outer)*int[math.random(1,2)]
43                                         x = pos.x + math.random(-outer,outer)
44                                 end
45                                 
46                                 local spawner
47                                 if pos.y >= 21000 then
48                                         spawner = minetest.find_nodes_in_area_under_air(vector.new(x,pos.y-32,z), vector.new(x,pos.y+32,z), {"aether:grass"})
49                                 else
50                                         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"})
51                                 end
52                                 
53                                 --print(dump(spawner))
54                                 if table.getn(spawner) > 0 then
55                                         local mob_pos = spawner[1]
56                                         --aether spawning
57                                         if mob_pos.y >= 21000 then
58                                                 mob_pos.y = mob_pos.y + 1
59                                                 local mob_spawning = aether_spawn_table[1]
60                                                 print("Spawning "..mob_spawning.." at: "..minetest.pos_to_string(mob_pos))
61                                                 minetest.add_entity(mob_pos,"mob:"..mob_spawning)
62
63                                         else
64                                                 mob_pos.y = mob_pos.y + 1
65                                                 local mob_spawning = spawn_table[math.random(1,2)]
66                                                 print("Spawning "..mob_spawning.." at: "..minetest.pos_to_string(mob_pos))
67                                                 minetest.add_entity(mob_pos,"mob:"..mob_spawning)
68                                         end
69                                 end
70                         end
71                 end
72         elseif timer > tick then
73                 timer = 0
74         end
75         end
76 end)