Skip to content

Commit

Permalink
Restore bridge repair upon examining the bridge
Browse files Browse the repository at this point in the history
This change hints to the user to examine the bridge to learn if it's
broken. Examining the bridge effectively restores its state from
previous sessions.

Prior to this change, the state of the bridge would not get persisted in
the save file, and the user (with the engineer) would need to fix it
whenever it loaded the game back.

This approach provides slightly better continuity, and is implemented
within the constraints of the current framework.
  • Loading branch information
dmasspla committed Mar 12, 2024
1 parent c83febf commit de5cb4f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,30 @@ def _hole(state: GameState, world):
)


def _bridge(state: GameState, world) -> str:
def _examine_bridge(state: GameState, world) -> str:
location = world.get(state.current_location_label)
if "hbridgestate" in state.state_dict:
location.exits[Direction.NORTH] = "hadamard1"
if Direction.NORTH in location.exits:
return "You have already fixed the bridge."
return (
"It looks as if this bridge has fallen into disrepair.\n"
"Parts have decayed and have fallen into the river. However,\n"
"there are plenty of logs around, and the supports are intact,\n"
"so it should be possible to repair it, for someone with the right skills."
)


def _fix_bridge(state: GameState, world) -> str:
location = world.get(state.current_location_label)
if Direction.NORTH in location.exits:
return "You have already fixed the bridge."
has_engineer = any(isinstance(qar, Engineer) for qar in state.party)
if not has_engineer:
return "You do not have the required skills to fix the bridge."
else:
location = world.get(state.current_location_label)
if Direction.NORTH in location.exits:
return "You have already fixed the bridge."
location.exits[Direction.NORTH] = "hadamard1"
state.state_dict["hbridgestate"] = "fixed"
return "The engineer uses nearby logs to repair the bridge and provide a safe passage."


Expand All @@ -146,17 +161,12 @@ def _bridge(state: GameState, world) -> str:
(
EXAMINE,
["bridge"],
(
"It looks as if this bridge has fallen into disrepair.\n"
"Parts have decayed and have fallen into the river. However,\n"
"there are plenty of logs around, and the supports are intact,\n"
"so it should be possible to repair it, for someone with the right skills."
),
_examine_bridge,
),
(
["fix", "repair"],
["bridge"],
_bridge,
_fix_bridge,
),
]
)
Expand Down Expand Up @@ -320,8 +330,8 @@ def _bridge(state: GameState, world) -> str:
description=(
"Here, at the southern shore of the river is a rickety bridge\n"
"that leads to the northern side of the river. Pieces of the bridge\n"
"have collapsed and fallen apart, and there seems to be no way to\n"
"cross safely, given the condition the bridge is in now."
"have collapsed and fallen apart, and without further examination\n"
"it's unclear if the bridge is safe to cross, given its current condition."
),
encounters=[red_foam(2, 0.3), green_foam(3, 0.2)],
items=[BRIDGE],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,61 @@ def test_bridge():
state = game_state.GameState(party=[c], user_input=["Hamilton"], file=io.StringIO())
state.current_location_label = "classical12"
test_world = go_directions("nnwnnnne")
bridge_location = test_world.get("classical12")
fix = test_world.current_location.get_action("fix bridge")
examine = test_world.current_location.get_action("examine bridge")

# The world and the game state are at the location of the broken bridge, which can be fixed and examined.
assert test_world.current_location.title == "At a Broken Bridge"
action = test_world.current_location.get_action("fix bridge")
assert callable(action)
msg = action(state, test_world)
assert (
test_world.current_location.label
== state.current_location_label
== bridge_location.label
)
assert callable(fix)
assert callable(examine)

# The bridge is not yet fixed
assert Direction.NORTH not in bridge_location.exits

# Examining the bridge doesn't fix it, nor can it be fixed without an engineer.
msg = examine(state, test_world)
assert Direction.NORTH not in bridge_location.exits

msg = fix(state, test_world)
assert msg == "You do not have the required skills to fix the bridge."
bridge_location = test_world.get("classical12")
assert Direction.NORTH not in bridge_location.exits

# The engineer is able to fix the bridge,
# and the fix is saved in the game state.
c = classes.Engineer("Tesla")
state.party.append(c)
assert callable(action)
msg = action(state, test_world)
msg = fix(state, test_world)
assert (
msg
== "The engineer uses nearby logs to repair the bridge and provide a safe passage."
)
assert Direction.NORTH in bridge_location.exits
assert callable(action)
msg = action(state, test_world)
assert state.state_dict["hbridgestate"] == "fixed"

# The bridge is already fixed,
# fixing it or examining it doesn't change that.
msg = fix(state, test_world)
assert msg == "You have already fixed the bridge."

msg = examine(state, test_world)
assert msg == "You have already fixed the bridge."

assert Direction.NORTH in bridge_location.exits

# Remove North direction to simulate restarting the game
# with bridge fix in the save file.
bridge_location.exits.pop(Direction.NORTH)

# The bridge doesn't appear fixed,
# but on further examination we learn that it had been fixed,
# so now we know it can be used to exit North.
assert Direction.NORTH not in bridge_location.exits
msg = examine(state, test_world)
assert msg == "You have already fixed the bridge."
assert Direction.NORTH in bridge_location.exits

0 comments on commit de5cb4f

Please sign in to comment.