Rename "Monster Spawner" to "Mob Spawner"
[MineClone/MineClone2.git] / mods / ITEMS / mcl_mobspawners / init.lua
blobed2d1fc15ea6f51452d1d151fb5c6f56550708d5
1 local S = mobs.intllib
3 mcl_mobspawners = {}
5 local default_mob = "mobs_mc:pig"
7 -- Mob spawner
8 local spawner_default = default_mob.." 0 15 4 15"
10 local function get_mob_textures(mob)
11 -- FIXME: Ummm … wtf? Why isn't there a textures attribute?
12 return minetest.registered_entities[mob].texture_list[1]
13 end
15 local function find_doll(pos)
16 for _,obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
17 if not obj:is_player() then
18 if obj ~= nil and obj:get_luaentity().name == "mcl_mobspawners:doll" then
19 return obj
20 end
21 end
22 end
23 return nil
24 end
26 local function spawn_doll(pos)
27 return minetest.add_entity({x=pos.x, y=pos.y-0.3, z=pos.z}, "mcl_mobspawners:doll")
28 end
30 local function set_doll_properties(doll, mob)
31 local mobinfo = minetest.registered_entities[mob]
32 local prop = {
33 mesh = mobinfo.mesh,
34 textures = get_mob_textures(mob),
35 visual_size = {
36 x = mobinfo.visual_size.x * 0.33333,
37 y = mobinfo.visual_size.y * 0.33333,
40 doll:set_properties(prop)
41 doll:get_luaentity()._mob = mob
42 end
44 --[[ Public function: Setup the spawner at pos.
45 This function blindly assumes there's actually a spawner at pos.
46 If not, then the results are undefined.
47 All the arguments are optional!
49 * Mob: ID of mob to spawn (default: mobs_mc:pig)
50 * MinLight: Minimum light to spawn (default: 0)
51 * MaxLight: Maximum light to spawn (default: 15)
52 * MaxMobsInArea: How many mobs are allowed in the area around the spawner (default: 4)
53 * PlayerDistance: Spawn mobs only if a player is within this distance; 0 to disable (default: 15)
54 * YOffset: Y offset to spawn mobs; 0 to disable (default: 0)
57 function mcl_mobspawners.setup_spawner(pos, Mob, MinLight, MaxLight, MaxMobsInArea, PlayerDistance, YOffset)
58 -- Activate mob spawner and disable editing functionality
59 if Mob == nil then Mob = default_mob end
60 if MinLight == nil then MinLight = 0 end
61 if MaxLight == nil then MaxLight = 15 end
62 if MaxMobsInArea == nil then MaxMobsInArea = 4 end
63 if PlayerDistance == nil then PlayerDistance = 15 end
64 if YOffset == nil then YOffset = 0 end
65 local meta = minetest.get_meta(pos)
66 meta:set_string("Mob", Mob)
67 meta:set_int("MinLight", MinLight)
68 meta:set_int("MaxLight", MaxLight)
69 meta:set_int("MaxMobsInArea", MaxMobsInArea)
70 meta:set_int("PlayerDistance", PlayerDistance)
71 meta:set_int("YOffset", YOffset)
73 -- Create doll or replace existing doll
74 local doll = find_doll(pos)
75 if not doll then
76 doll = spawn_doll(pos)
77 end
78 set_doll_properties(doll, Mob)
81 -- Start spawning very soon
82 local t = minetest.get_node_timer(pos)
83 t:start(2)
84 end
86 -- Spawn mobs around pos
87 -- NOTE: The node is timer-based, rather than ABM-based.
88 local spawn_mobs = function(pos, elapsed)
90 -- get meta
91 local meta = minetest.get_meta(pos)
93 -- get settings
94 local mob = meta:get_string("Mob")
95 local mlig = meta:get_int("MinLight")
96 local xlig = meta:get_int("MaxLight")
97 local num = meta:get_int("MaxMobsInArea")
98 local pla = meta:get_int("PlayerDistance")
99 local yof = meta:get_int("YOffset")
101 -- if amount is 0 then do nothing
102 if num == 0 then
103 return
106 -- are we spawning a registered mob?
107 if not mobs.spawning_mobs[mob] then
108 minetest.log("error", "[mcl_mobspawners] Mob Spawner: Mob doesn't exist: "..mob)
109 return
112 -- check objects inside 8×8 area around spawner
113 local objs = minetest.get_objects_inside_radius(pos, 8)
114 local count = 0
115 local ent = nil
118 local timer = minetest.get_node_timer(pos)
120 -- spawn mob if player detected and in range
121 if pla > 0 then
123 local in_range = 0
124 local objs = minetest.get_objects_inside_radius(pos, pla)
126 for _,oir in pairs(objs) do
128 if oir:is_player() then
130 in_range = 1
132 break
136 -- player not found
137 if in_range == 0 then
138 -- Try again quickly
139 timer:start(2)
140 return
144 --[[ HACK!
145 The doll may not stay spawned if the mob spawner is placed far away from
146 players, so we will check for its existance periodically when a player is nearby.
147 This would happen almost always when the mob spawner is placed by the mapgen.
148 This is probably caused by a Minetest bug:
149 https://github.com/minetest/minetest/issues/4759
150 FIXME: Fix this horrible hack.
152 local doll = find_doll(pos)
153 if not doll then
154 doll = spawn_doll(pos)
155 set_doll_properties(doll, mob)
158 -- count mob objects of same type in area
159 for k, obj in ipairs(objs) do
161 ent = obj:get_luaentity()
163 if ent and ent.name and ent.name == mob then
164 count = count + 1
168 -- Are there too many of same type? then fail
169 if count >= num then
170 timer:start(math.random(5, 20))
171 return
174 -- find air blocks within 8×3×8 nodes of spawner
175 local air = minetest.find_nodes_in_area(
176 {x = pos.x - 4, y = pos.y - 1 + yof, z = pos.z - 4},
177 {x = pos.x + 4, y = pos.y + 1 + yof, z = pos.z + 4},
178 {"air"})
180 -- spawn up to 4 mobs in random air blocks
181 if air then
182 for a=1, 4 do
183 if #air <= 0 then
184 -- We're out of space! Stop spawning
185 break
187 local air_index = math.random(#air)
188 local pos2 = air[air_index]
189 local lig = minetest.get_node_light(pos2) or 0
191 pos2.y = pos2.y + 0.5
193 -- only if light levels are within range
194 if lig >= mlig and lig <= xlig then
195 minetest.add_entity(pos2, mob)
197 table.remove(air, air_index)
201 -- Spawn attempt done. Next spawn attempt much later
202 timer:start(math.random(10, 39.95))
206 -- The mob spawner node.
207 -- PLACEMENT INSTRUCTIONS:
208 -- If this node is placed by a player, minetest.item_place, etc. default settings are applied
209 -- automatially.
210 -- IF this node is placed by ANY other method (e.g. minetest.set_node, LuaVoxelManip), you
211 -- MUST call mcl_mobspawners.setup_spawner right after the spawner has been placed.
212 minetest.register_node("mcl_mobspawners:spawner", {
213 tiles = {"mob_spawner.png"},
214 drawtype = "glasslike",
215 paramtype = "light",
216 sunlight_propagates = true,
217 walkable = true,
218 description = S("Mob Spawner"),
219 _doc_items_longdesc = S("A mob spawner regularily causes mobs to appear around it while a player is nearby. Mobs are spawned regardless of the light level."),
220 _doc_items_usagehelp = S("If you have a spawn egg, you use it to change the monter to spawn. Just place the item on the mob spawner."),
221 groups = {pickaxey=1, material_stone=1, deco_block=1},
222 is_ground_content = false,
223 drop = "",
225 -- If placed by player, setup spawner with default settings
226 on_place = function(itemstack, placer, pointed_thing)
227 if pointed_thing.type ~= "node" then
228 return itemstack
231 -- Use pointed node's on_rightclick function first, if present
232 local node = minetest.get_node(pointed_thing.under)
233 if placer and not placer:get_player_control().sneak then
234 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
235 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack) or itemstack
239 local name = placer:get_player_name()
240 local privs = minetest.get_player_privs(name)
241 if not privs.maphack then
242 minetest.chat_send_player(name, "Placement denied. You need the “maphack” privilege to place mob spawners.")
243 return itemstack
245 local node_under = minetest.get_node(pointed_thing.under)
246 local new_itemstack, success = minetest.item_place(itemstack, placer, pointed_thing)
247 if success then
248 local placepos
249 if minetest.registered_nodes[node_under.name].buildable_to then
250 placepos = pointed_thing.under
251 else
252 placepos = pointed_thing.above
254 mcl_mobspawners.setup_spawner(placepos)
256 return new_itemstack
257 end,
259 on_destruct = function(pos)
260 -- Remove doll (if any)
261 local obj = find_doll(pos)
262 if obj then
263 obj:remove()
265 end,
267 on_timer = spawn_mobs,
269 on_receive_fields = function(pos, formname, fields, sender)
271 if not fields.text or fields.text == "" then
272 return
275 local meta = minetest.get_meta(pos)
276 local comm = fields.text:split(" ")
277 local name = sender:get_player_name()
279 if minetest.is_protected(pos, name) then
280 minetest.record_protection_violation(pos, name)
281 return
284 local mob = comm[1] -- mob to spawn
285 local mlig = tonumber(comm[2]) -- min light
286 local xlig = tonumber(comm[3]) -- max light
287 local num = tonumber(comm[4]) -- total mobs in area
288 local pla = tonumber(comm[5]) -- player distance (0 to disable)
289 local yof = tonumber(comm[6]) or 0 -- Y offset to spawn mob
291 if mob and mob ~= "" and mobs.spawning_mobs[mob] == true
292 and num and num >= 0 and num <= 10
293 and mlig and mlig >= 0 and mlig <= 15
294 and xlig and xlig >= 0 and xlig <= 15
295 and pla and pla >=0 and pla <= 20
296 and yof and yof > -10 and yof < 10 then
298 mcl_mobspawners.setup_spawner(pos, mob, mlig, xlig, num, pla, yof)
299 else
300 minetest.chat_send_player(name, S("Mob Spawner settings failed!"))
301 minetest.chat_send_player(name,
302 S("Syntax: name min_light[0-14] max_light[0-14] max_mobs_in_area[0 to disable] distance[1-20] y_offset[-10 to 10]"))
304 end,
305 sounds = mcl_sounds.node_sound_metal_defaults(),
307 _mcl_blast_resistance = 25,
308 _mcl_hardness = 5,
311 -- Mob spawner doll (rotating icon inside cage)
313 local doll_def = {
314 hp_max = 1,
315 physical = true,
316 collisionbox = {0,0,0,0,0,0},
317 visual = "mesh",
318 makes_footstep_sound = false,
319 timer = 0,
320 automatic_rotate = math.pi * 2.9,
322 _mob = default_mob, -- name of the mob this doll represents
325 doll_def.get_staticdata = function(self)
326 return self._mob
329 doll_def.on_activate = function(self, staticdata, dtime_s)
330 local mob = staticdata
331 if mob == "" or mob == nil then
332 mob = default_mob
334 set_doll_properties(self.object, mob)
335 self.object:setvelocity({x=0, y=0, z=0})
336 self.object:setacceleration({x=0, y=0, z=0})
337 self.object:set_armor_groups({immortal=1})
341 doll_def.on_step = function(self, dtime)
342 -- Check if spawner is still present. If not, delete the entity
343 self.timer = self.timer + 0.01
344 local n = minetest.get_node_or_nil(self.object:getpos())
345 if self.timer > 1 then
346 if n and n.name and n.name ~= "mcl_mobspawners:spawner" then
347 self.object:remove()
352 doll_def.on_punch = function(self, hitter) end
354 minetest.register_entity("mcl_mobspawners:doll", doll_def)