Merge branch 'master' of https://git.tagachop.fr/epicsparrow/PixelWars
This commit is contained in:
commit
0d3143773d
58
main.c
58
main.c
@ -8,12 +8,6 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
t_dude dude1;
|
|
||||||
t_dude dude2;
|
|
||||||
int attack;
|
|
||||||
} t_fight;
|
|
||||||
|
|
||||||
// temp code
|
// temp code
|
||||||
t_action purple_update(void* my_info, void* com_data, int success);
|
t_action purple_update(void* my_info, void* com_data, int success);
|
||||||
t_action orange_update(void* my_info, void* com_data, int success);
|
t_action orange_update(void* my_info, void* com_data, int success);
|
||||||
@ -56,17 +50,16 @@ void initWorld(int width, int height){
|
|||||||
generateImg(width, height);
|
generateImg(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addFight(t_dude dude, t_dude other_dude){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateTeam(t_team team){
|
void updateTeam(t_team team){
|
||||||
int i;
|
int i;
|
||||||
t_action action;
|
|
||||||
|
for(i=0; i<team.nb_dudes; i++){
|
||||||
|
team.dudes[i].action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), team.dudes[i].success);
|
||||||
|
}
|
||||||
|
|
||||||
for(i=0; i<team.nb_dudes; i++){
|
for(i=0; i<team.nb_dudes; i++){
|
||||||
current_dude = team.dudes + i;
|
current_dude = team.dudes + i;
|
||||||
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), team.dudes[i].success);
|
handleAction(current_dude);
|
||||||
handleAction(action, team.dudes+i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,6 +126,7 @@ void spawnDudes(){
|
|||||||
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.success = 1;
|
||||||
|
new_dude.dead = 0;
|
||||||
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(DUDE_MEMORY);
|
new_dude.custom_data = malloc(DUDE_MEMORY);
|
||||||
memset(new_dude.custom_data, 0, DUDE_MEMORY);
|
memset(new_dude.custom_data, 0, DUDE_MEMORY);
|
||||||
@ -143,7 +137,8 @@ void spawnDudes(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleAction(t_action action, t_dude* dude){
|
void handleAction(t_dude* dude){
|
||||||
|
t_action action = dude->action;
|
||||||
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;
|
int* nb_res;
|
||||||
@ -164,9 +159,28 @@ 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"); // TODO : implement that
|
dude->success = 0;
|
||||||
//if(target.type == DUDE)
|
if( target.type == DUDE
|
||||||
// addFight(dude, *((t_dude*)(target.data)));
|
|| target.type == SPAWN
|
||||||
|
|| target.type == WALL
|
||||||
|
|| target.type == LIBRARY
|
||||||
|
|| target.type == ROAD
|
||||||
|
){
|
||||||
|
dude->success = 1;
|
||||||
|
if(target.type == DUDE)
|
||||||
|
add_fight(dude, target_pos);
|
||||||
|
else{
|
||||||
|
if(target.type == SPAWN){
|
||||||
|
nb_res = target.data;
|
||||||
|
teams[*nb_res].spawn_food = 0;
|
||||||
|
teams[*nb_res].spawn_count = 0;
|
||||||
|
}
|
||||||
|
if(target.data != NULL)
|
||||||
|
free(target.data);
|
||||||
|
map[target_pos.x][target_pos.y].type = GRASS;
|
||||||
|
map[target_pos.x][target_pos.y].data = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PICK :
|
case PICK :
|
||||||
nb_res = target.data;
|
nb_res = target.data;
|
||||||
@ -286,18 +300,27 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
break;
|
break;
|
||||||
case COMMUNICATE :
|
case COMMUNICATE :
|
||||||
switch(target.type){
|
switch(target.type){
|
||||||
|
// TODO : conflicts are not handled in a fair way
|
||||||
case DUDE :
|
case DUDE :
|
||||||
action.com_data.flag = (action.dir+2)%4;
|
action.com_data.flag = (action.dir+2)%4;
|
||||||
|
if(dude->com_data == NULL){
|
||||||
dude->com_data = malloc(sizeof(t_com));
|
dude->com_data = malloc(sizeof(t_com));
|
||||||
*(dude->com_data) = action.com_data;
|
*(dude->com_data) = action.com_data;
|
||||||
dude->success = 1;
|
dude->success = 1;
|
||||||
|
}else
|
||||||
|
dude->success = 0;
|
||||||
break;
|
break;
|
||||||
case LIBRARY :
|
case LIBRARY :
|
||||||
if(action.com_data.flag & READ){
|
if(action.com_data.flag & READ){
|
||||||
|
if(dude->com_data == NULL){
|
||||||
action.com_data.flag = action.dir;
|
action.com_data.flag = action.dir;
|
||||||
dude->com_data = malloc(sizeof(t_com));
|
dude->com_data = malloc(sizeof(t_com));
|
||||||
*(dude->com_data) = action.com_data;
|
*(dude->com_data) = action.com_data;
|
||||||
memcpy(dude->com_data + sizeof(int), target.data + (action.com_data.flag | 3), 32);
|
memcpy(dude->com_data + sizeof(int), target.data + (action.com_data.flag | 3), 32);
|
||||||
|
}else{
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
memcpy(target.data + action.com_data.flag, action.com_data.data, 32);
|
memcpy(target.data + action.com_data.flag, action.com_data.data, 32);
|
||||||
}
|
}
|
||||||
@ -481,6 +504,7 @@ int MAIN
|
|||||||
spawnDudes();
|
spawnDudes();
|
||||||
for(i=0; i<NB_TEAMS; i++)
|
for(i=0; i<NB_TEAMS; i++)
|
||||||
updateTeam(teams[i]);
|
updateTeam(teams[i]);
|
||||||
|
// resolve_fights(); // TODO : debugging
|
||||||
resolve_moves();
|
resolve_moves();
|
||||||
|
|
||||||
remaining_time = wait_time;
|
remaining_time = wait_time;
|
||||||
|
4
main.h
4
main.h
@ -35,7 +35,9 @@ typedef struct{
|
|||||||
int team;
|
int team;
|
||||||
int inventory;
|
int inventory;
|
||||||
int success;
|
int success;
|
||||||
|
int dead;
|
||||||
t_pixel ground;
|
t_pixel ground;
|
||||||
|
t_action action;
|
||||||
void* custom_data;
|
void* custom_data;
|
||||||
t_com* com_data;
|
t_com* com_data;
|
||||||
} t_dude;
|
} t_dude;
|
||||||
@ -52,7 +54,7 @@ typedef struct{
|
|||||||
|
|
||||||
void generateImg();
|
void generateImg();
|
||||||
|
|
||||||
void handleAction(t_action action, t_dude* dude);
|
void handleAction(t_dude* dude);
|
||||||
|
|
||||||
// variables globales
|
// variables globales
|
||||||
t_pixel** map;
|
t_pixel** map;
|
||||||
|
2
team.h
2
team.h
@ -34,7 +34,7 @@ typedef struct{
|
|||||||
enum{
|
enum{
|
||||||
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
||||||
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
||||||
DUDE, // humans
|
DUDE, DEAD_DUDE, // humans
|
||||||
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
|
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
|
||||||
};
|
};
|
||||||
|
|
||||||
|
65
tools.c
65
tools.c
@ -3,13 +3,17 @@
|
|||||||
typedef struct{
|
typedef struct{
|
||||||
t_dude* dude;
|
t_dude* dude;
|
||||||
t_coord dst;
|
t_coord dst;
|
||||||
int ok;
|
|
||||||
} t_move;
|
} t_move;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
|
t_dude* dude;
|
||||||
|
t_dude* target;
|
||||||
int dmg;
|
int dmg;
|
||||||
} t_attack;
|
int proba;
|
||||||
|
} t_fight;
|
||||||
|
|
||||||
|
t_fight* fights[MAX_DUDES*NB_TEAMS];
|
||||||
|
int nb_fight = 0;
|
||||||
t_move* clearMoves[MAX_DUDES*NB_TEAMS];
|
t_move* clearMoves[MAX_DUDES*NB_TEAMS];
|
||||||
int nb_clear = 0;
|
int nb_clear = 0;
|
||||||
t_move* occupiedMoves[MAX_DUDES*NB_TEAMS];
|
t_move* occupiedMoves[MAX_DUDES*NB_TEAMS];
|
||||||
@ -19,16 +23,46 @@ void add_move(t_dude* dude, t_coord dst){
|
|||||||
t_move* move = malloc(sizeof(t_move));
|
t_move* move = malloc(sizeof(t_move));
|
||||||
move->dude = dude;
|
move->dude = dude;
|
||||||
move->dst = dst;
|
move->dst = dst;
|
||||||
move->ok = 0;
|
|
||||||
if(map[dst.x][dst.y].type == DUDE)
|
if(map[dst.x][dst.y].type == DUDE)
|
||||||
occupiedMoves[nb_occupied++] = move;
|
occupiedMoves[nb_occupied++] = move;
|
||||||
else
|
else
|
||||||
clearMoves[nb_clear++] = move;
|
clearMoves[nb_clear++] = move;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void add_fight(t_dude* dude, t_coord dst){
|
||||||
|
int i;
|
||||||
|
t_pixel target = map[dst.x][dst.y];
|
||||||
|
t_fight* fight;
|
||||||
|
if(target.type == DUDE){
|
||||||
|
t_dude* dude2 = target.data;
|
||||||
|
for(i=0; i<nb_fight; i++){
|
||||||
|
if(fights[i]->target == dude){
|
||||||
|
fight = malloc(sizeof(t_fight));
|
||||||
|
fight->dude = dude;
|
||||||
|
fight->target = dude2;
|
||||||
|
fight->dmg = 1 + 3*(dude->inventory == SWORD);
|
||||||
|
fight->proba = 99 - fights[i]->proba;
|
||||||
|
fights[nb_fight++] = fight;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(fights[i]->target == dude2){
|
||||||
|
fights[i]->dmg += 1 + 3*(dude->inventory == SWORD);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fight = malloc(sizeof(t_fight));
|
||||||
|
fight->dude = dude;
|
||||||
|
fight->target = dude2;
|
||||||
|
fight->dmg = 1 + 3*(dude->inventory == SWORD);
|
||||||
|
fight->proba = rand()%100;
|
||||||
|
fights[nb_fight++] = fight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void applyMove(t_move* move){
|
void applyMove(t_move* move){
|
||||||
t_pixel target = map[move->dst.x][move->dst.y];
|
t_pixel target = map[move->dst.x][move->dst.y];
|
||||||
if( target.type == WALL
|
if( move->dude->dead
|
||||||
|
|| target.type == WALL
|
||||||
|| target.type == ROCK
|
|| target.type == ROCK
|
||||||
|| target.type == BEDROCK
|
|| target.type == BEDROCK
|
||||||
|| target.type == IRON_ORE
|
|| target.type == IRON_ORE
|
||||||
@ -49,6 +83,28 @@ void applyMove(t_move* move){
|
|||||||
free(move);
|
free(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void resolve_fights(){
|
||||||
|
int i, j;
|
||||||
|
int defense = 0;
|
||||||
|
for(i=0; i<nb_fight; i++){
|
||||||
|
int type = fights[i]->dude->action.type;
|
||||||
|
defense = (type == MOVE || type == ATTACK)*(1 + 3*(fights[i]->dude->inventory == SWORD));
|
||||||
|
if((defense*100)/(defense + fights[i]->dmg) < fights[i]->proba){
|
||||||
|
t_dude* target = fights[i]->target;
|
||||||
|
t_team team = teams[target->team];
|
||||||
|
for(j=0; j<team.nb_dudes; j++){
|
||||||
|
if(team.dudes + j == target){
|
||||||
|
team.dudes[j] = team.dudes[--team.nb_dudes];
|
||||||
|
map[target->pos.x][target->pos.y].type = DEAD_DUDE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(fights[i]);
|
||||||
|
}
|
||||||
|
nb_fight = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void resolve_moves(){
|
void resolve_moves(){
|
||||||
int change = 1;
|
int change = 1;
|
||||||
int i;
|
int i;
|
||||||
@ -100,6 +156,7 @@ Uint32 getColor(t_pixel pixel){
|
|||||||
case ROAD : return 0xEDB287;
|
case ROAD : return 0xEDB287;
|
||||||
case SWORD : return 0xEBEBEB;
|
case SWORD : return 0xEBEBEB;
|
||||||
case LIBRARY : return 0xA37A50;
|
case LIBRARY : return 0xA37A50;
|
||||||
|
case DEAD_DUDE : return 0xFF0000;
|
||||||
default : return 0x0000FF; // bleu absolu = bug
|
default : return 0x0000FF; // bleu absolu = bug
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user