Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
Fix usage of map_reader.
Handle error in map reading
Handle headleass environments
  • Loading branch information
nicolas-f committed Sep 2, 2014
1 parent 2c493b3 commit 412b526
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
15 changes: 14 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
return container;
}
});
map.addControl(new MyControl());

// Add mouse position
L.control.mousePosition({
lngFormatter : function(pos) {
// lng to W E
Expand All @@ -96,7 +98,18 @@
return Math.abs(Math.round(pos)) + (pos >=0 ? " N" : " S");
}
}).addTo(map);
map.addControl(new MyControl());

// Add overlay region tiles
//var canvasTiles = L.tileLayer.canvas({maxNativeZoom : native_zoom_level, continuousWorld: 'false'});
//canvasTiles.drawTile = function(canvas, tilePoint, zoom) {
// var ctx = canvas.getContext('2d');
// ctx.fillStyle="#FF0000";
// ctx.fillText(tilePoint.toString()+" "+zoom, 50, 50);
// ctx.globalAlpha = 0.2;
// var tileSize = this.options.tileSize;
//};

//canvasTiles.addTo(map);
</script>
</body>
</html>
44 changes: 27 additions & 17 deletions map_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# @author Nicolas Fortin github@nettrader.fr https://github.com/nicolas-f
# @author Nicolas Grimaud ketchu13@hotmail.com

from struct import unpack
import struct
import itertools
import getopt
import sys
Expand Down Expand Up @@ -52,22 +52,24 @@ def import_file(self, map_file, index_only, skip_existing=True):
curs.read(1)
#######################
# read index
num = unpack("I", curs.read(4))[0]
num = struct.unpack("I", curs.read(4))[0]
print "Tiles :", num
# read tiles position
tiles_index = [unpack("i", curs.read(4))[0] for i in xrange(num)]
tiles_index = [struct.unpack("i", curs.read(4))[0] for i in xrange(num)]
#######################
# read tiles pixels
new_tiles = 0
# A tile is 256 unsigned integer
fmt = "H" * 256
if not index_only:
curs.seek(524297)
for i in xrange(num):
if not skip_existing or tiles_index[i] not in self.tiles:
#extract 16-bytes pixel then convert to RGB component
chunk = [[[((rgb & 0x7C00) >> 10 << 3), ((rgb & 0x3E0) >> 5) << 3, (rgb & 0x1F) << 3]
for rgb in unpack("H", curs.read(2))][0] for c in xrange(256)]
# extract 16-bytes pixel then convert to RGB component
chunk = "".join([chr((rgb & 0x7C00) >> 10 << 3) +
chr(((rgb & 0x3E0) >> 5) << 3) +
chr((rgb & 0x1F) << 3) for rgb in struct.unpack(fmt, curs.read(512))])
#flatten pixels and convert to char
chunk = "".join([chr(band) for pixel in chunk for band in pixel])
self.tiles[tiles_index[i]] = chunk
new_tiles += 1
else:
Expand Down Expand Up @@ -97,8 +99,11 @@ def create_base_tiles(player_map_path, tile_output_path, tile_level):
reader = MapReader()
# Read and merge all tiles in .map files
for i, map_file in enumerate(player_map_path):
print "Read map file ", i + 1, "/", len(player_map_path)
reader.import_file(map_file, False)
print "Read map file ", os.path.basename(map_file), i + 1, "/", len(player_map_path)
try:
reader.import_file(map_file, False)
except struct.error:
print "Skip "+os.path.basename(map_file)+" may be already used by another process"
# make zoom folder
z_path = os.path.join(tile_output_path, str(tile_level))
if not os.path.exists(z_path):
Expand Down Expand Up @@ -231,14 +236,19 @@ def main():
raw_input()
exit(-1)
if game_player_path is None:
# Show gui to select tile folder (windows only)
import tkFileDialog
from Tkinter import Tk
root = Tk()
root.withdraw()
opts = {"initialdir": os.path.expanduser("~\\Documents\\7 Days To Die\\Saves\\Random Gen\\"),
"title": "Choose player path that countain .map files"}
game_player_path = tkFileDialog.askdirectory(**opts)
# Show gui to select tile folder
try:
import tkFileDialog
from Tkinter import Tk
root = Tk()
root.withdraw()
opts = {"initialdir": os.path.expanduser("~\\Documents\\7 Days To Die\\Saves\\Random Gen\\"),
"title": "Choose player path that countain .map files"}
game_player_path = tkFileDialog.askdirectory(**opts)
except ImportError:
#Headless environment
usage()
exit(-1)
if len(game_player_path) == 0:
print "You must define the .map game path"
exit(-1)
Expand Down

0 comments on commit 412b526

Please sign in to comment.