fixed purple IA problems and found bug in spawn food handling

This commit is contained in:
Anselme 2015-01-15 19:59:55 +01:00
parent cb02ca1330
commit 4a29cad6cc
5 changed files with 55 additions and 27 deletions

View File

@ -5,13 +5,11 @@ int distance_manhattan(int x1,int y1, int x2, int y2);
int absolute(int val);
int min(int a, int b)
{
int custom_min(int a, int b){
return a < b ? a : b;
}
int max(int a, int b)
{
int custom_max(int a, int b){
return a > b ? a : b;
}
@ -31,9 +29,9 @@ void create_map(int w, int h){
if (i == 0 || j == 0 || i == w-1 || j == h-1){
map[i][j].type = BEDROCK;
}else{
int d = max(w, h);
int d = custom_max(w, h);
for(k=0; k<NB_TEAMS; k++){
d = min(d, distance_manhattan(teams[k].spawn.x, teams[k].spawn.y, i, j));
d = custom_min(d, distance_manhattan(teams[k].spawn.x, teams[k].spawn.y, i, j));
if(!d) break;
}
if(d == 0){

11
main.c
View File

@ -99,7 +99,7 @@ void spawnDudes(){
for(i=0; i<NB_TEAMS; i++){
if(teams[i].spawn_food > 0)
teams[i].spawn_count++;
if(teams[i].spawn_count > 10 && map[teams[i].spawn.x][teams[i].spawn.y].type == SPAWN){
if(teams[i].spawn_count > SPAWN_COOLDOWN && map[teams[i].spawn.x][teams[i].spawn.y].type == SPAWN){
teams[i].spawn_food--;
teams[i].spawn_count = 0;
@ -121,7 +121,6 @@ void handleAction(t_action action, t_dude* dude){
t_coord target_pos = getPos(dude->pos, action.dir);
t_pixel target = map[target_pos.x][target_pos.y];
int* nb_res;
t_team* team;
switch(action.type){
case MOVE :
dude->success = 0;
@ -129,7 +128,6 @@ void handleAction(t_action action, t_dude* dude){
&& target.type != ROCK
&& target.type != BEDROCK
&& target.type != IRON_ORE
&& target.type != DUDE
&& target.type != TREE
&& target.type != LIBRARY
)
@ -170,8 +168,8 @@ void handleAction(t_action action, t_dude* dude){
}else if(target.type == SPAWN && dude->inventory == FOOD){
printf("put food in spawn\n");
dude->inventory = -1;
team = (t_team*)target.data;
team->spawn_food++;
nb_res = (int*)target.data;
teams[*nb_res].spawn_food++;
}else{
printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
dude->success = 0;
@ -300,6 +298,7 @@ int screen_to_img(int pos_screen, int zoom ,int offset){
void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
int i, j, x, y;
int pos_x,pos_y;
pos_x = x_offset - img->w/(2*zoom_level);
pos_y = y_offset - img->h/(2*zoom_level);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
@ -367,6 +366,8 @@ int MAIN
printf("Launching simulation...\n");
printf("spawn (%d, %d)\n", teams[PURPLE].spawn.x, teams[PURPLE].spawn.y);
time = SDL_GetTicks();
while (!over){
while(SDL_PollEvent(&event)){

1
main.h
View File

@ -16,6 +16,7 @@
#define DEFAULT_WIDTH 400
#define DEFAULT_HEIGHT 250
#define NB_STARTING_FOOD 5
#define SPAWN_COOLDOWN 30
// Teams
enum{

View File

@ -6,13 +6,31 @@
typedef struct{
t_coord pos;
int new_born;
int try;
int brings_food;
int last_dir;
int last_action;
} t_data;
t_coord getPos(t_coord coord, int dir);
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;
@ -27,12 +45,18 @@ t_action purple_update(void* my_info, void* com_data, int success){
t_action action;
int i, type;
if(data->last_action == MOVE && success)
data->pos = getPos(data->pos, data->last_dir);
if(data->try && success){
data->brings_food = 1;
if(!data->new_born){
success = 0;
data->new_born = 1;
}
if(data->last_action == MOVE){
if(success)
data->pos = newPos(data->pos, data->last_dir);
}
if(data->try && success)
data->brings_food = 1;
data->try = 0;
if(data->brings_food){
@ -41,14 +65,12 @@ t_action purple_update(void* my_info, void* com_data, int success){
action.type = WAIT;
action.data = NULL;
action.dir = 0;
data->last_action = action.type;
for(i=0; i<4; i++){
type = getNear(i);
if(type == SPAWN){
action.dir = i;
action.type = PUT;
data->brings_food = 0;
data->last_action = action.type;
break;
}
}
@ -56,12 +78,11 @@ t_action purple_update(void* my_info, void* com_data, int success){
action.type = MOVE;
do{
action.dir = rand()%4;
}while(dist(getPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
}while(dist(newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
action.data = NULL;
data->pos = getPos(data->pos, action.dir);
data->last_dir = action.dir;
data->last_action = action.type;
}
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}
@ -79,6 +100,7 @@ t_action purple_update(void* my_info, void* com_data, int success){
action.dir = i;
action.data = NULL;
data->try = 1;
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}
@ -86,8 +108,14 @@ t_action purple_update(void* my_info, void* com_data, int success){
action.type = MOVE;
do{
action.dir = rand()%4;
}while(action.dir == data->last_dir);
action.dir = (data->last_dir + rand()%3)%4;
type = getNear(action.dir);
}while(type == WALL
&& type == ROCK
&& type == BEDROCK
&& type == IRON_ORE
&& type == TREE
&& type == LIBRARY);
action.data = NULL;
data->last_dir = action.dir;
data->last_action = action.type;

View File

@ -24,6 +24,7 @@ void add_move(t_dude* dude, t_coord dst){
void applyMove(t_move* move){
t_pixel target = map[move->dst.x][move->dst.y];
move->dude->success = 1;
map[move->dude->pos.x][move->dude->pos.y] = move->dude->ground; // set the ground where the dude was
move->dude->ground = target; // set the ground of the dude to where he goes
map[move->dst.x][move->dst.y].type = DUDE; // set the target tile with the dude
@ -32,7 +33,6 @@ void applyMove(t_move* move){
putpixel(img, move->dude->pos.x, move->dude->pos.y, getColor(map[move->dude->pos.x][move->dude->pos.y]));
move->dude->pos.x = move->dst.x;
move->dude->pos.y = move->dst.y;
move->dude->success = 1;
free(move);
}
@ -79,7 +79,7 @@ Uint32 getColor(t_pixel pixel){
case IRON : return 0x4A4036;
case DUDE :
dudeData = pixel.data;
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
return dudeData->team == ORANGE ? 0x7A4100 : dudeData->success ? 0x9900FF : 0xFF0000;
case SPAWN :
spawnData = (int*)(pixel.data);
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;