[โ€“] 4 points 2 days ago

My game loop is single-threaded. I figure with small 2D maps and optimized native code, I should never need multi-threading. Plus, I target wasm, so I can't depend on parallelism for performance.

Ignoring UI and menu modes, there are two cases in the game loop:

  1. An animation is running: tick it
  2. Else: do regular state updates

Animation frames are represented as a simple list of (point, glyph) deltas to apply to the map. We render the map as normal, and then render the deltas as an overlay. Even there, there are a few wrinkles. I test each of the deltas for visibility before rendering them. If all of an animation frame's deltas are skipped, then we skip to the next frame during that rendering. In fact, if none of an animation's frames are visible, it won't any time at all, and we'll fall through to case 2. This check is actually relevant to gameplay! It means that players have no way of telling when an animated event occurs unless they can see (at least a portion) of the animation.

Another wrinkle in the animation system is that there may be callbacks that update game state queued between two frames. Adding in these callbacks was essential. For instance, for a fire attack, a callback might light a tile on fire only when the flame moves through it. For all future frames, that cell will be lit in the underlying map.

Compared to animations, case 2 - regular state updates - are trivial! I run the following logic:

  • Find the actor with the smallest time-to-next-turn
  • Advance time to that turn
  • Run plan() to choose an action, and act() to apply it
  • If the actor is the player, plan() can return "blockedOnInput" - that's when we break!
  • Else, repeat

Easy!

  • source
  • [โ€“] 3 points 1 week ago

    I just use vim + cargo for development. However, for WildsRL, I've built two project-specific tools that have been essential for debugging.

    The first is time-travel debugging. When debug is enabled, every entity dumps detailed debug for each turn they take. It includes their current field of view, their knowledge of tiles and entities nearby, any Dijkstra maps they computed, and (since I'm using behavior trees) the latest (Success, Failed, Running, Unused) state for each node in the tree. I have a web UI that can display this debug information, so if I see anything unusual in testing, I can review recent gameplay from any entity's perspective to see exactly why it did what it did.

    The second is autoplay. There are enough interactions between NPC entities that a lot of my code is exercised by just running a small scene (100x100 map, 25 entities) for a few thousand turns. I have unit tests that check for crashes using this mode. However, there aren't that many crashes - one of the perks of writing in Rust. Arguably more important is that I can enable debug for the autoplay view and examine NPC behavior over many turns. I have another suite of tests that does that, and for all changes, I review any history diffs to check that the changes are what I intended.

  • source