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()
20 local slots = "listcolors[#00000000;#00000000;#00000000;#30434C;#FFF]"
23 inv:set_size("board", 64)
25 meta:set_string("formspec",
27 "bgcolor[#080808BB;true]"..
28 "background[0,0;8,8;chess_bg.png]"..
29 "button[3.1,7.8;2,2;new;New game]"..
30 "list[context;board;0,0;8,8;]"..
33 meta:set_string("infotext", "Chess Board")
34 meta:set_string("playerBlack", "")
35 meta:set_string("playerWhite", "")
36 meta:set_string("lastMove", "")
37 meta:set_int("lastMoveTime", 0)
38 meta:set_string("winner", "")
39 meta:set_int("castlingBlackL", 1)
40 meta:set_int("castlingBlackR", 1)
41 meta:set_int("castlingWhiteL", 1)
42 meta:set_int("castlingWhiteR", 1)
44 inv:set_list("board", {
45 "realchess:rook_black_1",
46 "realchess:knight_black_1",
47 "realchess:bishop_black_1",
48 "realchess:queen_black",
49 "realchess:king_black",
50 "realchess:bishop_black_2",
51 "realchess:knight_black_2",
52 "realchess:rook_black_2",
53 "realchess:pawn_black_1",
54 "realchess:pawn_black_2",
55 "realchess:pawn_black_3",
56 "realchess:pawn_black_4",
57 "realchess:pawn_black_5",
58 "realchess:pawn_black_6",
59 "realchess:pawn_black_7",
60 "realchess:pawn_black_8",
61 "", "", "", "", "", "", "", "",
62 "", "", "", "", "", "", "", "",
63 "", "", "", "", "", "", "", "",
64 "", "", "", "", "", "", "", "",
65 "realchess:pawn_white_1",
66 "realchess:pawn_white_2",
67 "realchess:pawn_white_3",
68 "realchess:pawn_white_4",
69 "realchess:pawn_white_5",
70 "realchess:pawn_white_6",
71 "realchess:pawn_white_7",
72 "realchess:pawn_white_8",
73 "realchess:rook_white_1",
74 "realchess:knight_white_1",
75 "realchess:bishop_white_1",
76 "realchess:queen_white",
77 "realchess:king_white",
78 "realchess:bishop_white_2",
79 "realchess:knight_white_2",
80 "realchess:rook_white_2"
84 function realchess.move(pos, from_list, from_index, to_list, to_index, count, player)
85 if from_list ~= "board" and to_list ~= "board" then
89 local playerName = player:get_player_name()
90 local meta = minetest.get_meta(pos)
92 if meta:get_string("winner") ~= "" then
93 minetest.chat_send_player(playerName, "This game is over.")
97 local inv = meta:get_inventory()
98 local pieceFrom = inv:get_stack(from_list, from_index):get_name()
99 local pieceTo = inv:get_stack(to_list, to_index):get_name()
100 local lastMove = meta:get_string("lastMove")
101 local thisMove -- will replace lastMove when move is legal
102 local playerWhite = meta:get_string("playerWhite")
103 local playerBlack = meta:get_string("playerBlack")
105 if pieceFrom:find("white") then
106 if playerWhite ~= "" and playerWhite ~= playerName then
107 minetest.chat_send_player(playerName, "Someone else plays white pieces!")
110 if lastMove ~= "" and lastMove ~= "black" then
111 minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
114 if pieceTo:find("white") then
115 -- Don't replace pieces of same color
118 playerWhite = playerName
120 elseif pieceFrom:find("black") then
121 if playerBlack ~= "" and playerBlack ~= playerName then
122 minetest.chat_send_player(playerName, "Someone else plays black pieces!")
125 if lastMove ~= "" and lastMove ~= "white" then
126 minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
129 if pieceTo:find("black") then
130 -- Don't replace pieces of same color
133 playerBlack = playerName
137 -- DETERMINISTIC MOVING
139 local from_x, from_y = index_to_xy(from_index)
140 local to_x, to_y = index_to_xy(to_index)
142 if pieceFrom:find("pawn") then
143 if thisMove == "white" then
144 -- white pawns can go up only
145 if from_y - 1 == to_y then
146 if from_x == to_x then
147 if pieceTo ~= "" then
149 elseif to_index >= 1 and to_index <= 8 then
150 inv:set_stack(from_list, from_index, "realchess:queen_white")
152 elseif from_x - 1 == to_x or from_x + 1 == to_x then
153 if not pieceTo:find("black") then
155 elseif to_index >= 1 and to_index <= 8 then
156 inv:set_stack(from_list, from_index, "realchess:queen_white")
161 elseif from_y - 2 == to_y then
162 if pieceTo ~= "" or from_y < 6 or inv:get_stack(from_list, xy_to_index(from_x, from_y - 1)):get_name() ~= "" then
168 elseif thisMove == "black" then
169 -- black pawns can go down only
170 if from_y + 1 == to_y then
171 if from_x == to_x then
172 if pieceTo ~= "" then
174 elseif to_index >= 57 and to_index <= 64 then
175 inv:set_stack(from_list, from_index, "realchess:queen_black")
177 elseif from_x - 1 == to_x or from_x + 1 == to_x then
178 if not pieceTo:find("white") then
180 elseif to_index >= 57 and to_index <= 64 then
181 inv:set_stack(from_list, from_index, "realchess:queen_black")
186 elseif from_y + 2 == to_y then
187 if pieceTo ~= "" or from_y > 1 or inv:get_stack(from_list, xy_to_index(from_x, from_y + 1)):get_name() ~= "" then
195 -- ensure that destination cell is empty
196 -- elseif x changed one unit left or right
197 -- ensure the pawn is killing opponent piece
199 -- move is not legal - abort
201 if from_x == to_x then
202 if pieceTo ~= "" then
205 elseif from_x - 1 == to_x or from_x + 1 == to_x then
206 if not pieceTo:find("white") then
216 elseif pieceFrom:find("rook") then
217 if from_x == to_x then
219 if from_y < to_y then
221 -- ensure that no piece disturbs the way
222 for i = from_y + 1, to_y - 1 do
223 if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
229 -- ensure that no piece disturbs the way
230 for i = to_y + 1, from_y - 1 do
231 if inv:get_stack(from_list, xy_to_index(from_x, i)):get_name() ~= "" then
236 elseif from_y == to_y then
237 -- mocing horizontally
238 if from_x < to_x then
240 -- ensure that no piece disturbs the way
241 for i = from_x + 1, to_x - 1 do
242 if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
248 -- ensure that no piece disturbs the way
249 for i = to_x + 1, from_x - 1 do
250 if inv:get_stack(from_list, xy_to_index(i, from_y)):get_name() ~= "" then
256 -- attempt to move arbitrarily -> abort
260 if thisMove == "white" then
261 if pieceFrom:find("1") then
262 meta:set_int("castlingWhiteL", 0)
263 elseif pieceFrom:find("2") then
264 meta:set_int("castlingWhiteR", 0)
266 elseif thisMove == "black" then
267 if pieceFrom:find("1") then
268 meta:set_int("castlingWhiteL", 0)
269 elseif pieceFrom:find("2") then
270 meta:set_int("castlingWhiteR", 0)
274 elseif pieceFrom:find("knight") then
276 local dx = from_x - to_x
277 local dy = from_y - to_y
279 -- get absolute values
292 -- ensure that dx == 1 and dy == 2
293 if dx ~= 1 or dy ~= 2 then
296 -- just ensure that destination cell does not contain friend piece
297 -- ^ it was done already thus everything ok
299 elseif pieceFrom:find("bishop") then
301 local dx = from_x - to_x
302 local dy = from_y - to_y
304 -- get absolute values
312 -- ensure dx and dy are equal
317 if from_x < to_x then
318 if from_y < to_y then
320 -- ensure that no piece disturbs the way
322 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
328 -- ensure that no piece disturbs the way
330 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
336 if from_y < to_y then
338 -- ensure that no piece disturbs the way
340 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
346 -- ensure that no piece disturbs the way
348 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
355 elseif pieceFrom:find("queen") then
356 local dx = from_x - to_x
357 local dy = from_y - to_y
359 -- get absolute values
367 -- ensure valid relative move
368 if dx ~= 0 and dy ~= 0 and dx ~= dy then
372 if from_x == to_x then
373 if from_y < to_y then
375 -- ensure that no piece disturbs the way
377 if inv:get_stack(from_list, xy_to_index(from_x, from_y + i)):get_name() ~= "" then
383 -- ensure that no piece disturbs the way
385 if inv:get_stack(from_list, xy_to_index(from_x, from_y - i)):get_name() ~= "" then
390 elseif from_x < to_x then
391 if from_y == to_y then
393 -- ensure that no piece disturbs the way
395 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y)):get_name() ~= "" then
399 elseif from_y < to_y then
401 -- ensure that no piece disturbs the way
403 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
409 -- ensure that no piece disturbs the way
411 if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
417 if from_y == to_y then
419 -- ensure that no piece disturbs the way and destination cell does
421 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y)):get_name() ~= "" then
425 elseif from_y < to_y then
427 -- ensure that no piece disturbs the way
429 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
435 -- ensure that no piece disturbs the way
437 if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
444 elseif pieceFrom:find("king") then
445 local dx = from_x - to_x
446 local dy = from_y - to_y
449 if thisMove == "white" then
450 if from_y == 7 and to_y == 7 then
452 if meta:get_int("castlingWhiteL") == 1 and inv:get_stack(from_list, 57):get_name() == "realchess:rook_white_1" then
453 for i = 58, from_index - 1 do
454 if inv:get_stack(from_list, i):get_name() ~= "" then
458 inv:set_stack(from_list, 57, "")
459 inv:set_stack(from_list, 59, "realchess:rook_white_1")
462 elseif to_x == 6 then
463 if meta:get_int("castlingWhiteR") == 1 and inv:get_stack(from_list, 64):get_name() == "realchess:rook_white_2" then
464 for i = from_index + 1, 63 do
465 if inv:get_stack(from_list, i):get_name() ~= "" then
469 inv:set_stack(from_list, 62, "realchess:rook_white_2")
470 inv:set_stack(from_list, 64, "")
475 elseif thisMove == "black" then
476 if from_y == 0 and to_y == 0 then
478 if meta:get_int("castlingBlackL") == 1 and inv:get_stack(from_list, 1):get_name() == "realchess:rook_black_1" then
479 for i = 2, from_index - 1 do
480 if inv:get_stack(from_list, i):get_name() ~= "" then
484 inv:set_stack(from_list, 1, "")
485 inv:set_stack(from_list, 3, "realchess:rook_black_1")
488 elseif to_x == 6 then
489 if meta:get_int("castlingBlackR") == 1 and inv:get_stack(from_list, 8):get_name() == "realchess:rook_black_2" then
490 for i = from_index + 1, 7 do
491 if inv:get_stack(from_list, i):get_name() ~= "" then
495 inv:set_stack(from_list, 6, "realchess:rook_black_2")
496 inv:set_stack(from_list, 8, "")
511 if dx > 1 or dy > 1 then
516 if thisMove == "white" then
517 meta:set_int("castlingWhiteL", 0)
518 meta:set_int("castlingWhiteR", 0)
519 elseif thisMove == "black" then
520 meta:set_int("castlingBlackL", 0)
521 meta:set_int("castlingBlackR", 0)
525 meta:set_string("playerWhite", playerWhite)
526 meta:set_string("playerBlack", playerBlack)
527 meta:set_string("lastMove", thisMove)
528 meta:set_int("lastMoveTime", minetest.get_gametime())
530 if meta:get_string("lastMove") == "black" then
531 minetest.chat_send_player(playerWhite, "["..os.date("%H:%M:%S").."] "..
532 playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
533 elseif meta:get_string("lastMove") == "white" then
534 minetest.chat_send_player(playerBlack, "["..os.date("%H:%M:%S").."] "..
535 playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
538 if pieceTo:find("king") then
539 minetest.chat_send_player(playerBlack, playerName.." won the game.")
540 minetest.chat_send_player(playerWhite, playerName.." won the game.")
541 meta:set_string("winner", thisMove)
547 local function timeout_format(timeout_limit)
548 local time_remaining = timeout_limit - minetest.get_gametime()
549 local minutes = math.floor(time_remaining / 60)
550 local seconds = time_remaining % 60
552 if minutes == 0 then return seconds.." sec." end
553 return minutes.." min. "..seconds.." sec."
556 function realchess.fields(pos, formname, fields, sender)
557 local playerName = sender:get_player_name()
558 local meta = minetest.get_meta(pos)
559 local timeout_limit = meta:get_int("lastMoveTime") + 300
560 if fields.quit then return end
562 -- timeout is 5 min. by default for resetting the game (non-players only)
563 if fields.new and (meta:get_string("playerWhite") == playerName or
564 meta:get_string("playerBlack") == playerName) then
566 elseif fields.new and meta:get_int("lastMoveTime") ~= 0 and
567 minetest.get_gametime() >= timeout_limit and
568 (meta:get_string("playerWhite") ~= playerName or
569 meta:get_string("playerBlack") ~= playerName) then
572 minetest.chat_send_player(playerName, "[!] You can't reset the chessboard, a game has been started.\n"..
573 "If you are not a current player, try again in "..timeout_format(timeout_limit))
577 function realchess.dig(pos, player)
578 local meta = minetest.get_meta(pos)
579 local playerName = player:get_player_name()
580 local timeout_limit = meta:get_int("lastMoveTime") + 300
582 -- timeout is 5 min. by default for digging the chessboard (non-players only)
583 if meta:get_int("lastMoveTime") ~= 0 and minetest.get_gametime() <= timeout_limit then
584 minetest.chat_send_player(playerName, "[!] You can't dig the chessboard, a game has been started.\n"..
585 "Reset it first if you're a current player, or dig again in "..timeout_format(timeout_limit))
592 function realchess.on_move(pos, from_list, from_index, to_list, to_index, count, player)
593 local inv = minetest.get_meta(pos):get_inventory()
594 inv:set_stack(from_list, from_index, '')
598 function realchess.take(pos, listname, index, stack, player)
602 minetest.register_node(":realchess:chessboard", {
603 description = "Chess Board",
604 drawtype = "nodebox",
606 paramtype2 = "facedir",
607 inventory_image = "chessboard_top.png",
608 wield_image = "chessboard_top.png",
609 tiles = {"chessboard_top.png", "chessboard_top.png", "chessboard_sides.png"},
610 groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
611 sounds = default.node_sound_wood_defaults(),
612 node_box = {type = "fixed", fixed = {-.375, -.5, -.375, .375, -.4375, .375}},
613 sunlight_propagates = true,
614 on_rotate = screwdriver.rotate_simple,
615 can_dig = realchess.dig,
616 on_construct = realchess.init,
617 on_receive_fields = realchess.fields,
618 allow_metadata_inventory_move = realchess.move,
619 on_metadata_inventory_move = realchess.on_move,
620 allow_metadata_inventory_take = realchess.take
623 local function register_piece(name, count)
624 for _, color in pairs({"black", "white"}) do
626 minetest.register_craftitem(":realchess:"..name.."_"..color, {
627 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
628 inventory_image = name.."_"..color..".png",
630 groups = {not_in_creative_inventory=1}
634 minetest.register_craftitem(":realchess:"..name.."_"..color.."_"..i, {
635 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
636 inventory_image = name.."_"..color..".png",
638 groups = {not_in_creative_inventory=1}
645 register_piece("pawn", 8)
646 register_piece("rook", 2)
647 register_piece("knight", 2)
648 register_piece("bishop", 2)
649 register_piece("queen")
650 register_piece("king")
652 minetest.register_craft({
653 output = "realchess:chessboard",
655 {"dye:black", "dye:white", "dye:black"},
656 {"stairs:slab_wood", "stairs:slab_wood", "stairs:slab_wood"}