diff options
Diffstat (limited to 'tools/gendata.js')
-rw-r--r-- | tools/gendata.js | 136 |
1 files changed, 126 insertions, 10 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) |