tweak and tidy code

This commit is contained in:
TenPlus1 2018-10-05 18:39:29 +01:00
parent 7705d08e33
commit f6c4b9d693
2 changed files with 343 additions and 278 deletions

219
init.lua

@ -80,7 +80,9 @@ local lucky_list = {
{"sch", "wishingwell", 0, true}, {"sch", "wishingwell", 0, true},
{"cus", bushy}, {"cus", bushy},
{"cus", punchy}, {"cus", punchy},
{"fal", {"default:wood", "default:gravel", "default:sand", "default:desert_sand", "default:stone", "default:dirt", "default:goldblock"}, 0}, {"fal", {"default:wood", "default:gravel", "default:sand",
"default:desert_sand", "default:stone", "default:dirt",
"default:goldblock"}, 0},
{"lig"}, {"lig"},
{"nod", "lucky_block:super_lucky_block", 0}, {"nod", "lucky_block:super_lucky_block", 0},
{"exp", 2}, {"exp", 2},
@ -153,10 +155,7 @@ end
local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow) local effect = function(pos, amount, texture, min_size, max_size, radius, gravity, glow)
radius = radius or 2 radius = radius or 2
min_size = min_size or 0.5
max_size = max_size or 1
gravity = gravity or -10 gravity = gravity or -10
glow = glow or 0
minetest.add_particlespawner({ minetest.add_particlespawner({
amount = amount, amount = amount,
@ -169,10 +168,10 @@ local effect = function(pos, amount, texture, min_size, max_size, radius, gravit
maxacc = {x = 0, y = gravity, z = 0}, maxacc = {x = 0, y = gravity, z = 0},
minexptime = 0.1, minexptime = 0.1,
maxexptime = 1, maxexptime = 1,
minsize = min_size, minsize = min_size or 0.5,
maxsize = max_size, maxsize = max_size or 1.0,
texture = texture, texture = texture,
glow = glow, glow = glow or 0,
}) })
end end
@ -212,6 +211,7 @@ local function entity_physics(pos, radius)
obj_pos = objs[n]:get_pos() obj_pos = objs[n]:get_pos()
dist = vector.distance(pos, obj_pos) dist = vector.distance(pos, obj_pos)
if dist < 1 then dist = 1 end if dist < 1 then dist = 1 end
local damage = math.floor((4 / dist) * radius) local damage = math.floor((4 / dist) * radius)
@ -240,11 +240,15 @@ local function fill_chest(pos, items)
inv:set_size("main", 8 * 4) inv:set_size("main", 8 * 4)
for i = 0, 2, 1 do for i = 0, 2, 1 do
local stuff = chest_stuff[math.random(1, #chest_stuff)] local stuff = chest_stuff[math.random(1, #chest_stuff)]
table.insert(stacks, {name = stuff.name, count = math.random(1, stuff.max)})
table.insert(stacks, {
name = stuff.name, count = math.random(1, stuff.max)})
end end
for s = 1, #stacks do for s = 1, #stacks do
if not inv:contains_item("main", stacks[s]) then if not inv:contains_item("main", stacks[s]) then
inv:set_stack("main", math.random(1, 32), stacks[s]) inv:set_stack("main", math.random(1, 32), stacks[s])
end end
@ -276,30 +280,17 @@ local function explode(pos, radius, sound)
end end
-- this is what happens when you dig a lucky block local lb_schematic = function(pos, digger, def)
local lucky_block = function(pos, digger)
-- make sure it's really random
math.randomseed(os.time() + minetest.get_timeofday())
local luck = math.random(1, #lucky_list) ; -- luck = 1
local action = lucky_list[luck][1]
--local schem
-- print ("luck ["..luck.." of "..#lucky_list.."]", action)
-- place schematic
if action == "sch" then
if #lucky_schems == 0 then if #lucky_schems == 0 then
print ("[lucky block] No schematics") print ("[lucky block] No schematics")
return return
end end
local schem = lucky_list[luck][2] local schem = def[2]
local switch = lucky_list[luck][3] or 0 local switch = def[3] or 0
local force = lucky_list[luck][4] local force = def[4]
local reps = lucky_list[luck][5] or {} local reps = def[5] or {}
if switch == 1 then if switch == 1 then
pos = vector.round(digger:get_pos()) pos = vector.round(digger:get_pos())
@ -320,20 +311,20 @@ local lucky_block = function(pos, digger)
if switch == 1 then if switch == 1 then
digger:set_pos(pos, false) digger:set_pos(pos, false)
end end
end
-- place node (if chest then fill chest)
elseif action == "nod" then
local nod = lucky_list[luck][2] local lb_node = function(pos, digger, def)
local switch = lucky_list[luck][3]
local items = lucky_list[luck][4] local nod = def[2]
local switch = def[3]
local items = def[4]
if switch == 1 then if switch == 1 then
pos = digger:get_pos() pos = digger:get_pos()
end end
if not minetest.registered_nodes[nod] then if not minetest.registered_nodes[nod] then
print (nod)
nod = "default:goldblock" nod = "default:goldblock"
end end
@ -344,16 +335,17 @@ local lucky_block = function(pos, digger)
if nod == "default:chest" then if nod == "default:chest" then
fill_chest(pos, items) fill_chest(pos, items)
end end
end
-- place entity
elseif action == "spw" then local lb_spawn = function(pos, digger, def)
local pos2 = {} local pos2 = {}
local num = lucky_list[luck][3] or 1 local num = def[3] or 1
local tame = lucky_list[luck][4] local tame = def[4]
local own = lucky_list[luck][5] local own = def[5]
local range = lucky_list[luck][6] or 5 local range = def[6] or 5
local name = lucky_list[luck][7] local name = def[7]
for i = 1, num do for i = 1, num do
@ -362,11 +354,11 @@ local lucky_block = function(pos, digger)
pos2.z = pos.z + math.random(-range, range) pos2.z = pos.z + math.random(-range, range)
local nod = minetest.get_node(pos2) local nod = minetest.get_node(pos2)
local def = minetest.registered_nodes[nod.name] local nodef = minetest.registered_nodes[nod.name]
if def and def.walkable == false then if nodef and nodef.walkable == false then
local entity = lucky_list[luck][2] local entity = def[2]
-- coloured sheep -- coloured sheep
if entity == "mobs:sheep" then if entity == "mobs:sheep" then
@ -399,20 +391,22 @@ local lucky_block = function(pos, digger)
end end
end end
end end
end
-- explosion
elseif action == "exp" then
local rad = lucky_list[luck][2] or 2 local lb_explode = function(pos, def)
local snd = lucky_list[luck][3] or "tnt_explode"
local rad = def[2] or 2
local snd = def[3] or "tnt_explode"
explode(pos, rad, snd) explode(pos, rad, snd)
end
-- teleport
elseif action == "tel" then
local xz_range = lucky_list[luck][2] or 10 local lb_teleport = function(pos, digger, def)
local y_range = lucky_list[luck][3] or 5
local xz_range = def[2] or 10
local y_range = def[3] or 5
pos.x = pos.x + math.random(-xz_range, xz_range) pos.x = pos.x + math.random(-xz_range, xz_range)
pos.x = pos.y + math.random(-y_range, y_range) pos.x = pos.y + math.random(-y_range, y_range)
@ -426,20 +420,21 @@ local lucky_block = function(pos, digger)
minetest.chat_send_player(digger:get_player_name(), minetest.chat_send_player(digger:get_player_name(),
green .. S("Random Teleport!")) green .. S("Random Teleport!"))
end
-- drop items
elseif action == "dro" then
local num = lucky_list[luck][3] or 1 local lb_drop = function(pos, digger, def)
local colours = lucky_list[luck][4]
local items = #lucky_list[luck][2] local num = def[3] or 1
local colours = def[4]
local items = #def[2]
-- drop multiple different items or colours -- drop multiple different items or colours
if items > 1 or colours then if items > 1 or colours then
for i = 1, num do for i = 1, num do
local item = lucky_list[luck][2][math.random(1, items)] local item = def[2][math.random(1, items)]
if colours then if colours then
item = item .. all_colours[math.random(#all_colours)] item = item .. all_colours[math.random(#all_colours)]
@ -463,7 +458,8 @@ local lucky_block = function(pos, digger)
else -- drop single item in a stack else -- drop single item in a stack
local item = lucky_list[luck][2][1] local item = def[2][1]
if not minetest.registered_items[item] then if not minetest.registered_items[item] then
item = ItemStack("default:coal_lump " .. tonumber(num)) item = ItemStack("default:coal_lump " .. tonumber(num))
else else
@ -481,11 +477,12 @@ local lucky_block = function(pos, digger)
}) })
end end
end end
end
-- lightning strike
elseif action == "lig" then
local nod = lucky_list[luck][2] or "fire:basic_flame" local lb_lightning = function(pos, digger, def)
local nod = def[2]
if not minetest.registered_nodes[nod] then if not minetest.registered_nodes[nod] then
nod = "fire:basic_flame" nod = "fire:basic_flame"
@ -515,14 +512,15 @@ local lucky_block = function(pos, digger)
gain = 1.0, gain = 1.0,
max_hear_distance = 25 max_hear_distance = 25
}) })
end
-- falling nodes
elseif action == "fal" then
local nods = lucky_list[luck][2] local lb_falling = function(pos, digger, def)
local switch = lucky_list[luck][3]
local spread = lucky_list[luck][4] local nods = def[2]
local range = lucky_list[luck][5] or 5 local switch = def[3]
local spread = def[4]
local range = def[5] or 5
if switch == 1 then if switch == 1 then
pos = digger:get_pos() pos = digger:get_pos()
@ -557,17 +555,19 @@ local lucky_block = function(pos, digger)
end end
end) end)
end end
end
-- troll block, disappears or explodes after 2 seconds
elseif action == "tro" then
local nod = lucky_list[luck][2] local lb_troll = function(pos, def)
local snd = lucky_list[luck][3]
local exp = lucky_list[luck][4] local nod = def[2]
local snd = def[3]
local exp = def[4]
minetest.set_node(pos, {name = nod}) minetest.set_node(pos, {name = nod})
if snd then if snd then
minetest.sound_play(snd, { minetest.sound_play(snd, {
pos = pos, pos = pos,
gain = 1.0, gain = 1.0,
@ -576,7 +576,6 @@ local lucky_block = function(pos, digger)
end end
if not minetest.registered_nodes[nod] then if not minetest.registered_nodes[nod] then
print (nod)
nod = "default:goldblock" nod = "default:goldblock"
end end
@ -598,13 +597,14 @@ local lucky_block = function(pos, digger)
}) })
end end
end) end)
end
-- floor paint
elseif action == "flo" then
local size = lucky_list[luck][2] or 1 local lb_floor = function(pos, def)
local nods = lucky_list[luck][3] or {"default:dirt"}
local offs = lucky_list[luck][4] or 0 local size = def[2] or 1
local nods = def[3] or {"default:dirt"}
local offs = def[4] or 0
local num = 1 local num = 1
for x = 0, size - 1 do for x = 0, size - 1 do
@ -628,6 +628,69 @@ local lucky_block = function(pos, digger)
num = num + 1 num = num + 1
end end
end end
end
-- this is what happens when you dig a lucky block
local lucky_block = function(pos, digger)
-- make sure it's really random
math.randomseed(minetest.get_timeofday() + pos.x + pos.z) -- os.time()
local luck = math.random(1, #lucky_list) ; -- luck = 1
local action = lucky_list[luck][1]
-- print ("luck ["..luck.." of "..#lucky_list.."]", action)
-- place schematic
if action == "sch" then
lb_schematic(pos, digger, lucky_list[luck])
-- place node (if chest then fill chest)
elseif action == "nod" then
lb_node(pos, digger, lucky_list[luck])
-- place entity
elseif action == "spw" then
lb_spawn(pos, digger, lucky_list[luck])
-- explosion
elseif action == "exp" then
lb_explode(pos, lucky_list[luck])
-- teleport
elseif action == "tel" then
lb_teleport(pos, digger, lucky_list[luck])
-- drop items
elseif action == "dro" then
lb_drop(pos, digger, lucky_list[luck])
-- lightning strike
elseif action == "lig" then
lb_lightning(pos, digger, lucky_list[luck])
-- falling nodes
elseif action == "fal" then
lb_falling(pos, digger, lucky_list[luck])
-- troll block, disappears or explodes after 2 seconds
elseif action == "tro" then
lb_troll(pos, lucky_list[luck])
-- floor paint
elseif action == "flo" then
lb_floor(pos, lucky_list[luck])
-- custom function -- custom function
elseif action == "cus" then elseif action == "cus" then
@ -699,7 +762,9 @@ minetest.register_node('lucky_block:super_lucky_block', {
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Super Lucky Block") meta:set_string("infotext", "Super Lucky Block")
end, end,