2015-10-24 14:14:26 +02:00
|
|
|
--[[ TODO:
|
|
|
|
- If a pawn reaches row A or row H -> becomes a queen;
|
|
|
|
- If one of kings is defeat -> the game stops;
|
|
|
|
- Actions recording;
|
|
|
|
- Counter per player.
|
|
|
|
--]]
|
|
|
|
|
2015-10-24 14:10:36 +02:00
|
|
|
realchess = {}
|
|
|
|
|
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]"
|
|
|
|
|
|
|
|
local rows = {
|
|
|
|
{'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7}
|
|
|
|
}
|
|
|
|
local formspec = ""
|
|
|
|
for _, n in pairs(rows) do
|
|
|
|
local letter = n[1]
|
|
|
|
local number = n[2]
|
|
|
|
inv:set_size(letter, 8)
|
|
|
|
formspec = formspec.."list[context;"..letter..";0,"..number..";8,1;false]"
|
|
|
|
end
|
|
|
|
|
|
|
|
meta:set_string("formspec", "size[8,8.6;]bgcolor[#080808BB;true]background[0,0;8,8;chess_bg.png]button[3.2,7.6;2,2;new;New game]"..formspec..slots)
|
|
|
|
meta:set_string("infotext", "Chess Board")
|
|
|
|
meta:set_string("playerOne", "")
|
|
|
|
meta:set_string("playerTwo", "")
|
|
|
|
meta:set_string("lastMove", "")
|
2015-10-25 11:49:23 +01:00
|
|
|
meta:set_string("lastMoveTime", "")
|
2015-10-24 14:10:36 +02:00
|
|
|
|
2015-10-24 22:17:06 +02:00
|
|
|
inv:set_list('A', {"realchess:rook_black_1 1", "realchess:knight_black_1 1",
|
|
|
|
"realchess:bishop_black_1 1", "realchess:king_black_1 1",
|
|
|
|
"realchess:queen_black_1 1", "realchess:bishop_black_2 1",
|
|
|
|
"realchess:knight_black_2 1", "realchess:rook_black_2 1"})
|
2015-10-24 14:10:36 +02:00
|
|
|
|
2015-10-24 22:17:06 +02:00
|
|
|
inv:set_list('H', {"realchess:rook_white_1 1", "realchess:knight_white_1 1",
|
|
|
|
"realchess:bishop_white_1 1", "realchess:queen_white_1 1",
|
|
|
|
"realchess:king_white_1 1", "realchess:bishop_white_2 1",
|
|
|
|
"realchess:knight_white_2 1", "realchess:rook_white_2 1"})
|
2015-10-24 14:10:36 +02:00
|
|
|
|
|
|
|
inv:set_list("C", {})
|
|
|
|
inv:set_list("D", {})
|
|
|
|
inv:set_list("E", {})
|
|
|
|
inv:set_list("F", {})
|
|
|
|
|
|
|
|
local bpawns, wpawns = {}, {}
|
2015-10-24 21:02:43 +02:00
|
|
|
for i = 1, 8 do
|
|
|
|
bpawns[#bpawns+1] = "realchess:pawn_black_"..i.." 1"
|
|
|
|
wpawns[#wpawns+1] = "realchess:pawn_white_"..i.." 1"
|
2015-10-24 14:10:36 +02:00
|
|
|
inv:set_list('B', bpawns)
|
|
|
|
inv:set_list('G', wpawns)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function realchess.move(pos, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
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 playerName = player:get_player_name()
|
|
|
|
local lastMove = meta:get_string("lastMove")
|
|
|
|
local playerWhite = meta:get_string("playerWhite")
|
|
|
|
local playerBlack = meta:get_string("playerBlack")
|
2015-10-24 14:14:26 +02:00
|
|
|
|
2015-10-25 11:49:23 +01:00
|
|
|
if pieceFrom:find("white") then
|
|
|
|
if playerWhite == "" then
|
|
|
|
meta:set_string("playerWhite", playerName)
|
|
|
|
elseif playerWhite ~= "" and playerWhite ~= playerName then
|
|
|
|
minetest.chat_send_player(playerName, "Someone else plays white pieces.")
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if lastMove ~= "white" then
|
|
|
|
meta:set_string("lastMove", "white")
|
|
|
|
meta:set_string("lastMoveTime", minetest.get_gametime())
|
|
|
|
elseif lastMove == "white" then
|
|
|
|
minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif pieceFrom:find("black") then
|
|
|
|
if playerBlack == "" then
|
|
|
|
meta:set_string("playerBlack", playerName)
|
|
|
|
elseif playerBlack ~= "" and playerBlack ~= playerName then
|
|
|
|
minetest.chat_send_player(playerName, "Someone else plays black pieces.")
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if lastMove ~= "black" then
|
|
|
|
meta:set_string("lastMove", "black")
|
|
|
|
meta:set_string("lastMoveTime", minetest.get_gametime())
|
|
|
|
elseif lastMove == "black" then
|
|
|
|
minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
|
|
|
|
return 0
|
|
|
|
end
|
2015-10-24 14:10:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Don't replace pieces of same color
|
2015-10-24 14:14:26 +02:00
|
|
|
if (pieceFrom:find("white") and pieceTo:find("white")) or
|
|
|
|
(pieceFrom:find("black") and pieceTo:find("black")) then
|
2015-10-24 14:10:36 +02:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2015-10-24 14:14:26 +02:00
|
|
|
-- DETERMINISTIC MOVING
|
|
|
|
|
2015-10-24 14:10:36 +02:00
|
|
|
-- PAWNS
|
|
|
|
if pieceFrom:find("pawn_white") then
|
2015-10-24 14:14:26 +02:00
|
|
|
if from_index == to_index and
|
|
|
|
inv:get_stack(string.char(string.byte(from_list)-1), from_index):get_name() == "" then
|
|
|
|
if string.byte(to_list) == string.byte(from_list) - 1 then
|
|
|
|
return 1
|
|
|
|
elseif from_list == 'G' and
|
|
|
|
string.byte(to_list) == string.byte(from_list) - 2 then
|
|
|
|
return 1
|
|
|
|
end
|
2015-10-24 14:10:36 +02:00
|
|
|
elseif string.byte(from_list) > string.byte(to_list) and
|
|
|
|
(from_index ~= to_index and pieceTo:find("black")) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
elseif pieceFrom:find("pawn_black") then
|
2015-10-24 14:14:26 +02:00
|
|
|
if from_index == to_index and
|
|
|
|
inv:get_stack(string.char(string.byte(from_list)+1), from_index):get_name() == "" then
|
|
|
|
if string.byte(to_list) == string.byte(from_list) + 1 then
|
|
|
|
return 1
|
|
|
|
elseif from_list == 'B' and
|
|
|
|
string.byte(to_list) == string.byte(from_list) + 2 then
|
|
|
|
return 1
|
|
|
|
end
|
2015-10-24 14:10:36 +02:00
|
|
|
elseif string.byte(from_list) < string.byte(to_list) and
|
|
|
|
(from_index ~= to_index and pieceTo:find("white")) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-10-24 22:46:21 +02:00
|
|
|
-- ROOKS
|
2015-10-24 22:17:06 +02:00
|
|
|
if pieceFrom:find("rook") then
|
2015-10-24 14:10:36 +02:00
|
|
|
for i = 1, 7 do
|
|
|
|
if from_index == to_index and (string.byte(to_list) == string.byte(from_list) - i or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + i) then
|
|
|
|
return 1
|
|
|
|
elseif string.byte(to_list) == string.byte(from_list) and
|
|
|
|
from_index ~= to_index then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-10-24 22:46:21 +02:00
|
|
|
-- KNIGHTS
|
|
|
|
local knight_dirs = {
|
2015-10-24 14:10:36 +02:00
|
|
|
{-2, -1}, {-2, 1}, {2, 1}, {2, -1}, -- Moves type 1
|
|
|
|
{-1, 2}, {-1, -2}, {1, -2}, {1, 2} -- Moves type 2
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:17:06 +02:00
|
|
|
if pieceFrom:find("knight") then
|
2015-10-24 22:46:21 +02:00
|
|
|
for _, d in pairs(knight_dirs) do
|
2015-10-24 14:10:36 +02:00
|
|
|
if string.byte(to_list) == string.byte(from_list) + d[1] and
|
|
|
|
(to_index == from_index + d[2]) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-10-24 22:46:21 +02:00
|
|
|
-- BISHOPS
|
2015-10-24 22:17:06 +02:00
|
|
|
if pieceFrom:find("bishop") then
|
2015-10-24 14:10:36 +02:00
|
|
|
for i = 1, 7 do
|
|
|
|
if (to_index == from_index + i or to_index == from_index - i) and
|
|
|
|
(string.byte(to_list) == string.byte(from_list) - i or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + i) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- QUEENS
|
|
|
|
if pieceFrom:find("queen") then
|
|
|
|
for i = 1, 7 do
|
|
|
|
if from_index == to_index and (string.byte(to_list) == string.byte(from_list) - i or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + i) then
|
|
|
|
return 1
|
|
|
|
elseif string.byte(to_list) == string.byte(from_list) and
|
|
|
|
from_index ~= to_index then
|
|
|
|
return 1
|
|
|
|
elseif (to_index == from_index + i or to_index == from_index - i) and
|
|
|
|
(string.byte(to_list) == string.byte(from_list) - i or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + i) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- KINGS
|
|
|
|
if pieceFrom:find("king") then
|
|
|
|
if from_index == to_index and (string.byte(to_list) == string.byte(from_list) - 1 or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + 1) then
|
|
|
|
return 1
|
|
|
|
elseif string.byte(to_list) == string.byte(from_list) and
|
|
|
|
from_index ~= to_index then
|
|
|
|
return 1
|
|
|
|
elseif (to_index == from_index + 1 or to_index == from_index - 1) and
|
|
|
|
(string.byte(to_list) == string.byte(from_list) - 1 or
|
|
|
|
string.byte(to_list) == string.byte(from_list) + 1) then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
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-24 14:10:36 +02:00
|
|
|
-- If someone's playing, nobody except the players can reset the game
|
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)
|
|
|
|
elseif fields.new and meta:get_string("lastMoveTime") ~= "" and
|
|
|
|
minetest.get_gametime() >= tonumber(meta:get_string("lastMoveTime") + 250) and
|
|
|
|
(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-25 11:49:23 +01:00
|
|
|
minetest.chat_send_player(playerName, "You can't reset the chessboard, a game has been started.\nIf you weren't playing it, 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-25 11:49:23 +01:00
|
|
|
-- The chess can't be dug while playing unless if nobody has played since 500s
|
|
|
|
if (meta:get_string("playerWhite") ~= "" or meta:get_string("playerBlack") ~= "") and
|
|
|
|
meta:get_string("lastMoveTime") ~= "" and
|
|
|
|
minetest.get_gametime() <= tonumber(meta:get_string("lastMoveTime") + 250) then
|
|
|
|
minetest.chat_send_player(playerName, "You can't dug the chessboard, a game has been started.\nIf you weren't playing it, try again after a while.")
|
2015-10-24 14:10:36 +02:00
|
|
|
return false
|
|
|
|
end
|
2015-10-25 11:49:23 +01:00
|
|
|
|
2015-10-24 14:10:36 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
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"},
|
|
|
|
groups = {choppy=3, fammable=3},
|
|
|
|
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,
|
|
|
|
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
2015-10-25 11:49:23 +01:00
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
2015-10-24 21:02:43 +02:00
|
|
|
inv:set_stack(from_list, from_index, '')
|
|
|
|
end
|
2015-10-24 14:10:36 +02:00
|
|
|
})
|
|
|
|
|
2015-10-24 21:02:43 +02:00
|
|
|
local pieces = {
|
|
|
|
{name = "pawn", count = 8},
|
2015-10-24 22:17:06 +02:00
|
|
|
{name = "rook", count = 2},
|
|
|
|
{name = "knight", count = 2},
|
|
|
|
{name = "bishop", count = 2},
|
2015-10-24 21:02:43 +02:00
|
|
|
{name = "queen", count = 1},
|
|
|
|
{name = "king", count = 1}
|
|
|
|
}
|
2015-10-24 14:10:36 +02:00
|
|
|
local colors = {"black", "white"}
|
|
|
|
|
|
|
|
for _, p in pairs(pieces) do
|
|
|
|
for _, c in pairs(colors) do
|
2015-10-24 21:02:43 +02:00
|
|
|
for i = 1, p.count do
|
|
|
|
minetest.register_craftitem("realchess:"..p.name.."_"..c.."_"..i, {
|
|
|
|
description = c:gsub("^%l", string.upper).." "..p.name:gsub("^%l", string.upper),
|
|
|
|
inventory_image = p.name.."_"..c..".png",
|
2015-10-24 14:10:36 +02:00
|
|
|
stack_max = 1,
|
|
|
|
groups = {not_in_creative_inventory=1}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2015-10-24 21:02:43 +02:00
|
|
|
end
|
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"}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|