updated example map generator python script

master
Perttu Ahola 2011-04-11 18:33:05 +03:00
parent 50a8087684
commit 81ac026e1f
4 changed files with 176 additions and 22 deletions

View File

@ -1,17 +1,12 @@
#!/usr/bin/python
#!/usr/bin/python2
"
This is an example script that generates some valid map data.
"
# This is an example script that generates some valid map data.
import struct
import random
def getrand():
i = random.randrange(0,2)
if i==0:
return 0
return 254
import os
import sys
from pnoise import pnoise
"""
Map format:
@ -25,18 +20,72 @@ ffff = -1
0001 = 1
"""
f = open("map/sectors/00000000/ffff", "wb")
def to4h(i):
s = "";
s += '{0:1x}'.format((i>>12) & 0x000f)
s += '{0:1x}'.format((i>>8) & 0x000f)
s += '{0:1x}'.format((i>>4) & 0x000f)
s += '{0:1x}'.format((i>>0) & 0x000f)
return s
# version
f.write(struct.pack('B', 2))
# is_underground
f.write(struct.pack('B', 0))
def getrand():
i = random.randrange(0,2)
if i==0:
return 0
return 254
for i in range(0,16*16*16):
# Material content
f.write(struct.pack('B', getrand()))
# Brightness
f.write(struct.pack('B', 15))
def writeblock(mapdir, px,py,pz, version):
sectordir = mapdir + "/sectors/" + to4h(px) + to4h(pz)
try:
os.makedirs(sectordir)
except OSError:
pass
f.close()
f = open(sectordir+"/"+to4h(py), "wb")
if version == 0:
# version
f.write(struct.pack('B', 0))
# is_underground
f.write(struct.pack('B', 0))
elif version == 2:
# version
f.write(struct.pack('B', 2))
# is_underground
f.write(struct.pack('B', 0))
for z in range(0,16):
for y in range(0,16):
for x in range(0,16):
b = 254
r = 20.0*pnoise((px*16+x)/100.,(pz*16+z)/100.,0)
r += 5.0*pnoise((px*16+x)/25.,(pz*16+z)/25.,0)
#print("r="+str(r))
y1 = py*16+y
if y1 <= r-3:
b = 0 #stone
elif y1 <= r:
b = 1 #grass
elif y1 <= 1:
b = 9 #water
if version == 0:
# Material content
f.write(struct.pack('B', b))
elif version == 2:
# Material content
f.write(struct.pack('B', b))
# Brightness
f.write(struct.pack('B', 15))
f.close()
mapdir = "map"
for z in range(-2,3):
for y in range(-1,2):
for x in range(-2,3):
print("generating block "+str(x)+","+str(y)+","+str(z))
writeblock(mapdir, x,y,z, 0)
#END

102
pnoise.py Normal file
View File

@ -0,0 +1,102 @@
#
# A python perlin noise implementation, from
# http://www.fundza.com/c4serious/noise/perlin/perlin.html
#
# This is used for testing how to create maps with a python script.
#
import math
p = (
151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,
30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,
62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,
125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,
83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,
143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,
196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,
250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,
58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,
221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,
224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,
12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,
199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,
205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,
30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,
62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,
125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,
83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,
143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,
196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,
250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,
58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,
221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,
224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,
12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,
199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,
205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180)
def lerp(t, a, b):
return a + t * (b - a)
def fade(t):
return t * t * t * (t * (t * 6 - 15) + 10)
def grad(hash, x, y, z):
h = hash & 15
if h < 8:
u = x
else:
u = y
if h < 4:
v = y
elif h == 12 or h == 14:
v = x
else:
v = z
if h & 1 != 0:
u = -u
if h & 2 != 0:
v = -v
return u + v
def pnoise(x, y, z):
global p
X = int(math.floor(x)) & 255
Y = int(math.floor(y)) & 255
Z = int(math.floor(z)) & 255
x -= math.floor(x)
y -= math.floor(y)
z -= math.floor(z)
u = fade(x)
v = fade(y)
w = fade(z)
A = p[X] + Y
AA = p[A] + Z
AB = p[A + 1] + Z
B = p[X + 1] + Y
BA = p[B] + Z
BB = p[B + 1] + Z
pAA = p[AA]
pAB = p[AB]
pBA = p[BA]
pBB = p[BB]
pAA1 = p[AA + 1]
pBA1 = p[BA + 1]
pAB1 = p[AB + 1]
pBB1 = p[BB + 1]
gradAA = grad(pAA, x, y, z)
gradBA = grad(pBA, x-1, y, z)
gradAB = grad(pAB, x, y-1, z)
gradBB = grad(pBB, x-1, y-1, z)
gradAA1 = grad(pAA1,x, y, z-1)
gradBA1 = grad(pBA1,x-1, y, z-1)
gradAB1 = grad(pAB1,x, y-1, z-1)
gradBB1 = grad(pBB1,x-1, y-1, z-1)
return lerp(w,
lerp(v, lerp(u, gradAA, gradBA), lerp(u, gradAB, gradBB)),
lerp(v, lerp(u, gradAA1,gradBA1),lerp(u, gradAB1,gradBB1)))

View File

@ -4711,6 +4711,9 @@ plan_b:
/*
Plan B: Get from map generator perlin noise function
*/
// This won't work if proper generation is disabled
if(m_chunksize == 0)
return WATER_LEVEL+2;
double level = base_rock_level_2d(m_seed, p2d);
return (s16)level;
}

View File

@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
NOTE: The goal is to increment this so that saved maps will be
loadable by any version. Other compatibility is not
maintained.
Serialization format versions:
Serialization format versions (for raw map data (blocks, nodes, sectors)):
== Unsupported ==
0: original networked test with 1-byte nodes
1: update with 2-byte nodes