My function:

Code:
void getNewMap(uint8_t outputArray[DUNGEON_WIDTH][DUNGEON_HEIGHT])
{
   uint8_t i;
   uint8_t j = 1;
   for( i = 0; i < BSP_SUBDIVISIONS; i++ ) {
      j = j + ipow(2, i + 1); // Separate function
   };
   uint8_t bspLeaves[j][4]; // Line causing issues
   bspLeaves[0][0] = 0;
}


Compiler output:
Quote:
"[compiling C] src/main.c"
C:\Users\Ben\Desktop\Stuff\Programming\TI-84 Plus CE C\PDCE\src\main.c
C:\USERS\BEN\DESKTOP\STUFF\PROGRAMMING\TI-84 PLUS CE C\PDCE\SRC\MAIN.C (68,5) : ERROR (100) Syntax error
C:\USERS\BEN\DESKTOP\STUFF\PROGRAMMING\TI-84 PLUS CE C\PDCE\SRC\MAIN.C (68,16) : ERROR (128) Identifier "bspLeaves" not defined within current scope
C:\USERS\BEN\DESKTOP\STUFF\PROGRAMMING\TI-84 PLUS CE C\PDCE\SRC\MAIN.C (68,19) : ERROR (141) Object cannot be subscripted
C:\USERS\BEN\DESKTOP\STUFF\PROGRAMMING\TI-84 PLUS CE C\PDCE\SRC\MAIN.C (69,15) : ERROR (141) Object cannot be subscripted
make: *** [obj/main.src] Error -1


I'm new to CE C, and I can't figure out what I'm doing wrong.

EDIT:
It appears that C is trying to initialize a variable array, which it is unable to do. How do I initialize a static array using a variable input?
You can't initialize a static array with a dynamic size. You'll have to use malloc and a pointer.
Also, with ZDS you can't mix declarations with code. You have to define all of your variables before adding any line that's not a variable declaration.
I'm having a new problem now, and if anyone could help me, that would be great.
I am trying to randomly assign top-left and bottom-right coordinates to BSP leaves and place them in arrays. Most of it is working correctly, but if you run it on-calc, sometimes some of the leaves will be twice their intended size.
Source code:

Code:
/* Required headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* CE C library headers */
#include <graphx.h>

/* Custom headers */

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */

/* Definitions */

/* Dungeon width and height in tiles */
#define DUNGEON_WIDTH 64
#define DUNGEON_HEIGHT 64

/* BSP_SUBDIVISIONS means there will be 2^BSP_SUBDIVISIONS dungeon rooms. More rooms means more loot. */
#define BSP_SUBDIVISIONS 3

/* LEAF_COUNT is calculated by 1 + (2^1 + 2^2 + ... + 2^BSP_SUBDIVISIONS) */
/* THIS MUST BE BASED ON THE BSP_SUBDIVISIONS DEFINITION OR IT WILL NOT WORK! */
#define LEAF_COUNT 15

/* Function prototypes */
void getNewMap(uint8_t outputArray[DUNGEON_WIDTH][DUNGEON_HEIGHT]);
uint8_t ipow(uint8_t base, uint8_t exp);
int roundInt(const uint8_t n, const uint8_t d);

/* Main code */
void main(void)
{
   /* Variable declarations */
   uint8_t map[DUNGEON_WIDTH][DUNGEON_HEIGHT];
   uint8_t secs, mins, hrs;
   uint8_t *secsP, *minsP, *hrsP;
   uint32_t seed;
   
   /* Initialize random number generator */
   secsP = &secs;
   minsP = &mins;
   hrsP = &hrs;
   boot_GetTime(secsP, minsP, hrsP);
   seed = ((hrs / 60) / 60) + (mins / 60) + secs;
   srandom(seed);
   
   /* Initialize GraphX palette */
    gfx_Begin();
    gfx_SetTransparentColor(0);
    gfx_SetTextTransparentColor(0);
    gfx_SetTextBGColor(0);
    gfx_SetTextFGColor(2);
   
   /* Display title screen */
    gfx_FillScreen(1);
    // gfx_Sprite_NoClip(title_screen_l, 0, 0);
    // gfx_Sprite_NoClip(title_screen_r, 160, 0);
   
   /* Generate new map */
   getNewMap(map);
   
    /* Wait for a key press */
    while (!os_GetCSC());
    gfx_End();
}

/* Create a new dungeon map in an array */
void getNewMap(uint8_t outputArray[DUNGEON_WIDTH][DUNGEON_HEIGHT])
{
   /* My (hopefully not, but probably) crappy implementation of BSP */
   
   /* Declare variables */
   uint8_t bspLeaves[LEAF_COUNT][4];
   uint8_t i, k, isVert, parentLeaf, randSize;
   uint8_t j = 1;
   
   /* Declare default leaves array values */
   bspLeaves[0][0] = bspLeaves[0][1] = 0;
   bspLeaves[0][2] = DUNGEON_WIDTH - 1;
   bspLeaves[0][3] = DUNGEON_HEIGHT - 1;
   
   /* Loop through array and create BSP leaves */
   for (i = 0; i < BSP_SUBDIVISIONS; i++) {
      
      for (k = 0; k < ipow(2, i + 1); k = k + 2) {
         
         isVert = random() % 2;
         randSize = (random() % 6) + 5;
         parentLeaf = j - ipow(2, i) + roundInt(k, 2);
         
         if (isVert) {
            
            /* Assign first leaf coords */
            bspLeaves[j + k][0] = bspLeaves[parentLeaf][0];
            bspLeaves[j + k][1] = bspLeaves[parentLeaf][1];
            bspLeaves[j + k][2] = bspLeaves[parentLeaf][2] * randSize / 16;
            bspLeaves[j + k][3] = bspLeaves[parentLeaf][3];
            
            /* Assign second leaf coords */
            bspLeaves[j + k + 1][0] = bspLeaves[j + k][2] + 1;
            bspLeaves[j + k + 1][1] = bspLeaves[parentLeaf][1];
            bspLeaves[j + k + 1][2] = bspLeaves[parentLeaf][2];
            bspLeaves[j + k + 1][3] = bspLeaves[parentLeaf][3];
            
         } else {
            
            /* Assign first leaf coords */
            bspLeaves[j + k][0] = bspLeaves[parentLeaf][0];
            bspLeaves[j + k][1] = bspLeaves[parentLeaf][1];
            bspLeaves[j + k][2] = bspLeaves[parentLeaf][2];
            bspLeaves[j + k][3] = bspLeaves[parentLeaf][3] * randSize / 16;
            
            /* Assign second leaf coords */
            bspLeaves[j + k + 1][0] = bspLeaves[parentLeaf][0];
            bspLeaves[j + k + 1][1] = bspLeaves[j + k][3] + 1;
            bspLeaves[j + k + 1][2] = bspLeaves[parentLeaf][2];
            bspLeaves[j + k + 1][3] = bspLeaves[parentLeaf][3];
            
         }
         
      }
      
      /* Update j to point to the next level */
      j = j + ipow(2, i + 1);
   }
   
   for (i = 0; i < 15; i++) {
      gfx_SetColor(i*2);
      gfx_FillRectangle(bspLeaves[i][0], bspLeaves[i][1], bspLeaves[i][2] - bspLeaves[i][0] + 1, bspLeaves[i][3] - bspLeaves[i][1] + 1);
      while (!os_GetCSC());
   }
}

/* Calculate the power of an integer */
uint8_t ipow(uint8_t base, uint8_t exp)
{
   /* Declare result variable */
    uint8_t result = 1;
   
   /* Recursively loop and multiply to get an exponent */
    for (;;) {
      
        if (exp & 1)
            result *= base;
      
        exp >>= 1;
      
        if (!exp)
            break;
      
        base *= base;
    }

   /* Return the result */
    return result;
}

int roundInt(const uint8_t n, const uint8_t d)
{
   return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
}



GIF of the problem (none of the leaves should overlap, and each should subdivide it's parent):


EDIT:
I figured it out (with some help). My logic was wrong. I needed to add the width/height of the parent leaves divided by two
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement