summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-01-03 02:15:01 +0100
committerTor Andersson <tor@ccxvii.net>2023-02-18 13:02:38 +0100
commit7232fb3d5189814491220fc2c583b0651b7e40cb (patch)
tree11f6204d4dba9757f6efc70c5b242151d5a1fe01
parent650de5f413c9744131e328c62b2fc7161813ddf8 (diff)
downloadnevsky-7232fb3d5189814491220fc2c583b0651b7e40cb.tar.gz
Calculate distance maps.
-rw-r--r--rules.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/rules.js b/rules.js
index d3c36d2..4df9dc8 100644
--- a/rules.js
+++ b/rules.js
@@ -1437,6 +1437,41 @@ function count_unbesieged_friendly_lords(loc) {
return n
}
+// === MAP ===
+
+function calculate_distance(start, adjacent) {
+ let queue = []
+ queue.push([start, 0])
+
+ let distance = new Array(last_locale+1).fill(-1)
+ distance[start] = 0
+
+ while (queue.length > 0) {
+ let [ here, d ] = queue.shift()
+ for (let next of data.locales[here][adjacent]) {
+ if (distance[next] < 0) {
+ distance[next] = d+1
+ queue.push([next, d+1])
+ }
+ }
+ }
+
+ return distance
+}
+
+for (let loc = 0; loc <= last_locale; ++loc) {
+ data.locales[loc].distance = calculate_distance(loc, "adjacent")
+ data.locales[loc].distance_by_waterway = calculate_distance(loc, "adjacent_by_waterway")
+}
+
+function locale_distance(from, to) {
+ return data.locales[from].distance[to]
+}
+
+function locale_distance_by_waterway(from, to) {
+ return data.locales[from].distance_by_waterway[to]
+}
+
// === SETUP ===
function setup_lord_on_calendar(lord, turn) {