Procedural Microbe Generator

So I need to learn c++ for a job interview (which I imagine you guys will not want me to get as it’ll be stressful and take up all my time, lol).

Anyway I thought what better way to practice than write a program I’ve been meaning to write for a while. So basically what this does is it takes a string of letters and turns it into a body plan for a microbe. This means you can start the game in a procedurally generated patch with random microbes all around you (if we want to go that way).

It took a while to figure it out and it’s very inefficient (checking the same spaces 20 times) however what it can do is place any rectangular organelles (they have to be rectangular) on the hex grid such that they don’t overlap and such that all the exterior organelles (flagella, toxin, agents, pilus etc) are around the edge and the interior organelles are in the center. Moreover the microbes should come out circular as I used the distances on the hex grid to try and get each new organelle added as close to the center as possible.

Also it makes these cool ascii art microbe plans. Here are some examples. (n = nucleus, c = cytoplasm, h = chloroplast, m = mitochondria, v = vacuole, f = flagella, t = toxin which I think is all the organelles we have right now). Can you work out which shapes I have arbitrarily chosen for each organelle (these should be changed to the real values)?

List of Organelles to make : ncchcmccvmffftf

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . f . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . t . m m f . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . c c h h . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . c n n v v . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . c c n n v v . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . f . m m . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . f . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

List of Organelles to make : nchmcchhmvtttft

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . h h . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . t m m h h . t . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . c n n h h . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . t . c c n n f . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . m m . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . t . v v . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . v v . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

List of Organelles to make : ncmmvmvvhmttfft

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . f . . . t . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . m m . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . t v v m m h h . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . v v n n v v . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . t v v c n n v v . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . v v m m . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . f . m m . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

So yeah if we add the ability to read these codes from a file it’ll be really easy to share setups (like if you find a cool microbe you just need to find out it’s code and you can share it on the forums and people can load it into their games really easily).

It also opens the door to a super basic auto evo because each time the player goes to the editor the code of one ai species could be altered (add an organelle up to 20 max or take one away down to only nucleus and 1 cytoplasm remaining) and so the patch would change over time as you play. Of course this will be a random change and so it won’t make much sense however it will be at least a step in the right direction (it’s gonna take like 1000 lines of code to figure out which letter is the best to change, lol).

As far as AI goes maybe the best solution is to say “if you have no flagella do nothing, if you have flagella and no toxin then run from the player, if you have flagella and toxin try to go towards the player firing toxin, if you have only toxin then fire when the player is near”. So at least the ai will update a little bit when the microbes are changed (though if this is a lot of hassle then maybe it’s better to wait until we can write proper ai.)

Anyway yeah all feedback welcome, code is here. It’s horrible but it’s the first c++ program I’ve written.

1 Like

Good idea. I think it would work well for now, though it would be a good idea to be able to “push organelle out of the way”, so that the center isn’t always the same and only the outside keeps growing. There also needs to be some algorithm to fill in holes from deleted organelles. Also, here are some suggestions about your coding in C++, since it’s fairly different from python and I want you to good on your job interview:

  1. You included <math.h>, that is a C library and is deprecated in C++, you should use instead.
  2. All of your variables are public, which is okay for a prototype, but will be a very bad idea in production code. Furthermore, if you are only grouping all public variables, a better idea is to just use a struct.
  3. Class names should start with ACaptitalLetter by convention, just like functions. Variables are camel cased, and you should use underscores only in macros or global variables.
  4. HEX_SIZE should probably be a global variable, since you use the same value for the whole program.
  5. When using if statements, you should put the following lines in brackets ({}). Otherwise someone might add an extra line, forget the brackets, and mess up your conditional.
  6. You often pass char (&grid)[50][50] as simply char* grid. Much less code and lets you pass different sized arrays (since 50 isn’t hardcoded in your add organelle function at any point). If you would have used 50, it might have made sense to create a function template.
  7. organelle nucleus;
    nucleus.letter = ‘n’;
    nucleus.size_x = 2;
    nucleus.size_y = 2;
    You are doing too much copying and assigning. A better idea would be to just use a constructor and do: nucleus(‘n’,2,2);
  8. Use std::cerr for errors instead of std::cout

Other than that great job.

1 Like

I don’t understand what you mean here. Do you mean what happens if you change the string of letters?

Thanks for all the coding advice, I was definitely hacking my way around a lot of problems.