2016-05-16 00:34:44 +02:00

123 lines
2.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include "../team.h"
// Purple Hello World
typedef struct{
t_coord pos;
int new_born;
int try;
int brings_food;
int last_dir;
int last_action;
} purple_data;
t_coord p_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 p_abs(int val){
return val > 0 ? val : -val;
}
int p_dist(t_coord coord, int x, int y){
return p_abs(coord.x-x) + p_abs(coord.y-y);
}
t_action purple_update(){
t_action action;
int i, type;
int success = getSuccess();
purple_data* data = (purple_data*)getMemory();
if(!data->new_born){
success = 0;
data->new_born = 1;
}
if(data->last_action == MOVE){
if(success)
data->pos = p_newPos(data->pos, data->last_dir);
}
if(data->try && success)
data->brings_food = 1;
data->try = 0;
if(data->brings_food){
int distance = p_dist(data->pos, 0, 0);
if(distance == 1){
action.type = WAIT;
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(p_dist(p_newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
}
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;
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}else if(type == FOOD){
action.type = PICK;
action.dir = i;
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);
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}