Automating Interactive Fiction Logic Generation with LLMs in Emacs

summary: I automated the creation of transaction tracking logic for an Emacs-based interactive fiction book using an LLM (via gptel).

I have been writing an interactive children's book in Emacs where the protagonist Daphne earns, saves or spends money during her weeklong entrepreneurial adventure. The passages in my book are organized as follows:

Layout of my interactive fiction book

Since a reader may arrive at a passage from multiple routes, every passage includes some code to track transaction amounts, amongst other parameters. Using this code, I'm able to show the reader Daphne's cash balance at any point in the book.

screenshot of piggy bank showing cash balance

After writing 34 passages, I realized I could enhance the book's educational value by showing how Daphne's cash balance was calculated. This new feature would require modifying the code in every passage to include the context of the transactions. Hard to imagine now, but there was a time when implementing the feature would require tediously editing the code in every passage. To explore a more efficient solution, I turned to an LLM via the gptel package for Emacs.

Here's how I added the context of the transactions to every passage: I select the entire story, which is a single org file in Emacs, with C-x h, invoke gptel-rewrite with C-u C-c r RET d, then type out the following descriptive prompt:

The selection is a series of passages, each passage falls under an org heading. each passage has a source code block. update the source code blocks to include a json object, cashOperations, that tracks the change in cash in the corresponding passage. The json object contains the following keys: "operation", "amount" and "description". "operation" can be "add" or "subtract", "amount" is the change in cash amount (positive value), and description is a string of 3-5 words that describes the reason for the change in cash. Only append the cashOperations object if there has been an event or action to change the cash amount even if the net change is zero.

I follow up with RET r and wait.

I now have the context to implement an arithmetic explainer option.

screenshot of the piggy bank with the arithmetic explainer enabled