diff --git a/generator_default.c b/generator_default.c new file mode 100644 index 0000000..2b52f2c --- /dev/null +++ b/generator_default.c @@ -0,0 +1,234 @@ +#include "main.h" + +#define MAX_POWER 10 +#define MAX_EPICENTER_BY_TYPE 7 +#define DIST_MIN_INTER_EPICENTRE 50 +#define MIN_RADIUS 25 +#define OFFSET_RADIUS 50 + +//map type ----> not used for now +enum{ + FLAT, VALLEY, +}; + +//biome type +enum{ + VILLAGE, PLAINS, FOREST,MOUNTAINS,NB_BIOMES +}; + +// probability for each biomes +// in following order {GRASS,TREE,BERRIES, ROCK, IRON_ORE} +char proba_table[NB_BIOMES][5] = {{97,2,1,0,0},{85,13,2,0,0},{40,40,20,0,0},{0,0,0,80,20}}; + +typedef struct{ + int x; + int y; + int type; + int power; + int radius; +} t_biome; + +// variables +int sp_x[NB_TEAMS], sp_y[NB_TEAMS]; +t_biome* l_biomes; +int size_biomes; +int nb_plains, nb_forests, nb_mountains; +int cpt_biome = 0; +int width, height; + +// functions +int distance_manhattan(int x1,int y1, int x2, int y2); +int absolute(int val); +void set_spawns(t_pixel** map, t_team* teams); +void create_biome(int x, int y, int type); +void create_biome_random(int type); +int check_nears_biomes(int x, int y); +int check_nears_spawn(int x, int y); +int in_radius(int x,int y,t_biome e); +int generate(int x, int y); +void init_generator(); + +void create_map(int w, int h){ + int i,j; + + //biome variable + + width = w; + height = h; + + init_generator(); + + //Epicenters generation + // random choice for numbers of biomes + nb_plains = (rand()%MAX_EPICENTER_BY_TYPE)+3; + nb_forests = (rand()%MAX_EPICENTER_BY_TYPE)+3; + nb_mountains = (rand()%MAX_EPICENTER_BY_TYPE)+3; + + size_biomes = nb_plains+ nb_forests + nb_mountains + NB_TEAMS; + + l_biomes = malloc(sizeof(t_biome)*size_biomes); + + // Spawn generations + set_spawns(map,teams); + + for(i=0;ix=x; + biome->y=y; + biome->type = type; + + switch(type){ + case VILLAGE: + biome->power = (rand()%MAX_POWER)+1; + biome->radius = (rand()%(25-10))+10; + break; + case PLAINS: + case FOREST: + case MOUNTAINS: + default: + biome->power = (rand()%MAX_POWER)+1; + biome->radius = (rand()%(OFFSET_RADIUS))+MIN_RADIUS; + break; + } + + l_biomes[cpt_biome++]=*biome; +} + +void create_biome_random(int type){ + int x,y; + do { + x=rand()%width; + y=rand()%height; + } while ((check_nears_biomes(x,y) != 0) || (check_nears_spawn(x,y) != 0)); //prevent biome superposition + + create_biome(x,y,type); +} + +int check_nears_biomes(int x, int y){ + int i, c=0; + for(i=0;i 0 ? val : -val; +} diff --git a/main.c b/main.c index 1d5e59b..7373b9c 100755 --- a/main.c +++ b/main.c @@ -9,8 +9,8 @@ #include // temp code -t_action purple_update(void* my_info, void* com_data, int success); -t_action orange_update(void* my_info, void* com_data, int success); +t_action purple_update(); +t_action orange_update(); t_dude* current_dude; @@ -55,7 +55,7 @@ void updateTeam(t_team team){ for(i=0; iinventory; } +t_com* getComData(){ + return current_dude->com_data; +} + +void* getMemory(){ + return current_dude->custom_data; +} + +int getSuccess(){ + return current_dude->success; +} + void spawnDudes(){ int i; t_dude new_dude; diff --git a/main.h b/main.h index 8a75730..310cb0e 100644 --- a/main.h +++ b/main.h @@ -21,11 +21,6 @@ #define NB_STARTING_FOOD 5 #define SPAWN_COOLDOWN 30 -// Teams -enum{ - PURPLE, ORANGE, NB_TEAMS -}; - typedef struct{ int type; void* data; @@ -45,7 +40,7 @@ typedef struct{ typedef struct{ int team; - t_action (*update)(void*, void*, int); + t_action (*update)(); int nb_dudes; t_dude* dudes; t_coord spawn; @@ -62,4 +57,4 @@ t_pixel** map; t_team* teams; SDL_Surface* img; -#endif \ No newline at end of file +#endif diff --git a/orange/orange.a b/orange/orange.a new file mode 100644 index 0000000..8dbb4fc Binary files /dev/null and b/orange/orange.a differ diff --git a/purple/Makefile b/purple/Makefile index 41d7dd0..93c1708 100644 --- a/purple/Makefile +++ b/purple/Makefile @@ -20,8 +20,8 @@ endif all : $(TARGET).$(LIB_PREFIX) -$(TARGET).$(LIB_PREFIX) : purple.o - $(AR) $(TARGET).$(LIB_PREFIX) purple.o +$(TARGET).$(LIB_PREFIX) : purple.o tools.o king.o worker.o + $(AR) $(TARGET).$(LIB_PREFIX) purple.o tools.o king.o worker.o %.o: %.c $(CC) -o $@ -c $< $(FLAGS) diff --git a/purple/job.h b/purple/job.h new file mode 100644 index 0000000..6c5acf7 --- /dev/null +++ b/purple/job.h @@ -0,0 +1,31 @@ +#ifndef P_JOB_H +#define P_JOB_H + +#include "../team.h" + +enum{ // jobs + P_JOBLESS, + P_KING, + P_WORKER, + P_EXPLORER +}; + +typedef struct{ + char identity; + char job; + char request_type; + char area_x; + char area_y; + char work_type; + char amount; +} p_request; + +t_action p_jobless_ia(); + +t_action p_king_ia(); + +t_action p_explorer_ia(); + +t_action p_worker_ia(); + +#endif // P_JOB_H diff --git a/purple/king.c b/purple/king.c new file mode 100644 index 0000000..13191ee --- /dev/null +++ b/purple/king.c @@ -0,0 +1,51 @@ +#include +#include "job.h" + +typedef struct{ + char job; + t_coord pos; + char identity; + int last_dir; + int last_action; + + int population; + int nb_explorers; + int nb_workers; + unsigned char town_done; + unsigned char town[8]; + unsigned char country_done; + unsigned char country[8]; +} purple_data_king; + +t_action p_king_ia(){ + t_action action; + purple_data_king* data = getMemory(); + action.type = WAIT; + if(data->pos.x != -1 || data->pos.y != -2) + data->job = P_JOBLESS; + else{ + t_com* com_data = getComData(); + p_request* request = ((void*)com_data)+sizeof(int); + p_request answer; + if(com_data != NULL){ + action.dir = com_data->flag; + action.type = COMMUNICATE; + if(request->identity < 1) + answer.identity = data->population++; // temp way to handle demography + if(request->job == P_WORKER) + data->nb_workers--; + if(request->job == P_EXPLORER) + data->nb_explorers--; + if(data->nb_explorers*2 > data->nb_workers){ + answer.job = P_WORKER; + + }else{ + answer.job = P_EXPLORER; + + } + + // send message + } + } + return action; +} diff --git a/purple/purple.a b/purple/purple.a new file mode 100644 index 0000000..4c95927 Binary files /dev/null and b/purple/purple.a differ diff --git a/purple/purple.c b/purple/purple.c index d994f8a..7edbcba 100644 --- a/purple/purple.c +++ b/purple/purple.c @@ -1,50 +1,23 @@ +#include #include #include "../team.h" -#include +#include "job.h" +#include "tools.h" -// Hello World +#define P_READY 0 -typedef struct{ - t_coord pos; - int new_born; - int try; - int brings_food; - int last_dir; - int last_action; -} t_data; +t_action purple_ia(); -t_coord newPos(t_coord coord, int dir){ - t_coord new_coord = coord; - switch(dir){ - case NORTH : - new_coord.y++; - break; - case SOUTH : - new_coord.y--; - break; - case WEST : - new_coord.x++; - break; - case EAST : - new_coord.x--; - break; - } - return new_coord; -} - -int abs(int val){ - return val > 0 ? val : -val; -} - -int dist(t_coord coord, int x, int y){ - return abs(coord.x-x) + abs(coord.y-y); -} - -t_action purple_update(void* my_info, t_com* com_data, int success){ - t_data* data = (t_data*)my_info; +t_action purple_update(){ t_action action; int i, type; + if(P_READY) + return purple_ia(); + + int success = getSuccess(); + purple_data* data = (purple_data*)getMemory(); + if(!data->new_born){ success = 0; data->new_born = 1; @@ -117,4 +90,37 @@ t_action purple_update(void* my_info, t_com* com_data, int success){ data->last_dir = action.dir; data->last_action = action.type; return action; +} + +t_action purple_ia(){ + t_action action; + + + int critic = check_critic_situation(); + if(critic != -1){ + action.dir = critic; + action.type = ATTACK; + return action; + } + + char* job = (char*)getMemory(); + switch(*job){ + case P_KING : + action = p_king_ia(); + break; + case P_JOBLESS : + action = p_jobless_ia(); + break; + case P_WORKER : + action = p_worker_ia(); + break; + case P_EXPLORER : + action = p_explorer_ia(); + break; + default : + *job = P_JOBLESS; + action.type = WAIT; + break; + } + return action; } \ No newline at end of file diff --git a/purple/tools.c b/purple/tools.c new file mode 100644 index 0000000..27143c0 --- /dev/null +++ b/purple/tools.c @@ -0,0 +1,37 @@ +#include "tools.h" + +t_coord newPos(t_coord coord, int dir){ + t_coord new_coord = coord; + switch(dir){ + case NORTH : + new_coord.y++; + break; + case SOUTH : + new_coord.y--; + break; + case WEST : + new_coord.x++; + break; + case EAST : + new_coord.x--; + break; + } + return new_coord; +} + +int abs(int val){ + return val > 0 ? val : -val; +} + +int dist(t_coord coord, int x, int y){ + return abs(coord.x-x) + abs(coord.y-y); +} + +int check_critic_situation(){ + int i; + for(i=0; i<4; i++){ + if(getNear(i) == DUDE && getInfo(i) == ORANGE) + return i; + } + return -1; +} \ No newline at end of file diff --git a/purple/tools.h b/purple/tools.h new file mode 100644 index 0000000..5255ddc --- /dev/null +++ b/purple/tools.h @@ -0,0 +1,23 @@ +#ifndef P_TOOLS_H +#define P_TOOLS_H + +#include "../team.h" + +typedef struct{ + t_coord pos; + int new_born; + int try; + int brings_food; + int last_dir; + int last_action; +} purple_data; + +t_coord newPos(t_coord coord, int dir); + +int abs(int val); + +int dist(t_coord coord, int x, int y); + +int check_critic_situation(); + +#endif // P_TOOLS_H \ No newline at end of file diff --git a/purple/worker.c b/purple/worker.c new file mode 100644 index 0000000..74ebfad --- /dev/null +++ b/purple/worker.c @@ -0,0 +1,42 @@ +#include "job.h" + +typedef struct{ + char job; + t_coord pos; + char identity; + int last_dir; + int last_action; + + int work_type; // worker + int amount_needed; // worker + t_coord storage; // worker + t_coord target_area; // worker and explorer +} purple_data_worker; + +t_action p_jobless_ia(){ + t_action action; + // goto (-1, -1) + // check NORTH + // empty -> be KING and go NORTH + // KING -> ask for identity + return action; +} + +t_action p_explorer_ia(){ + t_action action; + // goto nearest spot of the area + // start the loop + // at end of loop, go back to KING to get new AREA + return action; +} + +t_action p_worker_ia(){ + t_action action; + // loop : + // goto area + // search for requested resource + // bring it back to storage and amount-- + // while(amount > 0) + // go see the master to get new goal + return action; +} diff --git a/team.h b/team.h index 8c4752a..d6019e0 100644 --- a/team.h +++ b/team.h @@ -4,6 +4,11 @@ #define WRITE 0 #define READ 4 +// Teams +enum{ + PURPLE, ORANGE, NB_TEAMS +}; + // coordinates structure typedef struct{ int x; @@ -61,4 +66,10 @@ int getNear(int dir); int getInfo(int dir); +t_com* getComData(); + +void* getMemory(); + +int getSuccess(); + #endif \ No newline at end of file