Skip to content

Commit 94a3fa1

Browse files
committed
Refactor new Event class. Makes it work with DependencyTree. Add tests.
1 parent e6c4c52 commit 94a3fa1

File tree

22 files changed

+1008
-1759
lines changed

22 files changed

+1008
-1759
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ tmp/*
2424
*.ipynb_checkpoints
2525
/dist
2626
/wheelhouse
27+
docs/build
28+
docs/src
2729
*.orig

textworld/challenges/tests/test_coin_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def test_making_coin_collector():
2020

2121
settings = {"level": level}
2222
game = coin_collector.make(settings, options)
23-
assert len(game.quests[0].commands) == expected[level]["quest_length"]
23+
assert len(game.walkthrough) == expected[level]["quest_length"]
2424
assert len(game.world.rooms) == expected[level]["nb_rooms"]

textworld/challenges/tests/test_treasure_hunter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ def test_making_treasure_hunter_games():
1313

1414
settings = {"level": level}
1515
game = treasure_hunter.make(settings, options)
16-
assert len(game.quests[0].commands) == game.metadata["quest_length"], "Level {}".format(level)
16+
assert len(game.walkthrough) == game.metadata["quest_length"], "Level {}".format(level)
1717
assert len(game.world.rooms) == game.metadata["world_size"], "Level {}".format(level)

textworld/challenges/tw_coin_collector/coin_collector.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,30 @@
1515
other than the coin to collect.
1616
"""
1717

18+
import os
1819
import argparse
20+
from os.path import join as pjoin
1921
from typing import Mapping, Optional, Any
2022

2123
import textworld
2224
from textworld.generator.graph_networks import reverse_direction
2325

2426
from textworld.utils import encode_seeds
25-
from textworld.generator.game import GameOptions, Quest, EventCondition
27+
from textworld.generator.data import KnowledgeBase
28+
from textworld.generator.game import GameOptions, Quest, Event
2629
from textworld.challenges import register
2730

2831

32+
KB_PATH = pjoin(os.path.dirname(__file__), "textworld_data")
33+
34+
2935
def build_argparser(parser=None):
3036
parser = parser or argparse.ArgumentParser()
3137

3238
group = parser.add_argument_group('Coin Collector game settings')
3339
group.add_argument("--level", required=True, type=int,
3440
help="The difficulty level. Must be between 1 and 300 (included).")
3541

36-
group.add_argument("--force-entity-numbering", required=True, action="store_true",
37-
help="This will set `--entity-numbering` to be True which is required for this challenge.")
38-
3942
return parser
4043

4144

@@ -49,7 +52,7 @@ def make(settings: Mapping[str, Any], options: Optional[GameOptions] = None) ->
4952
:py:class:`textworld.GameOptions <textworld.generator.game.GameOptions>`
5053
for the list of available options).
5154
52-
.. warning:: This challenge requires `options.grammar.allowed_variables_numbering` to be `True`.
55+
.. warning:: This challenge enforces `options.grammar.allowed_variables_numbering` to be `True`.
5356
5457
Returns:
5558
Generated game.
@@ -68,9 +71,11 @@ def make(settings: Mapping[str, Any], options: Optional[GameOptions] = None) ->
6871
"""
6972
options = options or GameOptions()
7073

74+
# Load knowledge base specific to this challenge.
75+
options.kb = KnowledgeBase.load(KB_PATH)
76+
7177
# Needed for games with a lot of rooms.
72-
options.grammar.allowed_variables_numbering = settings["force_entity_numbering"]
73-
assert options.grammar.allowed_variables_numbering
78+
options.grammar.allowed_variables_numbering = True
7479

7580
level = settings["level"]
7681
if level < 1 or level > 300:
@@ -167,7 +172,7 @@ def make_game(mode: str, options: GameOptions) -> textworld.Game:
167172

168173
# Generate the quest thats by collecting the coin.
169174
quest = Quest(win_events=[
170-
EventCondition(conditions={M.new_fact("in", coin, M.inventory)})
175+
Event(conditions={M.new_fact("in", coin, M.inventory)})
171176
])
172177

173178
M.quests = [quest]

textworld/challenges/tw_coin_collector/textworld_data/logic/player.twl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ type P {
44
look :: at(P, r) -> at(P, r); # Nothing changes.
55
}
66

7+
reverse_rules {
8+
look :: look;
9+
}
10+
711
inform7 {
812
commands {
913
look :: "look" :: "looking";
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# # key
2-
# type k : o {
3-
# predicates {
4-
# match(k, c);
5-
# match(k, d);
6-
# }
1+
# key
2+
type k : o {
3+
predicates {
4+
match(k, c);
5+
match(k, d);
6+
}
77

8-
# constraints {
9-
# k1 :: match(k, c) & match(k', c) -> fail();
10-
# k2 :: match(k, c) & match(k, c') -> fail();
11-
# k3 :: match(k, d) & match(k', d) -> fail();
12-
# k4 :: match(k, d) & match(k, d') -> fail();
13-
# }
8+
constraints {
9+
k1 :: match(k, c) & match(k', c) -> fail();
10+
k2 :: match(k, c) & match(k, c') -> fail();
11+
k3 :: match(k, d) & match(k', d) -> fail();
12+
k4 :: match(k, d) & match(k, d') -> fail();
13+
}
1414

15-
# inform7 {
16-
# type {
17-
# kind :: "key";
18-
# }
15+
inform7 {
16+
type {
17+
kind :: "key";
18+
}
1919

20-
# predicates {
21-
# match(k, c) :: "The matching key of the {c} is the {k}";
22-
# match(k, d) :: "The matching key of the {d} is the {k}";
23-
# }
24-
# }
25-
# }
20+
predicates {
21+
match(k, c) :: "The matching key of the {c} is the {k}";
22+
match(k, d) :: "The matching key of the {d} is the {k}";
23+
}
24+
}
25+
}

textworld/challenges/tw_simple/textworld_data/logic/room.twl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ type r {
77
north_of(r, r);
88
west_of(r, r);
99

10+
north_of/d(r, d, r);
11+
west_of/d(r, d, r);
12+
1013
free(r, r);
1114

1215
south_of(r, r') = north_of(r', r);
1316
east_of(r, r') = west_of(r', r);
1417

15-
# north_of/d(r, d, r);
16-
# west_of/d(r, d, r);
17-
# south_of/d(r, d, r') = north_of/d(r', d, r);
18-
# east_of/d(r, d, r') = west_of/d(r', d, r);
18+
south_of/d(r, d, r') = north_of/d(r', d, r);
19+
east_of/d(r, d, r') = west_of/d(r', d, r);
1920
}
2021

2122
rules {
@@ -65,10 +66,10 @@ type r {
6566
east_of(r, r') :: "The {r} is mapped east of {r'}";
6667
west_of(r, r') :: "The {r} is mapped west of {r'}";
6768

68-
# north_of/d(r, d, r') :: "South of {r} and north of {r'} is a door called {d}";
69-
# south_of/d(r, d, r') :: "North of {r} and south of {r'} is a door called {d}";
70-
# east_of/d(r, d, r') :: "West of {r} and east of {r'} is a door called {d}";
71-
# west_of/d(r, d, r') :: "East of {r} and west of {r'} is a door called {d}";
69+
north_of/d(r, d, r') :: "South of {r} and north of {r'} is a door called {d}";
70+
south_of/d(r, d, r') :: "North of {r} and south of {r'} is a door called {d}";
71+
east_of/d(r, d, r') :: "West of {r} and east of {r'} is a door called {d}";
72+
west_of/d(r, d, r') :: "East of {r} and west of {r'} is a door called {d}";
7273
}
7374

7475
commands {

textworld/generator/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from textworld.generator.chaining import ChainingOptions, QuestGenerationError
1818
from textworld.generator.chaining import sample_quest
1919
from textworld.generator.world import World
20-
from textworld.generator.game import Game, Quest, Event, GameOptions
20+
from textworld.generator.game import Game, Quest, GameOptions
21+
from textworld.generator.game import EventCondition, EventAction, EventAnd, EventOr
22+
from textworld.generator.game import Event # For backward compatibility
2123
from textworld.generator.graph_networks import create_map, create_small_map
2224
from textworld.generator.text_generation import generate_text_from_grammar
2325

@@ -142,12 +144,10 @@ def make_quest(world: Union[World, State], options: Optional[GameOptions] = None
142144
for i in range(1, len(chain.nodes)):
143145
actions.append(chain.actions[i - 1])
144146
if chain.nodes[i].breadth != chain.nodes[i - 1].breadth:
145-
event = Event(actions)
146-
quests.append(Quest(win_events=[event]))
147+
quests.append(Quest(win_event=EventCondition(actions=actions)))
147148

148149
actions.append(chain.actions[-1])
149-
event = Event(actions)
150-
quests.append(Quest(win_events=[event]))
150+
quests.append(Quest(win_event=EventCondition(actions=actions)))
151151

152152
return quests
153153

textworld/generator/data/text_grammars/house_quests.twg

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)