Wordgrind: a Logic Programming Language for Creating Quality-Based Narrative Jan Wanot Royal Holloway University of London Egham, Surrey, United Kingdom jan.wanot.2017@rhul.ac.uk Abstract writing game logic while being completely flexible. Despite requiring little to no conventional programming experience, We present a tool for writing interactive fiction based on the storylet model (Kreminski and Wardrip-Fruin 2018), which Wordgrind can be used for creating interactive systems of allows for crafting an interactive narrative out of many dis- great complexity. crete “chunks“, the order and presence of which is based on player choices and the structure of game logic. To facilitate Example creation of the latter, our tool is equipped with an integrated Suppose we want to create an interactive system with a declarative logic programming capability, similar to a logic world model evocative of traditional parser-based interac- programming language such as Prolog. The created narrative tive fiction: a series of rooms that the player can navigate, systems can be exported to a browser-readable format, which allows for easy distribution and future preservation. containing items that can be pick up and placed in the in- ventory. It should be noted that while unlike more special- ized IF creation tools such as Inform 7 (2006), Wordgrind Introduction itself contains no built-in preference for that model of in- Quality-based narrative, also known as the storylet model teraction compared to any other alternative (i.e. one could (Kreminski and Wardrip-Fruin 2018) allows for structuring just as easily use Wordgrind to create a dialogue system, a narrative content in a way that enables the content to be se- social simulation system, or a resource management game), lected and ordered in a dynamic way depending on the game such a system can still be created very simply and without state and player choices. Our tool incorporates a full logic much boilerplate code by utilizing some of the logic pro- programming capability that allows for creation, manipu- gramming features of the language in conjunction with the lation and selection of content in an intuitive and efficient storylet model. way, along with more advanced state tracking then offered Predicates: by most other QBN interactive fiction authoring systems. - is a room The functionality of Wordgrind as a tool can be separated - is a room into three discrete aspects bound together by the underlying - is a room logic programming engine: - is a room • A textual templating system, allowing parts of longer text - is connected to chunks to be substituted, reordered or omitted based on - is connected to programmatic conditions, as well as allowing for creating - is connected to multiple variants of a given text chunk to be created based on logical terms. - ?A and ?B can be walked between: or: • A content selection system, allowing both for present- - ?A is connected to ?B ing various available context-dependent player choices, as - ?B is connected to ?A well as more automated “actions“, which are executed au- tomatically as soon as their preconditions are met. Unique facts: - Player is in ?_ • A dynamic database of current game state, presented in a form of structured logic programming terms, along with Initial state: convenience features for simpler state management. condition: All of these aspects are enabled by the logic program- - Player is in ming functionality, which allows for a declarative style of - is in - is in Copyright © 2021for this paper by its authors. Use permitted under - is in Creative Commons License Attribution 4.0 International (CC BY displays: 4.0). You wake up in a cold sweat. You had nightmares all night, and now you have a horrible headache. tions. Here is an example interaction with the system: default: You wake up in a cold sweat. You had nightmares all Choices: night, and now you have a horrible headache. Walk from ?A to ?B: available when: > Walk from bedroom to living room - Player is in ?A such that: You walk from bedroom to living room. and: - ?B is a room > Walk from living room to kitchen - ?A is a room > Walk from living room to bathroom - ?A and ?B can be walked between > Walk from living room to bedroom displays: > Pick up TV remote You walk from ?A to ?B. causes: You have picked up TV remote. - Player is in ?B > Walk from living room to kitchen Pick up ?Item: > Walk from living room to bathroom available when: > Walk from living room to bedroom - Player is in ?Place > Drop TV remote - ?Item is in ?Place displays: You walk from living room to bathroom. You have picked up ?Item. causes: > Walk from bathroom to living room - Player has ?Item > Pick up soap - removes: ?Item is in ?Place > Drop TV remote Drop ?Item: Content selection overview available when: - Player is in ?Place - Player has ?Item displays: You have dropped ?Item. causes: - ?Item is in ?Place - removes: Player has ?Item Our example scenario consists of four rooms (kitchen, liv- ing room, bathroom and a bedroom) which are declared us- ing the “{} is a room“ predicate. We establish connections between rooms using the “{} is connected to {}“ predicate, but for actual choices given to the player we will be using the “{} and {} can be walked between“ predicate, to allow player to walk in both directions (we assume there are no one-way passages in this system for convenience). Since content selection is one of the most important as- Since the locations of the player and the items are dy- pects of every interactive storytelling system, we implement namic, we use database terms rather than a predicate to rep- a relatively robust solution that allows for authoring both resent them. The initial condition of the system, along with the pattern of interaction with the player and more the au- the starting text, is placed in the initial state section. In ad- tomated simulation-like systems (such as NPCs or dynamic dition, we declare the player location to be a unique fact, state of the world) using the same storylet-based content sys- meaning that the old value in the database will be deleted au- tem. tomatically when the new one is inserted, without the need In Wordgrind, content is divided into pieces of content to declare so explicitly. called Elements. Elements can be either Actions or Choices, Our scenario consists of a single default deck with three and are grouped into Decks. choices: one for walking between the rooms, and two for Each Element contains preconditions which must be met interacting with items. Note that the “Walk from {} to {}“ in order for the element to be selected. These preconditions choice makes use of the logical predicates we have defined take a form of a list of (partially of fully instantiated) logic before, whereas the other two choices work only by match- terms, which are matched with the dynamic database. If a ing the terms available in the dynamic database. Simply given term unifies with a term in the database, this precon- augmenting the precondition/effects syntax with variables is dition is considered to be fulfilled. Optionally, a precondi- sufficient to achieve non-determinism and allow for a single tion can be negative, in the sense that it is only considered element to serve as a template for an entire class of interac- to be fulfilled if no term in the database unifies with it. In addition to these preconditions, which are dependent on the plating system, as well as enables a more “literate program- dynamic database, Elements can optionally also have direct ming“ style suited to non-technical content creators. logic statements embedded inside of them, which must also Both the logic terms embedded in the Action and Choice be satisfied in order for the Element to be selected. Elements as well as the standalone predicates are compiled Elements also contain effects which are the changes to the directly to high-level JavaScript code, which is intended to game state that occur as a result of a given Element being be human-readable. Using a method inspired by the Mer- executed. Like preconditions, the effects also take the form cury logic programming language, each predicate is com- of a list of logic terms. Each effect represents a term to ei- piled into a separate JavaScript function, where the non- ther be added or removed from the database. Certain kinds deterministic aspect is handled through continuation passing of terms can be marked as Unique in a specific section of (Henderson and Somogyi 2002). This one-predicate-one- the file, meaning that only one term with a specific top-level function correspondence allows for easier human compre- functor name can be held in the database at one time. This hension of the generated code, which is valuable when de- means that when a new such term is added, the previous one bugging or embedding the generated code within external is automatically removed. This is useful for types of state systems and frameworks. which should not contain duplicate values, such as player The non-deterministic capability is an important feature location, or a plot-tracking stat value. of the system in itself. Since each predicate can have a num- The execution of the selected Elements depends on their ber of valid “results“ (bindings of variables which are con- type. Actions are executed automatically as soon as they are sidered true), it is very easy to create a number of differ- selected, in order of their priority level. Choices are pre- ent available Choices or Actions working from the same sented to the player, and only the one that is chosen is exe- template, by introducing inside of it a variable with sev- cuted. In addition, Actions can be further divided into those eral possible valid bindings. For instance, when implement- that are selected and executed before and after the player ing a system where a player character can move freely be- makes a choice. tween rooms, instead of manually writing a Choice for each All Elements are grouped into Decks, with those not ex- connection, one can create a single Choice for walking be- plicitly marked with one being grouped into the default tween two locations designated with variables, and then cre- Deck. One Deck is active at the time, with content selection ate logic that ensures that this option is only available if these always happening within the Elements from the currently two variables represent two directly connected rooms. This active Deck. Each Element can specify a Deck that it “leads“ feature is similar to the “parameterized storylet“ concept in- to, and once that Element is executed, that Deck becomes ac- troduced by (Kreminski and Wardrip-Fruin 2018). tive. This is intended as an easier way of grouping together content, as well as a method of optimizing selection time in Related work more sophisticated interactive systems. While both interactive fiction tools making usage of logic In the current version of Wordgrind, Decks operate on a programming and those that allow for QBN exist, to our stack-based system - when a new Deck becomes active, it is knowledge those two concepts had not previously been used pushed onto the stack on top of the old one, and when an Ele- together in one authoring tool. Our design is partially influ- ment that does not lead to a new Deck is executed the active enced by the Dialog interactive fiction language (2018), es- Deck is popped from the stack and the previous Deck be- pecially with regards to the usage of natural language with comes active. This is intended to make Decks more reusable embedded logic terms and variables as a way of structuring and enable easier creation of nested menu structures. How- data (in contrast to the more traditional functor-like notation ever, as of writing, the details of this particular system are used by the Prolog family of logic programming languages). still being refined. Despite the fact that it is a rule specification language rather than an interactive narrative tool, Ceptre (Martens Logic programming aspects 2021) offers an approach to content selection similar to ours, with stages and rules being the equivalent of Decks and El- As previously mentioned, Wordgrind owes much of its ex- ements in Wordgrind. In place of preconditions and effects pressivity to the logic programming language that extends commonly seen in QBN systems, Ceptre instead uses linear throughout the whole tool. Logic variables and terms can be logic to describe changes to the game state happening due to substituted into any piece of textual content shown to the the application of its rules. player, which allows for simple text templating being inte- The exclusion logic used within the Praxis logic lan- grated directly into the language. guage, one of the components of the Versu storytelling sys- Along with each Action and Choice Elements being able tem (Evans and Short 2014), is used within that tool to solve to specify its own logic, Wordgrind file includes a section a similar problem as the Unique facts feature in Wordgrind, for predicates, allowing for code reusability. All predicates namely the necessity of repeatedly removing and adding a in Wordgrind, along with all the structured data terms used persistent stat property every time it needs to be changed. in the predicates and the dynamic database, are in the form Exclusion logic however allows for more sophisticated con- of natural language sentences with variables and data terms trol, as well as the ability to easily express tree data struc- “embedded“ inside of them. This solution, while only devi- tures. ating from the standard logic programming convention on While the concept of QBN has existed for over a decade, it the syntax level, provides a simpler foundation for the tem- wasn’t until recent years that a number of tools for creating QBN fiction have emerged, including SimpleQBN (2020), Martens, C. 2021. Ceptre: A language for modeling gener- StoryletManager (2021) and Tiny QBN (2019). A popular ative interactive systems. Proceedings of the AAAI Confer- data format for an existing interactive fiction tool Twine, ence on Artificial Intelligence and Interactive Digital Enter- Harlowe, had recently added support for a storylet-based tainment 11(1):51–57. selection mode as well (2021). Most of these tools so far 2020. Simpleqbn. https://github.com/videlais/simple-qbn. only support simple methods of content selection based on Accessed: 2021-08-16. integer-based state values and preconditions, or otherwise 2021. Storyletmanager. https://github.com/dmasad/ lack the ability to use structured data for game state. The StoryletManager. Accessed: 2021-08-16. main exception is SimpleQBN, which uses the MongoDB database query language that permits more advanced forms 2019. Tinyqbn. https://github.com/JoshuaGrams/tiny-qbn. of state and condition tracking. Of the mentioned systems, Accessed: 2021-08-16. only StoryletManager has support for parameterized sto- 2021. Twine harlowe. https://twine2.neocities.org/#macro rylets, a feature which emerges in Wordgrind naturally as a storylet. Accessed: 2021-08-16. direct result of augmenting content with logic variables and terms. Conclusions & Future Work In this paper we introduced Wordgrind, a tool for authoring storylet-based interactive fiction based on logic program- ming, and demonstrated both its content selection mecha- nism and how it uses logic to augment its functionality. While our tool in its current implementation has suffi- cient functionality to demonstrate the underlying concepts, more work will be needed in the future to ensure both tech- nical stability and user convenience sufficient for a public release. In particular, the question of syntax had only been mentioned in this paper in broad strokes, as it is one of the aspects of the tool that will require heavy user feedback and iteration. The current implementation uses a simple YAML file as a document format, however it is most likely that a custom parser will be used at some point in the future. The current logic programming system, while complete enough for most purposes, still lacks some features that we hope to make it into the final version. In particular, logical negation and if/else statements are still to be implemented, as well as syntax and library functions for dealing with list data structures. In the farther future, we also hope to im- plement a mode-based compilation system, similar to the one used by the Mercury programming language (Hender- son and Somogyi 2002), which would allow for better gen- erated code performance. References 2018. Dialog. http://www.linusakesson.net/dialog/index. php. Accessed: 2021-08-16. Evans, R., and Short, E. 2014. Versu—a simulationist sto- rytelling system. IEEE Transactions on Computational In- telligence and AI in Games 6(2):113–130. Henderson, F., and Somogyi, Z. 2002. Compiling mer- cury to high-level c code. In Proceedings of the 2002 Inter- national Conference on Compiler Construction. Grenoble, France: Springer. 2006. Inform 7. http://inform7.com. Accessed: 2021-09-27. Kreminski, M., and Wardrip-Fruin, N. 2018. Sketching a map of the storylets design space. In ICIDS 2018: Interac- tive Storytelling, 160–164. Dublin, Ireland: Springer.