PixelWars/purple/purple.c
2015-01-22 17:47:43 +01:00

126 lines
2.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include "../team.h"
#include "job.h"
#include "tools.h"
#define P_READY 0
t_action purple_ia();
t_action purple_update(){
t_action action;
int i, type;
if(P_READY)
return purple_ia();
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 = 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.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);
}
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;
}
t_action purple_ia(){
t_action action;
int critic = check_critic_situation();
if(critic != -1){
action.dir = critic;
action.type = ATTACK;
return action;
}
char* job = (char*)getMemory();
switch(*job){
case P_KING :
action = p_king_ia();
break;
case P_JOBLESS :
action = p_jobless_ia();
break;
case P_WORKER :
action = p_worker_ia();
break;
case P_EXPLORER :
action = p_explorer_ia();
break;
default :
*job = P_JOBLESS;
action.type = WAIT;
break;
}
return action;
}