1
0
mirror of https://github.com/MinetestForFun/fishing.git synced 2024-11-13 01:20:19 +01:00

continue rewriting

refactor code
add formspec menu config
This commit is contained in:
crabman77 2015-06-17 02:18:12 +02:00
parent 03e2321a22
commit b4fb3c3b17
7 changed files with 203 additions and 150 deletions

View File

@ -106,6 +106,13 @@ local FISHING_BOBBER_ENTITY={
local pos = self.object:getpos()
--remove if no owner, no player, owner no in bobber_view_range
if self.owner == nil then self.object:remove(); return end
--remove if not node water
local node = minetest.get_node_or_nil({x=pos.x, y=pos.y-0.5, z=pos.z})
if not node or string.find(node.name, "water_source") == nil then
minetest.chat_send_player(self.owner, "Haha, Fishing is prohibited outside water!")
self.object:remove()
return
end
local player = minetest.get_player_by_name(self.owner)
if not player then self.object:remove(); return end
local p = player:getpos()
@ -150,7 +157,7 @@ local FISHING_BOBBER_ENTITY={
end
self.randomtime = math.random(1,5)*10
if math.random(1, 100) <= fishing_setting.settings.chance["fish"] then
if math.random(1, 100) <= fishing_setting.settings["fish_chance"] then
self.prize = fishing_setting.prizes["fish"][math.random(1,#fishing_setting.prizes["fish"])]
else
if math.random(1, 100) <= 10 then

View File

@ -11,62 +11,66 @@ function fishing_setting.func.save()
end
function fishing_setting.func.set_settings(new_settings, settings)
if settings["message"] ~= nil then
new_settings["message"] = settings["message"]
end
if settings["worm_is_mob"] ~= nil then
new_settings["worm_is_mob"] = settings["worm_is_mob"]
end
if settings["worm_chance"] ~= nil then
new_settings["worm_chance"] = settings["worm_chance"]
end
if settings["new_worm_source"] ~= nil then
new_settings["new_worm_source"] = settings["new_worm_source"]
end
if settings["wear_out"] ~= nil then
new_settings["wear_out"] = settings["wear_out"]
end
if settings["bobber_view_range"] ~= nil then
new_settings["bobber_view_range"] = settings["bobber_view_range"]
end
if settings["simple_deco_fishing_pole"] ~= nil then
new_settings["simple_deco_fishing_pole"] = settings["simple_deco_fishing_pole"]
end
if settings["fish_chance"] ~= nil then
new_settings["fish_chance"] = settings["fish_chance"]
end
if settings["tresor_chance"] ~= nil then
new_settings["tresor_chance"] = settings["tresor_chance"]
end
if settings["shark_chance"] ~= nil then
new_settings["shark_chance"] = settings["shark_chance"]
end
if settings["tresor_timer"] ~= nil then
new_settings["tresor_timer"] = settings["tresor_timer"]
end
if settings["tresor_random_enable"] ~= nil then
new_settings["tresor_random_enable"] = settings["tresor_random_enable"]
end
end
--function load settings from file
function fishing_setting.func.load()
local file = io.open(fishing_setting.file, "r")
local settings = {}
if file then
settings = minetest.deserialize(file:read("*all"))
file:close()
else
return
end
if settings == nil or type(settings) ~= "table" then return end
if settings["message"] ~= nil then
fishing_setting.settings["message"] = settings["message"]
end
if settings["worm_is_mob"] ~= nil then
fishing_setting.settings["worm_is_mob"] = settings["worm_is_mob"]
end
if settings["worm_chance"] ~= nil then
fishing_setting.settings["worm_chance"] = settings["worm_chance"]
end
if settings["new_worm_source"] ~= nil then
fishing_setting.settings["new_worm_source"] = settings["new_worm_source"]
end
if settings["wear_out"] ~= nil then
fishing_setting.settings["wear_out"] = settings["wear_out"]
end
if settings["bobber_view_range"] ~= nil then
fishing_setting.settings["bobber_view_range"] = settings["bobber_view_range"]
end
if settings["simple_deco_fishing_pole"] ~= nil then
fishing_setting.settings["simple_deco_fishing_pole"] = settings["simple_deco_fishing_pole"]
end
if settings.chance["fish"] ~= nil then
fishing_setting.settings.chance["fish"] = settings.chance["fish"]
end
if settings.chance["tresor"] ~= nil then
fishing_setting.settings.chance["tresor"] = settings.chance["tresor"]
end
if settings.chance["shark"] ~= nil then
fishing_setting.settings.chance["shark"] = settings.chance["shark"]
end
if settings["tresor_timer"] ~= nil then
fishing_setting.settings["tresor_timer"] = settings["tresor_timer"]
end
if settings["tresor_random_enable"] ~= nil then
fishing_setting.settings["tresor_random_enable"] = settings["tresor_random_enable"]
if settings and type(settings) == "table" then
fishing_setting.func.set_settings(fishing_setting.settings, settings)
end
end
end
@ -162,114 +166,157 @@ end
--Menu fishing configuration
local formspec = "size[11,10]"..
"label[4,0;Fishing Config]"..
fishing_setting.func.on_show_settings = function(name)
if not fishing_setting.tmp_setting then
fishing_setting.tmp_setting = {}
fishing_setting.func.set_settings(fishing_setting.tmp_setting, fishing_setting.settings)
end
local formspec = "size[11,9]label[4,0;FISHING CONFIGURATION]"..
--Chance fish
"label[1.6,0.5;Chance fish]"..
"button[0,1;1,1;hsharkd1;-1]"..
"button[1,1;1,1;hsharkd10;-10]"..
"label[2.1,1.2;%s]"..
"button[2.7,1;1,1;hsharki10;+10]"..
"button[3.7,1;1,1;hsharki1;+1]"..
"button[0,1;1,1;cfish;-1]"..
"button[1,1;1,1;cfish;-10]"..
"label[2.1,1.2;"..tostring(fishing_setting.tmp_setting["fish_chance"]).."]"..
"button[2.7,1;1,1;cfish;+10]"..
"button[3.7,1;1,1;cfish;+1]"..
--Chance shark
"label[1.5,2;Chance shark]"..
"button[0,2.5;1,1;hfishd1;-1]"..
"button[1,2.5;1,1;hfishd10;-10]"..
"label[2.1,2.7;%s]"..
"button[2.7,2.5;1,1;hfishi10;+10]"..
"button[3.7,2.5;1,1;hfishi1;+1]"..
"button[0,2.5;1,1;cshark;-1]"..
"button[1,2.5;1,1;cshark;-10]"..
"label[2.1,2.7;"..tostring(fishing_setting.tmp_setting["shark_chance"]).."]"..
"button[2.7,2.5;1,1;cshark;+10]"..
"button[3.7,2.5;1,1;cshark;+1]"..
--Chance tresor
"label[1.5,3.5;Chance tresor]"..
"button[0,4.;1,1;hfishd1;-1]"..
"button[1,4;1,1;hfishd10;-10]"..
"label[2.1,4.2;%s]"..
"button[2.7,4;1,1;hfishi10;+10]"..
"button[3.7,4;1,1;hfishi1;+1]"..
"button[0,4.;1,1;ctresor;-1]"..
"button[1,4;1,1;ctresor;-10]"..
"label[2.1,4.2;"..tostring(fishing_setting.tmp_setting["tresor_chance"]).."]"..
"button[2.7,4;1,1;ctresor;+10]"..
"button[3.7,4;1,1;ctresor;+1]"..
--Bobber view range
"label[7.4,0.5;Bobber view range]"..
"button[7,1;1,1;hfishd1;-1]"..
"label[8.1,1.2;%s]"..
"button[8.7,1;1,1;hfishi1;+1]"..
"label[7.2,0.5;Bobber view range]"..
"button[7,1;1,1;bvrange;-1]"..
"label[8.1,1.2;"..tostring(fishing_setting.tmp_setting["bobber_view_range"]).."]"..
"button[8.7,1;1,1;bvrange;+1]"..
--Chance worm
"label[7.5,2;Chance worm]"..
"button[6,2.5;1,1;hfishd1;-1]"..
"button[7,2.5;1,1;hfishd10;-10]"..
"label[8.1,2.7;%s]"..
"button[8.7,2.5;1,1;hfishi10;+10]"..
"button[9.7,2.5;1,1;hfishi1;+1]"..
"button[6,2.5;1,1;cworm;-1]"..
"button[7,2.5;1,1;cworm;-10]"..
"label[8.1,2.7;"..tostring(fishing_setting.tmp_setting["worm_chance"]).."]"..
"button[8.7,2.5;1,1;cworm;+10]"..
"button[9.7,2.5;1,1;cworm;+1]"..
--Timer tresor
"label[7.5,3.5;Timer tresor]"..
"button[6,4.;1,1;hfishd10;-10]"..
"button[7,4;1,1;hfishd100;-100]"..
"label[8.1,4.2;%s]"..
"button[8.7,4;1,1;hfishi100;+100]"..
"button[9.7,4;1,1;hfishi10;+10]"..
"button[0,5.2;1,1;messages;true]"..
"label[1,5.4;Messages]"..
"button[0,6;1,1;poledeco;true]"..
"label[1,6.2;Simple pole deco]"..
"button[0,8.2;1,1;wearout;true]"..
"label[1,8.4;Wear out]"..
"button[0,9.7;1,1;newworm;true]"..
"label[1,9.4;New worm source]"..
"button_exit[0.5,9.2;1.5,1;abort;Abort]"..
"button_exit[8,9.2;1.5,1;abort;Ok]"
--[[
MESSAGES = true
SIMPLE_DECO_FISHING_POLE = true
WEAR_OUT = true
NEW_WORM_SOURCE = true
WORM_IS_MOB = true
TRESOR_RANDOM_ENABLE = true
BOBBER_VIEW_RANGE = 7
CHANCE_FISH = 60
CHANCE_SHARK = 30
CHANCE_TRESOR = 5
TRESOR_TIMER = 3600
WORM_CHANCE = 66
--]]
fishing_setting.func.on_show_settings = function(name, music, ambience, other)
minetest.show_formspec( name, "fishing:settings", string.format(formspec, tostring("7"), tostring("80"), tostring("80"), tostring("08"), tostring("08"), tostring("08")))
"label[7.2,3.5;Timer tresor (in sec)]"..
"button[6,4.;1,1;ttresor;-60]"..
"button[7,4;1,1;ttresor;-600]"..
"label[7.9,4.2;"..tostring(fishing_setting.tmp_setting["tresor_timer"]).."]"..
"button[8.7,4;1,1;ttresor;+600]"..
"button[9.7,4;1,1;ttresor;+60]"..
--messages display
"label[0,5.7;Display messages in chat]"..
"button[3.7,5.5;1,1;dmessages;"..tostring(fishing_setting.tmp_setting["message"]).."]"..
--poledeco
"label[0,6.5;Simple pole deco]"..
"button[3.7,6.3;1,1;poledeco;"..tostring(fishing_setting.tmp_setting["simple_deco_fishing_pole"]).."]"..
--wearout
"label[0,7.3;Poles Wear]"..
"button[3.7,7.1;1,1;wearout;"..tostring(fishing_setting.tmp_setting["wear_out"]).."]"..
--TRESOR_RANDOM_ENABLE
"label[6,5.7;Random tresor enable]"..
"button[9.7,5.5;1,1;tresorenable;"..tostring(fishing_setting.tmp_setting["tresor_random_enable"]).."]"..
--NEW_WORM_SOURCE
"label[6,6.5;New worm source]"..
"button[9.7,6.3;1,1;newworm;"..tostring(fishing_setting.tmp_setting["new_worm_source"]).."]"..
--WORM_IS_MOB
"label[6,7.3;Worm is mob]"..
"button[9.7,7.1;1,1;wormmob;"..tostring(fishing_setting.tmp_setting["worm_is_mob"]).."]"..
"button_exit[0.5,8.2;1.5,1;save;Abort]"..
"button_exit[9,8.2;1.5,1;save;Ok]"
minetest.show_formspec( name, "fishing:settings", formspec)
end
local inc = function(value, field, min, max)
local v
if field == "+1" then
v = value + 1
elseif field == "+10" then
v = value + 10
elseif field == "+60" then
v = value + 60
elseif field == "+600" then
v = value + 600
elseif field == "-1" then
v = value - 1
elseif field == "-10" then
v = value - 10
elseif field == "-60" then
v = value - 60
elseif field == "-600" then
v = value - 600
else -- useless, prevent crash
return value
end
if v > max then
v = max
end
if v < min then
v = min
end
return v
end
local bool = function(field)
local v
if field == "true" then
v = false
else
v = true
end
return v
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "fishing:settings" then
local name = player:get_player_name()
if not name then return end
if fields["abort"] == "Ok" then
if fields["save"] == "Ok" then
print("ok")
--fishing_setting.func.save()
fishing_setting.func.set_settings(fishing_setting.settings, fishing_setting.tmp_setting)
fishing_setting.func.save()
return
elseif fields["abort"] == "Abort" then
return
elseif fields["quit"] == "true" then
elseif fields["quit"] or fields["abort"] then
fishing_setting.tmp_setting = nil
return
elseif fields["cfish"] then
fishing_setting.tmp_setting["fish_chance"] = inc(fishing_setting.tmp_setting["fish_chance"], fields["cfish"], 1, 100)
elseif fields["cshark"] then
fishing_setting.tmp_setting["shark_chance"] = inc(fishing_setting.tmp_setting["shark_chance"], fields["cshark"], 1, 100)
elseif fields["ctresor"] then
fishing_setting.tmp_setting["tresor_chance"] = inc(fishing_setting.tmp_setting["tresor_chance"], fields["ctresor"], 1, 100)
elseif fields["bvrange"] then
fishing_setting.tmp_setting["bobber_view_range"] = inc(fishing_setting.tmp_setting["bobber_view_range"], fields["bvrange"], 4, 20)
elseif fields["cworm"] then
fishing_setting.tmp_setting["worm_chance"] = inc(fishing_setting.tmp_setting["worm_chance"], fields["cworm"], 1, 100)
elseif fields["ttresor"] then
fishing_setting.tmp_setting["tresor_timer"] = inc(fishing_setting.tmp_setting["tresor_timer"], fields["ttresor"], 1800, 36000)
elseif fields["dmessages"] then
fishing_setting.tmp_setting["message"] = bool(fields["dmessages"])
elseif fields["poledeco"] then
fishing_setting.tmp_setting["simple_deco_fishing_pole"] = bool(fields["poledeco"])
elseif fields["wearout"] then
fishing_setting.tmp_setting["wear_out"] = bool(fields["wearout"])
elseif fields["tresorenable"] then
fishing_setting.tmp_setting["tresor_random_enable"] = bool(fields["tresorenable"])
elseif fields["newworm"] then
fishing_setting.tmp_setting["new_worm_source"] = bool(fields["newworm"])
elseif fields["wormmob"] then
fishing_setting.tmp_setting["worm_is_mob"] = bool(fields["wormmob"])
else
return
end
@ -278,5 +325,3 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end)

View File

@ -12,7 +12,6 @@ random_objects = {}
fishing_setting.baits = {}
fishing_setting.hungry = {}
fishing_setting.prizes = {}
fishing_setting.settings.chance = {}
if (minetest.get_modpath("intllib")) then
dofile(minetest.get_modpath("intllib").."/intllib.lua")
@ -33,9 +32,9 @@ fishing_setting.settings["new_worm_source"] = NEW_WORM_SOURCE
fishing_setting.settings["wear_out"] = WEAR_OUT
fishing_setting.settings["simple_deco_fishing_pole"] = SIMPLE_DECO_FISHING_POLE
fishing_setting.settings["bobber_view_range"] = BOBBER_VIEW_RANGE
fishing_setting.settings.chance["fish"] = CHANCE_FISH
fishing_setting.settings.chance["tresor"] = CHANCE_TRESOR
fishing_setting.settings.chance["shark"] = CHANCE_SHARK
fishing_setting.settings["fish_chance"] = FISH_CHANCE
fishing_setting.settings["shark_chance"] = SHARK_CHANCE
fishing_setting.settings["tresor_chance"] = TRESOR_CHANCE
fishing_setting.settings["tresor_timer"] = TRESOR_TIMER
fishing_setting.settings["tresor_random_enable"] = TRESOR_RANDOM_ENABLE

View File

@ -26,7 +26,8 @@ for _,pole in pairs(fishing_setting.poles) do
if pointed_thing and pointed_thing.under then
local pt = pointed_thing
local node = minetest.get_node(pt.under)
if node.name ~= "default:water_source" and node.name ~= "noairblocks:water_sourcex" and node.name ~= "default:river_water_source" then return nil end
--if node.name ~= "default:water_source" and node.name ~= "noairblocks:water_sourcex" and node.name ~= "default:river_water_source" then return nil end
if not node or string.find(node.name, "water_source") == nil then return nil end
local player = user:get_player_name()
local inv = user:get_inventory()
local bait = inv:get_stack("main", user:get_wield_index()+1 ):get_name()
@ -46,7 +47,8 @@ for _,pole in pairs(fishing_setting.poles) do
for _,k in pairs({ 1, 0, -1}) do
for _,l in pairs({ -1, 0, 1}) do
local node_name = minetest.get_node({x=pt.under.x+l, y=pt.under.y, z=pt.under.z+k}).name
if (node_name == "default:water_source" or node_name == "noairblocks:water_sourcex" or node_name == "default:river_water_source")
--if (node_name == "default:water_source" or node_name == "noairblocks:water_sourcex" or node_name == "default:river_water_source")
if node and string.find(node_name, "water_source") ~= nil
and minetest.get_node({x=pt.under.x+l, y=pt.under.y+1, z=pt.under.z+k}).name == "air" then
local empty = true
for o, obj in pairs(bobbers) do

View File

@ -5,8 +5,8 @@ BOBBER_VIEW_RANGE = 7
NEW_WORM_SOURCE = true
WORM_IS_MOB = true
WORM_CHANCE = 66
CHANCE_FISH = 60
CHANCE_SHARK = 50
CHANCE_TRESOR = 5
FISH_CHANCE = 60
SHARK_CHANCE = 50
TRESOR_CHANCE = 5
TRESOR_TIMER = 3600
TRESOR_RANDOM_ENABLE = true

Binary file not shown.

Before

Width:  |  Height:  |  Size: 503 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 180 B