Update init.lua

master
Aftermoth 2016-05-12 20:12:15 +12:00
parent 2e66e304c3
commit 71848cb8cd
1 changed files with 48 additions and 70 deletions

118
init.lua
View File

@ -11,7 +11,6 @@ http://www.gnu.org/licenses/lgpl-2.1.html
--]]
nudger = {}
-- ======== FYI ========
@ -41,32 +40,19 @@ local menu1 = {
"Store a node's rotation.",
"Equip stored rotation.",
"Toggle chat level.",
"Buy nudgiquin for 20% tool wear.", -- "i" for less ambiguous pronunciation.
"Buy nudgiquin for 20% tool wear.",
"",
"",
"Nudge to stored rotation.",
"Store ...",
"",
}
local infos = {
"Reversed.",
"Stored.",
"* Apply mode is active *",
"More chat.",
"Less chat.",
"Shift+click to cycle options.",
"Right click to toggle menu.",
"Sorry, this nudger's too damaged.",
"Nudgiquin can be nudged freely with no tool damage.",
}
-- ======== Helpers ========
local function say(msg, plr)
local name
if minetest.is_singleplayer() then name = "singleplayer" else name = plr:get_player_name() end
minetest.chat_send_player(name, msg)
minetest.chat_send_player(plr:get_player_name(), msg)
end
local function edit_ok(pos, plr)
@ -135,10 +121,8 @@ local function choose_axis(plr,ptd,mode)
else
local hdir = (5 - math.floor(plr:get_look_yaw()/1.571+.5))%4
if mode == 2 then return axsgn((hdir+3)%4)
elseif sign == 1 then
return axsgn(hdir)
else
return axsgn((hdir+2)%4)
elseif sign == 1 then return axsgn(hdir)
else return axsgn((hdir+2)%4)
end
end
@ -167,38 +151,38 @@ local map = {
local function rotate(istk,plr,ptd,mode,sgn)
local pos = ptd.under
local node = node_ok(pos)
if not node then return end
if not node then return istk end
local p = (node.param2)%24
local q, r = math.floor(p/4), p%4
local a, s = choose_axis(plr,ptd,mode)
local t = map[a+1]
local q, r = math.floor(p/4), p%4
local k = t[1][q+1]
s = s*sgn
if k == -1 then p = t[4] + (p+4+s)%4
elseif k == -2 then p = t[5] - (t[5]-p+3+s)%4 - 1
if k == -1 then p = t[4] + (p + 4 + s)%4
elseif k == -2 then p = t[5] - (t[5] - p + 3 + s)%4 - 1
else
local o, i = t[3], (k+4+s)%4+1
local o, i = t[3], (k + 4 + s)%4 + 1
p = t[2][i] + (r + o[k+1] + 4 - o[i])%4
end
node.param2 = p
minetest.swap_node(pos, node)
return tool_use(istk, 1, pos)
return tool_use(istk, 1, pos), p
end
-- == Store / Apply ==
local function store(istk,pos)
local node = node_ok(pos)
if not node then return end
if not node then return istk end
return tool_use(istk, 4, pos), node.param2
end
local function apply(istk,pos,p2)
local node = node_ok(pos)
if not node then return end
if not node then return istk end
node.param2 = p2
minetest.swap_node(pos, node)
return tool_use(istk, 1, pos)
return tool_use(istk, 1, pos), p2
end
@ -206,7 +190,7 @@ end
local function shop(istk,plr)
if tonumber(istk:get_wear()) > 52428 then
say(infos[8],plr)
say("Sorry, this nudger is too damaged.",plr)
else
tool_use(istk,40,nil)
local inv = plr:get_inventory()
@ -214,13 +198,13 @@ local function shop(istk,plr)
if not stk:is_empty() then
minetest.item_drop(stk,plr,plr:get_pos())
end
say(infos[9],plr)
say("Nudgiquin can be nudged without tool damage.",plr)
end
end
local function reset(istk, plr)
say(infos[6], plr)
say(infos[7], plr)
say("Shift+click to cycle options.", plr)
say("Right click to switch menus.", plr)
istk:set_name("nudger:nudger0")
return 0
end
@ -228,21 +212,20 @@ end
local function do_on_use(istk, plr, ptd)
local m = tonumber(istk:get_metadata())
if not m then m = reset(istk, plr) end
-- unpack metadata
-- unpack metadata
local p2 = math.floor(m/120)
local sh = math.floor((m%120)/60)
local mm = math.floor((m%60)/6)
local rm = m%6
local sr, dr = math.floor(rm/3), rm%3
-- options
-- menu options
if plr:get_player_control().sneak then
if mm == 0 then -- change axis
dr = (dr + 1)%3
rm = 3*sr + dr
elseif mm < 6 then -- main submenu
if mm == 0 then
rm = 3*sr + (dr + 1)%3
elseif mm < 6 then
mm = mm%5 + 1
elseif mm < 9 then -- apply submenu
mm = 15 - mm
elseif mm < 10 then
mm = 17 - mm
end
if mm == 0 then
@ -250,58 +233,54 @@ local function do_on_use(istk, plr, ptd)
istk:set_name("nudger:nudger"..rm)
else
say(menu1[mm], plr)
if mm == 2 or mm == 8 then istk:set_name("nudger:nudger6")
if mm == 2 or mm == 9 then istk:set_name("nudger:nudger6")
elseif mm == 3 then istk:set_name("nudger:nudger")
elseif mm == 7 then istk:set_name("nudger:nudger7")
elseif mm == 8 then istk:set_name("nudger:nudger7")
end
end
-- actions
-- menu actions
else
local tmp
local once = true
if mm == 1 then -- change sign
sr = 1 - sr
rm = 3*sr + dr
if sh==0 then say(infos[1], plr) end
elseif mm == 4 then -- toggle chat
if mm == 1 then
rm = 3*(1 - sr) + dr
if sh==0 then say("Reversed.", plr) end
elseif mm == 4 then
sh = 1 - sh
say(infos[4+sh], plr)
-- 5 help
elseif mm == 5 then -- buy nudgiquin
say(((sh==0 and "More") or "Less").." chat.", plr)
elseif mm == 5 then
shop(istk,plr)
else
--
once = false
end
local pos = (ptd.type == "node" and ptd.under) or false
local qp2
if pos and not once then
if mm == 0 then
if edit_ok(pos,plr) then
istk = rotate(istk,plr,ptd,dr,1-2*sr)
istk, qp2 = rotate(istk,plr,ptd,dr,1-2*sr)
end
elseif mm == 2 or mm == 3 or mm == 8 then
if mm == 2 or mm == 8 then --store
istk, tmp = store(istk,pos)
if tmp then p2 = tmp end
if sh==0 then say(infos[2], plr) end
elseif mm == 2 or mm == 3 or mm == 9 then
if mm == 2 or mm == 9 then
istk, qp2 = store(istk,pos)
if qp2 then p2 = qp2 end
if sh==0 then say("Stored.", plr) end
end
-- switch to apply mode and menu
if sh==0 and not mm == 8 then say(infos[3], plr) end
mm = 7
mm = 8
istk:set_name("nudger:nudger7")
say(menu1[7], plr)
elseif mm == 7 then -- apply
say(menu1[8], plr)
elseif mm == 8 then
if edit_ok(pos,plr) then
istk = apply(istk,pos,p2)
istk, qp2 = apply(istk,pos,p2)
end
end
end
if qp2 and sh==0 and minetest.get_node_or_nil(pos).name == "nudger:nudgiquin" then say("nq: "..qp2, plr) end
if once then
mm = 0
istk:set_name("nudger:nudger"..rm)
end
end
istk:set_metadata(120*p2 + 60*sh + 6*mm + rm)
return istk
@ -324,7 +303,6 @@ local function do_on_place(istk, plr)
end
m = 60*mh + 6*mm + rm
end
istk:set_metadata(m)
return istk
end