6 ms·
Game Programming in Prolog
- Philpax 2y agoThe centered monospace text is quite hard to read on mobile, but reader mode seems to work well. Back to reading it now...
- Philpax 2y agoI haven't quite finished reading this yet, but it's really quite interesting! Seeing how entity "components", interactions and relations come for free as a result of logic programming is fascinating. That being said, it seems to get quite convoluted when trying to introduce dynamism (i.e. time) to the system. I was about to wonder if a hybrid of functional and logic programming could address this, and then I remembered Verse [0] exists ;-) [0]: https://github.com/UnrealVerseGuru/VerseProgrammingLanguage https://github.com/UnrealVerseGuru/VerseProgrammingLanguage
- runevault 2y agoI'm so curious to see how Verse ends up, since last I knew it was not even 1.0 yet but have not kept up in a while. But I recall being interested in the set of design decisions SPJ
- deleted 2y ago[deleted]
- dang 2y agoNormally 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 https://thingspool.net/morsels/page-11.html (part 2) https://thingspool.net/morsels/page-12.html https://thingspool.net/morsels/page-12.html (part 3) https://thingspool.net/morsels/page-13.html https://thingspool.net/morsels/page-13.html (part 4) https://thingspool.net/morsels/page-14.html https://thingspool.net/morsels/page-14.html (part 5) (and keeps going!)
- keyle 2y agoyou should consider the opposite to push the author's website and force them to continue ;)
- dang 2y agoWe optimize for reader satisfaction, not author satisfaction—but that's probably in the author's best interest too!
- sitkack 2y ago(Will be continued in Part 13) It will take me to read this book, but I love it! I'll be following along in https://github.com/mthom/scryer-prolog https://github.com/mthom/scryer-prolog (written in Rust) so one should be able to take these techniques into Rust game programming.
- febin 2y agoAssume you are the author, I found your website is such a treasure. I couldn't believe your website wasn't listed on HN before. Next time please post on HN when you write.
- sitkack 2y agoI am not, just a huge fan of prolog and works of passion like this.
- marttt 2y ago+1, this entire website is a fascinating find. Interestingly, in terms of formatting, all the long-form texts on the site are centered (as opposed to left align). I wonder what's the logic behind this decision (surprisingly, it is not at all annoying to read, though).
- Pet_Ant 2y agoMaybe change the link to the index?: https://thingspool.net/morsels/list.html https://thingspool.net/morsels/list.html
- haolez 2y agoIn 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 https://a.co/d/7wVOcZp
- Jtsummers 2y agoI was going to mention that one as well. I was exploring using CLIPS (or something like it) for a system at work and after digging through the free documentation I picked it up. It was an enjoyable read. https://www.clipsrules.net/ https://www.clipsrules.net/
- veqq 2y agohttps://ryjo.codes/ https://ryjo.codes/ has a lot of good material on CLIPS.
- runevault 2y agoHuh that sounds fascinating. Any idea if the kindle version is any good? Most programming books are awful on kindle (though I do have a Fire tablet which it might be fine for).
- deleted 2y ago[deleted]
- haolez 2y agoI read it on Kindle. It was good enough. The code formatting was normal.
- stephen_cagle 2y agoNote, I read it on kindle as well, and like many kindle editions sometimes the '-' character is replaced by some weird thing that isn't a dash. I had a hell of a time figuring out what was going on when I copy and pasted code from the book... :]
- sterlind 2y agoI'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?
- xelxebar 2y agoNaïve question, but doesn't amount of brute-force search depend on the particular solver used? I'm vaguely aware of finite domain something or other but curious about the affordances prolog has for using different solvers over different parts of your codebase. At first guess, I would imagine this could look like writing down known constraints on the solution as a kind of "type declaration".
- sterlind 2y agoProlog's clpfd (constrained logic programming over finite domains) is actually just a library - albeit one with very ergonomic bindings that mesh almost invisibly into the language. Prolog's evaluator backtracks when it hits a conflict while trying to expand a predicate. It evaluates queries top-down, and tries predicate definitions/clauses in order. It's basically eager. This isn't a good fit for math, so CLPFD fixed this by making arithmetic constraints lazy. This works really well, but you're ultimately using Prolog to build a model, then (invisibly) solving the model separately. Ultimately, the problem is that Prolog looks like a DSL but is Turing-complete. You can weave other solvers into the language, but there's a seam between the evaluators. I like the alternative, which is to sacrifice Prolog's Turing-completeness for a ridiculously powerful DSL. Answer Set Programming (clasp) and Datalog are fine examples of this.
- simplify 2y agoBrute force search is just the naive way of finding solutions. Just like you can index a SQL table, you can index a Prolog predicate for better performance.
- 2y ago
- klaussilveira 2y agoThis 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!
- klaussilveira 2y agoI'll leave this here since it is useful: https://github.com/emacstheviking/gnuprolog-libsdl2 https://github.com/emacstheviking/gnuprolog-libsdl2
- kelseyfrog 2y agoThere'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-system.pdf https://www.gamedevs.org/uploads/data-driven-game-object-sys...
- kasbah 2y agoTo me the interesting thing is the logic programming "rules" and their overlap with game rules. Inspired by work at Stanford on the logic programming based Game Description Language [1] I implemented Tic Tac Toe in Datascript yesterday: https://github.com/kasbah/datascript-games/blob/e06a37025bf921ed86935d40a0c882c8d053ad7b/tic-tac-toe.mjs https://github.com/kasbah/datascript-games/blob/e06a37025bf9... I am still not clear whether there isn't a more succinct rule definition than what I have there. In the Stanford paper you have rules like: (<= (column ?n ?x) (true (cell 1 ?n ?x)) (true (cell 2 ?n ?x)) (true (cell 3 ?n ?x))) But in Datascript I have to do much more rigmarole around shuffling the data around: [(column ?n ?x) [?current "ident" "current"] [?coord0 "type" "coord"] [?coord1 "type" "coord"] [?coord2 "type" "coord"] [?coord0 "m" 0] [?coord1 "m" 1] [?coord2 "m" 2] [?coord0 "n" ?n] [?coord1 "n" ?n] [?coord2 "n" ?n] [?coord0 "name" ?key0] [?current ?key0 ?x] [?coord1 "name" ?key1] [?current ?key1 ?x] [?coord2 "name" ?key2] [?current ?key2 ?x]] I don't know if this is down to Datascript/Datomic Datalog limitations or more the limitations of my understanding. How do you approach your experiments? If you have any of your work to share or some tips on what I am doing I'd be very interested. [1]: https://www.cs.uic.edu/~hinrichs/papers/love2006general.pdf https://www.cs.uic.edu/~hinrichs/papers/love2006general.pdf
- refset 2y agoThis is a consequence of the Datomic information model which is focused on handling a single universe of triples (to facilitate natural schema growth) instead of independent n-ary relations. However unlike when building serious business applications, for most simple games you probably won't ever care about the ease of handling the schema growth of persisted data. You would likely care more once you think about supporting long-lived multiplayer environments or introducing played-defined concepts within the game.
- JoeDaDude 2y agoWhen 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 http://logicmazes.com/bureau/index.htm
- Avshalom 2y agohttps://amzi.com/AdventureInProlog/ https://amzi.com/AdventureInProlog/ teaches prolog the same way.
- anthk 2y agoWith Common Lisp you literally write one in Land of Lisp as a beginners' exercise. But with Inform6, the OOP applied to game design and literal ingame objects' relation (as the ZMachine) states it's when the difficulty plummets down to the point to be trivial.
- discarded1023 2y ago@JoeDaDude -- on macOS using recent Chrome/FireFox the puzzles linked from the top-level page yield terminal JavaScript errors. It'd be great to see them in action. http://logicmazes.com/alice.html http://logicmazes.com/alice.html alice.html:353 Uncaught TypeError: Cannot read properties of undefined (reading 'play') at playSound (alice.html:353:30) at finalize2 (alice.html:347:1) at <anonymous>:1:1
- brandonpollack2 2y agoInteresting note: I believe the scripting language that larian (creators of the highly dynamic divinity titles and baldurs gate 3) is a prolog variant.
- chvrchbvrner 2y agoIt's called Osiris and they state it "is a mostly declarative programming language, similar to Prolog" [1] I think you get access to their engine if you buy one of their games through steam and you can mess with Osiris (not sure if that's still true for BG3, but it was the case for Divinity: Original Sin). [1] https://docs.larian.game/Osiris_Overview https://docs.larian.game/Osiris_Overview
- talideon 2y agoI believe you have Osiris access at least since Patch 7, when they opened things up to modders.
- Apocryphon 2y agoNow, to put this on a cartridge for the Prolog-powered Sega AI: https://news.ycombinator.com/item?id=39206529 https://news.ycombinator.com/item?id=39206529
- tannhaeuser 2y agoThis 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.
- bubblyworld 2y agoI'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?
- fire_lake 2y ago[flagged]
- deleted 2y ago[deleted]
- ralferoo 2y agoI 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.
- greener_grass 2y agoIs 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.
- ssrc 2y agoThe most similar thing to a "static" Prolog would be Mercury[0] or Turbo Prolog[1]. OTOH, if you want an embed-able logic programming library there is the mini/microKanren family[2]. [0] https://en.wikipedia.org/wiki/Mercury_(programming_language) https://en.wikipedia.org/wiki/Mercury_(programming_language) [1] https://en.wikipedia.org/wiki/Visual_Prolog https://en.wikipedia.org/wiki/Visual_Prolog [2] https://en.wikipedia.org/wiki/MiniKanren https://en.wikipedia.org/wiki/MiniKanren
- cmrdporcupine 2y agoSo much of the "magic" with Prolog and similar relational languages necessarily happens with dynamic data at runtime (especially in a game engine where entities move around, etc so the state of relations changes significantly) I'm not sure how much static you could get with it. That said, there's plenty of research in the DB world on precompiled and JITted query execution plans. All of that would apply to a Prolog or Datalog engine as well. Also, a personal interest of mine I keep wanting to come back to is how much one can make use of GPU and TPU hardware to accelerate the joins that go on in these systems. There are people working in this space already: https://arxiv.org/html/2311.02206v3 https://arxiv.org/html/2311.02206v3 but I believe their emphasis is on large scale data sets (e.g. big data graphs) whereas I'm curious to see if there's a way that an approach using HW acceleration could make low latency "OLTP" type applications possible for applications like robotics/vehicle autonomy/games, using complex rule sets. I watch my son play Dwarf Fortress and I'm like... it needs a Prolog.
- znpy 2y agoA 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?
- upghost 2y agoThere's very little that's modern and most modern resources teach you bad habits and non-idiomatic code. For good habits and a good introduction: https://www.metalevel.at/prolog https://www.metalevel.at/prolog I like "Prolog Programming for Artificial Intelligence" as a great print book with interesting projects that is fairly modern but be warned that much of the code is not idiomatic, so please refer to the first resource if you are looking to write too quality stuff. https://a.co/d/5NGr6KS https://a.co/d/5NGr6KS
- rustman123 2y agoI 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?
- upghost 2y agothat is prolog pseudocode. You would probably use the lists:nth/3 predicate. https://www.scryer.pl/lists https://www.scryer.pl/lists Markus Triska has probably the best resources on modern Prolog. Just search for "Power of Prolog" on YouTube or the internets.
- sevensor 2y agoFrom 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.
- iamwil 2y agoThe 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 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".
- itishappy 2y agoNoita is a game based around the concept of a chemistry engine running at the pixel level. It uses a custom rules engine written in C++. It's one of the more interesting games I have ever played. https://store.steampowered.com/app/881100/Noita/ https://store.steampowered.com/app/881100/Noita/ https://www.youtube.com/watch?v=prXuyMCgbTc https://www.youtube.com/watch?v=prXuyMCgbTc
- nffaria 2y agoNoita is an amazingly complex game, from the alchemy reactions, to the cryptographic secrets [1], to the wand building mechanic -- which is itself like a small programming language. Here is someone using a (really complex) wand to beat the game in 2 seconds (some spoilers): https://youtu.be/YYTB5_zBANg?feature=shared&t=309 https://youtu.be/YYTB5_zBANg?feature=shared&t=309 [1] https://noita.wiki.gg/wiki/Eye_Messages https://noita.wiki.gg/wiki/Eye_Messages
- beretguy 2y agoI’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.
- HexDecOctBin 2y agoIs there a embeddable Prolog implementation (à la Lua) that can be linked in to an application as a scripting language?
- Jtsummers 2y agohttps://www.swi-prolog.org/pldoc/man?section=embedded https://www.swi-prolog.org/pldoc/man?section=embedded - I've never used this myself so can't comment on how good it is. There are probably others, too. http://minikanren.org/ http://minikanren.org/ - MiniKanren, a different language in the same vein, small and embedded into lots of host languages. https://microsoft.github.io/z3guide/docs/logic/intro/ https://microsoft.github.io/z3guide/docs/logic/intro/ - Z3, again a different language but hits many of the same areas and use cases along with plenty of others. Also usable as a library (probably how most people access it, often through Python).
- HexDecOctBin 2y agoThanks, MiniKanren implemented in C and Lua seems like the right way. SWI Prolog seems to use global state, which becomes no-no without using GILs and whatnot. Z3 I've use before in college, but it wasn't really a scripting tool, unless something changed in the meantime.
- deleted 2y ago[deleted]