Random generator v2
This commit is contained in:
parent
2566ef569c
commit
9acfce6b5f
@ -106,14 +106,14 @@ 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 = random_lim(5);
|
||||
tile_h->type_id = 0;
|
||||
//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 = random_lim(5);
|
||||
tile_h->type_id = 0;
|
||||
//tile_h->playerOnTile = NULL;
|
||||
tile_h->nextRow = NULL;
|
||||
tile_h->nextColumn = NULL;
|
||||
@ -128,7 +128,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
if (prevHTile != NULL && prevWTile == NULL) {
|
||||
tile_w = calloc(1,sizeof(ARENA_W_TILE));
|
||||
prevHTile->nextColumn = tile_w;
|
||||
tile_w->type_id = random_lim(5);
|
||||
tile_w->type_id = 0;
|
||||
//tile_w->playerOnTile = NULL;
|
||||
tile_w->nextColumn = NULL;
|
||||
|
||||
@ -136,7 +136,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
} else if (prevHTile == NULL && prevWTile != NULL) {
|
||||
tile_w = calloc(1,sizeof(ARENA_W_TILE));
|
||||
prevWTile->nextColumn = tile_w;
|
||||
tile_w->type_id = random_lim(5);
|
||||
tile_w->type_id = 0;
|
||||
//tile_w->playerOnTile = NULL;
|
||||
tile_w->nextColumn = NULL;
|
||||
}
|
||||
@ -144,12 +144,58 @@ 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 i;
|
||||
|
||||
if (coord_x > A_WIDTH || coord_y > A_HEIGHT) 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 = arena->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 (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;
|
||||
}
|
||||
}
|
||||
|
||||
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 i,j;
|
||||
int z,i,j,rand_x,rand_y;
|
||||
|
||||
/*
|
||||
* Generate flatgrass arena
|
||||
*/
|
||||
for(i=0;i<size_h;i++){
|
||||
if (i==0) {
|
||||
arenaOrigin = createHTile(NULL);
|
||||
@ -169,6 +215,24 @@ 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 (i=-3;i>=3;i++) {
|
||||
for (j=-3;j>=3;j++) {
|
||||
if (random_lim(101) < 50) {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arenaOrigin;
|
||||
}
|
||||
|
||||
|
@ -65,6 +65,7 @@ typedef struct arena_w_tile{ //Columns chained list
|
||||
TILE* createTileList(void);
|
||||
PLAYER *createPlayerList(void);
|
||||
void clearRessourcesCache(TILE *t, PLAYER *p);
|
||||
int setTileTypeID(ARENA_H_TILE* arena, int x, int y, int new_id);
|
||||
ARENA_H_TILE* genNewArena(int size_h, int size_w);
|
||||
void deleteArena(ARENA_H_TILE* arena);
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 2.6 KiB |
2
main.c
2
main.c
@ -5,7 +5,7 @@
|
||||
#include "logHelper.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
//#include <SDL2/SDL_ttf.h>
|
||||
//#include <SDL2/SDL_thread.h>
|
||||
//#include <SDL2/SDL_mutex.h>
|
||||
#include "arenaEngine.h"
|
||||
|
9
main.h
9
main.h
@ -8,7 +8,16 @@
|
||||
#define WINDOWS_WIDTH TILE_SIZE * A_WIDTH
|
||||
#define WINDOWS_HEIGHT TILE_SIZE * A_HEIGHT
|
||||
|
||||
/*
|
||||
* Generator tiles spawn rate (in percent)
|
||||
*/
|
||||
#define ROCK_GEN_RATE 25
|
||||
#define TREE_GEN_RATE 35
|
||||
#define WATER_GEN_RATE 20
|
||||
#define GOLD_GEN_RATE 10
|
||||
|
||||
enum {DOWN, UP, LEFT, RIGHT};
|
||||
enum {GRASS, ROCK, TREE, WATER, GOLD};
|
||||
|
||||
int random_lim(int max);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user