diff options
author | Tor Andersson <tor@ccxvii.net> | 2022-07-29 13:03:47 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2022-11-17 13:11:26 +0100 |
commit | 4e92007e7a7e7e2c5a432ccb38f235abe67236b5 (patch) | |
tree | 16ce3224a719fc26c909f1f83e86c2c7d0ea415c /tools | |
parent | 352d41a05f51c4f1a1849519bb3c28e13a7be29c (diff) | |
download | rommel-in-the-desert-4e92007e7a7e7e2c5a432ccb38f235abe67236b5.tar.gz |
Data arrays.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gendata.js | 136 | ||||
-rw-r--r-- | tools/genunits.js | 114 |
2 files changed, 191 insertions, 59 deletions
diff --git a/tools/gendata.js b/tools/gendata.js index cf77791..93d7502 100644 --- a/tools/gendata.js +++ b/tools/gendata.js @@ -1,5 +1,7 @@ // https://www.redblobgames.com/grids/hexagons/ +const fs = require('fs') + // Clear const CLEAR = 2; @@ -466,10 +468,58 @@ function def_block(nationality, type, appearance, steps, elite, label, name) { let speed = speed_from_type[type]; if (name in unit_map) throw new Error("duplicate block name:", name); - unit_map[name] = units.length; + unit_map[name] = 1 units.push({nationality, type, class: klass, speed, appearance, steps, elite, label, name}); } +function sort_nation(u) { + if (u.nationality === 'italian') + return 0 + if (u.nationality === 'german') + return 1 + return 2 +} + +function sort_subnation(u) { + function isn(x) { + let [a, b] = u.name.split('/'); + if (b) + b = b.split('+'); + return a.endsWith(x) || (b && b[0] === x) + } + switch (true) { + case isn("IN"): return 3 + case isn("SA"): return 4 + case isn("AU"): return 5 + case isn("NZ"): return 6 + case isn("FF"): return 7 + case isn("Pol"): return 8 + } + return 2 +} + +function sort_name(u) { + if (u.nationality === 'italian') + return 0 + if (u.nationality === 'german') + return 1 + return sort_subnation(u) +} + +function cmp_block(a, b) { + let x = sort_name(a) * 10000 + a.class * 1000 + (1-a.elite) * 10 + (4-a.speed) * 100 + (4-a.steps) + let y = sort_name(b) * 10000 + b.class * 1000 + (1-b.elite) * 10 + (4-b.speed) * 100 + (4-b.steps) + // let x = sort_nation(a) * 10000 + a.class * 1000 + (1-a.elite) * 10 + (4-a.speed) * 100 + sort_subnation(a) + // let y = sort_nation(b) * 10000 + b.class * 1000 + (1-b.elite) * 10 + (4-b.speed) * 100 + sort_subnation(b) + if (x < y) return -1 + if (x > y) return 1 + if (parseInt(a.name) < parseInt(b.name)) return -1 + if (parseInt(a.name) > parseInt(b.name)) return 1 + if (a.name < b.name) return -1 + if (a.name > b.name) return 1 + return 0 +} + let S = "S"; let M = "M"; let T = "T"; @@ -587,14 +637,80 @@ def_block("allied", "armor", 18, 3, 1, 77, "Sher/A"); def_block("allied", "mot. a/t", 18, 4, 0, 78, "6#/B"); def_block("allied", "mot. inf.", 18, 4, 0, 79, "51H/154"); +units.sort(cmp_block) + let all_hexes = hex_exists.map((x,i) => x ? i : 0).filter(x => x > 0) -console.log("const all_hexes = " + JSON.stringify(all_hexes)); -console.log("const hex_exists = " + JSON.stringify(hex_exists)); -console.log("const hex_road = " + JSON.stringify(hex_road)); -console.log("const side_road = " + JSON.stringify(side_road)); -console.log("const side_limit = " + JSON.stringify(side_limit)); -console.log("const hex_name = " + JSON.stringify(hex_name)); -console.log("const regions = " + JSON.stringify(regions)); -console.log("const units = " + JSON.stringify(units)); -console.log("if (typeof module !== 'undefined') module.exports = { all_hexes, hex_exists, hex_name, hex_road, side_road, side_limit, regions, units }"); +function gen_array(name, fn) { + data += "const " + name + " = [" + units.map(fn).join(",") + "]\n" +} + +let data = "// DON'T EDIT - AUTOGENERATED\n" +data += "const all_hexes = " + JSON.stringify(all_hexes) + "\n" +data += "const hex_exists = " + JSON.stringify(hex_exists) + "\n" +data += "const hex_road = " + JSON.stringify(hex_road) + "\n" +data += "const side_road = " + JSON.stringify(side_road) + "\n" +data += "const side_limit = " + JSON.stringify(side_limit) + "\n" +data += "const hex_name = " + JSON.stringify(hex_name) + "\n" +data += "const regions = " + JSON.stringify(regions) + "\n" + +gen_array("unit_name", u => JSON.stringify(u.name)) +gen_array("unit_appearance", u => JSON.stringify(u.appearance)) +gen_array("unit_start_steps", u => u.steps) +gen_array("unit_elite", u => u.elite) +gen_array("unit_class", u => u.class) +gen_array("unit_speed", u => u.speed) + +data += "if (typeof module !== 'undefined')\n\tmodule.exports = {\n" +data += "\t\tall_hexes, hex_exists, hex_name, hex_road, side_road, side_limit, regions,\n" +data += "\t\tunit_name, unit_appearance, unit_start_steps, unit_elite, unit_class, unit_speed,\n" +data += "\t}\n" + +fs.writeFileSync("data.js", data) + +fs.writeFileSync("rawdata.js", "exports.units = " + JSON.stringify(units)) + +// Show ranges + +let test = "" +function gen_test_range(fn) { + let ranges = []; + let start = -1; + for (let p = 0; p < units.length; ++p) { + if (fn(units[p])) { + if (start < 0) + start = p; + } else { + if (start >= 0) { + ranges.push([start,p-1]) + start = -1; + } + } + } + if (start >= 0) + ranges.push([start,units.length-1]) + return ranges.map(([a,b])=>`(u >= ${a} && u <= ${b})`).join(" || ") +} + +function gen_test(name, fn) { + test += "function " + name + "(u) { return " + gen_test_range(fn) + " }\n" +} + +gen_test("is_armor_unit", u => u.class === ARMOR) +gen_test("is_artillery_unit", u => u.class === ARTILLERY) +gen_test("is_infantry_unit", u => u.class === INFANTRY) +gen_test("is_antitank_unit", u => u.class === ANTITANK) + +gen_test("is_recon_unit", u => u.speed === 4) +gen_test("is_mechanized_unit", u => u.speed === 3) +gen_test("is_motorized_unit", u => u.speed === 2) +gen_test("is_leg_unit", u => u.speed === 1) + +gen_test("is_elite_unit", u => u.elite) + +gen_test("is_italian_unit", u => u.nationality === 'italian') +gen_test("is_german_unit", u => u.nationality === 'german') +gen_test("is_axis_unit", u => u.nationality !== 'allied') +gen_test("is_allied_unit", u => u.nationality === 'allied') + +console.log(test) diff --git a/tools/genunits.js b/tools/genunits.js index 4e0f30e..820de56 100644 --- a/tools/genunits.js +++ b/tools/genunits.js @@ -1,5 +1,9 @@ -const { units } = require("../data.js"); -const print = console.log; +const fs = require('fs') +const { units } = require("../rawdata.js"); +var out = [] +function print(s) { out.push(s) } + +function print_defs() { print(`<svg xmlns="http://www.w3.org/2000/svg" width="510" height="510" version="1.2" viewBox="0 0 510 510"> <defs> @@ -62,9 +66,10 @@ print(`<svg xmlns="http://www.w3.org/2000/svg" width="510" height="510" version= </symbol> </defs> -<g font-family="Arial,Helvetica,sans-serif" font-weight="bold">`); +<g font-family="Arial,Helvetica,sans-serif" font-weight="bold">`) +} -SYMBOLS = { +const SYMBOLS = { "armor": ` <path fill="#ccc" stroke="#111" stroke-linejoin="round" stroke-width="1.5" d="M38.3 38.3H12.8V12.9h25.5zm0 0"/> @@ -175,53 +180,64 @@ const BLACK = { allied: "#5c3a1e" } -let x = 0, y = 0; -for (let u of units) { - let type = TYPEMAP[u.type]; - let black = BLACK[u.nationality]; - let fill = COLORMAP[u.nationality]; - - let [a, b] = u.name.split('/'); - if (b) - b = b.split('+'); - - for (let n in COLORMAP) - if (a.endsWith(n) || (b && b[0] === n)) - fill = COLORMAP[n]; - - print(`<g transform="translate(${x},${y})">`); - - if (type === 'armor' && u.elite && u.nationality === 'allied') - type = 'armor_elite'; - - let symbol = SYMBOLS[type]; - print(symbol.replace(/#ccc/g, fill).replace(/#111/g, black).trim()); - - if (u.elite) - print(`<use href="#cv${u.steps}e" fill="${black}"/>`); - else - print(`<use href="#cv${u.steps}" fill="${black}"/>`); - - print(`<g fill="${black}">`); - print(`<text text-anchor="start" font-size="7" x="2" y="8">${u.appearance}</text>`); - print(`<text text-anchor="start" font-size="7" x="2" y="48">${a}</text>`); +function print_units(show_text) { + out = [] + print_defs() + let x = 0, y = 0; + for (let u of units) { + let type = TYPEMAP[u.type]; + let black = BLACK[u.nationality]; + let fill = COLORMAP[u.nationality]; + + let [a, b] = u.name.split('/'); + if (b) + b = b.split('+'); + + for (let n in COLORMAP) + if (a.endsWith(n) || (b && b[0] === n)) + fill = COLORMAP[n]; + + print(`<g transform="translate(${x},${y})">`); + + if (type === 'armor' && u.elite && u.nationality === 'allied') + type = 'armor_elite'; + + let symbol = SYMBOLS[type]; + print(symbol.replace(/#ccc/g, fill).replace(/#111/g, black).trim()); + + if (u.elite) + print(`<use href="#cv${u.steps}e" fill="${black}"/>`); + else + print(`<use href="#cv${u.steps}" fill="${black}"/>`); + + if (show_text) { + print(`<g fill="${black}">`); + print(`<text text-anchor="start" font-size="7" x="2" y="8">${u.appearance}</text>`); + print(`<text text-anchor="start" font-size="7" x="2" y="48">${a}</text>`); + + if (b) { + if (b.length > 1) { + print(`<text text-anchor="end" font-size="7" x="49" y="43">${b[0]}</text>`); + print(`<text text-anchor="end" font-size="7" x="49" y="49">${b[1]}</text>`); + } else { + print(`<text text-anchor="end" font-size="7" x="48" y="48">${b[0]}</text>`); + } + } + + print(`</g>`); + } - if (b) { - if (b.length > 1) { - print(`<text text-anchor="end" font-size="7" x="49" y="43">${b[0]}</text>`); - print(`<text text-anchor="end" font-size="7" x="49" y="49">${b[1]}</text>`); - } else { - print(`<text text-anchor="end" font-size="7" x="48" y="48">${b[0]}</text>`); + print(`</g>`); + x += 51; + if (x >= 510) { + y += 51; + x = 0; } } - print(`</g>`); - print(`</g>`); - x += 51; - if (x >= 510) { - y += 51; - x = 0; - } + print(`</svg>`); + return out.join("\n") } -print(`</g>`); -print(`</svg>`); + +fs.writeFileSync("units.svg", print_units(true)) +fs.writeFileSync("units-simple.svg", print_units(false)) |