From 4f75bf691ad0a264133a98ca14c8e8d933ca842c Mon Sep 17 00:00:00 2001 From: FrozenCow Date: Sun, 24 Jul 2011 14:56:23 +0200 Subject: [PATCH] Fixed nightday and making use of options.mapzoomin and options.mapzoomout. --- web/js/dynmaputils.js | 40 ++++++++++++++++++++++++++++++++++++---- web/js/flatmap.js | 2 +- web/js/hdmap.js | 37 ++++++++----------------------------- web/js/kzedmaps.js | 2 +- web/js/map.js | 29 ++++++++++++----------------- 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/web/js/dynmaputils.js b/web/js/dynmaputils.js index 341db0d5..69e9072d 100644 --- a/web/js/dynmaputils.js +++ b/web/js/dynmaputils.js @@ -124,15 +124,47 @@ var DynmapTileLayer = L.TileLayer.extend({ this._removeOtherTiles(tileBounds); } }, - calculateTileSize: function(zoom) { + /*calculateTileSize: function(zoom) { return this.options.tileSize; + },*/ + calculateTileSize: function(zoom) { + // zoomoutlevel: 0 when izoom > mapzoomin, else mapzoomin - izoom (which ranges from 0 till mapzoomin) + var izoom = this.options.maxZoom - zoom; + var zoominlevel = Math.max(0, this.options.mapzoomin - izoom); + return 128 << zoominlevel; }, setTileSize: function(tileSize) { this.options.tileSize = tileSize; this._tiles = {}; this._createTileProto(); }, - updateTileSize: function(zoom) {} + updateTileSize: function(zoom) {}, + + // Some helper functions. + zoomprefix: function(amount) { + // amount == 0 -> '' + // amount == 1 -> 'z_' + // amount == 2 -> 'zz_' + return 'zzzzzzzzzzzzzzzzzzzzzz'.substr(0, amount) + (amount === 0 ? '' : '_'); + }, + getTileInfo: function(tilePoint, zoom) { + // zoom: max zoomed in = this.options.maxZoom, max zoomed out = 0 + // izoom: max zoomed in = 0, max zoomed out = this.options.maxZoom + // zoomoutlevel: 0 when izoom < mapzoomin, else izoom - mapzoomin (which ranges from 0 till mapzoomout) + var izoom = this.options.maxZoom - zoom; + var zoomoutlevel = Math.max(0, izoom - this.options.mapzoomin); + var scale = 1 << zoomoutlevel; + var zoomprefix = this.zoomprefix(zoomoutlevel); + return { + prefix: this.options.prefix, + nightday: (this.options.nightandday && this.options.dynmap.serverday) ? '_day' : '', + scaledx: (scale*tilePoint.x) >> 5, + scaledy: (-scale*tilePoint.y) >> 5, + zoom: this.zoomprefix(zoomoutlevel), + x: scale*tilePoint.x, + y: -scale*tilePoint.y + }; + } }); function loadjs(url, completed) { @@ -215,8 +247,8 @@ function namedReplace(str, obj) break; } if (variableBegin < variableEnd) { - var variableName = str.substring(variableBegin+1, variableEnd-1); - result += str.substring(startIndex, variableBegin-1); + var variableName = str.substring(variableBegin+1, variableEnd); + result += str.substring(startIndex, variableBegin); result += obj[variableName]; } else /* found '{}' */ { result += str.substring(startIndex, variableBegin-1); diff --git a/web/js/flatmap.js b/web/js/flatmap.js index d740b4c6..b7b8eb67 100644 --- a/web/js/flatmap.js +++ b/web/js/flatmap.js @@ -17,7 +17,7 @@ var FlatMapType = DynmapTileLayer.extend({ getTileName: function(tilePoint, zoom) { var tileName; var dnprefix = ''; - if(this.options.nightandday && this.dynmap.serverday) { + if(this.options.nightandday && this.options.dynmap.serverday) { dnprefix = '_day'; } var extrazoom = this.options.world.extrazoomout; diff --git a/web/js/hdmap.js b/web/js/hdmap.js index 79a047fb..da799d0e 100644 --- a/web/js/hdmap.js +++ b/web/js/hdmap.js @@ -3,9 +3,10 @@ var HDProjection = DynmapProjection.extend({ var wtp = this.options.worldtomap; var xx = wtp[0]*location.x + wtp[1]*location.y + wtp[2]*location.z; var yy = wtp[3]*location.x + wtp[4]*location.y + wtp[5]*location.z; - var lat = xx / (8 << this.options.extrazoom); - var lng = (128-yy) / (8 << this.options.extrazoom); - return new L.LatLng(lat, lng, true); + return new L.LatLng( + xx / (1 << this.options.mapzoomout) + , (128-yy) / (1 << this.options.mapzoomout) + , true); } }); @@ -13,37 +14,15 @@ var HDMapType = DynmapTileLayer.extend({ projection: undefined, options: { minZoom: 0, - maxZoom: 3 + maxZoom: 0 }, initialize: function(options) { - options.maxZoom = options.mapzoomin + options.world.extrazoomout; + options.maxZoom = options.mapzoomin + options.mapzoomout; L.Util.setOptions(this, options); - this.projection = new HDProjection({worldtomap: options.worldtomap}) + this.projection = new HDProjection($.extend({map: this}, options)); }, getTileName: function(tilePoint, zoom) { - var tileName; - - var dnprefix = ''; - if(this.options.nightandday && this.options.dynmap.serverday) { - dnprefix = '_day'; - } - - var extrazoom = this.options.mapzoomout; - if(zoom < extrazoom) { - var scale = 1 << (extrazoom-zoom); - var zprefix = "zzzzzzzzzzzzzzzzzzzzzz".substring(0, extrazoom-zoom); - tileName = this.options.prefix + dnprefix + '/' + ((scale*tilePoint.x) >> 5) + '_' + ((-scale*tilePoint.y) >> 5) + '/' + zprefix + "_" + (scale*tilePoint.x) + '_' + (-scale*tilePoint.y) + '.png'; - } else { - tileName = this.options.prefix + dnprefix + '/' + (tilePoint.x >> 5) + '_' + ((-tilePoint.y) >> 5) + '/' + tilePoint.x + '_' + (-tilePoint.y) + '.png'; - } - return tileName; - }, - calculateTileSize: function(zoom) { - var extrazoom = this.options.mapzoomout; - console.log(zoom <= extrazoom, zoom, extrazoom); - return (zoom <= extrazoom) - ? 128 - : Math.pow(2, 7+zoom-extrazoom); + return namedReplace('{prefix}{nightday}/{scaledx}_{scaledy}/{zoom}{x}_{y}.png', this.getTileInfo(tilePoint, zoom)); } }); diff --git a/web/js/kzedmaps.js b/web/js/kzedmaps.js index f2b711b1..d5c9ed0c 100644 --- a/web/js/kzedmaps.js +++ b/web/js/kzedmaps.js @@ -53,7 +53,7 @@ var KzedMapType = DynmapTileLayer.extend({ }, calculateTileSize: function(zoom) { var extrazoomout = this.options.dynmap.world.extrazoomout; - return (zoom <= extrazoom) + return (zoom <= extrazoomout) ? 128 : Math.pow(2, 6+zoom-extrazoomout); } diff --git a/web/js/map.js b/web/js/map.js index a4c5abd4..7d4cacb6 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -391,11 +391,18 @@ DynMap.prototype = { } me.servertime = update.servertime; - var oldday = me.serverday; - if(me.servertime > 23100 || me.servertime < 12900) - me.serverday = true; - else - me.serverday = false; + var newserverday = (me.servertime > 23100 || me.servertime < 12900); + if(me.serverday != newserverday) { + console.log('serverday changed', newserverday) + me.serverday = newserverday; + + me.updateBackground(); + if(me.maptype.options.nightandday) { + // Readd map. + me.map.removeLayer(me.maptype); + me.map.addLayer(me.maptype); + } + } var newplayers = {}; $.each(update.players, function(index, playerUpdate) { @@ -438,18 +445,6 @@ DynMap.prototype = { //var divs = $('div[rel]'); //divs.filter(function(i){return parseInt(divs[i].attr('rel')) > timestamp+me.options.messagettl;}).remove(); }); - - if(me.serverday != oldday) { - me.updateBackground(); - var mtid = me.map.mapTypeId; - console.log('TODO: RENDER NIGHTANDDAY!!!') - /*if(me.map.mapTypes[mtid].nightandday) { - me.map.setMapTypeId('none'); - window.setTimeout(function() { - me.map.setMapTypeId(mtid); - }, 0.1); - }*/ - } $(me).trigger('worldupdated', [ update ]);