Lock picking puzzle added.

master
Nathan Salapat 2021-11-26 20:35:00 -06:00
parent e8e9d414d6
commit 4f357bbef8
22 changed files with 104 additions and 21 deletions

View File

@ -1,6 +1,5 @@
-- doors/init.lua
-- our API object
doors = {}
doors.registered_doors = {}

View File

@ -1,3 +1,6 @@
doors.player_picking_answer = {}
doors.lock_pick_timer = {}
minetest.register_craftitem('doors:lock_pick', {
description = 'Lock Picking Tool',
inventory_image = 'doors_lock_pick.png',
@ -10,41 +13,122 @@ minetest.register_craftitem('doors:lock_pick', {
if status >= 3 then
local name = user:get_player_name()
doors.player_config[name] = pos
minetest.show_formspec(name, 'doors:lock_picking', doors.lock_picking_formspec(status))
doors.pick_lock(user, status)
end
end,
on_drop = lobby.no_drop
})
function doors.lock_picking_formspec(dif)
local button_size = string.format("%.2f", (13/dif))
local bounds = 8-math.ceil(button_size)
local ran_x = math.random(0, bounds)
local ran_y = math.random(1, bounds)
for i = 0,9 do
minetest.register_craftitem('doors:key_wedge_'..i, {
description = 'Key Wedge',
inventory_image = 'doors_key_wedge_'..i..'.png',
stack_max = 1,
groups = {not_in_creative_inventory=1}
})
end
local wedges = {'doors:key_wedge_0', 'doors:key_wedge_1', 'doors:key_wedge_2', 'doors:key_wedge_3', 'doors:key_wedge_4', 'doors:key_wedge_5', 'doors:key_wedge_6', 'doors:key_wedge_7', 'doors:key_wedge_8', 'doors:key_wedge_9'}
local function shuffle_table(table)
for i = #table, 2 , -1 do
local j = math.random(i)
table[i], table[j] = table[j], table[i]
end
return table
end
function doors.pick_lock(player, difficulty)
local pname = player:get_player_name()
local player_attributes = player:get_meta()
local luck = player_attributes:get_int('luck')
local grid_inv = minetest.create_detached_inventory('lock_picking_wedges_'..pname, {
allow_take = function()
return -1
end,
allow_put = function()
return -1
end,
})
grid_inv:set_size('wedges', 10)
grid_inv:set_list('wedges', shuffle_table(wedges))
local solution_inv = minetest.create_detached_inventory('lock_picking_solution_'..pname, {})
solution_inv:set_size('pattern', 5)
local timer = 20-difficulty+luck
local sequence = math.random(11111, 99999)
doors.player_picking_answer[pname] = sequence
doors.lock_pick_timer[pname] = timer
doors.lock_picking_wrapper(player, timer)
end
function doors.lock_picking_wrapper(player, timer)
local pname = player:get_player_name()
local timer = doors.lock_pick_timer[pname] - 1
doors.lock_pick_timer[pname] = timer
if timer <= 0 then
minetest.close_formspec(pname, 'doors:lock_picking')
elseif timer > 0 then
minetest.show_formspec(pname, 'doors:lock_picking', doors.lock_picking_formspec(player, timer))
local looping = minetest.after(1, function()
doors.lock_picking_wrapper(player, timer)
end)
end
end
function doors.lock_picking_formspec(player, timer)
local pname = player:get_player_name()
local sequence = doors.player_picking_answer[pname]
local val1 = string.sub(sequence, 1, 1)
local val2 = string.sub(sequence, 2, 2)
local val3 = string.sub(sequence, 3, 3)
local val4 = string.sub(sequence, 4, 4)
local val5 = string.sub(sequence, 5, 5)
local formspec =
'formspec_version[3]'..
'size[8,8]'..
'textarea[.5,.5;7,1;;;Is breaking and entering your thing? If you don\'t break the lock is it technically breaking and entering???]'..
'textarea[.5,7.5;7,1;;;Picking locks is a shot in the dark, just like this.]'..
'image_button[0,0;8,8;tasks_blank.png;failure;;true;false;tasks_blank.png]'..
'image_button['..ran_x..','..ran_y..';'..button_size..','..button_size..';tasks_blank.png1;success;;true;false;tasks_blank.png]'
'textarea[.5,3;7,2;;;Is breaking and entering your thing? If you don\'t break the lock is it technically breaking and entering???]'..
'textarea[.25,.1;8,1;;;The timer is running, '..timer..' seconds remain.]'..
'list[detached:lock_picking_wedges_'..pname..';wedges;1,.5;5,2;]'..
'image[0.5,4.5;1,1;doors_key_profile_'..val1..'.png]'..
'image[1.5,4.5;1,1;doors_key_profile_'..val2..'.png]'..
'image[2.5,4.5;1,1;doors_key_profile_'..val3..'.png]'..
'image[3.5,4.5;1,1;doors_key_profile_'..val4..'.png]'..
'image[4.5,4.5;1,1;doors_key_profile_'..val5..'.png]'..
'button[5.75,4.5;2,1;check;Check]'..
'list[detached:lock_picking_solution_'..pname..';pattern;1,6;5,1;]'..
'listring[]'
return formspec
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if formname == 'doors:lock_picking' then
if fields.success then
local pos = doors.player_config[name]
minetest.chat_send_player(name, 'nice work!')
minetest.close_formspec(name, 'doors:lock_picking')
doors.door_toggle(pos, nil, player, true)
minetest.after(3, function()
if fields.check then
local inv = minetest.get_inventory({type = 'detached', name = 'lock_picking_solution_'..name})
local slot1 = inv:get_stack('pattern', 1)
local key1 = string.sub(slot1:get_name(), -1)
local slot2 = inv:get_stack('pattern', 2)
local key2 = string.sub(slot2:get_name(), -1)
local slot3 = inv:get_stack('pattern', 3)
local key3 = string.sub(slot3:get_name(), -1)
local slot4 = inv:get_stack('pattern', 4)
local key4 = string.sub(slot4:get_name(), -1)
local slot5 = inv:get_stack('pattern', 5)
local key5 = string.sub(slot5:get_name(), -1)
local solution = tonumber(key1..key2..key3..key4..key5)
local answer = doors.player_picking_answer[name]
if solution == answer then
local pos = doors.player_config[name]
minetest.chat_send_player(name, 'nice work!')
doors.lock_pick_timer[name] = 0
minetest.close_formspec(name, 'doors:lock_picking')
doors.door_toggle(pos, nil, player, true)
end)
elseif fields.failure then
minetest.chat_send_player(name, 'Try again!')
minetest.close_formspec(name, 'doors:lock_picking')
minetest.after(3, function()
doors.door_toggle(pos, nil, player, true)
end)
else
minetest.chat_send_player(name, 'Try again!')
end
end
end
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB