Generating Family Trees

Background

As I am creating a life simulation game, families need to be generated. This is so the player can keep track of the family relationships, and also so the AI doesn't do anything... undesirable.

The game has a 2 dimensional array which stores the people (peeps) on the x axis and their attributes on the y axis. 2 of these attributes are the mother and father ID. Each peep has their own unique ID, so the mother and father slots are self referenced IDs.

The Problem

Each peep only stores their mother and their father. The problem is they need to know who their siblings, aunts, uncles, cousins, grandparents, nieces, nephews, children etc. are.

Current knowledge of the player in the game.

The Solution

Luckily, the player isn't the only peep who stores their parents information, because every peep in the array stores their father and mother. This means that when certain criteria are met, we can find out who peeps are in relation to the player.

For example, we can loop through all of the peeps in the array and check if the current peep has the same parents are the player. If so, they must be your sibling.

Since these peeps share parents, they must be siblings.

After this breakthrough, the rest of the family members can be discovered. These relations are explained below:

  • If their parents = the player's parents, then they must be siblings.
  • If they = player's parent's parent, then they are the player's grandparent.
  • If their grandparents = the player's parents, then they must be the player's niece/nephew (or child).
  • If their parents = the player, then they must be the player's children.
  • If their parents = the player's grandparents, then they must be the player's uncle/aunt (or parent).
  • If their grandparents = the player's grandparents, then they must be cousins.
This has to be conducted in a specific order to work, otherwise parents and uncles, grandchildren and nieces/nephews will merge.

Step, Half and In-laws

The same applies to step-parents, as a peep can also store a step-mother and step-father and then generate a step-family. Half siblings are when the player shares one parent but not another. And in-laws can be added through the storing of a spouse and using that to run similar queries.

In Practice

To find specific relationships, the method above can be used. However, this is not needed for the AI, as the player cannot see from their perspective. Instead, the AI just need a generic "family" tag so they know who they can and can't have certain relationships with.

For the AI, then, a simplified approach can be used. This involves first establishing the parents of said peep and storing their IDs in a list. Grandparents can then be added to the list by finding the peep's parent's parents.

Then, a loop can be performed. This loop asks 2 questions:

  1. (a.) Is my father in the family? (b.) Am I NOT in the family yet? = Add me to the list.
  2. (a.) Is my mother in the family? (b.) Am I NOT in the family yet? = Add me to the list.

These questions are asked to all peeps in the game world. On first iteration, all immediate family members are added to the list. Siblings, aunts and uncles.

On the second iteration, a more extended family is added to the list. Cousins, nieces and nephews.

To clear a few things up, the peep's children's girlfriends, boyfriends and spouses can be added.

This will create an appropriate "family" list of family members. Continually looping through the main loop will lead to an ever greater family tree, expanding horizontally only. To add more generations (some verticality), great-grandparents need to be added at the start. 

The pseudocode below shows the basic algorithm to generate a list of family members which will include: parents, grandparents, children, cousins, nieces/nephews, uncles/aunts, siblings, step-parents and siblings as well as half-parents and siblings. We'll call this algorithm the Creeping Tree (as it can continually expand horizontally). (Note: person is used instead of peep in the pseudocode, as it is more applicable).

  1. DEFINE mother, father, person vars and family list
  2. ADD person, person's spouse and parents (if any) to family
  3. ADD person's step-parents to family
  4. ADD person's parent's parents to family
  5. REPEAT 2 times LOOP THROUGH all people in array
    1. IS current person's father in the family AND current person NOT in family THEN
      1. ADD current person to family
    2. IS current person's mother in family AND current person NO in family THEN
      1. ADD current person to family
  6. RETURN family

Popular Posts