AST/arenaEngine.c

435 lines
10 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include "fileHandler.h"
#include "logHelper.h"
#include "arenaEngine.h"
/*
* Arena generate functions
*/
TILE *createTileList(void) {
TILE *tile_0 = NULL, *tile_1 = NULL, *tile_2 = NULL, *tile_3 = NULL, *tile_4 = NULL;
tile_0 = calloc(1,sizeof(TILE));
tile_1 = calloc(1,sizeof(TILE));
tile_2 = calloc(1,sizeof(TILE));
tile_3 = calloc(1,sizeof(TILE));
tile_4 = calloc(1,sizeof(TILE));
tile_0->type_id = 0;
tile_0->texture = IMG_Load("data/tile_grass.png");
tile_0->canBeMined = 0;
tile_0->isGround = 1;
tile_0->nextTile = tile_1;
tile_1->type_id = 1;
tile_1->texture = IMG_Load("data/tile_rock.png");
tile_1->canBeMined = 0;
tile_1->isGround = 1;
tile_1->nextTile = tile_2;
tile_2->type_id = 2;
tile_2->texture = IMG_Load("data/tile_tree.png");
tile_2->canBeMined = 0;
tile_2->isGround = 0;
tile_2->nextTile = tile_3;
tile_3->type_id = 3;
tile_3->texture = IMG_Load("data/tile_water.png");
tile_3->canBeMined = 0;
tile_3->isGround = 0;
tile_3->nextTile = tile_4;
tile_4->type_id = 4;
tile_4->texture = IMG_Load("data/tile_gold.png");
tile_4->canBeMined = 1;
tile_4->isGround = 1;
tile_4->nextTile = NULL;
return tile_0;
}
PLAYER *createPlayerList(void) {
PLAYER *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL;
p0 = calloc(1,sizeof(PLAYER));
p1 = calloc(1,sizeof(PLAYER));
p2 = calloc(1,sizeof(PLAYER));
p3 = calloc(1,sizeof(PLAYER));
p0->Id = 0;
p0->PositionX = 0;
p0->PositionY = 0;
p0->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
p0->texture[UP] = IMG_Load("data/sprite_player_1.png");
p0->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
p0->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p0->suiv = p1;
p1->Id = 1;
p1->PositionX = A_WIDTH;
p1->PositionY = A_HEIGHT;
p1->texture[DOWN] = IMG_Load("data/sprite_ia_0.png");
p1->texture[UP] = IMG_Load("data/sprite_ia_1.png");
p1->texture[LEFT] = IMG_Load("data/sprite_ia_2.png");
p1->texture[RIGHT] = IMG_Load("data/sprite_ia_3.png");
p1->suiv = p2;
p2->Id = 0;
p2->PositionX = 0;
p2->PositionY = A_HEIGHT;
//p2->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
//p2->texture[UP] = IMG_Load("data/sprite_player_1.png");
//p2->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
//p2->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p2->suiv = p3;
p3->Id = 0;
p3->PositionX = A_WIDTH;
p3->PositionY = 0;
//p3->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
//p3->texture[UP] = IMG_Load("data/sprite_player_1.png");
//p3->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
//p3->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p3->suiv = NULL;
return p1;
}
void clearRessourcesCache(TILE *t, PLAYER *p) {
}
ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
ARENA_H_TILE* tile_h = NULL;
if (prevHTile == NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE)); //Using calloc because of resetting all memory allocated to 0
tile_h->type_id = 0;
//tile_h->type_id = random_lim(5);
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
} else if (prevHTile != NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE));
prevHTile->nextRow = tile_h;
tile_h->type_id = 0;
//tile_h->type_id = random_lim(5);
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
}
return tile_h;
}
ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
ARENA_W_TILE* tile_w = NULL;
if (prevHTile != NULL && prevWTile == NULL) {
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevHTile->nextColumn = tile_w;
tile_w->type_id = 0;
//tile_w->type_id = random_lim(5);
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
return tile_w;
} else if (prevHTile == NULL && prevWTile != NULL) {
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevWTile->nextColumn = tile_w;
tile_w->type_id = 0;
//tile_w->type_id = random_lim(5);
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
}
return NULL;
}
int setTileTypeID(ARENA_H_TILE* arena, int x, int y, int new_id) {
int i;
//x=coord_x-1;
//y=coord_y-1;
if (x > A_WIDTH || y > A_HEIGHT || x < 0 || y < 0) return -1;
if (y == 0) {
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (x != 0) {
for (i=0;i<x;i++) {
tile_h = tile_h->nextRow;
}
}
tile_h->type_id = new_id;
if (tile_h->type_id == new_id) return 0;
} else {
ARENA_W_TILE* tile_w = NULL;
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (x != 0) {
for (i=0;i<x;i++) {
tile_h = tile_h->nextRow;
}
}
tile_w = tile_h->nextColumn;
if (y != 0) {
for (i=0;i<y;i++) {
tile_w = tile_w->nextColumn;
}
}
tile_w->type_id = new_id;
if (tile_w->type_id == new_id) return 0;
}
return -1;
}
ARENA_H_TILE* genNewArena(int size_h, int size_w) {
ARENA_H_TILE* arenaOrigin = NULL;
ARENA_H_TILE* TmpCursor_h = NULL;
ARENA_W_TILE* TmpCursor_w = NULL;
int z,i,j,rand_x,rand_y;
/*
* Generate flatgrass arena
*/
for(i=0;i<=size_h;i++){
if (i==0) {
arenaOrigin = createHTile(NULL);
TmpCursor_h = arenaOrigin;
} else {
TmpCursor_h = createHTile(TmpCursor_h);
}
for(j=0;j<=size_w;j++){
if (j==0) {
TmpCursor_w = createWTile(TmpCursor_h, NULL);
} else {
createWTile(NULL, TmpCursor_w);
}
if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn;
}
}
/*
* Generate some rock area
*/
for (z=1; z <= A_WIDTH * A_HEIGHT * ROCK_GEN_RATE/100; z++) {
rand_x = random_lim(A_WIDTH+1)-1;
rand_y = random_lim(A_HEIGHT+1)-1;
for (i=-2;i<=1;i++) {
for (j=-1;j<=3;j++) {
if (random_lim(101) < 65) {
if ((rand_x + i >= 0 && rand_x + i <= A_WIDTH) || (rand_y + i <= 0 && rand_y + i <= A_HEIGHT)) {
if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, ROCK)!=0) addLogCritical("Rock failed to add on arena."); else z++;
}
}
}
}
}
/*
* Generate some tree area on grass
*/
for (z=1; z <= A_WIDTH * A_HEIGHT * TREE_GEN_RATE/100; z++) {
rand_x = random_lim(A_WIDTH+1)-1;
rand_y = random_lim(A_HEIGHT+1)-1;
for (i=-2;i<=2;i++) {
for (j=-2;j<=2;j++) {
if (random_lim(101) < 65) {
if (((rand_x + i >= 0 && rand_x + i <= A_WIDTH) || (rand_y + i <= 0 && rand_y + i <= A_HEIGHT)) && getTileTypeID(arenaOrigin, rand_x+i, rand_y+j) == GRASS) {
if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, TREE)!=0) addLogCritical("Tree failed to add on arena."); else z++;
}
}
}
}
}
/*
* Generate some gold area on rock
*/
for (z=1; z <= A_WIDTH * A_HEIGHT * GOLD_GEN_RATE/100; z++) {
rand_x = random_lim(A_WIDTH+1)-1;
rand_y = random_lim(A_HEIGHT+1)-1;
if (((rand_x + i >= 0 && rand_x + i <= A_WIDTH) || (rand_y + i <= 0 && rand_y + i <= A_HEIGHT)) && getTileTypeID(arenaOrigin, rand_x+i, rand_y+j) == ROCK) {
if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, GOLD)!=0) addLogCritical("Tree failed to add on arena."); else z++;
}
}
/*
* Generate some water area on grass
*/
for (z=1; z <= A_WIDTH * A_HEIGHT * WATER_GEN_RATE/100; z++) {
rand_x = random_lim(A_WIDTH+1)-1;
rand_y = random_lim(A_HEIGHT+1)-1;
for (i=-3;i<=3;i++) {
for (j=-3;j<=3;j++) {
if (i >= -1 && j >= -1 && i <= 1 && j <= 1) {
if ((rand_x + i >= 0 && rand_x + i <= A_WIDTH) || (rand_y + i <= 0 && rand_y + i <= A_HEIGHT)) {
if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, WATER)!=0) addLogCritical("Tree failed to add on arena."); else z++;
}
} else if (i < -1 && j < -1 && i > 1 && j > 1) {
if (random_lim(101) < 60) {
if ((rand_x + i >= 0 && rand_x + i <= A_WIDTH) || (rand_y + i <= 0 && rand_y + i <= A_HEIGHT)) {
if(setTileTypeID(arenaOrigin, rand_x+i, rand_y+j, WATER)!=0) addLogCritical("Tree failed to add on arena."); else z++;
}
}
}
}
}
}
return arenaOrigin;
}
/*
* Arena delete functions
*/
void deleteWTile(ARENA_W_TILE* WTile) {
if (WTile->nextColumn != NULL) {
deleteWTile(WTile->nextColumn);
}
free(WTile);
}
void deleteHTile(ARENA_H_TILE* HTile) {
if (HTile->nextRow != NULL) {
deleteHTile(HTile->nextRow);
}
deleteWTile(HTile->nextColumn);
free(HTile);
}
void deleteArena(ARENA_H_TILE* arena) {
deleteHTile(arena);
}
/*
* Arena status functions
*/
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) {
int type_id = -1;
int i;
//coord_x=x-1;
//coord_y=y-1;
if (coord_x > A_WIDTH || coord_y > A_HEIGHT || coord_x < 0 || coord_y < 0) return -1;
if (coord_y == 0) {
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (coord_x != 0) {
for (i=0;i<coord_x;i++) {
tile_h = tile_h->nextRow;
}
}
type_id = tile_h->type_id;
} else {
ARENA_W_TILE* tile_w = NULL;
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (coord_x != 0) {
for (i=0;i<coord_x;i++) {
tile_h = tile_h->nextRow;
}
}
tile_w = tile_h->nextColumn;
if (coord_y != 0) {
for (i=0;i<coord_y;i++) {
tile_w = tile_w->nextColumn;
}
}
type_id = tile_w->type_id;
}
return type_id;
}
int isGroundTile(TILE *t_list, int id) {
TILE *tmp_tile = NULL;
int i=0;
tmp_tile = t_list;
while (i<id) {
tmp_tile = tmp_tile->nextTile;
i++;
}
return tmp_tile->isGround;
}
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id) {
TILE *tmp_tile = NULL;
int i=0;
tmp_tile = t_list;
while (i<id) {
tmp_tile = tmp_tile->nextTile;
i++;
}
return tmp_tile->texture;
}
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
int adjPlayer = -1;
if ((p1->PositionX-1 || p1->PositionX+1) == p2->PositionX && (p1->PositionY-1 || p1->PositionY+1) == p2->PositionY) adjPlayer = 1;
return adjPlayer;
}
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction) {
if (!((coord_x <= 0 && direction == LEFT) || (coord_y <= 0 && direction == UP) || (coord_x >= A_HEIGHT && direction == RIGHT) || (coord_y >= A_WIDTH && direction == DOWN))) {
if (isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y))) {
return 1;
}
}
return 0;
}
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2) {
int _x,_y;
_x = pos2.x - pos1.x;
_y = pos2.y - pos1.y;
if (_x>1) {
return DOWN;
} else if (_x<1) {
return UP;
} else if (_y>1) {
return RIGHT;
} else if (_y<1) {
return LEFT;
} else {
return -1;
}
}