Skip to content

Commit

Permalink
Parameterised get_attackable_countries so that they can be applied wi…
Browse files Browse the repository at this point in the history
…th a specific set of game modes in mind.

Right now it's used when rerolling spawn so that get_attackable_coutnries only returns countries that are attackable in the classic game mode.
ra314 committed Oct 21, 2021
1 parent 3d3ad54 commit 0d85b04
Showing 3 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Scripts/Levels/Level Components/Country.gd
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ func update_labels():

# Flash all countries that can be attacked
func flash_attackable_neighbours():
for country in get_attackable_countries():
for country in get_attackable_countries(Game_Manager.game_modes):
# Creation of a mask sprite to do the flashing
country.flashing = true

@@ -97,21 +97,21 @@ func draw_line_to_country(selected_country):
new_line.add_point(Vector2(20,20))
new_line.add_point(selected_country.position - position + Vector2(20,20))

func can_attack(attacker, defender):
func can_attack(attacker, defender, game_modes):
# Check if the defender and attacker are connected
if defender in attacker.connected_countries:
# Check if the defender and attacker had different owners
if defender.belongs_to != attacker.belongs_to:
# Check if the number of units is sufficient for an attack
if "drain" in Game_Manager.game_modes:
if "drain" in game_modes:
return attacker.num_troops > 1
else:
return attacker.num_troops > defender.num_troops

func get_attackable_countries():
func get_attackable_countries(game_modes):
var attackable_countries = []
for country in connected_countries:
if can_attack(self, country):
if can_attack(self, country, game_modes):
attackable_countries.append(country)
return attackable_countries

@@ -203,7 +203,7 @@ func on_click(event, is_long_press):
elif Game_Manager.selected_country != null:
var attacker = Game_Manager.selected_country
# Check if this country is attackable by the attacker
if can_attack(attacker, self):
if can_attack(attacker, self, Game_Manager.game_modes):
# If the attacker has more troops
if attacker.num_troops > num_troops:
num_troops = attacker.num_troops - num_troops
7 changes: 4 additions & 3 deletions Scripts/Levels/Level Main.gd
Original file line number Diff line number Diff line change
@@ -80,14 +80,14 @@ func spawn_and_allocate():
for player in players.values().slice(0,1):
for country in player.owned_countries:
if country.num_troops > 1:
if len(country.get_attackable_countries()) == 0:
if len(country.get_attackable_countries(["classic"])) == 0:
print("BAD spawn, I have " + str(country.num_troops) + " units and am " + country.belongs_to.color)
return false

# Check that all player owned countries cannot immediately attack another player owned country
for player in players.values().slice(0,1):
for attacker in player.owned_countries:
for defender in attacker.get_attackable_countries():
for defender in attacker.get_attackable_countries(["classic"]):
if defender.belongs_to.color != "gray":
print("BAD spawn, I have " + str(defender.num_troops) + " units and am " + defender.belongs_to.color + " and can be attacked")
return false
@@ -226,7 +226,8 @@ func minimax(game_state, depth, alpha, beta, maximizing_player):
pass
######
func reroll_spawn():
spawn_and_allocate()
while not spawn_and_allocate():
pass
if _root.online_game:
synchronize(_root.players["guest"])

2 changes: 1 addition & 1 deletion Scripts/UI/Level Select.gd
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ func back():
# Removing the previous scene from history since we're going to load it again
var prev_scene_str = _root.loaded_scene_history.pop_back()
# Reverting side effects
_root.game_mode = ""
_root.game_modes = []
# Loading the previous scene
var scene = _root.scene_manager._load_scene(prev_scene_str)
_root.scene_manager._replace_scene(scene)

0 comments on commit 0d85b04

Please sign in to comment.