#include #include "team.h" #include "stdlib.h" // Hello World typedef struct{ t_coord pos; int new_born; int try; int brings_food; int last_dir; int last_action; } t_data; 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, void* com_data, int success){ t_data* data = (t_data*)my_info; t_action action; int i, type; 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){ int distance = dist(data->pos, 0, 0); if(distance == 1){ action.type = WAIT; action.data = NULL; action.dir = 0; for(i=0; i<4; i++){ type = getNear(i); if(type == SPAWN){ action.dir = i; action.type = PUT; data->brings_food = 0; break; } } }else{ action.type = MOVE; do{ action.dir = rand()%4; }while(dist(newPos(data->pos, action.dir), 0, 0) > distance && distance != 0); action.data = NULL; } data->last_dir = action.dir; data->last_action = action.type; 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; data->last_dir = action.dir; data->last_action = action.type; return action; }else if(type == FOOD){ action.type = PICK; action.dir = i; action.data = NULL; data->try = 1; data->last_dir = action.dir; data->last_action = action.type; return action; } } action.type = MOVE; do{ 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; return action; }