Signed by: Gabriel Pérez-Cerezo <gabriel@gpcf.eu> added sale feature
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Mon, 27 Jun 2016 20:30:03 +0000 (22:30 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Mon, 27 Jun 2016 20:30:03 +0000 (22:30 +0200)
init.lua
textures/forsale.png [new file with mode: 0644]

index 3c977b3..ae7195c 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -3,8 +3,9 @@ minetest.register_privilege("delprotect","Ignore player protection")
 
 protector = {}
 protector.mod = "redo"
-protector.radius = (tonumber(minetest.setting_get("protector_radius")) or 5)
+protector.radius = (tonumber(minetest.setting_get("protector_radius")) or 7)
 protector.drop = minetest.setting_getbool("protector_drop") or false
+protector.craft = minetest.setting_getbool("protector_craft") or false
 protector.hurt = (tonumber(minetest.setting_get("protector_hurt")) or 0)
 
 -- Intllib
@@ -81,8 +82,11 @@ end
 
 -- Protector Interface
 
-protector.generate_formspec = function(meta)
-
+protector.generate_formspec = function(meta, pos)
+   local sell = ""
+   if not protector.overlaps(pos) then
+      sell = "button[7,1;1,1;sell;sell]"
+   end
        local formspec = "size[8,7]"
                .. default.gui_bg
                .. default.gui_bg_img
@@ -90,7 +94,8 @@ protector.generate_formspec = function(meta)
                .. "label[2.5,0;" .. S("-- Protector interface --") .. "]"
                .. "label[0,1;" .. S("PUNCH node to show protected area or USE for area check") .. "]"
                .. "label[0,2;" .. S("Members:") .. "]"
-               .. "button_exit[2.5,6.2;3,0.5;close_me;" .. S("Close") .. "]"
+               .. "button_exit[2.5,6.2;3,0.5;close_me;" .. S("Close") .. "]" .. sell
+
 
        local members = protector.get_member_list(meta)
        local npp = 12 -- max users added onto protector list
@@ -130,6 +135,54 @@ protector.generate_formspec = function(meta)
        return formspec
 end
 
+protector.generate_sale_formspec = function(meta, pos)
+   local price = minetest.deserialize(meta:get_string("price")) or 0
+   local formspec = "size[8,7]"
+      .. default.gui_bg
+      .. default.gui_bg_img
+      .. default.gui_slots
+      .. "label[2.5,0;" .. S("-- Sell area --") .. "]"
+      .. "label[2.5,1;" .. S("Price: $") .. price .. "]" ..
+      "item_image_button[2.5,2;1,1;".. "currency:minegeld" ..";i1;\n\n\b\b\b\b\b" .. "1" .."]" ..
+      "item_image_button[2.5,3;1,1;".. "currency:minegeld_5" ..";i5;\n\n\b\b\b\b\b" .. "1" .."]" ..
+      "item_image_button[2.5,4;1,1;".. "currency:minegeld_10" ..";i10;\n\n\b\b\b\b\b" .. "1" .."]" ..
+      "item_image_button[4.5,2;1,1;".. "currency:minegeld" ..";i-1;\n\n\b\b\b\b\b" .. "-1" .."]" ..
+      "item_image_button[4.5,3;1,1;".. "currency:minegeld_5" ..";i-5;\n\n\b\b\b\b\b" .. "-1" .."]" ..
+      "item_image_button[4.5,4;1,1;".. "currency:minegeld_10" ..";i-10;\n\n\b\b\b\b\b" .. "-1" .."]" ..
+      "button_exit[1.5,6.2;3,0.5;dont_sell;" .. S("Don't sell!") .. "]" .. "button_exit[4.6,6.2;3,0.5;ok;Sell!]"
+   return formspec
+end
+
+protector.generate_buy_formspec = function(meta, player)
+   local price = minetest.deserialize(meta:get_string("price")) or 15
+   if not atm.balance[player] then
+      atm.balance[player] = 0
+      atm.saveaccounts()
+   end
+   local formspec = "size[8,7]"
+      .. default.gui_bg
+      .. default.gui_bg_img
+      .. default.gui_slots
+      .. "label[2.5,0;" .. S("-- Sell area --") .. "]"
+      .. "label[2.5,1;" .. S("Price: $") .. price .. "]" ..
+      "label[2.5,2;" .. S("Your account balance: $") .. atm.balance[player] .. "]" ..
+      "button_exit[1.5,6.2;3,0.5;cancel;" .. S("Cancel") .. "]" .. "button_exit[4.6,6.2;3,0.5;ok;Buy]"
+   return formspec
+end
+
+
+protector.overlaps = function(p)
+   local r = protector.radius * 2
+   local pos = minetest.find_nodes_in_area(
+      {x = p.x - r, y = p.y - r, z = p.z - r},
+      {x = p.x + r, y = p.y + r, z = p.z + r},
+      {"protector:protect", "protector:protect2"})
+   if (#pos > 1) then
+      return true
+   end
+   return false
+end
+
 -- Infolevel:
 -- 0 for no info
 -- 1 for "This area is owned by <owner> !" if you can't dig
@@ -351,7 +404,7 @@ minetest.register_node("protector:protect", {
                if meta
                and protector.can_dig(1, pos,clicker:get_player_name(), true, 1) then
                        minetest.show_formspec(clicker:get_player_name(), 
-                       "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
+                       "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta, pos))
                end
        end,
 
@@ -372,15 +425,68 @@ minetest.register_node("protector:protect", {
        on_blast = function() end,
 })
 
-minetest.register_craft({
-       output = "protector:protect",
-       recipe = {
-               {"default:stone", "default:stone", "default:stone"},
-               {"default:stone", "default:steel_ingot", "default:stone"},
-               {"default:stone", "default:stone", "default:stone"},
-       }
+minetest.register_node("protector:protect_sell", {
+       description = S("Protection Block For Sale"),
+       drawtype = "nodebox",
+       tiles = {
+               "forsale.png",
+               "forsale.png",
+               "forsale.png^protector_logo.png"
+       },
+       sounds = default.node_sound_stone_defaults(),
+       groups = {dig_immediate = 2, unbreakable = 1},
+       is_ground_content = false,
+       paramtype = "light",
+       light_source = 4,
+       drop = "protector:protect",
+       node_box = {
+               type = "fixed",
+               fixed = {
+                       {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
+               }
+       },
+
+       on_punch = function(pos, node, puncher)
+
+               if minetest.is_protected(pos, puncher:get_player_name()) then
+                       return
+               end
+
+               minetest.add_entity(pos, "protector:display")
+       end,
+       on_rightclick = function(pos, node, clicker, itemstack)
+          
+          local meta = minetest.get_meta(pos)
+          minetest.show_formspec(clicker:get_player_name(), 
+                                 "protector_sell:node_" .. minetest.pos_to_string(pos), protector.generate_buy_formspec(meta, clicker:get_player_name()))
+       end,
+
+       can_dig = function(pos, player)
+
+               return protector.can_dig(1, pos, player:get_player_name(), true, 1)
+       end,
+
+       on_blast = function() end,
 })
 
+if protector.craft then
+   minetest.register_craft({
+        output = "protector:protect",
+        recipe = {
+           {"default:stone", "default:stone", "default:stone"},
+           {"default:stone", "default:steel_ingot", "default:stone"},
+           {"default:stone", "default:stone", "default:stone"},
+        }
+   })
+   minetest.register_craft({
+        output = "protector:protect2",
+        recipe = {
+           {"default:stone", "default:stone", "default:stone"},
+           {"default:stone", "default:copper_ingot", "default:stone"},
+           {"default:stone", "default:stone", "default:stone"},
+        }
+   })
+end
 --= Protection Logo
 
 minetest.register_node("protector:protect2", {
@@ -410,7 +516,7 @@ minetest.register_node("protector:protect2", {
        after_place_node = function(pos, placer)
 
                local meta = minetest.get_meta(pos)
-
+               meta:set_string("price", minetest.serialize(0))
                meta:set_string("owner", placer:get_player_name() or "")
                meta:set_string("infotext", S("Protection (owned by @1)", meta:get_string("owner")))
                meta:set_string("members", "")
@@ -432,7 +538,7 @@ minetest.register_node("protector:protect2", {
                if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
 
                        minetest.show_formspec(clicker:get_player_name(), 
-                       "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
+                       "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta,pos))
                end
        end,
 
@@ -449,55 +555,107 @@ minetest.register_node("protector:protect2", {
 
                return protector.can_dig(1, pos, player:get_player_name(), true, 1)
        end,
-
+       
        on_blast = function() end,
 })
 
-minetest.register_craft({
-       output = "protector:protect2",
-       recipe = {
-               {"default:stone", "default:stone", "default:stone"},
-               {"default:stone", "default:copper_ingot", "default:stone"},
-               {"default:stone", "default:stone", "default:stone"},
-       }
-})
 
 -- If name entered or button press
+local function swap_node(pos, name)
+   local node = minetest.get_node(pos)
+   if node.name == name then
+      return
+   end
+   node.name = name
+   minetest.swap_node(pos, node)
+end
 
 minetest.register_on_player_receive_fields(function(player, formname, fields)
 
-       if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
-
-               local pos_s = string.sub(formname, string.len("protector:node_") + 1)
-               local pos = minetest.string_to_pos(pos_s)
-               local meta = minetest.get_meta(pos)
-
-               if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
-                       return
-               end
-
-               if fields.protector_add_member then
-
-                       for _, i in pairs(fields.protector_add_member:split(" ")) do
-                               protector.add_member(meta, i)
-                       end
-               end
-
-               for field, value in pairs(fields) do
-
-                       if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
-                               protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
-                       end
-               end
-               
-               if not fields.close_me then
-                       minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
-               end
-
-       end
-
+      if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
+
+        local pos_s = string.sub(formname, string.len("protector:node_") + 1)
+        local pos = minetest.string_to_pos(pos_s)
+        local meta = minetest.get_meta(pos)
+
+        if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
+           return
+        end
+
+        if fields.protector_add_member then
+
+           for _, i in pairs(fields.protector_add_member:split(" ")) do
+              protector.add_member(meta, i)
+           end
+        end
+
+        for field, value in pairs(fields) do
+
+           if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
+              protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
+           end
+        end
+        
+        if not fields.close_me and not fields.sell then
+           minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta,pos))
+        end
+
+        if fields.sell then
+           minetest.show_formspec(player:get_player_name(), "sell"..formname, protector.generate_sale_formspec(meta,pos))
+        end
+        if fields.sell then
+           swap_node(pos, "protector:protect_sell")
+           meta:set_string("infotext", S("Protection for sale (owned by @1)", meta:get_string("owner")))
+        end
+
+      end
+      if string.sub(formname, 0, string.len("sellprotector:node_")) == "sellprotector:node_" then
+
+        local pos_s = string.sub(formname, string.len("sellprotector:node_") + 1)
+        local pos = minetest.string_to_pos(pos_s)
+        local meta = minetest.get_meta(pos)
+        local price = minetest.deserialize(meta:get_string("price")) or 0
+        for _,i in pairs({"1","5","10", "-1", "-5", "-10"}) do
+           if fields["i"..i] then
+              price = price + i
+              if price < 0 then
+                 price = 0
+              end
+           end
+        end
+        meta:set_string("price", minetest.serialize(price))
+        if not fields.ok and not fields.dont_sell then
+           minetest.show_formspec(player:get_player_name(), formname, protector.generate_sale_formspec(meta,pos))
+        end
+      end
+
+      if string.sub(formname, 0, string.len("protector_sell:node_")) == "protector_sell:node_" then
+        local pos_s = string.sub(formname, string.len("protector_sell:node_") + 1)
+        local pos = minetest.string_to_pos(pos_s)
+        local meta = minetest.get_meta(pos)
+        local price = minetest.deserialize(meta:get_string("price")) or 0
+        local name = player:get_player_name()
+        local oldowner = meta:get_string("owner")
+        if fields.cancel then
+           return
+        end
+        if price > atm.balance[name] then
+           minetest.chat_send_player(name, "Not enough money on your account. Please recharge on the atm")
+           return
+        else
+           atm.balance[name] = atm.balance[name] - price
+           atm.balance[oldowner] = atm.balance[oldowner] + price
+           atm.saveaccounts()
+        end
+        swap_node(pos, "protector:protect")
+        meta:set_string("owner", name)
+        meta:set_string("members", "")
+        meta:set_string("infotext", S("Protection (owned by @1)", meta:get_string("owner")))
+        minetest.chat_send_player(name, "Bought protector")
+      end
 end)
 
+
 -- Display entity shown when protector node is punched
 
 minetest.register_entity("protector:display", {
diff --git a/textures/forsale.png b/textures/forsale.png
new file mode 100644 (file)
index 0000000..c9ddafe
Binary files /dev/null and b/textures/forsale.png differ