Skip to content

Commit

Permalink
Merge pull request #4 from GitTerraGame/deterministic-map
Browse files Browse the repository at this point in the history
Deterministic tiling algorythm based on file clustering
  • Loading branch information
sergeychernyshev authored Mar 27, 2024
2 parents c89c9d7 + 4c30636 commit 14b42e1
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 45 deletions.
9 changes: 6 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ runs:
shell: "bash"

- name: Extracting the inner knowledge hidden inside the scrolls
run: time wizard/scc scrolls/ --format json --output gitterra.json
run: time wizard/scc scrolls/ --by-file --format json --output gitterra.json
shell: "bash"

- name: Transporting the builders to the construction site
- name: Transporting builders to the construction site
uses: actions/checkout@v4
with:
repository: "GitTerraGame/Play-GitTerra-Action"
path: "builders"

- name: Equipping builders with the tools
run: (cd builders && npm install)
shell: "bash"

- name: Building the city of GitTerra
run: node builders/src/generateMap.js scrolls/.gitterra.config.js
shell: "bash"
Expand All @@ -43,4 +47,3 @@ runs:
name: gitterra
path: |
index.html
gitterra.json
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "MIT",
"type": "module",
"dependencies": {
"kmeansjs": "^0.0.3",
"open-cli": "^8.0.0"
}
}
22 changes: 22 additions & 0 deletions src/clusterize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import kmeans from "kmeansjs";

async function clusterize(files, number_of_blocks) {
const vector = files.map((file) => [file, file.Bytes, file.Lines]);

const clusters = await new Promise((resolve, reject) => {
const k =
vector.length > number_of_blocks ? number_of_blocks : vector.length;

kmeans(vector, k, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});

return await clusters;
}

export default clusterize;
12 changes: 11 additions & 1 deletion src/generateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { pathToFileURL } from "url";

import { generateMapHTML } from "./map.js";
import { defaultGameConfig } from "./gameConfig.js";
import clusterize from "./clusterize.js";

import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -48,6 +49,9 @@ const repoStats = {
},
};

/*
* Calculating global statistics to determine the number of city blocks
*/
const repo = JSON.parse(fs.readFileSync(input, "utf8"));
repo.forEach((elem) => {
repoStats.total.bytes += elem.Bytes;
Expand Down Expand Up @@ -75,5 +79,11 @@ const number_of_blocks = Math.round(
gameConfig.minTiles
);

const mapHTML = generateMapHTML(gameConfig, number_of_blocks);
/**
* Deterministicly group files into clusters for each city block
*/
const files = repo.map((elem) => elem.Files).flat();
const clusters = await clusterize(files, number_of_blocks);

const mapHTML = generateMapHTML(gameConfig, clusters);
fs.writeFileSync(output, mapHTML);
27 changes: 23 additions & 4 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getMapTileCoordinates(n) {
// boolean representing the side of the diamond, e.g. left (false) or right (true)
const direction =
Math.ceil((n - Math.pow(Math.floor(Math.sqrt(n)), 2)) / 2) -
Math.floor((n - Math.pow(Math.floor(Math.sqrt(n)), 2)) / 2) ===
Math.floor((n - Math.pow(Math.floor(Math.sqrt(n)), 2)) / 2) ===
0;

if (direction) {
Expand All @@ -35,7 +35,26 @@ function getMapTileCoordinates(n) {
}
}
}
export const generateMapHTML = function (gameConfig, total) {

/**
* Returns a tile number from a tileset based on the cluster of files
* It currently uses total number of lines of code as a seed for the tile number
*
* @param {*} cluster an array of file objects tile represents
* @param {*} numberOfTileVariations the number of tile variations in the tileset
*
* @returns {int} a 1-based number of the tile
*/
function getTileNumber(cluster, numberOfTileVariations) {
const totalLinesInCluster = cluster.reduce(
(acc, [file]) => acc + file.Lines,
0
);

return (totalLinesInCluster % numberOfTileVariations) + 1;
}

export const generateMapHTML = function (gameConfig, clusters) {
// scale the image if total is too high
const tileScale = 1;

Expand All @@ -57,7 +76,7 @@ export const generateMapHTML = function (gameConfig, total) {

const tiles = [];

for (let i = total; i >= 1; i--) {
for (let i = clusters.length; i >= 1; i--) {
const blockCoordinates = getMapTileCoordinates(i);

const isoX =
Expand All @@ -76,7 +95,7 @@ export const generateMapHTML = function (gameConfig, total) {
highestIsoY = isoY;
}

const tileNumber = Math.floor(Math.random() * numberOfTileVariations) + 1;
const tileNumber = getTileNumber(clusters[i - 1], numberOfTileVariations);

tiles.push({ tileNumber, isoX, isoY });
}
Expand Down
4 changes: 2 additions & 2 deletions templates/gitterra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ GitTerra:
# - find . -not -path './.git' -not -path './.git/*'
- mkdir -p /tmp/gitterra/scc
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/gitterra/GitTerra.git /tmp/gitterra/gitterra
- (cd /tmp/gitterra/gitterra && npm install)
- wget https://github.com/boyter/scc/releases/download/v3.2.0/scc_Linux_x86_64.tar.gz -O /tmp/gitterra/scc/scc.gz
- tar -C /tmp/gitterra/scc -xvf /tmp/gitterra/scc/scc.gz
- chmod +x /tmp/gitterra/scc/scc
- time /tmp/gitterra/scc/scc . --format json --output gitterra.json
- time /tmp/gitterra/scc/scc . --by-file --format json --output gitterra.json
- node /tmp/gitterra/gitterra/src/generateMap.js .gitterra.config.js
artifacts:
paths:
- index.html
- gitterra.json
Loading

0 comments on commit 14b42e1

Please sign in to comment.