Refactoring BodyPlan

I’m don’t have too much to say about my multithreading work just yet (aside from “it’s coming along, and early results are promising”), so instead let’s talk about a long-overdue task I undertook at the start of work for 0.12.0. It’s not directly related to multithreading, but it’s related to things that are related to multithreading, so it’ll provide a foundation for future blog posts.

Creature in Species are built out of a series of sub-objects, the biggest of which are the following five:

Creature
{
    Genome //Contains Genetic Stats
    BodyPlan //Contains Skeletal Physics and Drawable Body Parts
    Phenotype //Contains Derived-At-Birth Stats (things like speed, attack damage, etc)
    Specimen //Contains always-up-to-date State Information (Health/Food/Etc)
    Brain //Contains the creatures AI State
}

One of these things is not like the others, one of these things just doesn’t belong…

… and that thing is BodyPlan. I mean, obviously. It’s right there in the title of the blog post. I’m not exactly being coy about it here.

BodyPlan handles two responsibilities: generating and applying the skeletal physics, and drawing the creature’s body parts to the screen. The reason it was originally built this way is because these things are superficially related: I take the skeleton shape from the drawable art assets, and the art assets are rotated by droop and balance effects when I draw the creature.

But let’s embarrass ourselves trying to justify it. This is bad. This is really bad. To be gratuitously technical for a moment: under the SOLID principles of Object Oriented Programming, this is a severe violation of the first rule: the Single Responsibility Principle.

The Single Responsibility Principle dictates… well, exactly what you’d expect it to dictate based on that name. A class should have one responsibility. BodyPlan has two. Ergo propter hoc, Quasar is a dumbass.

This is important because those rules exist for reason: by violating the SRP, I’ve coupled drawing and animating limbs, heads and other body parts to the skeletal physics. Let’s say I wanted to draw heads or tails separate from a creature, as part of a creature editor or mutation map viewer: I can’t do that. At least, not without also creating some sort of temporary skeletal data for them.

The same also applies in reverse: I need to provide drawable assets like the body cover texture and colour to make the skeleton. This is less of an issue because there’s not too many places where I’m going to want a disembodied skeleton walking around (they fall apart too easily to use as soldiers and they’re too dumb to be servants), but still. It’s the principle of the thing. I might not want disembodied skeletons, but I’d object if someone tried to take away my right to have disembodied skeletons.

So this needed fixing and since I was already waist deep in refactoring everything else for multithreading, I figured I might as well address it. I’ve been splitting “BodyPlan” into “DrawableBodyPlan” and “SkeletalBodyPlan” (which I’ll probably rename simply to “Body” and “Skeleton” for the sake of being concise).

This is the only change I’ve made for 0.12.0 not directly related to multithreading: everything else from here on out will be in service of smearing the game across multiple cores like some sort of weird inedible Vegemite.

Cheers,
Quasar



My plan was to make a joke about Vegemite, but I’m like 70% sure that would be grounds for having my Australian citizenship revoked.

Advertisements

4 Comments

Where to Next – Multithreading 0.12.0

Updates will be slowing down for a while as I work on multithreading for 0.12.0. This is a big and promising feature, but also a dangerous one. It’s kind of like summoning an eldritch god: you might get immortality, or your life force might be consumed to power a terrible murder portal through which demons can invade the earth.

(it’s important to make your analogies relatable)

The number one complaint on Steam is performance. And I get it. Species gets more interesting with more creatures. If you’ve got a supercomputer with a trillion cores that cost you as much as the first three Apollo missions, you’re probably going to resent it if it can’t run Species better than a machine somebody got in a garage sale for 5 bucks and may actually be a microwave. Unfortunately, there’s a good possibility it won’t because CPU core speed hasn’t significantly increased in the last decade.

Performance is not the reason I’m looking at multithreading, however.

This is how the game is currently built, using a bit of sample data taken from a quick P. specium simulation:

iBOlH1i

As you can see, each frame time is given primarily to creature AI, specifically to the method called “UpdateBehaviourals” (that big red chunk of CPU time in the middle of each frame).

Logically, then, that’s the method I should extract and put into another thread, so it can run in parallel to the other operations.

mOcjZmQ

This would be the “simple” (not actually simple) way of implementing multithreading.

And to be fair, a potential 30% performance improvement (assuming ideal conditions) isn’t to be sneezed at. But to be frank, it’s not enough to justify how much development time and risk multithreading the update call introduces.

But most modern CPU’s have more than two cores, and there’s still a big chunk of time on the main thread going unused (that blue block in the picture labelled “await”), which means we’re not done. What if we could *split* the “UpdateBehaviourals” method across multiple cores, by updating some of the creatures on one, some on another, and so on?

NXJ2ELH

That’s a lot better: a whopping 70% improvement! (assuming ideal conditions and that this sample data is representative).

But even more importantly, from my perspective at least, the blue ‘await’ blocks are now on the other threads.We have spare CPU cycles, not on the main thread, but on the threads doing the AI.

This is the real reason I want multithreading in 0.12.0. I still have features I want to add, specifically to Creature AI.Creatures need to be able to rationally decide what movement method to use to reach a target, which requires increased situational awareness.Predators need to be able to find food from further afield, so they can travel to more habitable areas from across the map rather than just dying off once all the prey in their area are gone.Social creatures need to communicate with one another and factor those communications into their decision-making process. And each of those features will have an associated CPU cost, which I simply can’t afford at the moment.

But if I can properly parallelize the creatures “UpdateBehaviourals” routine…that is, not just offload it to another core, but offload portions of it onto all the other cores… then I’ll get a significant amount of spare CPU cycles to work with, and I’ll be able to implement those features without worrying about them impacting performance at all!

Of course, this is even harder to implement than the aforementioned “simple” option, which itself is nothing of the sort. But I’ll have plenty of time and incentive to whinge about those problems soon enough.

For now, let’s leave it on a optimistic note: if this works performance will scale with the number of cores you have.* More cores = higher creature cap!

Cheers,
Quasar

 

* (assuming ideal conditions and that the sample data is representative and that the earth hasn’t been invaded by demons pouring from a terrible murder portal)

1 Comment

0.11.2.5

* Fixed bug causing uncontrollable population explosions when Automatic Population Control was set to “Niches”
* Fixed crash when setting Time Acceleration to 0 while the “Energy” information panel was open
* Fixed crash caused by new migration routine
* Allowed “Energy” information panel to continue to display change information when paused
* Adjusted “H&C” Automatic Population Control to keep the maximum population a bit further away from the creature cap

Leave a comment

0.11.2.4 – Aesthetics and Balance

 

hugs

HUGS.

* Sedimentary layers visible in cliffs.
* ‘Fallen Fruit’ (Fruit/Pinecones) now visible on Palm Tree, Bunya Pine and Norfolk Pine (to better communicate that P. specium *can* get food from the large trees).
* Terrain lighting angle adjusted to make the game less dark overall.
* Creatures will now (attempt to) migrate to a neighboring zone if they’re hungry and can’t find any food source that meets an “optimality” threshold.
* Predators that are suppressing population growth and hunting because of lack of prey will no longer suppress the hunger need, and will attempt to migrate instead of idling or occupying themselves with other behaviors.
* “H&C” automatic population control adjusted to try and maintain the population of both at a higher number and reduce the number of extinctions (even if it means capping out sometimes).
* Sat Map will now display the species average of the creatures you hover over, rather than the individual creature.
* Species List (next to Sat Map/Clade Diagram) will now take you to a specimen when you click on a species name.
* No Audio Hardware bug should now be handled more gracefully (muting the audio and putting some error text on the Main Menu rather than crashing).
* “Out of Order” sign hung on the Web of Life (I tried fixing it and made it worse)
* Parking brake added to rovers (should reduce the amount of slipping down hills, especially on 10x speed.
* Droop and Balance costs slightly reduced.
* “Motivation” gene renamed to “Risk-Taking” to better communicate what it does.

1 Comment

Sedimentary Layers

Charles Darwin’s theory of evolution likely wouldn’t have existed without James Hutton’s concept of deep time, which he based on evidence from his profession as a geologist.

I’ve wanted layers for a while. It bothered me more than it should have that the cliff textures were vertically applied like all the other biome textures. So I took a bit of time out of trying to balance carnivores and did this.

I like it. I think it gives the world much more of a sense of age and natural history.

 

Cheers,
Qu

1 Comment

0.11.2.3

* Significantly reduced tree regrowth rate
* Reduced tree energy content
* Balanced oxygen absorption of advanced head types to make them less OP.
* Reduced growth cost of limbs.
* Adjusted creatures to take speed into consideration when deciding whether to race others for a food source.
* Increased the threshold at which Automatic Population Control starts raising food efficiency
* Adjusted “H” menu to show only the basic three efficiency options if “Niches” isn’t selected.

* Fixed a bug preventing Automatic Population Control from maintaining the population below cap.
* Fixed another clade-diagram related cause of crashes in long-running games.
* Fixed crash if “Forest Density” was set to 0

Leave a comment

0.11.2.2 – Game Stability Buff

* Fixed a common crash that happened when recording a new species to the clade diagram in long-running simulations.
* Fixed a crash that happened when importing creatures, if doing so would exceed the creature cap.
* Fixed a crash that happened if an underwater grazer tried to graze from pixels outside the map borders
* Fixed a crash that happened when starting a game with significantly increased forest density
* Fixed an issue creating an invisible cap limiting forest density to around 3. Forest density is now unbounded. (Cue people melting their computers with stupidly high forest densities)
* Fixed an issue allowing UI spinners to exceed their maximum and minimum values
* Fixed an issue causing “Initialise Random” to generate 25% more creatures.

DenseForest.png

Insane forest densities work again! Remember to rake the floors like they do in Finland.

 

Leave a comment