summaryrefslogtreecommitdiff
path: root/genmap.py
blob: aaa35690fd6f0f83937c3ca78693a12fce85d8b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python2

# This is an example script that generates some valid map data.

import struct
import random
import os
import sys
from pnoise import pnoise

"""
Map format:
map/sectors/XXXXZZZZ/YYYY

XXXX,YYYY,ZZZZ = coordinates in hexadecimal

fffe = -2
ffff = -1
0000 =  0
0001 =  1
"""

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

def getrand():
	i = random.randrange(0,2)
	if i==0:
		return 0
	return 254

def writeblock(mapdir, px,py,pz, version):
	sectordir = mapdir + "/sectors/" + to4h(px) + to4h(pz)
	
	try:
		os.makedirs(sectordir)
	except OSError:
		pass

	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