Skip to content

Instantly share code, notes, and snippets.

@z81
Created November 30, 2017 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z81/32ff540d25d8503933c25176548f20d2 to your computer and use it in GitHub Desktop.
Save z81/32ff540d25d8503933c25176548f20d2 to your computer and use it in GitHub Desktop.
best ne govnokod mapgen.js
const textures = {
none: "░░",
road: "██",
user: "*/"
};
const mapSize = 30;
const locations = [
{
name: "Глеdvбоиния"
},
{
name: "Артурия"
},
{
name: "Мск"
},
{
name: "grrgegerg"
}
];
const gameObjects = [
{
name: "oleg"
},
{
name: "oleg2"
},
{
name: "oleg 3"
}
];
const generateArray = size => Array.from(new Array(size));
let map = generateArray(mapSize).map(() =>
generateArray(mapSize).map(() => textures.none)
);
const locationsSorted = locations.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
const drawLine = (x1, y1, x2, y2, texture) => {
const xDirection = x1 < x2 ? 1 : -1;
const yDirection = y1 < y2 ? 1 : -1;
while (x1 !== x2 || y1 != y2) {
map[y1][x1] = texture;
if (x1 !== x2) x1 += xDirection;
if (y1 !== y2) y1 += yDirection;
}
};
const getLocationSeed = name => {
return (
Math.round(name.split("").reduce((a, b) => a + b.charCodeAt(0), 0)) % 100
);
};
const drawRoad = () => {
locationsSorted.forEach(({ name }, i) => {
let x1 = 0;
let y1 = 0;
let x2 = 0;
let y2 = 0;
const locationSeed = getLocationSeed(name);
const positionOffset = Math.round(locationSeed / 100 * mapSize / 3);
if (i === 0) {
x1 = Math.round(mapSize / 2) - positionOffset;
x2 = Math.round(mapSize / 2);
y2 = Math.round(mapSize / 2) + 1;
} else if (i === 1) {
y1 = Math.round(mapSize / 2) - positionOffset;
y2 = Math.round(mapSize / 2);
x2 = Math.round(mapSize / 2) + 1;
} else if (i === 2) {
x1 = Math.round(mapSize / 2);
x2 = mapSize;
y1 = Math.round(mapSize / 2);
y2 = Math.round(mapSize / 2) - positionOffset;
} else if (i === 3) {
x1 = Math.round(mapSize / 2);
x2 = Math.round(mapSize / 2) + positionOffset;
y1 = Math.round(mapSize / 2);
y2 = mapSize;
}
drawLine(x1, y1, x2, y2, textures.road);
});
};
const drawGameObjects = () => {
gameObjects.forEach(({ name }, i) => {
const seed = getLocationSeed(name);
let x = Math.round(mapSize / 2);
let y = Math.round(mapSize / 2);
x += Math.round((x - seed % (42 / i)) / 100 * mapSize);
y += Math.round((y - seed % 42) / 100 * mapSize);
if (name === "oleg") {
map[x][y] = "::";
map[x][y + 1] = "/░";
} else if (name === "oleg2") {
map[x][y] = "./";
} else {
map[x][y] = textures.user;
}
});
};
drawRoad();
drawGameObjects();
{
const name = locationsSorted[0].name;
console.log(" ".repeat((mapSize - name.length) / 2 + 1), name);
}
map = map.map(row => row.join("")).join("\n");
console.log(map);
if (locationsSorted.length >= 3) {
const name = locationsSorted[3].name;
console.log(" ".repeat(mapSize - name.length + 1), name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment