Rendering a randomly generated 3d terrain

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 14 Jan 2019 15:25

Rendering a randomly generated 3d terrain

#1 Post by Manc0w » 14 Jan 2019 15:55

Hey dudes, kinda new to this forum but figured I would post something cool I wrote.
This is entirely a proof of concept demo and doesn't really do anything super interesting with gameplay, but nonetheless here we go.
Last week I got bored and had an idea to render a 3 dimensional world in a batch file by only rendering one "slice" of it at a time. Think of a loaf of bread, you can visualize it in 2 dimensions by only looking at one slice at a time. This bit of code works exactly on that premise.
I added a further level of complexity by deciding to make it a randomly generated terrain that the code would be rendering, which was the real meat of this project.

The 3 dimensional thing was pretty easy, but the random generation wasn't quite so. Had to get a bit creative in order to make something at least somewhat natural and believable :lol:

Here is the code, I did my best to explain it in the file.
Pretty happy I could do it in around 90 lines before adding comments.

Feel free to add or remove anything you deem fit, but if you do please share with others in order to inspire creativity :D

Have fun!

P.S. Should be able to copy and paste right into notepad, no external things used and no weird encoding necessary. :D

Code: Select all

REM This was a proof of concept testing an idea of randomly generating a world on a large scale.
REM This script renders a 3 dimensional world by only displaying 1 "slice" of the world at a time.
REM WASD to move and E to exit

REM Housekeeping variables, very self explanatory
set title=Game
set resolution=80,40
set color=0F

REM This set of variables determines the size of the world, making these values higher significantly increases the amount of time it takes to generate the world.
set MapSizeX=20
set MapSizeY=10
set MapSizeZ=10

REM These values determine what the values displayed on the screen are, very self explanatory.
set Player_Char=@
set Block_Char=#
set Air_Char=.
set Air_Char_Minimap=.

REM Housekeeping stuff
@echo off
setlocal enabledelayedexpansion
title %title%
mode %resolution%
color %color%

REM Generating the world
call :init

REM Main Function
REM Establishes gravity so character isn't floating.
for /l %%y in (!MapSizeY!, -1, 0) do (
	if !Tile%x%x%%yx%z%! == !Air_Char! set y=%%y

REM Render character into map
set Tile!x!x!y!x!z!=!Player_Char!
set Planar!x!x!z!=!Player_Char!

REM Displaying player coordinates.
echo.%x% , %y% , %z%

REM Rendering Minimap
for /l %%z in (!MapSizeZ!, -1, 0) do (

REM Rendering "slice" of the map currently in view
for /l %%y in (!MapSizeY!, -1, 0) do (
	echo. !Tile0x%%yx%z%! !Tile1x%%yx%z%! !Tile2x%%yx%z%! !Tile3x%%yx%z%! !Tile4x%%yx%z%! !Tile5x%%yx%z%! !Tile6x%%yx%z%! !Tile7x%%yx%z%! !Tile8x%%yx%z%! !Tile9x%%yx%z%! !Tile10x%%yx%z%! !Tile11x%%yx%z%! !Tile12x%%yx%z%! !Tile13x%%yx%z%! !Tile14x%%yx%z%! !Tile15x%%yx%z%! !Tile16x%%yx%z%! !Tile17x%%yx%z%! !Tile18x%%yx%z%! !Tile19x%%yx%z%! !Tile20x%%yx%z%!

REM Reset tiles back from character code
set Tile!x!x!y!x!z!=!Air_Char!
set Planar!x!x!z!=!Air_Char_Minimap!

REM Control input, also handles character movement.
choice /c WASDE /n /m ":"
if !ERRORLEVEL! == 1 set /a z+=1
if !ERRORLEVEL! == 2 set /a x-=1
if !ERRORLEVEL! == 3 set /a z-=1
if !ERRORLEVEL! == 4 set /a x+=1
if !ERRORLEVEL! == 5 goto :eof
goto main

echo.generating 3 dimensional world - will take a sec :)

REM Accounting for the fact that computers include 0 while counting
set /a MapSizeX-=1, MapSizeY-=1, MapSizeZ-=1

REM Establishing random location for player to start.  The Y value is handled while doing gravity in the main function.
set /a x=%random% %%!MapSizeX!
set /a z=%random% %%!MapSizeY!

REM Initial height that the randomization of every part of the map is based off of.
set initialHeight=%random% %%!MapSizeY!/2+1

REM Create 2d minimap
for /l %%x in (0,1,!MapSizeX!) do (
	for /l %%z in (0,1,!MapSizeZ!) do (
		set Planar%%xx%%z=!Air_Char_Minimap!
echo.minimap done

REM Create blank canvas, a skeleton of the map
for /l %%x in (0,1,!MapSizeX!) do for /l %%y in (0,1,!MapSizeY!) do for /l %%z in (0,1,!MapSizeZ!) do (
	set Tile%%xx%%yx%%z=!Air_Char!
) skeleton done

REM Establish 10 random height values for each column in each "slice" of the map
for /l %%z in (0,1,!MapSizeZ!) do for /l %%x in (0,1,!MapSizeX!) do (
	set tmp=%%a
	set /a rand%%xx%%z=!random! %%3 -1
echo.randomization done
echo.filling in world...
REM Rendering world based off of the random height values
for /l %%z in (0,1,!MapSizeZ!) do for /l %%x in (0,1,!MapSizeX!) do (
	set /a height%%xx%%z=!initialHeight! + !rand%%xx%%z!
	for /l %%y in (0,1,!height%%xx%%z!) do (
		set Tile%%xx%%yx%%z=!Block_Char!

Post Reply