source: binary-improvements/webserver/js/map.js@ 245

Last change on this file since 245 was 245, checked in by alloc, 9 years ago

Fixes

File size: 5.4 KB
Line 
1var mapinfo = {
2 regionsize: 512,
3 chunksize: 16,
4 tilesize: 128,
5 maxzoom: 4
6}
7
8function InitMap() {
9 // ===============================================================================================
10 // 7dtd coordinate transformations
11
12 SDTD_Projection = {
13 project: function (latlng) {
14 return new L.Point(
15 (latlng.lat) / Math.pow(2, mapinfo.maxzoom),
16 (latlng.lng) / Math.pow(2, mapinfo.maxzoom) );
17 },
18
19 unproject: function (point) {
20 return new L.LatLng(
21 point.x * Math.pow(2, mapinfo.maxzoom),
22 point.y * Math.pow(2, mapinfo.maxzoom) );
23 }
24 };
25
26 SDTD_CRS = L.extend({}, L.CRS.Simple, {
27 projection: SDTD_Projection,
28 transformation: new L.Transformation(1, 0, -1, 0),
29
30 scale: function (zoom) {
31 return Math.pow(2, zoom);
32 }
33 });
34
35
36
37
38 // ===============================================================================================
39 // Map and basic tile layers
40
41 var initTime = new Date().getTime();
42
43 map = L.map('tab_map', {
44 zoomControl: false, // Added by Zoomslider
45 zoomsliderControl: true,
46 attributionControl: false,
47 crs: SDTD_CRS
48 }).setView([0, 0], Math.max(0, mapinfo.maxzoom - 5));
49
50 var tileLayer = L.tileLayer('../map/{z}/{x}/{y}.png?t={time}', {
51 maxZoom: mapinfo.maxzoom + 1,
52 minZoom: Math.max(0, mapinfo.maxzoom - 5),
53 maxNativeZoom: mapinfo.maxzoom,
54 tileSize: mapinfo.tilesize,
55 continuousWorld: true,
56 tms: true,
57 unloadInvisibleTiles: false,
58 time: initTime
59 });
60
61 // TileLayer w/ TMS=true fix for zoomlevel >= 8
62 tileLayer._getWrapTileNum = function () {
63 return L.point(0, 0);
64 };
65
66 var tileLayerMiniMap = L.tileLayer('../map/{z}/{x}/{y}.png?t={time}', {
67 maxZoom: mapinfo.maxzoom,
68 minZoom: 0,
69 maxNativeZoom: mapinfo.maxzoom,
70 tileSize: mapinfo.tilesize,
71 continuousWorld: true,
72 tms: true,
73 unloadInvisibleTiles: false,
74 time: initTime
75 });
76
77
78
79
80
81
82
83 // ===============================================================================================
84 // Overlays and controls
85
86 var playersOnlineMarkerGroup = L.markerClusterGroup({
87 maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
88 });
89 var playersOfflineMarkerGroup = L.markerClusterGroup({
90 maxClusterRadius: function(zoom) { return zoom == mapinfo.maxzoom ? 10 : 50; }
91 });
92
93
94
95 var baseLayers = {
96 //"Map": tileLayer
97 };
98
99 var layerControl = L.control.layers(baseLayers, null, {
100 collapsed: false
101 });
102
103 var layerCount = 0;
104
105
106 tileLayer.addTo(map);
107 new L.Control.Coordinates({}).addTo(map);
108 new L.Control.ReloadTiles({layers: [tileLayer, tileLayerMiniMap]}).addTo(map);
109 layerControl.addOverlay (GetRegionLayer (mapinfo), "Region files");
110 var miniMap = new L.Control.MiniMap(tileLayerMiniMap, {
111 zoomLevelOffset: -6,
112 toggleDisplay: true
113 }).addTo(map);
114
115 if (HasPermission ("webapi.getlandclaims")) {
116 layerControl.addOverlay (GetLandClaimsLayer (map, mapinfo), "Land claims");
117 layerCount++;
118 }
119 if (HasPermission ("webapi.getplayerslocation")) {
120 layerControl.addOverlay (playersOfflineMarkerGroup, "Players (offline) (<span id='mapControlOfflineCount'>0</span>)");
121 layerControl.addOverlay (playersOnlineMarkerGroup, "Players (online) (<span id='mapControlOnlineCount'>0</span>)");
122 layerCount++;
123 }
124
125 if (layerCount > 0) {
126 layerControl.addTo(map);
127 }
128
129
130
131
132 var playersMappingList = {};
133
134
135
136 // ===============================================================================================
137 // Player markers
138
139 $(".leaflet-popup-pane").on('click.action', '.inventoryButton', function(event) {
140 ShowInventoryDialog ($(this).data('steamid'));
141 });
142
143 var setPlayerMarkers = function(data) {
144 var online = 0;
145 var offline = 0;
146 $.each( data, function( key, val ) {
147 var marker;
148 if (playersMappingList.hasOwnProperty(val.steamid)) {
149 marker = playersMappingList[val.steamid].currentPosMarker;
150 marker.setLatLng([val.position.x, val.position.z]);
151 } else {
152 marker = L.marker([val.position.x, val.position.z]).bindPopup(
153 "Player: " + val.name +
154 (HasPermission ("webapi.getplayerinventory") ?
155 "<br/><a class='inventoryButton' data-steamid='"+val.steamid+"'>Show inventory</a>"
156 : "")
157 );
158 playersMappingList[val.steamid] = { online: !val.online };
159 }
160 if (playersMappingList[val.steamid].online != val.online) {
161 if (val.online) {
162 marker.setOpacity(1.0);
163 playersOfflineMarkerGroup.removeLayer(marker);
164 playersOnlineMarkerGroup.addLayer(marker);
165 } else {
166 marker.setOpacity(0.5);
167 playersOnlineMarkerGroup.removeLayer(marker);
168 playersOfflineMarkerGroup.addLayer(marker);
169 }
170 }
171 val.currentPosMarker = marker;
172 playersMappingList[val.steamid] = val;
173
174 if (val.online)
175 online++;
176 else
177 offline++;
178 });
179 $( "#mapControlOnlineCount" ).text( online );
180 $( "#mapControlOfflineCount" ).text( offline );
181 }
182
183 var updatePlayerEvent = function() {
184 $.getJSON( "../api/getplayerslocation")
185 .done(setPlayerMarkers)
186 .fail(function(jqxhr, textStatus, error) {
187 console.log("Error fetching players list");
188 })
189 .always(function() {
190 window.setTimeout(updatePlayerEvent, 2000);
191 });
192 }
193
194 if (HasPermission ("webapi.getplayerslocation")) {
195 window.setTimeout(updatePlayerEvent, 0);
196 }
197
198
199}
200
201
202
203
204
205function StartMapModule () {
206 $.getJSON( "../map/mapinfo.json")
207 .done(function(data) {
208 mapinfo.tilesize = data.blockSize;
209 mapinfo.maxzoom = data.maxZoom;
210 })
211 .fail(function(jqxhr, textStatus, error) {
212 console.log ("Error fetching map information");
213 })
214 .always(function() {
215 InitMap ();
216 });
217}
218
219
Note: See TracBrowser for help on using the repository browser.