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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
extends Piece
class_name Pawn, "res://assets/pieces/california/wP.png"

const promotables := "QNRB"

var just_double_stepped := false
var just_set := false
var enpassant: Array = []

var promoteposition := Vector2()
var promotetake := false

onready var whiteint := 1 if white else -1
onready var sprites := []
onready var darken: ColorRect = $"../../Darken"


func _ready() -> void:
	Globals.pawns.append(self)
	Events.connect("turn_over", self, "_on_turn_over")
	Events.connect("just_before_turn_over", self, "_just_before_turn_over")
	sprite.position = Globals.grid.piece_size / 2
	for i in range(0, 4):  # add 3 sprites
		var newsprite: Node2D = load("res://ui/ClickableSprite.tscn").instance()
		newsprite.position = (sprite.position + Vector2(0, (i * Globals.grid.piece_size.y) * whiteint))
		newsprite.get_node("Sprite").texture = load(
			"%s%s%s.png" % [Globals.grid.ASSETS_PATH, team.to_lower(), promotables[i]]
		)
		newsprite.name = promotables[i]
		newsprite.connect("clicked", self, "handle_sprite_input_event", [newsprite.name])
		newsprite.z_index = 5  # its not a texturebutton so i can use this
		newsprite.hide()
		add_child(newsprite)
		sprites.append(newsprite)


func _exit_tree() -> void:
	var find: int = Globals.pawns.find(self)
	if find != -1:
		Globals.pawns.remove(find)


func moveto(position: Vector2, instant := false) -> void:
	# check if 2 step
	if !just_double_stepped and !has_moved:
		if white and real_position.y - position.y == 2:
			just_double_stepped = true
			just_set = true
		if !white and position.y - real_position.y == 2:
			just_double_stepped = true
			just_set = true
	.moveto(position, instant)
	Globals.reset_halfmove()


func get_moves(_var := false, check_spots_check := true) -> PoolVector2Array:
	var points := [Vector2.UP, Vector2.UP * 2]
	var moves: PoolVector2Array = []
	for i in range(len(points)):
		var point: Vector2 = points[i]
		point *= whiteint
		point = pos_around(point)
		if is_on_board(point) and at_pos(point) == null:
			if i == 1 and has_moved or at_pos(pos_around(points[0] * whiteint)) != null:
				continue
			if check_spots_check and checkcheck(point):
				continue
			if is_on_board(point):
				moves.append(point)
	moves.append_array(en_passant())
	return moves


static func can_promote(position: Vector2) -> bool:
	return position.y >= 7 or position.y <= 0


func passant(position: Vector2, instant := false) -> void:
	var to_take = position + Vector2(0, whiteint)
	at_pos(to_take).took(instant)
	enpassant.resize(0)
	moveto(position, instant)


func valid_to_passant_take(piece) -> bool:
	return !piece or !Utils.is_pawn(piece) or piece.white != white or !piece.just_double_stepped


func get_attacks(check_spots_check := true) -> PoolVector2Array:
	var points := [Vector2.UP + Vector2.RIGHT, Vector2.UP + Vector2.LEFT]
	var moves: PoolVector2Array = []
	for i in range(len(points)):
		var point: Vector2 = points[i]
		point *= whiteint
		point = pos_around(point)
		if !is_on_board(point):
			continue
		if check_spots_check and checkcheck(point):
			continue
		if at_pos(point) != null and at_pos(point).white != white:
			moves.append(point)
	en_passant()  # for the fen
	return moves


func en_passant(turncheck := true, check_spots_check := true) -> Array:  # in passing
	var passants := [pos_around(Vector2.LEFT), pos_around(Vector2.RIGHT)]
	var moves := []
	for i in passants:
		if !is_on_board(i) or !at_pos(i):
			continue
		var spot := at_pos(i)
		if spot.white == white or !Utils.is_pawn(spot):
			continue
		if turncheck and white != Globals.turn:
			continue
		if !spot.just_double_stepped:
			continue
		if check_spots_check and checkcheck(i):
			continue
		var position: Vector2 = i + (Vector2.UP * whiteint)
		if !at_pos(position):
			moves.append(position)
		enpassant.append([position, spot])
	return moves


func promote(position: Vector2, type: String) -> void:
	if type == "take":
		at_pos(position).hide()
	move(position)  # only move the visuals
	promoteposition = position
	darken.show()
	for i in range(len(promotables)):
		sprites[i].show()


func promote_to(promote_to: String, is_capture: bool, position: Vector2, instant := false):
	if is_capture and at_pos(position):
		at_pos(position).took(instant)
	clear_clicked()
	Globals.grid.make_piece(position, piece(promote_to), white)
	took()


func handle_sprite_input_event(promote_to: String) -> void:
	darken.hide()
	var is_cap = at_pos(promoteposition) != null
	var mov = Move.new(SanParser.PAWN, [real_position, promoteposition], is_cap)
	mov.promotion = SanParse.from_str(promote_to)
	Globals.network.send_mov(mov)


static func piece(string: String) -> String:
	match string:
		"Q":
			return "Queen"
		"N":
			return "Knight"
		"R":
			return "Rook"
		"B":
			return "Bishop"
		_:
			return "Piece"


func _on_turn_over() -> void:
	if just_set:
		just_set = false
		return
	if just_double_stepped:
		just_double_stepped = false


func _just_before_turn_over() -> void:
	var had_a_enpassant := len(enpassant) > 0
	enpassant.clear()
	if !had_a_enpassant:  # scuffed method to check if enpassant is possible
		en_passant(false)
	var temporary := []
	for i in enpassant:
		temporary.append(i[0])
	if !temporary:
		return
	if white:
		Globals.grid.matrix[8].wcep.append_array(temporary)
	else:
		Globals.grid.matrix[8].bcep.append_array(temporary)