PixelWars/orange.c

217 lines
4.2 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_orange_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_orange_ressource_spot res_spot;
char last_action;
char last_dir;
} t_orange_info_data;
typedef union{
t_orange_ressource_spot res_spot;
}t_orange_message;
typedef struct{
char name_sender;
char type_message;
t_orange_message message;
} t_orange_com;
//check function
void orangeCheckActionSuccess(t_info_data*, int);
//tools functions
int orangeNewDir(t_info_data*);
t_coord orangeNewPos(t_coord, int);
int orangeDistance(t_coord,t_coord);
//jobs function
void orangeSearchJob(t_action*, t_info_data*, int);
void orangeGatherFood(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;
}
orangeCheckActionSuccess(data,success);
//orangeSearchJob(&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:
orangeGatherFood(&action,data,success);
break;
case WOODCUTTER:
case MINER:
break;
}
data->last_action = action.type;
data->last_dir = action.dir;
return action;
}
void orangeCheckActionSuccess(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 (orangeDistance(data->pos,data->res_spot.pos) == 0){
data->aim = NO_AIM;
}
}else{
aim_pos.x = 0;
aim_pos.y = 0;
if(orangeDistance(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;
}
}
// job functions
void orangeSearchJob(t_action* action, t_info_data* data, int success){
return;
}
void orangeGatherFood(t_action* action, t_info_data* data, int success){
int i,t;
//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 = orangeNewDir(data);
return;
}
//orange tools
int orangeNewDir(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;
}
current_dist = orangeDistance(data->pos,aim_pos);
if (current_dist == 0) return 0;
do{
dir= rand()%4;
new_dist = orangeDistance(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 orangeDistance(t_coord p1, t_coord p2){
return abs(p2.x-p1.x) + abs(p2.y-p1.y);
}