1 | var REGIONSIZE = 512;
|
---|
2 | var CHUNKSIZE = 16;
|
---|
3 | var TILESIZE = 128;
|
---|
4 | var MAXZOOM = 4;
|
---|
5 |
|
---|
6 | var BAG_COLS = 8;
|
---|
7 | var BAG_ROWS = 4;
|
---|
8 | var BELT_COLS = 8;
|
---|
9 | var INV_ITEM_WIDTH = 58;
|
---|
10 | var INV_ITEM_HEIGHT = 40;
|
---|
11 |
|
---|
12 | SDTD_Projection = {
|
---|
13 | project: function (latlng) {
|
---|
14 | return new L.Point(latlng.lng / 16, latlng.lat / 16);
|
---|
15 | },
|
---|
16 |
|
---|
17 | unproject: function (point) {
|
---|
18 | return new L.LatLng(point.y * 16, point.x * 16);
|
---|
19 | }
|
---|
20 | };
|
---|
21 |
|
---|
22 | SDTD_CRS = L.extend({}, L.CRS.Simple, {
|
---|
23 | projection: SDTD_Projection,
|
---|
24 | transformation: new L.Transformation(1, 0, -1, 0),
|
---|
25 |
|
---|
26 | scale: function (zoom) {
|
---|
27 | return Math.pow(2, zoom);
|
---|
28 | }
|
---|
29 | });
|
---|
30 |
|
---|
31 | var CoordToChunk = function(latlng) {
|
---|
32 | var x = Math.floor(((latlng.lng + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
|
---|
33 | var y = Math.floor(((latlng.lat + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
|
---|
34 | return L.latLng(y, x);
|
---|
35 | }
|
---|
36 |
|
---|
37 | var CoordToRegion = function(latlng) {
|
---|
38 | var x = Math.floor(((latlng.lng + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
|
---|
39 | var y = Math.floor(((latlng.lat + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
|
---|
40 | return L.latLng(y, x);
|
---|
41 | }
|
---|
42 |
|
---|
43 | var FormatCoord = function(latlng) {
|
---|
44 | return Math.abs(latlng.lat)+ (latlng.lat>=0 ? " N" : " S") + " / " + Math.abs(latlng.lng) + (latlng.lng>=0 ? " E" : " W");
|
---|
45 | }
|
---|
46 |
|
---|
47 | var FormatRegionFileName = function(latlng) {
|
---|
48 | return "r." + latlng.lng + "." + latlng.lat + ".7rg";
|
---|
49 | }
|
---|
50 |
|
---|
51 | var map = L.map('map', {
|
---|
52 | zoomControl: true,
|
---|
53 | attributionControl: false,
|
---|
54 | crs: SDTD_CRS
|
---|
55 | }).setView([0, 0], 0);
|
---|
56 |
|
---|
57 | var tileLayer = L.tileLayer('../../map/{z}/{x}/{y}.png?t={time}', {
|
---|
58 | maxZoom: MAXZOOM+1,
|
---|
59 | minZoom: 0,
|
---|
60 | maxNativeZoom: MAXZOOM,
|
---|
61 | tileSize: TILESIZE,
|
---|
62 | continuousWorld: true,
|
---|
63 | tms: true,
|
---|
64 | unloadInvisibleTiles: true,
|
---|
65 | time: function() { return new Date().getTime(); }
|
---|
66 | });
|
---|
67 |
|
---|
68 | var regionLayer = L.tileLayer.canvas({
|
---|
69 | maxZoom: MAXZOOM+1,
|
---|
70 | minZoom: 0,
|
---|
71 | maxNativeZoom: MAXZOOM+1,
|
---|
72 | tileSize: TILESIZE,
|
---|
73 | continuousWorld: true
|
---|
74 | });
|
---|
75 |
|
---|
76 | regionLayer.drawTile = function(canvas, tilePoint, zoom) {
|
---|
77 | var blockWorldSize = TILESIZE * Math.pow(2, MAXZOOM-zoom);
|
---|
78 | var tileLeft = tilePoint.x * blockWorldSize;
|
---|
79 | var tileBottom = (-1-tilePoint.y) * blockWorldSize;
|
---|
80 | var blockPos = L.latLng(tileBottom, tileLeft);
|
---|
81 |
|
---|
82 | var ctx = canvas.getContext('2d');
|
---|
83 |
|
---|
84 | ctx.strokeStyle = "lightblue";
|
---|
85 | ctx.fillStyle = "lightblue";
|
---|
86 | ctx.lineWidth = 1;
|
---|
87 | ctx.font="14px Arial";
|
---|
88 |
|
---|
89 | var lineCount = blockWorldSize / REGIONSIZE;
|
---|
90 | if (lineCount >= 1) {
|
---|
91 | var pos = 0;
|
---|
92 | while (pos < TILESIZE) {
|
---|
93 | // Vertical
|
---|
94 | ctx.beginPath();
|
---|
95 | ctx.moveTo(pos, 0);
|
---|
96 | ctx.lineTo(pos, TILESIZE);
|
---|
97 | ctx.stroke();
|
---|
98 |
|
---|
99 | // Horizontal
|
---|
100 | ctx.beginPath();
|
---|
101 | ctx.moveTo(0, pos);
|
---|
102 | ctx.lineTo(TILESIZE, pos);
|
---|
103 | ctx.stroke();
|
---|
104 |
|
---|
105 | pos += TILESIZE / lineCount;
|
---|
106 | }
|
---|
107 | ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
|
---|
108 | } else {
|
---|
109 | if ((tileLeft % REGIONSIZE) == 0) {
|
---|
110 | // Vertical
|
---|
111 | ctx.beginPath();
|
---|
112 | ctx.moveTo(0, 0);
|
---|
113 | ctx.lineTo(0, TILESIZE);
|
---|
114 | ctx.stroke();
|
---|
115 | }
|
---|
116 | if ((tileBottom % REGIONSIZE) == 0) {
|
---|
117 | // Horizontal
|
---|
118 | ctx.beginPath();
|
---|
119 | ctx.moveTo(0, TILESIZE);
|
---|
120 | ctx.lineTo(TILESIZE, TILESIZE);
|
---|
121 | ctx.stroke();
|
---|
122 | }
|
---|
123 | if ((tileLeft % REGIONSIZE) == 0 && (tileBottom % REGIONSIZE) == 0) {
|
---|
124 | ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | var pos = tileLeft;
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | var playersOnlineMarkerGroup = L.layerGroup();
|
---|
133 | var playersOfflineMarkerGroup = L.layerGroup();
|
---|
134 |
|
---|
135 | var baseLayers = {
|
---|
136 | //"Map": tileLayer
|
---|
137 | };
|
---|
138 |
|
---|
139 | var overlays = {
|
---|
140 | "Region files": regionLayer,
|
---|
141 | "Players (online) (<span id='mapControlOnlineCount'>0</span>)" : playersOnlineMarkerGroup,
|
---|
142 | "Players (offline) (<span id='mapControlOfflineCount'>0</span>)" : playersOfflineMarkerGroup
|
---|
143 | };
|
---|
144 |
|
---|
145 | tileLayer.addTo(map);
|
---|
146 | L.control.layers(baseLayers, overlays, {
|
---|
147 | collapsed: false
|
---|
148 | }).addTo(map);
|
---|
149 |
|
---|
150 | map.on('mousemove', function(e) {
|
---|
151 | L.DomUtil.get('pos').textContent = FormatCoord(e.latlng);
|
---|
152 | });
|
---|
153 |
|
---|
154 | var playersMappingList = {};
|
---|
155 |
|
---|
156 | var showInv = function(steamid) {
|
---|
157 | $.getJSON( "../api/getplayerinventory", { steamid: steamid })
|
---|
158 | .done(function(data) {
|
---|
159 | $("#invPlayerName").text(playersMappingList[steamid].name);
|
---|
160 | for (var y = 0; y < BAG_ROWS; y++) {
|
---|
161 | for (var x = 0; x < BAG_COLS; x++) {
|
---|
162 | if (data.bag[y*BAG_COLS+x].count > 0) {
|
---|
163 | $("#bagField"+x+"_"+y).attr("style", "background-image: url(itemimages/" + data.bag[y*BAG_COLS+x].name + ".png);");
|
---|
164 | $("#bagFieldText"+x+"_"+y).text(data.bag[y*BAG_COLS+x].count);
|
---|
165 | } else {
|
---|
166 | $("#bagField"+x+"_"+y).attr("style", "background-image: none;");
|
---|
167 | $("#bagFieldText"+x+"_"+y).text("");
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | for (var x = 0; x < BELT_COLS; x++) {
|
---|
173 | if (data.belt[x].count > 0) {
|
---|
174 | $("#beltField"+x).attr("style", "background-image: url(itemimages/" + data.belt[x].name + ".png);");
|
---|
175 | $("#beltFieldText"+x).text(data.belt[x].count);
|
---|
176 | } else {
|
---|
177 | $("#beltField"+x).attr("style", "background-image: none;");
|
---|
178 | $("#beltFieldText"+x).text("");
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | $( "#dialog-message" ).dialog({
|
---|
183 | modal: true,
|
---|
184 | width: BAG_COLS*INV_ITEM_WIDTH + 60,
|
---|
185 | buttons: {
|
---|
186 | Ok: function() {
|
---|
187 | $( this ).dialog( "close" );
|
---|
188 | }
|
---|
189 | }
|
---|
190 | });
|
---|
191 | })
|
---|
192 | .fail(function() {
|
---|
193 | console.log("Error fetching player inventory");
|
---|
194 | })
|
---|
195 | .always(function() {
|
---|
196 | });
|
---|
197 | };
|
---|
198 |
|
---|
199 | for (var y = 0; y < BAG_ROWS; y++) {
|
---|
200 | $("#bagTable").append("<tr id=\"bagRow"+y+"\"></tr>");
|
---|
201 | for (var x = 0; x < BAG_COLS; x++) {
|
---|
202 | $("#bagRow"+y).append(
|
---|
203 | "<td class=\"invField\" id=\"bagField"+x+"_"+y+"\">" +
|
---|
204 | "<span class=\"invFieldText\" id=\"bagFieldText"+x+"_"+y+"\"></span>" +
|
---|
205 | "</td>");
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | $("#beltTable").append("<tr id=\"beltRow0\"></tr>");
|
---|
210 | for (var x = 0; x < BELT_COLS; x++) {
|
---|
211 | $("#beltRow0").append(
|
---|
212 | "<td class=\"invField\" id=\"beltField"+x+"\">" +
|
---|
213 | "<span class=\"invFieldText\" id=\"beltFieldText"+x+"\"></span>" +
|
---|
214 | "</td>");
|
---|
215 | }
|
---|
216 |
|
---|
217 | var setPlayerMarkers = function(data) {
|
---|
218 | var online = 0;
|
---|
219 | var offline = 0;
|
---|
220 | $.each( data, function( key, val ) {
|
---|
221 | var marker;
|
---|
222 | if (playersMappingList.hasOwnProperty(val.steamid)) {
|
---|
223 | marker = playersMappingList[val.steamid].currentPosMarker;
|
---|
224 | marker.setLatLng([val.position.z, val.position.x]);
|
---|
225 | } else {
|
---|
226 | marker = L.marker([val.position.z, val.position.x]).bindPopup(
|
---|
227 | "Player: " + val.name + "<br/>" +
|
---|
228 | "<a onClick='showInv(\""+val.steamid+"\")'>Show inventory</a>"
|
---|
229 | );
|
---|
230 | playersMappingList[val.steamid] = { online: !val.online };
|
---|
231 | }
|
---|
232 | if (playersMappingList[val.steamid].online != val.online) {
|
---|
233 | if (val.online) {
|
---|
234 | marker.setOpacity(1.0);
|
---|
235 | playersOfflineMarkerGroup.removeLayer(marker);
|
---|
236 | playersOnlineMarkerGroup.addLayer(marker);
|
---|
237 | } else {
|
---|
238 | marker.setOpacity(0.5);
|
---|
239 | playersOnlineMarkerGroup.removeLayer(marker);
|
---|
240 | playersOfflineMarkerGroup.addLayer(marker);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | val.currentPosMarker = marker;
|
---|
244 | playersMappingList[val.steamid] = val;
|
---|
245 |
|
---|
246 | if (val.online)
|
---|
247 | online++;
|
---|
248 | else
|
---|
249 | offline++;
|
---|
250 | });
|
---|
251 | $( "#mapControlOnlineCount" ).text( online );
|
---|
252 | $( "#mapControlOfflineCount" ).text( offline );
|
---|
253 | }
|
---|
254 |
|
---|
255 | var updatePlayerEvent = function() {
|
---|
256 | $.getJSON( "../api/getplayerslocation")
|
---|
257 | .done(setPlayerMarkers)
|
---|
258 | .fail(function() {
|
---|
259 | console.log("Error fetching players list");
|
---|
260 | })
|
---|
261 | .always(function() {
|
---|
262 | window.setTimeout(updatePlayerEvent, 2000);
|
---|
263 | });
|
---|
264 | }
|
---|
265 |
|
---|
266 | window.setTimeout(updatePlayerEvent, 500);
|
---|
267 |
|
---|