@zardoy/flying-squid 0.0.120 → 0.0.122

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,7 @@ const playerDat = __importStar(require("../playerDat"));
46
46
  const prismarine_world_1 = __importDefault(require("prismarine-world"));
47
47
  const prismarine_registry_1 = __importDefault(require("prismarine-registry"));
48
48
  const generations_1 = __importDefault(require("../generations"));
49
+ const template_1 = require("../worldGenerations/template");
49
50
  const vec3_1 = require("vec3");
50
51
  const utils_1 = require("../../utils");
51
52
  const levelDat_1 = require("../../levelDat");
@@ -142,7 +143,8 @@ const server = async function (serv, options) {
142
143
  ...generation.options,
143
144
  seed,
144
145
  version,
145
- getRenamedData: blockRenames_1.getRenamedData
146
+ getRenamedData: blockRenames_1.getRenamedData,
147
+ baseY: options.baseY
146
148
  };
147
149
  serv.emit('seed', generationOptions.seed);
148
150
  const patchWorld = (world) => {
@@ -150,7 +152,17 @@ const server = async function (serv, options) {
150
152
  world['height'] = generation.options.worldHeight;
151
153
  };
152
154
  const generationModule = generations_1.default[generation.name] ? generations_1.default[generation.name] : require(generation.name);
153
- const originalGenerator = generationModule(generationOptions);
155
+ const genOpts = generation.options;
156
+ const templateStr = typeof options.chunkTemplate === 'string' ? options.chunkTemplate
157
+ : typeof genOpts.chunkTemplate === 'string' ? genOpts.chunkTemplate
158
+ : undefined;
159
+ const templateBlockMap = options.blockMap ?? genOpts.blockMap;
160
+ const originalGenerator = templateStr !== undefined
161
+ ? (0, template_1.makeTemplateGenerator)(templateStr, generationOptions, templateBlockMap)
162
+ : generationModule(generationOptions);
163
+ if (templateStr !== undefined) {
164
+ serv.spawnPoint = (0, template_1.getTemplateSpawnPoint)(generationOptions);
165
+ }
154
166
  serv.overworldOriginalGenerator = originalGenerator;
155
167
  serv.overworld = new World((chunkX, chunkZ) => {
156
168
  return serv.overworldGeneratorOverride ? serv.overworldGeneratorOverride(chunkX, chunkZ, generationOptions) : originalGenerator(chunkX, chunkZ, generationOptions);
@@ -0,0 +1,77 @@
1
+ import { Vec3 } from 'vec3';
2
+ /**
3
+ * Default single-character → Minecraft block-name mapping used by the
4
+ * chunk-template generator. Users can override any entry (or add new ones)
5
+ * via `generation.options.blockMap`.
6
+ */
7
+ export declare const DEFAULT_BLOCK_MAP: Record<string, string>;
8
+ /**
9
+ * Parses a block-map override string into a `Record<string, string>`.
10
+ *
11
+ * Format: comma-separated `CHAR=block_name` pairs (whitespace around
12
+ * `=` and `,` is ignored).
13
+ *
14
+ * @example
15
+ * parseBlockMap("C=cobblestone, G=glass_pane, X=diamond_block")
16
+ * // → { C: 'cobblestone', G: 'glass_pane', X: 'diamond_block' }
17
+ */
18
+ export declare function parseBlockMap(s: string): Record<string, string>;
19
+ /**
20
+ * Expands a run-length-encoded row string.
21
+ * "3B" → "BBB", "7A" → "AAAAAAA", bare "C" → "C".
22
+ */
23
+ export declare function decodeRLE(s: string): string;
24
+ export interface LayerData {
25
+ /**
26
+ * 0-based floor index as written in the template (Y0, Y1, …).
27
+ * Absolute world Y = baseY + yIndex.
28
+ */
29
+ yIndex: number;
30
+ /** Fully decoded (RLE-expanded) row strings, one per Z offset */
31
+ rows: string[];
32
+ }
33
+ /**
34
+ * Parses a chunk-template string into an array of LayerData objects.
35
+ *
36
+ * Strict format:
37
+ * Y<n>: row0 | row1 | … | rowN-1 Y<n>: …
38
+ *
39
+ * - Layers are delimited by the next `Y<digits>:` token (no other separator).
40
+ * - `<n>` is a 0-based floor index; the chunk generator maps it to world Y
41
+ * via `baseY + n` (default baseY = 64).
42
+ * - Rows are separated by `|`. Leading/trailing whitespace around each row
43
+ * is stripped before RLE decoding.
44
+ * - Run-length encoding: `3C` → `CCC`, `7A` → `AAAAAAA`, bare `C` → `C`.
45
+ * - `A` (or a space) represents air and is never written to the chunk.
46
+ * - Width and depth are inferred from the decoded rows (no explicit header).
47
+ *
48
+ * @example
49
+ * "Y0: 16C | 16C Y1: C14AC | C14AC"
50
+ */
51
+ export declare function parseTemplate(templateStr: string): LayerData[];
52
+ /**
53
+ * Returns the deterministic spawn point for a chunk-template world.
54
+ * The player is placed one block above the Y0 floor (`baseY + 1`) at a
55
+ * fixed 10-block inset from the template's (0, 0) corner.
56
+ */
57
+ export declare function getTemplateSpawnPoint(options: any): Vec3;
58
+ /**
59
+ * Returns a `(chunkX, chunkZ) => Chunk` generator that paints the decoded
60
+ * template structure into the world starting at (0, baseY, 0).
61
+ *
62
+ * A stone block is guaranteed directly below the spawn position so the
63
+ * player always has something solid to land on regardless of what the
64
+ * template has at that cell.
65
+ *
66
+ * Chunks that do not overlap the structure are returned as empty (air +
67
+ * full sky-light), compatible with every generator contract.
68
+ *
69
+ * @param templateStr Encoded template string (see parseTemplate).
70
+ * @param options generationOptions forwarded from world.ts
71
+ * (must include `version`; may include `minY`,
72
+ * `worldHeight`, `baseY`).
73
+ * @param userBlockMap Optional extra / override char→name mappings on top
74
+ * of DEFAULT_BLOCK_MAP. Accepts a string
75
+ * (`"C=cobblestone, G=glass"`) or a plain object.
76
+ */
77
+ export declare function makeTemplateGenerator(templateStr: string, options: any, userBlockMap?: Record<string, string> | string): (chunkX: number, chunkZ: number) => any;
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_BLOCK_MAP = void 0;
4
+ exports.parseBlockMap = parseBlockMap;
5
+ exports.decodeRLE = decodeRLE;
6
+ exports.parseTemplate = parseTemplate;
7
+ exports.getTemplateSpawnPoint = getTemplateSpawnPoint;
8
+ exports.makeTemplateGenerator = makeTemplateGenerator;
9
+ const vec3_1 = require("vec3");
10
+ /**
11
+ * Default single-character → Minecraft block-name mapping used by the
12
+ * chunk-template generator. Users can override any entry (or add new ones)
13
+ * via `generation.options.blockMap`.
14
+ */
15
+ exports.DEFAULT_BLOCK_MAP = {
16
+ // 'A' is always air (skip – never written to the chunk)
17
+ C: 'cobblestone',
18
+ M: 'mossy_cobblestone',
19
+ G: 'glass',
20
+ R: 'red_wool',
21
+ D: 'dark_oak_planks',
22
+ I: 'iron_block',
23
+ S: 'stone',
24
+ B: 'bricks',
25
+ W: 'oak_planks',
26
+ L: 'lapis_block',
27
+ O: 'obsidian',
28
+ P: 'oak_planks',
29
+ Y: 'gold_block',
30
+ N: 'netherrack',
31
+ T: 'stone_bricks',
32
+ F: 'oak_log',
33
+ E: 'emerald_block',
34
+ X: 'tnt',
35
+ Z: 'dirt',
36
+ H: 'sandstone',
37
+ J: 'glass_pane',
38
+ K: 'gravel',
39
+ Q: 'quartz_block',
40
+ U: 'sea_lantern',
41
+ V: 'stone_slab',
42
+ };
43
+ // ---------------------------------------------------------------------------
44
+ // Block-map parser
45
+ // ---------------------------------------------------------------------------
46
+ /**
47
+ * Parses a block-map override string into a `Record<string, string>`.
48
+ *
49
+ * Format: comma-separated `CHAR=block_name` pairs (whitespace around
50
+ * `=` and `,` is ignored).
51
+ *
52
+ * @example
53
+ * parseBlockMap("C=cobblestone, G=glass_pane, X=diamond_block")
54
+ * // → { C: 'cobblestone', G: 'glass_pane', X: 'diamond_block' }
55
+ */
56
+ function parseBlockMap(s) {
57
+ const result = {};
58
+ for (const entry of s.split(',')) {
59
+ const eq = entry.indexOf('=');
60
+ if (eq === -1)
61
+ continue;
62
+ const char = entry.slice(0, eq).trim();
63
+ const name = entry.slice(eq + 1).trim();
64
+ if (char && name)
65
+ result[char] = name;
66
+ }
67
+ return result;
68
+ }
69
+ // ---------------------------------------------------------------------------
70
+ // RLE decoder
71
+ // ---------------------------------------------------------------------------
72
+ /**
73
+ * Expands a run-length-encoded row string.
74
+ * "3B" → "BBB", "7A" → "AAAAAAA", bare "C" → "C".
75
+ */
76
+ function decodeRLE(s) {
77
+ let result = '';
78
+ let i = 0;
79
+ while (i < s.length) {
80
+ let numStr = '';
81
+ while (i < s.length && s[i] >= '0' && s[i] <= '9') {
82
+ numStr += s[i++];
83
+ }
84
+ if (i < s.length) {
85
+ result += s[i++].repeat(numStr ? parseInt(numStr, 10) : 1);
86
+ }
87
+ }
88
+ return result;
89
+ }
90
+ /**
91
+ * Parses a chunk-template string into an array of LayerData objects.
92
+ *
93
+ * Strict format:
94
+ * Y<n>: row0 | row1 | … | rowN-1 Y<n>: …
95
+ *
96
+ * - Layers are delimited by the next `Y<digits>:` token (no other separator).
97
+ * - `<n>` is a 0-based floor index; the chunk generator maps it to world Y
98
+ * via `baseY + n` (default baseY = 64).
99
+ * - Rows are separated by `|`. Leading/trailing whitespace around each row
100
+ * is stripped before RLE decoding.
101
+ * - Run-length encoding: `3C` → `CCC`, `7A` → `AAAAAAA`, bare `C` → `C`.
102
+ * - `A` (or a space) represents air and is never written to the chunk.
103
+ * - Width and depth are inferred from the decoded rows (no explicit header).
104
+ *
105
+ * @example
106
+ * "Y0: 16C | 16C Y1: C14AC | C14AC"
107
+ */
108
+ function parseTemplate(templateStr) {
109
+ const layers = [];
110
+ // Each match captures: [1] yIndex, [2] the rows blob up to the next Y<n>:
111
+ const re = /Y(\d+):\s*([\s\S]*?)(?=\s*Y\d+:|$)/g;
112
+ let m;
113
+ while ((m = re.exec(templateStr)) !== null) {
114
+ const yIndex = parseInt(m[1], 10);
115
+ const rows = m[2]
116
+ .split('|')
117
+ .map(r => decodeRLE(r.trim()))
118
+ .filter(r => r.length > 0);
119
+ if (rows.length === 0) {
120
+ console.warn(`[chunkTemplate] Y${yIndex}: no rows found, skipping`);
121
+ continue;
122
+ }
123
+ layers.push({ yIndex, rows });
124
+ }
125
+ return layers;
126
+ }
127
+ // ---------------------------------------------------------------------------
128
+ // Chunk generator factory
129
+ // ---------------------------------------------------------------------------
130
+ /** X/Z offset inward from the (0, 0) corner used as the template spawn. */
131
+ const SPAWN_OFFSET = 10;
132
+ /**
133
+ * Returns the deterministic spawn point for a chunk-template world.
134
+ * The player is placed one block above the Y0 floor (`baseY + 1`) at a
135
+ * fixed 10-block inset from the template's (0, 0) corner.
136
+ */
137
+ function getTemplateSpawnPoint(options) {
138
+ const baseY = options.baseY ?? 64;
139
+ return new vec3_1.Vec3(SPAWN_OFFSET, baseY + 1, SPAWN_OFFSET);
140
+ }
141
+ /**
142
+ * Returns a `(chunkX, chunkZ) => Chunk` generator that paints the decoded
143
+ * template structure into the world starting at (0, baseY, 0).
144
+ *
145
+ * A stone block is guaranteed directly below the spawn position so the
146
+ * player always has something solid to land on regardless of what the
147
+ * template has at that cell.
148
+ *
149
+ * Chunks that do not overlap the structure are returned as empty (air +
150
+ * full sky-light), compatible with every generator contract.
151
+ *
152
+ * @param templateStr Encoded template string (see parseTemplate).
153
+ * @param options generationOptions forwarded from world.ts
154
+ * (must include `version`; may include `minY`,
155
+ * `worldHeight`, `baseY`).
156
+ * @param userBlockMap Optional extra / override char→name mappings on top
157
+ * of DEFAULT_BLOCK_MAP. Accepts a string
158
+ * (`"C=cobblestone, G=glass"`) or a plain object.
159
+ */
160
+ function makeTemplateGenerator(templateStr, options, userBlockMap) {
161
+ /* eslint-disable @typescript-eslint/no-require-imports */
162
+ const Chunk = require('prismarine-chunk')(options.version);
163
+ const mcData = require('minecraft-data')(options.version);
164
+ /* eslint-enable @typescript-eslint/no-require-imports */
165
+ const minY = options.minY ?? 0;
166
+ const worldHeight = options.worldHeight ?? 256;
167
+ const baseY = options.baseY ?? 64;
168
+ const theFlattening = mcData.supportFeature('blockStateId');
169
+ // Merge user overrides on top of defaults
170
+ const resolvedMap = typeof userBlockMap === 'string' ? parseBlockMap(userBlockMap) : userBlockMap;
171
+ const blockMap = { ...exports.DEFAULT_BLOCK_MAP, ...resolvedMap };
172
+ // Resolve char → numeric block ID once at construction time
173
+ const charToId = {};
174
+ for (const [char, name] of Object.entries(blockMap)) {
175
+ if (char === 'A')
176
+ continue; // air is always a skip
177
+ const block = mcData.blocksByName[name];
178
+ if (block) {
179
+ charToId[char] = theFlattening ? block.minStateId : block.id;
180
+ }
181
+ else {
182
+ console.warn(`[chunkTemplate] Unknown block name for char '${char}': '${name}'`);
183
+ }
184
+ }
185
+ // Build a sparse "wx,wy,wz" → blockId map covering the whole structure.
186
+ // layer.yIndex is 0-based (Y0 = ground); world Y = baseY + yIndex.
187
+ const blockGrid = new Map();
188
+ for (const layer of parseTemplate(templateStr)) {
189
+ const wy = baseY + layer.yIndex;
190
+ for (let zi = 0; zi < layer.rows.length; zi++) {
191
+ const row = layer.rows[zi];
192
+ for (let xi = 0; xi < row.length; xi++) {
193
+ const char = row[xi];
194
+ if (char === 'A' || char === ' ')
195
+ continue;
196
+ const id = charToId[char];
197
+ if (id !== undefined) {
198
+ blockGrid.set(`${xi},${wy},${zi}`, id);
199
+ }
200
+ else {
201
+ console.warn(`[chunkTemplate] No block mapping for char '${char}'`);
202
+ }
203
+ }
204
+ }
205
+ }
206
+ // Guarantee a solid block directly below the spawn position so the player
207
+ // always has something to land on (stone at Y0 = baseY, spawn is at baseY+1).
208
+ const spawnPlatformId = theFlattening
209
+ ? mcData.blocksByName.stone.minStateId
210
+ : mcData.blocksByName.stone.id;
211
+ blockGrid.set(`${SPAWN_OFFSET},${baseY},${SPAWN_OFFSET}`, spawnPlatformId);
212
+ return (chunkX, chunkZ) => {
213
+ const chunk = new Chunk({ minY, worldHeight });
214
+ // Full sky-light (standard for surface/flat worlds)
215
+ for (let lx = 0; lx < 16; lx++) {
216
+ for (let lz = 0; lz < 16; lz++) {
217
+ for (let ly = minY; ly < minY + worldHeight; ly++) {
218
+ chunk.setSkyLight(new vec3_1.Vec3(lx, ly, lz), 15);
219
+ }
220
+ }
221
+ }
222
+ const wx0 = chunkX * 16;
223
+ const wz0 = chunkZ * 16;
224
+ for (const [key, id] of blockGrid) {
225
+ const parts = key.split(',');
226
+ const wx = parseInt(parts[0], 10);
227
+ const wy = parseInt(parts[1], 10);
228
+ const wz = parseInt(parts[2], 10);
229
+ const lx = wx - wx0;
230
+ const lz = wz - wz0;
231
+ if (lx < 0 || lx >= 16 || lz < 0 || lz >= 16)
232
+ continue;
233
+ if (wy < minY || wy >= minY + worldHeight)
234
+ continue;
235
+ const pos = new vec3_1.Vec3(lx, wy, lz);
236
+ if (theFlattening) {
237
+ chunk.setBlockStateId(pos, id);
238
+ }
239
+ else {
240
+ chunk.setBlockType(pos, id);
241
+ }
242
+ }
243
+ return chunk;
244
+ };
245
+ }
package/dist/modules.d.ts CHANGED
@@ -89,6 +89,47 @@ declare global {
89
89
  version?: string;
90
90
  };
91
91
  };
92
+ /**
93
+ * If provided, this encoded string completely replaces the normal chunk
94
+ * generator for the overworld. Every chunk is filled from the decoded
95
+ * structure; chunks outside the structure bounds are empty (air + sky
96
+ * light).
97
+ *
98
+ * Strict format:
99
+ * Y<n>: row0 | row1 | … | rowN-1 Y<n>: …
100
+ *
101
+ * - `<n>` is a 0-based floor index (Y0 = ground, Y1 = one block up, …).
102
+ * - Rows are separated by `|`; each row is one Z-slice of the floor.
103
+ * - Run-length encoding: `3C` → `CCC`, `7A` → `AAAAAAA`, bare `C` → `C`.
104
+ * - `A` (or a space) represents air and is never written.
105
+ * - Single characters map to block names via `blockMap` (see below).
106
+ * - Width and depth are inferred from the decoded row lengths.
107
+ *
108
+ * @example
109
+ * "Y0: 16C | 16C Y1: C14AC | C14AC"
110
+ */
111
+ chunkTemplate?: string;
112
+ /**
113
+ * World Y coordinate that `Y0` in `chunkTemplate` maps to.
114
+ * `Y<n>` → world Y `baseY + n`.
115
+ * @default 64
116
+ */
117
+ baseY?: number;
118
+ /**
119
+ * Override or extend the default single-character → Minecraft block-name
120
+ * mapping used by `chunkTemplate`.
121
+ *
122
+ * Accepts either an object or a comma-separated string of `CHAR=block_name`
123
+ * pairs: `"C=cobblestone, G=glass_pane, X=diamond_block"`.
124
+ *
125
+ * Default map includes: A=air, C=cobblestone, M=mossy_cobblestone,
126
+ * G=glass, R=red_wool, D=dark_oak_planks, I=iron_block, S=stone,
127
+ * B=bricks, W=oak_planks, T=stone_bricks, Y=gold_block, and more.
128
+ *
129
+ * @example "C=cobblestone, G=glass_pane, X=diamond_block"
130
+ * @example { X: 'diamond_block', Z: 'emerald_block' }
131
+ */
132
+ blockMap?: Record<string, string> | string;
92
133
  kickTimeout?: number;
93
134
  plugins?: Record<string, any>;
94
135
  modpe?: boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zardoy/flying-squid",
3
3
  "description": "A minecraft server written in TS",
4
- "version": "0.0.120",
4
+ "version": "0.0.122",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",