online multiplayer chess game (note server currently down)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
extends Piece
class_name King, "res://assets/california/wK.png"

var castle_check = true
var can_castle = []


func get_moves():
	var moves = []
	for i in all_dirs():
		var spot = pos_around(i)
		if is_on_board(spot):
			if no_enemys and at_pos(spot):
				continue
			if check_spots_check and checkcheck(spot):
				continue
			moves.append(spot)
	if castle_check and !has_moved and !Globals.in_check:  # make sure this is only called when clicking
		moves.append_array(castleing())
	return moves


func just_before_over():  # assign metadata for threefold repetition draw check
	castleing()
	if can_castle.size() > 0:
		for i in can_castle:
			if i[3] == "O-O-O":
				if white:
					Globals.grid.matrix[8].wccl = true
				else:
					Globals.grid.matrix[8].bccl = true
			else:
				if white:
					Globals.grid.matrix[8].wccr = true
				else:
					Globals.grid.matrix[8].bccr = true


func _ready():
	Events.connect("just_before_turn_over", self, "just_before_over")


func castleing():
	var moves = []
	var rooks = [pos_around(Vector2.RIGHT * 3), pos_around(Vector2.LEFT * 4)]
	var rook_motion = [pos_around(Vector2.RIGHT), pos_around(Vector2.LEFT)]
	var king_moveto_spots = [Vector2.RIGHT, Vector2.LEFT]  # O-O  and O-O-O respectivel
	for i in range(len(rooks)):
		if !is_on_board(rooks[i]):
			continue
		var rook = at_pos(rooks[i])
		if !rook is Rook:
			continue
		if rook.has_moved:
			continue
		var direction = king_moveto_spots[i]
		var posx2 = pos_around(direction * 2)
		var pos = pos_around(direction)
		if at_pos(posx2) or at_pos(pos):
			continue
		if checkcheck(posx2) or checkcheck(pos):
			continue
		can_castle.append([posx2, rook, rook_motion[i], "O-O-O" if i == 1 else "O-O"])
		moves.append(posx2)
	return moves


func castle(position):
	var return_string = ""
	if can_castle.size() == 1:
		return_string = can_castle[0][3]
	else:
		for i in can_castle:
			if i[0] == position:
				return_string = i[3]
				break
	override_moveto = true
	can_castle.clear()
	moveto(position)
	override_moveto = false
	return return_string


func can_move():  # checks if you can legally move
	castle_check = false
	var can = .can_move()
	castle_check = true
	return can


func get_attacks():
	castle_check = false
	var final = .get_attacks()
	castle_check = true
	return final