Skip to content

Commit 792cfbb

Browse files
committed
refactor: update map utils for better accuracy with new framework
God. I. Hate. This. There's no guarantee that the values entered will work for everyone. There's no guarantee they will be accurate. And I cannot tell you for the life of me where the values have come from.
1 parent 8d7c2d6 commit 792cfbb

File tree

4 files changed

+33
-54
lines changed

4 files changed

+33
-54
lines changed

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

js/src/map.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ var _MAP_tiles = {
2626
};
2727

2828
function mapInit(elementID) {
29-
3029
_MAP_map = L.map(elementID, {
3130
maxZoom: 3,
3231
minZoom: 0,
3332
crs: L.CRS.Simple,
3433
layers: [_MAP_tiles["Normal"]]
3534
}).setView([0, 0], 0);
3635

36+
var mapBounds = [[0,256*2], [-256*3, 0]];
37+
38+
//_MAP_map.fitBounds(mapBounds);
39+
_MAP_map.setMaxBounds(mapBounds);
40+
//L.rectangle(mapBounds).addTo(_MAP_map);
41+
3742
L.control.layers(_MAP_tiles).addTo(_MAP_map);
3843

3944
_MAP_map.on("baselayerchange", function (e) {
@@ -55,7 +60,9 @@ function createMarker(animated, draggable, objectRef, title) {
5560
name = "@Locator";
5661
}
5762
objectRef.position = stringCoordToFloat(objectRef.position);
63+
console.log(objectRef.position);
5864
var coord = convertToMapLeaflet(objectRef.position.x, objectRef.position.y);
65+
console.log(coord);
5966
var markerType = objectRef.type;
6067

6168
//console.log(JSON.stringify(locationType));

js/src/socket.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ function onMessage(e) {
102102
if (data.type == "playerData") {
103103
//console.log("updating players(" + typeof(data.payload) + "): " + JSON.stringify(data.payload));
104104
var sortedPlayers = data.payload.sort(sorter);
105-
//doPlayerUpdate(sortedPlayers);
105+
doPlayerUpdate(sortedPlayers);
106+
webSocket.close();
106107
}
107108
}
108109

@@ -340,26 +341,26 @@ function doPlayerUpdate(players) {
340341
if (plr.icon) {
341342
var t = MarkerTypes[plr.icon];
342343
//console.log("Got icon of :" + plr.icon);
344+
/*
343345
_MAP_markerStore[localCache[plr.identifer].marker].setIcon({
344346
url: _MAP_iconURL + t.icon,
345347
size: t.size,
346348
origin: t.origin,
347349
anchor: t.anchor,
348350
scaledSize: t.scaledSize
349351
});
352+
*/
350353
}
351354

352355
// Update the player's location on the map :)
353-
_MAP_markerStore[localCache[plr.identifer].marker].setPosition(convertToMapGMAP(plr.pos.x, plr.pos.y));
356+
//_MAP_markerStore[localCache[plr.identifer].marker].setPosition(convertToMapLeaflet(plr.pos.x, plr.pos.y));
354357

355358
//update popup with the information we have been sent
356359
var html = getPlayerInfoHtml(plr);
357360

358361
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
359-
var infoBox = new google.maps.InfoWindow({
360-
content: infoContent
361-
});
362-
_MAP_markerStore[localCache[plr.identifer].marker].popup.setContent(infoContent);
362+
363+
_MAP_markerStore[localCache[plr.identifer].marker].bindPopup(infoContent);
363364

364365
} else {
365366

@@ -369,10 +370,8 @@ function doPlayerUpdate(players) {
369370
var html = getPlayerInfoHtml(plr);
370371

371372
var infoContent = '<div class="info-window"><div class="info-header-box"><div class="info-icon"></div><div class="info-header">' + plr.name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
372-
var infoBox = new google.maps.InfoWindow({
373-
content: infoContent
374-
});
375-
_MAP_markerStore[m].popup.setContent(infoContent);
373+
374+
_MAP_markerStore[m].bindPopup(infoContent);
376375

377376
}
378377

js/src/utils.js

+14-43
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
function isNumeric(n) {
2020
return !isNaN(parseFloat(n)) && isFinite(n)
2121
}
22-
var game_min_x = -4000;
23-
var game_max_x = 6000;
2422

23+
//These settings seem to work for tilesize 256 (the default). The resources I could find don't help here.. So, the values below are botched. And _seem_ to work alright.
24+
25+
// Gta 5's min max bounds
26+
var game_min_x = -4000.00;
27+
var game_max_x = 6000.00;
2528
var game_min_y = 8000;
2629
var game_max_y = -4000;
2730

31+
// Map's min max bounds based on Leaflet's shitty system
2832
var map_min_x = 0;
29-
var map_max_x = 2048*2 + 1024;
30-
31-
var map_min_y = 0;
32-
var map_max_y = 2048*3 - 256;
33+
var map_max_x = 585; // Don't fucking ask.
34+
var map_min_y = 17; // IDFK why this needs to be 17 but, it seems to be accurate
35+
var map_max_y = 256*3 - 45; // WHYYYY???
3336

3437
function normalize(value, min, max){
3538
return Math.abs((value - min) / (max - min));
@@ -41,46 +44,14 @@ function convertToMap(x, y) {
4144

4245
var yPercent = normalize(y, game_min_y, game_max_y);
4346
var destY = yPercent * (Math.abs(map_max_y - map_min_y)) + map_min_y;
44-
45-
return {lat: destX, lng: destY};
47+
48+
console.log(destX, destY);
49+
return {x: destX, y: destY};
4650
}
4751

4852
function convertToMapLeaflet(x, y){
49-
var xPercent = normalize(x, game_min_x, game_max_x);
50-
var destX = xPercent * (Math.abs(map_max_x - map_min_x)) + map_min_x;
51-
52-
var yPercent = normalize(y, game_min_y, game_max_y);
53-
var destY = yPercent * (Math.abs(map_max_y - map_min_y)) + map_min_y;
54-
55-
return _MAP_map.unproject([destX, destY], _MAP_map.getMaxZoom());
56-
}
57-
58-
function convertToGame(lat, lng) {
59-
var rX = game_1_x + (lng - map_1_lng) * (game_1_x - game_2_x) / (map_1_lng - map_2_lng);
60-
var rY = game_1_y + (lat - map_1_lat) * (game_1_y - game_2_y) / (map_1_lat - map_2_lat);
61-
return result = {
62-
x: rX,
63-
y: rY
64-
};
65-
}
66-
67-
function convertToGameCoord(lat, lng) {
68-
var rX = game_1_x + (lng - map_1_lng) * (game_1_x - game_2_x) / (map_1_lng - map_2_lng);
69-
var rY = game_1_y + (lat - map_1_lat) * (game_1_y - game_2_y) / (map_1_lat - map_2_lat);
70-
return result = {
71-
x: rX,
72-
y: rY,
73-
z: 0
74-
};
75-
}
76-
77-
function convertToMap_OLD(x, y) {
78-
var rLng = map_1_lng + (x - game_1_x) * (map_1_lng - map_2_lng) / (game_1_x - game_2_x);
79-
var rLat = map_1_lat + (y - game_1_y) * (map_1_lat - map_2_lat) / (game_1_y - game_2_y);
80-
return result = {
81-
lat: rLat,
82-
lng: rLng
83-
};
53+
var t = convertToMap(x, y);
54+
return {lat: -t.y, lng: t.x};// Leaflet switches lat and lng around. Y gets lower the further down you go so.. Need to invert it
8455
}
8556

8657
function stringCoordToFloat(coord) {

0 commit comments

Comments
 (0)