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, 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, playerName.." has moved a "..pieceFrom:match("%a+:(%a+)")..", it's now your turn.")
536 if pieceTo:find("king") then
537 minetest.chat_send_player(playerBlack, playerName.." won the game.")
538 minetest.chat_send_player(playerWhite, playerName.." won the game.")
539 meta:set_string("winner", thisMove)
545 function realchess.fields(pos, formname, fields, sender)
546 local playerName = sender:get_player_name()
547 local meta = minetest.get_meta(pos)
549 if fields.quit then return end
551 -- the chess can't be reset during a started game unless if nobody has played during a while (~5 min. by default)
552 if fields.new and (meta:get_string("playerWhite") == playerName or
553 meta:get_string("playerBlack") == playerName) then
555 elseif fields.new and meta:get_int("lastMoveTime") ~= 0 and
556 minetest.get_gametime() >= meta:get_int("lastMoveTime") + 300 and
557 (meta:get_string("playerWhite") ~= playerName or
558 meta:get_string("playerBlack") ~= playerName) then
561 minetest.chat_send_player(playerName, "You can't reset the chessboard, a game has been started.\nIf you are not a current player, try again after a while.")
565 function realchess.dig(pos, player)
566 local meta = minetest.get_meta(pos)
567 local playerName = player:get_player_name()
569 -- the chess can't be dug during a started game unless if nobody has played during a while (~5 min. by default)
570 if meta:get_int("lastMoveTime") ~= 0 and
571 minetest.get_gametime() <= meta:get_int("lastMoveTime") + 300 then
572 minetest.chat_send_player(playerName, "You can't dig the chessboard, a game has been started.\nReset it first if you're a current player, or try digging again after a while.")
579 function realchess.on_move(pos, from_list, from_index, to_list, to_index, count, player)
580 local inv = minetest.get_meta(pos):get_inventory()
581 inv:set_stack(from_list, from_index, '')
585 function realchess.take(pos, listname, index, stack, player)
589 minetest.register_node(":realchess:chessboard", {
590 description = "Chess Board",
591 drawtype = "nodebox",
593 paramtype2 = "facedir",
594 inventory_image = "chessboard_top.png",
595 wield_image = "chessboard_top.png",
596 tiles = {"chessboard_top.png", "chessboard_top.png",
597 "chessboard_sides.png", "chessboard_sides.png",
598 "chessboard_top.png", "chessboard_top.png"},
599 groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
600 sounds = default.node_sound_wood_defaults(),
601 node_box = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5}},
602 sunlight_propagates = true,
603 on_rotate = screwdriver.rotate_simple,
604 can_dig = realchess.dig,
605 on_construct = realchess.init,
606 on_receive_fields = realchess.fields,
607 allow_metadata_inventory_move = realchess.move,
608 on_metadata_inventory_move = realchess.on_move,
609 allow_metadata_inventory_take = realchess.take
612 local function register_piece(name, count)
613 for _, color in pairs({"black", "white"}) do
615 minetest.register_craftitem(":realchess:"..name.."_"..color, {
616 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
617 inventory_image = name.."_"..color..".png",
619 groups = {not_in_creative_inventory=1}
623 minetest.register_craftitem(":realchess:"..name.."_"..color.."_"..i, {
624 description = color:gsub("^%l", string.upper).." "..name:gsub("^%l", string.upper),
625 inventory_image = name.."_"..color..".png",
627 groups = {not_in_creative_inventory=1}
634 register_piece("pawn", 8)
635 register_piece("rook", 2)
636 register_piece("knight", 2)
637 register_piece("bishop", 2)
638 register_piece("queen")
639 register_piece("king")
641 minetest.register_craft({
642 output = "realchess:chessboard",
644 {"dye:black", "dye:white", "dye:black"},
645 {"stairs:slab_wood", "stairs:slab_wood", "stairs:slab_wood"}