moved old_code to the dungeon
This commit is contained in:
parent
a7044584c0
commit
9c08f784b1
@ -1,41 +0,0 @@
|
||||
CC=gcc
|
||||
AR=ar rcs
|
||||
GENERATOR=anselme
|
||||
|
||||
ifdef SystemRoot
|
||||
RM=del /Q
|
||||
FixPath=$(subst /,\,$1)
|
||||
BINARY=PixelWars.exe
|
||||
LIB=-lSDL -lm -L./lib
|
||||
FLAGS=-Wall -I./include
|
||||
LIB_PREFIX=lib
|
||||
else
|
||||
ifeq ($(shell uname), Linux)
|
||||
RM=rm -f
|
||||
FixPath=$1
|
||||
BINARY=PixelWars
|
||||
LIB=-lSDL -lm
|
||||
FLAGS=-g -Wall
|
||||
LIB_PREFIX=a
|
||||
endif
|
||||
endif
|
||||
|
||||
all : $(BINARY)
|
||||
|
||||
purple/purple.$(LIB_PREFIX) : purple/*.c
|
||||
$(MAKE) -C purple
|
||||
|
||||
orange/orange.$(LIB_PREFIX) : orange/*.c
|
||||
$(MAKE) -C orange
|
||||
|
||||
$(BINARY) : orange/orange.$(LIB_PREFIX) purple/purple.$(LIB_PREFIX) PixelWars_$(GENERATOR).$(LIB_PREFIX)
|
||||
$(CC) PixelWars_$(GENERATOR).$(LIB_PREFIX) orange/orange.$(LIB_PREFIX) purple/purple.$(LIB_PREFIX) -o $(BINARY) $(LIB)
|
||||
|
||||
PixelWars_$(GENERATOR).$(LIB_PREFIX) : main.o tools.o generator_$(GENERATOR).o
|
||||
$(AR) PixelWars_$(GENERATOR).$(LIB_PREFIX) main.o tools.o generator_$(GENERATOR).o
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -o $@ -c $< $(FLAGS)
|
||||
|
||||
clean :
|
||||
$(RM) *.o *.a $(BINARY) *.stackdump *~
|
@ -1,6 +0,0 @@
|
||||
#ifndef GENERATOR_H
|
||||
#define GENERATOR_H
|
||||
|
||||
void create_map(int width, int height);
|
||||
|
||||
#endif
|
@ -1,63 +0,0 @@
|
||||
#include "main.h"
|
||||
|
||||
// functions
|
||||
int distance_manhattan(int x1,int y1, int x2, int y2);
|
||||
|
||||
int absolute(int val);
|
||||
|
||||
int custom_min(int a, int b){
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
int custom_max(int a, int b){
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
void create_map(int w, int h){
|
||||
int i,j, k;
|
||||
int r = (w/NB_TEAMS < h ? w/NB_TEAMS : h)/2;
|
||||
|
||||
for(i=0; i<NB_TEAMS; i++){
|
||||
teams[i].spawn.x = (w/(NB_TEAMS*2))*(1+i*2);
|
||||
teams[i].spawn.y = h/2;
|
||||
}
|
||||
|
||||
//génération de la carte
|
||||
for (i=0;i<w;i++){
|
||||
for(j=0;j<h;j++){
|
||||
map[i][j].data = NULL;
|
||||
if (i == 0 || j == 0 || i == w-1 || j == h-1){
|
||||
map[i][j].type = BEDROCK;
|
||||
}else{
|
||||
int d = custom_max(w, h);
|
||||
for(k=0; k<NB_TEAMS; k++){
|
||||
d = custom_min(d, distance_manhattan(teams[k].spawn.x, teams[k].spawn.y, i, j));
|
||||
if(!d) break;
|
||||
}
|
||||
if(d == 0){
|
||||
map[i][j].type = SPAWN;
|
||||
map[i][j].data = malloc(sizeof(int));
|
||||
*((int*)(map[i][j].data)) = k;
|
||||
}else{
|
||||
int l = (d-20)+(rand()%40);
|
||||
if(l > r+15){
|
||||
map[i][j].type = rand()%8 ? ROCK : IRON_ORE;
|
||||
}else if(l < r-15){
|
||||
map[i][j].type = rand()%15 ? GRASS : BERRIES;
|
||||
}else{
|
||||
l = rand()%10;
|
||||
map[i][j].type = l > 5 ? TREE : l ? GRASS : BERRIES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int distance_manhattan(int x1,int y1, int x2, int y2){
|
||||
return absolute(x1-x2) + absolute(y1-y2);
|
||||
}
|
||||
|
||||
int absolute(int val){
|
||||
return val > 0 ? val : -val;
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
#include "main.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifndef MAX
|
||||
#define max( a,b ) ( ((a) > (b) ) ? (a) : (b) )
|
||||
#endif
|
||||
|
||||
//variables
|
||||
int width, height, size;
|
||||
float corner_value[4];
|
||||
|
||||
//functions
|
||||
int closest_pow_2(int);
|
||||
void init_height_map(float**);
|
||||
void start_diamond_square(float**);
|
||||
void diamond_square(float**,int,int,int,int);
|
||||
float average(float,float);
|
||||
float average_noisy(float,float);
|
||||
float average_t(float*, int);
|
||||
void apply_noise(float*);
|
||||
|
||||
void create_map(int w, int h){
|
||||
width = w;
|
||||
height = h;
|
||||
|
||||
int i;
|
||||
float** height_map;
|
||||
|
||||
size=closest_pow_2(max(w,h));
|
||||
|
||||
height_map = (float**) malloc(sizeof(float*)*(size+1));
|
||||
for(i=0;i<=size;i++)
|
||||
height_map[i]=malloc(sizeof(float)*(size+1));
|
||||
|
||||
printf("init height map...\n");
|
||||
init_height_map(height_map);
|
||||
|
||||
printf("generate height map...\n");
|
||||
//generate height map
|
||||
start_diamond_square(height_map);
|
||||
|
||||
printf("erosion\n");
|
||||
|
||||
|
||||
|
||||
printf("finished generation! \n");
|
||||
|
||||
}
|
||||
|
||||
int closest_pow_2(int x){
|
||||
return pow(2,floor(log(x)/log(2))+1);
|
||||
}
|
||||
|
||||
void init_height_map(float** h_map){
|
||||
h_map[0][0]=0.7;
|
||||
h_map[size][0]=0.3;
|
||||
h_map[size][size]=0.9;
|
||||
h_map[0][size]=0.8;
|
||||
}
|
||||
|
||||
void start_diamond_square(float ** h_map){
|
||||
diamond_square(h_map,0,size,0,size);
|
||||
}
|
||||
|
||||
void dummy(int x1, int y1, int x2, int y2, int avg_x,int avg_y){
|
||||
}
|
||||
|
||||
void diamond_square(float** h_map,int x1,int x2,int y1, int y2){
|
||||
int avg_x, avg_y;
|
||||
float avg_value;
|
||||
|
||||
//get value of corner from h_map
|
||||
corner_value[0] = h_map[x1][y1]; //up_left
|
||||
corner_value[1] = h_map[x2][y1]; //up_right
|
||||
corner_value[2] = h_map[x2][y2]; //down_right
|
||||
corner_value[3] = h_map[x1][y2]; //down_left
|
||||
|
||||
//process coordinate of center
|
||||
avg_x = average(x1,x2);
|
||||
avg_y = average(y1,y2);
|
||||
|
||||
//dummy(x1,y1,x2,y2,avg_x,avg_y);
|
||||
//Diamond Step
|
||||
//process average value of the corner
|
||||
avg_value = average_t(corner_value,4);
|
||||
|
||||
apply_noise(&avg_value);
|
||||
//affect value
|
||||
h_map[avg_x][avg_y]=avg_value;
|
||||
|
||||
// Square Step
|
||||
//update value of the four mid-point between corner, following clockwise orde, and adding noise
|
||||
h_map[avg_x][y1] = average_noisy(corner_value[0], corner_value[1]); // up
|
||||
h_map[x2][avg_y] = average_noisy(corner_value[1], corner_value[2]); // right
|
||||
h_map[avg_x][y2] = average_noisy(corner_value[2], corner_value[3]); // down
|
||||
h_map[x1][avg_y] = average_noisy(corner_value[3], corner_value[0]); // left
|
||||
|
||||
//recursive call to diamond_square
|
||||
int offset = x2-x1;
|
||||
|
||||
if (offset > 2){
|
||||
diamond_square(h_map, x1, avg_x, y1, avg_y); //up_left square
|
||||
diamond_square(h_map, avg_x, x2, y1, avg_y); // up_right square
|
||||
diamond_square(h_map, avg_x, x2, avg_y, y2); // down_right square
|
||||
diamond_square(h_map, x1, avg_x, avg_y, y2); // down_left square
|
||||
}
|
||||
}
|
||||
|
||||
float average(float a, float b){
|
||||
return (a+b)/2;
|
||||
}
|
||||
|
||||
float average_noisy(float a, float b){
|
||||
float value = average(a,b);
|
||||
apply_noise(&value);
|
||||
return value;
|
||||
}
|
||||
|
||||
float average_t(float* tab, int size){
|
||||
int i;
|
||||
float avg_value=0;
|
||||
for(i=0;i<size;i++)
|
||||
avg_value += tab[i];
|
||||
return avg_value/size;
|
||||
}
|
||||
|
||||
void apply_noise(float* value){
|
||||
float noise = (float)rand()/(float)RAND_MAX;
|
||||
*value = *value + noise - 1;
|
||||
}
|
@ -1,234 +0,0 @@
|
||||
#include "main.h"
|
||||
|
||||
#define MAX_POWER 10
|
||||
#define MAX_EPICENTER_BY_TYPE 7
|
||||
#define DIST_MIN_INTER_EPICENTRE 50
|
||||
#define MIN_RADIUS 25
|
||||
#define OFFSET_RADIUS 50
|
||||
|
||||
//map type ----> not used for now
|
||||
enum{
|
||||
FLAT, VALLEY,
|
||||
};
|
||||
|
||||
//biome type
|
||||
enum{
|
||||
VILLAGE, PLAINS, FOREST,MOUNTAINS,NB_BIOMES
|
||||
};
|
||||
|
||||
// probability for each biomes
|
||||
// in following order {GRASS,TREE,BERRIES, ROCK, IRON_ORE}
|
||||
char proba_table[NB_BIOMES][5] = {{97,2,1,0,0},{85,13,2,0,0},{40,40,20,0,0},{0,0,0,80,20}};
|
||||
|
||||
typedef struct{
|
||||
int x;
|
||||
int y;
|
||||
int type;
|
||||
int power;
|
||||
int radius;
|
||||
} t_biome;
|
||||
|
||||
// variables
|
||||
int sp_x[NB_TEAMS], sp_y[NB_TEAMS];
|
||||
t_biome* l_biomes;
|
||||
int size_biomes;
|
||||
int nb_plains, nb_forests, nb_mountains;
|
||||
int cpt_biome = 0;
|
||||
int width, height;
|
||||
|
||||
// functions
|
||||
int distance_manhattan(int x1,int y1, int x2, int y2);
|
||||
int absolute(int val);
|
||||
void set_spawns(t_pixel** map, t_team* teams);
|
||||
void create_biome(int x, int y, int type);
|
||||
void create_biome_random(int type);
|
||||
int check_nears_biomes(int x, int y);
|
||||
int check_nears_spawn(int x, int y);
|
||||
int in_radius(int x,int y,t_biome e);
|
||||
int generate(int x, int y);
|
||||
void init_generator();
|
||||
|
||||
void create_map(int w, int h){
|
||||
int i,j;
|
||||
|
||||
//biome variable
|
||||
|
||||
width = w;
|
||||
height = h;
|
||||
|
||||
init_generator();
|
||||
|
||||
//Epicenters generation
|
||||
// random choice for numbers of biomes
|
||||
nb_plains = (rand()%MAX_EPICENTER_BY_TYPE)+3;
|
||||
nb_forests = (rand()%MAX_EPICENTER_BY_TYPE)+3;
|
||||
nb_mountains = (rand()%MAX_EPICENTER_BY_TYPE)+3;
|
||||
|
||||
size_biomes = nb_plains+ nb_forests + nb_mountains + NB_TEAMS;
|
||||
|
||||
l_biomes = malloc(sizeof(t_biome)*size_biomes);
|
||||
|
||||
// Spawn generations
|
||||
set_spawns(map,teams);
|
||||
|
||||
for(i=0;i<nb_plains;i++)
|
||||
create_biome_random(PLAINS);
|
||||
|
||||
for(i=0;i<nb_forests;i++)
|
||||
create_biome_random(FOREST);
|
||||
|
||||
for(i=0;i<nb_mountains;i++)
|
||||
create_biome_random(MOUNTAINS);
|
||||
// */
|
||||
//génération de la carte
|
||||
|
||||
for (i=0;i<width;i++){
|
||||
for(j=0;j<height;j++){
|
||||
if (i == 0 || j == 0 || i == w-1 || j == h-1){
|
||||
map[i][j].type = BEDROCK;
|
||||
}else if((i == sp_x[PURPLE] && j == sp_y[PURPLE])){
|
||||
map[i][j].type = SPAWN;
|
||||
map[i][j].data=malloc(sizeof(int));
|
||||
*((int*)(map[i][j].data)) = PURPLE;
|
||||
}else if(i == sp_x[ORANGE] && j == sp_y[ORANGE]){
|
||||
map[i][j].type = SPAWN;
|
||||
map[i][j].data=malloc(sizeof(int));
|
||||
*((int*)(map[i][j].data)) = ORANGE;
|
||||
}else{
|
||||
map[i][j].type = generate(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void init_generator(){
|
||||
|
||||
}
|
||||
|
||||
void set_spawns(t_pixel** map, t_team* teams){
|
||||
int i;
|
||||
int tier_w, tier_h;
|
||||
|
||||
tier_w = width/6;
|
||||
tier_h = height/3;
|
||||
|
||||
sp_x[0] = (rand()%tier_w)+tier_w;
|
||||
sp_y[0] = (rand()%tier_h)+tier_h;
|
||||
|
||||
sp_x[1] = width-sp_x[0];
|
||||
sp_y[1] = height-sp_y[0];
|
||||
|
||||
for(i=0;i<2;i++){
|
||||
teams[i].spawn.x = sp_x[i];
|
||||
teams[i].spawn.y = sp_y[i];
|
||||
create_biome(sp_x[i],sp_y[i],VILLAGE);
|
||||
}
|
||||
}
|
||||
|
||||
void create_biome(int x, int y, int type){
|
||||
t_biome *biome = malloc(sizeof(t_biome));
|
||||
|
||||
biome->x=x;
|
||||
biome->y=y;
|
||||
biome->type = type;
|
||||
|
||||
switch(type){
|
||||
case VILLAGE:
|
||||
biome->power = (rand()%MAX_POWER)+1;
|
||||
biome->radius = (rand()%(25-10))+10;
|
||||
break;
|
||||
case PLAINS:
|
||||
case FOREST:
|
||||
case MOUNTAINS:
|
||||
default:
|
||||
biome->power = (rand()%MAX_POWER)+1;
|
||||
biome->radius = (rand()%(OFFSET_RADIUS))+MIN_RADIUS;
|
||||
break;
|
||||
}
|
||||
|
||||
l_biomes[cpt_biome++]=*biome;
|
||||
}
|
||||
|
||||
void create_biome_random(int type){
|
||||
int x,y;
|
||||
do {
|
||||
x=rand()%width;
|
||||
y=rand()%height;
|
||||
} while ((check_nears_biomes(x,y) != 0) || (check_nears_spawn(x,y) != 0)); //prevent biome superposition
|
||||
|
||||
create_biome(x,y,type);
|
||||
}
|
||||
|
||||
int check_nears_biomes(int x, int y){
|
||||
int i, c=0;
|
||||
for(i=0;i<cpt_biome;i++){
|
||||
if (in_radius(x,y,l_biomes[i]) != -1) c++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int check_nears_spawn(int x, int y){
|
||||
int i,c = 0;
|
||||
for(i=0;i<2;i++)
|
||||
if (distance_manhattan(x,y,sp_x[i],sp_y[i]) < 75) c++;
|
||||
return c;
|
||||
}
|
||||
|
||||
int in_radius(int x,int y,t_biome e){
|
||||
int d = distance_manhattan(x,y,e.x,e.y);
|
||||
return d < e.radius ? d : -1;
|
||||
}
|
||||
|
||||
int generate(int x, int y){
|
||||
int i, j;
|
||||
int proba[5];
|
||||
int sum, dist, ratio, val, seuil=0;
|
||||
t_biome biome;
|
||||
|
||||
memset(&proba,0,sizeof(int)*5);
|
||||
sum=0;
|
||||
|
||||
for(i=0;i<size_biomes;i++){
|
||||
biome = l_biomes[i];
|
||||
if ((dist=in_radius(x,y,biome)) != -1){
|
||||
ratio=(((biome.radius-dist)*100)/biome.radius);
|
||||
//ne marche pas correctement, besoin de fractale à la place
|
||||
/* if (biome.type == MOUNTAINS && (ratio < 20)){
|
||||
sum += biome.power*ratio*100;
|
||||
proba[0] += biome.power*ratio*20;
|
||||
proba[3] += biome.power*ratio*75;
|
||||
proba[4] += biome.power*ratio*5;
|
||||
}else{*/
|
||||
sum += biome.power*ratio*100;
|
||||
for(j=0;j<5;j++){
|
||||
proba[j]+=(proba_table[biome.type][j])*(biome.power)*ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sum!=0){
|
||||
val = rand()%sum;
|
||||
for (i=0;i<5;i++){
|
||||
seuil += proba[i];
|
||||
if(val < seuil)
|
||||
return i+1;
|
||||
}
|
||||
}else{
|
||||
val = rand()%100;
|
||||
if (val <95){
|
||||
return GRASS;
|
||||
}else{
|
||||
return TREE;
|
||||
}
|
||||
}
|
||||
return GRASS;
|
||||
}
|
||||
|
||||
int distance_manhattan(int x1,int y1, int x2, int y2){
|
||||
return absolute(x1-x2) + absolute(y1-y2);
|
||||
}
|
||||
|
||||
int absolute(int val){
|
||||
return val > 0 ? val : -val;
|
||||
}
|
543
old_code/main.c
543
old_code/main.c
@ -1,543 +0,0 @@
|
||||
#include "main.h"
|
||||
#include "team.h"
|
||||
#include "tools.h"
|
||||
#include "generator.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
// temp code
|
||||
t_action purple_update();
|
||||
t_action orange_update();
|
||||
|
||||
t_dude* current_dude;
|
||||
|
||||
void initWorld(int width, int height){
|
||||
int i;
|
||||
|
||||
// allocations
|
||||
printf("Allocating memory...\n");
|
||||
teams = malloc(sizeof(t_team)*NB_TEAMS);
|
||||
for(i=0; i<NB_TEAMS; i++){
|
||||
teams[i].team = i;
|
||||
switch(i){
|
||||
case ORANGE :
|
||||
teams[i].update = orange_update;
|
||||
break;
|
||||
case PURPLE :
|
||||
teams[i].update = purple_update;
|
||||
break;
|
||||
}
|
||||
teams[i].nb_dudes = 0;
|
||||
teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES);
|
||||
teams[i].spawn.x = 0;
|
||||
teams[i].spawn.y = 0;
|
||||
teams[i].spawn_food = NB_STARTING_FOOD;
|
||||
teams[i].spawn_count = 0;
|
||||
}
|
||||
map = (t_pixel**)malloc(sizeof(t_pixel*)*width);
|
||||
for(i=0; i<width; i++)
|
||||
map[i] = (t_pixel*)malloc(sizeof(t_pixel)*height);
|
||||
|
||||
// generate map
|
||||
printf("Generating map...\n");
|
||||
create_map(width, height);
|
||||
|
||||
// create image from map
|
||||
printf("Creating image from map...\n");
|
||||
generateImg(width, height);
|
||||
}
|
||||
|
||||
void updateTeam(t_team team){
|
||||
int i;
|
||||
|
||||
for(i=0; i<team.nb_dudes; i++){
|
||||
current_dude = team.dudes + i;
|
||||
team.dudes[i].action = team.update();
|
||||
if(team.dudes[i].com_data != NULL){
|
||||
free(team.dudes[i].com_data);
|
||||
team.dudes[i].com_data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=0; i<team.nb_dudes; i++){
|
||||
current_dude = team.dudes + i;
|
||||
handleAction(current_dude);
|
||||
}
|
||||
}
|
||||
|
||||
t_coord getPos(t_coord coord, int dir){
|
||||
switch(dir){
|
||||
case NORTH :
|
||||
coord.y--;
|
||||
break;
|
||||
case SOUTH :
|
||||
coord.y++;
|
||||
break;
|
||||
case WEST :
|
||||
coord.x++;
|
||||
break;
|
||||
case EAST :
|
||||
coord.x--;
|
||||
break;
|
||||
}
|
||||
return coord;
|
||||
}
|
||||
|
||||
int getNear(int dir){
|
||||
t_coord coord = getPos(current_dude->pos, dir);
|
||||
return map[coord.x][coord.y].type;
|
||||
}
|
||||
|
||||
int getInfo(int dir){
|
||||
t_coord coord = getPos(current_dude->pos, dir);
|
||||
t_pixel pixel = map[coord.x][coord.y];
|
||||
t_dude* dude;
|
||||
int* n;
|
||||
switch(pixel.type){
|
||||
case DUDE :
|
||||
dude = pixel.data;
|
||||
return dude->team;
|
||||
case FOOD :
|
||||
case WOOD :
|
||||
case STONE :
|
||||
case IRON :
|
||||
case SWORD :
|
||||
case SPAWN :
|
||||
n = pixel.data;
|
||||
return *n;
|
||||
default :
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int getInventory(){
|
||||
return current_dude->inventory;
|
||||
}
|
||||
|
||||
t_com* getComData(){
|
||||
return current_dude->com_data;
|
||||
}
|
||||
|
||||
void* getMemory(){
|
||||
return current_dude->custom_data;
|
||||
}
|
||||
|
||||
int getSuccess(){
|
||||
return current_dude->success;
|
||||
}
|
||||
|
||||
void spawnDudes(){
|
||||
int i;
|
||||
t_dude new_dude;
|
||||
for(i=0; i<NB_TEAMS; i++){
|
||||
if(teams[i].spawn_food > 0)
|
||||
teams[i].spawn_count++;
|
||||
if(teams[i].nb_dudes < MAX_DUDES && teams[i].spawn_count > SPAWN_COOLDOWN && map[teams[i].spawn.x][teams[i].spawn.y].type == SPAWN){
|
||||
teams[i].spawn_food--;
|
||||
teams[i].spawn_count = 0;
|
||||
|
||||
new_dude.pos = teams[i].spawn;
|
||||
new_dude.team = teams[i].team;
|
||||
new_dude.inventory = -1;
|
||||
new_dude.success = 1;
|
||||
new_dude.dead = 0;
|
||||
new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y];
|
||||
new_dude.custom_data = malloc(DUDE_MEMORY);
|
||||
memset(new_dude.custom_data, 0, DUDE_MEMORY);
|
||||
new_dude.com_data = NULL;
|
||||
|
||||
teams[i].dudes[teams[i].nb_dudes++] = new_dude;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handleAction(t_dude* dude){
|
||||
t_action action = dude->action;
|
||||
t_coord target_pos = getPos(dude->pos, action.dir);
|
||||
t_pixel target = map[target_pos.x][target_pos.y];
|
||||
t_dude* target_dude;
|
||||
int* nb_res;
|
||||
switch(action.type){
|
||||
case MOVE :
|
||||
dude->success = 0;
|
||||
if( target.type != WALL
|
||||
&& target.type != ROCK
|
||||
&& target.type != BEDROCK
|
||||
&& target.type != IRON_ORE
|
||||
&& target.type != TREE
|
||||
&& target.type != LIBRARY
|
||||
)
|
||||
add_move(dude, target_pos);
|
||||
break;
|
||||
case ATTACK :
|
||||
dude->success = 0;
|
||||
if( target.type == DUDE
|
||||
|| 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;
|
||||
case PICK :
|
||||
nb_res = target.data;
|
||||
if(target.type >= FOOD && target.type <= SWORD && dude->inventory == -1){
|
||||
dude->inventory = target.type;
|
||||
(*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;
|
||||
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
||||
}
|
||||
dude->success = 1;
|
||||
}else
|
||||
dude->success = 0;
|
||||
break;
|
||||
case PUT :
|
||||
dude->success = 1;
|
||||
if(dude->inventory != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->inventory)){
|
||||
if(target.type == GRASS || target.type == MARK){
|
||||
map[target_pos.x][target_pos.y].type = dude->inventory;
|
||||
nb_res = malloc(sizeof(int));
|
||||
*nb_res = 1;
|
||||
map[target_pos.x][target_pos.y].data = nb_res;
|
||||
}else{
|
||||
nb_res = target.data;
|
||||
(*nb_res)++;
|
||||
}
|
||||
dude->inventory = -1;
|
||||
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
||||
}else if(target.type == SPAWN && dude->inventory == FOOD){
|
||||
dude->inventory = -1;
|
||||
nb_res = (int*)target.data;
|
||||
teams[*nb_res].spawn_food++;
|
||||
}else{
|
||||
printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
||||
dude->success = 0;
|
||||
}
|
||||
break;
|
||||
case WORK :
|
||||
dude->success = 1;
|
||||
switch(target.type){
|
||||
case ROCK :
|
||||
map[target_pos.x][target_pos.y].type = STONE;
|
||||
nb_res = malloc(sizeof(int));
|
||||
*nb_res = 1;
|
||||
map[target_pos.x][target_pos.y].data = nb_res;
|
||||
break;
|
||||
case BERRIES :
|
||||
map[target_pos.x][target_pos.y].type = FOOD;
|
||||
nb_res = malloc(sizeof(int));
|
||||
*nb_res = 1;
|
||||
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 = MARK;
|
||||
map[target_pos.x][target_pos.y].data = NULL;
|
||||
break;
|
||||
case MARK :
|
||||
map[target_pos.x][target_pos.y].type = GRASS;
|
||||
map[target_pos.x][target_pos.y].data = NULL;
|
||||
break;
|
||||
case WOOD :
|
||||
nb_res = target.data;
|
||||
switch(*nb_res){
|
||||
case 1 :
|
||||
free(target.data);
|
||||
map[target_pos.x][target_pos.y].type = WALL;
|
||||
map[target_pos.x][target_pos.y].data = NULL;
|
||||
break;
|
||||
case 2 :
|
||||
free(target.data);
|
||||
map[target_pos.x][target_pos.y].type = LIBRARY;
|
||||
map[target_pos.x][target_pos.y].data = malloc(128);
|
||||
memset(map[target_pos.x][target_pos.y].data, 0, 128);
|
||||
default :
|
||||
dude->success = 0;
|
||||
break;
|
||||
}
|
||||
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 = ROAD;
|
||||
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;
|
||||
default :
|
||||
dude->success = 0;
|
||||
break;
|
||||
}
|
||||
if(dude->success)
|
||||
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
||||
break;
|
||||
case WAIT :
|
||||
dude->success = 1;
|
||||
break;
|
||||
case COMMUNICATE :
|
||||
switch(target.type){
|
||||
// TODO : conflicts are not handled in a fair way
|
||||
case DUDE :
|
||||
printf("message to dude\n");
|
||||
action.com_data.flag = (action.dir+2)%4;
|
||||
target_dude = target.data;
|
||||
if(target_dude->com_data == NULL){
|
||||
target_dude->com_data = malloc(sizeof(t_com));
|
||||
*(target_dude->com_data) = action.com_data;
|
||||
dude->success = 1;
|
||||
}else
|
||||
dude->success = 0;
|
||||
break;
|
||||
case LIBRARY :
|
||||
if(action.com_data.flag & READ){
|
||||
if(dude->com_data == NULL){
|
||||
action.com_data.flag = action.dir;
|
||||
dude->com_data = malloc(sizeof(t_com));
|
||||
*(dude->com_data) = action.com_data;
|
||||
memcpy(dude->com_data + sizeof(int), target.data + (action.com_data.flag | 3), 32);
|
||||
}else{
|
||||
dude->success = 0;
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
memcpy(target.data + action.com_data.flag, action.com_data.data, 32);
|
||||
}
|
||||
dude->success = 1;
|
||||
break;
|
||||
default :
|
||||
dude->success = 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void generateImg(int width, int height){
|
||||
img = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
|
||||
int i, j;
|
||||
for(i=0; i<width; i++){
|
||||
for(j=0; j<height; j++){
|
||||
putpixel(img, i, j, getColor(map[i][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface* initSDL(int width, int height, int fullscreen)
|
||||
{
|
||||
SDL_Surface* screen;
|
||||
srand(time(NULL));
|
||||
if(fullscreen)
|
||||
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
|
||||
else
|
||||
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
||||
SDL_WM_SetCaption("Pixel Wars", NULL);
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == -1)
|
||||
{
|
||||
fprintf(stderr, "Erreur d'initialisation de la SDL");// and you don't want my french comment :p
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return screen;
|
||||
}
|
||||
|
||||
int screen_to_img(int pos_screen, int zoom ,int offset){
|
||||
return pos_screen/zoom + offset;
|
||||
}
|
||||
|
||||
void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
|
||||
int i, j, x, y;
|
||||
int pos_x,pos_y;
|
||||
|
||||
pos_x = x_offset - img->w/(2*zoom_level);
|
||||
pos_y = y_offset - img->h/(2*zoom_level);
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
for(i=0; i<screen->w; i++){
|
||||
for(j=0; j<screen->h; j++){
|
||||
//new version
|
||||
x = screen_to_img(i,zoom_level,pos_x);
|
||||
y = screen_to_img(j,zoom_level,pos_y);
|
||||
|
||||
//old version
|
||||
//x = (i - img->w/2 )/zoom_level + x_offset;
|
||||
//y = (j - img->h/2 )/zoom_level + y_offset;
|
||||
if(x >= 0 && x < img->w && y >= 0 && y < img->h)
|
||||
putpixel(screen, i, j, getpixel(img, x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int MAIN
|
||||
{
|
||||
SDL_Surface* screen;
|
||||
SDL_Event event;
|
||||
int i;
|
||||
int paused = 0;
|
||||
int x_offset = 0;
|
||||
int y_offset = 0;
|
||||
int zoom_level = DEFAULT_STARTING_ZOOM;
|
||||
int over = 0;
|
||||
int time = 0;
|
||||
int wait_time = 100;
|
||||
int new_time = 0;
|
||||
int remaining_time = -1;
|
||||
int fullscreen = 0;
|
||||
int width = DEFAULT_GAME_WIDTH;
|
||||
int height = DEFAULT_GAME_HEIGHT;
|
||||
int disp_width = DEFAULT_WINDOW_WIDTH;
|
||||
int disp_height = DEFAULT_WINDOW_HEIGHT;
|
||||
|
||||
#ifndef _WIN32
|
||||
argv++;
|
||||
while(argc > 1){ // pas initialisé sur windows
|
||||
printf("%s\n", argv[0]);
|
||||
switch(argv[0][0]){
|
||||
case 'f' :
|
||||
fullscreen = 1;
|
||||
break;
|
||||
case 'w' :
|
||||
width = atoi(argv[0]+2);
|
||||
disp_width = 2*width;
|
||||
break;
|
||||
case 'h' :
|
||||
height = atoi(argv[0]+2);
|
||||
disp_height = 2*height;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
argv++;
|
||||
argc--;
|
||||
}
|
||||
#endif
|
||||
|
||||
screen = initSDL(disp_width, disp_height, fullscreen);
|
||||
initWorld(width, height);
|
||||
SDL_Flip(img);
|
||||
|
||||
x_offset = width/2;
|
||||
y_offset = height/2;
|
||||
|
||||
printf("Launching simulation...\n");
|
||||
|
||||
time = SDL_GetTicks();
|
||||
while (!over){
|
||||
while(SDL_PollEvent(&event)){
|
||||
switch (event.type){
|
||||
case SDL_KEYDOWN:
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_ESCAPE :
|
||||
over = 1;
|
||||
break;
|
||||
case SDLK_z :
|
||||
y_offset -= 5;
|
||||
break;
|
||||
case SDLK_s :
|
||||
y_offset += 5;
|
||||
break;
|
||||
case SDLK_q :
|
||||
x_offset -= 5;
|
||||
break;
|
||||
case SDLK_d :
|
||||
x_offset += 5;
|
||||
break;
|
||||
case SDLK_UP :
|
||||
wait_time *= 2;
|
||||
break;
|
||||
case SDLK_DOWN :
|
||||
if(wait_time > 2)
|
||||
wait_time /= 2;
|
||||
break;
|
||||
case SDLK_LEFT :
|
||||
zoom_level++;
|
||||
break;
|
||||
case SDLK_RIGHT :
|
||||
zoom_level--;
|
||||
if(zoom_level < 1)
|
||||
zoom_level = 1;
|
||||
break;
|
||||
case SDLK_SPACE :
|
||||
paused = !paused;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
x_offset += event.motion.xrel;
|
||||
y_offset += event.motion.yrel;
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
over = 1;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* check x and y offset*/
|
||||
if (x_offset < 0) x_offset=0;
|
||||
if (x_offset > width) x_offset=width;
|
||||
|
||||
if (y_offset < 0) y_offset=0;
|
||||
if(y_offset > height) y_offset=height;
|
||||
|
||||
new_time = SDL_GetTicks();
|
||||
remaining_time -= new_time - time;
|
||||
time = new_time;
|
||||
if(remaining_time < 0 && !paused){
|
||||
spawnDudes();
|
||||
for(i=0; i<NB_TEAMS; i++)
|
||||
updateTeam(teams[i]);
|
||||
resolve_moves();
|
||||
|
||||
remaining_time = wait_time;
|
||||
}
|
||||
|
||||
render(screen, x_offset, y_offset, zoom_level);
|
||||
|
||||
SDL_Delay(30);
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
SDL_FreeSurface(screen);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include "team.h"
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#define MAIN APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
||||
#else
|
||||
#define MAIN main(int argc, char** argv)
|
||||
#endif
|
||||
|
||||
#define MAX_DUDES 50
|
||||
#define STACK_SIZE 5
|
||||
#define DEFAULT_GAME_WIDTH 400
|
||||
#define DEFAULT_GAME_HEIGHT 250
|
||||
#define DEFAULT_WINDOW_WIDTH 800
|
||||
#define DEFAULT_WINDOW_HEIGHT 500
|
||||
#define DEFAULT_STARTING_ZOOM 2
|
||||
#define NB_STARTING_FOOD 5
|
||||
#define SPAWN_COOLDOWN 30
|
||||
|
||||
typedef struct{
|
||||
int type;
|
||||
void* data;
|
||||
} t_pixel;
|
||||
|
||||
typedef struct{
|
||||
t_coord pos;
|
||||
int team;
|
||||
int inventory;
|
||||
int success;
|
||||
int dead;
|
||||
t_pixel ground;
|
||||
t_action action;
|
||||
void* custom_data;
|
||||
t_com* com_data;
|
||||
} t_dude;
|
||||
|
||||
typedef struct{
|
||||
int team;
|
||||
t_action (*update)();
|
||||
int nb_dudes;
|
||||
t_dude* dudes;
|
||||
t_coord spawn;
|
||||
int spawn_food;
|
||||
int spawn_count;
|
||||
} t_team;
|
||||
|
||||
void generateImg();
|
||||
|
||||
void handleAction(t_dude* dude);
|
||||
|
||||
// variables globales
|
||||
t_pixel** map;
|
||||
t_team* teams;
|
||||
SDL_Surface* img;
|
||||
|
||||
#endif
|
@ -1,30 +0,0 @@
|
||||
CC=gcc
|
||||
AR=ar rcs
|
||||
TARGET=orange
|
||||
|
||||
ifdef SystemRoot
|
||||
RM=del /Q
|
||||
FixPath=$(subst /,\,$1)
|
||||
LIB=
|
||||
FLAGS=-g -Wall
|
||||
LIB_PREFIX=lib
|
||||
else
|
||||
ifeq ($(shell uname), Linux)
|
||||
RM=rm -f
|
||||
FixPath=$1
|
||||
LIB=
|
||||
FLAGS=-g -Wall
|
||||
LIB_PREFIX=a
|
||||
endif
|
||||
endif
|
||||
|
||||
all : $(TARGET).$(LIB_PREFIX)
|
||||
|
||||
$(TARGET).$(LIB_PREFIX) : orange.o
|
||||
$(AR) $(TARGET).$(LIB_PREFIX) orange.o
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -o $@ -c $< $(FLAGS)
|
||||
|
||||
clean :
|
||||
$(RM) *.o *.a *.stackdump *~
|
@ -1,122 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../team.h"
|
||||
|
||||
// Orange Hello World
|
||||
|
||||
typedef struct{
|
||||
t_coord pos;
|
||||
int new_born;
|
||||
int try;
|
||||
int brings_food;
|
||||
int last_dir;
|
||||
int last_action;
|
||||
} orange_data;
|
||||
|
||||
t_coord o_newPos(t_coord coord, int dir){
|
||||
t_coord new_coord = coord;
|
||||
switch(dir){
|
||||
case NORTH :
|
||||
new_coord.y++;
|
||||
break;
|
||||
case SOUTH :
|
||||
new_coord.y--;
|
||||
break;
|
||||
case WEST :
|
||||
new_coord.x++;
|
||||
break;
|
||||
case EAST :
|
||||
new_coord.x--;
|
||||
break;
|
||||
}
|
||||
return new_coord;
|
||||
}
|
||||
|
||||
int o_abs(int val){
|
||||
return val > 0 ? val : -val;
|
||||
}
|
||||
|
||||
int o_dist(t_coord coord, int x, int y){
|
||||
return o_abs(coord.x-x) + o_abs(coord.y-y);
|
||||
}
|
||||
|
||||
t_action orange_update(){
|
||||
t_action action;
|
||||
int i, type;
|
||||
|
||||
int success = getSuccess();
|
||||
orange_data* data = (orange_data*)getMemory();
|
||||
|
||||
if(!data->new_born){
|
||||
success = 0;
|
||||
data->new_born = 1;
|
||||
}
|
||||
|
||||
if(data->last_action == MOVE){
|
||||
if(success)
|
||||
data->pos = o_newPos(data->pos, data->last_dir);
|
||||
}
|
||||
|
||||
if(data->try && success)
|
||||
data->brings_food = 1;
|
||||
data->try = 0;
|
||||
|
||||
if(data->brings_food){
|
||||
int distance = o_dist(data->pos, 0, 0);
|
||||
if(distance == 1){
|
||||
action.type = WAIT;
|
||||
|
||||
action.dir = 0;
|
||||
for(i=0; i<4; i++){
|
||||
type = getNear(i);
|
||||
if(type == SPAWN){
|
||||
action.dir = i;
|
||||
action.type = PUT;
|
||||
data->brings_food = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
action.type = MOVE;
|
||||
do{
|
||||
action.dir = rand()%4;
|
||||
}while(o_dist(o_newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
|
||||
|
||||
}
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
||||
|
||||
for(i=0; i<4; i++){
|
||||
type = getNear(i);
|
||||
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
|
||||
action.type = WORK;
|
||||
action.dir = i;
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}else if(type == FOOD){
|
||||
action.type = PICK;
|
||||
action.dir = i;
|
||||
data->try = 1;
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
||||
}
|
||||
|
||||
action.type = MOVE;
|
||||
do{
|
||||
action.dir = (data->last_dir + rand()%3)%4;
|
||||
type = getNear(action.dir);
|
||||
}while(type == WALL
|
||||
&& type == ROCK
|
||||
&& type == BEDROCK
|
||||
&& type == IRON_ORE
|
||||
&& type == TREE
|
||||
&& type == LIBRARY);
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
CC=gcc
|
||||
AR=ar rcs
|
||||
TARGET=purple
|
||||
|
||||
ifdef SystemRoot
|
||||
RM=del /Q
|
||||
FixPath=$(subst /,\,$1)
|
||||
LIB=
|
||||
FLAGS=-g -Wall
|
||||
LIB_PREFIX=lib
|
||||
else
|
||||
ifeq ($(shell uname), Linux)
|
||||
RM=rm -f
|
||||
FixPath=$1
|
||||
LIB=
|
||||
FLAGS=-g -Wall
|
||||
LIB_PREFIX=a
|
||||
endif
|
||||
endif
|
||||
|
||||
all : $(TARGET).$(LIB_PREFIX)
|
||||
|
||||
$(TARGET).$(LIB_PREFIX) : purple.o
|
||||
$(AR) $(TARGET).$(LIB_PREFIX) purple.o
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -o $@ -c $< $(FLAGS)
|
||||
|
||||
clean :
|
||||
$(RM) *.o *.a *.stackdump *~
|
@ -1,122 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../team.h"
|
||||
|
||||
// Purple Hello World
|
||||
|
||||
typedef struct{
|
||||
t_coord pos;
|
||||
int new_born;
|
||||
int try;
|
||||
int brings_food;
|
||||
int last_dir;
|
||||
int last_action;
|
||||
} purple_data;
|
||||
|
||||
t_coord p_newPos(t_coord coord, int dir){
|
||||
t_coord new_coord = coord;
|
||||
switch(dir){
|
||||
case NORTH :
|
||||
new_coord.y++;
|
||||
break;
|
||||
case SOUTH :
|
||||
new_coord.y--;
|
||||
break;
|
||||
case WEST :
|
||||
new_coord.x++;
|
||||
break;
|
||||
case EAST :
|
||||
new_coord.x--;
|
||||
break;
|
||||
}
|
||||
return new_coord;
|
||||
}
|
||||
|
||||
int p_abs(int val){
|
||||
return val > 0 ? val : -val;
|
||||
}
|
||||
|
||||
int p_dist(t_coord coord, int x, int y){
|
||||
return p_abs(coord.x-x) + p_abs(coord.y-y);
|
||||
}
|
||||
|
||||
t_action purple_update(){
|
||||
t_action action;
|
||||
int i, type;
|
||||
|
||||
int success = getSuccess();
|
||||
purple_data* data = (purple_data*)getMemory();
|
||||
|
||||
if(!data->new_born){
|
||||
success = 0;
|
||||
data->new_born = 1;
|
||||
}
|
||||
|
||||
if(data->last_action == MOVE){
|
||||
if(success)
|
||||
data->pos = p_newPos(data->pos, data->last_dir);
|
||||
}
|
||||
|
||||
if(data->try && success)
|
||||
data->brings_food = 1;
|
||||
data->try = 0;
|
||||
|
||||
if(data->brings_food){
|
||||
int distance = p_dist(data->pos, 0, 0);
|
||||
if(distance == 1){
|
||||
action.type = WAIT;
|
||||
|
||||
action.dir = 0;
|
||||
for(i=0; i<4; i++){
|
||||
type = getNear(i);
|
||||
if(type == SPAWN){
|
||||
action.dir = i;
|
||||
action.type = PUT;
|
||||
data->brings_food = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
action.type = MOVE;
|
||||
do{
|
||||
action.dir = rand()%4;
|
||||
}while(p_dist(p_newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
|
||||
|
||||
}
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
||||
|
||||
for(i=0; i<4; i++){
|
||||
type = getNear(i);
|
||||
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
|
||||
action.type = WORK;
|
||||
action.dir = i;
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}else if(type == FOOD){
|
||||
action.type = PICK;
|
||||
action.dir = i;
|
||||
data->try = 1;
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
||||
}
|
||||
|
||||
action.type = MOVE;
|
||||
do{
|
||||
action.dir = (data->last_dir + rand()%3)%4;
|
||||
type = getNear(action.dir);
|
||||
}while(type == WALL
|
||||
&& type == ROCK
|
||||
&& type == BEDROCK
|
||||
&& type == IRON_ORE
|
||||
&& type == TREE
|
||||
&& type == LIBRARY);
|
||||
data->last_dir = action.dir;
|
||||
data->last_action = action.type;
|
||||
return action;
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
#ifndef TEAM_H
|
||||
#define TEAM_H
|
||||
|
||||
#define WRITE 0
|
||||
#define READ 4
|
||||
|
||||
// Teams
|
||||
enum{
|
||||
PURPLE, ORANGE, NB_TEAMS
|
||||
};
|
||||
|
||||
// coordinates structure
|
||||
typedef struct{
|
||||
int x;
|
||||
int y;
|
||||
} t_coord;
|
||||
|
||||
// Directions
|
||||
enum{
|
||||
NORTH, EAST, SOUTH, WEST
|
||||
};
|
||||
|
||||
// communication
|
||||
#define LIBRARY_SIZE 128
|
||||
#define DUDE_MEMORY 128
|
||||
enum{
|
||||
BOOK_1,
|
||||
BOOK_2,
|
||||
BOOK_3,
|
||||
BOOK_4
|
||||
};
|
||||
|
||||
typedef struct{
|
||||
int flag;
|
||||
char data[32];
|
||||
} t_com;
|
||||
|
||||
// Tile types
|
||||
enum{
|
||||
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
||||
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
||||
DUDE, DEAD_DUDE, // humans
|
||||
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
|
||||
};
|
||||
|
||||
// Action types
|
||||
enum{
|
||||
MOVE,
|
||||
ATTACK,
|
||||
PICK,
|
||||
PUT,
|
||||
WORK,
|
||||
WAIT,
|
||||
COMMUNICATE
|
||||
};
|
||||
|
||||
typedef struct{
|
||||
int type;
|
||||
int dir;
|
||||
t_com com_data;
|
||||
} t_action;
|
||||
|
||||
int getInventory();
|
||||
|
||||
int getNear(int dir);
|
||||
|
||||
int getInfo(int dir);
|
||||
|
||||
t_com* getComData();
|
||||
|
||||
void* getMemory();
|
||||
|
||||
int getSuccess();
|
||||
|
||||
#endif
|
213
old_code/tools.c
213
old_code/tools.c
@ -1,213 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
#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 add_fight(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