Skip to content

Commit

Permalink
Stardew Valley: Fix masteries logic so it requires levels and tools (A…
Browse files Browse the repository at this point in the history
…rchipelagoMW#3640)

* fix and add test

* add test to make sure we check xp can be earned

* fix python 3.8 test my god I hope it gets removed soon

* fixing some review comments

* curse you monstersanity

* move month rule to has_level vanilla, so next level is in logic once the previous item is received

* use progressive masteries to skills in test alsanity

* rename reset_collection_state

* add more tests around skill and masteries rules

* progressive level issue

---------

Co-authored-by: agilbert1412 <[email protected]>
  • Loading branch information
Jouramie and agilbert1412 authored Sep 8, 2024
1 parent e4a5ed1 commit cabfef6
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 52 deletions.
72 changes: 38 additions & 34 deletions worlds/stardew_valley/logic/skill_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from ..data.harvest import HarvestCropSource
from ..mods.logic.magic_logic import MagicLogicMixin
from ..mods.logic.mod_skills_levels import get_mod_skill_levels
from ..stardew_rule import StardewRule, True_, False_, true_, And
from ..stardew_rule import StardewRule, true_, True_, False_
from ..strings.craftable_names import Fishing
from ..strings.machine_names import Machine
from ..strings.performance_names import Performance
from ..strings.quality_names import ForageQuality
from ..strings.region_names import Region
from ..strings.skill_names import Skill, all_mod_skills
from ..strings.skill_names import Skill, all_mod_skills, all_vanilla_skills
from ..strings.tool_names import ToolMaterial, Tool
from ..strings.wallet_item_names import Wallet

Expand All @@ -43,22 +43,17 @@ def can_earn_level(self, skill: str, level: int) -> StardewRule:
if level <= 0:
return True_()

tool_level = (level - 1) // 2
tool_level = min(4, (level - 1) // 2)
tool_material = ToolMaterial.tiers[tool_level]
months = max(1, level - 1)
months_rule = self.logic.time.has_lived_months(months)

if self.options.skill_progression != options.SkillProgression.option_vanilla:
previous_level_rule = self.logic.skill.has_level(skill, level - 1)
else:
previous_level_rule = true_
previous_level_rule = self.logic.skill.has_previous_level(skill, level)

if skill == Skill.fishing:
xp_rule = self.logic.tool.has_fishing_rod(max(tool_level, 3))
elif skill == Skill.farming:
xp_rule = self.can_get_farming_xp & self.logic.tool.has_tool(Tool.hoe, tool_material) & self.logic.tool.can_water(tool_level)
elif skill == Skill.foraging:
xp_rule = (self.can_get_foraging_xp & self.logic.tool.has_tool(Tool.axe, tool_material)) |\
xp_rule = (self.can_get_foraging_xp & self.logic.tool.has_tool(Tool.axe, tool_material)) | \
self.logic.magic.can_use_clear_debris_instead_of_tool_level(tool_level)
elif skill == Skill.mining:
xp_rule = self.logic.tool.has_tool(Tool.pickaxe, tool_material) | \
Expand All @@ -70,22 +65,34 @@ def can_earn_level(self, skill: str, level: int) -> StardewRule:
xp_rule = xp_rule & self.logic.region.can_reach(Region.mines_floor_5)
elif skill in all_mod_skills:
# Ideal solution would be to add a logic registry, but I'm too lazy.
return previous_level_rule & months_rule & self.logic.mod.skill.can_earn_mod_skill_level(skill, level)
return previous_level_rule & self.logic.mod.skill.can_earn_mod_skill_level(skill, level)
else:
raise Exception(f"Unknown skill: {skill}")

return previous_level_rule & months_rule & xp_rule
return previous_level_rule & xp_rule

# Should be cached
def has_level(self, skill: str, level: int) -> StardewRule:
if level <= 0:
return True_()
assert level >= 0, f"There is no level before level 0."
if level == 0:
return true_

if self.options.skill_progression == options.SkillProgression.option_vanilla:
return self.logic.skill.can_earn_level(skill, level)

return self.logic.received(f"{skill} Level", level)

def has_previous_level(self, skill: str, level: int) -> StardewRule:
assert level > 0, f"There is no level before level 0."
if level == 1:
return true_

if self.options.skill_progression == options.SkillProgression.option_vanilla:
months = max(1, level - 1)
return self.logic.time.has_lived_months(months)

return self.logic.received(f"{skill} Level", level - 1)

@cache_self1
def has_farming_level(self, level: int) -> StardewRule:
return self.logic.skill.has_level(Skill.farming, level)
Expand All @@ -108,18 +115,9 @@ def has_total_level(self, level: int, allow_modded_skills: bool = False) -> Star
return rule_with_fishing
return self.logic.time.has_lived_months(months_with_4_skills) | rule_with_fishing

def has_all_skills_maxed(self, included_modded_skills: bool = True) -> StardewRule:
if self.options.skill_progression == options.SkillProgression.option_vanilla:
return self.has_total_level(50)
skills_items = vanilla_skill_items
if included_modded_skills:
skills_items += get_mod_skill_levels(self.options.mods)
return And(*[self.logic.received(skill, 10) for skill in skills_items])

def can_enter_mastery_cave(self) -> StardewRule:
if self.options.skill_progression == options.SkillProgression.option_progressive_with_masteries:
return self.logic.received(Wallet.mastery_of_the_five_ways)
return self.has_all_skills_maxed()
def has_any_skills_maxed(self, included_modded_skills: bool = True) -> StardewRule:
skills = self.content.skills.keys() if included_modded_skills else sorted(all_vanilla_skills)
return self.logic.or_(*(self.logic.skill.has_level(skill, 10) for skill in skills))

@cached_property
def can_get_farming_xp(self) -> StardewRule:
Expand Down Expand Up @@ -197,13 +195,19 @@ def can_forage_quality(self, quality: str) -> StardewRule:
return self.has_level(Skill.foraging, 9)
return False_()

@cached_property
def can_earn_mastery_experience(self) -> StardewRule:
if self.options.skill_progression != options.SkillProgression.option_progressive_with_masteries:
return self.has_all_skills_maxed() & self.logic.time.has_lived_max_months
return self.logic.time.has_lived_max_months
def can_earn_mastery(self, skill: str) -> StardewRule:
# Checking for level 11, so it includes having level 10 and being able to earn xp.
return self.logic.skill.can_earn_level(skill, 11) & self.logic.region.can_reach(Region.mastery_cave)

def has_mastery(self, skill: str) -> StardewRule:
if self.options.skill_progression != options.SkillProgression.option_progressive_with_masteries:
return self.can_earn_mastery_experience and self.logic.region.can_reach(Region.mastery_cave)
return self.logic.received(f"{skill} Mastery")
if self.options.skill_progression == options.SkillProgression.option_progressive_with_masteries:
return self.logic.received(f"{skill} Mastery")

return self.logic.skill.can_earn_mastery(skill)

@cached_property
def can_enter_mastery_cave(self) -> StardewRule:
if self.options.skill_progression == options.SkillProgression.option_progressive_with_masteries:
return self.logic.received(Wallet.mastery_of_the_five_ways)

return self.has_any_skills_maxed(included_modded_skills=False)
12 changes: 7 additions & 5 deletions worlds/stardew_valley/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def set_bundle_rules(bundle_rooms: List[BundleRoom], logic: StardewLogic, multiw
extra_raccoons = extra_raccoons + num
bundle_rules = logic.received(CommunityUpgrade.raccoon, extra_raccoons) & bundle_rules
if num > 1:
previous_bundle_name = f"Raccoon Request {num-1}"
previous_bundle_name = f"Raccoon Request {num - 1}"
bundle_rules = bundle_rules & logic.region.can_reach_location(previous_bundle_name)
room_rules.append(bundle_rules)
MultiWorldRules.set_rule(location, bundle_rules)
Expand All @@ -168,13 +168,16 @@ def set_skills_rules(logic: StardewLogic, multiworld, player, world_options: Sta
mods = world_options.mods
if world_options.skill_progression == SkillProgression.option_vanilla:
return

for i in range(1, 11):
set_vanilla_skill_rule_for_level(logic, multiworld, player, i)
set_modded_skill_rule_for_level(logic, multiworld, player, mods, i)
if world_options.skill_progression != SkillProgression.option_progressive_with_masteries:

if world_options.skill_progression == SkillProgression.option_progressive:
return

for skill in [Skill.farming, Skill.fishing, Skill.foraging, Skill.mining, Skill.combat]:
MultiWorldRules.set_rule(multiworld.get_location(f"{skill} Mastery", player), logic.skill.can_earn_mastery_experience)
MultiWorldRules.set_rule(multiworld.get_location(f"{skill} Mastery", player), logic.skill.can_earn_mastery(skill))


def set_vanilla_skill_rule_for_level(logic: StardewLogic, multiworld, player, level: int):
Expand Down Expand Up @@ -256,8 +259,7 @@ def set_entrance_rules(logic: StardewLogic, multiworld, player, world_options: S
set_entrance_rule(multiworld, player, LogicEntrance.farmhouse_cooking, logic.cooking.can_cook_in_kitchen)
set_entrance_rule(multiworld, player, LogicEntrance.shipping, logic.shipping.can_use_shipping_bin)
set_entrance_rule(multiworld, player, LogicEntrance.watch_queen_of_sauce, logic.action.can_watch(Channel.queen_of_sauce))
set_entrance_rule(multiworld, player, Entrance.forest_to_mastery_cave, logic.skill.can_enter_mastery_cave())
set_entrance_rule(multiworld, player, Entrance.forest_to_mastery_cave, logic.skill.can_enter_mastery_cave())
set_entrance_rule(multiworld, player, Entrance.forest_to_mastery_cave, logic.skill.can_enter_mastery_cave)
set_entrance_rule(multiworld, player, LogicEntrance.buy_experience_books, logic.time.has_lived_months(2))
set_entrance_rule(multiworld, player, LogicEntrance.buy_year1_books, logic.time.has_year_two)
set_entrance_rule(multiworld, player, LogicEntrance.buy_year3_books, logic.time.has_year_three)
Expand Down
8 changes: 7 additions & 1 deletion worlds/stardew_valley/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def allsanity_no_mods_6_x_x():
options.QuestLocations.internal_name: 56,
options.SeasonRandomization.internal_name: options.SeasonRandomization.option_randomized,
options.Shipsanity.internal_name: options.Shipsanity.option_everything,
options.SkillProgression.internal_name: options.SkillProgression.option_progressive,
options.SkillProgression.internal_name: options.SkillProgression.option_progressive_with_masteries,
options.SpecialOrderLocations.internal_name: options.SpecialOrderLocations.option_board_qi,
options.ToolProgression.internal_name: options.ToolProgression.option_progressive,
options.TrapItems.internal_name: options.TrapItems.option_nightmare,
Expand Down Expand Up @@ -310,6 +310,12 @@ def create_item(self, item: str) -> StardewItem:
self.multiworld.worlds[self.player].total_progression_items -= 1
return created_item

def remove_one_by_name(self, item: str) -> None:
self.remove(self.create_item(item))

def reset_collection_state(self):
self.multiworld.state = self.original_state.copy()


pre_generated_worlds = {}

Expand Down
97 changes: 85 additions & 12 deletions worlds/stardew_valley/test/rules/TestSkills.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,113 @@
from ... import HasProgressionPercent
from ... import HasProgressionPercent, StardewLogic
from ...options import ToolProgression, SkillProgression, Mods
from ...strings.skill_names import all_skills
from ...strings.skill_names import all_skills, all_vanilla_skills, Skill
from ...test import SVTestBase


class TestVanillaSkillLogicSimplification(SVTestBase):
class TestSkillProgressionVanilla(SVTestBase):
options = {
SkillProgression.internal_name: SkillProgression.option_vanilla,
ToolProgression.internal_name: ToolProgression.option_progressive,
}

def test_skill_logic_has_level_only_uses_one_has_progression_percent(self):
rule = self.multiworld.worlds[1].logic.skill.has_level("Farming", 8)
self.assertEqual(1, sum(1 for i in rule.current_rules if type(i) == HasProgressionPercent))
rule = self.multiworld.worlds[1].logic.skill.has_level(Skill.farming, 8)
self.assertEqual(1, sum(1 for i in rule.current_rules if type(i) is HasProgressionPercent))

def test_has_mastery_requires_month_equivalent_to_10_levels(self):
logic: StardewLogic = self.multiworld.worlds[1].logic
rule = logic.skill.has_mastery(Skill.farming)
time_rule = logic.time.has_lived_months(10)

class TestAllSkillsRequirePrevious(SVTestBase):
self.assertIn(time_rule, rule.current_rules)


class TestSkillProgressionProgressive(SVTestBase):
options = {
SkillProgression.internal_name: SkillProgression.option_progressive_with_masteries,
SkillProgression.internal_name: SkillProgression.option_progressive,
Mods.internal_name: frozenset(Mods.valid_keys),
}

def test_all_skill_levels_require_previous_level(self):
for skill in all_skills:
self.collect_everything()
self.remove_by_name(f"{skill} Level")

for level in range(1, 11):
location_name = f"Level {level} {skill}"
location = self.multiworld.get_location(location_name, self.player)

with self.subTest(location_name):
can_reach = self.can_reach_location(location_name)
if level > 1:
self.assertFalse(can_reach)
self.assert_reach_location_false(location, self.multiworld.state)
self.collect(f"{skill} Level")
can_reach = self.can_reach_location(location_name)
self.assertTrue(can_reach)
self.multiworld.state = self.original_state.copy()

self.assert_reach_location_true(location, self.multiworld.state)

self.reset_collection_state()

def test_has_level_requires_exact_amount_of_levels(self):
logic: StardewLogic = self.multiworld.worlds[1].logic
rule = logic.skill.has_level(Skill.farming, 8)
level_rule = logic.received("Farming Level", 8)

self.assertEqual(level_rule, rule)

def test_has_previous_level_requires_one_less_level_than_requested(self):
logic: StardewLogic = self.multiworld.worlds[1].logic
rule = logic.skill.has_previous_level(Skill.farming, 8)
level_rule = logic.received("Farming Level", 7)

self.assertEqual(level_rule, rule)

def test_has_mastery_requires_10_levels(self):
logic: StardewLogic = self.multiworld.worlds[1].logic
rule = logic.skill.has_mastery(Skill.farming)
level_rule = logic.received("Farming Level", 10)

self.assertIn(level_rule, rule.current_rules)


class TestSkillProgressionProgressiveWithMasteryWithoutMods(SVTestBase):
options = {
SkillProgression.internal_name: SkillProgression.option_progressive_with_masteries,
ToolProgression.internal_name: ToolProgression.option_progressive,
Mods.internal_name: frozenset(),
}

def test_has_mastery_requires_the_item(self):
logic: StardewLogic = self.multiworld.worlds[1].logic
rule = logic.skill.has_mastery(Skill.farming)
received_mastery = logic.received("Farming Mastery")

self.assertEqual(received_mastery, rule)

def test_given_all_levels_when_can_earn_mastery_then_can_earn_mastery(self):
self.collect_everything()

for skill in all_vanilla_skills:
with self.subTest(skill):
location = self.multiworld.get_location(f"{skill} Mastery", self.player)
self.assert_reach_location_true(location, self.multiworld.state)

self.reset_collection_state()

def test_given_one_level_missing_when_can_earn_mastery_then_cannot_earn_mastery(self):
for skill in all_vanilla_skills:
with self.subTest(skill):
self.collect_everything()
self.remove_one_by_name(f"{skill} Level")

location = self.multiworld.get_location(f"{skill} Mastery", self.player)
self.assert_reach_location_false(location, self.multiworld.state)

self.reset_collection_state()

def test_given_one_tool_missing_when_can_earn_mastery_then_cannot_earn_mastery(self):
self.collect_everything()

self.remove_one_by_name(f"Progressive Pickaxe")
location = self.multiworld.get_location("Mining Mastery", self.player)
self.assert_reach_location_false(location, self.multiworld.state)

self.reset_collection_state()

0 comments on commit cabfef6

Please sign in to comment.