]> git.lizzy.rs Git - xdecor.git/blob - chess.lua
Crafting guide : change craft grid list to image buttons and indication for groups...
[xdecor.git] / chess.lua
1 -- See https://github.com/kilbith/realchess for the main repository
2
3 local realchess = {}
4 screwdriver = screwdriver or {}
5
6 local function index_to_xy(index)
7         index = index - 1
8         local x = index % 8
9         local y = (index - x) / 8
10         return x, y
11 end
12
13 local function xy_to_index(x, y)
14         return x + y * 8 + 1
15 end
16
17 function realchess.init(pos)
18         local meta = minetest.get_meta(pos)
19         local inv = meta:get_inventory()
20
21         inv:set_size("board", 64)
22         
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] ]]
29
30         meta:set_string("formspec", formspec)
31
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)
42
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"
80         })
81 end
82
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
85                 return 0
86         end
87
88         local playerName = player:get_player_name()
89         local meta = minetest.get_meta(pos)
90
91         if meta:get_string("winner") ~= "" then
92                 minetest.chat_send_player(playerName, "This game is over.")
93                 return 0
94         end
95
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")
103
104         if pieceFrom:find("white") then
105                 if playerWhite ~= "" and playerWhite ~= playerName then
106                         minetest.chat_send_player(playerName, "Someone else plays white pieces!")
107                         return 0
108                 end             
109                 if lastMove ~= "" and lastMove ~= "black" then
110                         minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
111                         return 0
112                 end
113                 if pieceTo:find("white") then
114                         -- Don't replace pieces of same color
115                         return 0
116                 end
117                 playerWhite = playerName
118                 thisMove = "white"
119         elseif pieceFrom:find("black") then
120                 if playerBlack ~= "" and playerBlack ~= playerName then
121                         minetest.chat_send_player(playerName, "Someone else plays black pieces!")
122                         return 0
123                 end
124                 if lastMove ~= "" and lastMove ~= "white" then
125                         minetest.chat_send_player(playerName, "It's not your turn, wait for your opponent to play.")
126                         return 0
127                 end
128                 if pieceTo:find("black") then
129                         -- Don't replace pieces of same color
130                         return 0
131                 end
132                 playerBlack = playerName
133                 thisMove = "black"
134         end
135
136         -- DETERMINISTIC MOVING
137
138         local from_x, from_y = index_to_xy(from_index)
139         local to_x, to_y = index_to_xy(to_index)
140
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
147                                                 return 0
148                                         elseif to_index >= 1 and to_index <= 8 then
149                                                 inv:set_stack(from_list, from_index, "realchess:queen_white")
150                                         end
151                                 elseif from_x - 1 == to_x or from_x + 1 == to_x then
152                                         if not pieceTo:find("black") then
153                                                 return 0
154                                         elseif to_index >= 1 and to_index <= 8 then
155                                                 inv:set_stack(from_list, from_index, "realchess:queen_white")
156                                         end
157                                 else
158                                         return 0
159                                 end
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
162                                         return 0
163                                 end
164                         else
165                                 return 0
166                         end
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
172                                                 return 0
173                                         elseif to_index >= 57 and to_index <= 64 then
174                                                 inv:set_stack(from_list, from_index, "realchess:queen_black")
175                                         end
176                                 elseif from_x - 1 == to_x or from_x + 1 == to_x then
177                                         if not pieceTo:find("white") then
178                                                 return 0
179                                         elseif to_index >= 57 and to_index <= 64 then
180                                                 inv:set_stack(from_list, from_index, "realchess:queen_black")
181                                         end
182                                 else
183                                         return 0
184                                 end
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
187                                         return 0
188                                 end
189                         else
190                                 return 0
191                         end
192
193                         -- if x not changed,
194                         --   ensure that destination cell is empty
195                         -- elseif x changed one unit left or right
196                         --   ensure the pawn is killing opponent piece
197                         -- else
198                         --   move is not legal - abort
199
200                         if from_x == to_x then
201                                 if pieceTo ~= "" then
202                                         return 0
203                                 end
204                         elseif from_x - 1 == to_x or from_x + 1 == to_x then
205                                 if not pieceTo:find("white") then
206                                         return 0
207                                 end
208                         else
209                                 return 0
210                         end
211                 else
212                         return 0
213                 end
214
215         elseif pieceFrom:find("rook") then
216                 if from_x == to_x then
217                         -- moving vertically
218                         if from_y < to_y then
219                                 -- moving down
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
223                                                 return 0
224                                         end
225                                 end
226                         else
227                                 -- mocing up
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
231                                                 return 0
232                                         end
233                                 end
234                         end
235                 elseif from_y == to_y then
236                         -- mocing horizontally
237                         if from_x < to_x then
238                                 -- mocing right
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
242                                                 return 0
243                                         end
244                                 end
245                         else
246                                 -- mocing left
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
250                                                 return 0
251                                         end
252                                 end
253                         end
254                 else
255                         -- attempt to move arbitrarily -> abort
256                         return 0
257                 end
258
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)
264                         end
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)
270                         end
271                 end
272
273         elseif pieceFrom:find("knight") then
274                 -- get relative pos
275                 local dx = from_x - to_x
276                 local dy = from_y - to_y
277
278                 -- get absolute values
279                 if dx < 0 then
280                         dx = -dx
281                 end
282                 if dy < 0 then
283                         dy = -dy
284                 end
285
286                 -- sort x and y
287                 if dx > dy then
288                         dx, dy = dy, dx
289                 end
290
291                 -- ensure that dx == 1 and dy == 2
292                 if dx ~= 1 or dy ~= 2 then
293                         return 0
294                 end
295                 -- just ensure that destination cell does not contain friend piece
296                 -- ^ it was done already thus everything ok
297
298         elseif pieceFrom:find("bishop") then
299                 -- get relative pos
300                 local dx = from_x - to_x
301                 local dy = from_y - to_y
302
303                 -- get absolute values
304                 if dx < 0 then
305                         dx = -dx
306                 end
307                 if dy < 0 then
308                         dy = -dy
309                 end
310
311                 -- ensure dx and dy are equal
312                 if dx ~= dy then
313                         return 0
314                 end
315
316                 if from_x < to_x then
317                         if from_y < to_y then
318                                 -- moving right-down
319                                 -- ensure that no piece disturbs the way
320                                 for i = 1, dx - 1 do
321                                         if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
322                                                 return 0
323                                         end
324                                 end
325                         else
326                                 -- moving right-up
327                                 -- ensure that no piece disturbs the way
328                                 for i = 1, dx - 1 do
329                                         if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
330                                                 return 0
331                                         end
332                                 end
333                         end
334                 else
335                         if from_y < to_y then
336                                 -- moving left-down
337                                 -- ensure that no piece disturbs the way
338                                 for i = 1, dx - 1 do
339                                         if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
340                                                 return 0
341                                         end
342                                 end
343                         else
344                                 -- moving left-up
345                                 -- ensure that no piece disturbs the way
346                                 for i = 1, dx - 1 do
347                                         if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
348                                                 return 0
349                                         end
350                                 end
351                         end
352                 end
353
354         elseif pieceFrom:find("queen") then
355                 local dx = from_x - to_x
356                 local dy = from_y - to_y
357
358                 -- get absolute values
359                 if dx < 0 then
360                         dx = -dx
361                 end
362                 if dy < 0 then
363                         dy = -dy
364                 end
365
366                 -- ensure valid relative move
367                 if dx ~= 0 and dy ~= 0 and dx ~= dy then
368                         return 0
369                 end
370
371                 if from_x == to_x then
372                         if from_y < to_y then
373                                 -- goes down
374                                 -- ensure that no piece disturbs the way
375                                 for i = 1, dx - 1 do
376                                         if inv:get_stack(from_list, xy_to_index(from_x, from_y + i)):get_name() ~= "" then
377                                                 return 0
378                                         end
379                                 end
380                         else
381                                 -- goes up
382                                 -- ensure that no piece disturbs the way
383                                 for i = 1, dx - 1 do
384                                         if inv:get_stack(from_list, xy_to_index(from_x, from_y - i)):get_name() ~= "" then
385                                                 return 0
386                                         end
387                                 end
388                         end             
389                 elseif from_x < to_x then
390                         if from_y == to_y then
391                                 -- goes right
392                                 -- ensure that no piece disturbs the way
393                                 for i = 1, dx - 1 do
394                                         if inv:get_stack(from_list, xy_to_index(from_x + i, from_y)):get_name() ~= "" then
395                                                 return 0
396                                         end
397                                 end
398                         elseif from_y < to_y then
399                                 -- goes right-down
400                                 -- ensure that no piece disturbs the way
401                                 for i = 1, dx - 1 do
402                                         if inv:get_stack(from_list, xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
403                                                 return 0
404                                         end
405                                 end
406                         else
407                                 -- goes right-up
408                                 -- ensure that no piece disturbs the way
409                                 for i = 1, dx - 1 do
410                                         if inv:get_stack(from_list, xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
411                                                 return 0
412                                         end
413                                 end
414                         end                             
415                 else
416                         if from_y == to_y then
417                                 -- goes left
418                                 -- ensure that no piece disturbs the way and destination cell does
419                                 for i = 1, dx - 1 do
420                                         if inv:get_stack(from_list, xy_to_index(from_x - i, from_y)):get_name() ~= "" then
421                                                 return 0
422                                         end
423                                 end
424                         elseif from_y < to_y then
425                                 -- goes left-down
426                                 -- ensure that no piece disturbs the way
427                                 for i = 1, dx - 1 do
428                                         if inv:get_stack(from_list, xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
429                                                 return 0
430                                         end
431                                 end
432                         else
433                                 -- goes left-up
434                                 -- ensure that no piece disturbs the way
435                                 for i = 1, dx - 1 do
436                                         if inv:get_stack(from_list, xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
437                                                 return 0
438                                         end
439                                 end
440                         end             
441                 end
442
443         elseif pieceFrom:find("king") then
444                 local dx = from_x - to_x
445                 local dy = from_y - to_y
446                 local check = true
447                 
448                 if thisMove == "white" then
449                         if from_y == 7 and to_y == 7 then
450                                 if to_x == 1 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
454                                                                 return 0
455                                                         end
456                                                 end
457                                                 inv:set_stack(from_list, 57, "")
458                                                 inv:set_stack(from_list, 59, "realchess:rook_white_1")
459                                                 check = false
460                                         end
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
465                                                                 return 0
466                                                         end
467                                                 end
468                                                 inv:set_stack(from_list, 62, "realchess:rook_white_2")
469                                                 inv:set_stack(from_list, 64, "")
470                                                 check = false
471                                         end
472                                 end
473                         end
474                 elseif thisMove == "black" then
475                         if from_y == 0 and to_y == 0 then
476                                 if to_x == 1 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
480                                                                 return 0
481                                                         end
482                                                 end
483                                                 inv:set_stack(from_list, 1, "")
484                                                 inv:set_stack(from_list, 3, "realchess:rook_black_1")
485                                                 check = false
486                                         end
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
491                                                                 return 0
492                                                         end
493                                                 end
494                                                 inv:set_stack(from_list, 6, "realchess:rook_black_2")
495                                                 inv:set_stack(from_list, 8, "")
496                                                 check = false
497                                         end
498                                 end
499                         end
500                 end
501
502                 if check then
503                         if dx < 0 then
504                                 dx = -dx
505                         end
506                         if dy < 0 then
507                                 dy = -dy
508                         end
509                         
510                         if dx > 1 or dy > 1 then
511                                 return 0
512                         end
513                 end
514                 
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)
521                 end
522         end
523
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())
528
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.")
535         end
536
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)
541         end
542
543         return 1
544 end
545
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
550
551         if minutes == 0 then return seconds.." sec." end
552         return minutes.." min. "..seconds.." sec."
553 end
554
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
560
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
564                 realchess.init(pos)
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
569                 realchess.init(pos)
570         else
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))
573         end
574 end
575
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
580
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))
585                 return false
586         end
587
588         return true
589 end
590
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, '')
594         return false
595 end
596
597 minetest.register_node(":realchess:chessboard", {
598         description = "Chess Board",
599         drawtype = "nodebox",
600         paramtype = "light",
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
616 })
617
618 local function register_piece(name, count)
619         for _, color in pairs({"black", "white"}) do
620         if not count then
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",
624                         stack_max = 1,
625                         groups = {not_in_creative_inventory=1}
626                 })
627         else
628                 for i = 1, count do
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",
632                                 stack_max = 1,
633                                 groups = {not_in_creative_inventory=1}
634                         })
635                 end
636         end
637         end
638 end
639
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")
646