Merge branch 'master' of https://git.tagachop.fr/epicsparrow/PixelWars
This commit is contained in:
commit
6822bb8cbb
10
Makefile
10
Makefile
@ -22,13 +22,13 @@ orange : orange.o
|
||||
|
||||
purple : purple.o
|
||||
|
||||
main : main.o generator.o
|
||||
main : main.o generator.o tools.o
|
||||
|
||||
$(BINARY) : orange.o purple.o main.o generator.o
|
||||
$(CC) main.o generator.o orange.o purple.o -o $(BINARY) $(LIB)
|
||||
$(BINARY) : orange.o purple.o main.o generator.o tools.o
|
||||
$(CC) main.o generator.o orange.o purple.o tools.o -o $(BINARY) $(LIB)
|
||||
|
||||
anselme : orange.o purple.o main.o generator_anselme.o
|
||||
$(CC) main.o generator_anselme.o orange.o purple.o -o $(BINARY) $(LIB)
|
||||
anselme : orange.o purple.o main.o generator_anselme.o tools.o
|
||||
$(CC) main.o generator_anselme.o orange.o purple.o tools.o -o $(BINARY) $(LIB)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -o $@ -c $< $(FLAGS)
|
||||
|
@ -46,7 +46,7 @@ int in_radius(int x,int y,t_biome e);
|
||||
int generate(int x, int y);
|
||||
void init_generator();
|
||||
|
||||
void create_map(t_pixel** map, t_team* teams, int w, int h){
|
||||
void create_map(int w, int h){
|
||||
int i,j;
|
||||
int type;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#ifndef GENERATOR_H
|
||||
#define GENERATOR_H
|
||||
|
||||
void create_map(t_pixel** map, t_team* teams, int width, int height);
|
||||
void create_map(int width, int height);
|
||||
|
||||
#endif
|
@ -15,7 +15,7 @@ int max(int a, int b)
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
void create_map(t_pixel** map, t_team* teams, int w, int h){
|
||||
void create_map(int w, int h){
|
||||
int i,j, k;
|
||||
int r = (w/NB_TEAMS < h ? w/NB_TEAMS : h)/2;
|
||||
|
||||
@ -39,7 +39,7 @@ void create_map(t_pixel** map, t_team* teams, int w, int h){
|
||||
if(d == 0){
|
||||
map[i][j].type = SPAWN;
|
||||
map[i][j].data = malloc(sizeof(int));
|
||||
*(int*)(map[i][j].data) = k;
|
||||
*((int*)(map[i][j].data)) = k;
|
||||
}else{
|
||||
int l = (d-20)+(rand()%40);
|
||||
if(l > r+15){
|
||||
|
179
main.c
179
main.c
@ -1,14 +1,17 @@
|
||||
#include "main.h"
|
||||
#include "team.h"
|
||||
#include "tools.h"
|
||||
#include "generator.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
t_pixel** map;
|
||||
t_team* teams;
|
||||
SDL_Surface* img;
|
||||
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 my_id);
|
||||
@ -17,7 +20,7 @@ int get_purple_size();
|
||||
int get_orange_size();
|
||||
|
||||
void initWorld(){
|
||||
int i, j;
|
||||
int i;
|
||||
|
||||
// allocations
|
||||
printf("Allocating memory...\n");
|
||||
@ -38,7 +41,7 @@ void initWorld(){
|
||||
teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES);
|
||||
teams[i].spawn.x = 0;
|
||||
teams[i].spawn.y = 0;
|
||||
teams[i].spawn_food = 0;
|
||||
teams[i].spawn_food = 10;
|
||||
teams[i].spawn_count = 0;
|
||||
}
|
||||
map = (t_pixel**)malloc(sizeof(t_pixel*)*WIDTH);
|
||||
@ -47,7 +50,7 @@ void initWorld(){
|
||||
|
||||
// generate map
|
||||
printf("Generating map...\n");
|
||||
create_map(map, teams, WIDTH, HEIGHT);
|
||||
create_map(WIDTH, HEIGHT);
|
||||
|
||||
// create image from map
|
||||
printf("Creating image from map...\n");
|
||||
@ -62,56 +65,55 @@ void updateTeam(t_team team){
|
||||
int i;
|
||||
t_action action;
|
||||
for(i=0; i<team.nb_dudes; i++){
|
||||
t_dude dude = team.dudes[i];
|
||||
action = team.update((void*)(dude.custom_data), (void*)(dude.com_data), i);
|
||||
handleAction(action, dude);
|
||||
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i);
|
||||
handleAction(action, team.dudes+i);
|
||||
}
|
||||
}
|
||||
|
||||
t_pixel getTile(t_coord coord, int dir){
|
||||
t_coord getPos(t_coord coord, int dir){
|
||||
switch(dir){
|
||||
case NORTH :
|
||||
coord.x++;
|
||||
break;
|
||||
case SOUTH :
|
||||
coord.x--;
|
||||
break;
|
||||
case WEST :
|
||||
coord.y++;
|
||||
break;
|
||||
case EAST :
|
||||
case SOUTH :
|
||||
coord.y--;
|
||||
break;
|
||||
case WEST :
|
||||
coord.x++;
|
||||
break;
|
||||
case EAST :
|
||||
coord.x--;
|
||||
break;
|
||||
}
|
||||
return map[coord.x][coord.y];
|
||||
return coord;
|
||||
}
|
||||
|
||||
void spawnDudes(){
|
||||
int i;
|
||||
t_dude new_dude;
|
||||
for(i=0; i<NB_TEAMS; i++){
|
||||
t_team team = teams[i];
|
||||
if(team.spawn_food)
|
||||
team.spawn_count++;
|
||||
if(team.spawn_count > 10 && map[team.spawn.x][team.spawn.y].type == SPAWN){
|
||||
team.spawn_food--;
|
||||
if(teams[i].spawn_food > 0)
|
||||
teams[i].spawn_count++;
|
||||
if(teams[i].spawn_count > 10 && map[teams[i].spawn.x][teams[i].spawn.y].type == SPAWN){
|
||||
teams[i].spawn_food--;
|
||||
teams[i].spawn_count = 0;
|
||||
|
||||
new_dude.pos = team.spawn;
|
||||
new_dude.team = team.team;
|
||||
new_dude.pos = teams[i].spawn;
|
||||
new_dude.team = teams[i].team;
|
||||
new_dude.inventory = -1;
|
||||
new_dude.ground.type = SPAWN;
|
||||
new_dude.ground.data = NULL;
|
||||
new_dude.custom_data = malloc(team.dude_size);
|
||||
memset(new_dude.custom_data, 0, team.dude_size);
|
||||
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);
|
||||
new_dude.com_data = NULL;
|
||||
|
||||
team.dudes[team.nb_dudes++] = new_dude;
|
||||
teams[i].dudes[teams[i].nb_dudes++] = new_dude;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handleAction(t_action action, t_dude dude){
|
||||
t_pixel target = getTile(dude.pos, action.dir);
|
||||
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];
|
||||
switch(action.type){
|
||||
case MOVE :
|
||||
if( target.type != WALL
|
||||
@ -120,23 +122,22 @@ void handleAction(t_action action, t_dude dude){
|
||||
&& target.type != IRON_ORE
|
||||
&& target.type != DUDE
|
||||
&& target.type != TREE
|
||||
){
|
||||
map[dude.pos.x][dude.pos.y] = dude.ground;
|
||||
dude.ground = target;
|
||||
target.type = DUDE;
|
||||
target.data = &dude;
|
||||
}
|
||||
)
|
||||
add_move(dude, target_pos);
|
||||
break;
|
||||
case ATTACK :
|
||||
if(target.type == DUDE)
|
||||
addFight(dude, *((t_dude*)(target.data)));
|
||||
printf("forbidden action\n");
|
||||
//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
|
||||
break;
|
||||
case PUT :
|
||||
printf("forbidden action\n");
|
||||
// if target is grass or road or corpse
|
||||
// target = inventory
|
||||
// inventory = NULL
|
||||
@ -144,6 +145,7 @@ void handleAction(t_action action, t_dude dude){
|
||||
// spawn.food ++
|
||||
break;
|
||||
case WORK :
|
||||
printf("forbidden action\n");
|
||||
// switch target
|
||||
// case rock -> stone
|
||||
// case berries -> food
|
||||
@ -158,9 +160,11 @@ void handleAction(t_action action, t_dude dude){
|
||||
// case corpse -> grass
|
||||
break;
|
||||
case WAIT :
|
||||
printf("forbidden action\n");
|
||||
// ...
|
||||
break;
|
||||
case COMMUNICATE :
|
||||
printf("forbidden action\n");
|
||||
// if target is sign -> set sign message
|
||||
// if target is dude -> sent message to dude
|
||||
break;
|
||||
@ -169,101 +173,13 @@ void handleAction(t_action action, t_dude dude){
|
||||
|
||||
void generateImg(){
|
||||
int i, j;
|
||||
Uint32 color;
|
||||
t_dude* dudeData;
|
||||
int* spawnData;
|
||||
for(i=0; i<WIDTH; i++){
|
||||
for(j=0; j<HEIGHT; j++){
|
||||
switch(map[i][j].type){
|
||||
case BEDROCK : color = 0x101020; break;
|
||||
case GRASS : color = 0x004400; break;
|
||||
case ROCK : color = 0x8C8C8C; break;
|
||||
case IRON_ORE : color = 0x917B61; break;
|
||||
case TREE : color = 0x000F0F; break;
|
||||
case BERRIES : color = 0x6B87C7; break;
|
||||
case FOOD : color = 0xFF7A7A; break;
|
||||
case WOOD : color = 0x634A22; break;
|
||||
case STONE : color = 0x999999; break;
|
||||
case IRON : color = 0x555555; break;
|
||||
case CORPSE : color = 0xFF0000; break;
|
||||
|
||||
case DUDE :
|
||||
dudeData = map[i][j].data;
|
||||
color = dudeData->team == ORANGE ? 0xFF8000 : 0x9900FF;
|
||||
break;
|
||||
case SPAWN :
|
||||
spawnData = (int*)map[i][j].data;
|
||||
color = *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
|
||||
break;
|
||||
|
||||
case WALL : color = 0xE6B2A1; break;
|
||||
case ROAD : color = 0xEDB287; break;
|
||||
case SWORD : color = 0xEBEBEB; break;
|
||||
case SIGN : color = 0xA37A50; break;
|
||||
default : color = 0x0000FF; break; // bleu absolu = bug
|
||||
}
|
||||
putpixel(img, i, j, color);
|
||||
//img->pixels[j * WIDTH + i] = color;
|
||||
putpixel(img, i, j, getColor(map[i][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void initSDL()
|
||||
{
|
||||
srand(time(NULL));
|
||||
@ -288,13 +204,16 @@ int MAIN( int argc, char** argv )
|
||||
initWorld();
|
||||
SDL_Flip(img);
|
||||
|
||||
printf("Launching simulation... press ESCAPE to stop.\n");
|
||||
|
||||
while (!keystate[SDLK_ESCAPE] && !over){
|
||||
temps = SDL_GetTicks();
|
||||
SDL_PumpEvents();
|
||||
int i;
|
||||
//spawnDudes();
|
||||
spawnDudes();
|
||||
for(i=0; i<NB_TEAMS; i++)
|
||||
updateTeam(teams[i]);
|
||||
resolve_moves();
|
||||
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
|
||||
SDL_Flip(img);
|
||||
}
|
||||
|
11
main.h
11
main.h
@ -12,7 +12,7 @@
|
||||
#define OS "Linux"
|
||||
#endif
|
||||
|
||||
#define MAX_FPS 60
|
||||
#define MAX_FPS 4
|
||||
#define MAX_DUDES 50
|
||||
#define WIDTH 400
|
||||
#define HEIGHT 250
|
||||
@ -47,10 +47,13 @@ typedef struct{
|
||||
int spawn_count;
|
||||
} t_team;
|
||||
|
||||
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
|
||||
|
||||
void generateImg();
|
||||
|
||||
void handleAction(t_action action, t_dude dude);
|
||||
void handleAction(t_action action, t_dude* dude);
|
||||
|
||||
// variables globales
|
||||
t_pixel** map;
|
||||
t_team* teams;
|
||||
SDL_Surface* img;
|
||||
|
||||
#endif
|
2
orange.c
2
orange.c
@ -16,7 +16,7 @@ t_action orange_update(void* my_info, void* com_data, int my_id){
|
||||
t_action action;
|
||||
|
||||
action.type = MOVE;
|
||||
action.dir = SOUTH;
|
||||
action.dir = rand()%4;
|
||||
action.data = NULL;
|
||||
|
||||
return action;
|
||||
|
2
purple.c
2
purple.c
@ -16,7 +16,7 @@ t_action purple_update(void* my_info, void* com_data, int my_id){
|
||||
t_action action;
|
||||
|
||||
action.type = MOVE;
|
||||
action.dir = NORTH;
|
||||
action.dir = rand()%4;
|
||||
action.data = NULL;
|
||||
|
||||
return action;
|
||||
|
142
tools.c
Normal file
142
tools.c
Normal file
@ -0,0 +1,142 @@
|
||||
#include "tools.h"
|
||||
|
||||
typedef struct{
|
||||
t_dude* dude;
|
||||
t_coord dst;
|
||||
int ok;
|
||||
} t_move;
|
||||
|
||||
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;
|
||||
move->ok = 0;
|
||||
if(map[dst.x][dst.y].type == DUDE)
|
||||
occupiedMoves[nb_occupied++] = move;
|
||||
else
|
||||
clearMoves[nb_clear++] = move;
|
||||
}
|
||||
|
||||
void applyMove(t_move* move){
|
||||
t_pixel target = map[move->dst.x][move->dst.y];
|
||||
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;
|
||||
}
|
||||
|
||||
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 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 CORPSE : return 0xFF0000;
|
||||
case DUDE :
|
||||
dudeData = pixel.data;
|
||||
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
|
||||
case SPAWN :
|
||||
spawnData = (int*)(pixel.data);
|
||||
if(spawnData == NULL) printf("WTF\n");
|
||||
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
|
||||
case WALL : return 0xE6B2A1;
|
||||
case ROAD : return 0xEDB287;
|
||||
case SWORD : return 0xEBEBEB;
|
||||
case SIGN : return 0xA37A50;
|
||||
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;
|
||||
}
|
||||
}
|
18
tools.h
Normal file
18
tools.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef TOOLS_H
|
||||
#define TOOLS_H
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include "main.h"
|
||||
#include "team.h"
|
||||
|
||||
void add_move(t_dude* dude, t_coord dst);
|
||||
|
||||
void resolve_moves();
|
||||
|
||||
Uint32 getColor(t_pixel pixel);
|
||||
|
||||
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
|
||||
|
||||
Uint32 getpixel(SDL_Surface *surface, int x, int y);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user