Skip to content

Commit

Permalink
Global code style cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbith committed Jul 23, 2019
1 parent f0860cb commit 61d9da6
Show file tree
Hide file tree
Showing 17 changed files with 614 additions and 404 deletions.
5 changes: 4 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ read_globals = {
"default",
"stairs", "doors", "xpanes",
"xdecor", "xbg",
table = { fields = { "copy" } },
table = {fields = {"copy"}},
string = {fields = {"split"}},
"unpack",
"stairsplus",
"mesecon"
}
20 changes: 13 additions & 7 deletions handlers/animations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ function xdecor.sit(pos, node, clicker, pointed_thing)
if default.player_attached[player_name] then
pos.y = pos.y - 0.5
clicker:setpos(pos)
clicker:set_eye_offset({x=0, y=0, z=0}, {x=0, y=0, z=0})
clicker:set_eye_offset(vector.new(), vector.new())
clicker:set_physics_override({speed = 1, jump = 1, gravity = 1})
default.player_attached[player_name] = false
default.player_set_animation(clicker, "stand", 30)

elseif not default.player_attached[player_name] and node.param2 <= 3 and
not ctrl.sneak and vector.equals(vel, {x=0,y=0,z=0}) then
not ctrl.sneak and vector.equals(vel, vector.new()) then

clicker:set_eye_offset({x=0, y=-7, z=2}, {x=0, y=0, z=0})
clicker:set_eye_offset({x = 0, y = -7, z = 2}, vector.new())
clicker:set_physics_override({speed = 0, jump = 0, gravity = 1})
clicker:setpos(pos)
default.player_attached[player_name] = true
default.player_set_animation(clicker, "sit", 30)

if node.param2 == 0 then clicker:set_look_yaw(3.15)
elseif node.param2 == 1 then clicker:set_look_yaw(7.9)
elseif node.param2 == 2 then clicker:set_look_yaw(6.28)
elseif node.param2 == 3 then clicker:set_look_yaw(4.75) end
if node.param2 == 0 then
clicker:set_look_yaw(3.15)
elseif node.param2 == 1 then
clicker:set_look_yaw(7.9)
elseif node.param2 == 2 then
clicker:set_look_yaw(6.28)
elseif node.param2 == 3 then
clicker:set_look_yaw(4.75)
end
end
end

Expand All @@ -47,6 +52,7 @@ function xdecor.sit_dig(pos, digger)
return false
end
end

return true
end

17 changes: 14 additions & 3 deletions handlers/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@
function xdecor.maxn(T)
local n = 0
for k in pairs(T) do
if k > n then n = k end
if k > n then
n = k
end
end

return n
end

-- Returns the length of an hash table.
function xdecor.tablelen(T)
local n = 0
for _ in pairs(T) do n = n + 1 end

for _ in pairs(T) do
n = n + 1
end

return n
end

-- Deep copy of a table. Borrowed from mesecons mod (https://github.com/Jeija/minetest-mod-mesecons).
function xdecor.tablecopy(T)
if type(T) ~= "table" then return T end -- No need to copy.
if type(T) ~= "table" then
return T -- No need to copy.
end

local new = {}

for k, v in pairs(T) do
Expand All @@ -26,6 +36,7 @@ function xdecor.tablecopy(T)
new[k] = v
end
end

return new
end

Expand Down
33 changes: 21 additions & 12 deletions handlers/nodeboxes.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
xdecor.box = {
slab_y = function(height, shift)
return {-0.5, -0.5 + (shift or 0), -0.5, 0.5, -0.5 + height +
(shift or 0), 0.5}
return {
-0.5,
-0.5 + (shift or 0),
-0.5,
0.5,
-0.5 + height + (shift or 0),
0.5
}
end,
slab_z = function(depth)
return {-0.5, -0.5, -0.5 + depth, 0.5, 0.5, 0.5}
Expand All @@ -15,16 +21,18 @@ xdecor.box = {
}

xdecor.nodebox = {
regular = {type="regular"},
null = {type="fixed", fixed={0,0,0,0,0,0}}
regular = {type = "regular"},
null = {
type = "fixed", fixed = {0,0,0,0,0,0}
}
}

xdecor.pixelbox = function(size, boxes)
local fixed = {}
for _, box in pairs(boxes) do
for _, box in ipairs(boxes) do
-- `unpack` has been changed to `table.unpack` in newest Lua versions.
local x, y, z, w, h, l = unpack(box)
fixed[#fixed+1] = {
fixed[#fixed + 1] = {
(x / size) - 0.5,
(y / size) - 0.5,
(z / size) - 0.5,
Expand All @@ -33,26 +41,27 @@ xdecor.pixelbox = function(size, boxes)
((z + l) / size) - 0.5
}
end
return {type="fixed", fixed=fixed}

return {type = "fixed", fixed = fixed}
end

local mt = {}

mt.__index = function(table, key)
local ref = xdecor.box[key]
local ref_type = type(ref)

if ref_type == "function" then
return function(...)
return {type="fixed", fixed=ref(...)}
return {type = "fixed", fixed = ref(...)}
end
elseif ref_type == "table" then
return {type="fixed", fixed=ref}
return {type = "fixed", fixed = ref}
elseif ref_type == "nil" then
error(key.."could not be found among nodebox presets and functions")
error(key .. "could not be found among nodebox presets and functions")
end

error("unexpected datatype "..tostring(type(ref)).." while looking for "..key)
error("unexpected datatype " .. tostring(type(ref)) .. " while looking for " .. key)
end

setmetatable(xdecor.nodebox, mt)

42 changes: 23 additions & 19 deletions handlers/registration.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
local default_inventory_size = 32

local default_inventory_formspecs = {
["8"] = [[ size[8,6]
list[context;main;0,0;8,1;]
list[current_player;main;0,2;8,4;]
listring[current_player;main]
listring[context;main] ]]
..default.get_hotbar_bg(0,2),
listring[context;main] ]] ..
default.get_hotbar_bg(0,2),

["16"] = [[ size[8,7]
list[context;main;0,0;8,2;]
list[current_player;main;0,3;8,4;]
listring[current_player;main]
listring[context;main] ]]
..default.get_hotbar_bg(0,3),
listring[context;main] ]] ..
default.get_hotbar_bg(0,3),

["24"] = [[ size[8,8]
list[context;main;0,0;8,3;]
list[current_player;main;0,4;8,4;]
listring[current_player;main]
listring[context;main]" ]]
..default.get_hotbar_bg(0,4),
listring[context;main]" ]] ..
default.get_hotbar_bg(0,4),

["32"] = [[ size[8,9]
list[context;main;0,0.3;8,4;]
list[current_player;main;0,4.85;8,1;]
list[current_player;main;0,6.08;8,3;8]
listring[current_player;main]
listring[context;main] ]]
..default.get_hotbar_bg(0,4.85)
listring[context;main] ]] ..
default.get_hotbar_bg(0,4.85)
}

local function get_formspec_by_size(size)
Expand All @@ -42,13 +42,13 @@ local default_can_dig = function(pos)
return inv:is_empty("main")
end

function xdecor.register(name, def)
local function xdecor_stairs_alternative(nodename, def)
local function xdecor_stairs_alternative(nodename, def)
local mod, name = nodename:match("(.*):(.*)")

for groupname, value in pairs(def.groups) do
if groupname ~= "cracky" and groupname ~= "choppy" and
groupname ~= "flammable" and groupname ~= "crumbly" and
groupname ~= "snappy" then
groupname ~= "snappy" then
def.groups.groupname = nil
end
end
Expand All @@ -65,17 +65,18 @@ function xdecor.register(name, def)
sounds = def.sounds,
}
)
elseif minetest.get_modpath("stairs") then
elseif minetest.get_modpath("stairs") then
stairs.register_stair_and_slab(name,nodename,
def.groups,
def.tiles,
("%s Stair"):format(def.description),
("%s Slab"):format(def.description),
def.sounds
)
end
)
end
end

function xdecor.register(name, def)
def.drawtype = def.drawtype or (def.mesh and "mesh") or (def.node_box and "nodebox")
def.sounds = def.sounds or default.node_sound_defaults()

Expand Down Expand Up @@ -108,24 +109,27 @@ function xdecor.register(name, def)

local size = inventory.size or default_inventory_size
local inv = meta:get_inventory()

inv:set_size("main", size)
meta:set_string("formspec", (inventory.formspec or
get_formspec_by_size(size))..xbg)
meta:set_string("formspec",
(inventory.formspec or get_formspec_by_size(size)) .. xbg)
end

def.can_dig = def.can_dig or default_can_dig

elseif infotext and not def.on_construct then
def.on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", infotext)
end
end

minetest.register_node("xdecor:"..name, def)
minetest.register_node("xdecor:" .. name, def)

local workbench = minetest.settings:get_bool("enable_xdecor_workbench")

if workbench == false and
(minetest.get_modpath("moreblocks") or minetest.get_modpath("stairs")) then
(minetest.get_modpath("moreblocks") or minetest.get_modpath("stairs")) then
if xdecor.stairs_valid_def(def) then
xdecor_stairs_alternative("xdecor:"..name, def)
end
Expand Down
34 changes: 10 additions & 24 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
--local t = os.clock()

local mver_major, mver_minor, mver_patch = 0, 4, 16 -- Minetest 0.4.16 minimum.

local client_version = minetest.get_version().string
local major, minor, patch = client_version:match("(%d+).(%d+).(%d+)")

if (major and minor and patch) and
((tonumber(major) < mver_major) or
(mver_major == tonumber(major) and tonumber(minor) < mver_minor) or
(mver_minor == tonumber(minor) and tonumber(patch) < mver_patch)) then
minetest.log("error", "[xdecor] Your Minetest client is too old to run this mod. Disabling.")
return
end

xdecor = {}
local modpath = minetest.get_modpath("xdecor")

dofile(modpath.."/handlers/animations.lua")
dofile(modpath.."/handlers/helpers.lua")
dofile(modpath.."/handlers/nodeboxes.lua")
dofile(modpath.."/handlers/registration.lua")
dofile(modpath .. "/handlers/animations.lua")
dofile(modpath .. "/handlers/helpers.lua")
dofile(modpath .. "/handlers/nodeboxes.lua")
dofile(modpath .. "/handlers/registration.lua")

dofile(modpath.."/src/alias.lua")
dofile(modpath.."/src/nodes.lua")
dofile(modpath.."/src/recipes.lua")
dofile(modpath .. "/src/nodes.lua")
dofile(modpath .. "/src/recipes.lua")

local subpart = {
"chess",
Expand All @@ -34,13 +20,13 @@ local subpart = {
"mailbox",
"mechanisms",
"rope",
"workbench"
"workbench",
}

for _, name in pairs(subpart) do
local enable = minetest.settings:get_bool("enable_xdecor_"..name)
for _, name in ipairs(subpart) do
local enable = minetest.settings:get_bool("enable_xdecor_" .. name)
if enable or enable == nil then
dofile(modpath.."/src/"..name..".lua")
dofile(modpath .. "/src/" .. name .. ".lua")
end
end

Expand Down
1 change: 0 additions & 1 deletion src/alias.lua

This file was deleted.

2 changes: 2 additions & 0 deletions src/chess.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ local function index_to_xy(idx)
if not idx then
return nil
end

idx = idx - 1

local x = idx % 8
local y = (idx - x) / 8

Expand Down
Loading

0 comments on commit 61d9da6

Please sign in to comment.