]> git.lizzy.rs Git - xdecor.git/blobdiff - chess.lua
Work Table : fix crash on fail to fetch 'tiles' node definition
[xdecor.git] / chess.lua
index c966221b5612cd6924d00a8a8173ab1466d23a8e..5d08f86daebbda0f074c7ef61d2b76fa8f225b5b 100644 (file)
--- a/chess.lua
+++ b/chess.lua
@@ -17,18 +17,17 @@ end
 function realchess.init(pos)
        local meta = minetest.get_meta(pos)
        local inv = meta:get_inventory()
-       local slots = "listcolors[#00000000;#00000000;#00000000;#30434C;#FFF]"
-       local formspec
 
        inv:set_size("board", 64)
+       
+       local formspec = [[ size[8,8.6;]
+                       bgcolor[#080808BB;true]
+                       background[0,0;8,8;chess_bg.png]
+                       button[3.1,7.8;2,2;new;New game]
+                       list[context;board;0,0;8,8;]
+                       listcolors[#00000000;#00000000;#00000000;#30434C;#FFF] ]]
 
-       meta:set_string("formspec",
-               "size[8,8.6;]"..
-               "bgcolor[#080808BB;true]"..
-               "background[0,0;8,8;chess_bg.png]"..
-               "button[3.1,7.8;2,2;new;New game]"..
-               "list[context;board;0,0;8,8;]"..
-               slots)
+       meta:set_string("formspec", formspec)
 
        meta:set_string("infotext", "Chess Board")
        meta:set_string("playerBlack", "")
@@ -528,9 +527,11 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, count, pl
        meta:set_int("lastMoveTime", minetest.get_gametime())
 
        if meta:get_string("lastMove") == "black" then
-               minetest.chat_send_player(playerWhite, "["..os.date("%H:%M:%S").."] "..playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
+               minetest.chat_send_player(playerWhite, "["..os.date("%H:%M:%S").."] "..
+                               playerName.." has moved a "..pieceFrom:match(":(.-)%_")..", it's now your turn.")
        elseif meta:get_string("lastMove") == "white" then
-               minetest.chat_send_player(playerBlack, "["..os.date("%H:%M:%S").."] "..playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
+               minetest.chat_send_player(playerBlack, "["..os.date("%H:%M:%S").."] "..
+                               playerName.." has moved a "..pieceFrom:match(":(.-)%_")..", it's now your turn.")
        end
 
        if pieceTo:find("king") then
@@ -542,34 +543,45 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, count, pl
        return 1
 end
 
+local function timeout_format(timeout_limit)
+       local time_remaining = timeout_limit - minetest.get_gametime()
+       local minutes = math.floor(time_remaining / 60)
+       local seconds = time_remaining % 60
+
+       if minutes == 0 then return seconds.." sec." end
+       return minutes.." min. "..seconds.." sec."
+end
+
 function realchess.fields(pos, formname, fields, sender)
        local playerName = sender:get_player_name()
        local meta = minetest.get_meta(pos)
-
+       local timeout_limit = meta:get_int("lastMoveTime") + 300
        if fields.quit then return end
 
-       -- the chess can't be reset during a started game unless if nobody has played during a while (~5 min. by default)
+       -- timeout is 5 min. by default for resetting the game (non-players only)
        if fields.new and (meta:get_string("playerWhite") == playerName or
                        meta:get_string("playerBlack") == playerName) then
                realchess.init(pos)
        elseif fields.new and meta:get_int("lastMoveTime") ~= 0 and
-                       minetest.get_gametime() >= meta:get_int("lastMoveTime") + 300 and
+                       minetest.get_gametime() >= timeout_limit and
                        (meta:get_string("playerWhite") ~= playerName or
                        meta:get_string("playerBlack") ~= playerName) then
                realchess.init(pos)
        else
-               minetest.chat_send_player(playerName, "You can't reset the chessboard, a game has been started.\nIf you are not a current player, try again after a while.")
+               minetest.chat_send_player(playerName, "[!] You can't reset the chessboard, a game has been started.\n"..
+                               "If you are not a current player, try again in "..timeout_format(timeout_limit))
        end
 end
 
 function realchess.dig(pos, player)
        local meta = minetest.get_meta(pos)
        local playerName = player:get_player_name()
+       local timeout_limit = meta:get_int("lastMoveTime") + 300
 
-       -- the chess can't be dug during a started game unless if nobody has played during a while (~5 min. by default)
-       if meta:get_int("lastMoveTime") ~= 0 and
-                       minetest.get_gametime() <= meta:get_int("lastMoveTime") + 300 then
-               minetest.chat_send_player(playerName, "You can't dig the chessboard, a game has been started.\nReset it first if you're a current player, or try digging again after a while.")
+       -- timeout is 5 min. by default for digging the chessboard (non-players only)
+       if meta:get_int("lastMoveTime") ~= 0 and minetest.get_gametime() <= timeout_limit then
+               minetest.chat_send_player(playerName, "[!] You can't dig the chessboard, a game has been started.\n"..
+                               "Reset it first if you're a current player, or dig again in "..timeout_format(timeout_limit))
                return false
        end
 
@@ -582,10 +594,6 @@ function realchess.on_move(pos, from_list, from_index, to_list, to_index, count,
        return false
 end
 
-function realchess.take(pos, listname, index, stack, player)
-       return 0
-end
-
 minetest.register_node(":realchess:chessboard", {
        description = "Chess Board",
        drawtype = "nodebox",
@@ -593,12 +601,10 @@ minetest.register_node(":realchess:chessboard", {
        paramtype2 = "facedir",
        inventory_image = "chessboard_top.png",
        wield_image = "chessboard_top.png",
-       tiles = {"chessboard_top.png", "chessboard_top.png",
-               "chessboard_sides.png", "chessboard_sides.png",
-               "chessboard_top.png", "chessboard_top.png"},
+       tiles = {"chessboard_top.png", "chessboard_top.png", "chessboard_sides.png"},
        groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
        sounds = default.node_sound_wood_defaults(),
-       node_box = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5}},
+       node_box = {type = "fixed", fixed = {-.375, -.5, -.375, .375, -.4375, .375}},
        sunlight_propagates = true,
        on_rotate = screwdriver.rotate_simple,
        can_dig = realchess.dig,
@@ -606,7 +612,7 @@ minetest.register_node(":realchess:chessboard", {
        on_receive_fields = realchess.fields,
        allow_metadata_inventory_move = realchess.move,
        on_metadata_inventory_move = realchess.on_move,
-       allow_metadata_inventory_take = realchess.take
+       allow_metadata_inventory_take = function() return 0 end
 })
 
 local function register_piece(name, count)
@@ -638,11 +644,3 @@ register_piece("bishop", 2)
 register_piece("queen")
 register_piece("king")
 
-minetest.register_craft({ 
-       output = "realchess:chessboard",
-       recipe = {
-               {"dye:black", "dye:white", "dye:black"},
-               {"stairs:slab_wood", "stairs:slab_wood", "stairs:slab_wood"}
-       } 
-})
-