worked on move action
This commit is contained in:
parent
839eebe5d4
commit
ade7d5d38b
10
Makefile
10
Makefile
@ -22,13 +22,13 @@ orange : orange.o
|
|||||||
|
|
||||||
purple : purple.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
|
$(BINARY) : orange.o purple.o main.o generator.o tools.o
|
||||||
$(CC) main.o generator.o orange.o purple.o -o $(BINARY) $(LIB)
|
$(CC) main.o generator.o orange.o purple.o tools.o -o $(BINARY) $(LIB)
|
||||||
|
|
||||||
anselme : orange.o purple.o main.o generator_anselme.o
|
anselme : orange.o purple.o main.o generator_anselme.o tools.o
|
||||||
$(CC) main.o generator_anselme.o orange.o purple.o -o $(BINARY) $(LIB)
|
$(CC) main.o generator_anselme.o orange.o purple.o tools.o -o $(BINARY) $(LIB)
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(CC) -o $@ -c $< $(FLAGS)
|
$(CC) -o $@ -c $< $(FLAGS)
|
||||||
|
83
main.c
83
main.c
@ -1,11 +1,18 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "team.h"
|
#include "team.h"
|
||||||
|
#include "tools.h"
|
||||||
#include "generator.h"
|
#include "generator.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
t_dude dude1;
|
||||||
|
t_dude dude2;
|
||||||
|
int attack;
|
||||||
|
} t_fight;
|
||||||
|
|
||||||
t_pixel** map;
|
t_pixel** map;
|
||||||
t_team* teams;
|
t_team* teams;
|
||||||
SDL_Surface* img;
|
SDL_Surface* img;
|
||||||
@ -17,7 +24,7 @@ int get_purple_size();
|
|||||||
int get_orange_size();
|
int get_orange_size();
|
||||||
|
|
||||||
void initWorld(){
|
void initWorld(){
|
||||||
int i, j;
|
int i;
|
||||||
|
|
||||||
// allocations
|
// allocations
|
||||||
printf("Allocating memory...\n");
|
printf("Allocating memory...\n");
|
||||||
@ -68,7 +75,7 @@ void updateTeam(t_team team){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_pixel getTile(t_coord coord, int dir){
|
t_coord getPos(t_coord coord, int dir){
|
||||||
switch(dir){
|
switch(dir){
|
||||||
case NORTH :
|
case NORTH :
|
||||||
coord.x++;
|
coord.x++;
|
||||||
@ -83,7 +90,7 @@ t_pixel getTile(t_coord coord, int dir){
|
|||||||
coord.y--;
|
coord.y--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return map[coord.x][coord.y];
|
return coord;
|
||||||
}
|
}
|
||||||
|
|
||||||
void spawnDudes(){
|
void spawnDudes(){
|
||||||
@ -111,7 +118,8 @@ void spawnDudes(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleAction(t_action action, t_dude dude){
|
void handleAction(t_action action, t_dude dude){
|
||||||
t_pixel target = getTile(dude.pos, action.dir);
|
t_coord target_pos = getPos(dude.pos, action.dir);
|
||||||
|
t_pixel target = map[target_pos.x][target_pos.y];
|
||||||
switch(action.type){
|
switch(action.type){
|
||||||
case MOVE :
|
case MOVE :
|
||||||
if( target.type != WALL
|
if( target.type != WALL
|
||||||
@ -120,12 +128,8 @@ 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
|
||||||
){
|
)
|
||||||
map[dude.pos.x][dude.pos.y] = dude.ground;
|
add_move(&dude, target_pos, map);
|
||||||
dude.ground = target;
|
|
||||||
target.type = DUDE;
|
|
||||||
target.data = &dude;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ATTACK :
|
case ATTACK :
|
||||||
if(target.type == DUDE)
|
if(target.type == DUDE)
|
||||||
@ -208,62 +212,6 @@ void generateImg(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
void initSDL()
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
@ -288,6 +236,8 @@ int MAIN( int argc, char** argv )
|
|||||||
initWorld();
|
initWorld();
|
||||||
SDL_Flip(img);
|
SDL_Flip(img);
|
||||||
|
|
||||||
|
printf("Launching simulation... press ESCAPE to stop.\n");
|
||||||
|
|
||||||
while (!keystate[SDLK_ESCAPE] && !over){
|
while (!keystate[SDLK_ESCAPE] && !over){
|
||||||
temps = SDL_GetTicks();
|
temps = SDL_GetTicks();
|
||||||
SDL_PumpEvents();
|
SDL_PumpEvents();
|
||||||
@ -295,6 +245,7 @@ int MAIN( int argc, char** argv )
|
|||||||
//spawnDudes();
|
//spawnDudes();
|
||||||
for(i=0; i<NB_TEAMS; i++)
|
for(i=0; i<NB_TEAMS; i++)
|
||||||
updateTeam(teams[i]);
|
updateTeam(teams[i]);
|
||||||
|
resolve_moves();
|
||||||
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
|
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
|
||||||
SDL_Flip(img);
|
SDL_Flip(img);
|
||||||
}
|
}
|
||||||
|
2
main.h
2
main.h
@ -47,8 +47,6 @@ typedef struct{
|
|||||||
int spawn_count;
|
int spawn_count;
|
||||||
} t_team;
|
} t_team;
|
||||||
|
|
||||||
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
|
|
||||||
|
|
||||||
void generateImg();
|
void generateImg();
|
||||||
|
|
||||||
void handleAction(t_action action, t_dude dude);
|
void handleAction(t_action action, t_dude dude);
|
||||||
|
111
tools.c
Normal file
111
tools.c
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#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;
|
||||||
|
t_pixel** map;
|
||||||
|
|
||||||
|
void add_move(t_dude* dude, t_coord dst, t_pixel** the_map){
|
||||||
|
map = the_map;
|
||||||
|
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;
|
||||||
|
move->dude->ground = target;
|
||||||
|
target.type = DUDE;
|
||||||
|
target.data = move->dude;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
}
|
16
tools.h
Normal file
16
tools.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#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, t_pixel** the_map);
|
||||||
|
|
||||||
|
void resolve_moves();
|
||||||
|
|
||||||
|
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