2015-10-27 13:26:08 +03:00
|
|
|
local realchess = {}
|
2015-11-15 22:35:20 +03:00
|
|
|
screwdriver = screwdriver or {}
|
2015-10-27 13:26:08 +03:00
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
local function index_to_xy(idx)
|
|
|
|
idx = idx - 1
|
|
|
|
local x = idx % 8
|
|
|
|
local y = (idx - x) / 8
|
2015-10-27 13:26:08 +03:00
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
|
|
|
|
local function xy_to_index(x, y)
|
|
|
|
return x + y * 8 + 1
|
|
|
|
end
|
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
local chat_prefix = minetest.colorize("#FFFF00", "[Chess] ")
|
|
|
|
local letters = {'A','B','C','D','E','F','G','H'}
|
|
|
|
|
|
|
|
local pieces = {
|
|
|
|
"realchess:rook_black_1",
|
|
|
|
"realchess:knight_black_1",
|
|
|
|
"realchess:bishop_black_1",
|
|
|
|
"realchess:queen_black",
|
|
|
|
"realchess:king_black",
|
|
|
|
"realchess:bishop_black_2",
|
|
|
|
"realchess:knight_black_2",
|
|
|
|
"realchess:rook_black_2",
|
|
|
|
"realchess:pawn_black_1",
|
|
|
|
"realchess:pawn_black_2",
|
|
|
|
"realchess:pawn_black_3",
|
|
|
|
"realchess:pawn_black_4",
|
|
|
|
"realchess:pawn_black_5",
|
|
|
|
"realchess:pawn_black_6",
|
|
|
|
"realchess:pawn_black_7",
|
|
|
|
"realchess:pawn_black_8",
|
|
|
|
'','','','','','','','','','','','','','','','',
|
|
|
|
'','','','','','','','','','','','','','','','',
|
|
|
|
"realchess:pawn_white_1",
|
|
|
|
"realchess:pawn_white_2",
|
|
|
|
"realchess:pawn_white_3",
|
|
|
|
"realchess:pawn_white_4",
|
|
|
|
"realchess:pawn_white_5",
|
|
|
|
"realchess:pawn_white_6",
|
|
|
|
"realchess:pawn_white_7",
|
|
|
|
"realchess:pawn_white_8",
|
|
|
|
"realchess:rook_white_1",
|
|
|
|
"realchess:knight_white_1",
|
|
|
|
"realchess:bishop_white_1",
|
|
|
|
"realchess:queen_white",
|
|
|
|
"realchess:king_white",
|
|
|
|
"realchess:bishop_white_2",
|
|
|
|
"realchess:knight_white_2",
|
|
|
|
"realchess:rook_white_2"
|
|
|
|
}
|
|
|
|
|
|
|
|
local pieces_str, x = "", 0
|
|
|
|
for i = 1, #pieces do
|
|
|
|
local p = pieces[i]:match(":(%w+_%w+)")
|
|
|
|
if pieces[i]:find(":(%w+)_(%w+)") and not pieces_str:find(p) then
|
|
|
|
pieces_str = pieces_str .. x .. "=" .. p .. ".png,"
|
|
|
|
x = x + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
pieces_str = pieces_str .. "69=mailbox_blank16.png"
|
|
|
|
|
|
|
|
local fs = [[
|
|
|
|
size[14.7,10;]
|
|
|
|
no_prepend[]
|
|
|
|
bgcolor[#080808BB;true]
|
|
|
|
background[0,0;14.7,10;chess_bg.png]
|
|
|
|
list[context;board;0.3,1;8,8;]
|
|
|
|
listcolors[#00000000;#00000000;#00000000;#30434C;#FFF]
|
|
|
|
tableoptions[background=#00000000;highlight=#00000000;border=false]
|
|
|
|
button[10.5,8.5;2,2;new;New game]
|
|
|
|
]] .. "tablecolumns[image," .. pieces_str ..
|
|
|
|
";text;color;text;color;text;image," .. pieces_str .. "]"
|
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
function realchess.init(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
2016-12-30 17:04:57 +03:00
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
meta:set_string("formspec", fs)
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_string("infotext", "Chess Board")
|
|
|
|
meta:set_string("playerBlack", "")
|
|
|
|
meta:set_string("playerWhite", "")
|
|
|
|
meta:set_string("lastMove", "")
|
|
|
|
meta:set_string("winner", "")
|
2016-02-18 00:53:00 +03:00
|
|
|
|
|
|
|
meta:set_int("lastMoveTime", 0)
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_int("castlingBlackL", 1)
|
|
|
|
meta:set_int("castlingBlackR", 1)
|
|
|
|
meta:set_int("castlingWhiteL", 1)
|
|
|
|
meta:set_int("castlingWhiteR", 1)
|
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
meta:set_string("moves", "")
|
|
|
|
meta:set_string("eaten", "")
|
2016-02-18 00:53:00 +03:00
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
inv:set_list("board", pieces)
|
2016-02-18 00:53:00 +03:00
|
|
|
inv:set_size("board", 64)
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
function realchess.move(pos, from_list, from_index, to_list, to_index, _, player)
|
2015-10-27 13:26:08 +03:00
|
|
|
if from_list ~= "board" and to_list ~= "board" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local playerName = player:get_player_name()
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
|
|
|
if meta:get_string("winner") ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local pieceFrom = inv:get_stack(from_list, from_index):get_name()
|
|
|
|
local pieceTo = inv:get_stack(to_list, to_index):get_name()
|
|
|
|
local lastMove = meta:get_string("lastMove")
|
|
|
|
local thisMove -- will replace lastMove when move is legal
|
|
|
|
local playerWhite = meta:get_string("playerWhite")
|
|
|
|
local playerBlack = meta:get_string("playerBlack")
|
|
|
|
|
|
|
|
if pieceFrom:find("white") then
|
|
|
|
if playerWhite ~= "" and playerWhite ~= playerName then
|
2018-08-15 02:54:56 +03:00
|
|
|
minetest.chat_send_player(playerName, chat_prefix .. "Someone else plays white pieces!")
|
2015-10-27 13:26:08 +03:00
|
|
|
return 0
|
2016-12-30 17:04:57 +03:00
|
|
|
end
|
2015-10-27 13:26:08 +03:00
|
|
|
if lastMove ~= "" and lastMove ~= "black" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if pieceTo:find("white") then
|
|
|
|
-- Don't replace pieces of same color
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
playerWhite = playerName
|
|
|
|
thisMove = "white"
|
|
|
|
elseif pieceFrom:find("black") then
|
|
|
|
if playerBlack ~= "" and playerBlack ~= playerName then
|
2018-08-15 02:54:56 +03:00
|
|
|
minetest.chat_send_player(playerName, chat_prefix .. "Someone else plays black pieces!")
|
2015-10-27 13:26:08 +03:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if lastMove ~= "" and lastMove ~= "white" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if pieceTo:find("black") then
|
|
|
|
-- Don't replace pieces of same color
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
playerBlack = playerName
|
|
|
|
thisMove = "black"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- DETERMINISTIC MOVING
|
|
|
|
|
|
|
|
local from_x, from_y = index_to_xy(from_index)
|
|
|
|
local to_x, to_y = index_to_xy(to_index)
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
if pieceFrom:sub(11,14) == "pawn" then
|
2015-10-27 13:26:08 +03:00
|
|
|
if thisMove == "white" then
|
2016-02-18 00:53:00 +03:00
|
|
|
local pawnWhiteMove = inv:get_stack(from_list, xy_to_index(from_x, from_y - 1)):get_name()
|
2015-10-27 13:26:08 +03:00
|
|
|
-- white pawns can go up only
|
|
|
|
if from_y - 1 == to_y then
|
|
|
|
if from_x == to_x then
|
|
|
|
if pieceTo ~= "" then
|
|
|
|
return 0
|
|
|
|
elseif to_index >= 1 and to_index <= 8 then
|
|
|
|
inv:set_stack(from_list, from_index, "realchess:queen_white")
|
|
|
|
end
|
|
|
|
elseif from_x - 1 == to_x or from_x + 1 == to_x then
|
|
|
|
if not pieceTo:find("black") then
|
|
|
|
return 0
|
2015-10-29 20:43:57 +03:00
|
|
|
elseif to_index >= 1 and to_index <= 8 then
|
|
|
|
inv:set_stack(from_list, from_index, "realchess:queen_white")
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif from_y - 2 == to_y then
|
2016-02-18 00:53:00 +03:00
|
|
|
if pieceTo ~= "" or from_y < 6 or pawnWhiteMove ~= "" then
|
2015-10-27 13:26:08 +03:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
2018-03-08 23:21:20 +03:00
|
|
|
|
|
|
|
-- if x not changed,
|
|
|
|
-- ensure that destination cell is empty
|
|
|
|
-- elseif x changed one unit left or right
|
|
|
|
-- ensure the pawn is killing opponent piece
|
|
|
|
-- else
|
|
|
|
-- move is not legal - abort
|
|
|
|
|
|
|
|
if from_x == to_x then
|
|
|
|
if pieceTo ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif from_x - 1 == to_x or from_x + 1 == to_x then
|
|
|
|
if not pieceTo:find("black") then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
elseif thisMove == "black" then
|
2016-02-18 00:53:00 +03:00
|
|
|
local pawnBlackMove = inv:get_stack(from_list, xy_to_index(from_x, from_y + 1)):get_name()
|
2015-10-27 13:26:08 +03:00
|
|
|
-- black pawns can go down only
|
|
|
|
if from_y + 1 == to_y then
|
|
|
|
if from_x == to_x then
|
|
|
|
if pieceTo ~= "" then
|
|
|
|
return 0
|
|
|
|
elseif to_index >= 57 and to_index <= 64 then
|
|
|
|
inv:set_stack(from_list, from_index, "realchess:queen_black")
|
|
|
|
end
|
|
|
|
elseif from_x - 1 == to_x or from_x + 1 == to_x then
|
|
|
|
if not pieceTo:find("white") then
|
|
|
|
return 0
|
2015-10-29 20:43:57 +03:00
|
|
|
elseif to_index >= 57 and to_index <= 64 then
|
|
|
|
inv:set_stack(from_list, from_index, "realchess:queen_black")
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif from_y + 2 == to_y then
|
2016-02-18 00:53:00 +03:00
|
|
|
if pieceTo ~= "" or from_y > 1 or pawnBlackMove ~= "" then
|
2015-10-27 13:26:08 +03:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if x not changed,
|
|
|
|
-- ensure that destination cell is empty
|
|
|
|
-- elseif x changed one unit left or right
|
|
|
|
-- ensure the pawn is killing opponent piece
|
|
|
|
-- else
|
|
|
|
-- move is not legal - abort
|
|
|
|
|
|
|
|
if from_x == to_x then
|
|
|
|
if pieceTo ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif from_x - 1 == to_x or from_x + 1 == to_x then
|
|
|
|
if not pieceTo:find("white") then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(11,14) == "rook" then
|
2015-10-27 13:26:08 +03:00
|
|
|
if from_x == to_x then
|
|
|
|
-- moving vertically
|
|
|
|
if from_y < to_y then
|
|
|
|
-- moving down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = from_y + 1, to_y - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- mocing up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = to_y + 1, from_y - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif from_y == to_y then
|
|
|
|
-- mocing horizontally
|
|
|
|
if from_x < to_x then
|
|
|
|
-- mocing right
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = from_x + 1, to_x - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- mocing left
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = to_x + 1, from_x - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- attempt to move arbitrarily -> abort
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
if thisMove == "white" or thisMove == "black" then
|
|
|
|
if pieceFrom:sub(-1) == "1" then
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_int("castlingWhiteL", 0)
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(-1) == "2" then
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_int("castlingWhiteR", 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(11,16) == "knight" then
|
2015-10-27 13:26:08 +03:00
|
|
|
-- get relative pos
|
|
|
|
local dx = from_x - to_x
|
|
|
|
local dy = from_y - to_y
|
|
|
|
|
|
|
|
-- get absolute values
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx < 0 then dx = -dx end
|
|
|
|
if dy < 0 then dy = -dy end
|
2015-10-27 13:26:08 +03:00
|
|
|
|
|
|
|
-- sort x and y
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx > dy then dx, dy = dy, dx end
|
2015-10-27 13:26:08 +03:00
|
|
|
|
|
|
|
-- ensure that dx == 1 and dy == 2
|
|
|
|
if dx ~= 1 or dy ~= 2 then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
-- just ensure that destination cell does not contain friend piece
|
|
|
|
-- ^ it was done already thus everything ok
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(11,16) == "bishop" then
|
2015-10-27 13:26:08 +03:00
|
|
|
-- get relative pos
|
|
|
|
local dx = from_x - to_x
|
|
|
|
local dy = from_y - to_y
|
|
|
|
|
|
|
|
-- get absolute values
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx < 0 then dx = -dx end
|
|
|
|
if dy < 0 then dy = -dy end
|
2015-10-27 13:26:08 +03:00
|
|
|
|
|
|
|
-- ensure dx and dy are equal
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx ~= dy then return 0 end
|
2015-10-27 13:26:08 +03:00
|
|
|
|
|
|
|
if from_x < to_x then
|
|
|
|
if from_y < to_y then
|
|
|
|
-- moving right-down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- moving right-up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if from_y < to_y then
|
|
|
|
-- moving left-down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- moving left-up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(11,15) == "queen" then
|
2015-10-27 13:26:08 +03:00
|
|
|
local dx = from_x - to_x
|
|
|
|
local dy = from_y - to_y
|
|
|
|
|
|
|
|
-- get absolute values
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx < 0 then dx = -dx end
|
|
|
|
if dy < 0 then dy = -dy end
|
2015-10-27 13:26:08 +03:00
|
|
|
|
|
|
|
-- ensure valid relative move
|
|
|
|
if dx ~= 0 and dy ~= 0 and dx ~= dy then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if from_x == to_x then
|
|
|
|
if from_y < to_y then
|
|
|
|
-- goes down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x, from_y + i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- goes up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x, from_y - i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2016-12-30 17:04:57 +03:00
|
|
|
end
|
2015-10-27 13:26:08 +03:00
|
|
|
elseif from_x < to_x then
|
|
|
|
if from_y == to_y then
|
|
|
|
-- goes right
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x + i, from_y)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif from_y < to_y then
|
|
|
|
-- goes right-down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- goes right-up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2016-12-30 17:04:57 +03:00
|
|
|
end
|
2015-10-27 13:26:08 +03:00
|
|
|
else
|
|
|
|
if from_y == to_y then
|
|
|
|
-- goes left
|
|
|
|
-- ensure that no piece disturbs the way and destination cell does
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x - i, from_y)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif from_y < to_y then
|
|
|
|
-- goes left-down
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- goes left-up
|
|
|
|
-- ensure that no piece disturbs the way
|
|
|
|
for i = 1, dx - 1 do
|
|
|
|
if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2016-12-30 17:04:57 +03:00
|
|
|
end
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
elseif pieceFrom:sub(11,14) == "king" then
|
2015-10-27 13:26:08 +03:00
|
|
|
local dx = from_x - to_x
|
|
|
|
local dy = from_y - to_y
|
|
|
|
local check = true
|
2016-12-30 17:04:57 +03:00
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
if thisMove == "white" then
|
|
|
|
if from_y == 7 and to_y == 7 then
|
|
|
|
if to_x == 1 then
|
2016-02-18 00:53:00 +03:00
|
|
|
local castlingWhiteL = meta:get_int("castlingWhiteL")
|
|
|
|
local idx57 = inv:get_stack(from_list, 57):get_name()
|
|
|
|
|
|
|
|
if castlingWhiteL == 1 and idx57 == "realchess:rook_white_1" then
|
2015-10-27 13:26:08 +03:00
|
|
|
for i = 58, from_index - 1 do
|
|
|
|
if inv:get_stack(from_list, i):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inv:set_stack(from_list, 57, "")
|
|
|
|
inv:set_stack(from_list, 59, "realchess:rook_white_1")
|
|
|
|
check = false
|
|
|
|
end
|
|
|
|
elseif to_x == 6 then
|
2016-02-18 00:53:00 +03:00
|
|
|
local castlingWhiteR = meta:get_int("castlingWhiteR")
|
|
|
|
local idx64 = inv:get_stack(from_list, 64):get_name()
|
|
|
|
|
|
|
|
if castlingWhiteR == 1 and idx64 == "realchess:rook_white_2" then
|
2015-10-27 13:26:08 +03:00
|
|
|
for i = from_index + 1, 63 do
|
|
|
|
if inv:get_stack(from_list, i):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inv:set_stack(from_list, 62, "realchess:rook_white_2")
|
|
|
|
inv:set_stack(from_list, 64, "")
|
|
|
|
check = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif thisMove == "black" then
|
|
|
|
if from_y == 0 and to_y == 0 then
|
|
|
|
if to_x == 1 then
|
2016-02-18 00:53:00 +03:00
|
|
|
local castlingBlackL = meta:get_int("castlingBlackL")
|
|
|
|
local idx1 = inv:get_stack(from_list, 1):get_name()
|
|
|
|
|
|
|
|
if castlingBlackL == 1 and idx1 == "realchess:rook_black_1" then
|
2015-10-27 13:26:08 +03:00
|
|
|
for i = 2, from_index - 1 do
|
|
|
|
if inv:get_stack(from_list, i):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inv:set_stack(from_list, 1, "")
|
|
|
|
inv:set_stack(from_list, 3, "realchess:rook_black_1")
|
|
|
|
check = false
|
|
|
|
end
|
|
|
|
elseif to_x == 6 then
|
2016-02-18 00:53:00 +03:00
|
|
|
local castlingBlackR = meta:get_int("castlingBlackR")
|
|
|
|
local idx8 = inv:get_stack(from_list, 1):get_name()
|
|
|
|
|
|
|
|
if castlingBlackR == 1 and idx8 == "realchess:rook_black_2" then
|
2015-10-27 13:26:08 +03:00
|
|
|
for i = from_index + 1, 7 do
|
|
|
|
if inv:get_stack(from_list, i):get_name() ~= "" then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inv:set_stack(from_list, 6, "realchess:rook_black_2")
|
|
|
|
inv:set_stack(from_list, 8, "")
|
|
|
|
check = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if check then
|
2016-02-18 00:53:00 +03:00
|
|
|
if dx < 0 then dx = -dx end
|
|
|
|
if dy < 0 then dy = -dy end
|
|
|
|
if dx > 1 or dy > 1 then return 0 end
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
2016-12-30 17:04:57 +03:00
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
if thisMove == "white" then
|
|
|
|
meta:set_int("castlingWhiteL", 0)
|
|
|
|
meta:set_int("castlingWhiteR", 0)
|
|
|
|
elseif thisMove == "black" then
|
2016-12-30 17:04:57 +03:00
|
|
|
meta:set_int("castlingBlackL", 0)
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_int("castlingBlackR", 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
meta:set_string("playerWhite", playerWhite)
|
|
|
|
meta:set_string("playerBlack", playerBlack)
|
2016-08-06 01:00:06 +03:00
|
|
|
lastMove = thisMove
|
|
|
|
meta:set_string("lastMove", lastMove)
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_int("lastMoveTime", minetest.get_gametime())
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
if pieceTo:sub(11,14) == "king" then
|
2015-10-27 13:26:08 +03:00
|
|
|
meta:set_string("winner", thisMove)
|
|
|
|
end
|
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
local moves = meta:get_string("moves")
|
|
|
|
local pieceFrom_s = pieceFrom:match(":(%w+_%w+)")
|
|
|
|
local pieceFrom_si_id = pieces_str:match("(%d+)=" .. pieceFrom_s)
|
|
|
|
local pieceTo_s = pieceTo_s ~= "" and pieceTo:match(":(%w+_%w+)") or ""
|
|
|
|
local pieceTo_si_id = pieceTo_s ~= "" and pieces_str:match("(%d+)=" .. pieceTo_s) or ""
|
|
|
|
|
|
|
|
moves = pieceFrom_si_id .. "," ..
|
|
|
|
letters[from_x + 1] .. (from_y + 1) .. "," ..
|
|
|
|
(pieceTo ~= "" and "#33FF33" or "#FFFFFF") .. ", > ,#FFFFFF," ..
|
|
|
|
letters[to_x + 1] .. (to_y + 1) .. "," ..
|
|
|
|
(pieceTo ~= "" and pieceTo_si_id or "69") .. "," ..
|
|
|
|
moves
|
|
|
|
|
|
|
|
meta:set_string("moves", moves)
|
|
|
|
|
|
|
|
local eaten = meta:get_string("eaten")
|
|
|
|
if pieceTo ~= "" then
|
|
|
|
eaten = eaten .. pieceTo_s .. ","
|
|
|
|
end
|
|
|
|
|
|
|
|
meta:set_string("eaten", eaten)
|
|
|
|
|
|
|
|
local eaten_t = string.split(eaten, ",")
|
|
|
|
local eaten_img = ""
|
|
|
|
|
|
|
|
local a, b = 0, 0
|
|
|
|
for i = 1, #eaten_t do
|
|
|
|
local is_white = eaten_t[i]:sub(-5,-1) == "white"
|
|
|
|
local X = (is_white and a or b) % 4
|
|
|
|
local Y = ((is_white and a or b) % 16 - X) / 4
|
|
|
|
|
|
|
|
if is_white then
|
|
|
|
a = a + 1
|
|
|
|
else
|
|
|
|
b = b + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
eaten_img = eaten_img ..
|
|
|
|
"image[" .. ((X + (is_white and 11.7 or 8.8)) - (X * 0.45)) .. "," ..
|
|
|
|
((Y + 5.56) - (Y * 0.2)) .. ";1,1;" .. eaten_t[i] .. ".png]"
|
|
|
|
end
|
|
|
|
|
|
|
|
local black_win = lastMove == "black" and pieceTo:sub(11,14) == "king"
|
|
|
|
local white_win = lastMove == "white" and pieceTo:sub(11,14) == "king"
|
|
|
|
|
|
|
|
local formspec = fs ..
|
|
|
|
"label[2,0.3;" .. (black_win and
|
|
|
|
minetest.colorize("#00FF00", playerBlack .. " has win") or
|
|
|
|
minetest.colorize("#000001",
|
|
|
|
(lastMove == "white" and playerBlack ~= "" and not white_win) and
|
|
|
|
playerBlack .. "..." or playerBlack)) .. "]" ..
|
|
|
|
"label[2,9.15;" .. (white_win and
|
|
|
|
minetest.colorize("#00FF00", playerWhite .. " has win") or
|
|
|
|
minetest.colorize("#000001",
|
|
|
|
(lastMove == "black" and playerWhite ~= "" and not black_win) and
|
|
|
|
playerWhite .. "..." or playerWhite)) .. "]" ..
|
|
|
|
"table[8.9,1.05;5.07,3.75;moves;" .. moves:sub(1,-2) .. ";1]" ..
|
|
|
|
eaten_img
|
|
|
|
|
|
|
|
meta:set_string("formspec", formspec)
|
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2015-11-21 23:00:54 +03:00
|
|
|
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
|
|
|
|
|
2018-08-15 02:54:56 +03:00
|
|
|
if minutes == 0 then
|
|
|
|
return seconds .. " sec."
|
|
|
|
end
|
|
|
|
|
|
|
|
return minutes .. " min. " .. seconds .. " sec."
|
2015-11-21 23:00:54 +03:00
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
function realchess.fields(pos, _, fields, sender)
|
2015-10-27 13:26:08 +03:00
|
|
|
local playerName = sender:get_player_name()
|
|
|
|
local meta = minetest.get_meta(pos)
|
2015-11-21 23:00:54 +03:00
|
|
|
local timeout_limit = meta:get_int("lastMoveTime") + 300
|
2016-02-18 00:53:00 +03:00
|
|
|
local playerWhite = meta:get_string("playerWhite")
|
|
|
|
local playerBlack = meta:get_string("playerBlack")
|
|
|
|
local lastMoveTime = meta:get_int("lastMoveTime")
|
2015-10-27 13:26:08 +03:00
|
|
|
if fields.quit then return end
|
|
|
|
|
2015-11-21 23:00:54 +03:00
|
|
|
-- timeout is 5 min. by default for resetting the game (non-players only)
|
2018-08-15 02:54:56 +03:00
|
|
|
if fields.new then
|
|
|
|
if (playerWhite == playerName or playerBlack == playerName) then
|
|
|
|
realchess.init(pos)
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(playerName, chat_prefix ..
|
|
|
|
"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
|
|
|
|
|
|
|
|
if fields.new and lastMoveTime ~= 0 and minetest.get_gametime() >= timeout_limit and
|
2016-02-18 00:53:00 +03:00
|
|
|
(playerWhite ~= playerName or playerBlack ~= playerName) then
|
2015-10-27 13:26:08 +03:00
|
|
|
realchess.init(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function realchess.dig(pos, player)
|
2016-08-05 22:03:22 +03:00
|
|
|
if not player then
|
|
|
|
return false
|
|
|
|
end
|
2018-08-15 02:54:56 +03:00
|
|
|
|
2015-10-27 13:26:08 +03:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local playerName = player:get_player_name()
|
2015-11-21 23:00:54 +03:00
|
|
|
local timeout_limit = meta:get_int("lastMoveTime") + 300
|
2016-02-18 00:53:00 +03:00
|
|
|
local lastMoveTime = meta:get_int("lastMoveTime")
|
2015-10-27 13:26:08 +03:00
|
|
|
|
2015-11-21 23:00:54 +03:00
|
|
|
-- timeout is 5 min. by default for digging the chessboard (non-players only)
|
2016-02-18 00:53:00 +03:00
|
|
|
return (lastMoveTime == 0 and minetest.get_gametime() > timeout_limit) or
|
2018-08-15 02:54:56 +03:00
|
|
|
minetest.chat_send_player(playerName, chat_prefix ..
|
|
|
|
"You can't dig the chessboard, a game has been started.\n" ..
|
|
|
|
"Reset it first if you're a current player, or dig it again in " ..
|
|
|
|
timeout_format(timeout_limit))
|
2015-10-27 13:26:08 +03:00
|
|
|
end
|
|
|
|
|
2016-02-18 00:53:00 +03:00
|
|
|
function realchess.on_move(pos, from_list, from_index)
|
2015-10-27 13:26:08 +03:00
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
inv:set_stack(from_list, from_index, '')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_node(":realchess:chessboard", {
|
|
|
|
description = "Chess Board",
|
|
|
|
drawtype = "nodebox",
|
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
inventory_image = "chessboard_top.png",
|
|
|
|
wield_image = "chessboard_top.png",
|
2015-11-22 19:02:58 +03:00
|
|
|
tiles = {"chessboard_top.png", "chessboard_top.png", "chessboard_sides.png"},
|
2015-11-16 00:55:12 +03:00
|
|
|
groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
|
2015-10-27 13:26:08 +03:00
|
|
|
sounds = default.node_sound_wood_defaults(),
|
2015-11-22 19:02:58 +03:00
|
|
|
node_box = {type = "fixed", fixed = {-.375, -.5, -.375, .375, -.4375, .375}},
|
2015-10-27 13:26:08 +03:00
|
|
|
sunlight_propagates = true,
|
2015-11-15 22:35:20 +03:00
|
|
|
on_rotate = screwdriver.rotate_simple,
|
2015-10-27 13:26:08 +03:00
|
|
|
can_dig = realchess.dig,
|
|
|
|
on_construct = realchess.init,
|
|
|
|
on_receive_fields = realchess.fields,
|
|
|
|
allow_metadata_inventory_move = realchess.move,
|
|
|
|
on_metadata_inventory_move = realchess.on_move,
|
2016-01-08 00:51:58 +03:00
|
|
|
allow_metadata_inventory_take = function() return 0 end
|
2015-10-27 13:26:08 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
local function register_piece(name, count)
|
|
|
|
for _, color in pairs({"black", "white"}) do
|
|
|
|
if not count then
|
2018-08-15 02:54:56 +03:00
|
|
|
minetest.register_craftitem(":realchess:" .. name .. "_" .. color, {
|
|
|
|
description = color:gsub("^%l", string.upper) .. " " .. name:gsub("^%l", string.upper),
|
|
|
|
inventory_image = name .. "_" .. color .. ".png",
|
2015-10-27 13:26:08 +03:00
|
|
|
stack_max = 1,
|
|
|
|
groups = {not_in_creative_inventory=1}
|
|
|
|
})
|
|
|
|
else
|
|
|
|
for i = 1, count do
|
2018-08-15 02:54:56 +03:00
|
|
|
minetest.register_craftitem(":realchess:" .. name .. "_" .. color .. "_" .. i, {
|
|
|
|
description = color:gsub("^%l", string.upper) .. " " .. name:gsub("^%l", string.upper),
|
|
|
|
inventory_image = name .. "_" .. color .. ".png",
|
2015-10-27 13:26:08 +03:00
|
|
|
stack_max = 1,
|
|
|
|
groups = {not_in_creative_inventory=1}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
register_piece("pawn", 8)
|
|
|
|
register_piece("rook", 2)
|
|
|
|
register_piece("knight", 2)
|
|
|
|
register_piece("bishop", 2)
|
|
|
|
register_piece("queen")
|
|
|
|
register_piece("king")
|
|
|
|
|
2017-08-28 08:41:14 +03:00
|
|
|
-- Recipes
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "realchess:chessboard",
|
|
|
|
recipe = {
|
|
|
|
{"dye:black", "dye:white", "dye:black"},
|
|
|
|
{"stairs:slab_wood", "stairs:slab_wood", "stairs:slab_wood"}
|
|
|
|
}
|
|
|
|
})
|