fixed purple IA problems and found bug in spawn food handling
This commit is contained in:
parent
cb02ca1330
commit
4a29cad6cc
@ -5,13 +5,11 @@ int distance_manhattan(int x1,int y1, int x2, int y2);
|
|||||||
|
|
||||||
int absolute(int val);
|
int absolute(int val);
|
||||||
|
|
||||||
int min(int a, int b)
|
int custom_min(int a, int b){
|
||||||
{
|
|
||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
int max(int a, int b)
|
int custom_max(int a, int b){
|
||||||
{
|
|
||||||
return a > b ? a : 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){
|
if (i == 0 || j == 0 || i == w-1 || j == h-1){
|
||||||
map[i][j].type = BEDROCK;
|
map[i][j].type = BEDROCK;
|
||||||
}else{
|
}else{
|
||||||
int d = max(w, h);
|
int d = custom_max(w, h);
|
||||||
for(k=0; k<NB_TEAMS; k++){
|
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) break;
|
||||||
}
|
}
|
||||||
if(d == 0){
|
if(d == 0){
|
||||||
|
11
main.c
11
main.c
@ -99,7 +99,7 @@ void spawnDudes(){
|
|||||||
for(i=0; i<NB_TEAMS; i++){
|
for(i=0; i<NB_TEAMS; i++){
|
||||||
if(teams[i].spawn_food > 0)
|
if(teams[i].spawn_food > 0)
|
||||||
teams[i].spawn_count++;
|
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_food--;
|
||||||
teams[i].spawn_count = 0;
|
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_coord target_pos = getPos(dude->pos, action.dir);
|
||||||
t_pixel target = map[target_pos.x][target_pos.y];
|
t_pixel target = map[target_pos.x][target_pos.y];
|
||||||
int* nb_res;
|
int* nb_res;
|
||||||
t_team* team;
|
|
||||||
switch(action.type){
|
switch(action.type){
|
||||||
case MOVE :
|
case MOVE :
|
||||||
dude->success = 0;
|
dude->success = 0;
|
||||||
@ -129,7 +128,6 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
&& target.type != ROCK
|
&& target.type != ROCK
|
||||||
&& target.type != BEDROCK
|
&& target.type != BEDROCK
|
||||||
&& target.type != IRON_ORE
|
&& target.type != IRON_ORE
|
||||||
&& target.type != DUDE
|
|
||||||
&& target.type != TREE
|
&& target.type != TREE
|
||||||
&& target.type != LIBRARY
|
&& target.type != LIBRARY
|
||||||
)
|
)
|
||||||
@ -170,8 +168,8 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
}else if(target.type == SPAWN && dude->inventory == FOOD){
|
}else if(target.type == SPAWN && dude->inventory == FOOD){
|
||||||
printf("put food in spawn\n");
|
printf("put food in spawn\n");
|
||||||
dude->inventory = -1;
|
dude->inventory = -1;
|
||||||
team = (t_team*)target.data;
|
nb_res = (int*)target.data;
|
||||||
team->spawn_food++;
|
teams[*nb_res].spawn_food++;
|
||||||
}else{
|
}else{
|
||||||
printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
||||||
dude->success = 0;
|
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){
|
void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
|
||||||
int i, j, x, y;
|
int i, j, x, y;
|
||||||
int pos_x,pos_y;
|
int pos_x,pos_y;
|
||||||
|
|
||||||
pos_x = x_offset - img->w/(2*zoom_level);
|
pos_x = x_offset - img->w/(2*zoom_level);
|
||||||
pos_y = y_offset - img->h/(2*zoom_level);
|
pos_y = y_offset - img->h/(2*zoom_level);
|
||||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||||
@ -367,6 +366,8 @@ int MAIN
|
|||||||
|
|
||||||
printf("Launching simulation...\n");
|
printf("Launching simulation...\n");
|
||||||
|
|
||||||
|
printf("spawn (%d, %d)\n", teams[PURPLE].spawn.x, teams[PURPLE].spawn.y);
|
||||||
|
|
||||||
time = SDL_GetTicks();
|
time = SDL_GetTicks();
|
||||||
while (!over){
|
while (!over){
|
||||||
while(SDL_PollEvent(&event)){
|
while(SDL_PollEvent(&event)){
|
||||||
|
1
main.h
1
main.h
@ -16,6 +16,7 @@
|
|||||||
#define DEFAULT_WIDTH 400
|
#define DEFAULT_WIDTH 400
|
||||||
#define DEFAULT_HEIGHT 250
|
#define DEFAULT_HEIGHT 250
|
||||||
#define NB_STARTING_FOOD 5
|
#define NB_STARTING_FOOD 5
|
||||||
|
#define SPAWN_COOLDOWN 30
|
||||||
|
|
||||||
// Teams
|
// Teams
|
||||||
enum{
|
enum{
|
||||||
|
56
purple.c
56
purple.c
@ -6,13 +6,31 @@
|
|||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
t_coord pos;
|
t_coord pos;
|
||||||
|
int new_born;
|
||||||
int try;
|
int try;
|
||||||
int brings_food;
|
int brings_food;
|
||||||
int last_dir;
|
int last_dir;
|
||||||
int last_action;
|
int last_action;
|
||||||
} t_data;
|
} 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){
|
int abs(int val){
|
||||||
return val > 0 ? val : -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;
|
t_action action;
|
||||||
int i, type;
|
int i, type;
|
||||||
|
|
||||||
if(data->last_action == MOVE && success)
|
if(!data->new_born){
|
||||||
data->pos = getPos(data->pos, data->last_dir);
|
success = 0;
|
||||||
|
data->new_born = 1;
|
||||||
if(data->try && success){
|
|
||||||
data->brings_food = 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;
|
data->try = 0;
|
||||||
|
|
||||||
if(data->brings_food){
|
if(data->brings_food){
|
||||||
@ -41,14 +65,12 @@ t_action purple_update(void* my_info, void* com_data, int success){
|
|||||||
action.type = WAIT;
|
action.type = WAIT;
|
||||||
action.data = NULL;
|
action.data = NULL;
|
||||||
action.dir = 0;
|
action.dir = 0;
|
||||||
data->last_action = action.type;
|
|
||||||
for(i=0; i<4; i++){
|
for(i=0; i<4; i++){
|
||||||
type = getNear(i);
|
type = getNear(i);
|
||||||
if(type == SPAWN){
|
if(type == SPAWN){
|
||||||
action.dir = i;
|
action.dir = i;
|
||||||
action.type = PUT;
|
action.type = PUT;
|
||||||
data->brings_food = 0;
|
data->brings_food = 0;
|
||||||
data->last_action = action.type;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,12 +78,11 @@ t_action purple_update(void* my_info, void* com_data, int success){
|
|||||||
action.type = MOVE;
|
action.type = MOVE;
|
||||||
do{
|
do{
|
||||||
action.dir = rand()%4;
|
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;
|
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;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +100,7 @@ t_action purple_update(void* my_info, void* com_data, int success){
|
|||||||
action.dir = i;
|
action.dir = i;
|
||||||
action.data = NULL;
|
action.data = NULL;
|
||||||
data->try = 1;
|
data->try = 1;
|
||||||
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
data->last_action = action.type;
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
@ -86,8 +108,14 @@ t_action purple_update(void* my_info, void* com_data, int success){
|
|||||||
|
|
||||||
action.type = MOVE;
|
action.type = MOVE;
|
||||||
do{
|
do{
|
||||||
action.dir = rand()%4;
|
action.dir = (data->last_dir + rand()%3)%4;
|
||||||
}while(action.dir == data->last_dir);
|
type = getNear(action.dir);
|
||||||
|
}while(type == WALL
|
||||||
|
&& type == ROCK
|
||||||
|
&& type == BEDROCK
|
||||||
|
&& type == IRON_ORE
|
||||||
|
&& type == TREE
|
||||||
|
&& type == LIBRARY);
|
||||||
action.data = NULL;
|
action.data = NULL;
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
data->last_action = action.type;
|
||||||
|
4
tools.c
4
tools.c
@ -24,6 +24,7 @@ void add_move(t_dude* dude, t_coord dst){
|
|||||||
|
|
||||||
void applyMove(t_move* move){
|
void applyMove(t_move* move){
|
||||||
t_pixel target = map[move->dst.x][move->dst.y];
|
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
|
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
|
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
|
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]));
|
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.x = move->dst.x;
|
||||||
move->dude->pos.y = move->dst.y;
|
move->dude->pos.y = move->dst.y;
|
||||||
move->dude->success = 1;
|
|
||||||
free(move);
|
free(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ Uint32 getColor(t_pixel pixel){
|
|||||||
case IRON : return 0x4A4036;
|
case IRON : return 0x4A4036;
|
||||||
case DUDE :
|
case DUDE :
|
||||||
dudeData = pixel.data;
|
dudeData = pixel.data;
|
||||||
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
|
return dudeData->team == ORANGE ? 0x7A4100 : dudeData->success ? 0x9900FF : 0xFF0000;
|
||||||
case SPAWN :
|
case SPAWN :
|
||||||
spawnData = (int*)(pixel.data);
|
spawnData = (int*)(pixel.data);
|
||||||
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
|
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user