half-implemented figthts

This commit is contained in:
anselme16 2015-01-19 16:56:41 +01:00
parent 0bda0053a9
commit f69fece27a
5 changed files with 65 additions and 17 deletions

23
main.c
View File

@ -8,12 +8,6 @@
#include <math.h>
#include <string.h>
typedef struct{
t_dude dude1;
t_dude dude2;
int attack;
} t_fight;
// temp code
t_action purple_update(void* my_info, void* com_data, int success);
t_action orange_update(void* my_info, void* com_data, int success);
@ -56,17 +50,17 @@ void initWorld(int width, int height){
generateImg(width, height);
}
void addFight(t_dude dude, t_dude other_dude){
}
void updateTeam(t_team team){
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++){
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(action, team.dudes+i);
handleAction(current_dude);
}
}
@ -143,9 +137,10 @@ void spawnDudes(){
}
}
void handleAction(t_action action, t_dude* dude){
void handleAction(t_dude* dude){
t_coord target_pos = getPos(dude->pos, action.dir);
t_pixel target = map[target_pos.x][target_pos.y];
t_action action = dude->action;
int* nb_res;
if(dude->com_data != NULL){
free(dude->com_data);
@ -286,6 +281,7 @@ void handleAction(t_action action, t_dude* dude){
break;
case COMMUNICATE :
switch(target.type){
// TODO : conflict possible if 2 dudes talk to the same dude
case DUDE :
action.com_data.flag = (action.dir+2)%4;
dude->com_data = malloc(sizeof(t_com));
@ -481,6 +477,7 @@ int MAIN
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_fights();
resolve_moves();
remaining_time = wait_time;

1
main.h
View File

@ -36,6 +36,7 @@ typedef struct{
int inventory;
int success;
t_pixel ground;
t_action action;
void* custom_data;
t_com* com_data;
} t_dude;

2
team.h
View File

@ -34,7 +34,7 @@ typedef struct{
enum{
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
FOOD, WOOD, STONE, IRON, SWORD, // resources
DUDE, // humans
DUDE, DEAD_DUDE // humans
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
};

54
tools.c
View File

@ -3,13 +3,17 @@
typedef struct{
t_dude* dude;
t_coord dst;
int ok;
} t_move;
typedef struct{
t_dude* dude;
t_dude* target;
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];
int nb_clear = 0;
t_move* occupiedMoves[MAX_DUDES*NB_TEAMS];
@ -19,13 +23,42 @@ void add_move(t_dude* dude, t_coord dst){
t_move* move = malloc(sizeof(t_move));
move->dude = dude;
move->dst = dst;
move->ok = 0;
if(map[dst.x][dst.y].type == DUDE)
occupiedMoves[nb_occupied++] = move;
else
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){
t_pixel target = map[move->dst.x][move->dst.y];
if( target.type == WALL
@ -49,6 +82,20 @@ void applyMove(t_move* move){
free(move);
}
void resolve_fights(){
int i;
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){
// perdu
}
free(fights[i]);
}
nb_fight = 0;
}
void resolve_moves(){
int change = 1;
int i;
@ -100,6 +147,7 @@ Uint32 getColor(t_pixel pixel){
case ROAD : return 0xEDB287;
case SWORD : return 0xEBEBEB;
case LIBRARY : return 0xA37A50;
case DEAD_DUDE : return 0xFF0000;
default : return 0x0000FF; // bleu absolu = bug
}

View File

@ -7,6 +7,8 @@
void add_move(t_dude* dude, t_coord dst);
void add_fight(t_dude* dude, t_coord dst);
void resolve_moves();
Uint32 getColor(t_pixel pixel);