Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: New Event classes #235

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ tmp/*
/wheelhouse
docs/build
docs/src
*.orig
2 changes: 1 addition & 1 deletion textworld/challenges/tests/test_coin_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def test_making_coin_collector():

settings = {"level": level}
game = coin_collector.make(settings, options)
assert len(game.quests[0].commands) == expected[level]["quest_length"]
assert len(game.walkthrough) == expected[level]["quest_length"]
assert len(game.world.rooms) == expected[level]["nb_rooms"]
2 changes: 1 addition & 1 deletion textworld/challenges/tests/test_treasure_hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def test_making_treasure_hunter_games():

settings = {"level": level}
game = treasure_hunter.make(settings, options)
assert len(game.quests[0].commands) == game.metadata["quest_length"], "Level {}".format(level)
assert len(game.walkthrough) == game.metadata["quest_length"], "Level {}".format(level)
assert len(game.world.rooms) == game.metadata["world_size"], "Level {}".format(level)
2 changes: 1 addition & 1 deletion textworld/challenges/tw_cooking/cooking.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ def make(settings: Mapping[str, str], options: Optional[GameOptions] = None) ->
start_room = rng_map.choice(M.rooms)
M.set_player(start_room)

M.grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar)
M.grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar, kb=options.kb)

# Remove every food preparation with grilled, if there is no BBQ.
if M.find_by_name("BBQ") is None:
Expand Down
2 changes: 1 addition & 1 deletion textworld/challenges/tw_treasure_hunter/treasure_hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def make_game(mode: str, options: GameOptions) -> textworld.Game:
quest = Quest(win_events=[event],
fail_events=[Event(conditions={Proposition("in", [wrong_obj, world.inventory])})])

grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar)
grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar, kb=options.kb)
game = textworld.generator.make_game_with(world, [quest], grammar)
game.metadata.update(metadata)
mode_choice = modes.index(mode)
Expand Down
15 changes: 8 additions & 7 deletions textworld/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from textworld.generator.chaining import ChainingOptions, QuestGenerationError
from textworld.generator.chaining import sample_quest
from textworld.generator.world import World
from textworld.generator.game import Game, Quest, Event, GameOptions
from textworld.generator.game import Game, Quest, GameOptions
from textworld.generator.game import EventCondition, EventAction, EventAnd, EventOr
from textworld.generator.game import Event # For backward compatibility
from textworld.generator.graph_networks import create_map, create_small_map
from textworld.generator.text_generation import generate_text_from_grammar

Expand Down Expand Up @@ -142,19 +144,18 @@ def make_quest(world: Union[World, State], options: Optional[GameOptions] = None
for i in range(1, len(chain.nodes)):
actions.append(chain.actions[i - 1])
if chain.nodes[i].breadth != chain.nodes[i - 1].breadth:
event = Event(actions)
quests.append(Quest(win_events=[event]))
quests.append(Quest(win_event=EventCondition(actions=actions)))

actions.append(chain.actions[-1])
event = Event(actions)
quests.append(Quest(win_events=[event]))
quests.append(Quest(win_event=EventCondition(actions=actions)))

return quests


def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None) -> Grammar:
def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None,
kb: Optional[KnowledgeBase] = None) -> Grammar:
rng = g_rng.next() if rng is None else rng
grammar = Grammar(options, rng)
grammar = Grammar(options, rng, kb)
grammar.check()
return grammar

Expand Down
Loading