summaryrefslogtreecommitdiff
path: root/scripts/Board.gd
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/Board.gd')
-rw-r--r--scripts/Board.gd30
1 files changed, 22 insertions, 8 deletions
diff --git a/scripts/Board.gd b/scripts/Board.gd
index f18ca74..04f2d90 100644
--- a/scripts/Board.gd
+++ b/scripts/Board.gd
@@ -17,6 +17,8 @@ var airports = {} # id : HexSpace of cell_type airport
var side_len: int
+const BOARD_GEN_ATTEMPTS : int = 5
+
# Y R B G
var airport_colors = [ Color(1, 1, 0), Color(1, 0, 0), Color(0.3, 0.3, 1), Color(0, 0.8, 0) ]
@@ -31,7 +33,7 @@ enum { PLAIN, HILLS, MOUNTAINS, AIRPORT }
const adjacent_offsets = [ [0,1] , [-1, 0], [-1, -1], [0, -1], [1, 0], [1, 1] ]
# indices of the offsets that are valid cells to approach from
-const approaches_i: Array= [ [0, 1, 2, 3, 4, 5], [0,1,3,4], [0,3] ]
+const approaches_i: Array= [ [], [0,3], [0,1,3,4], [0, 1, 2, 3, 4, 5] ]
func _ready():
@@ -87,8 +89,8 @@ func display_board():
var new_cell = hex_space.instance()
new_cell.call_deferred("set", "global_position", Vector3(x, randf()/15, z))
- new_cell.set_up(row[c])
add_child(new_cell)
+ new_cell.set_up(row[c])
row_display[c] = new_cell
board_display.push_back(row_display)
@@ -126,6 +128,7 @@ func populate_board(num_mountains : int, num_hills : int, num_airports : int, ru
airport_display = [ randi() % 9 + 1, randi() % 4 ] # number, color
while airport_display in used_airports:
airport_display = [ randi() % 9 + 1, randi() % 4 ]
+ used_airports.push_back(airport_display)
# find valid spot
var spot_okay:bool = false
var rot:int
@@ -189,25 +192,36 @@ func populate_board(num_mountains : int, num_hills : int, num_airports : int, ru
if not spot_okay:
return false # could not form valid map
- var args = {"cell_type" : AIRPORT , "pos" : [spot_r, spot_c], "orientation" : rot, "airport_id" : airport_id, "runways" : runways, 'valid_approach_offsets' : valid_approaches, "use_names" : use_names}
+ var args = {"cell_type" : AIRPORT , "pos" : [spot_r, spot_c], "orientation" : rot, "airport_id" : airport_id, "runway_count" : runways, 'valid_approach_offsets' : valid_approaches, "use_names" : use_names}
if use_names:
args["airport_name"] = airport_display
else:
- args["airport_color"] = airport_colors[airport_display[COLOR]]
+ args["airport_color"] = airport_colors[airport_display[COLOR]].to_html()
args["airport_number"] = airport_display[NUMBER]
board[spot_r][spot_c] = args
available_board_coords.pop_at(spot_i)
airport_id += 1
return true
-func get_json():
- return board
+func get_board():
+ return self.board
+
+func set_board(board_array):
+ reset_board()
+ self.board = board_array
+ display_board()
+
func generate_board(hex_side_len: int, num_mountains : int, num_hills : int, num_airports : int, runway_count : int, use_names : bool = false) -> bool:
create_board_base(hex_side_len)
- if not populate_board(num_mountains, num_hills, num_airports, runway_count, use_names):
+ var attempts : int = 1
+ while attempts < BOARD_GEN_ATTEMPTS and (not populate_board(num_mountains, num_hills, num_airports, runway_count, use_names)):
+ attempts += 1
+ reset_board()
+
+ if attempts >= BOARD_GEN_ATTEMPTS:
reset_board()
- print("Invalid board creation parameters")
+ display_board()
return false
display_board()
return true