
-----------------------------------
Raknarg
Thu Jan 02, 2014 8:35 pm

Random Map Generator
-----------------------------------
I decided this deserved a new post on its own.

It results in the same thing as my previous program, however now it performs the task iterively rather than recursively. On small depths, there's no noticeable difference. At depth 9, the new version is about a second faster or so. At depth 10, it is twice as fast.

With the new version it finds each point mathematically without having to generate a new array each time and do a lot of value swapping within the array.

As before, uses pygame. Also as before, neat results.


Does anyone have ideas on making a map witha  custom size? I could make a huge map and basically crop it, but that seems unnecessary.


from __future__ import division
from random import randint, random
import math
import pygame
import sys

# If n + modifier is out of range, it generates the index it should be at (otherwise it never gets the right number).
def mod (n, modifier, maxVal):
    if n+modifier < 0:
        return modifier - 1
    elif n+modifier > maxVal:
        return modifier
    else:
        return n + modifier
# Generates a random real value
def randreal(minVal, maxVal):
    return (maxVal-minVal) * random() + minVal
# Takes the average of the corners surrounding the point
"""
x . x
. o .
x . x
"""
def square (i, j, minVal, maxVal, r, currentSize, currentMap):
    return round(min(maxVal, max(minVal, (currentMap

-----------------------------------
smool
Sat Jan 04, 2014 10:43 am

RE:Random Map Generator
-----------------------------------
Very nice. I can imagine all the math and logic was annoying to debug

-----------------------------------
Insectoid
Sat Jan 04, 2014 11:49 am

RE:Random Map Generator
-----------------------------------
I've been meaning to implement diamond-square myself for a while, but as usual I never get around to it. Closest I've come so far is a 2d fractal mountain silhouette.

-----------------------------------
Raknarg
Sat Jan 04, 2014 8:41 pm

RE:Random Map Generator
-----------------------------------
@Instectoid I've been trying to figure out how to do this for a long time, the other night I just pulled out a piece of paper and figured it out by a fluke. Do you mean midpoint displacement?

@smool yeah it was a bit annoying cause there were some exceptions and such I had to handle that I didn't know about, but the majority of the math kindof just came to me all at once

-----------------------------------
Insectoid
Sat Jan 04, 2014 9:15 pm

RE:Random Map Generator
-----------------------------------
http://en.wikipedia.org/wiki/Diamond-square_algorithm

"It is a slightly better algorithm than the three-dimensional implementation of the midpoint displacement algorithm"

I saw your functions named diamond and square and assumed, given that this is a map/terrain generator, that this is an implementation of this algorithm. I can't be bothered to go through your code to find out how similar it is, but even coming up with diamond and square function names in this context is an impressive coincidence.

-----------------------------------
Raknarg
Sat Jan 04, 2014 9:27 pm

RE:Random Map Generator
-----------------------------------
No it is the diamond square algorithm, but you mentioned "2d fractal mountain silhouette" so I assumed you meant a midpoint displacement rendering of a mountain

-----------------------------------
Insectoid
Sat Jan 04, 2014 10:54 pm

RE:Random Map Generator
-----------------------------------
Oh, yeah, I did one of those. Midpoint displacement. Yeah. I keep meaning to add more procedural stuff to it to create a complete scene, but I'm far too lazy to actually finish it.
