forked from overviewer/Minecraft-Overviewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
markers.py
executable file
·151 lines (114 loc) · 4.77 KB
/
markers.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This file is part of the Minecraft Overviewer.
#
# Minecraft Overviewer is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# Minecraft Overviewer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
import functools
import os
import os.path
import numpy
import math
import sys
import chunk
import nbt
import json
"""
This module has routines related to exporting markers to overlay on a generated map
"""
def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
'''
Convert an integer to a base36 string.
'''
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')
newn = abs(number)
# Special case for zero
if number == 0:
return '0'
base36 = ''
while newn != 0:
newn, i = divmod(newn, len(alphabet))
base36 = alphabet[i] + base36
if number < 0:
return "-" + base36
return base36
class MarkerGenerator(object):
"""Generates all markers to overlay on map.
worlddir is the path to the minecraft world
"""
def __init__(self, worlddir, destdir):
self.worlddir = worlddir
self.destdir = destdir
# stores Points Of Interest to be mapped with markers
# a list of dictionaries, see below for an example
self.POI = []
def addSpawn(self):
"""Adds the true spawn location to self.POI."""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
spawnX = data['Data']['SpawnX']
spawnY = data['Data']['SpawnY']
spawnZ = data['Data']['SpawnZ']
self.POI.append( dict(x=spawnX, y=spawnY, z=spawnZ, msg="Spawn", id=0))
def addLabels(self):
"""Adds the labels from the server to self.POI."""
## read label info from mapper-labels.txt
path = os.path.join(self.worlddir, "mapper-labels.txt")
print "Adding labels to map"
self.addMarkers(path);
def addPlayers(self):
"""Adds the players positions from the server to self.POI."""
## read label info from mapper-labels.txt
path = os.path.join(self.worlddir, "mapper-playerpos.txt")
print "Adding players to map"
self.addMarkers(path);
def addHomes(self):
"""Adds the players homes from the server to self.POI."""
## read label info from mapper-labels.txt
path = os.path.join(self.worlddir, "mapper-homes.txt")
print "Adding homes to map"
self.addMarkers(path);
def addMarkers(self, path):
"""Add marker to array"""
try:
if os.path.exists(path):
fileobj = open(path, "rb")
#print "Adding markers to map"
for line in fileobj:
#print "marker found: "+line
split = line.split(":");
if (len(split) >= 5):
text = split[0]
locX = math.trunc(float(split[1]))
locY = math.trunc(float(split[2]))
locZ = math.trunc(float(split[3]))
id = math.trunc(float(split[4]))
self.POI.append( dict(x=locX, y=locY, z=locZ, msg=text, id=id))
else:
continue;
except (Exception):
print "Exception while reading markers";
pass;
def write_markers(self):
"""Writes out markers.js"""
with open(os.path.join(self.destdir, "markers.js"), 'w') as output:
output.write("var markerData=%s" % json.dumps(self.POI))
with open(os.path.join(self.destdir, "markers.json"), 'w') as output:
output.write(json.dumps(self.POI))
def go(self, procs):
"""Starts the generation of markers"""
#print "Adding markers"
self.addSpawn()
self.addLabels()
self.addPlayers()
self.addHomes()
self.write_markers()