online multiplayer chess game (note server currently down)
Diffstat (limited to 'Utils.gd')
| -rw-r--r-- | Utils.gd | 182 |
1 files changed, 99 insertions, 83 deletions
@@ -1,15 +1,74 @@ extends Node +var internet := false signal newmove(move) signal newfen(fen) -var turn_moves: PoolStringArray = [] -var turns_moves: PoolStringArray = [] -var internet := false -var counter := 0 +var moves_list: PoolStringArray = [] var fen := "" +func get_pgn(): + return moves_list.join(" ") + + +func _on_turn_over() -> void: + fen = get_fen() + Log.info("fen: " + fen) + emit_signal("newfen", fen) + + +func get_fen() -> String: + var pieces := "" + for rank in range(8): + var empty := 0 + for file in range(8): + var spot: Piece = Globals.grid.matrix[rank][file] + if spot == null: + empty += 1 + if len(pieces) > 0 and str(empty - 1) == pieces[-1]: + pieces[-1] = str(empty) + else: + pieces += str(empty) + else: + pieces += (spot.shortname[0].to_upper() if spot.white else spot.shortname[0].to_lower()) + empty = 0 + if rank != 7: + pieces += "/" + # handle castling checks + var whitecastling := PoolStringArray(Globals.white_king.castleing(true)).join("") + var blackcastling := PoolStringArray(Globals.black_king.castleing(true)).join("") + var castlingrights := "" + if blackcastling and whitecastling: + castlingrights = whitecastling.to_upper() + blackcastling.to_lower() + else: + castlingrights = "-" + + var enpassants := "" + for pawn in Globals.pawns: + if pawn.twostepfirstmove and pawn.just_set: + enpassants += Utils.to_algebraic(pawn.real_position + (Vector2.DOWN * pawn.whiteint)) + return ( + "%s %s %s %s %s %s" + % [ + pieces, + "w" if Globals.turn else "b", + castlingrights, + enpassants if enpassants else "-", + Globals.halfmove, + Globals.fullmove, + ] + ) # pos # turn # castling # enpassant # halfmove # fullmove + + +func add_move(move: String) -> void: + if Globals.turn == false: + moves_list.append("%s. %s" % [Globals.fullmove, move]) + else: + moves_list.append(move) + emit_signal("newmove", move) + + func get_args() -> Dictionary: var arguments := {} for argument in OS.get_cmdline_args(): @@ -30,6 +89,7 @@ func _ready() -> void: print("run with command help to show this help") get_tree().quit() # dont wait Debug.monitor(self, "fen") + Debug.monitor(self, "pgn", "get_pgn()") static func exec_ext() -> String: @@ -52,26 +112,8 @@ static func is_king(inode) -> bool: return inode is King -func add_move(move: String) -> void: - if turn_moves.size() == 0: - turn_moves.append("%s. %s" % [Globals.fullmove, move]) - else: - turn_moves.append(move) - emit_signal("newmove", move) - - func reset_vars() -> void: - turn_moves.resize(0) - turns_moves.resize(0) - counter = 0 - - -static func to_algebraic(pos: Vector2) -> String: - return "abcdefgh"[pos.x] + str(round(8 - pos.y)) - - -static func from_algebraic(algebraic_position: String) -> Vector2: - return Vector2(ord(algebraic_position[0]) - ord("a"), 8 - int(algebraic_position[1])) + moves_list.resize(0) static func get_node_name(node: Node) -> Array: @@ -101,79 +143,31 @@ func internet_available() -> bool: return returnable -func walk_dir(path := "res://assets/pieces") -> PoolStringArray: # walk the directory, finding the asset packs - var folders: PoolStringArray = [] # init the folders +func walk_dir(path := "res://assets/pieces", only_dir := true, of_ext := "png", exclude := []) -> PoolStringArray: # walk the directory, finding the asset packs + var files := [] # init the files var dir := Directory.new() # init the directory if dir.open(path) == OK: # open the directory dir.list_dir_begin(true) # list the directory var file_name := dir.get_next() # get the next file while file_name != "": # while there is a file - if dir.current_is_dir(): # if the current is a directory - folders.append(file_name) # add the folder + if only_dir: + if dir.current_is_dir(): # if the current is a directory + files.append(file_name) # add the folder + else: + var split = file_name.split(".") + if split[-1] == of_ext and !split[0] in exclude: + files.append(split[0]) # add the file file_name = dir.get_next() # get the next file else: - Log.err("An error occurred when trying to access the path " + path) # print the error - return folders # return the folders + push_error("An error occurred when trying to access the path " + path) # print the error + files.sort() # sort the files + return PoolStringArray(files) # return the files func format_seconds(time: float, use_milliseconds: bool = false) -> String: return "%02d:%04.1f" if use_milliseconds else "%02d:%02d" % [time / 60, fmod(time, 60)] -func _on_turn_over() -> void: - fen = get_fen() - Log.info("fen: " + fen) - emit_signal("newfen", fen) - counter += 1 - if counter >= 2: - counter = 0 - turns_moves.append(turn_moves.join(" ")) - turn_moves.resize(0) - - -func get_fen() -> String: - var pieces := "" - for rank in range(8): - var empty := 0 - for file in range(8): - var spot: Piece = Globals.grid.matrix[rank][file] - if spot == null: - empty += 1 - if len(pieces) > 0 and str(empty - 1) == pieces[-1]: - pieces[-1] = str(empty) - else: - pieces += str(empty) - else: - pieces += (spot.shortname[0].to_upper() if spot.white else spot.shortname[0].to_lower()) - empty = 0 - if rank != 7: - pieces += "/" - # handle castling checks - var whitecastling := PoolStringArray(Globals.white_king.castleing(true)).join("") - var blackcastling := PoolStringArray(Globals.black_king.castleing(true)).join("") - var castlingrights := "" - if blackcastling and whitecastling: - castlingrights = whitecastling.to_upper() + blackcastling.to_lower() - else: - castlingrights = "-" - - var enpassants := "" - for pawn in Globals.pawns: - if pawn.twostepfirstmove and pawn.just_set: - enpassants += to_algebraic(pawn.real_position + (Vector2.DOWN * pawn.whiteint)) - return ( - "%s %s %s %s %s %s" - % [ - pieces, - "w" if Globals.turn else "b", - castlingrights, - enpassants if enpassants else "-", - Globals.halfmove, - Globals.fullmove, - ] - ) # pos # turn # castling # enpassant # halfmove # fullmove - - func _notification(what: int) -> void: if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST or what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST: if get_tree().get_root().has_node("Board"): @@ -181,3 +175,25 @@ func _notification(what: int) -> void: yield(get_tree(), "idle_frame") # wait for the packet to send Log.debug("Bye!") get_tree().quit() + + +static func to_algebraic(pos: Vector2) -> String: + var column = "abcdefgh"[pos.x] if pos.x != -1 else "" + var row = str(round(8 - pos.y)) if pos.y != -1 else "" + return column + row + + +static func col_pos(col: String) -> int: + return "abcdefgh".find(col) + + +static func row_pos(row: String) -> int: + return 8 - int(row) + + +static func from_algebraic(pos: String) -> Vector2: + return Vector2(col_pos(pos[0]), row_pos(pos[1])) + + +static func to_str(type: int) -> String: + return " NBRQK"[type].strip_edges() # if its a pawn, return nothing |