debugged put

This commit is contained in:
anselme16 2015-01-15 17:42:59 +01:00
parent 944fc66665
commit 242cb0eec8
6 changed files with 93 additions and 57 deletions

57
main.c
View File

@ -15,10 +15,8 @@ typedef struct{
} t_fight; } t_fight;
// temp code // temp code
t_action purple_update(void* my_info, void* com_data, int my_id, 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 my_id, int success); t_action orange_update(void* my_info, void* com_data, int success);
int get_purple_size();
int get_orange_size();
t_dude* current_dude; t_dude* current_dude;
@ -32,11 +30,9 @@ void initWorld(int width, int height){
teams[i].team = i; teams[i].team = i;
switch(i){ switch(i){
case ORANGE : case ORANGE :
teams[i].dude_size = get_orange_size();
teams[i].update = orange_update; teams[i].update = orange_update;
break; break;
case PURPLE : case PURPLE :
teams[i].dude_size = get_purple_size();
teams[i].update = purple_update; teams[i].update = purple_update;
break; break;
} }
@ -44,7 +40,7 @@ void initWorld(int width, int height){
teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES); teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES);
teams[i].spawn.x = 0; teams[i].spawn.x = 0;
teams[i].spawn.y = 0; teams[i].spawn.y = 0;
teams[i].spawn_food = 10; teams[i].spawn_food = NB_STARTING_FOOD;
teams[i].spawn_count = 0; teams[i].spawn_count = 0;
} }
map = (t_pixel**)malloc(sizeof(t_pixel*)*width); map = (t_pixel**)malloc(sizeof(t_pixel*)*width);
@ -69,7 +65,7 @@ void updateTeam(t_team team){
t_action action; t_action action;
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), i, team.dudes[i].success); 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(action, team.dudes+i);
} }
} }
@ -112,8 +108,8 @@ void spawnDudes(){
new_dude.inventory = -1; new_dude.inventory = -1;
new_dude.success = 1; new_dude.success = 1;
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(teams[i].dude_size); new_dude.custom_data = malloc(DUDE_MEMORY);
memset(new_dude.custom_data, 0, teams[i].dude_size); memset(new_dude.custom_data, 0, DUDE_MEMORY);
new_dude.com_data = NULL; new_dude.com_data = NULL;
teams[i].dudes[teams[i].nb_dudes++] = new_dude; teams[i].dudes[teams[i].nb_dudes++] = new_dude;
@ -125,6 +121,7 @@ void handleAction(t_action action, t_dude* dude){
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;
t_team* team;
switch(action.type){ switch(action.type){
case MOVE : case MOVE :
dude->success = 0; dude->success = 0;
@ -134,6 +131,7 @@ void handleAction(t_action action, t_dude* dude){
&& target.type != IRON_ORE && target.type != IRON_ORE
&& target.type != DUDE && target.type != DUDE
&& target.type != TREE && target.type != TREE
&& target.type != LIBRARY
) )
add_move(dude, target_pos); add_move(dude, target_pos);
break; break;
@ -151,13 +149,13 @@ void handleAction(t_action action, t_dude* dude){
free(nb_res); free(nb_res);
map[target_pos.x][target_pos.y].type = GRASS; map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL; 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]));
} }
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
}else }else
dude->success = 0; dude->success = 0;
break; break;
case PUT : case PUT :
if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){ if(dude->inventory != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->inventory)){
if(target.type == GRASS || target.type == MARK){ if(target.type == GRASS || target.type == MARK){
map[target_pos.x][target_pos.y].type = dude->inventory; map[target_pos.x][target_pos.y].type = dude->inventory;
nb_res = malloc(sizeof(int)); nb_res = malloc(sizeof(int));
@ -169,8 +167,15 @@ void handleAction(t_action action, t_dude* dude){
} }
dude->inventory = -1; dude->inventory = -1;
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y])); putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
}else }else if(target.type == SPAWN && dude->inventory == FOOD){
printf("put food in spawn\n");
dude->inventory = -1;
team = (t_team*)target.data;
team->spawn_food++;
}else{
printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
dude->success = 0; dude->success = 0;
}
break; break;
case WORK : case WORK :
dude->success = 1; dude->success = 1;
@ -209,12 +214,20 @@ void handleAction(t_action action, t_dude* dude){
break; break;
case WOOD : case WOOD :
nb_res = target.data; nb_res = target.data;
if(*nb_res != 1) switch(*nb_res){
dude->success = 0; case 1 :
else{ free(target.data);
free(target.data); map[target_pos.x][target_pos.y].type = WALL;
map[target_pos.x][target_pos.y].type = WALL; map[target_pos.x][target_pos.y].data = NULL;
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; break;
case STONE : case STONE :
@ -246,8 +259,8 @@ void handleAction(t_action action, t_dude* dude){
break; break;
case COMMUNICATE : case COMMUNICATE :
printf("forbidden action\n"); // TODO : implement that printf("forbidden action\n"); // TODO : implement that
// if target is sign -> set sign message
// if target is dude -> sent message to dude // if target is dude -> sent message to dude
// if target is library -> sets library
break; break;
} }
} }
@ -352,13 +365,13 @@ int MAIN
case SDLK_ESCAPE : case SDLK_ESCAPE :
over = 1; over = 1;
break; break;
case SDLK_w : case SDLK_z :
y_offset -= 5; y_offset -= 5;
break; break;
case SDLK_s : case SDLK_s :
y_offset += 5; y_offset += 5;
break; break;
case SDLK_a : case SDLK_q :
x_offset -= 5; x_offset -= 5;
break; break;
case SDLK_d : case SDLK_d :

4
main.h
View File

@ -15,6 +15,7 @@
#define STACK_SIZE 5 #define STACK_SIZE 5
#define DEFAULT_WIDTH 400 #define DEFAULT_WIDTH 400
#define DEFAULT_HEIGHT 250 #define DEFAULT_HEIGHT 250
#define NB_STARTING_FOOD 5
// Teams // Teams
enum{ enum{
@ -38,8 +39,7 @@ typedef struct{
typedef struct{ typedef struct{
int team; int team;
int dude_size; t_action (*update)(void*, void*, int);
t_action (*update)(void*, void*, int, int);
int nb_dudes; int nb_dudes;
t_dude* dudes; t_dude* dudes;
t_coord spawn; t_coord spawn;

View File

@ -7,11 +7,7 @@ typedef struct{
int plop; // custom info int plop; // custom info
} t_data; } t_data;
int get_orange_size(){ t_action orange_update(void* my_info, void* com_data, int success){
return sizeof(t_data);
}
t_action orange_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info; t_data* data = (t_data*)my_info;
t_action action; t_action action;

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include "team.h" #include "team.h"
#include "stdlib.h" #include "stdlib.h"
@ -7,44 +8,60 @@ typedef struct{
t_coord pos; t_coord pos;
int try; int try;
int brings_food; int brings_food;
int last_dir;
int last_action;
} t_data; } t_data;
int get_purple_size(){
return sizeof(t_data);
}
t_coord getPos(t_coord coord, int dir); t_coord getPos(t_coord coord, int dir);
t_action purple_update(void* my_info, void* com_data, int my_id, int success){ int abs(int val){
return val > 0 ? val : -val;
}
int dist(t_coord coord, int x, int y){
return abs(coord.x-x) + abs(coord.y-y);
}
t_action purple_update(void* my_info, void* com_data, int success){
t_data* data = (t_data*)my_info; t_data* data = (t_data*)my_info;
t_action action; t_action action;
int i, type; int i, type;
if(data->try && success) if(data->last_action == MOVE && success)
data->pos = getPos(data->pos, data->last_dir);
if(data->try && success){
data->brings_food = 1; data->brings_food = 1;
}
data->try = 0; data->try = 0;
if(data->brings_food){ if(data->brings_food){
if(data->pos.x == 0) int distance = dist(data->pos, 0, 0);
action.dir = WEST; if(distance == 1){
else{ action.type = WAIT;
if(data->pos.y == 0){ action.data = NULL;
if(data->pos.x > 0) action.dir = 0;
action.dir = EAST; data->last_action = action.type;
else for(i=0; i<4; i++){
action.dir = WEST; type = getNear(i);
if(data->pos.x == -1 || data->pos.x == 1){ if(type == SPAWN){
data->brings_food = 0; action.dir = i;
action.type = PUT; action.type = PUT;
action.data = NULL; data->brings_food = 0;
return action; data->last_action = action.type;
break;
} }
}else }
action.dir = data->pos.y > 0 ? SOUTH : NORTH; }else{
action.type = MOVE;
do{
action.dir = rand()%4;
}while(dist(getPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
action.data = NULL;
data->pos = getPos(data->pos, action.dir);
data->last_dir = action.dir;
data->last_action = action.type;
} }
action.type = MOVE;
action.data = NULL;
data->pos = getPos(data->pos, action.dir);
return action; return action;
} }
@ -54,18 +71,25 @@ t_action purple_update(void* my_info, void* com_data, int my_id, int success){
action.type = WORK; action.type = WORK;
action.dir = i; action.dir = i;
action.data = NULL; action.data = NULL;
data->last_dir = action.dir;
data->last_action = action.type;
return action; return action;
}else if(type == FOOD){ }else if(type == FOOD){
action.type = PICK; action.type = PICK;
action.dir = i; action.dir = i;
action.data = NULL; action.data = NULL;
data->try = 1; data->try = 1;
data->last_action = action.type;
return action;
} }
} }
action.type = MOVE; action.type = MOVE;
action.dir = rand()%4; do{
action.dir = rand()%4;
}while(action.dir == data->last_dir);
action.data = NULL; action.data = NULL;
data->pos = getPos(data->pos, action.dir); data->last_dir = action.dir;
data->last_action = action.type;
return action; return action;
} }

5
team.h
View File

@ -1,6 +1,9 @@
#ifndef TEAM_H #ifndef TEAM_H
#define TEAM_H #define TEAM_H
#define LIBRARY_SIZE 128
#define DUDE_MEMORY 128
// Directions // Directions
enum{ enum{
NORTH, SOUTH, EAST, WEST NORTH, SOUTH, EAST, WEST
@ -11,7 +14,7 @@ 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, // humans
SPAWN, WALL, ROAD, MARK, SIGN // buildings SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
}; };
// Action types // Action types

View File

@ -68,6 +68,7 @@ Uint32 getColor(t_pixel pixel){
switch(pixel.type){ switch(pixel.type){
case BEDROCK : return 0x101020; case BEDROCK : return 0x101020;
case GRASS : return 0x719678; case GRASS : return 0x719678;
case MARK : return 0x5D7B62;
case ROCK : return 0x8C8C8C; case ROCK : return 0x8C8C8C;
case IRON_ORE : return 0x917B61; case IRON_ORE : return 0x917B61;
case TREE : return 0x003800; case TREE : return 0x003800;
@ -81,12 +82,11 @@ Uint32 getColor(t_pixel pixel){
return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF; return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;
case SPAWN : case SPAWN :
spawnData = (int*)(pixel.data); spawnData = (int*)(pixel.data);
if(spawnData == NULL) printf("WTF\n");
return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF; return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF;
case WALL : return 0xE6B2A1; case WALL : return 0xE6B2A1;
case ROAD : return 0xEDB287; case ROAD : return 0xEDB287;
case SWORD : return 0xEBEBEB; case SWORD : return 0xEBEBEB;
case SIGN : return 0xA37A50; case LIBRARY : return 0xA37A50;
default : return 0x0000FF; // bleu absolu = bug default : return 0x0000FF; // bleu absolu = bug
} }