Skip to content

Commit

Permalink
support fill_ter
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Nov 27, 2024
1 parent e468b54 commit f7da026
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion _test/all.meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"buildNum":"cdda-experimental-2024-11-11-0430","sha":"bb558e50f9b73d5b992c0d80c961fc05821e745a4c5dc79e3296d1853931c3af"}
{"buildNum":"cdda-experimental-2024-11-27-0633","sha":"3be13d3a705a4d1c3f2afca7959f487d1de04777fe0f81d903d1c7282ca2be8c"}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ export type MapgenPlaceFurniture = {
};

export interface MapgenObject {
fill_ter?: string;
fill_ter?: MapgenValue;
rows?: string[];
terrain?: PlaceMappingAlternative<MapgenValue>;
place_terrain?: PlaceList<MapgenPlaceTerrain>;
Expand Down
33 changes: 26 additions & 7 deletions src/types/item/spawnLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ function getMapgenValueDistribution(val: raw.MapgenValue): Map<string, number> {
return new Map();
}

function toLoot(distribution: Map<string, number>): Loot {
// TODO: i'm not sure this is correct?
return new Map(
[...distribution.entries()].map(([id, prob]) => [
id,
{ prob, expected: prob },
])
);
}

let onStack = 0;
function lootForChunks(
data: CddaData,
Expand Down Expand Up @@ -677,19 +687,20 @@ export function getTerrainForMapgen(data: CddaData, mapgen: raw.Mapgen): Loot {
if (terrainForMapgenCache.has(mapgen))
return terrainForMapgenCache.get(mapgen)!;
const palette = parseTerrainPalette(data, mapgen.object);
const place_terrain = (mapgen.object.place_terrain ?? []).map(({ ter }) => {
const distribution = getMapgenValueDistribution(ter);
const loot = new Map<string, ItemChance>();
for (const [id, prob] of distribution.entries())
loot.set(id, { prob, expected: prob });
return loot;
});
const fill_ter = mapgen.object.fill_ter
? getMapgenValueDistribution(mapgen.object.fill_ter)
: new Map<string, number>();
const place_terrain = (mapgen.object.place_terrain ?? []).map(({ ter }) =>
toLoot(getMapgenValueDistribution(ter))
);
const additional_items = collection([...place_terrain]);
const countByPalette = new Map<string, number>();
let fillCount = 0;
for (const row of mapgen.object.rows ?? [])
for (const char of row)
if (palette.has(char))
countByPalette.set(char, (countByPalette.get(char) ?? 0) + 1);
else fillCount += 1;
const items: Loot[] = [];
for (const [sym, count] of countByPalette.entries()) {
const loot = palette.get(sym)!;
Expand All @@ -699,6 +710,14 @@ export function getTerrainForMapgen(data: CddaData, mapgen: raw.Mapgen): Loot {
}
items.push(multipliedLoot);
}
if (fillCount > 0) {
const loot = toLoot(fill_ter);
const multipliedLoot: Loot = new Map();
for (const [id, chance] of loot.entries()) {
multipliedLoot.set(id, repeatItemChance(chance, [fillCount, fillCount]));
}
items.push(multipliedLoot);
}
items.push(additional_items);
const loot = collection(items);
loot.delete("t_null");
Expand Down

0 comments on commit f7da026

Please sign in to comment.