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
190
191
extends Piece
class_name Pawn, "res://assets/pieces/california/wP.png"

const promotables := ["Q.png", "N.png", "R.png", "B.png"]

var twostepfirstmove := false
var just_set := false
var enpassant := []

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

onready var whiteint := 1 if white else -1
onready var sprites := []
onready var darken = get_node("../../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 = load("res://ClickableSprite.tscn").instance()
		newsprite.position = (sprite.position + Vector2(0, (i * Globals.grid.piece_size.y) * whiteint))
		newsprite.name = "Sprite%s" % str(i)
		newsprite.connect("clicked", self, "handle_sprite_input_event")
		newsprite.z_index = 5
		newsprite.hide()
		add_child(newsprite)
		sprites.append(newsprite)


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


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


func get_moves() -> Array:
	var points := [Vector2.UP, Vector2.UP * 2]
	var moves := []
	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) -> bool:
	if position.y >= 7 or position.y <= 0:
		return true
	return false


func passant(position) -> void:
	enpassant.clear()
	moveto(position)


func get_attacks() -> Array:
	var points := [Vector2.UP + Vector2.RIGHT, Vector2.UP + Vector2.LEFT]
	var moves := []
	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()
	return moves


func en_passant(turncheck = 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.twostepfirstmove:
			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, type) -> 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].sprite.texture = load("%s%s%s" % [Globals.grid.ASSETS_PATH, team.to_lower(), promotables[i]])
		sprites[i].show()


func handle_sprite_input_event(node) -> void:
	darken.hide()
	var promote_to = promotables[sprites.find(node)][0]
	var first = (
		algebraic_move_notation(promoteposition)
		if !promotetake
		else algebraic_take_notation(promoteposition, real_position)
	)
	var notation = "%s=%s" % [first, promote_to]
	Globals.grid.promoting = null
	Globals.network.send_move_packet(
		{
			"start_position": real_position,
			"destination": promoteposition,
			"become": promote_to,
			"notation": notation,
			"white": white
		},
		Network.MOVEHEADERS.promote
	)


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


func _on_turn_over() -> void:
	if just_set:
		just_set = false
		return
	if twostepfirstmove:
		twostepfirstmove = 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)