2016-05-16 00:34:44 +02:00

213 lines
5.3 KiB
C

#include "tools.h"
typedef struct{
t_dude* dude;
t_coord dst;
} t_move;
typedef struct{
t_dude* dude;
t_dude* target;
int dmg;
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];
int nb_occupied = 0;
void add_move(t_dude* dude, t_coord dst){
t_move* move = malloc(sizeof(t_move));
move->dude = dude;
move->dst = dst;
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( move->dude->dead
|| target.type == WALL
|| target.type == ROCK
|| target.type == BEDROCK
|| target.type == IRON_ORE
|| target.type == TREE
|| target.type == DUDE
|| target.type == LIBRARY){
free(move);
}
move->dude->success = 1;
map[move->dude->pos.x][move->dude->pos.y] = move->dude->ground; // set the ground where the dude was
move->dude->ground = target; // set the ground of the dude to where he goes
map[move->dst.x][move->dst.y].type = DUDE; // set the target tile with the dude
map[move->dst.x][move->dst.y].data = move->dude;
putpixel(img, move->dst.x, move->dst.y, getColor(map[move->dst.x][move->dst.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.y = move->dst.y;
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(){
int change = 1;
int i;
// clear moves
while(nb_clear > 0){
i = rand()%nb_clear;
if(map[clearMoves[i]->dst.x][clearMoves[i]->dst.y].type == DUDE){
occupiedMoves[nb_occupied++] = clearMoves[i];
}else{
applyMove(clearMoves[i]);
}
clearMoves[i] = clearMoves[--nb_clear];
}
// occupied moves
while(change){
change = 0;
for(i=0; i<nb_occupied; i++){
if(map[occupiedMoves[i]->dst.x][occupiedMoves[i]->dst.y].type != DUDE){
change = 1;
applyMove(occupiedMoves[i]);
}
}
}
nb_occupied = 0;
}
Uint32 getColor(t_pixel pixel){
t_dude* dudeData;
int* spawnData;
switch(pixel.type){
case BEDROCK : return 0x101020;
case GRASS : return 0x719678;
case MARK : return 0x5D7B62;
case ROCK : return 0x8C8C8C;
case IRON_ORE : return 0x917B61;
case TREE : return 0x003800;
case BERRIES : return 0x4D6394;
case FOOD : return 0xFF7A7A;
case WOOD : return 0x634A22;
case STONE : return 0x454545;
case IRON : return 0x4A4036;
case DUDE :
dudeData = pixel.data;
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
case SPAWN :
spawnData = (int*)(pixel.data);
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
case WALL : return 0xE6B2A1;
case ROAD : return 0xEDB287;
case SWORD : return 0xEBEBEB;
case LIBRARY : return 0xA37A50;
case DEAD_DUDE : return 0xFF0000;
default : return 0x0000FF; // bleu absolu = bug
}
}
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}