2015-10-25 16:18:37 +01:00
local realchess = { }
2015-10-24 14:10:36 +02:00
2015-10-25 15:23:50 +01:00
local function index_to_xy ( index )
index = index - 1
local x = index % 8
local y = ( index - x ) / 8
return x , y
end
local function xy_to_index ( x , y )
return x + y * 8 + 1
end
2015-10-25 11:49:23 +01:00
function realchess . init ( pos )
2015-10-24 14:10:36 +02:00
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
local slots = " listcolors[#00000000;#00000000;#00000000;#30434C;#FFF] "
2015-10-25 16:38:05 +01:00
local formspec
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
inv : set_size ( " board " , 64 )
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
meta : set_string ( " formspec " ,
" size[8,8.6;] " ..
" bgcolor[#080808BB;true] " ..
" background[0,0;8,8;chess_bg.png] " ..
2015-10-25 18:37:05 +01:00
" button[3.1,7.8;2,2;new;New game] " ..
2015-10-25 15:23:50 +01:00
" list[context;board;0,0;8,8;] " ..
slots )
2015-10-27 11:06:16 +01:00
2015-10-24 14:10:36 +02:00
meta : set_string ( " infotext " , " Chess Board " )
2015-10-25 15:23:50 +01:00
meta : set_string ( " playerBlack " , " " )
meta : set_string ( " playerWhite " , " " )
2015-10-24 14:10:36 +02:00
meta : set_string ( " lastMove " , " " )
2015-10-27 11:06:16 +01:00
meta : set_int ( " lastMoveTime " , 0 )
2015-10-25 16:54:00 +01:00
meta : set_string ( " winner " , " " )
2015-10-26 22:08:00 +01:00
meta : set_int ( " castlingBlackL " , 1 )
meta : set_int ( " castlingBlackR " , 1 )
meta : set_int ( " castlingWhiteL " , 1 )
meta : set_int ( " castlingWhiteR " , 1 )
2015-10-27 11:06:16 +01:00
2015-10-25 15:23:50 +01:00
inv : set_list ( " board " , {
" realchess:rook_black_1 " ,
" realchess:knight_black_1 " ,
" realchess:bishop_black_1 " ,
" realchess:queen_black " ,
2015-10-26 10:11:56 +01:00
" realchess:king_black " ,
2015-10-25 15:23:50 +01:00
" 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 " ,
2015-10-26 17:40:25 +01:00
" realchess:rook_white_2 "
2015-10-25 15:23:50 +01:00
} )
2015-10-24 14:10:36 +02:00
end
function realchess . move ( pos , from_list , from_index , to_list , to_index , count , player )
2015-10-25 15:23:50 +01:00
if from_list ~= " board " and to_list ~= " board " then
return 0
end
2015-10-26 17:40:25 +01:00
2015-10-25 16:54:00 +01:00
local playerName = player : get_player_name ( )
2015-10-24 14:10:36 +02:00
local meta = minetest.get_meta ( pos )
2015-10-26 17:40:25 +01:00
2015-10-25 16:54:00 +01:00
if meta : get_string ( " winner " ) ~= " " then
minetest.chat_send_player ( playerName , " This game is over. " )
return 0
end
local inv = meta : get_inventory ( )
2015-10-24 14:10:36 +02:00
local pieceFrom = inv : get_stack ( from_list , from_index ) : get_name ( )
local pieceTo = inv : get_stack ( to_list , to_index ) : get_name ( )
2015-10-25 11:49:23 +01:00
local lastMove = meta : get_string ( " lastMove " )
2015-10-25 16:18:37 +01:00
local thisMove -- will replace lastMove when move is legal
2015-10-25 11:49:23 +01:00
local playerWhite = meta : get_string ( " playerWhite " )
local playerBlack = meta : get_string ( " playerBlack " )
2015-10-26 17:40:25 +01:00
2015-10-25 11:49:23 +01:00
if pieceFrom : find ( " white " ) then
2015-10-25 15:23:50 +01:00
if playerWhite ~= " " and playerWhite ~= playerName then
2015-10-26 17:40:25 +01:00
minetest.chat_send_player ( playerName , " Someone else plays white pieces! " )
2015-10-25 11:49:23 +01:00
return 0
2015-10-25 15:23:50 +01:00
end
if lastMove ~= " " and lastMove ~= " black " then
2015-10-25 11:49:23 +01:00
minetest.chat_send_player ( playerName , " It's not your turn, wait for your opponent to play. " )
return 0
end
2015-10-25 15:23:50 +01:00
if pieceTo : find ( " white " ) then
-- Don't replace pieces of same color
return 0
end
playerWhite = playerName
thisMove = " white "
2015-10-25 11:49:23 +01:00
elseif pieceFrom : find ( " black " ) then
2015-10-25 15:23:50 +01:00
if playerBlack ~= " " and playerBlack ~= playerName then
2015-10-26 17:40:25 +01:00
minetest.chat_send_player ( playerName , " Someone else plays black pieces! " )
2015-10-25 11:49:23 +01:00
return 0
end
2015-10-25 15:23:50 +01:00
if lastMove ~= " " and lastMove ~= " white " then
2015-10-25 11:49:23 +01:00
minetest.chat_send_player ( playerName , " It's not your turn, wait for your opponent to play. " )
return 0
end
2015-10-25 15:23:50 +01:00
if pieceTo : find ( " black " ) then
-- Don't replace pieces of same color
return 0
end
playerBlack = playerName
thisMove = " black "
2015-10-24 14:10:36 +02:00
end
2015-10-26 17:40:25 +01:00
2015-10-24 14:14:26 +02:00
-- DETERMINISTIC MOVING
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
local from_x , from_y = index_to_xy ( from_index )
local to_x , to_y = index_to_xy ( to_index )
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
if pieceFrom : find ( " pawn " ) then
if thisMove == " white " then
-- white pawns can go up only
2015-10-25 15:50:26 +01:00
if from_y - 1 == to_y then
if from_x == to_x then
if pieceTo ~= " " then
return 0
2015-10-26 12:13:58 +01:00
elseif to_index >= 1 and to_index <= 8 then
inv : set_stack ( from_list , from_index , " realchess:queen_white " )
2015-10-25 15:50:26 +01:00
end
elseif from_x - 1 == to_x or from_x + 1 == to_x then
if not pieceTo : find ( " black " ) then
return 0
end
else
2015-10-25 15:23:50 +01:00
return 0
2015-10-24 14:14:26 +02:00
end
2015-10-25 15:50:26 +01:00
elseif from_y - 2 == to_y then
if pieceTo ~= " " or from_y < 6 or inv : get_stack ( from_list , xy_to_index ( from_x , from_y - 1 ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
2015-10-24 14:14:26 +02:00
end
2015-10-25 15:23:50 +01:00
else
return 0
2015-10-24 14:10:36 +02:00
end
2015-10-25 15:23:50 +01:00
elseif thisMove == " black " then
-- black pawns can go down only
2015-10-25 15:50:26 +01:00
if from_y + 1 == to_y then
if from_x == to_x then
if pieceTo ~= " " then
return 0
2015-10-26 22:08:00 +01:00
elseif to_index >= 57 and to_index <= 64 then
2015-10-26 12:13:58 +01:00
inv : set_stack ( from_list , from_index , " realchess:queen_black " )
2015-10-25 15:50:26 +01:00
end
elseif from_x - 1 == to_x or from_x + 1 == to_x then
2015-10-25 16:57:01 +01:00
if not pieceTo : find ( " white " ) then
return 0
end
else
2015-10-25 15:50:26 +01:00
return 0
end
elseif from_y + 2 == to_y then
if pieceTo ~= " " or from_y > 1 or inv : get_stack ( from_list , xy_to_index ( from_x , from_y + 1 ) ) : get_name ( ) ~= " " then
return 0
end
else
2015-10-25 15:23:50 +01:00
return 0
2015-10-24 14:10:36 +02:00
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01: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
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
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
2015-10-24 14:10:36 +02:00
end
2015-10-25 16:38:05 +01:00
2015-10-25 15:23:50 +01:00
elseif pieceFrom : find ( " rook " ) then
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
2015-10-24 14:10:36 +02:00
end
2015-10-25 15:23:50 +01:00
else
-- attempt to move arbitrarily -> abort
return 0
2015-10-24 14:10:36 +02:00
end
2015-10-25 16:38:05 +01:00
2015-10-26 22:08:00 +01:00
if thisMove == " white " then
if pieceFrom : find ( " 1 " ) then
meta : set_int ( " castlingWhiteL " , 0 )
elseif pieceFrom : find ( " 2 " ) then
meta : set_int ( " castlingWhiteR " , 0 )
end
elseif thisMove == " black " then
if pieceFrom : find ( " 1 " ) then
meta : set_int ( " castlingWhiteL " , 0 )
elseif pieceFrom : find ( " 2 " ) then
meta : set_int ( " castlingWhiteR " , 0 )
end
end
2015-10-27 11:06:16 +01:00
2015-10-25 15:23:50 +01:00
elseif pieceFrom : find ( " knight " ) then
-- get relative pos
local dx = from_x - to_x
local dy = from_y - to_y
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- get absolute values
if dx < 0 then
dx = - dx
end
if dy < 0 then
dy = - dy
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- sort x and y
if dx > dy then
dx , dy = dy , dx
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01: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
2015-10-24 14:10:36 +02:00
2015-10-25 15:23:50 +01:00
elseif pieceFrom : find ( " bishop " ) then
-- get relative pos
local dx = from_x - to_x
local dy = from_y - to_y
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- get absolute values
if dx < 0 then
dx = - dx
end
if dy < 0 then
dy = - dy
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- ensure dx and dy are equal
if dx ~= dy then
return 0
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01: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
2015-10-25 16:59:36 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x + i , from_y - i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
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
2015-10-25 16:59:36 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x - i , from_y + i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
else
-- moving left-up
-- ensure that no piece disturbs the way
for i = 1 , dx - 1 do
2015-10-25 16:59:36 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x - i , from_y - i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
2015-10-24 14:10:36 +02:00
end
end
2015-10-25 18:37:05 +01:00
2015-10-25 15:23:50 +01:00
elseif pieceFrom : find ( " queen " ) then
local dx = from_x - to_x
local dy = from_y - to_y
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- get absolute values
if dx < 0 then
dx = - dx
end
if dy < 0 then
dy = - dy
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
-- ensure valid relative move
if dx ~= 0 and dy ~= 0 and dx ~= dy then
return 0
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
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
2015-10-25 15:35:45 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x , from_y - i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
end
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
2015-10-25 15:35:45 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x + i , from_y - i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
end
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
2015-10-25 15:35:45 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x - i , from_y ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
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
2015-10-25 15:35:45 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x - i , from_y + i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
else
-- goes left-up
-- ensure that no piece disturbs the way
for i = 1 , dx - 1 do
2015-10-25 15:35:45 +01:00
if inv : get_stack ( from_list , xy_to_index ( from_x - i , from_y - i ) ) : get_name ( ) ~= " " then
2015-10-25 15:23:50 +01:00
return 0
end
end
end
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
elseif pieceFrom : find ( " king " ) then
local dx = from_x - to_x
local dy = from_y - to_y
2015-10-26 22:08:00 +01:00
local check = true
2015-10-25 15:23:50 +01:00
2015-10-26 22:08:00 +01:00
if thisMove == " white " then
if from_y == 7 and to_y == 7 then
if to_x == 1 then
if meta : get_int ( " castlingWhiteL " ) == 1 and inv : get_stack ( from_list , 57 ) : get_name ( ) == " realchess:rook_white_1 " then
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
if meta : get_int ( " castlingWhiteR " ) == 1 and inv : get_stack ( from_list , 64 ) : get_name ( ) == " realchess:rook_white_2 " then
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
if meta : get_int ( " castlingBlackL " ) == 1 and inv : get_stack ( from_list , 1 ) : get_name ( ) == " realchess:rook_black_1 " then
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
if meta : get_int ( " castlingBlackR " ) == 1 and inv : get_stack ( from_list , 8 ) : get_name ( ) == " realchess:rook_black_2 " then
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
2015-10-25 15:23:50 +01:00
end
2015-10-27 11:06:16 +01:00
2015-10-26 22:08:00 +01:00
if check then
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-25 15:23:50 +01:00
end
2015-10-26 22:08:00 +01:00
if thisMove == " white " then
meta : set_int ( " castlingWhiteL " , 0 )
meta : set_int ( " castlingWhiteR " , 0 )
elseif thisMove == " black " then
meta : set_int ( " castlingBlackL " , 0 )
meta : set_int ( " castlingBlackR " , 0 )
2015-10-24 14:10:36 +02:00
end
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
meta : set_string ( " playerWhite " , playerWhite )
meta : set_string ( " playerBlack " , playerBlack )
meta : set_string ( " lastMove " , thisMove )
2015-10-27 11:06:16 +01:00
meta : set_int ( " lastMoveTime " , minetest.get_gametime ( ) )
2015-10-26 17:40:25 +01:00
2015-10-25 18:37:05 +01:00
if meta : get_string ( " lastMove " ) == " black " then
2015-10-26 17:40:25 +01:00
minetest.chat_send_player ( playerWhite , playerName .. " has moved a " .. pieceFrom : match ( " %a+:(%a+) " ) .. " , it's now your turn. " )
2015-10-25 18:37:05 +01:00
elseif meta : get_string ( " lastMove " ) == " white " then
2015-10-26 17:40:25 +01:00
minetest.chat_send_player ( playerBlack , playerName .. " has moved a " .. pieceFrom : match ( " %a+:(%a+) " ) .. " , it's now your turn. " )
2015-10-25 18:37:05 +01:00
end
2015-10-26 17:40:25 +01:00
2015-10-25 16:54:00 +01:00
if pieceTo : find ( " king " ) then
2015-10-26 17:40:25 +01:00
minetest.chat_send_player ( playerBlack , playerName .. " won the game. " )
minetest.chat_send_player ( playerWhite , playerName .. " won the game. " )
2015-10-25 16:54:00 +01:00
meta : set_string ( " winner " , thisMove )
end
2015-10-26 17:40:25 +01:00
2015-10-25 15:23:50 +01:00
return 1
2015-10-24 14:10:36 +02:00
end
2015-10-26 17:40:25 +01:00
2015-10-24 14:10:36 +02:00
function realchess . fields ( pos , formname , fields , sender )
2015-10-25 11:49:23 +01:00
local playerName = sender : get_player_name ( )
2015-10-24 14:10:36 +02:00
local meta = minetest.get_meta ( pos )
if fields.quit then return end
2015-10-25 11:49:23 +01:00
2015-10-27 11:06:16 +01:00
-- the chess can't be reset during a started game unless if nobody has played during a while (~5 min. by default)
2015-10-25 11:49:23 +01:00
if fields.new and ( meta : get_string ( " playerWhite " ) == playerName or
meta : get_string ( " playerBlack " ) == playerName ) then
realchess.init ( pos )
2015-10-27 11:06:16 +01:00
elseif fields.new and meta : get_int ( " lastMoveTime " ) ~= 0 and
minetest.get_gametime ( ) >= meta : get_int ( " lastMoveTime " ) + 250 and
2015-10-25 11:49:23 +01:00
( meta : get_string ( " playerWhite " ) ~= playerName or
meta : get_string ( " playerBlack " ) ~= playerName ) then
realchess.init ( pos )
2015-10-24 14:10:36 +02:00
else
2015-10-26 17:40:25 +01:00
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 after a while. " )
2015-10-24 14:10:36 +02:00
end
end
function realchess . dig ( pos , player )
local meta = minetest.get_meta ( pos )
2015-10-25 11:49:23 +01:00
local playerName = player : get_player_name ( )
2015-10-24 14:10:36 +02:00
2015-10-27 11:06:16 +01:00
-- 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 " ) + 250 then
2015-10-26 17:40:25 +01:00
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 try digging again after a while. " )
2015-10-24 14:10:36 +02:00
return false
end
2015-10-26 17:40:25 +01:00
2015-10-24 14:10:36 +02:00
return true
end
2015-10-25 15:23:50 +01:00
function realchess . on_move ( pos , from_list , from_index , to_list , to_index , count , player )
local inv = minetest.get_meta ( pos ) : get_inventory ( )
inv : set_stack ( from_list , from_index , ' ' )
return false
end
2015-10-25 16:18:37 +01:00
function realchess . take ( pos , listname , index , stack , player )
return 0
end
2015-10-24 14:10:36 +02:00
minetest.register_node ( " realchess:chessboard " , {
description = " Chess Board " ,
drawtype = " nodebox " ,
paramtype = " light " ,
paramtype2 = " facedir " ,
2015-10-24 14:14:26 +02:00
inventory_image = " chessboard_top.png " ,
wield_image = " chessboard_top.png " ,
2015-10-24 14:10:36 +02:00
tiles = { " chessboard_top.png " , " chessboard_top.png " ,
" chessboard_sides.png " , " chessboard_sides.png " ,
" chessboard_top.png " , " chessboard_top.png " } ,
2015-10-25 15:23:50 +01:00
groups = { choppy = 3 , flammable = 3 } ,
2015-10-24 14:10:36 +02:00
sounds = default.node_sound_wood_defaults ( ) ,
node_box = { type = " fixed " , fixed = { - 0.5 , - 0.5 , - 0.5 , 0.5 , - 0.4375 , 0.5 } } ,
2015-10-24 22:46:21 +02:00
sunlight_propagates = true ,
2015-10-24 14:10:36 +02:00
can_dig = realchess.dig ,
2015-10-25 11:49:23 +01:00
on_construct = realchess.init ,
2015-10-24 14:10:36 +02:00
on_receive_fields = realchess.fields ,
2015-10-24 21:02:43 +02:00
allow_metadata_inventory_move = realchess.move ,
2015-10-25 16:18:37 +01:00
on_metadata_inventory_move = realchess.on_move ,
allow_metadata_inventory_take = realchess.take
2015-10-24 14:10:36 +02:00
} )
2015-10-25 15:23:50 +01:00
local function register_piece ( name , count )
for _ , color in pairs ( { " black " , " white " } ) do
if not count then
minetest.register_craftitem ( " realchess: " .. name .. " _ " .. color , {
description = color : gsub ( " ^%l " , string.upper ) .. " " .. name : gsub ( " ^%l " , string.upper ) ,
inventory_image = name .. " _ " .. color .. " .png " ,
stack_max = 1 ,
2015-10-26 17:40:25 +01:00
groups = { not_in_creative_inventory = 1 }
2015-10-25 15:23:50 +01:00
} )
else
for i = 1 , count do
minetest.register_craftitem ( " realchess: " .. name .. " _ " .. color .. " _ " .. i , {
description = color : gsub ( " ^%l " , string.upper ) .. " " .. name : gsub ( " ^%l " , string.upper ) ,
inventory_image = name .. " _ " .. color .. " .png " ,
stack_max = 1 ,
2015-10-26 17:40:25 +01:00
groups = { not_in_creative_inventory = 1 }
2015-10-25 15:23:50 +01:00
} )
end
end
end
2015-10-24 21:02:43 +02:00
end
2015-10-24 14:10:36 +02:00
2015-10-25 15:23:50 +01:00
register_piece ( " pawn " , 8 )
register_piece ( " rook " , 2 )
register_piece ( " knight " , 2 )
register_piece ( " bishop " , 2 )
register_piece ( " queen " )
register_piece ( " king " )
2015-10-24 14:10:36 +02:00
minetest.register_craft ( {
output = " realchess:chessboard " ,
recipe = {
{ " dye:black " , " dye:white " , " dye:black " } ,
{ " stairs:slab_wood " , " stairs:slab_wood " , " stairs:slab_wood " }
}
} )
2015-10-25 16:38:05 +01:00