-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
add.ts
125 lines (109 loc) · 2.85 KB
/
add.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Settings } from "./settings";
type Path = {
path: FootpathElement;
x: number;
y: number;
};
type Paths = {
unsloped: Path[];
sloped: Path[];
queues: Path[];
};
export function Add(settings: Settings): Paths {
const paths: Paths = {
unsloped: [],
sloped: [],
queues: [],
};
// Iterate every tile in the map
for (let y = 0; y < map.size.y; y++) {
for (let x = 0; x < map.size.x; x++) {
const { elements } = map.getTile(x, y);
const surface = elements.filter(
(element) => element.type === "surface",
)[0] as SurfaceElement;
const footpaths = elements.filter(
(element) => element.type === "footpath",
) as FootpathElement[];
footpaths.forEach((path: FootpathElement) => {
if (canBuildAdditionOnPath(surface, path)) {
if (path.isQueue) {
paths.queues.push({ path, x, y });
} else if (path?.slopeDirection === null) {
paths.unsloped.push({ path, x, y });
} else {
paths.sloped.push({ path, x, y });
}
}
});
}
}
// Build benches and bins on unsloped paths
paths.unsloped.forEach(({ path, x, y }) => {
const { bench, bin } = settings;
const addition = findAddition(bench, bin, x, y);
ensureHasAddition(x, y, path.baseZ, addition);
});
// Build bins on sloped paths
paths.sloped.forEach(({ path, x, y }) => {
const { buildBinsOnAllSlopedPaths } = settings;
const evenTile = x % 2 === y % 2;
const buildOnSlopedPath = buildBinsOnAllSlopedPaths || evenTile;
if (buildOnSlopedPath) {
ensureHasAddition(x, y, path.baseZ, settings.bin);
}
});
// Build queue tvs on queue lines
paths.queues.forEach(({ path, x, y }) => {
ensureHasAddition(x, y, path.baseZ, settings.queuetv);
});
return paths;
}
/**
* Build the footpath addition on a footpath.
*/
function ensureHasAddition(
x: number,
y: number,
z: number,
addition: number,
): void {
context.executeAction(
"footpathadditionplace",
{
// x/y coords need to be multiples of 32
x: x * 32,
y: y * 32,
z,
object: addition,
},
({ errorTitle, errorMessage }) => {
if (errorMessage) throw new Error(`${errorTitle}: ${errorMessage}`);
},
);
}
export function findAddition(
bench: number,
bin: number,
x: number,
y: number,
): number {
if (x % 2 === y % 2) {
return bench;
} else {
return bin;
}
}
function canBuildAdditionOnPath(
surface: SurfaceElement,
path: FootpathElement,
) {
if (!surface || !path) return false;
if (path.addition !== null) return false;
if (surface.hasOwnership) return true;
// Only allowed to build underground or elevated on land with construction rights
if (surface.hasConstructionRights && surface.baseHeight !== path.baseHeight) {
return true;
}
return false;
}