Arena generator v1
This commit is contained in:
parent
9acfce6b5f
commit
e891ab880e
130
arenaEngine.c
130
arenaEngine.c
@ -107,6 +107,7 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
|
||||
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;
|
||||
@ -114,6 +115,7 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
|
||||
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;
|
||||
@ -129,6 +131,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
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;
|
||||
|
||||
@ -137,6 +140,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
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;
|
||||
}
|
||||
@ -144,18 +148,20 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int setTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y, int new_id) {
|
||||
int setTileTypeID(ARENA_H_TILE* arena, int x, int y, int new_id) {
|
||||
int i;
|
||||
//x=coord_x-1;
|
||||
//y=coord_y-1;
|
||||
|
||||
if (coord_x > A_WIDTH || coord_y > A_HEIGHT) return -1;
|
||||
if (x > A_WIDTH || y > A_HEIGHT || x < 0 || y < 0) return -1;
|
||||
|
||||
if (coord_y == 0) {
|
||||
if (y == 0) {
|
||||
ARENA_H_TILE* tile_h = NULL;
|
||||
|
||||
tile_h = arena;
|
||||
if (coord_x != 0) {
|
||||
for (i=0;i<coord_x;i++) {
|
||||
tile_h = arena->nextRow;
|
||||
if (x != 0) {
|
||||
for (i=0;i<x;i++) {
|
||||
tile_h = tile_h->nextRow;
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,15 +173,15 @@ int setTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y, int new_id) {
|
||||
ARENA_H_TILE* tile_h = NULL;
|
||||
|
||||
tile_h = arena;
|
||||
if (coord_x != 0) {
|
||||
for (i=0;i<coord_x;i++) {
|
||||
if (x != 0) {
|
||||
for (i=0;i<x;i++) {
|
||||
tile_h = tile_h->nextRow;
|
||||
}
|
||||
}
|
||||
|
||||
tile_w = tile_h->nextColumn;
|
||||
if (coord_y != 0) {
|
||||
for (i=0;i<coord_y;i++) {
|
||||
if (y != 0) {
|
||||
for (i=0;i<y;i++) {
|
||||
tile_w = tile_w->nextColumn;
|
||||
}
|
||||
}
|
||||
@ -196,7 +202,7 @@ ARENA_H_TILE* genNewArena(int size_h, int size_w) {
|
||||
/*
|
||||
* Generate flatgrass arena
|
||||
*/
|
||||
for(i=0;i<size_h;i++){
|
||||
for(i=0;i<=size_h;i++){
|
||||
if (i==0) {
|
||||
arenaOrigin = createHTile(NULL);
|
||||
TmpCursor_h = arenaOrigin;
|
||||
@ -204,7 +210,7 @@ ARENA_H_TILE* genNewArena(int size_h, int size_w) {
|
||||
TmpCursor_h = createHTile(TmpCursor_h);
|
||||
}
|
||||
|
||||
for(j=0;j<size_w;j++){
|
||||
for(j=0;j<=size_w;j++){
|
||||
if (j==0) {
|
||||
TmpCursor_w = createWTile(TmpCursor_h, NULL);
|
||||
} else {
|
||||
@ -218,15 +224,69 @@ ARENA_H_TILE* genNewArena(int size_h, int size_w) {
|
||||
/*
|
||||
* Generate some rock area
|
||||
*/
|
||||
for (z=0; z < random_lim(101) * ROCK_GEN_RATE/100; z++) {
|
||||
rand_x = random_lim(A_WIDTH);
|
||||
rand_y = random_lim(A_HEIGHT);
|
||||
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=-3;i>=3;i++) {
|
||||
for (j=-3;j>=3;j++) {
|
||||
if (random_lim(101) < 50) {
|
||||
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.");
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -270,8 +330,10 @@ void deleteArena(ARENA_H_TILE* arena) {
|
||||
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) return -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;
|
||||
@ -279,7 +341,7 @@ int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) {
|
||||
tile_h = arena;
|
||||
if (coord_x != 0) {
|
||||
for (i=0;i<coord_x;i++) {
|
||||
tile_h = arena->nextRow;
|
||||
tile_h = tile_h->nextRow;
|
||||
}
|
||||
}
|
||||
|
||||
@ -308,8 +370,7 @@ int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) {
|
||||
return type_id;
|
||||
}
|
||||
|
||||
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id) {
|
||||
SDL_Surface *t_surface = NULL;
|
||||
int isGroundTile(TILE *t_list, int id) {
|
||||
TILE *tmp_tile = NULL;
|
||||
int i=0;
|
||||
|
||||
@ -319,9 +380,20 @@ SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id) {
|
||||
i++;
|
||||
}
|
||||
|
||||
t_surface = tmp_tile->texture;
|
||||
return tmp_tile->isGround;
|
||||
}
|
||||
|
||||
return t_surface;
|
||||
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) {
|
||||
@ -332,8 +404,12 @@ int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
|
||||
return adjPlayer;
|
||||
}
|
||||
|
||||
int isMoveCorrect(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))) return 1;
|
||||
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;
|
||||
}
|
||||
|
@ -71,9 +71,10 @@ void deleteArena(ARENA_H_TILE* arena);
|
||||
|
||||
//Status functions
|
||||
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y);
|
||||
int isGroundTile(TILE *t_list, int id);
|
||||
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id);
|
||||
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2);
|
||||
int isMoveCorrect(int coord_x, int coord_y, int direction);
|
||||
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction);
|
||||
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2);
|
||||
|
||||
#endif
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
4
main.c
4
main.c
@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {
|
||||
*/
|
||||
|
||||
addLogInfo("Create SDL windows instance...");
|
||||
SDL_Window* gameWindows = SDL_CreateWindow("Arena Survival Tournament - alpha 0.2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOWS_WIDTH, WINDOWS_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
|
||||
SDL_Window* gameWindows = SDL_CreateWindow("Arena Survival Tournament - alpha 0.2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOWS_WIDTH, WINDOWS_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);
|
||||
|
||||
addLogInfo("Creating new arena...");
|
||||
ARENA_H_TILE* arena = NULL;
|
||||
@ -76,7 +76,7 @@ int main(int argc, char *argv[]) {
|
||||
displayArena(arena, gameWindows, tile_ressources, A_HEIGHT, A_WIDTH, TILE_SIZE);
|
||||
|
||||
|
||||
SDL_Delay(5000);
|
||||
SDL_Delay(10000);
|
||||
|
||||
deleteArena(arena);
|
||||
addLogInfo("Cleared arena.");
|
||||
|
8
main.h
8
main.h
@ -1,8 +1,8 @@
|
||||
#ifndef MAIN_H_
|
||||
#define MAIN_H_
|
||||
|
||||
#define A_WIDTH 30
|
||||
#define A_HEIGHT 30
|
||||
#define A_WIDTH 20
|
||||
#define A_HEIGHT 20
|
||||
|
||||
#define TILE_SIZE 32
|
||||
#define WINDOWS_WIDTH TILE_SIZE * A_WIDTH
|
||||
@ -11,8 +11,8 @@
|
||||
/*
|
||||
* Generator tiles spawn rate (in percent)
|
||||
*/
|
||||
#define ROCK_GEN_RATE 25
|
||||
#define TREE_GEN_RATE 35
|
||||
#define ROCK_GEN_RATE 30
|
||||
#define TREE_GEN_RATE 25
|
||||
#define WATER_GEN_RATE 20
|
||||
#define GOLD_GEN_RATE 10
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user