321 lines
6.6 KiB
C
321 lines
6.6 KiB
C
#include "team.h"
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
|
|
// Development in progress
|
|
|
|
//Structure definition
|
|
|
|
enum{
|
|
O_JOBLESS, O_MASTER, O_GATHERER, O_WOODCUTTER, O_MINER, O_NB_JOBS
|
|
};
|
|
|
|
enum{
|
|
O_NO_AIM, O_AIM_MASTER, O_AIM_SPAWN, O_AIM_SPOT
|
|
};
|
|
|
|
#define ORANGE_SPAWN_X 0
|
|
#define ORANGE_SPAWN_Y 0
|
|
|
|
#define ORANGE_MASTER_X 3
|
|
#define ORANGE_MASTER_Y 3
|
|
|
|
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 age;
|
|
char job;
|
|
char aim;
|
|
char talking;
|
|
char listening;
|
|
t_orange_ressource_spot res_spot;
|
|
char last_action;
|
|
char last_dir;
|
|
} t_orange_info;
|
|
|
|
//tools functions
|
|
int orangeNewDir(t_orange_info*);
|
|
t_coord orangeNewPos(t_coord, int);
|
|
int orangeDistance(t_coord, int, int);
|
|
//action function
|
|
void orangeApplyAction(t_orange_info*,t_com*);
|
|
void orangeMoveEffect(t_orange_info*);
|
|
void orangePickEffect(t_orange_info*);
|
|
void orangePutEffect(t_orange_info*);
|
|
void orangeCommunicateEffect(t_orange_info*);
|
|
void orangeWaitEffect(t_orange_info*, t_com*);
|
|
//jobs function
|
|
void orangeHatched(t_orange_info*);
|
|
|
|
void orangeJobless(t_action*, t_orange_info*, t_com*, int);
|
|
void orangeMaster(t_action*, t_orange_info*, t_com*, int);
|
|
void orangeGatherer(t_action*, t_orange_info*, t_com*, int);
|
|
|
|
t_action orange_update(void* my_info, t_com* com_data, int success){
|
|
t_orange_info* data = (t_orange_info*)my_info;
|
|
t_action action;
|
|
|
|
if(data->age == 0){
|
|
orangeHatched(data);
|
|
success = 0;
|
|
}
|
|
|
|
if (!success){
|
|
action.type = data->last_action;
|
|
action.dir = data->last_dir;
|
|
return action;
|
|
}
|
|
|
|
orangeApplyAction(data,com_data);
|
|
|
|
switch(data->job){
|
|
case O_JOBLESS:
|
|
orangeJobless(&action, data, com_data, success);
|
|
break;
|
|
case O_MASTER:
|
|
orangeMaster(&action, data, com_data, success);
|
|
break;
|
|
case O_GATHERER:
|
|
orangeGatherer(&action,data, com_data, success);
|
|
break;
|
|
case O_WOODCUTTER:
|
|
case O_MINER:
|
|
break;
|
|
}
|
|
data->last_action = action.type;
|
|
data->last_dir = action.dir;
|
|
return action;
|
|
}
|
|
|
|
void orangeHatched(t_orange_info* data){
|
|
data->age++;
|
|
data->job = O_JOBLESS;
|
|
data->aim = O_AIM_MASTER;
|
|
data->pos.x=0;
|
|
data->pos.y=0;
|
|
}
|
|
|
|
void orangeApplyAction(t_orange_info* data,t_com* com_data){
|
|
switch(data->last_action){
|
|
case MOVE:
|
|
data->pos=orangeNewPos(data->pos, data->last_dir);
|
|
orangeMoveEffect(data);
|
|
break;
|
|
case PICK:
|
|
orangePickEffect(data);
|
|
break;
|
|
case PUT:
|
|
orangePutEffect(data);
|
|
break;
|
|
case COMMUNICATE:
|
|
orangeCommunicateEffect(data);
|
|
break;
|
|
case WAIT:
|
|
orangeWaitEffect(data,com_data);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void orangeMoveEffect(t_orange_info* data){
|
|
int dist_to_master;
|
|
switch(data->job){
|
|
case O_JOBLESS:
|
|
dist_to_master = orangeDistance(data->pos,ORANGE_MASTER_X,ORANGE_MASTER_Y);
|
|
if(dist_to_master == 0){
|
|
data->job=O_MASTER;
|
|
data->listening = 1;
|
|
} else if(dist_to_master == 1)
|
|
data->talking = 1;
|
|
break;
|
|
case O_GATHERER:
|
|
if (getInventory() == -1){
|
|
if (orangeDistance(data->pos,data->res_spot.pos.x, data->res_spot.pos.y) == 0){
|
|
data->aim = O_NO_AIM;
|
|
}
|
|
}else{
|
|
if(orangeDistance(data->pos, ORANGE_SPAWN_X, ORANGE_SPAWN_Y) == 1){
|
|
data->aim=O_AIM_SPOT;
|
|
}
|
|
}
|
|
break;
|
|
case O_WOODCUTTER:
|
|
case O_MINER:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
void orangePickEffect(t_orange_info* data){
|
|
switch(data->job){
|
|
case O_GATHERER:
|
|
data->aim=O_AIM_SPAWN;
|
|
data->res_spot.type = getInventory();
|
|
data->res_spot.pos = data->pos;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void orangePutEffect(t_orange_info* data){
|
|
switch(data->job){
|
|
case O_GATHERER:
|
|
data->aim=O_AIM_SPOT;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void orangeCommunicateEffect(t_orange_info* data){
|
|
data->listening = 1;
|
|
}
|
|
|
|
void orangeWaitEffect(t_orange_info* data, t_com* com_data){
|
|
switch(data->job){
|
|
case O_JOBLESS:
|
|
if ((data->listening == 1) && (com_data != NULL)){
|
|
if (com_data->data[0] == 1){
|
|
data->job = com_data->data[1];
|
|
data->listening = 0;
|
|
}
|
|
}
|
|
break;
|
|
case O_MASTER:
|
|
if ((data->listening == 1) && (com_data != NULL)){
|
|
if(com_data->data[0] == 1)
|
|
data->talking = 1;
|
|
}else{
|
|
data->talking = 0;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// job functions
|
|
void orangeJobless(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
|
int dir_to_master;
|
|
|
|
dir_to_master = orangeNewDir(data);
|
|
|
|
if(data->listening){
|
|
action->type=WAIT;
|
|
action->dir=0;
|
|
return;
|
|
}
|
|
if (data->talking){
|
|
if (getNear(dir_to_master) == DUDE){
|
|
action->type = COMMUNICATE;
|
|
action->dir = dir_to_master;
|
|
action->com_data.data[0]=1;
|
|
return;
|
|
}
|
|
}
|
|
action->type = MOVE;
|
|
action->dir = dir_to_master;
|
|
return;
|
|
}
|
|
|
|
void orangeMaster(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
|
if (data->talking){
|
|
action->type = COMMUNICATE;
|
|
action->dir = com_data->flag;
|
|
action->com_data.data[0] = 1;
|
|
action->com_data.data[1] = O_GATHERER;
|
|
}else{
|
|
action->type = WAIT;
|
|
action->dir=0;
|
|
}
|
|
}
|
|
|
|
void orangeGatherer(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
|
int i,t;
|
|
printf("gatherer\n");
|
|
//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_orange_info* data){
|
|
int dir;
|
|
int current_dist,new_dist;
|
|
int x,y;
|
|
switch(data->aim){
|
|
case O_NO_AIM:
|
|
return (data->last_dir + rand()%3 )%4;
|
|
case O_AIM_MASTER:
|
|
x = ORANGE_MASTER_X;
|
|
y = ORANGE_MASTER_Y;
|
|
break;
|
|
case O_AIM_SPAWN:
|
|
x = ORANGE_SPAWN_X;
|
|
y = ORANGE_SPAWN_Y;
|
|
break;
|
|
case O_AIM_SPOT:
|
|
x = data->res_spot.pos.x;
|
|
y = data->res_spot.pos.y;
|
|
break;
|
|
}
|
|
current_dist = orangeDistance(data->pos,x,y);
|
|
if (current_dist == 0) return 0;
|
|
do{
|
|
dir= rand()%4;
|
|
new_dist = orangeDistance(orangeNewPos(data->pos,dir),x,y);
|
|
}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 c, int x, int y){
|
|
return abs(x-c.x) + abs(y-c.y);
|
|
} |