implemented work, pick, put (not debugged yet)

This commit is contained in:
anselme16 2015-01-14 11:57:27 +01:00
parent f6016843ed
commit 6697095a87
6 changed files with 102 additions and 37 deletions

121
main.c
View File

@ -15,8 +15,8 @@ typedef struct{
} t_fight;
// temp code
t_action purple_update(void* my_info, void* com_data, int my_id);
t_action orange_update(void* my_info, void* com_data, int my_id);
t_action purple_update(void* my_info, void* com_data, int my_id, int success);
t_action orange_update(void* my_info, void* com_data, int my_id, int success);
int get_purple_size();
int get_orange_size();
@ -66,7 +66,7 @@ void updateTeam(t_team team){
int i;
t_action action;
for(i=0; i<team.nb_dudes; i++){
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i);
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i, team.dudes[i].success);
handleAction(action, team.dudes+i);
}
}
@ -102,6 +102,7 @@ void spawnDudes(){
new_dude.pos = teams[i].spawn;
new_dude.team = teams[i].team;
new_dude.inventory = -1;
new_dude.success = 1;
new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y];
new_dude.custom_data = malloc(teams[i].dude_size);
memset(new_dude.custom_data, 0, teams[i].dude_size);
@ -115,8 +116,10 @@ void spawnDudes(){
void handleAction(t_action action, t_dude* dude){
t_coord target_pos = getPos(dude->pos, action.dir);
t_pixel target = map[target_pos.x][target_pos.y];
int* nb_res;
switch(action.type){
case MOVE :
dude->success = 0;
if( target.type != WALL
&& target.type != ROCK
&& target.type != BEDROCK
@ -127,45 +130,102 @@ void handleAction(t_action action, t_dude* dude){
add_move(dude, target_pos);
break;
case ATTACK :
printf("forbidden action\n");
printf("forbidden action\n"); // TODO : implement that
//if(target.type == DUDE)
// addFight(dude, *((t_dude*)(target.data)));
break;
case PICK :
printf("forbidden action\n");
// if target is resource :
// put target in inventory
// put grass on target
nb_res = target.data;
if(target.type >= FOOD && target.type <= SWORD && dude->inventory == -1){
dude->inventory = target.type;
(*nb_res)--;
if(*nb_res < 1){
free(nb_res);
map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL;
}
}else
dude->success = 0;
break;
case PUT :
printf("forbidden action\n");
// if target is grass or road or corpse
// target = inventory
// inventory = NULL
// else if target is spawn and inventory is food
// spawn.food ++
if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){
if(target.type == GRASS){
map[target_pos.x][target_pos.y].type = dude->inventory;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
}else{
nb_res = target.data;
(*nb_res)++;
}
dude->inventory = -1;
}else
dude->success = 0;
break;
case WORK :
printf("forbidden action\n");
// switch target
// case rock -> stone
// case berries -> food
// case tree -> wood
// case grass -> road
// case road -> grass
// case stone -> wall
// case wood -> sign
// case sign -> wood
// case iron_ore -> iron
// case iron -> sword
// case corpse -> grass
dude->success = 1;
switch(target.type){
case ROCK :
map[target_pos.x][target_pos.y].type = STONE;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case BERRIES :
map[target_pos.x][target_pos.y].type = FOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case TREE :
map[target_pos.x][target_pos.y].type = WOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case IRON_ORE :
map[target_pos.x][target_pos.y].type = IRON;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case GRASS :
map[target_pos.x][target_pos.y].type = ROAD;
map[target_pos.x][target_pos.y].data = NULL;
break;
case ROAD :
map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL;
break;
case STONE :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else{
free(target.data);
map[target_pos.x][target_pos.y].type = WALL;
map[target_pos.x][target_pos.y].data = NULL;
}
break;
case IRON :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else
map[target_pos.x][target_pos.y].type = SWORD;
break;
// TODO : case wood -> sign
// TODO : case sign -> wood
default :
dude->success = 0;
break;
}
break;
case WAIT :
printf("forbidden action\n");
// ...
dude->success = 1;
break;
case COMMUNICATE :
printf("forbidden action\n");
printf("forbidden action\n"); // TODO : implement that
// if target is sign -> set sign message
// if target is dude -> sent message to dude
break;
@ -228,6 +288,7 @@ int MAIN
break;
}
argv++;
argc--;
}
#endif

4
main.h
View File

@ -12,6 +12,7 @@
#endif
#define MAX_DUDES 50
#define STACK_SIZE 5
#define DEFAULT_WIDTH 400
#define DEFAULT_HEIGHT 250
@ -29,6 +30,7 @@ typedef struct{
t_coord pos;
int team;
int inventory;
int success;
t_pixel ground;
void* custom_data;
void* com_data;
@ -37,7 +39,7 @@ typedef struct{
typedef struct{
int team;
int dude_size;
t_action (*update)(void*, void*, int);
t_action (*update)(void*, void*, int, int);
int nb_dudes;
t_dude* dudes;
t_coord spawn;

View File

@ -11,7 +11,7 @@ int get_orange_size(){
return sizeof(t_data);
}
t_action orange_update(void* my_info, void* com_data, int my_id){
t_action orange_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info;
t_action action;

View File

@ -11,9 +11,10 @@ int get_purple_size(){
return sizeof(t_data);
}
t_action purple_update(void* my_info, void* com_data, int my_id){
t_action purple_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info;
t_action action;
int i;
action.type = MOVE;
action.dir = rand()%4;

6
team.h
View File

@ -9,9 +9,9 @@ enum{
// Tile types
enum{
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
FOOD, WOOD, STONE, IRON, // resources
CORPSE, DUDE, // humans
SPAWN, WALL, ROAD, SWORD, SIGN // buildings
FOOD, WOOD, STONE, IRON, SWORD, // resources
DUDE, // humans
SPAWN, WALL, ROAD, SIGN // buildings
};
// Action types

View File

@ -32,6 +32,8 @@ void applyMove(t_move* move){
putpixel(img, move->dude->pos.x, move->dude->pos.y, getColor(map[move->dude->pos.x][move->dude->pos.y]));
move->dude->pos.x = move->dst.x;
move->dude->pos.y = move->dst.y;
move->dude->success = 1;
free(move);
}
void resolve_moves(){
@ -74,7 +76,6 @@ Uint32 getColor(t_pixel pixel){
case WOOD : return 0x634A22;
case STONE : return 0x454545;
case IRON : return 0x4A4036;
case CORPSE : return 0xFF0000;
case DUDE :
dudeData = pixel.data;
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;