worked on purple IA

This commit is contained in:
anselme16 2015-01-22 17:47:43 +01:00
parent 178629a9ad
commit d86c96d693
13 changed files with 493 additions and 51 deletions

234
generator_default.c Normal file
View File

@ -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;i<nb_plains;i++)
create_biome_random(PLAINS);
for(i=0;i<nb_forests;i++)
create_biome_random(FOREST);
for(i=0;i<nb_mountains;i++)
create_biome_random(MOUNTAINS);
// */
//génération de la carte
for (i=0;i<width;i++){
for(j=0;j<height;j++){
if (i == 0 || j == 0 || i == w-1 || j == h-1){
map[i][j].type = BEDROCK;
}else if((i == sp_x[PURPLE] && j == sp_y[PURPLE])){
map[i][j].type = SPAWN;
map[i][j].data=malloc(sizeof(int));
*((int*)(map[i][j].data)) = PURPLE;
}else if(i == sp_x[ORANGE] && j == sp_y[ORANGE]){
map[i][j].type = SPAWN;
map[i][j].data=malloc(sizeof(int));
*((int*)(map[i][j].data)) = ORANGE;
}else{
map[i][j].type = generate(i,j);
}
}
}
}
void init_generator(){
}
void set_spawns(t_pixel** map, t_team* teams){
int i;
int tier_w, tier_h;
tier_w = width/6;
tier_h = height/3;
sp_x[0] = (rand()%tier_w)+tier_w;
sp_y[0] = (rand()%tier_h)+tier_h;
sp_x[1] = width-sp_x[0];
sp_y[1] = height-sp_y[0];
for(i=0;i<2;i++){
teams[i].spawn.x = sp_x[i];
teams[i].spawn.y = sp_y[i];
create_biome(sp_x[i],sp_y[i],VILLAGE);
}
}
void create_biome(int x, int y, int type){
t_biome *biome = malloc(sizeof(t_biome));
biome->x=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<cpt_biome;i++){
if (in_radius(x,y,l_biomes[i]) != -1) c++;
}
return c;
}
int check_nears_spawn(int x, int y){
int i,c = 0;
for(i=0;i<2;i++)
if (distance_manhattan(x,y,sp_x[i],sp_y[i]) < 75) c++;
return c;
}
int in_radius(int x,int y,t_biome e){
int d = distance_manhattan(x,y,e.x,e.y);
return d < e.radius ? d : -1;
}
int generate(int x, int y){
int i, j;
int proba[5];
int sum, dist, ratio, val, seuil=0;
t_biome biome;
memset(&proba,0,sizeof(int)*5);
sum=0;
for(i=0;i<size_biomes;i++){
biome = l_biomes[i];
if ((dist=in_radius(x,y,biome)) != -1){
ratio=(((biome.radius-dist)*100)/biome.radius);
//ne marche pas correctement, besoin de fractale à la place
/* if (biome.type == MOUNTAINS && (ratio < 20)){
sum += biome.power*ratio*100;
proba[0] += biome.power*ratio*20;
proba[3] += biome.power*ratio*75;
proba[4] += biome.power*ratio*5;
}else{*/
sum += biome.power*ratio*100;
for(j=0;j<5;j++){
proba[j]+=(proba_table[biome.type][j])*(biome.power)*ratio;
}
}
}
if (sum!=0){
val = rand()%sum;
for (i=0;i<5;i++){
seuil += proba[i];
if(val < seuil)
return i+1;
}
}else{
val = rand()%100;
if (val <95){
return GRASS;
}else{
return TREE;
}
}
return GRASS;
}
int distance_manhattan(int x1,int y1, int x2, int y2){
return absolute(x1-x2) + absolute(y1-y2);
}
int absolute(int val){
return val > 0 ? val : -val;
}

18
main.c
View File

@ -9,8 +9,8 @@
#include <string.h>
// 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; i<team.nb_dudes; i++){
current_dude = team.dudes + i;
team.dudes[i].action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), team.dudes[i].success);
team.dudes[i].action = team.update();
if(team.dudes[i].com_data != NULL){
free(team.dudes[i].com_data);
team.dudes[i].com_data = NULL;
@ -117,6 +117,18 @@ int getInventory(){
return current_dude->inventory;
}
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;

9
main.h
View File

@ -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
#endif

BIN
orange/orange.a Normal file

Binary file not shown.

View File

@ -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)

31
purple/job.h Normal file
View File

@ -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

51
purple/king.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdlib.h>
#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;
}

BIN
purple/purple.a Normal file

Binary file not shown.

View File

@ -1,50 +1,23 @@
#include <stdlib.h>
#include <stdio.h>
#include "../team.h"
#include <stdlib.h>
#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;
}

37
purple/tools.c Normal file
View File

@ -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;
}

23
purple/tools.h Normal file
View File

@ -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

42
purple/worker.c Normal file
View File

@ -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;
}

11
team.h
View File

@ -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