1 -- See https://github.com/kilbith/realchess for the main repository
4 screwdriver = screwdriver or {}
6 local function index_to_xy(index)
9 local y = (index - x) / 8
13 local function xy_to_index(x, y)
17 function realchess.init(pos)
18 local meta = minetest.get_meta(pos)
19 local inv = meta:get_inventory()
21 inv:set_size("board", 64)
23 local formspec = [[ size[8,8.6;]
24 bgcolor[#080808BB;true]
25 background[0,0;8,8;chess_bg.png]
26 button[3.1,7.8;2,2;new;New game]
27 list[context;board;0,0;8,8;]
28 listcolors[#00000000;#00000000;#00000000;#30434C;#FFF] ]]
30 meta:set_string("formspec", formspec)
32 meta:set_string("infotext", "Chess Board")
33 meta:set_string("playerBlack", "")
34 meta:set_string("playerWhite", "")
35 meta:set_string("lastMove", "")
36 meta:set_int("lastMoveTime", 0)
37 meta:set_string("winner", "")
38 meta:set_int("castlingBlackL", 1)
39 meta:set_int("castlingBlackR", 1)
40 meta:set_int("castlingWhiteL", 1)
41 meta:set_int("castlingWhiteR", 1)
43 inv:set_list("board", {
44 "realchess:rook_black_1",
45 "realchess:knight_black_1",
46 "realchess:bishop_black_1",
47 "realchess:queen_black",
48 "realchess:king_black",
49 "realchess:bishop_black_2",
50 "realchess:knight_black_2",
51 "realchess:rook_black_2",
52 "realchess:pawn_black_1",
53 "realchess:pawn_black_2",
54 "realchess:pawn_black_3",
55 "realchess:pawn_black_4",
56 "realchess:pawn_black_5",
57 "realchess:pawn_black_6",
58 "realchess:pawn_black_7",
59 "realchess:pawn_black_8",
60 "", "", "", "", "", "", "", "",
61 "", "", "", "", "", "", "", "",
62 "", "", "", "", "", "", "", "",
63 "", "", "", "", "", "", "", "",
64 "realchess:pawn_white_1",
65 "realchess:pawn_white_2",
66 "realchess:pawn_white_3",
67 "realchess:pawn_white_4",
68 "realchess:pawn_white_5",
69 "realchess:pawn_white_6",
70 "realchess:pawn_white_7",
71 "realchess:pawn_white_8",
72 "realchess:rook_white_1",
73 "realchess:knight_white_1",
74 "realchess:bishop_white_1",
75 "realchess:queen_white",
76 "realchess:king_white",
77 "realchess:bishop_white_2",
78 "realchess:knight_white_2",
79 "realchess:rook_white_2"
83 function realchess.move(pos, from_list, from_index, to_list, to_index, count, player)
84 if from_list ~= "board" and to_list ~= "board" then
88 local playerName = player:get_player_name()
89 local meta = minetest.get_meta(pos)
91 if meta:get_string("winner") ~= "" then
92 minetest.chat_send_player(playerName, "This game is over.")
96 local inv = meta:get_inventory()
97 local pieceFrom = inv:get_stack(from_list, from_index):get_name()
98 local pieceTo = inv:get_stack(to_list, to_index):get_name()
99 local lastMove = meta:get_string("lastMove")
100 local thisMove -- will replace lastMove when move is legal
101 local playerWhite = meta:get_string("playerWhite")
102 local playerBlack = meta:get_string("playerBlack")
104 if pieceFrom:find("white") then
105 if playerWhite ~= "" and playerWhite ~= playerName then
106 minetest.chat_send_player(playerName, "Someone else plays white pieces!")
109 if lastMove ~= "" and lastMove ~= "black" then
110 minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
113 if pieceTo:find("white") then
114 -- Don't replace pieces of same color
117 playerWhite = playerName
119 elseif pieceFrom:find("black") then
120 if playerBlack ~= "" and playerBlack ~= playerName then
121 minetest.chat_send_player(playerName, "Someone else plays black pieces!")
124 if lastMove ~= "" and lastMove ~= "white" then
125 minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
128 if pieceTo:find("black") then
129 -- Don't replace pieces of same color
132 playerBlack = playerName
136 -- DETERMINISTIC MOVING
138 local from_x, from_y = index_to_xy(from_index)
139 local to_x, to_y = index_to_xy(to_index)
141 if pieceFrom:find("pawn") then
142 if thisMove == "white" then
143 -- white pawns can go up only
144 if from_y - 1 == to_y then
145 if from_x == to_x then
146 if pieceTo ~= "" then
148 elseif to_index >= 1 and to_index <= 8 then
149 inv:set_stack(from_list, from_index, "realchess:queen_white")
151 elseif from_x - 1 == to_x or from_x + 1 == to_x then
152 if not pieceTo:find("black") then
154 elseif to_index >= 1 and to_index <= 8 then
155 inv:set_stack(from_list, from_index, "realchess:queen_white")
160 elseif from_y - 2 == to_y then
161 if pieceTo ~= "" or from_y < 6 or inv:get_stack(from_list, xy_to_index(from_x, from_y - 1)):get_name() ~= "" then
167 elseif thisMove == "black" then
168 -- black pawns can go down only
169 if from_y + 1 == to_y then
170 if from_x == to_x then
171 if pieceTo ~= "" then
173 elseif to_index >= 57 and to_index <= 64 then
174 inv:set_stack(from_list, from_index, "realchess:queen_black")
176 elseif from_x - 1 == to_x or from_x + 1 == to_x then
177 if not pieceTo:find("white") then
179 elseif to_index >= 57 and to_index <= 64 then
180 inv:set_stack(from_list, from_index, "realchess:queen_black")
185 elseif from_y + 2 == to_y then
186 if pieceTo ~= "" or from_y > 1 or inv:get_stack(from_list, xy_to_index(from_x, from_y + 1)):get_name() ~= "" then
194 -- ensure that destination cell is empty
195 -- elseif x changed one unit left or right
196 -- ensure the pawn is killing opponent piece
198 -- move is not legal - abort
200 if from_x == to_x then
201 if pieceTo ~= "" then
204 elseif from_x - 1 == to_x or from_x + 1 == to_x then
205 if not pieceTo:find("white") then
215 elseif pieceFrom:find("rook") then
216 if from_x == to_x then
218 if from_y < to_y then
220 -- ensure that no piece disturbs the way
221 for i = from_y + 1, to_y - 1 do
222 if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
228 -- ensure that no piece disturbs the way
229 for i = to_y + 1, from_y - 1 do
230 if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
235 elseif from_y == to_y then
236 -- mocing horizontally
237 if from_x < to_x then
239 -- ensure that no piece disturbs the way
240 for i = from_x + 1, to_x - 1 do
241 if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
247 -- ensure that no piece disturbs the way
248 for i = to_x + 1, from_x - 1 do
249 if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
255 -- attempt to move arbitrarily -> abort
259 if thisMove == "white" then
260 if pieceFrom:find("1") then
261 meta:set_int("castlingWhiteL", 0)
262 elseif pieceFrom:find("2") then
263 meta:set_int("castlingWhiteR", 0)
265 elseif thisMove == "black" then
266 if pieceFrom:find("1") then
267 meta:set_int("castlingWhiteL", 0)
268 elseif pieceFrom:find("2") then
269 meta:set_int("castlingWhiteR", 0)
273 elseif pieceFrom:find("knight") then
275 local dx = from_x - to_x
276 local dy = from_y - to_y
278 -- get absolute values
291 -- ensure that dx == 1 and dy == 2
292 if dx ~= 1 or dy ~= 2 then
295 -- just ensure that destination cell does not contain friend piece
296 -- ^ it was done already thus everything ok
298 elseif pieceFrom:find("bishop") then
300 local dx = from_x - to_x
301 local dy = from_y - to_y
303 -- get absolute values
311 -- ensure dx and dy are equal
316 if from_x < to_x then
317 if from_y < to_y then
319 -- ensure that no piece disturbs the way
321 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
327 -- ensure that no piece disturbs the way
329 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
335 if from_y < to_y then
337 -- ensure that no piece disturbs the way
339 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
345 -- ensure that no piece disturbs the way
347 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
354 elseif pieceFrom:find("queen") then
355 local dx = from_x - to_x
356 local dy = from_y - to_y
358 -- get absolute values
366 -- ensure valid relative move
367 if dx ~= 0 and dy ~= 0 and dx ~= dy then
371 if from_x == to_x then
372 if from_y < to_y then
374 -- ensure that no piece disturbs the way
376 if inv:get_stack(from_list, xy_to_index(from_x, from_y + i)):get_name() ~= "" then
382 -- ensure that no piece disturbs the way
384 if inv:get_stack(from_list, xy_to_index(from_x, from_y - i)):get_name() ~= "" then
389 elseif from_x < to_x then
390 if from_y == to_y then
392 -- ensure that no piece disturbs the way
394 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y)):get_name() ~= "" then
398 elseif from_y < to_y then
400 -- ensure that no piece disturbs the way
402 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
408 -- ensure that no piece disturbs the way
410 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
416 if from_y == to_y then
418 -- ensure that no piece disturbs the way and destination cell does
420 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y)):get_name() ~= "" then
424 elseif from_y < to_y then
426 -- ensure that no piece disturbs the way
428 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
434 -- ensure that no piece disturbs the way
436 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
443 elseif pieceFrom:find("king") then
444 local dx = from_x - to_x
445 local dy = from_y - to_y
448 if thisMove == "white" then
449 if from_y == 7 and to_y == 7 then
451 if meta:get_int("castlingWhiteL") == 1 and inv:get_stack(from_list, 57):get_name() == "realchess:rook_white_1" then
452 for i = 58, from_index - 1 do
453 if inv:get_stack(from_list, i):get_name() ~= "" then
457 inv:set_stack(from_list, 57, "")
458 inv:set_stack(from_list, 59, "realchess:rook_white_1")
461 elseif to_x == 6 then
462 if meta:get_int("castlingWhiteR") == 1 and inv:get_stack(from_list, 64):get_name() == "realchess:rook_white_2" then
463 for i = from_index + 1, 63 do
464 if inv:get_stack(from_list, i):get_name() ~= "" then
468 inv:set_stack(from_list, 62, "realchess:rook_white_2")
469 inv:set_stack(from_list, 64, "")
474 elseif thisMove == "black" then
475 if from_y == 0 and to_y == 0 then
477 if meta:get_int("castlingBlackL") == 1 and inv:get_stack(from_list, 1):get_name() == "realchess:rook_black_1" then
478 for i = 2, from_index - 1 do
479 if inv:get_stack(from_list, i):get_name() ~= "" then
483 inv:set_stack(from_list, 1, "")
484 inv:set_stack(from_list, 3, "realchess:rook_black_1")
487 elseif to_x == 6 then
488 if meta:get_int("castlingBlackR") == 1 and inv:get_stack(from_list, 8):get_name() == "realchess:rook_black_2" then
489 for i = from_index + 1, 7 do
490 if inv:get_stack(from_list, i):get_name() ~= "" then
494 inv:set_stack(from_list, 6, "realchess:rook_black_2")
495 inv:set_stack(from_list, 8, "")
510 if dx > 1 or dy > 1 then
515 if thisMove == "white" then
516 meta:set_int("castlingWhiteL", 0)
517 meta:set_int("castlingWhiteR", 0)
518 elseif thisMove == "black" then
519 meta:set_int("castlingBlackL", 0)
520 meta:set_int("castlingBlackR", 0)
524 meta:set_string("playerWhite", playerWhite)
525 meta:set_string("playerBlack", playerBlack)
526 meta:set_string("lastMove", thisMove)
527 meta:set_int("lastMoveTime", minetest.get_gametime())
529 if meta:get_string("lastMove") == "black" then
530 minetest.chat_send_player(playerWhite, "["..os.date("%H:%M:%S").."] "..
531 playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
532 elseif meta:get_string("lastMove") == "white" then
533 minetest.chat_send_player(playerBlack, "["..os.date("%H:%M:%S").."] "..
534 playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
537 if pieceTo:find("king") then
538 minetest.chat_send_player(playerBlack, playerName.." won the game.")
539 minetest.chat_send_player(playerWhite, playerName.." won the game.")
540 meta:set_string("winner", thisMove)
546 local function timeout_format(timeout_limit)
547 local time_remaining = timeout_limit - minetest.get_gametime()
548 local minutes = math.floor(time_remaining / 60)
549 local seconds = time_remaining % 60
551 if minutes == 0 then return seconds.." sec." end
552 return minutes.." min. "..seconds.." sec."
555 function realchess.fields(pos, formname, fields, sender)
556 local playerName = sender:get_player_name()
557 local meta = minetest.get_meta(pos)
558 local timeout_limit = meta:get_int("lastMoveTime") + 300
559 if fields.quit then return end
561 -- timeout is 5 min. by default for resetting the game (non-players only)
562 if fields.new and (meta:get_string("playerWhite") == playerName or
563 meta:get_string("playerBlack") == playerName) then
565 elseif fields.new and meta:get_int("lastMoveTime") ~= 0 and
566 minetest.get_gametime() >= timeout_limit and
567 (meta:get_string("playerWhite") ~= playerName or
568 meta:get_string("playerBlack") ~= playerName) then
571 minetest.chat_send_player(playerName, "[!] You can't reset the chessboard, a game has been started.\n"..
572 "If you are not a current player, try again in "..timeout_format(timeout_limit))
576 function realchess.dig(pos, player)
577 local meta = minetest.get_meta(pos)
578 local playerName = player:get_player_name()
579 local timeout_limit = meta:get_int("lastMoveTime") + 300
581 -- timeout is 5 min. by default for digging the chessboard (non-players only)
582 if meta:get_int("lastMoveTime") ~= 0 and minetest.get_gametime() <= timeout_limit then
583 minetest.chat_send_player(playerName, "[!] You can't dig the chessboard, a game has been started.\n"..
584 "Reset it first if you're a current player, or dig again in "..timeout_format(timeout_limit))
591 function realchess.on_move(pos, from_list, from_index, to_list, to_index, count, player)
592 local inv = minetest.get_meta(pos):get_inventory()
593 inv:set_stack(from_list, from_index, '')
597 minetest.register_node(":realchess:chessboard", {
598 description = "Chess Board",
599 drawtype = "nodebox",
601 paramtype2 = "facedir",
602 inventory_image = "chessboard_top.png",
603 wield_image = "chessboard_top.png",
604 tiles = {"chessboard_top.png", "chessboard_top.png", "chessboard_sides.png"},
605 groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
606 sounds = default.node_sound_wood_defaults(),
607 node_box = {type = "fixed", fixed = {-.375, -.5, -.375, .375, -.4375, .375}},
608 sunlight_propagates = true,
609 on_rotate = screwdriver.rotate_simple,
610 can_dig = realchess.dig,
611 on_construct = realchess.init,
612 on_receive_fields = realchess.fields,
613 allow_metadata_inventory_move = realchess.move,
614 on_metadata_inventory_move = realchess.on_move,
615 allow_metadata_inventory_take = function() return 0 end
618 local function register_piece(name, count)
619 for _, color in pairs({"black", "white"}) do
621 minetest.register_craftitem(":realchess:"..name.."_"..color, {
622 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
623 inventory_image = name.."_"..color..".png",
625 groups = {not_in_creative_inventory=1}
629 minetest.register_craftitem(":realchess:"..name.."_"..color.."_"..i, {
630 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
631 inventory_image = name.."_"..color..".png",
633 groups = {not_in_creative_inventory=1}
640 register_piece("pawn", 8)
641 register_piece("rook", 2)
642 register_piece("knight", 2)
643 register_piece("bishop", 2)
644 register_piece("queen")
645 register_piece("king")