Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziktofel committed Oct 25, 2024
1 parent 0728321 commit f83fc70
Showing 1 changed file with 58 additions and 93 deletions.
151 changes: 58 additions & 93 deletions worlds/sc2/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,31 @@ def total_mission_count(self):
# Unit classes in world, these are set properly into the world after the item culling is done
# Therefore need to get from the world
def world_has_barracks_unit(self):
if self.world is None:
return False
else:
return self.world.has_barracks_unit
return self.world is not None and self.world.has_barracks_unit

def world_has_factory_unit(self):
if self.world is None:
return False
else:
return self.world.has_factory_unit
return self.world is not None and self.world.has_factory_unit

def world_has_starport_unit(self):
if self.world is None:
return False
else:
return self.world.has_starport_unit
return self.world is not None and self.world.has_starport_unit

def world_has_zerg_melee_unit(self):
if self.world is None:
return False
else:
return self.world.has_zerg_melee_unit
return self.world is not None and self.world.has_zerg_melee_unit

def world_has_zerg_ranged_unit(self):
if self.world is None:
return False
else:
return self.world.has_zerg_ranged_unit
return self.world is not None and self.world.has_zerg_ranged_unit

def world_has_zerg_air_unit(self):
if self.world is None:
return False
else:
return self.world.has_zerg_air_unit
return self.world is not None and self.world.has_zerg_air_unit

def world_has_protoss_ground_unit(self):
if self.world is None:
return False
else:
return self.world.has_protoss_ground_unit
return self.world is not None and self.world.has_protoss_ground_unit

def world_has_protoss_air_unit(self):
if self.world is None:
return False
else:
return self.world.has_protoss_air_unit
return self.world is not None and self.world.has_protoss_air_unit

def get_very_hard_required_upgrade_level(self):
return 2 if self.advanced_tactics else 3

def weapon_armor_upgrade_count(self, upgrade_item: str, state: CollectionState) -> int:
assert upgrade_item in upgrade_bundle_inverted_lookup.keys()
Expand Down Expand Up @@ -116,7 +95,7 @@ def weapon_armor_upgrade_count(self, upgrade_item: str, state: CollectionState)
count += 1
return count

def terran_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int:
def terran_army_weapon_armor_upgrade_min_level(self, state: CollectionState) -> int:
"""
Minimum W/A upgrade level for unit classes present in the world
:param state:
Expand All @@ -143,12 +122,8 @@ def terran_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int:
)
return count

def terran_very_hard_mission_weapon_armor_upgrades(self, state: CollectionState) -> bool:
upgrade_level = self.terran_army_weapon_armor_upgrade_count(state)
return (
upgrade_level >= 3
or self.advanced_tactics and upgrade_level >= 2
)
def terran_very_hard_mission_weapon_armor_level(self, state: CollectionState) -> bool:
return self.terran_army_weapon_armor_upgrade_min_level(state) >= self.get_very_hard_required_upgrade_level()

# WoL
def terran_common_unit(self, state: CollectionState) -> bool:
Expand Down Expand Up @@ -267,13 +242,13 @@ def welcome_to_the_jungle_z_requirement(self, state: CollectionState) -> bool:
return (
self.zerg_competent_comp(state) and state.has_any({item_names.HYDRALISK, item_names.MUTALISK}, self.player)
) or (
self.advanced_tactics
and self.zerg_common_unit(state)
and (state.has_any({item_names.MUTALISK, item_names.INFESTOR}, self.player)
self.advanced_tactics
and self.zerg_common_unit(state)
and (state.has_any({item_names.MUTALISK, item_names.INFESTOR}, self.player)
or (self.morph_devourer(state) and state.has_any({item_names.HYDRALISK, item_names.SWARM_QUEEN}, self.player))
or (self.morph_viper(state) and state.has(item_names.VIPER_PARASITIC_BOMB, self.player))
)
and self.zerg_army_weapon_armor_upgrade_count(state) >= 1
and self.zerg_army_weapon_armor_upgrade_min_level(state) >= 1
)

def welcome_to_the_jungle_p_requirement(self, state: CollectionState) -> bool:
Expand Down Expand Up @@ -460,7 +435,7 @@ def terran_beats_protoss_deathball(self, state: CollectionState) -> bool:
or state.has_all({item_names.LIBERATOR, item_names.LIBERATOR_RAID_ARTILLERY}, self.player)
) and self.terran_competent_anti_air(state)
or self.terran_competent_comp(state) and self.terran_air_anti_air(state)
) and self.terran_army_weapon_armor_upgrade_count(state) >= 2
) and self.terran_army_weapon_armor_upgrade_min_level(state) >= 2

def marine_medic_upgrade(self, state: CollectionState) -> bool:
"""
Expand Down Expand Up @@ -550,7 +525,7 @@ def terran_base_trasher(self, state: CollectionState) -> bool:
or state.has_all({item_names.BANSHEE, item_names.BANSHEE_SHOCKWAVE_MISSILE_BATTERY}, self.player)
)
)
) and self.terran_very_hard_mission_weapon_armor_upgrades(state)
) and self.terran_very_hard_mission_weapon_armor_level(state)

def terran_mobile_detector(self, state: CollectionState) -> bool:
return state.has_any({item_names.RAVEN, item_names.SCIENCE_VESSEL, item_names.COMMAND_CENTER_SCANNER_SWEEP}, self.player)
Expand Down Expand Up @@ -659,7 +634,7 @@ def all_in_requirement(self, state: CollectionState):
:param state:
:return:
"""
if not self.terran_very_hard_mission_weapon_armor_upgrades(state):
if not self.terran_very_hard_mission_weapon_armor_level(state):
return False
beats_kerrigan = (
state.has_any({item_names.MARINE, item_names.DOMINION_TROOPER, item_names.BANSHEE, item_names.GHOST}, self.player)
Expand Down Expand Up @@ -687,7 +662,7 @@ def all_in_z_requirement(self, state: CollectionState):
:param state:
:return:
"""
if not self.zerg_very_hard_mission_weapon_armor_upgrades(state):
if not self.zerg_very_hard_mission_weapon_armor_level(state):
return False
beats_kerrigan = (
state.has_any({item_names.ZERGLING, item_names.MUTALISK, item_names.INFESTED_MARINE}, self.player)
Expand Down Expand Up @@ -716,7 +691,7 @@ def all_in_p_requirement(self, state: CollectionState):
:param state:
:return:
"""
if not self.protoss_very_hard_mission_weapon_armor_upgrades(state):
if not self.protoss_very_hard_mission_weapon_armor_level(state):
return False
beats_kerrigan = (
# cheap units with multiple small attacks, or anything with Feedback
Expand Down Expand Up @@ -808,7 +783,7 @@ def zerg_defense_rating(self, state: CollectionState, zerg_enemy: bool, air_enem
defense_score += 2
return defense_score

def zerg_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int:
def zerg_army_weapon_armor_upgrade_min_level(self, state: CollectionState) -> int:
count: int = WEAPON_ARMOR_UPGRADE_MAX_LEVEL
if self.world_has_zerg_melee_unit():
count = min(
Expand All @@ -830,12 +805,8 @@ def zerg_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int:
)
return count

def zerg_very_hard_mission_weapon_armor_upgrades(self, state: CollectionState) -> bool:
upgrade_levels = self.zerg_army_weapon_armor_upgrade_count(state)
return (
upgrade_levels >= 3
or self.advanced_tactics and upgrade_levels >= 2
)
def zerg_very_hard_mission_weapon_armor_level(self, state: CollectionState) -> bool:
return self.zerg_army_weapon_armor_upgrade_min_level(state) >= self.get_very_hard_required_upgrade_level()

def zerg_common_unit(self, state: CollectionState) -> bool:
return state.has_any(self.basic_zerg_units, self.player)
Expand Down Expand Up @@ -907,7 +878,7 @@ def morph_tyrannozor(self, state: CollectionState) -> bool:
(state.has(item_names.ULTRALISK, self.player) or self.morphling_enabled)

def zerg_competent_comp(self, state: CollectionState) -> bool:
if self.zerg_army_weapon_armor_upgrade_count(state) < 2:
if self.zerg_army_weapon_armor_upgrade_min_level(state) < 2:
return False
advanced = self.advanced_tactics
core_unit = state.has_any({item_names.ROACH, item_names.ABERRATION, item_names.ZERGLING}, self.player)
Expand Down Expand Up @@ -1066,18 +1037,18 @@ def the_reckoning_requirement(self, state: CollectionState) -> bool:
and (self.zerg_competent_anti_air(state)
or self.terran_competent_anti_air(state)
)
and self.terran_very_hard_mission_weapon_armor_upgrades(state)
and self.zerg_very_hard_mission_weapon_armor_upgrades(state)
and self.terran_very_hard_mission_weapon_armor_level(state)
and self.zerg_very_hard_mission_weapon_armor_level(state)
)
else:
return (
self.zerg_competent_comp(state)
and self.zerg_competent_anti_air(state)
and self.zerg_very_hard_mission_weapon_armor_upgrades(state)
and self.zerg_very_hard_mission_weapon_armor_level(state)
)

# LotV
def protoss_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int:
def protoss_army_weapon_armor_upgrade_min_level(self, state: CollectionState) -> int:
count: int = WEAPON_ARMOR_UPGRADE_MAX_LEVEL + 1 # +1 for Quatro
if self.world_has_protoss_ground_unit():
count = min(
Expand All @@ -1098,14 +1069,8 @@ def protoss_army_weapon_armor_upgrade_count(self, state: CollectionState) -> int
)
return count

def protoss_very_hard_mission_weapon_armor_upgrades(self, state: CollectionState) -> bool:
upgrade_level = self.protoss_army_weapon_armor_upgrade_count(state)
return (
upgrade_level >= 3
or self.advanced_tactics and upgrade_level >= 2
)


def protoss_very_hard_mission_weapon_armor_level(self, state: CollectionState) -> bool:
return self.protoss_army_weapon_armor_upgrade_min_level(state) >= self.get_very_hard_required_upgrade_level()

def protoss_defense_rating(self, state: CollectionState, zerg_enemy: bool) -> int:
"""
Expand Down Expand Up @@ -1378,11 +1343,11 @@ def harbinger_of_oblivion_requirement(self, state: CollectionState) -> bool:

def protoss_competent_comp(self, state: CollectionState) -> bool:
return (
self.protoss_common_unit(state)
and self.protoss_competent_anti_air(state)
and self.protoss_hybrid_counter(state)
and self.protoss_basic_splash(state)
and self.protoss_army_weapon_armor_upgrade_count(state) >= 2
self.protoss_common_unit(state)
and self.protoss_competent_anti_air(state)
and self.protoss_hybrid_counter(state)
and self.protoss_basic_splash(state)
and self.protoss_army_weapon_armor_upgrade_min_level(state) >= 2
)

def steps_of_the_rite_requirement(self, state: CollectionState) -> bool:
Expand Down Expand Up @@ -1418,7 +1383,7 @@ def the_host_requirement(self, state: CollectionState) -> bool:
(
self.protoss_fleet(state)
and self.protoss_static_defense(state)
and self.protoss_army_weapon_armor_upgrade_count(state) >= 2
and self.protoss_army_weapon_armor_upgrade_min_level(state) >= 2
)
or (
self.protoss_competent_comp(state)
Expand All @@ -1433,14 +1398,14 @@ def salvation_requirement(self, state: CollectionState) -> bool:
self.protoss_fleet(state),
self.protoss_static_defense(state)
].count(True) >= 2
) and self.protoss_very_hard_mission_weapon_armor_upgrades(state)
) and self.protoss_very_hard_mission_weapon_armor_level(state)

def into_the_void_requirement(self, state: CollectionState) -> bool:
if not self.protoss_very_hard_mission_weapon_armor_upgrades(state):
if not self.protoss_very_hard_mission_weapon_armor_level(state):
return False
if self.take_over_ai_allies and not (
self.terran_very_hard_mission_weapon_armor_upgrades(state)
and self.zerg_very_hard_mission_weapon_armor_upgrades(state)
self.terran_very_hard_mission_weapon_armor_level(state)
and self.zerg_very_hard_mission_weapon_armor_level(state)
):
return False
return (self.protoss_competent_comp(state)
Expand All @@ -1456,11 +1421,11 @@ def into_the_void_requirement(self, state: CollectionState) -> bool:
)

def essence_of_eternity_requirement(self, state: CollectionState) -> bool:
if not self.terran_very_hard_mission_weapon_armor_upgrades(state):
if not self.terran_very_hard_mission_weapon_armor_level(state):
return False
if self.take_over_ai_allies and not (
self.protoss_very_hard_mission_weapon_armor_upgrades(state)
and self.zerg_very_hard_mission_weapon_armor_upgrades(state)
self.protoss_very_hard_mission_weapon_armor_level(state)
and self.zerg_very_hard_mission_weapon_armor_level(state)
):
return False
defense_score = self.terran_defense_rating(state, False, True)
Expand All @@ -1483,11 +1448,11 @@ def essence_of_eternity_requirement(self, state: CollectionState) -> bool:
)

def amons_fall_requirement(self, state: CollectionState) -> bool:
if not self.zerg_very_hard_mission_weapon_armor_upgrades(state):
if not self.zerg_very_hard_mission_weapon_armor_level(state):
return False
if self.take_over_ai_allies and not (
self.terran_very_hard_mission_weapon_armor_upgrades(state)
and self.protoss_very_hard_mission_weapon_armor_upgrades(state)
self.terran_very_hard_mission_weapon_armor_level(state)
and self.protoss_very_hard_mission_weapon_armor_level(state)
):
return False
if self.take_over_ai_allies:
Expand Down Expand Up @@ -1680,9 +1645,9 @@ def trouble_in_paradise_requirement(self, state: CollectionState) -> bool:

def night_terrors_requirement(self, state: CollectionState) -> bool:
return (
self.terran_common_unit(state)
and self.terran_competent_anti_air(state)
and (
self.terran_common_unit(state)
and self.terran_competent_anti_air(state)
and (
# These can handle the waves of infested, even volatile ones
state.has(item_names.SIEGE_TANK, self.player)
or state.has_all({item_names.VIKING, item_names.VIKING_SHREDDER_ROUNDS}, self.player)
Expand All @@ -1707,15 +1672,15 @@ def night_terrors_requirement(self, state: CollectionState) -> bool:
)
)
)
and self.terran_army_weapon_armor_upgrade_count(state) >= 2
and self.terran_army_weapon_armor_upgrade_min_level(state) >= 2
)

def flashpoint_far_requirement(self, state: CollectionState) -> bool:
return (
self.terran_competent_comp(state)
and self.terran_mobile_detector(state)
and self.terran_defense_rating(state, True, False) >= 6
and self.terran_army_weapon_armor_upgrade_count(state) >= 2
self.terran_competent_comp(state)
and self.terran_mobile_detector(state)
and self.terran_defense_rating(state, True, False) >= 6
and self.terran_army_weapon_armor_upgrade_min_level(state) >= 2
)

def enemy_shadow_tripwires_tool(self, state: CollectionState) -> bool:
Expand Down Expand Up @@ -1791,7 +1756,7 @@ def end_game_requirement(self, state: CollectionState) -> bool:
and state.has_all({item_names.RAVEN, item_names.RAVEN_HUNTER_SEEKER_WEAPON}, self.player)
)
)
and self.terran_very_hard_mission_weapon_armor_upgrades(state)
and self.terran_very_hard_mission_weapon_armor_level(state)
)

def __init__(self, world: 'SC2World'):
Expand Down

0 comments on commit f83fc70

Please sign in to comment.