Background Simulation Devlog
Howdy again! Feel free to Join the Discord! to follow the dev more closely. If you know anyone who likes to read technical game dev blog posts like these, feel free to shoot them a link, I'd appreciate it.
Patch #138!
I just wanted to take a moment to celebrate the 138th Steam Upload to the game since its inception.
It's really surreal to see this number slowly tick up day by day. It will still take thousands of updates yet, but the game is slowly taking shape.
"The Background Sim"
Over the last two weeks or so, I've done a mountain of work to move my logic out of Unreals standard framework, and into a "ECS" centric framework, with FLECS.
One of the primary goals of Peasant Simulator is to let players live out their medieval lives in a world full of "things". A "Thing" can be really anything interactable. A tree, a bush, an apple that you eat, a bloomery currently smelting iron, a corpse decaying in a forest, an item inside an inventory, etc. The technical term for a "thing" is an Entity.
The Systems (the S part of ECS) that govern how these Entities interact with each other, I'm loosely calling "The Background Simulation". This background simulation will determine how fast food rots, where wolves go to hunt, how flammable things are and how fire progresses through them, how fast crops grow, etc.
Everything with a Blue Box around it, exists inside of the background Sim.
Everything in the game world is comprised of Components (Entity+ Component + System) which store their data, and are manipulated inside of this Background Sim, then exposed to the larger gameplay framework of Unreal for rendering and networking.
With FLECS handling it, I can fully utilize most of the CPU's horsepower.
A modern CPU is fast. Like, really fast.
Without getting too technical, an ECS framework is perfect for laying out your game worlds worth of data in a way that is easily digestible for a modern CPU to absolutely crunch through, utilizing all of its cores, and all of its speed.
This is a bit of digression, but 4.5 Ghz on 12+ cores with 32 gig of main memory is an *astronomic* amount of speed and resources that modern machines have access to - and its an absolute crime that we're still yet to standardize dev practises that fully utilize these resources.
ECS is one way to unlock this horse power, BUT ...
Its a very "different" way of making a game compared to the status quo. In exchange for this differentness, you gain SPEED.
By breaking the things in the game world apart into their constituent pieces, the CPU is able to process each piece with greater efficiency, for example:
A Fish isnt a fish - its an Npc{} with Age{Value}, WaterNavigating{} and DropsOnDeath{FishMeat}.
A Crop isn't a crop, its a WorldStatic{} with Age{Value}, CurrentWetness{Value} and Harvestable{RequiredAge}
Crops growing, and drying out.
The Background Sim then looks for all things with Age, and increases that value. All things with Wetness, and decreases that value. Very simple operations applied to many thousands of things, very efficiently. Once a Harvestable{} whose Age{} is greater than their RequiredAge is damaged, it drops fully grown food into the game world, and removes itself from the sim.
With this framework I can now setup the game world so that every fish in every river is slowly maturing, every item left about can slowly decay, trees and crops can grow by the thousands, and each building you place can maintain health, repairs, etc.
Usually you need to fake this sort of stuff, and use smoke and mirrors to hide the exchange while the players not looking - with ECS you get your cake and can eat it too.
Current Simulated attributes.
Right now, there are a few simulated attributes, and there will be more to come.
- Crops progress their growth, up to a maximum value.
- Things that are "wet" (crops) dry out slowly, and in the case of crops, will actually die if their "wetness value" ever becomes zero.
- Buildings maintain an internal health value, and can receive damage. When this value hits zero they drop building materials into the world which can be gathered.
- WorldItems ALSO maintain this same internal internal health, but use it to "change" into other items when destroyed. Trees Change into a log + some sticks, rocks change into chipped rock, etc.
This is a relatively small list at the moment, but the framework is there. I'm still tweaking it and testing weird edge cases, playing devils advocate to myself, and looking for flaws. But it's looking pretty good so far.
Here is what some example code looks like, for handling "death" of things in the Simulation.
Firstly, we apply damage to the Entity:
Then, for anyone that died as a result of that operation, who also drops items on death, drop the items (Add Static Item To World):
Querying the Background sim
Players need to know where stuff is. Wolves need to know where the closest deer is. NPCs need to know where the nearest Log is, so they can chop it to wood, etc. To do this efficiently, I've implemented an R-Tree acceleration structure to make this quick and efficient. I didn't write it myself, that's quite above my skill level, but thankfully there are some good R-Trees available for use online, which I've repurposed.
A player querying the game world for the closest three "Things" - you can see the green lines connecting their "face" to the "things" in the game world.
Knowing what's "close" to a location is a difficult problem, but thankfully that is now solved for Peasant Simulator. Now things like campfires will be able to set things on fire that sit too close to them, animals will be able to hunt, and perhaps even players will be able to "track" entities around the game world if their tracking skill is high enough. I'm yet to write these features, but it's looking good.
R-Tree's also have an interesting property in that they can be streamed on and off the disk, so there are potentially streaming related optimizations to be had there, but I'm not looking into that just yet.
Communicating all this to the player.
This is the next technical roadblock I need to solve. It's a multiplayer game! Which means that the connected clients need to be able to look at food on the floor, and see that it is 20% cooked, or 80% rotten. This is my next feature I will be working on.
Thanks for Reading!
That's all for now, thanks for reading if you got this far! Again feel free to Join the Discord! and say hi, pitch ideas and features, or just lurk and follow along. And again,if you know anyone who likes to read this sort of technical content, feel free to forward it their way. See you in the next one!
