PixelWars/orange.c
2015-01-19 09:04:17 +01:00

218 lines
4.1 KiB
C

#include "team.h"
#include "stdio.h"
#include "stdlib.h"
// Development in progress
//Structure definition
enum{
JOBLESS, MASTER, GATHERER, WOODCUTTER, MINER, NB_JOBS
};
enum{
NO_AIM, AIM_SPAWN, AIM_SPOT,
};
typedef struct{
char type; //the type of ressource that was found
t_coord pos; // pos where ressource was found
} t_ressource_spot;
typedef struct{
char name; // name of the dude
t_coord pos; // position relative to the spawn
char hatched;
char job;
char aim;
t_ressource_spot res_spot;
char last_action;
char last_dir;
} t_info_data;
typedef union{
t_ressource_spot res_spot;
}t_message;
typedef struct{
char name_sender;
char type_message;
t_message message;
} t_com_custom;
//check function
void checkActionSuccess(t_info_data*, int);
//tools functions
int newDir(t_info_data*);
t_coord orangeNewPos(t_coord, int);
int distance(t_coord,t_coord);
//jobs function
void searchJob(t_action*, t_info_data*, int);
void gatherFood(t_action*, t_info_data*, int);
t_action orange_update(void* my_info, t_com* communication_data, int success){
t_info_data* data = (t_info_data*)my_info;
//t_data* data = (t_data*)my_info;
t_action action;
if(data->hatched == 0){
success = 0;
data->hatched = 1;
data->job = GATHERER;
data->pos.x=0;
data->pos.y=0;
}
checkActionSuccess(data,success);
//searchJob(&action, data, success);
switch(data->job){
case JOBLESS:
action.type = WAIT;
action.dir=0;
break;
case MASTER:
printf("I'm the master!\n");
action.type = WAIT;
action.dir=0;
break;
case GATHERER:
gatherFood(&action,data,success);
break;
case WOODCUTTER:
case MINER:
break;
}
data->last_action = action.type;
data->last_dir = action.dir;
return action;
}
void checkActionSuccess(t_info_data* data, int success){
if (!success) return;
t_coord aim_pos;
switch(data->last_action){
case MOVE:
//update position
data->pos=orangeNewPos(data->pos, data->last_dir);
//update aim
if (getInventory() == -1){
if (distance(data->pos,data->res_spot.pos) == 0){
data->aim = NO_AIM;
}
}else{
aim_pos.x = 0;
aim_pos.y = 0;
if(distance(data->pos, aim_pos) == 1){
data->aim=AIM_SPOT;
}
}
break;
case ATTACK:
case PICK:
data->aim=AIM_SPAWN;
data->res_spot.type = getInventory();
data->res_spot.pos = data->pos;
break;
case PUT:
data->aim=AIM_SPOT;
break;
case WORK:
case WAIT:
case COMMUNICATE:
default:
break;
}
}
void searchJob(t_action* action, t_info_data* data, int success){
return;
}
void gatherFood(t_action* action, t_info_data* data, int success){
int i,t;
//default data for action
action->data = NULL;
//action choice
for(i=0;i<4;i++){
t = getNear(i);
switch(t){
case BERRIES:
action->type = WORK;
action->dir = i;
return;
case FOOD:
if(getInventory() == -1){
action->type = PICK;
action->dir = i;
return;
}
break;
case SPAWN:
if(getInventory() == FOOD){
action->type = PUT;
action->dir = i;
return;
}
break;
default:
break;
}
}
action->type= MOVE;
action->dir = newDir(data);
return;
}
//orange tools
int newDir(t_info_data* data){
int dir;
int current_dist,new_dist;
t_coord aim_pos;
switch(data->aim){
case NO_AIM:
return (data->last_dir + rand()%3 )%4;
case AIM_SPAWN:
aim_pos.x=0;
aim_pos.y=0;
break;
case AIM_SPOT:
aim_pos=data->res_spot.pos;
break;
}
// printf("[%d : %d] [%d : %d]",data->pos.x,data->pos.y,aim_pos.x,aim_pos.y);
current_dist = distance(data->pos,aim_pos);
if (current_dist == 0) return 0;
do{
dir= rand()%4;
new_dist = distance(orangeNewPos(data->pos,dir),aim_pos);
}while(current_dist <= new_dist);
if (getNear(dir) == DUDE || getNear(dir) == TREE || getNear(dir) == ROCK || getNear(dir) == IRON_ORE){
int offset = (rand()%3)-1;
dir+=offset;
dir%=4;
}
return dir;
}
t_coord orangeNewPos(t_coord pos, int dir){
t_coord new_pos = pos;
switch(dir){
case NORTH:
new_pos.y++;
break;
case SOUTH:
new_pos.y--;
break;
case EAST:
new_pos.x++;
break;
case WEST:
new_pos.x--;
break;
}
return new_pos;
}
int distance(t_coord p1, t_coord p2){
return abs(p2.x-p1.x) + abs(p2.y-p1.y);
}