For this I've used Godot Engine and GDScript.
Hello, I am working in a Map Generation project, and I am trying to come up with a Height Map generator for the hills and mountain-ranges. I'm pretty amateur when it comes to programming in general and I mostly try to make everything from zero as I don't know much about game theory or any big programming techniques, so forgive me if anything I've done is ugly or goes totally against any common sense.
Up until now, I've come up with this:
extends Node2D
export(int) var Origin_Min
export(int) var Origin_Max
export(int) var Power
var HeightMap = {}
func _ready():
new_seed()
_mountain_range()
func new_seed():
randomize()
var random_seed = randi()
seed(random_seed)
print(random_seed)
func _mountain_range():
#ORIGINS PHASE - set's origin points for the mountain-ranges
var OriginList = {}
for origin in round(rand_range(Origin_Min, Origin_Max)):
var originPointX = round(rand_range(1, 126))
var originPointY = round(rand_range(1, 126))
if OriginList.has(Vector2(originPointX, originPointY)):
continue
OriginList[Vector2(originPointX, originPointY)] = {"Power": round(rand_range(1, 5)), "Connected": false}
HeightMap[Vector2(originPointX, originPointY)] = 3
$TileMap.set_cellv(Vector2(originPointX, originPointY), 2)
#MOUNTAIN-RANGES PHASE - try to draw mountain_ranges between every origin based on their Power level
for origin in OriginList:
for point in OriginList:
if OriginList[point].Connected:
continue
if round(origin.distance_to(point)) <= (OriginList[origin].Power * Power) and round(origin.distance_to(point)) != 0:
var current = origin
while current != point:
var currentDist = round(current.distance_to(point))
var closerDist = []
var sameDist = []
for direction in dir.Directions:
var step = Vector2(clamp(current.x + direction.x, 1, 126), clamp(current.y + direction.y, 1, 126))
if step == point:
OriginList[origin] = {"Power": round(rand_range(2, 3)), "Connected": true}
current = point
break
if round((step).distance_to(point)) <= currentDist:
closerDist.append(step)
if round((step).distance_to(point)) >= currentDist:
sameDist.append(step)
if round(rand_range(0, 10)) >= 3 and closerDist.size() >= 1: #70% chance of going closer
current = closerDist[round(rand_range(0, clamp(closerDist.size() - 1, 0, 3)))]
HeightMap[current] = round(rand_range(2, 3))
$TileMap.set_cellv(current, 2)
elif sameDist.size() >= 1: #30% chance of taking a variation
current = sameDist[round(rand_range(0, clamp(sameDist.size() - 1, 0, 3)))]
HeightMap[current] = round(rand_range(2, 3))
$TileMap.set_cellv(current, 2)
#FILL UP PHASE - fill up the rest of the map with height level 0 tiles
var xCoord = 0
var yCoord = 0
for tile in 16384:
if not HeightMap.has(Vector2(xCoord, yCoord)):
$TileMap.set_cellv(Vector2(xCoord, yCoord), 0)
xCoord += 1
if xCoord == 128:
xCoord = 0
yCoord += 1
This code is divided in three parts:
- Set a random number of origins in random locations(excluding borders) and give these origins a power level.
- For each origin, check if it can "link" with each other origin based in its power level, if so, "draw" a mountain-range in between them.
- Draw the rest of the map.
The result is this:
I rarely have some random problems with this code, like an origin point that says that it is nor closer nor at the same distance as another origin point when they are drawing a mountain-range in between them.