#ifndef __LIFE_H__
#define __LIFE_H__
#include "global.h"
#include "elements.h"
#include "gui.h"
#include <pthread.h>

struct life_list_head_p;
struct world_cell_p;
typedef struct life_list_p {
    struct life_list_p *next;
    struct life_list_head_p* parent_list;
    element_list_head_t life_stats;//"genetic" stats, read only, written when spawning
    element_list_head_t instance_state;//instance stats, determines abilities
    unsigned int last_update;
} life_list_t;

struct life_list_head_p {
    life_list_t* head;//the list of elements
    life_list_t* tail;//end of the list
    int list_size;
    struct world_cell_p* parent_cell;
};
typedef struct life_list_head_p life_list_head_t;

typedef  struct world_cell_p {
    //constant values of the cell, determined at init
    int light;
    int water;
    int x;
    int y;
    element_list_head_t elements;//all other things may use that exist in this cell
    life_list_head_t life;//all things alive in this cell
    pthread_mutex_t* lock;
} world_cell_t;

#include "world.h"

//create structs for life
void init_global_life(void);

//create a random peice of life on the list
void init_life(world_cell_t* cell);

//destroy instance on the list
void remove_life(life_list_t* instance);


//get the color of something
Uint8 get_color_r(life_list_t* instance);
Uint8 get_color_g(life_list_t* instance);
Uint8 get_color_b(life_list_t* instance);

//define state to be NULL, and will return a new element, at the end of the list returns NULL
life_list_t* get_next_instance(life_list_head_t* life_list, life_list_t** state);
int rand_int(void);

int life_compete(world_cell_t* cell, unsigned int update_num);//process the life in the cell


//returns a number between 0 and mod for the gene constant
int get_life_value_mod(life_list_t* instance, int gene, int mod);

void wait_for_compete(void);

#endif
