forked from equalpants/pigmap
/
template.html
executable file
·633 lines (542 loc) · 19 KB
/
template.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Minecraft Map</title>
<script type="text/javascript" src="markers.js"></script>
<script type="text/javascript" src="regions.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="controls.js"></script>
<script type="text/javascript">
var config = {
path: '.',
fileExt: 'png',
tileSize: {tileSize},
defaultZoom: 0,
maxZoom: {baseZoom},
markerZoom: {baseZoom}-2, // This is the zoom level used after clicking on a marker in the marker list
B: {B},
T: {T},
debug: false,
bukkit: true, // Change to false if you are using hMod or anything other than Bukkit
craftbook: false, // Change this to true if you have used the CraftBook Hey0/Bukkit plugin
playerFaces: true, // Change to false to use custom icons, like before
avatarPath: 'http://www.example.com/path/to/avatar/directory', // See README.rst for details
playerInfos: false, // Change this to true to enable custom info windows for players, requires avatarPath
playerInterval: 5000 // in milliseconds
};
config.mapCenter = fromWorldToLatLng(8,64,8);
// set up MapMarkers marker array
var playerMarkers = new Array();
// URL parameter array
var urlParams = {};
// Regex for date/time
var reg = /(\d{4})(\d{2})(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
// Get current directory
var path = document.location.pathname;
var dir = path.substring(0, path.lastIndexOf('/'));
// our custom projection maps Latitude to Y, and Longitude to X as normal,
// but it maps the range [0.0, 1.0] to [0, tileSize] in both directions
// so it is easier to position markers, etc. based on their position
// (find their position in the lowest-zoom image, and divide by tileSize)
function MCMapProjection() {
this.inverseTileSize = 1.0 / config.tileSize;
}
MCMapProjection.prototype.fromLatLngToPoint = function(latLng) {
var x = latLng.lng() * config.tileSize;
var y = latLng.lat() * config.tileSize;
return new google.maps.Point(x, y);
};
MCMapProjection.prototype.fromPointToLatLng = function(point) {
var lng = point.x * this.inverseTileSize;
var lat = point.y * this.inverseTileSize;
return new google.maps.LatLng(lat, lng);
};
// pigmap lat/long converter
// this function takes its arguments in the *same order* as the previous
// Overviewer version--minecraft X, minecraft Y, minecraft Z--so callers
// do not need to be changed
// ...however, this one does not rename the variables, so what we call "y"
// here is also called "y" in both minecraft and pigmap
// (the pigmap docs write coords in X,Z,Y order, so unfortunately
// confusion is still possible, but at least the *names* are the same)
function fromWorldToLatLng(x, y, z) {
// the width and height of all the highest-zoom tiles combined, inverted
var perPixel = 1.0 / (config.tileSize * Math.pow(2, config.maxZoom));
var B = config.B;
var T = config.T;
// fail in a conspicuous way if tileSize doesn't match B and T
if (config.tileSize != 64*B*T) {
console.log("Tile size does not match 64*B*T");
return new google.maps.LatLng(0.5, 0.5);
}
// the center of block [0,0,0] is at [2B, 64BT-17B] in the tile [tiles/2, tiles/2]
var lng = 0.5 + 2*B * perPixel;
var lat = 0.5 + (config.tileSize - 17*B) * perPixel;
// each block on X adds [2B,-B]
lng += 2*B * x * perPixel;
lat += -B * x * perPixel;
// each block on Y adds [0,-2B]
lat += -2*B * y * perPixel;
// each block on Z adds [2B,B]
lng += 2*B * z * perPixel;
lat += B * z * perPixel;
return new google.maps.LatLng(lat, lng);
}
var MCMapOptions = {
getTileUrl: function(tile, zoom) {
var url = config.path;
if(tile.x < 0 || tile.x >= Math.pow(2, zoom) || tile.y < 0 || tile.y >= Math.pow(2, zoom)) {
url += '/blank';
} else if(zoom == 0) {
url += '/base';
} else {
for(var z = zoom - 1; z >= 0; --z) {
var x = Math.floor(tile.x / Math.pow(2, z)) % 2;
var y = Math.floor(tile.y / Math.pow(2, z)) % 2;
url += '/' + (x + 2 * y);
}
}
url = url + '.' + config.fileExt;
return(url);
},
tileSize: new google.maps.Size(config.tileSize, config.tileSize),
maxZoom: config.maxZoom,
minZoom: 0,
isPng: !(config.fileExt.match(/^png$/i) == null)
};
var MCMapType = new google.maps.ImageMapType(MCMapOptions);
MCMapType.name = "MC Map";
MCMapType.alt = "Minecraft Map";
MCMapType.projection = new MCMapProjection();
function CoordMapType() {
}
function CoordMapType(tileSize) {
this.tileSize = tileSize;
}
CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
var div = ownerDocument.createElement('DIV');
div.innerHTML = "(" + coord.x + ", " + coord.y + ", " + zoom + ")";
div.innerHTML += "<br />";
div.innerHTML += MCMapOptions.getTileUrl(coord, zoom);
div.style.width = this.tileSize.width + 'px';
div.style.height = this.tileSize.height + 'px';
div.style.fontSize = '10';
div.style.borderStyle = 'solid';
div.style.borderWidth = '1px';
div.style.borderColor = '#AAAAAA';
return div;
};
// Checks the URL for availability
function checkURL(url) {
var req= new AJ(); // XMLHttpRequest object
try {
req.open("HEAD", url, false);
req.send(null);
return req.status == 200 ? true : false;
} catch (er) {
return false;
}
}
// XMLHttpRequest Object, for use with checkURL()
function AJ() {
var obj;
if (window.XMLHttpRequest) obj= new XMLHttpRequest();
else if (window.ActiveXObject) {
try {
obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch(er) {
try {
obj= new ActiveXObject("Microsoft.XMLHTTP");
} catch(er) {
obj= false;
}
}
}
return obj;
}
var map;
function prepareSignMarker(marker, item) {
var c = "<div class=\"infoWindow\"><img src=\"signpost.png\" /><p>" + item.msg.replace(/\n/g,"<br/>") + "</p></div>";
var infowindow = new google.maps.InfoWindow({
content: c
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
function preparePlayerMarker(marker, item) {
var imageURL = 'default.png';
var testURL = config.avatarPath + "/" + item.msg + ".png";
if (checkURL(testURL))
var imageURL = testURL;
var c = "<div class=\"infoWindow\"><img src=\"" + imageURL + "\" /><p>" + item.msg + "</p></div>";
var infowindow = new google.maps.InfoWindow({
content: c
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
// Center map on player icon
function gotoPlayer(index) {
map.setCenter(playerMarkers[index].position);
map.setZoom(config.markerZoom);
}
// Test to make sure signs are not specific formats,
// like blank signs and those in the CraftBook plugin
function checkSign(str) {
var str2 = str.replace(/\n/g,"")
// Remove blank signs
if (str2 == "" || str2 == null || str2 == " ")
return false;
// Check for extra formats only if CraftBook is used :)
if (config.craftbook) {
if (str.toLowerCase().indexOf("[gate]") != -1)
return false;
if (str.toLowerCase().indexOf("[lift]") != -1)
return false;
if (str.toLowerCase().indexOf("[lift up]") != -1)
return false;
if (str.toLowerCase().indexOf("[lift down]") != -1)
return false;
if (str.toLowerCase().indexOf("[toggle]") != -1)
return false;
if (str.toLowerCase().indexOf("[mc") != -1)
return false;
if (str.toLowerCase().indexOf("[bridge]") != -1)
return false;
if (str.toLowerCase().indexOf("[station]") != -1)
return false;
if (str.toLowerCase().indexOf("[dispenser]") != -1)
return false;
}
return true;
}
var markersInit = false;
function initMarkers() {
if ((markersInit) || (!checkURL(dir + "/markers.js"))) { return; }
markersInit = true;
for (i in markerData) {
if ("no_markers" in urlParams)
continue;
var item = markerData[i];
if (item.type == 'sign') {
if ("no_signs" in urlParams)
continue;
// Check sign for unneeded formats
if (!checkSign(item.msg))
continue;
}
if ("no_places" in urlParams && item.type == 'spawn')
continue;
// a default:
var iconURL = '';
if (item.type == 'spawn') { iconURL = 'http://google-maps-icons.googlecode.com/files/home.png';}
if (item.type == 'sign') { iconURL = 'signpost_icon.png';}
var converted = fromWorldToLatLng(item.x, item.y, item.z);
var marker = new google.maps.Marker({
position: converted,
map: map,
title: item.msg,
icon: iconURL
});
if (item.type == 'sign') {
prepareSignMarker(marker, item);
}
}
}
var regionsInit = false;
function initRegions() {
if ((regionsInit) || (!checkURL(dir + "/regions.js"))) { return; }
regionsInit = true;
for (i in regionData) {
if ("no_markers" in urlParams)
return;
var region = regionData[i];
var converted = new google.maps.MVCArray();
for (j in region.path) {
var point = region.path[j];
converted.push(fromWorldToLatLng(point.x, point.y, point.z));
}
if (region.closed) {
new google.maps.Polygon({
clickable: false,
geodesic: false,
map: map,
strokeColor: region.color,
strokeOpacity: region.opacity,
strokeWeight: 2,
fillColor: region.color,
fillOpacity: region.opacity * 0.25,
zIndex: i,
paths: converted
});
} else {
new google.maps.Polyline({
clickable: false,
geodesic: false,
map: map,
strokeColor: region.color,
strokeOpacity: region.opacity,
strokeWeight: 2,
zIndex: i,
path: converted
});
}
}
}
function makeLink() {
var a=window.location.href.substring(0,location.href.lastIndexOf("/")+1)
+ "?lat=" + map.getCenter().lat().toFixed(6)
+ "&lng=" + map.getCenter().lng().toFixed(6)
+ "&zoom=" + map.getZoom();
document.getElementById("link").innerHTML = a;
}
function delMarker(markername) {
marker = playerMarkers[markername];
if (marker) {
marker.setVisible(false);
$('#mcmarkerlist div[name="'+markername+'"]').remove();
}
}
function addMarker(item) {
// Add marker if it doesnt exist
// if it does, update position
if ("no_markers" in urlParams)
return;
if ("no_players" in urlParams && item.id == 4)
return;
if ("no_places" in urlParams && item.id == 3)
return;
if (config.bukkit)
item.id = 4; // Expect Bukkit MapMarkers plugin is missing ID
if (item.id == 0 && item.msg == "Spawn")
return;
// defaults:
var iconURL = '';
var shadowURL = '';
var shape;
// Here you can add custom icons
// Go to http://code.google.com/p/google-maps-icons/ for icons similar to the default
if (item.id == 0) { iconURL = 'http://google-maps-icons.googlecode.com/files/home.png';}
else if (item.id == 1) { iconURL = 'http://google-maps-icons.googlecode.com/files/home.png';}
else if (item.id == 2) { iconURL = 'http://google-maps-icons.googlecode.com/files/apartment.png';}
else if (item.id == 3) { iconURL = 'http://google-maps-icons.googlecode.com/files/conference.png';}
else if (item.id == 4) {
if (config.playerFaces == true) {
// Set player icon
iconURL = new google.maps.MarkerImage(
dir + '/player-icon.php?player=' + item.msg,
new google.maps.Size(24, 24),
new google.maps.Point(0,0),
new google.maps.Point(-7 , 17));
// Set player shadow/outline
shadowURL = new google.maps.MarkerImage('iconShadow.png',
new google.maps.Size(38, 38),
new google.maps.Point(0,0),
new google.maps.Point(0, 24));
// Set clickable area
shape = {
coord: [0, 0, 0, 23, 23, 23, 23, 0],
type: 'poly'
};
} else {
// You can have different icons for each player.
if (item.msg == "Drakonas") { { iconURL = 'http://google-maps-icons.googlecode.com/files/hunting.png';}
//} else if (item.msg == "player") { { iconURL = 'http://google-maps-icons.googlecode.com/files/archery.png';}
} else { { iconURL = 'http://google-maps-icons.googlecode.com/files/hiking.png';} } // Default icon
}
}
else if (item.id == 5) { iconURL = 'http://google-maps-icons.googlecode.com/files/museum-historical.png';}
var m = reg.exec(item.timestamp),
ts = new Date(m[1],m[2]-1,m[3],m[4],m[5],m[6]),
d = new Date(),
diff = d.getTime() - ts.getTime(),
converted = fromWorldToLatLng(item.x, item.y, item.z);
marker = playerMarkers[item.msg+item.id];
if (marker) {
if (!marker.getVisible())
marker.setVisible(true);
if ($('#mcmarkerlist div[name="'+item.msg+item.id+'"]').text() == item.msg)
$('#mcmarkerlist div[name="'+item.msg+item.id+'"]').remove();
if (diff < 10 * 1000*60) {
$('#mcmarkerlist').append('<div name="'+item.msg+item.id+'" class="mcmarker">'+item.msg+'</div>');
$('#mcmarkerlist div[name="'+item.msg+item.id+'"]').click(function() {
gotoPlayer(item.msg+item.id);
});
} else {
marker.setTitle(item.msg + " - Idle since " + ts.toString());
$('#mcmarkerlist').append('<div name="'+item.msg+item.id+'" class="idle">'+item.msg+'</div>');
$('#mcmarkerlist div[name="'+item.msg+item.id+'"]').click(function() {
gotoPlayer(item.msg+item.id);
});
}
marker.setPosition(converted);
} else {
if (diff < 10 * 1000*60) {
var marker = new google.maps.Marker({
position: converted,
map: map,
title: item.msg,
icon: iconURL,
shadow: shadowURL,
shape: shape
});
playerMarkers[item.msg+item.id] = marker;
if (item.id == 4 && config.playerAvatars)
preparePlayerMarker(marker, item);
$('#mcmarkerlist').append('<div name="'+item.msg+item.id+'" class="mcmarker">'+item.msg+'</div>');
$('#mcmarkerlist div[name="'+item.msg+item.id+'"]').click(function() {
gotoPlayer(item.msg+item.id);
});
} else {
var marker = new google.maps.Marker({
position: converted,
map: map,
title: item.msg + " - Idle since " + ts.toString(),
icon: iconURL,
shadow: shadowURL,
shape: shape
});
playerMarkers[item.msg+item.id] = marker;
if (item.id == 4 && config.playerAvatars)
preparePlayerMarker(marker, item);
$('#mcmarkerlist').append('<div name="'+item.msg+item.id+'" class="idle">'+item.msg+'</div>');
$('#mcmarkerlist div[name="'+item.msg+item.id+'"]').click(function() {
gotoPlayer(item.msg+item.id);
});
}
}
}
var loading = true;
function refreshMarkers() {
$.getJSON('markers.json', function(data) {
if (data == null || data.length == 0) {
if (!($('#mcmarkerlist div[name="offline"]').text() == '[No players online]'))
$('#mcmarkerlist').append('<div name="offline">[No players online]</div>');
for (marker in playerMarkers) {
delMarker(marker);
}
return;
} else {
for (marker in playerMarkers) {
var found = false;
for (item in data) {
var itemID;
if (config.bukkit)
itemID = 4;
else
itemID = data[item].id;
if (marker == data[item].msg + itemID)
found = true;
}
if (!found) {
if (playerMarkers[marker].getVisible()) {
delMarker(marker);
}
}
}
if (data.length > 0 && ($('#mcmarkerlist div[name="offline"]').text() == '[No players online]'))
$('#mcmarkerlist div[name="offline"]').remove();
for (item in data) {
if ((data[item].id == 4) || (config.bukkit))
addMarker(data[item]);
}
}
});
if (loading) {
$('#mcmarkerlist div[name="loading"]').remove();
loading = false;
}
}
function initialize() {
var query = window.location.search.substring(1);
var lat = 0.5;
var lng = 0.5;
var zoom = config.defaultZoom;
var pairs = query.split("&");
for (var i=0; i<pairs.length; i++) {
// break each pair at the first "=" to obtain the argname and value
var pos = pairs[i].indexOf("=");
var argname = pairs[i].substring(0,pos).toLowerCase();
var value = pairs[i].substring(pos+1).toLowerCase();
// process each possible argname
if (argname == "lat") {lat = parseFloat(value);}
if (argname == "lng") {lng = parseFloat(value);}
if (argname == "zoom") {zoom = parseInt(value);}
}
var mapOptions = {
zoom: config.defaultZoom,
navigationControl: true,
scaleControl: false,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: 'mcmap'
};
map = new google.maps.Map(document.getElementById("mcmap"), mapOptions);
if(config.debug) {
map.overlayMapTypes.insertAt(0, new CoordMapType(new google.maps.Size(config.tileSize, config.tileSize)));
google.maps.event.addListener(map, 'click', function(event) {
console.log("latLng; " + event.latLng.lat() + ", " + event.latLng.lng());
var pnt = map.getProjection().fromLatLngToPoint(event.latLng);
console.log("point: " + pnt);
var pxx = pnt.x * config.tileSize * Math.pow(2, config.maxZoom);
var pxy = pnt.y * config.tileSize * Math.pow(2, config.maxZoom);
console.log("pixel: " + pxx + ", " + pxy);
});
}
// Now attach the coordinate map type to the map's registry
map.mapTypes.set('mcmap', MCMapType);
// We can now set the map to use the 'coordinate' map type
map.setMapTypeId('mcmap');
// Center map on spawn
map.setCenter(config.mapCenter);
(function () {
var e,
d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); },
q = window.location.search.substring(1),
r = /([^&=]+)=?([^&]*)/g;
while (e = r.exec(q))
urlParams[d(e[1])] = d(e[2]);
})();
if ("lat" in urlParams && "lng" in urlParams && "zoom" in urlParams) {
// process each possible argname
var lat = parseFloat(urlParams.lat);
var lng = parseFloat(urlParams.lng);
var zoom = parseFloat(urlParams.zoom);
map.setCenter(new google.maps.LatLng(lat,lng))
map.setZoom(zoom);
}
if ("no_overlay" in urlParams)
$('#mcmarkerlist').hide();
if ("no_markers" in urlParams)
$('#mcmarkerlist').hide();
if ("no_players" in urlParams)
$('#mcmarkerlist').hide();
google.maps.event.addListener(map, 'zoom_changed', function() {
makeLink();
});
google.maps.event.addListener(map, 'center_changed', function() {
makeLink();
});
// initialize the markers and regions
initMarkers();
initRegions();
var compassDiv = document.createElement('DIV');
compassDiv.style.padding = '5px';
var compassImg = document.createElement('IMG');
compassImg.src="compass.png";
compassDiv.appendChild(compassImg);
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(compassDiv);
var refreshInterval = setInterval(refreshMarkers, config.playerInterval);
}
</script>
</head>
<body onload="initialize()">
<div id="mcmap" style="width:100%; height:100%"></div>
</body>
</html>