-4

Currently I am making a game in batch that has randomly generated worlds. I'm currently working on biome generation, and I'm trying to figure out a way to have each variable generated, so the variables will look like %biomenum[0,0]%, %biomenum[0,1]%, and going maybe as far as %biomenum[512,512]% and as low as %biomenum[-512,-512]

The code that I have currently generates the biome for 9 different chunks, but it doesn't make the variables itself, and typing this out for each chunk between -512, -512 and 512, 512 doesn't seem all that plausible

rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem 0x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0x1y =  biomenum0x0y - 1
rem 1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x1y =  biomenum0x0y - 1
rem 1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x0y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x0y =  biomenum0x0y - 1
rem 1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1xneg1y =  biomenum0x0y - 1
rem 0x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0xneg1y =  biomenum0x0y - 1
rem -1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1xneg1y =  biomenum0x0y - 1
rem -1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x0y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x0y =  biomenum0x0y - 1
rem -1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x1y =  biomenum0x0y - 1
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1

How would I be able to automate the creation of variables, so I don't have to type out all 1,048,576 chunks?

*Note: This question was edited so it made more sense. The original was a very long explanation that I made on 3 hours of sleep, and I apologize for that. Hopefully this is a bit more easy to understand.

N3ther
  • 76
  • 1
  • 10
  • Possible duplicate of [Windows Batch Variable within variable](https://stackoverflow.com/questions/27114936/windows-batch-variable-within-variable) – N3ther Aug 20 '19 at 01:31
  • Just realized that this question has already been asked at [link](https://stackoverflow.com/questions/27114936/windows-batch-variable-within-variable) – N3ther Aug 20 '19 at 01:31

1 Answers1

1

Your explanation is pretty confusing, both in what you want to get and the way to obtain it...

When you have a series of variables with the same name and you want to select one variable based on the value of another variable (called index), then the name for such a data structure is array. If the array elements are selected by just one index variable (one dimensional array), then it is usually called vector. If the array elements are selected by two or more indices (multi-dimensional array), then it is called matrix.

In computer programs the usual way to repeat the same code over a range of values that are all processed in the same way is via the FOR statement/construct/command. The FOR control variable is placed in the code in the place of the varying value. The usual way to process a series of array elements is via a FOR command and place its control variable as the index of the array element.

The way to process array elements in Batch file programs is described at this answer.

The next Batch file obtain a result equivalent than your example code, but using a two-dimensional array and two nested FOR commands:

@echo off
setlocal EnableDelayedExpansion

rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem The rest!
for %%x in (neg1 0 1) do (
   for %%y in (neg1 0 1) do (
      set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum%%xx%%yy = biomenum0x0y + nextbiomenum %% 2
   )
)

echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1

The results obtained by this code are not exactly the same than your code. In your code you add 1 when random is 1 and subtract 1 when random is 4 (and ignore 2 and 3). In my simplification I generate a number between -1 and 2 (instead of 1 to 4) and add 1 when random is 1 and subtract 1 when random is -1 (and ignore 0 and 2); this is done taking the remainder (mod, % operator) of the number divided by 2. The statistical result is, however, the same...

Antonio

PS - I suggest you to use the standard array notation with the subscripts enclosed in square braquets, (like %biomenum[1,0]% or %biomenum[1][0]%instead of %biomenum1x0y% ) for the reasons explained at this post.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Sorry about the explanation of the question, sometimes when I'm writing I can go on a bit of a tangent, but this was exactly what I was looking for! – N3ther Aug 20 '19 at 03:33