Tile-based generation (even slower!), leafletjs support

master
Ilya Zhuravlev 2012-12-31 17:15:40 +04:00
parent f473f9eafe
commit e0fb4a35d9
9 changed files with 688 additions and 23 deletions

15
constants.py Normal file
View File

@ -0,0 +1,15 @@
# Y
# |
# |
# |
# /\
# / \
# / \
#X Z
NODE_SIZE = 24
NODES_PER_BLOCK = 16
BLOCK_SIZE = 16 * NODE_SIZE
CHUNK_HEIGHT = 16 * BLOCK_SIZE/2 + BLOCK_SIZE/2
BLOCKS_PER_CHUNK = 16

1
html/data Symbolic link
View File

@ -0,0 +1 @@
../data/

432
html/leaflet.css Normal file
View File

@ -0,0 +1,432 @@
/* required styles */
.leaflet-map-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-pane,
.leaflet-overlay-pane,
.leaflet-shadow-pane,
.leaflet-marker-pane,
.leaflet-popup-pane,
.leaflet-overlay-pane svg,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
position: absolute;
left: 0;
top: 0;
}
.leaflet-container {
overflow: hidden;
-ms-touch-action: none;
}
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.leaflet-marker-icon,
.leaflet-marker-shadow {
display: block;
}
/* map is broken in FF if you have max-width: 100% on tiles */
.leaflet-container img {
max-width: none !important;
}
/* stupid Android 2 doesn't understand "max-width: none" properly */
.leaflet-container img.leaflet-image-layer {
max-width: 15000px !important;
}
.leaflet-tile {
filter: inherit;
visibility: hidden;
}
.leaflet-tile-loaded {
visibility: inherit;
}
.leaflet-zoom-box {
width: 0;
height: 0;
}
.leaflet-tile-pane { z-index: 2; }
.leaflet-objects-pane { z-index: 3; }
.leaflet-overlay-pane { z-index: 4; }
.leaflet-shadow-pane { z-index: 5; }
.leaflet-marker-pane { z-index: 6; }
.leaflet-popup-pane { z-index: 7; }
/* control positioning */
.leaflet-control {
position: relative;
z-index: 7;
pointer-events: auto;
}
.leaflet-top,
.leaflet-bottom {
position: absolute;
z-index: 1000;
pointer-events: none;
}
.leaflet-top {
top: 0;
}
.leaflet-right {
right: 0;
}
.leaflet-bottom {
bottom: 0;
}
.leaflet-left {
left: 0;
}
.leaflet-control {
float: left;
clear: both;
}
.leaflet-right .leaflet-control {
float: right;
}
.leaflet-top .leaflet-control {
margin-top: 10px;
}
.leaflet-bottom .leaflet-control {
margin-bottom: 10px;
}
.leaflet-left .leaflet-control {
margin-left: 10px;
}
.leaflet-right .leaflet-control {
margin-right: 10px;
}
/* zoom and fade animations */
.leaflet-fade-anim .leaflet-tile,
.leaflet-fade-anim .leaflet-popup {
opacity: 0;
-webkit-transition: opacity 0.2s linear;
-moz-transition: opacity 0.2s linear;
-o-transition: opacity 0.2s linear;
transition: opacity 0.2s linear;
}
.leaflet-fade-anim .leaflet-tile-loaded,
.leaflet-fade-anim .leaflet-map-pane .leaflet-popup {
opacity: 1;
}
.leaflet-zoom-anim .leaflet-zoom-animated {
-webkit-transition: -webkit-transform 0.25s cubic-bezier(0,0,0.25,1);
-moz-transition: -moz-transform 0.25s cubic-bezier(0,0,0.25,1);
-o-transition: -o-transform 0.25s cubic-bezier(0,0,0.25,1);
transition: transform 0.25s cubic-bezier(0,0,0.25,1);
}
.leaflet-zoom-anim .leaflet-tile,
.leaflet-pan-anim .leaflet-tile,
.leaflet-touching .leaflet-zoom-animated {
-webkit-transition: none;
-moz-transition: none;
-o-transition: none;
transition: none;
}
.leaflet-zoom-anim .leaflet-zoom-hide {
visibility: hidden;
}
/* cursors */
.leaflet-clickable {
cursor: pointer;
}
.leaflet-container {
cursor: -webkit-grab;
cursor: -moz-grab;
}
.leaflet-popup-pane,
.leaflet-control {
cursor: auto;
}
.leaflet-dragging,
.leaflet-dragging .leaflet-clickable,
.leaflet-dragging .leaflet-container {
cursor: move;
cursor: -webkit-grabbing;
cursor: -moz-grabbing;
}
/* visual tweaks */
.leaflet-container {
background: #ddd;
outline: 0;
}
.leaflet-container a {
color: #0078A8;
}
.leaflet-container a.leaflet-active {
outline: 2px solid orange;
}
.leaflet-zoom-box {
border: 2px dotted #05f;
background: white;
opacity: 0.5;
}
/* general typography */
.leaflet-container {
font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
}
/* zoom control */
.leaflet-container .leaflet-control-zoom {
margin-left: 13px;
margin-top: 12px;
box-shadow: 0 0 8px rgba(0,0,0,0.4);
border: 1px solid #888;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.leaflet-control-zoom a {
width: 22px;
height: 22px;
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
text-decoration: none;
color: black;
}
.leaflet-control-zoom a,
.leaflet-control-layers-toggle {
background-position: 50% 50%;
background-repeat: no-repeat;
display: block;
}
.leaflet-control-zoom a:hover {
background-color: #fff;
color: #777;
}
.leaflet-control-zoom-in {
border-bottom: 1px solid #aaa;
font: bold 18px/24px Arial, Helvetica, sans-serif;
-webkit-border-radius: 5px 5px 0 0;
border-radius: 5px 5px 0 0;
}
.leaflet-control-zoom-out {
font: bold 23px/20px Tahoma, Verdana, sans-serif;
-webkit-border-radius: 0 0 5px 5px;
border-radius: 0 0 5px 5px;
}
.leaflet-control-zoom a.leaflet-control-zoom-disabled {
cursor: default;
background-color: rgba(255, 255, 255, 0.8);
color: #bbb;
}
.leaflet-touch .leaflet-control-zoom {
border-radius: 10px;
}
.leaflet-touch .leaflet-control-zoom a {
width: 30px;
height: 30px;
}
.leaflet-touch .leaflet-control-zoom-in {
font-size: 24px;
line-height: 29px;
border-bottom: 4px solid rgba(0,0,0,0.3);
border-radius: 7px 7px 0 0;
}
.leaflet-touch .leaflet-control-zoom-out {
font-size: 28px;
line-height: 24px;
}
/* layers control */
.leaflet-control-layers {
box-shadow: 0 1px 7px rgba(0,0,0,0.4);
background: #f8f8f9;
-webkit-border-radius: 8px;
border-radius: 8px;
}
.leaflet-control-layers-toggle {
background-image: url(images/layers.png);
width: 36px;
height: 36px;
}
.leaflet-touch .leaflet-control-layers-toggle {
width: 44px;
height: 44px;
}
.leaflet-control-layers .leaflet-control-layers-list,
.leaflet-control-layers-expanded .leaflet-control-layers-toggle {
display: none;
}
.leaflet-control-layers-expanded .leaflet-control-layers-list {
display: block;
position: relative;
}
.leaflet-control-layers-expanded {
padding: 6px 10px 6px 6px;
color: #333;
background: #fff;
}
.leaflet-control-layers-selector {
margin-top: 2px;
position: relative;
top: 1px;
}
.leaflet-control-layers label {
display: block;
}
.leaflet-control-layers-separator {
height: 0;
border-top: 1px solid #ddd;
margin: 5px -10px 5px -6px;
}
/* attribution and scale controls */
.leaflet-container .leaflet-control-attribution {
background-color: rgba(255, 255, 255, 0.7);
box-shadow: 0 0 5px #bbb;
margin: 0;
}
.leaflet-control-attribution,
.leaflet-control-scale-line {
padding: 0 5px;
color: #333;
}
.leaflet-container .leaflet-control-attribution,
.leaflet-container .leaflet-control-scale {
font-size: 11px;
}
.leaflet-left .leaflet-control-scale {
margin-left: 5px;
}
.leaflet-bottom .leaflet-control-scale {
margin-bottom: 5px;
}
.leaflet-control-scale-line {
border: 2px solid #777;
border-top: none;
color: black;
line-height: 1.1;
padding: 2px 5px 1px;
font-size: 11px;
text-shadow: 1px 1px 1px #fff;
background-color: rgba(255, 255, 255, 0.5);
box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.2);
white-space: nowrap;
overflow: hidden;
}
.leaflet-control-scale-line:not(:first-child) {
border-top: 2px solid #777;
border-bottom: none;
margin-top: -2px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
.leaflet-control-scale-line:not(:first-child):not(:last-child) {
border-bottom: 2px solid #777;
}
.leaflet-touch .leaflet-control-attribution,
.leaflet-touch .leaflet-control-layers,
.leaflet-touch .leaflet-control-zoom {
box-shadow: none;
}
.leaflet-touch .leaflet-control-layers,
.leaflet-touch .leaflet-control-zoom {
border: 4px solid rgba(0,0,0,0.3);
}
/* popup */
.leaflet-popup {
position: absolute;
text-align: center;
}
.leaflet-popup-content-wrapper {
padding: 1px;
text-align: left;
-webkit-border-radius: 20px;
border-radius: 20px;
}
.leaflet-popup-content {
margin: 14px 20px;
line-height: 1.4;
}
.leaflet-popup-content p {
margin: 18px 0;
}
.leaflet-popup-tip-container {
margin: 0 auto;
width: 40px;
height: 20px;
position: relative;
overflow: hidden;
}
.leaflet-popup-tip {
width: 15px;
height: 15px;
padding: 1px;
margin: -8px auto 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
background: white;
box-shadow: 0 3px 14px rgba(0,0,0,0.4);
}
.leaflet-container a.leaflet-popup-close-button {
position: absolute;
top: 0;
right: 0;
padding: 4px 5px 0 0;
text-align: center;
width: 18px;
height: 14px;
font: 16px/14px Tahoma, Verdana, sans-serif;
color: #c3c3c3;
text-decoration: none;
font-weight: bold;
background: transparent;
}
.leaflet-container a.leaflet-popup-close-button:hover {
color: #999;
}
.leaflet-popup-scrolled {
overflow: auto;
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
}
/* div icon */
.leaflet-div-icon {
background: #fff;
border: 1px solid #666;
}
.leaflet-editing-icon {
-webkit-border-radius: 2px;
border-radius: 2px;
}

57
html/leaflet.ie.css Normal file
View File

@ -0,0 +1,57 @@
.leaflet-vml-shape {
width: 1px;
height: 1px;
}
.lvml {
behavior: url(#default#VML);
display: inline-block;
position: absolute;
}
.leaflet-control {
display: inline;
}
.leaflet-popup-tip {
width: 21px;
_width: 27px;
margin: 0 auto;
_margin-top: -3px;
filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678);
-ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678)";
}
.leaflet-popup-tip-container {
margin-top: -1px;
}
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
border: 1px solid #999;
}
.leaflet-popup-content-wrapper {
zoom: 1;
}
.leaflet-control-zoom,
.leaflet-control-layers {
border: 3px solid #999;
}
.leaflet-control-zoom a {
background-color: #eee;
}
.leaflet-control-zoom a:hover {
background-color: #fff;
}
.leaflet-control-layers-toggle {
}
.leaflet-control-attribution,
.leaflet-control-layers,
.leaflet-control-scale-line {
background: white;
}
.leaflet-zoom-box {
filter: alpha(opacity=50);
}
.leaflet-control-attribution {
border-top: 1px solid #bbb;
border-left: 1px solid #bbb;
}

8
html/leaflet.js Normal file

File diff suppressed because one or more lines are too long

32
html/map.html Normal file
View File

@ -0,0 +1,32 @@
<html>
<head>
<link rel="stylesheet" href="leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="leaflet.ie.css" />
<![endif]-->
<script src="leaflet.js"></script>
<style>
#map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map', {
crs: L.CRS.Simple
}).setView([0, 0], 5);
L.tileLayer('data/{z}/{y}/{x}.png', {
attribution: 'Generated by <a href="https://github.com/xyzz/onomatopoeia">onomatopoeia</a>',
maxZoom: 5,
tileSize: 384,
continuousWorld: true
}).addTo(map);
</script>
</body>
</html>

16
map.py
View File

@ -6,14 +6,22 @@ import os.path
from util import *
def getBlockAsInteger(x, y, z):
return z * 16777216 + y * 4096 + x
class Map(object):
def __init__(self, path):
self.conn = sqlite3.connect(os.path.join(path, "map.sqlite"))
def getCoordinatesToDraw(self):
result = set()
cur = self.conn.cursor()
cur.execute("SELECT `pos` FROM `blocks`")
while True:
r = cur.fetchone()
if not r:
break
x, y, z = getIntegerAsBlock(r[0])
result.add(coordsToGrid(x, z))
return result
def getBlock(self, x, y, z):
cur = self.conn.cursor()
cur.execute("SELECT `data` FROM `blocks` WHERE `pos`==? LIMIT 1", (getBlockAsInteger(x, y, z), ))

129
mapper.py Normal file → Executable file
View File

@ -1,8 +1,11 @@
#!/usr/bin/env python2
import os.path
from PIL import Image
import os
from PIL import Image, ImageDraw
from map import Map
from blocks import build_block
from constants import *
from util import *
# name: (top, side)
textures = {
@ -22,32 +25,120 @@ for name in textures:
side = Image.open(os.path.join("textures", textures[name][1])).convert("RGBA")
blocks[name] = build_block(top, side)
start = (5000, 5000)
canvas = Image.new("RGBA", (10000, 10000))
map = Map(".")
def drawNode(canvas, x, y, z, block):
canvas.paste(block, (start[0] - 12 * x + 12 * z, start[1] + 6 * x + 6 * z - 12 * y), block)
def drawNode(canvas, x, y, z, block, start):
canvas.paste(block, (start[0] + NODE_SIZE/2 * (z - x), start[1] + NODE_SIZE/4 * (x + z - 2 * y)), block)
def drawBlock(canvas, bx, by, bz):
def drawBlock(canvas, bx, by, bz, start):
map_block = map.getBlock(bx, by, bz)
for y in range(16):
for z in range(16):
for x in range(16):
for y in range(NODES_PER_BLOCK):
for z in range(NODES_PER_BLOCK):
for x in range(NODES_PER_BLOCK):
p = map_block.get(x, y, z)
if p in textures:
drawNode(canvas, x + bx * 16, y + by * 16, z + bz * 16, blocks[p])
drawNode(canvas, x + bx * NODES_PER_BLOCK, y + by * NODES_PER_BLOCK, z + bz * NODES_PER_BLOCK, blocks[p], start)
cached_chunks = {}
def makeChunk(cx, cz):
if (cx, cz) not in cached_chunks:
canvas = Image.new("RGBA", (BLOCK_SIZE, CHUNK_HEIGHT))
for by in range(-8, 8):
drawBlock(canvas, cx, by, cz, (BLOCK_SIZE/2 * (cx - cz + 1) - NODE_SIZE/2, BLOCK_SIZE/4 * (BLOCKS_PER_CHUNK - cz - cx) - NODE_SIZE/2))
cached_chunks[(cx, cz)] = canvas
return cached_chunks[(cx, cz)]
P = 10
for by in range(-1, 2):
for bz in range(-P, P):
print(bz)
for bx in range(-P, P):
drawBlock(canvas, bx, by, bz)
def fullMap():
canvas = Image.new("RGBA", (5000, 5000))
start = (3000, 3000)
for y in range(-1, 10):
print(y)
for z in range(-5, 5):
for x in range(-5, 5):
drawBlock(canvas, x, y, z, start)
canvas.save("map.png")
canvas.save("map.png")
def chunks3(canvas, x, z, step):
chunk = makeChunk(x + step, z + step)
canvas.paste(chunk, (0, (16 + step) * BLOCK_SIZE/2), chunk)
#del chunk
chunk = makeChunk(x + step + 1, z + step)
canvas.paste(chunk, (-BLOCK_SIZE/2, (16 + step) * BLOCK_SIZE/2 + BLOCK_SIZE/4), chunk)
#del chunk
chunk = makeChunk(x + step, z + step + 1)
canvas.paste(chunk, (BLOCK_SIZE/2, (16 + step) * BLOCK_SIZE/2 + BLOCK_SIZE/4), chunk)
#del chunk
# row = x + z
# col = z - x
# x = (row - col) / 2
# z = (row + col) / 2
def dummyMakeTile(row, col):
x, z = gridToCoords(row, col)
x = (row - col) / 2
z = (row + col) / 2
canvas = Image.new("RGBA", (BLOCK_SIZE, 18 * BLOCK_SIZE/2))
for i in range(-16, 2):
print("step {}".format(i))
chunks3(canvas, x, z, i)
tile = canvas.crop((0, 16 * BLOCK_SIZE/2, BLOCK_SIZE, 18 * BLOCK_SIZE/2))
del canvas
return tile
coords = map.getCoordinatesToDraw()
step = 0
for row, col in coords:
step += 1
print("[{}%]".format(100.0 * step / len(coords)))
if row % 4 != 0 or col % 2 != 0:
continue
path = os.path.join("data", "5", "{}".format(row / 4 ))
if not os.path.exists(path):
os.makedirs(path)
dummyMakeTile(row, col).save(os.path.join(path, "{}.png".format(col / 2)))
# zoom 4 ---> 0
to_join = coords
for zoom in range(4, -1, -1):
new_join = set()
for row, col in to_join:
if zoom == 4:
if row % 4 != 0 or col % 2 != 0:
continue
row /= 4
col /= 2
if row % 2 == 1:
row -= 1
if col % 2 == 1:
col -= 1
new_join.add((row, col))
to_join = new_join
print(to_join)
for row, col in to_join:
#print("join {} {}".format(row, col))
R = row / 2
C = col / 2
path = os.path.join("data", "{}".format(zoom), "{}".format(R))
if not os.path.exists(path):
os.makedirs(path)
canvas = Image.new("RGBA", (BLOCK_SIZE, BLOCK_SIZE))
for dx in range(0, 2):
for dz in range(0, 2):
if zoom == 3 and row == 2 and col == -2:
print("test...")
try:
tile = Image.open(os.path.join("data", "{}".format(zoom + 1), "{}".format(row + dx), "{}.png".format(col + dz))).convert("RGBA")
except IOError:
tile = Image.new("RGBA", (BLOCK_SIZE, BLOCK_SIZE))
tile = tile.resize((BLOCK_SIZE/2, BLOCK_SIZE/2))
canvas.paste(tile, (dz * BLOCK_SIZE/2, dx * BLOCK_SIZE/2), tile)
canvas.save(os.path.join(path, "{}.png".format(C)))

21
util.py
View File

@ -1,3 +1,7 @@
def getBlockAsInteger(x, y, z):
return z * 16777216 + y * 4096 + x
def unsignedToSigned(i, max_positive):
if i < max_positive:
return i
@ -5,6 +9,15 @@ def unsignedToSigned(i, max_positive):
return i - 2 * max_positive
def getIntegerAsBlock(i):
x = unsignedToSigned(i % 4096, 2048)
i = int((i - x) / 4096)
y = unsignedToSigned(i % 4096, 2048)
i = int((i - y) / 4096)
z = unsignedToSigned(i % 4096, 2048)
return x,y,z
def readU8(f):
return ord(f.read(1))
@ -19,3 +32,11 @@ def readU32(f):
def readS32(f):
return unsignedToSigned(ord(f.read(1)) * 256 * 256 * 256 + ord(f.read(1)) * 256 * 256 + ord(f.read(1)) * 256 + ord(f.read(1)), 2 ** 31)
def gridToCoords(row, col):
return (row - col) / 2, (row + col) / 2
def coordsToGrid(x, z):
return x + z, z - x