123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- import struct
- import random
- import os
- import sys
- import zlib
- import array
- from pnoise import pnoise
- SEED = 0
- SECTOR_DIR_FORMAT = 1
- mapdir = "../world"
- 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 to3h(i):
- s = "";
- s += '{0:1x}'.format((i>>8) & 0x000f)
- s += '{0:1x}'.format((i>>4) & 0x000f)
- s += '{0:1x}'.format((i>>0) & 0x000f)
- return s
- def get_sector_dir(px, pz):
- global SECTOR_DIR_FORMAT
- if SECTOR_DIR_FORMAT == 0:
- return "/sectors/"+to4h(px)+to4h(pz)
- elif SECTOR_DIR_FORMAT == 1:
- return "/sectors2/"+to3h(px)+"/"+to3h(pz)
- else:
- assert(0)
- def getrand_air_stone():
- i = random.randrange(0,2)
- if i==0:
- return 0
- return 254
- class v3:
- def __init__(self, x=0, y=0, z=0):
- self.X = x
- self.Y = y
- self.Z = z
- class NodeMeta:
- def __init__(self, type_id, data):
- self.type_id = type_id
- self.data = data
- class StaticObject:
- def __init__(self):
- self.type_id = 0
- self.data = ""
- def ser_u16(i):
- return chr((i>>8)&0xff) + chr((i>>0)&0xff)
- def ser_u32(i):
- return (chr((i>>24)&0xff) + chr((i>>16)&0xff)
- + chr((i>>8)&0xff) + chr((i>>0)&0xff))
- class MapBlock:
- def __init__(self):
- self.content = array.array('B')
- self.param1 = array.array('B')
- self.param2 = array.array('B')
- for i in range(16*16*16):
-
- self.content.append(254)
-
- self.param1.append(15)
-
- self.param2.append(0)
-
-
-
- self.nodemeta = {}
-
-
-
- self.static_objects = {}
-
- def set_content(self, v3, b):
- self.content[v3.Z*16*16+v3.Y*16+v3.X] = b
- def set_param1(self, v3, b):
- self.param1[v3.Z*16*16+v3.Y*16+v3.X] = b
- def set_param2(self, v3, b):
- self.param2[v3.Z*16*16+v3.Y*16+v3.X] = b
-
-
- def serialize_data(self):
- s = ""
- for i in range(16*16*16):
- s += chr(self.content[i])
- for i in range(16*16*16):
- s += chr(self.param1[i])
- for i in range(16*16*16):
- s += chr(self.param2[i])
- return s
- def serialize_nodemeta(self):
- s = ""
- s += ser_u16(1)
- s += ser_u16(len(self.nodemeta))
- for pos, meta in self.nodemeta.items():
- pos_i = pos.Z*16*16 + pos.Y*16 + pos.X
- s += ser_u16(pos_i)
- s += ser_u16(meta.type_id)
- s += ser_u16(len(meta.data))
- s += meta.data
- return s
- def serialize_staticobj(self):
- s = ""
- s += chr(0)
- s += ser_u16(len(self.static_objects))
- for pos, obj in self.static_objects.items():
- pos_i = pos.Z*16*16 + pos.Y*16 + pos.X
- s += ser_s32(pos.X*1000)
- s += ser_s32(pos.Y*1000)
- s += ser_s32(pos.Z*1000)
- s += ser_u16(obj.type_id)
- s += ser_u16(len(obj.data))
- s += obj.data
- return s
- def writeblock(mapdir, px,py,pz, block):
- sectordir = mapdir + get_sector_dir(px, pz);
-
- try:
- os.makedirs(sectordir)
- except OSError:
- pass
-
- path = sectordir+"/"+to4h(py)
- print("writing block file "+path)
- f = open(sectordir+"/"+to4h(py), "wb")
- if f == None:
- return
-
- version = 17
- f.write(struct.pack('B', version))
-
-
- flags = 0 + 0x02 + 0x04
- f.write(struct.pack('B', flags))
-
-
- c_obj = zlib.compressobj()
- c_obj.compress(block.serialize_data())
- f.write(struct.pack('BB', 0x78, 0x9c))
- f.write(c_obj.flush())
-
- c_obj = zlib.compressobj()
- c_obj.compress(block.serialize_nodemeta())
- f.write(struct.pack('BB', 0x78, 0x9c))
- f.write(c_obj.flush())
-
- f.write(ser_u16(0))
-
- f.write(block.serialize_staticobj())
-
- f.write(ser_u32(0xffffffff))
- f.close()
- for z0 in range(-1,3):
- for x0 in range(-1,3):
- for y0 in range(-1,3):
- print("generating block "+str(x0)+","+str(y0)+","+str(z0))
-
-
-
- block = MapBlock()
-
-
- for z in range(0,16):
- for x in range(0,16):
- h = 20.0*pnoise((x0*16+x)/100.,(z0*16+z)/100.,SEED+0)
- h += 5.0*pnoise((x0*16+x)/25.,(z0*16+z)/25.,SEED+0)
- if pnoise((x0*16+x)/25.,(z0*16+z)/25.,SEED+92412) > 0.05:
- h += 10
-
-
- h = int(h)
- for y in range(0,16):
- p = v3(x,y,z)
- b = 254
- y1 = y0*16+y
- if y1 <= h-3:
- b = 0
- elif y1 <= h and y1 <= 0:
- b = 8
- elif y1 == h:
- b = 1
- elif y1 < h:
- b = 8
- elif y1 <= 1:
- b = 9
-
- block.set_content(p, b)
-
-
-
- if x == 8 and z == 8 and y0*16 <= h-1 and (y0+1)*16-1 > h:
- p = v3(8,h+1-y0*16,8)
-
- content_type = 14
- block.set_content(p, content_type)
-
-
- block.set_param2(p, 0x08)
-
- s = "Hello at sector ("+str(x0)+","+str(z0)+")"
- meta = NodeMeta(content_type, ser_u16(len(s))+s)
- block.nodemeta[p] = meta
-
- writeblock(mapdir, x0,y0,z0, block)
|