implemented work, pick, put (not debugged yet)
This commit is contained in:
parent
f6016843ed
commit
6697095a87
121
main.c
121
main.c
@ -15,8 +15,8 @@ typedef struct{
|
|||||||
} t_fight;
|
} t_fight;
|
||||||
|
|
||||||
// temp code
|
// temp code
|
||||||
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_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);
|
||||||
int get_purple_size();
|
int get_purple_size();
|
||||||
int get_orange_size();
|
int get_orange_size();
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ void updateTeam(t_team team){
|
|||||||
int i;
|
int i;
|
||||||
t_action action;
|
t_action action;
|
||||||
for(i=0; i<team.nb_dudes; i++){
|
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);
|
handleAction(action, team.dudes+i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,6 +102,7 @@ void spawnDudes(){
|
|||||||
new_dude.pos = teams[i].spawn;
|
new_dude.pos = teams[i].spawn;
|
||||||
new_dude.team = teams[i].team;
|
new_dude.team = teams[i].team;
|
||||||
new_dude.inventory = -1;
|
new_dude.inventory = -1;
|
||||||
|
new_dude.success = 1;
|
||||||
new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y];
|
new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y];
|
||||||
new_dude.custom_data = malloc(teams[i].dude_size);
|
new_dude.custom_data = malloc(teams[i].dude_size);
|
||||||
memset(new_dude.custom_data, 0, 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){
|
void handleAction(t_action action, t_dude* dude){
|
||||||
t_coord target_pos = getPos(dude->pos, action.dir);
|
t_coord target_pos = getPos(dude->pos, action.dir);
|
||||||
t_pixel target = map[target_pos.x][target_pos.y];
|
t_pixel target = map[target_pos.x][target_pos.y];
|
||||||
|
int* nb_res;
|
||||||
switch(action.type){
|
switch(action.type){
|
||||||
case MOVE :
|
case MOVE :
|
||||||
|
dude->success = 0;
|
||||||
if( target.type != WALL
|
if( target.type != WALL
|
||||||
&& target.type != ROCK
|
&& target.type != ROCK
|
||||||
&& target.type != BEDROCK
|
&& target.type != BEDROCK
|
||||||
@ -127,45 +130,102 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
add_move(dude, target_pos);
|
add_move(dude, target_pos);
|
||||||
break;
|
break;
|
||||||
case ATTACK :
|
case ATTACK :
|
||||||
printf("forbidden action\n");
|
printf("forbidden action\n"); // TODO : implement that
|
||||||
//if(target.type == DUDE)
|
//if(target.type == DUDE)
|
||||||
// addFight(dude, *((t_dude*)(target.data)));
|
// addFight(dude, *((t_dude*)(target.data)));
|
||||||
break;
|
break;
|
||||||
case PICK :
|
case PICK :
|
||||||
printf("forbidden action\n");
|
nb_res = target.data;
|
||||||
// if target is resource :
|
if(target.type >= FOOD && target.type <= SWORD && dude->inventory == -1){
|
||||||
// put target in inventory
|
dude->inventory = target.type;
|
||||||
// put grass on target
|
(*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;
|
break;
|
||||||
case PUT :
|
case PUT :
|
||||||
printf("forbidden action\n");
|
if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){
|
||||||
// if target is grass or road or corpse
|
if(target.type == GRASS){
|
||||||
// target = inventory
|
map[target_pos.x][target_pos.y].type = dude->inventory;
|
||||||
// inventory = NULL
|
nb_res = malloc(sizeof(int));
|
||||||
// else if target is spawn and inventory is food
|
*nb_res = 1;
|
||||||
// spawn.food ++
|
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;
|
break;
|
||||||
case WORK :
|
case WORK :
|
||||||
printf("forbidden action\n");
|
dude->success = 1;
|
||||||
// switch target
|
switch(target.type){
|
||||||
// case rock -> stone
|
case ROCK :
|
||||||
// case berries -> food
|
map[target_pos.x][target_pos.y].type = STONE;
|
||||||
// case tree -> wood
|
nb_res = malloc(sizeof(int));
|
||||||
// case grass -> road
|
*nb_res = 1;
|
||||||
// case road -> grass
|
map[target_pos.x][target_pos.y].data = nb_res;
|
||||||
// case stone -> wall
|
break;
|
||||||
// case wood -> sign
|
case BERRIES :
|
||||||
// case sign -> wood
|
map[target_pos.x][target_pos.y].type = FOOD;
|
||||||
// case iron_ore -> iron
|
nb_res = malloc(sizeof(int));
|
||||||
// case iron -> sword
|
*nb_res = 1;
|
||||||
// case corpse -> grass
|
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;
|
break;
|
||||||
case WAIT :
|
case WAIT :
|
||||||
printf("forbidden action\n");
|
dude->success = 1;
|
||||||
// ...
|
|
||||||
break;
|
break;
|
||||||
case COMMUNICATE :
|
case COMMUNICATE :
|
||||||
printf("forbidden action\n");
|
printf("forbidden action\n"); // TODO : implement that
|
||||||
// if target is sign -> set sign message
|
// if target is sign -> set sign message
|
||||||
// if target is dude -> sent message to dude
|
// if target is dude -> sent message to dude
|
||||||
break;
|
break;
|
||||||
@ -228,6 +288,7 @@ int MAIN
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
argv++;
|
argv++;
|
||||||
|
argc--;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
4
main.h
4
main.h
@ -12,6 +12,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAX_DUDES 50
|
#define MAX_DUDES 50
|
||||||
|
#define STACK_SIZE 5
|
||||||
#define DEFAULT_WIDTH 400
|
#define DEFAULT_WIDTH 400
|
||||||
#define DEFAULT_HEIGHT 250
|
#define DEFAULT_HEIGHT 250
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ typedef struct{
|
|||||||
t_coord pos;
|
t_coord pos;
|
||||||
int team;
|
int team;
|
||||||
int inventory;
|
int inventory;
|
||||||
|
int success;
|
||||||
t_pixel ground;
|
t_pixel ground;
|
||||||
void* custom_data;
|
void* custom_data;
|
||||||
void* com_data;
|
void* com_data;
|
||||||
@ -37,7 +39,7 @@ typedef struct{
|
|||||||
typedef struct{
|
typedef struct{
|
||||||
int team;
|
int team;
|
||||||
int dude_size;
|
int dude_size;
|
||||||
t_action (*update)(void*, void*, int);
|
t_action (*update)(void*, void*, int, int);
|
||||||
int nb_dudes;
|
int nb_dudes;
|
||||||
t_dude* dudes;
|
t_dude* dudes;
|
||||||
t_coord spawn;
|
t_coord spawn;
|
||||||
|
2
orange.c
2
orange.c
@ -11,7 +11,7 @@ int get_orange_size(){
|
|||||||
return sizeof(t_data);
|
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_data* data = (t_data*)my_info;
|
||||||
t_action action;
|
t_action action;
|
||||||
|
|
||||||
|
3
purple.c
3
purple.c
@ -11,9 +11,10 @@ int get_purple_size(){
|
|||||||
return sizeof(t_data);
|
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_data* data = (t_data*)my_info;
|
||||||
t_action action;
|
t_action action;
|
||||||
|
int i;
|
||||||
|
|
||||||
action.type = MOVE;
|
action.type = MOVE;
|
||||||
action.dir = rand()%4;
|
action.dir = rand()%4;
|
||||||
|
6
team.h
6
team.h
@ -9,9 +9,9 @@ enum{
|
|||||||
// Tile types
|
// Tile types
|
||||||
enum{
|
enum{
|
||||||
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
||||||
FOOD, WOOD, STONE, IRON, // resources
|
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
||||||
CORPSE, DUDE, // humans
|
DUDE, // humans
|
||||||
SPAWN, WALL, ROAD, SWORD, SIGN // buildings
|
SPAWN, WALL, ROAD, SIGN // buildings
|
||||||
};
|
};
|
||||||
|
|
||||||
// Action types
|
// Action types
|
||||||
|
3
tools.c
3
tools.c
@ -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]));
|
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.x = move->dst.x;
|
||||||
move->dude->pos.y = move->dst.y;
|
move->dude->pos.y = move->dst.y;
|
||||||
|
move->dude->success = 1;
|
||||||
|
free(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
void resolve_moves(){
|
void resolve_moves(){
|
||||||
@ -74,7 +76,6 @@ Uint32 getColor(t_pixel pixel){
|
|||||||
case WOOD : return 0x634A22;
|
case WOOD : return 0x634A22;
|
||||||
case STONE : return 0x454545;
|
case STONE : return 0x454545;
|
||||||
case IRON : return 0x4A4036;
|
case IRON : return 0x4A4036;
|
||||||
case CORPSE : return 0xFF0000;
|
|
||||||
case DUDE :
|
case DUDE :
|
||||||
dudeData = pixel.data;
|
dudeData = pixel.data;
|
||||||
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
|
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user