October 10th, 2024

Game Programming in Prolog

The author explores game programming with Prolog, highlighting its logic-based approach, declarative nature, and potential for robust gameplay systems through actors defined by tags and relationships. Future parts will cover dynamic changes.

Read original articleLink Icon
Game Programming in Prolog

The article discusses the author's exploration of game programming using Prolog, a logic programming language distinct from conventional object-oriented languages like C# and Java. The author reflects on their journey of learning Prolog, noting its unique syntax and the challenges it presents compared to more familiar languages. They highlight the potential of Prolog in developing complex systems, particularly in game mechanics, by utilizing logical relations as foundational elements. The author proposes that core gameplay mechanics can be effectively implemented in Prolog, emphasizing the benefits of a declarative approach that enhances robustness and modularity. The article outlines the structure of a game world consisting of actors, which are entities that interact within the game. It introduces the concept of tags to define actor characteristics and relationships, using Prolog predicates to establish these connections. The author illustrates how to assign tags and define relationships between actors, such as the ability of a human actor to eat bread or a fungus to spoil decomposable items. The article concludes by indicating that future parts will address how to incorporate dynamic changes in the game, moving beyond static definitions.

- The author explores game programming using Prolog, emphasizing its logic-based approach.

- Prolog's declarative nature is seen as beneficial for developing complex game mechanics.

- Actors in the game world are defined with unique tags and relationships using Prolog predicates.

- The article highlights the potential for modular and robust gameplay systems through logic programming.

- Future installments will address dynamic interactions and changes within the game.

Link Icon 23 comments
By @dang - 6 months
Normally we downweight posts that just have a Part 1 and then stop, but this one actually delivers the goods:

https://thingspool.net/morsels/page-11.html (part 2)

https://thingspool.net/morsels/page-12.html (part 3)

https://thingspool.net/morsels/page-13.html (part 4)

https://thingspool.net/morsels/page-14.html (part 5)

(and keeps going!)

By @iamwil - 6 months
The Breath of the Wild game design has a concept called the "Chemistry Engine". In that game engines usually have a physics engine to figure out how things interact in a motion sense. The chemistry engine figures out how materials interact in an alchemy sense. It's kinda like a rules-based engine to figure out how different things interact with different other things, so you get surprising interactions between everything in the world, like being able to light arrows on fire, because arrows are a "wood" material.

The Youtube link is here: https://www.youtube.com/watch?v=QyMsF31NdNc&t=2354s

It seems like most rule-based stuff in games is just hand-coded, as it's relatively simple and doesn't need to be general. When I asked the author of Baba is You, if he implemented a datalog engine for it, he said 'no'. I suspect it's the same for Breath of the Wild.

But still, I've often wondered if that sort of chemistry engine would be best implemented in a logic language like Prolog or Datalog, for fast experimentation. Just like how we use SQL to keep the flexibility of our queries, and we end up just shipping that. I'm sure, back in the day, lots of people lamented how slow SQL queries were. The flexibility was useful enough that we ended up pouring man-centuries into making them fast. Now, we think that's just the way you ship things, and (almost) never think, "I can hand-roll imperative code that will be faster than this query".

By @tannhaeuser - 6 months
This is an interesting take on Prolog game programming in that it's going straight to "action games" with a realtime, timeline, 3D, ECS, and event aspect. When most introductory texts on Prolog game development start with adventure games, in particular classic text adventures, since those build directly on Prolog constructs such as facts and rules for eg. mazes and inventory puzzles, and also DSLs. Or card and board games, the rules of which can be expressed so conveniently using Prolog, and can then almost trivially be extended into basic combinatorical general-purpose game opponents, not dissimilar to Prolog planners in robotics, logistics, finance, industry, etc.
By @JoeDaDude - 6 months
When I took an AI class, the first thing they taught was Prolog and for exercises we all had to write adventure/colossal cave style games in it. Prolog turned out be well suited to the task. The variety of simple games made in the class was astounding and I wish I could have collected the all the games made by the other students. We only spent a couple of weeks before moving on to other topics, like CLIPS and Lisp. In my own assignment, I made a Bureaucratic Maze [1], again, fairly straightforward in Prolog.

[1]. http://logicmazes.com/bureau/index.htm

By @ralferoo - 6 months
I skimmed through all 12 posts, and while it seems like it might be a good introduction to using Prolog, it seems a stretch to claim this has anything to do with game programming. Maybe that will develop later, but so far after 12 lessons, it's been mostly concerned with trying to model a few OOP concepts in Prolog.

Not sure if I'm missing anything, but nothing yet has been concerned with any kind of user interaction which would seem to be a pre-requisite for a game, although there was a brief discussion about sending messages so maybe that's what he was intending.

By @haolez - 6 months
In the same vein, I had a lot of fun reading a game programming book with logic programming, but instead of Prolog it used CLIPS:

Adventures in Rule-Based Programming: A CLIPS Tutorial https://a.co/d/7wVOcZp

By @kelseyfrog - 6 months
There's an interesting (to me at least) overlap between gamestate as a set of facts and relations(Prolog), and the point of ECS("This is a database"[1]).

I personally experiment in Datascript as a gamestate db, but it's still quite an early attempt to conclude if it's a success. It's great seeing how ideas in this tutorial map 1-to-1 to that idea.

1. https://www.gamedevs.org/uploads/data-driven-game-object-sys...

By @klaussilveira - 6 months
This is such a breath of fresh air on tackling the state machine issue in games with heavy logic (city simulation, for instance). I never thought about using Prolog for this.

Bravo!

By @sevensor - 6 months
From the causality and relativity discussion, it seems like you could do some really neat stuff. Like for instance, you could generate a random encounter with an NPC, and a history of events for that NPC that includes causal chains initiated by the player in the past. Because everything is relations, and you know that the NPC is present now, and we have a history of all observed world state and actions, we can work backwards to get an entirely consistent history for the NPC without having simulated it in advance.
By @sterlind - 6 months
I'd never considered how the commonplace actor-world/entity-trait model is a neat fit for Prolog's whole relational deal. Though predictable and efficient run-time is also critical, and Prolog typically brute-forces its way through matching terms to satisfy the query. I haven't finished reading the series though - maybe they address it?
By @rustman123 - 6 months
I admit to not having that much experience in prolog, but I'm having a hard time translating the time parameter `[n]` into executable prolog. Anyone got a clue?
By @brandonpollack2 - 6 months
Interesting note: I believe the scripting language that larian (creators of the highly dynamic divinity titles and baldurs gate 3) is a prolog variant.
By @bubblyworld - 6 months
I'm thoroughly enjoying the philosophy side of these posts (like the how of representing various common abstractions in prolog), but I would love to see an actual game implemented in prolog! Does anyone know of any examples that are more complex than simple text adventures?

Or any anecdotes from more experienced prologgers about the experience of writing a real game? At face value it looks like it would be very easy to accidentally tie yourself up in a knot of logical contradictions, but I guess there are tools in prolog land for debugging this kind of thing?

By @znpy - 6 months
A bit of OT, but if one would like to pick up a bit of Prolog, what's the recommended book?

And what prolog implementation should one pick?

By @HexDecOctBin - 6 months
Is there a embeddable Prolog implementation (à la Lua) that can be linked in to an application as a scripting language?
By @klaussilveira - 6 months
I'll leave this here since it is useful: https://github.com/emacstheviking/gnuprolog-libsdl2
By @Philpax - 6 months
The centered monospace text is quite hard to read on mobile, but reader mode seems to work well. Back to reading it now...
By @beretguy - 6 months
I’m not too dumb but my brain is not wired sufficiently enough to be willing to try to understand what’s happening. I can only understand it on a high level, but not I’m not going down this rabbit hole.
By @greener_grass - 6 months
Is there a variant of Prolog that is less dynamic with AoT compilation?

Imagine if you can be in "dynamic mode" where you can add rules, facts, etc. in an adhoc way.

Then, once your game is better defined, you can compile the Prolog to native code so that it is highly optimized, executed in parallel, etc. The compiler could apply all sorts of optimizations we see in commercial game engines automatically.

This compiled object could then be embedded into a game engine with native performance.

Done right, this could even outperform imperative game engines, but be very easy to debug and modify.

By @Apocryphon - 6 months
Now, to put this on a cartridge for the Prolog-powered Sega AI:

https://news.ycombinator.com/item?id=39206529