AST/arenaEngine.c
2018-06-17 17:53:24 +02:00

123 lines
2.7 KiB
C

#include "arenaEngine.h"
#include <stdlib.h>
#include <stdio.h>
#include "fileHandler.h"
#include "logHelper.h"
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->isGround = 1;
tile_h->canBeMined = 0;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
return tile_h;
} else if (prevHTile != NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE));
prevHTile->nextRow = tile_h;
tile_h->type_id = 0;
tile_h->isGround = 1;
tile_h->canBeMined = 0;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
}
return NULL;
}
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->isGround = 1;
tile_w->canBeMined = 0;
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->isGround = 1;
tile_w->canBeMined = 0;
tile_w->nextColumn = NULL;
}
return NULL;
}
ARENA_H_TILE* genNewArena(int h, int w) {
ARENA_H_TILE* arenaOrigin = NULL;
ARENA_H_TILE* TmpCursor_h = NULL;
ARENA_W_TILE* TmpCursor_w = NULL;
int i,j;
for(i=0;i<=h;i++){
if (i==0) {
arenaOrigin = createHTile(NULL);
TmpCursor_h = arenaOrigin;
} else {
createHTile(TmpCursor_h);
}
for(j=0;j<w;j++){
if (j==0) {
TmpCursor_w = createWTile(TmpCursor_h, NULL);
} else {
createWTile(NULL, TmpCursor_w);
}
if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn;
}
if (i!=0) TmpCursor_h = TmpCursor_h->nextRow;
}
return arenaOrigin;
}
void deleteWTile(ARENA_W_TILE* WTile, int* failNbr) {
addLogInfo("st2");
while (WTile->nextColumn != NULL) {
addLogInfo("goto end2");
deleteWTile(WTile->nextColumn, failNbr);
}
addLogInfo("suppress 1 column");
free(WTile);
if (WTile != NULL) (*failNbr)++;
}
void deleteHTile(ARENA_H_TILE* HTile, int* failNbr) {
//addLogInfo("st1");
if(HTile->nextColumn == NULL && HTile->nextRow == NULL) addLogInfo("yup3");
while (HTile->nextRow != NULL) {
if(HTile->nextColumn == NULL) addLogInfo("yup");
//addLogInfo("goto end1");
deleteHTile(HTile->nextRow, failNbr);
}
addLogInfo("suppress 1 column");
if(HTile->nextColumn == NULL) addLogInfo("yup2");
deleteWTile(HTile->nextColumn, failNbr);
free(HTile);
if (HTile != NULL) (*failNbr)++;
}
int deleteArena(ARENA_H_TILE* arena) {
int fnbr = 0;
int* failNbr = &fnbr;
deleteHTile(arena, failNbr);
return *failNbr;
}