implemented pick/put test algorith for purple, but it is bugged

This commit is contained in:
Anselme 2015-01-14 22:57:58 +01:00
parent b3a6964493
commit 0bc1604747
3 changed files with 112 additions and 26 deletions

83
main.c
View File

@ -20,6 +20,8 @@ t_action orange_update(void* my_info, void* com_data, int my_id, int success);
int get_purple_size();
int get_orange_size();
t_dude* current_dude;
void initWorld(int width, int height){
int i;
@ -66,6 +68,7 @@ void updateTeam(t_team team){
int i;
t_action action;
for(i=0; i<team.nb_dudes; i++){
current_dude = team.dudes + i;
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i, team.dudes[i].success);
handleAction(action, team.dudes+i);
}
@ -89,6 +92,11 @@ t_coord getPos(t_coord coord, int dir){
return coord;
}
int getNear(int dir){
t_coord coord = getPos(current_dude->pos, dir);
return map[coord.x][coord.y].type;
}
void spawnDudes(){
int i;
t_dude new_dude;
@ -144,12 +152,13 @@ void handleAction(t_action action, t_dude* dude){
map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL;
}
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
}else
dude->success = 0;
break;
case PUT :
if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){
if(target.type == GRASS){
if(target.type == GRASS || target.type == MARK){
map[target_pos.x][target_pos.y].type = dude->inventory;
nb_res = malloc(sizeof(int));
*nb_res = 1;
@ -159,6 +168,7 @@ void handleAction(t_action action, t_dude* dude){
(*nb_res)++;
}
dude->inventory = -1;
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
}else
dude->success = 0;
break;
@ -190,20 +200,30 @@ void handleAction(t_action action, t_dude* dude){
map[target_pos.x][target_pos.y].data = nb_res;
break;
case GRASS :
map[target_pos.x][target_pos.y].type = ROAD;
map[target_pos.x][target_pos.y].type = MARK;
map[target_pos.x][target_pos.y].data = NULL;
break;
case ROAD :
case MARK :
map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL;
break;
case WOOD :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else{
free(target.data);
map[target_pos.x][target_pos.y].type = WALL;
map[target_pos.x][target_pos.y].data = NULL;
}
break;
case STONE :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else{
free(target.data);
map[target_pos.x][target_pos.y].type = WALL;
map[target_pos.x][target_pos.y].type = ROAD;
map[target_pos.x][target_pos.y].data = NULL;
}
break;
@ -214,12 +234,12 @@ void handleAction(t_action action, t_dude* dude){
else
map[target_pos.x][target_pos.y].type = SWORD;
break;
// TODO : case wood -> sign
// TODO : case sign -> wood
default :
dude->success = 0;
break;
}
if(dude->success)
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
break;
case WAIT :
dude->success = 1;
@ -265,8 +285,8 @@ void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
for(i=0; i<screen->w; i++){
for(j=0; j<screen->h; j++){
x = i/zoom_level + x_offset;
y = j/zoom_level + y_offset;
x = (i - img->w/2)/zoom_level + x_offset;
y = (j - img->h/2)/zoom_level + y_offset;
if(x >= 0 && x < img->w && y >= 0 && y < img->h)
putpixel(screen, i, j, getpixel(img, x, y));
}
@ -283,7 +303,10 @@ int MAIN
int y_offset = 0;
int zoom_level = 1;
int over = 0;
int time = 0;
int wait_time = 100;
int new_time = 0;
int remaining_time = -1;
int fullscreen = 0;
int width = DEFAULT_WIDTH;
int height = DEFAULT_HEIGHT;
@ -296,9 +319,6 @@ int MAIN
case 'f' :
fullscreen = 1;
break;
case 't' :
wait_time = atoi(argv[0]+2);
break;
case 'w' :
width = atoi(argv[0]+2);
break;
@ -317,8 +337,11 @@ int MAIN
initWorld(width, height);
SDL_Flip(img);
x_offset = width/2;
y_offset = height/2;
printf("Launching simulation...\n");
time = SDL_GetTicks();
while (!over){
while(SDL_PollEvent(&event)){
switch (event.type){
@ -327,22 +350,29 @@ int MAIN
case SDLK_ESCAPE :
over = 1;
break;
case SDLK_w :
y_offset -= 5;
break;
case SDLK_s :
y_offset += 5;
break;
case SDLK_a :
x_offset -= 5;
break;
case SDLK_d :
x_offset += 5;
break;
case SDLK_UP :
y_offset--;
wait_time *= 2;
break;
case SDLK_DOWN :
y_offset++;
if(wait_time > 2)
wait_time /= 2;
break;
case SDLK_LEFT :
x_offset--;
break;
case SDLK_RIGHT :
x_offset++;
break;
case SDLK_PAGEUP :
zoom_level++;
break;
case SDLK_PAGEDOWN :
case SDLK_RIGHT :
zoom_level--;
if(zoom_level < 1)
zoom_level = 1;
@ -354,6 +384,10 @@ int MAIN
break;
}
break;
case SDL_MOUSEMOTION:
x_offset += event.motion.xrel;
y_offset += event.motion.yrel;
break;
case SDL_QUIT:
over = 1;
break;
@ -362,16 +396,21 @@ int MAIN
}
}
if(!paused){
new_time = SDL_GetTicks();
remaining_time -= new_time - time;
time = new_time;
if(remaining_time < 0 && !paused){
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();
remaining_time = wait_time;
}
render(screen, x_offset, y_offset, zoom_level);
SDL_Delay(wait_time);
SDL_Delay(30);
SDL_Flip(screen);
}
SDL_FreeSurface(screen);

View File

@ -4,21 +4,68 @@
// Hello World
typedef struct{
int plop; // custom info
t_coord pos;
int try;
int brings_food;
} t_data;
int get_purple_size(){
return sizeof(t_data);
}
t_coord getPos(t_coord coord, int dir);
t_action purple_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info;
t_action action;
int i;
int i, type;
if(data->try && success)
data->brings_food = 1;
data->try = 0;
if(data->brings_food){
if(data->pos.x == 0)
action.dir = WEST;
else{
if(data->pos.y == 0){
if(data->pos.x > 0)
action.dir = EAST;
else
action.dir = WEST;
if(data->pos.x == -1 || data->pos.x == 1){
data->brings_food = 0;
action.type = PUT;
action.data = NULL;
return action;
}
}else
action.dir = data->pos.y > 0 ? SOUTH : NORTH;
}
action.type = MOVE;
action.data = NULL;
data->pos = getPos(data->pos, action.dir);
return action;
}
for(i=0; i<4; i++){
type = getNear(i);
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
action.type = WORK;
action.dir = i;
action.data = NULL;
return action;
}else if(type == FOOD){
action.type = PICK;
action.dir = i;
action.data = NULL;
data->try = 1;
}
}
action.type = MOVE;
action.dir = rand()%4;
action.data = NULL;
data->pos = getPos(data->pos, action.dir);
return action;
}

2
team.h
View File

@ -11,7 +11,7 @@ enum{
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
FOOD, WOOD, STONE, IRON, SWORD, // resources
DUDE, // humans
SPAWN, WALL, ROAD, SIGN // buildings
SPAWN, WALL, ROAD, MARK, SIGN // buildings
};
// Action types