PixelWars/purple.c
2015-01-15 17:42:59 +01:00

95 lines
1.9 KiB
C

#include <stdio.h>
#include "team.h"
#include "stdlib.h"
// Hello World
typedef struct{
t_coord pos;
int try;
int brings_food;
int last_dir;
int last_action;
} t_data;
t_coord getPos(t_coord coord, int dir);
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->last_action == MOVE && success)
data->pos = getPos(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;
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;
}
}
}else{
action.type = MOVE;
do{
action.dir = rand()%4;
}while(dist(getPos(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;
}
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_action = action.type;
return action;
}
}
action.type = MOVE;
do{
action.dir = rand()%4;
}while(action.dir == data->last_dir);
action.data = NULL;
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}